From 62d3f2c7262bf9852c47fcc3c63cc9858a46a076 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Fri, 26 Nov 2021 13:39:38 -0600 Subject: [PATCH] More semantics --- src/Makefile | 2 +- src/parser.y | 20 +++++++++++--------- src/syntax.c | 15 +++++++++++++++ src/syntax.h | 32 +++++++++++++++++++++++--------- 4 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 src/syntax.c diff --git a/src/Makefile b/src/Makefile index abf58de..d7b8cec 100644 --- a/src/Makefile +++ b/src/Makefile @@ -11,7 +11,7 @@ parser.h parser.o: parser.y $(CC) -c parser.c rm parser.c -deelang: deelang.c lexer.o parser.o +deelang: deelang.c lexer.o parser.o syntax.o $(CC) -o $@ $^ clean: diff --git a/src/parser.y b/src/parser.y index 54efb69..720a4d0 100644 --- a/src/parser.y +++ b/src/parser.y @@ -14,6 +14,7 @@ int yyerror(const char* p) { fprintf(stderr, p); } num_t num; char *sym; string_t string; + object_t object; } %token ID @@ -31,6 +32,7 @@ int yyerror(const char* p) { fprintf(stderr, p); } %left MAPS %nterm expr +%nterm objdef %% program: statements | statements statement; @@ -42,16 +44,16 @@ assignment: ID GETS expr; assignments: assignments assignment STOP | // null production; expr: funcdef | funcall - | objdef - | expr CAT expr - | expr '+' expr - | expr '-' expr - | expr '*' expr - | expr '/' expr + | objdef { $$.type = DEE_OBJ; $$.object = $1;} + | expr CAT expr { $$ = make_binop(CAT, $1, $3); } + | expr '+' expr { $$ = make_binop('+', $1, $3); } + | expr '-' expr { $$ = make_binop('-', $1, $3); } + | expr '*' expr { $$ = make_binop('*', $1, $3); } + | expr '/' expr { $$ = make_binop('/', $1, $3); } | '(' expr ')' {$$ = $2;} - | ID {$$.id = $1; $$.type = ID;} - | NUM {$$.num = $1; $$.type = NUM;} - | STRING {$$.string = $1; $$.type = STRING;}; + | ID {$$.id = $1; $$.type = DEE_ID;} + | NUM {$$.num = $1; $$.type = DEE_NUM;} + | STRING {$$.string = $1; $$.type = DEE_STR;}; funcdef: param MAPS expr | param MAPS block; param: ID | '_' | // null production; diff --git a/src/syntax.c b/src/syntax.c new file mode 100644 index 0000000..0674438 --- /dev/null +++ b/src/syntax.c @@ -0,0 +1,15 @@ +#include +#include + +#include "syntax.h" + +expr_t make_binop(int op, expr_t first, expr_t second) { + expr_t myexpr; + myexpr.type = DEE_BINOP; + myexpr.binop.op = op; + myexpr.binop.first = (expr_t*) malloc(sizeof(expr_t)); + myexpr.binop.second = (expr_t*) malloc(sizeof(expr_t)); + memcpy(myexpr.binop.first, &first, sizeof(expr_t)); + memcpy(myexpr.binop.second, &second, sizeof(expr_t)); + return myexpr; +} diff --git a/src/syntax.h b/src/syntax.h index 523e208..31ba4fc 100644 --- a/src/syntax.h +++ b/src/syntax.h @@ -6,16 +6,15 @@ typedef char* atom_t; typedef char* string_t; typedef struct expr_t expr_t; typedef struct binop_t binop_t; +typedef struct object_t object_t; +typedef struct function_t function_t; +typedef struct block_t block_t; -struct expr_t { - int type; - union { - atom_t id; - num_t num; - string_t string; - binop_t *binop; - }; -}; +#define DEE_BINOP 1 +#define DEE_ID 2 +#define DEE_NUM 3 +#define DEE_STR 4 +#define DEE_OBJ 5 struct binop_t { expr_t *first; @@ -23,4 +22,19 @@ struct binop_t { int op; }; +struct object_t {}; + +struct expr_t { + int type; + union { + atom_t id; + num_t num; + string_t string; + binop_t binop; + object_t object; + }; +}; + +expr_t make_binop(int, expr_t, expr_t); + #endif /* SYNTAX_H */