-
Notifications
You must be signed in to change notification settings - Fork 0
/
mml_parser.y
233 lines (179 loc) · 10.5 KB
/
mml_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
%{
//-- don't change *any* of these: if you do, you'll break the compiler.
#include <algorithm>
#include <memory>
#include <cstring>
#include <cdk/compiler.h>
#include <cdk/types/types.h>
#include ".auto/all_nodes.h"
#define LINE compiler->scanner()->lineno()
#define yylex() compiler->scanner()->scan()
#define yyerror(compiler, s) compiler->scanner()->error(s)
//-- don't change *any* of these --- END!
%}
%parse-param {std::shared_ptr<cdk::compiler> compiler}
%union {
//--- don't change *any* of these: if you do, you'll break the compiler.
YYSTYPE() : type(cdk::primitive_type::create(0, cdk::TYPE_VOID)) {}
~YYSTYPE() {}
YYSTYPE(const YYSTYPE &other) { *this = other; }
YYSTYPE& operator=(const YYSTYPE &other) { type = other.type; return *this; }
//-- don't change *any* of these --- END!
double d;
int i; /* integer value */
std::string *s; /* symbol name or string literal */
cdk::basic_node *node; /* node pointer */
cdk::sequence_node *sequence;
cdk::expression_node *expression; /* expression nodes */
cdk::lvalue_node *lvalue;
std::shared_ptr<cdk::basic_type> type; /* expression type */
mml::block_node *block;
std::vector< std::shared_ptr<cdk::basic_type>> *functype;
};
%token <i> tINTEGER
%token <d> tDOUBLE
%token <s> tIDENTIFIER tSTRING
%token tWHILE tIF tPRINTLINE tREAD tBEGIN tEND tRETURN tELIF tELSE
%token tOR tAND tFE tPUBLIC tNEXT tSTOP
%token tFOREIGN tFORWARD tAUTO tINT tTYPE_TEXT tTYPE_DOUBLE tTYPE_VOID tTYPE_AUTO tSIZEOF
%nonassoc tIFX
%nonassoc tELSE tIF tELIF tWHILE
%nonassoc '~'
%right '='
%left tGE tLE tEQ tNE '>' '<'
%left '+' '-'
%left '*' '/' '%'
%left tOR tAND
%nonassoc tUNARY
%nonassoc tUMINUS
%nonassoc '('
%type <node> stmt program iffalse whiledec
%type <sequence> list exprs
%type <expression> expr
%type <lvalue> lval
%type <s> string
%type<sequence> file seqdecl vardecs opt_vardecs func_args
%type<node> decl vardec func_arg
%type<type> dtype
%type<block> block block_stmts
%type<functype> func_type opt_data_type
%{
//-- The rules below will be included in yyparse, the main parsing function.
%}
%%
file : /* empty */ {$$ = new cdk::sequence_node(LINE); }
| seqdecl { $$ = $1; }
| seqdecl program {$$ = new cdk::sequence_node(LINE,$2,$1);}
| program { $$ = new cdk::sequence_node(LINE, $1);}
;
program : tBEGIN block_stmts tEND { compiler->ast(new mml::program_node(LINE, $2)); }
;
list : stmt { $$ = new cdk::sequence_node(LINE, $1); }
| list stmt { $$ = new cdk::sequence_node(LINE, $2, $1); }
;
seqdecl: decl { $$ = new cdk::sequence_node(LINE, $1);}
| seqdecl decl { $$ = new cdk::sequence_node(LINE, $2, $1);}
;
decl : vardec ';' { $$ = $1;}
;
vardec : dtype tIDENTIFIER { $$ = new mml::declaration_node(LINE, false, false, false, false, $1, *$2, nullptr); delete $2; }
| dtype tIDENTIFIER '=' expr { $$ = new mml::declaration_node(LINE, false, false, false, false, $1, *$2, $4); delete $2; }
| tTYPE_AUTO tIDENTIFIER '=' expr { $$ = new mml::declaration_node(LINE, false, false, false, true, nullptr, *$2, $4); delete $2; }
| tFORWARD dtype tIDENTIFIER { $$ = new mml::declaration_node(LINE, true, false, false, false, $2, *$3, nullptr); delete $3; }
| tPUBLIC dtype tIDENTIFIER { $$ = new mml::declaration_node(LINE, false, false, true, false, $2, *$3, nullptr); delete $3; }
| tPUBLIC dtype tIDENTIFIER '=' expr { $$ = new mml::declaration_node(LINE, false, false, true, false, $2, *$3, $5); delete $3; }
| tPUBLIC tIDENTIFIER '=' expr { $$ = new mml::declaration_node(LINE, false, false, true, false, nullptr, *$2, $4); delete $2; }
| tPUBLIC tTYPE_AUTO tIDENTIFIER '=' expr { $$ = new mml::declaration_node(LINE, false, false, true, false, nullptr, *$3, $5); delete $3; }
| tFOREIGN dtype tIDENTIFIER { $$ = new mml::declaration_node(LINE, false, true, false, false, $2, *$3, nullptr); delete $3; }
;
vardecs : vardec ';' { $$ = new cdk::sequence_node(LINE, $1); }
| vardecs vardec ';' { $$ = new cdk::sequence_node(LINE, $2, $1); }
;
opt_vardecs : /* empty */ { $$ = NULL; }
| vardecs { $$ = $1; }
;
opt_data_type: dtype { $$ = new std::vector<std::shared_ptr<cdk::basic_type>>(); $$->push_back($1);}
| opt_data_type ',' dtype { $$ = $1; $$->push_back($3);}
;
dtype : tINT { $$ = cdk::primitive_type::create(4, cdk::TYPE_INT); }
| tTYPE_TEXT { $$ = cdk::primitive_type::create(4, cdk::TYPE_STRING); }
| tTYPE_DOUBLE { $$ = cdk::primitive_type::create(8, cdk::TYPE_DOUBLE); }
| tTYPE_VOID { $$ = cdk::primitive_type::create(0, cdk::TYPE_VOID); }
| '[' dtype ']' { $$ = cdk::reference_type::create(4, $2); }
| func_type '<' opt_data_type '>' { $$ = cdk::functional_type::create(*$3,*$1); }
| func_type '<' '>' { $$ = cdk::functional_type::create(*(new std::vector<std::shared_ptr<cdk::basic_type>>()),*$1); }
;
func_type: dtype {$$=new std::vector<std::shared_ptr<cdk::basic_type>>(); $$->push_back($1);}
;
func_args : func_arg { $$ = new cdk::sequence_node(LINE, $1); }
| func_args ',' func_arg { $$ = new cdk::sequence_node(LINE, $3, $1); }
| /*empty*/ { $$ = new cdk::sequence_node(LINE); }
;
func_arg : dtype tIDENTIFIER { $$ = new mml::declaration_node(LINE, false, false, false, true, $1, *$2, nullptr); }
;
block_stmts : opt_vardecs list { $$ = new mml::block_node(LINE, $1, $2); }
| opt_vardecs { $$ = new mml::block_node(LINE, $1, nullptr); }
;
block : '{' block_stmts '}' {$$ = $2;}
;
stmt : expr ';' { $$ = new mml::evaluation_node(LINE, $1); }
| exprs tPRINTLINE { $$ = new mml::print_node(LINE, $1, true); }
| exprs '!' { $$ = new mml::print_node(LINE, $1, false); }
| tREAD lval ';' { $$ = new mml::read_node(LINE, $2); }
| whiledec { $$ = $1; }
| tIF '(' expr ')' stmt %prec tIFX { $$ = new mml::if_node(LINE, $3, $5); }
| tIF '(' expr ')' stmt iffalse { $$ = new mml::if_else_node(LINE, $3, $5, $6); }
| '{' block_stmts '}' { $$ = $2; }
| tRETURN expr ';' { $$ = new mml::return_node(LINE, $2); }
| tRETURN ';' { $$ = new mml::return_node(LINE); }
| tSTOP tINTEGER ';' { $$ = new mml::stop_node(LINE, $2); }
| tSTOP ';' { $$ = new mml::stop_node(LINE, 1); }
| tNEXT tINTEGER ';' { $$ = new mml::next_node(LINE, $2); }
| tNEXT ';' { $$ = new mml::next_node(LINE,1); }
;
iffalse : tELSE stmt { $$ = $2; }
| tELIF '(' expr ')' stmt %prec tIFX { $$ = new mml::if_node(LINE, $3, $5); }
| tELIF '(' expr ')' stmt iffalse { $$ = new mml::if_else_node(LINE, $3, $5, $6); }
;
whiledec : tWHILE '(' expr ')' block { $$ = new mml::while_node(LINE,$3,$5); }
exprs : { $$ = new cdk::sequence_node(LINE); }
| expr { $$ = new cdk::sequence_node(LINE, $1); }
| exprs ',' expr { $$ = new cdk::sequence_node(LINE, $3, $1); }
;
expr : tINTEGER { $$ = new cdk::integer_node(LINE, $1); }
| tDOUBLE { $$ = new cdk::double_node(LINE, $1); }
| string { $$ = new cdk::string_node(LINE, $1); delete $1; }
| '-' expr %prec tUNARY { $$ = new cdk::neg_node(LINE, $2); }
| expr '+' expr { $$ = new cdk::add_node(LINE, $1, $3); }
| expr '-' expr { $$ = new cdk::sub_node(LINE, $1, $3); }
| expr '*' expr { $$ = new cdk::mul_node(LINE, $1, $3); }
| expr '/' expr { $$ = new cdk::div_node(LINE, $1, $3); }
| expr '%' expr { $$ = new cdk::mod_node(LINE, $1, $3); }
| expr '<' expr { $$ = new cdk::lt_node(LINE, $1, $3); }
| expr '>' expr { $$ = new cdk::gt_node(LINE, $1, $3); }
| expr tOR expr { $$ = new cdk::or_node(LINE, $1, $3); }
| expr tAND expr { $$ = new cdk::and_node(LINE, $1, $3); }
| expr tGE expr { $$ = new cdk::ge_node(LINE, $1, $3); }
| expr tLE expr { $$ = new cdk::le_node(LINE, $1, $3); }
| expr tNE expr { $$ = new cdk::ne_node(LINE, $1, $3); }
| expr tEQ expr { $$ = new cdk::eq_node(LINE, $1, $3); }
| tSIZEOF '(' expr ')' { $$ = new mml::size_of_node(LINE, $3); }
| '(' func_args ')' tFE dtype block { $$ = new mml::function_definition_node(LINE, $2, $5, $6); }
| expr '(' exprs ')' { $$ = new mml::function_call_node(LINE, $1, $3); }
| '@' '(' exprs ')' { $$ = new mml::function_call_node(LINE, nullptr, $3); }
| '+' expr %prec tUMINUS { $$ = $2; }
| '~' expr { $$ = new cdk::not_node(LINE, $2);}
| '(' expr ')' { $$ = $2; }
| lval { $$ = new cdk::rvalue_node(LINE, $1); }
| lval '=' expr { $$ = new cdk::assignment_node(LINE, $1, $3); }
| '[' expr ']' { $$ = new mml::alloc_node(LINE, $2); }
| lval '?' { $$ = new mml::read_node(LINE, $1); }
;
lval : tIDENTIFIER { $$ = new cdk::variable_node(LINE, $1); }
| lval '[' expr ']' { $$ = new mml::index_node(LINE, new cdk::rvalue_node(LINE, $1), $3); }
| '(' expr ')' '[' expr ']' { $$ = new mml::index_node(LINE, $2, $5); }
;
string : tSTRING { $$ = $1; }
| string tSTRING { $$ = $1; $$->append(*$2); delete $2; }
;
%%