-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
110 lines (88 loc) · 3.48 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
%{
#include <stdio.h>
#include <stdlib.h>
#include <parser_helper.h>
ast_stmt_t* prog;
extern int yylex();
extern int linenum;
void yyerror(const char* s) {
printf("Error (line %d) - %s\n", linenum, s);
}
%}
%union {
ast_stmt_t* stmt;
ast_expr_t* expr;
ast_binop_t* binop;
ast_ident_t* ident;
ast_decl_t* decl;
ast_assign_t* assign;
ast_block_t* block;
ast_cond_t* cond;
ast_while_t* whil;
char* string;
int token;
}
%debug
%error-verbose
%token <token> TLET TMUT TEQUAL TSTMT TPLUS TMINUS TMULTIPLY TDIVIDE TMOD TLPAREN TRPAREN
%token <token> TCOLON TLBRACE TRBRACE TTRUE TFALSE TEQUALTO TNOTEQUAL TLESSTHAN TGREATERTHAN
%token <token> TLESSOREQ TGREATEROREQ TIF TELSE TWHILE TCONTINUE
%token <string> TIDENTIFIER TTYPE
%token <string> TINTEGER
%type <stmt> program stmts stmt
%type <assign> var_assign
%type <decl> var_decl var_decl_typed
%type <expr> expr additive factor term
%type <ident> ident
%type <block> block
%type <cond> ifblock
%type <whil> whileblock
%%
program : stmts { prog = $1; }
block : TLBRACE stmts TRBRACE { $$ = block_stmt($2); }
| TLBRACE stmts expr TRBRACE { $$ = block_stmt_expr($2, $3); }
| TLBRACE expr TRBRACE { $$ = block_stmt_expr(NULL, $2); }
stmts : stmt { $$ = $1; }
| stmts stmt { $$ = statement_append($1, $2); }
stmt : var_assign TSTMT { $$ = statement_assign($1); }
| expr TSTMT { $$ = statement_expr($1); }
| var_decl_typed TSTMT { $$ = statement_decl($1); }
| ifblock { $$ = statement_conditional($1); }
| block { $$ = statement_block($1); }
| whileblock { $$ = statement_whileloop($1); }
| TCONTINUE TSTMT { $$ = statement_cont(); }
whileblock : TWHILE expr block { $$ = whileloop($2, $3); }
ifblock : TIF expr block { $$ = conditional($2, $3); }
| TIF expr block TELSE block { $$ = conditional_else($2, $3, $5); }
| TIF expr block TELSE ifblock { $$ = conditional_elif($2, $3, $5); }
var_assign : var_decl TEQUAL expr { $$ = assignment_decl($1, $3); }
| var_decl_typed TEQUAL expr { $$ = assignment_decl($1, $3); }
| ident TEQUAL expr { $$ = assignment_ident($1, $3); }
var_decl_typed : TLET ident TCOLON TTYPE { $$ = declaration_type($2, 0, $4); }
| TLET TMUT ident TCOLON TTYPE { $$ = declaration_type($3, 1, $5); }
var_decl : TLET ident { $$ = declaration($2, 0); }
| TLET TMUT ident { $$ = declaration($3, 1); }
ident : TIDENTIFIER { $$ = identifier($1); }
expr : additive { $$ = $1; }
| additive TEQUALTO expr { $$ = expression_binop(equalto($1, $3)); }
| additive TNOTEQUAL expr { $$ = expression_binop(notequal($1, $3)); }
| additive TLESSTHAN expr { $$ = expression_binop(lessthan($1, $3)); }
| additive TGREATERTHAN expr { $$ = expression_binop(greaterthan($1, $3)); }
| additive TLESSOREQ expr { $$ = expression_binop(lessoreq($1, $3)); }
| additive TGREATEROREQ expr { $$ = expression_binop(greateroreq($1, $3)); }
| block { $$ = expression_block($1); }
| ifblock { $$ = expression_if($1); }
additive : factor { $$ = $1; }
| factor TPLUS expr { $$ = expression_binop(addition($1, $3)); }
| factor TMINUS expr { $$ = expression_binop(subtraction($1, $3)); }
factor : term { $$ = $1; }
| term TMULTIPLY factor { $$ = expression_binop(multiplication($1, $3)); }
| term TDIVIDE factor { $$ = expression_binop(division($1, $3)); }
| term TMOD factor { $$ = expression_binop(modulo($1, $3)); }
term : TINTEGER { $$ = expression_int(atoi($1)); }
| TTRUE { $$ = expression_bool(1); }
| TFALSE { $$ = expression_bool(0); }
| TMINUS TINTEGER { $$ = expression_int(-atoi($2)); }
| ident { $$ = expression_ident($1); }
| TLPAREN expr TRPAREN { $$ = $2; }
%%