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
rm parser.c
deelang: deelang.c lexer.o parser.o
deelang: deelang.c lexer.o parser.o syntax.o
$(CC) -o $@ $^
clean:

View File

@ -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> ID
@ -31,6 +32,7 @@ int yyerror(const char* p) { fprintf(stderr, p); }
%left MAPS
%nterm <expr> expr
%nterm <object> 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;

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 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 */