From 4aff13c33296097848973f1c7e974ef6d7d160d6 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Fri, 26 Nov 2021 13:04:08 -0600 Subject: [PATCH] Begin semantics --- src/deelang.c | 1 + src/lexer.l | 1 + src/parser.y | 23 ++++++++++++++++------- src/syntax.h | 20 ++++++++++++++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/deelang.c b/src/deelang.c index 9f94509..cc16030 100644 --- a/src/deelang.c +++ b/src/deelang.c @@ -1,3 +1,4 @@ +#include "syntax.h" #include "lexer.h" #include "parser.h" diff --git a/src/lexer.l b/src/lexer.l index 0c8fc1a..8c73416 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -1,4 +1,5 @@ %{ +#include "syntax.h" #include "parser.h" int stack[100]; // Max indentation depth int sp = -1; diff --git a/src/parser.y b/src/parser.y index 47424e9..54efb69 100644 --- a/src/parser.y +++ b/src/parser.y @@ -8,14 +8,19 @@ int yyerror(const char* p) { fprintf(stderr, p); } %define parse.trace %union { - char* sym; - float num; + expr_t expr; + binop_t binop; + atom_t id; + num_t num; + char *sym; + string_t string; } -%token ID -%token STOP +%token ID %token NUM -%token STRING +%token STRING + +%token STOP %token INDENT DEDENT %token IF ELIF ELSE @@ -25,6 +30,8 @@ int yyerror(const char* p) { fprintf(stderr, p); } %left '/' '*' %left MAPS +%nterm expr + %% program: statements | statements statement; statements: statements statement STOP @@ -41,8 +48,10 @@ expr: funcdef | expr '-' expr | expr '*' expr | expr '/' expr - | '(' expr ')' - | ID | NUM | STRING; + | '(' expr ')' {$$ = $2;} + | ID {$$.id = $1; $$.type = ID;} + | NUM {$$.num = $1; $$.type = NUM;} + | STRING {$$.string = $1; $$.type = STRING;}; funcdef: param MAPS expr | param MAPS block; param: ID | '_' | // null production; diff --git a/src/syntax.h b/src/syntax.h index 5d753ee..523e208 100644 --- a/src/syntax.h +++ b/src/syntax.h @@ -1,6 +1,26 @@ #ifndef SYNTAX_H #define SYNTAX_H +typedef float num_t; +typedef char* atom_t; +typedef char* string_t; +typedef struct expr_t expr_t; +typedef struct binop_t binop_t; +struct expr_t { + int type; + union { + atom_t id; + num_t num; + string_t string; + binop_t *binop; + }; +}; + +struct binop_t { + expr_t *first; + expr_t *second; + int op; +}; #endif /* SYNTAX_H */