More semantics

This commit is contained in:
Dane Johnson 2021-11-26 13:39:38 -06:00
parent 4aff13c332
commit 62d3f2c726
4 changed files with 50 additions and 19 deletions

View File

@ -11,7 +11,7 @@ parser.h parser.o: parser.y
$(CC) -c parser.c $(CC) -c parser.c
rm parser.c rm parser.c
deelang: deelang.c lexer.o parser.o deelang: deelang.c lexer.o parser.o syntax.o
$(CC) -o $@ $^ $(CC) -o $@ $^
clean: clean:

View File

@ -14,6 +14,7 @@ int yyerror(const char* p) { fprintf(stderr, p); }
num_t num; num_t num;
char *sym; char *sym;
string_t string; string_t string;
object_t object;
} }
%token <id> ID %token <id> ID
@ -31,6 +32,7 @@ int yyerror(const char* p) { fprintf(stderr, p); }
%left MAPS %left MAPS
%nterm <expr> expr %nterm <expr> expr
%nterm <object> objdef
%% %%
program: statements | statements statement; program: statements | statements statement;
@ -42,16 +44,16 @@ assignment: ID GETS expr;
assignments: assignments assignment STOP | // null production; assignments: assignments assignment STOP | // null production;
expr: funcdef expr: funcdef
| funcall | funcall
| objdef | objdef { $$.type = DEE_OBJ; $$.object = $1;}
| expr CAT expr | expr CAT expr { $$ = make_binop(CAT, $1, $3); }
| expr '+' expr | expr '+' expr { $$ = make_binop('+', $1, $3); }
| expr '-' expr | expr '-' expr { $$ = make_binop('-', $1, $3); }
| expr '*' expr | expr '*' expr { $$ = make_binop('*', $1, $3); }
| expr '/' expr | expr '/' expr { $$ = make_binop('/', $1, $3); }
| '(' expr ')' {$$ = $2;} | '(' expr ')' {$$ = $2;}
| ID {$$.id = $1; $$.type = ID;} | ID {$$.id = $1; $$.type = DEE_ID;}
| NUM {$$.num = $1; $$.type = NUM;} | NUM {$$.num = $1; $$.type = DEE_NUM;}
| STRING {$$.string = $1; $$.type = STRING;}; | STRING {$$.string = $1; $$.type = DEE_STR;};
funcdef: param MAPS expr | param MAPS block; funcdef: param MAPS expr | param MAPS block;
param: ID | '_' | // null production; param: ID | '_' | // null production;

15
src/syntax.c Normal file
View File

@ -0,0 +1,15 @@
#include <string.h>
#include <stdlib.h>
#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;
}

View File

@ -6,16 +6,15 @@ typedef char* atom_t;
typedef char* string_t; typedef char* string_t;
typedef struct expr_t expr_t; typedef struct expr_t expr_t;
typedef struct binop_t binop_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 { #define DEE_BINOP 1
int type; #define DEE_ID 2
union { #define DEE_NUM 3
atom_t id; #define DEE_STR 4
num_t num; #define DEE_OBJ 5
string_t string;
binop_t *binop;
};
};
struct binop_t { struct binop_t {
expr_t *first; expr_t *first;
@ -23,4 +22,19 @@ struct binop_t {
int op; 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 */ #endif /* SYNTAX_H */