forked from ClaudiuIoanMaftei/verse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimbaj.ypp
384 lines (344 loc) · 9.36 KB
/
limbaj.ypp
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
%{
#include <stdio.h>
#include "include/types.h"
#include "include/AST.h"
#include "include/Execute.h"
#define YYDEBUG 1
extern FILE* yyin;
extern char* yytext;
extern int yylineno;
int yylex(void);
%}
%define parse.error verbose
%union {
AS_TREE* treevalue;
int* intvalue;
bool* boolvalue;
std::string* strvalue;
Operation optype;
}
%token UPON NOT SINCE FROM THEN CHANGES INCREASES DECREASES WHENEVER DOES NUMERIC VERBOSE
%token <strvalue>ID TELLING FIXED
%token INT_TYPE STR_TYPE BOOLEAN_TYPE NONE ONE
%token <intvalue>NUMBER <strvalue>STRING <boolvalue>BOOL_TRUE <boolvalue>BOOL_FALSE
%token QUOTE SUMMON
%token DIVIDING REMINDING ADDING CONSPIRING EMPOWERING
%token DIVIDE ADDED EMPOWER CONSPIRED REMIND ASCEND DESCEND
%token BOOL_IS BOOL_UP BOOL_TO BOOL_DOWN BOOL_LOWER BOOL_GREATER BOOL_THAN
%token FLOW_BREAK FLOW_FREE FLOW_CONTINUE EXEUNT
%token DESCRIBE ADVENTURE AS OF CRY FOR
%token ASK WHETHER OTHERWISE
%token BE ENTER PROCLAIM LET THERE
%token BOOK CHAPTER WRITE ERASE PAGE ENLIST LENGTH
%type <treevalue>statements
%type <treevalue>statement
%type <treevalue>complex_identifier
%type <strvalue>var_type
%type <treevalue>assign
%type <treevalue>declaration
%type <treevalue>struct_decl
%type <treevalue>expression
%type <treevalue>input
%type <treevalue>output
%type <treevalue>if_statement
%type <treevalue>while_statement
%type <treevalue>for_statement
%type <treevalue>flow_break
%type <treevalue>function_decl
%type <treevalue>function_decl_item
%type <treevalue>function_decl_list
%type <treevalue>function_call
%type <treevalue>expression_list
%type <treevalue>verbose
%type <treevalue>numeric
%type <treevalue>to_list
%type <treevalue>length_of
%left ADDING CONSPIRING
%left EMPOWERING REMINDING DIVIDING
%nonassoc BOOL_IS BOOL_UP BOOL_TO BOOL_DOWN BOOL_LOWER BOOL_GREATER BOOL_THAN NOT
%start programm
%%
programm:
statements { executeStatements($1); }
;
statements:
statement
{
$$ = make_statements(nullptr, $1);
}
| statements statement
{
$$ = make_statements($1, $2);
}
;
statement:
expression { $$ = $1; }
| declaration { $$ = $1; }
| struct_decl { $$ = $1; }
| assign { $$ = $1; }
| output { $$ = $1; }
| if_statement { $$ = $1; }
| while_statement { $$ = $1; }
| for_statement { $$ = $1; }
| flow_break { $$ = $1; }
| function_decl { $$ = $1; }
;
complex_identifier:
ID
{
$$ = make_identifier(nullptr, new std::string(STR($1)));
}
| ID FROM complex_identifier
{
$$ = make_identifier($3, new std::string(STR($1)));
}
| CHAPTER expression OF complex_identifier
{
$$ = make_indexer($4, $2);
}
;
var_type:
INT_TYPE
{
$$ = make_type("$INT");
}
| STR_TYPE
{
$$ = make_type("$STR");
}
| BOOLEAN_TYPE
{
$$ = make_type("$BOOL");
}
| BOOK
{
$$ = make_type("$LIST");
}
| ID
{
$$ = make_type(STR($1));
}
;
assign:
LET complex_identifier BE expression
{
$$ = make_assignment($2, $4);
}
| LET complex_identifier ASCEND
{
$$ = make_incdec($2, make_value(new std::string("$INT"), new int(1)), Operation::OP_ADDING);
}
| LET complex_identifier DESCEND
{
$$ = make_incdec($2, make_value(new std::string("$INT"), new int(1)), Operation::OP_CONSPIRING);
}
| LET THERE BE expression ADDED BOOL_TO complex_identifier
{
$$ = make_incdec($7, $4, Operation::OP_ADDING);
}
| LET THERE BE expression CONSPIRED FROM complex_identifier
{
$$ = make_incdec($7, $4, Operation::OP_CONSPIRING);
}
| LET THERE BE expression DIVIDING complex_identifier
{
$$ = make_incdec($6, $4, Operation::OP_DIVIDING);
}
| LET THERE BE expression EMPOWERING complex_identifier
{
$$ = make_incdec($6, $4, Operation::OP_EMPOWERING);
}
| LET THERE BE expression REMINDING complex_identifier
{
$$ = make_incdec($6, $4, Operation::OP_REMINDING);
}
| WRITE CHAPTER expression BOOL_TO complex_identifier
{
$$ = make_list_alter($3, nullptr, $5, Type::LIST_ADD);
}
| WRITE expression AS CHAPTER expression BOOL_TO complex_identifier
{
$$ = make_list_alter($2, $5, $7, Type::LIST_ADD);
}
| ERASE CHAPTER expression OF complex_identifier
{
$$ = make_list_alter(nullptr, $3, $5, Type::LIST_REMOVE);
}
;
declaration:
ENTER var_type ID
{
$$ = make_variable($2, new std::string(STR($3)), false, nullptr);
}
| ENTER FIXED var_type ID
{
$$ = make_variable($3, new std::string(STR($4)), true, nullptr);
}
| ENTER var_type ID TELLING expression
{
$$ = make_variable($2, new std::string(STR($3)), false, $5);
}
| ENTER FIXED var_type ID TELLING expression
{
$$ = make_variable($3, new std::string(STR($4)), true, $6);
}
;
struct_decl:
PROCLAIM ID '<' function_decl_list '>'
{
$$ = make_struct_declaration(new std::string(STR($2)), $4);
}
;
expression:
'<' expression '>'
{
$$ = $2;
}
| expression ADDING expression { $$ = make_expression($1, $3, Operation::OP_ADDING);}
| expression CONSPIRING expression { $$ = make_expression($1, $3, Operation::OP_CONSPIRING);}
| expression DIVIDING expression { $$ = make_expression($1, $3, Operation::OP_DIVIDING); }
| expression REMINDING expression { $$ = make_expression($1, $3, Operation::OP_REMINDING);}
| expression EMPOWERING expression { $$ = make_expression($1, $3, Operation::OP_EMPOWERING);}
| expression BOOL_IS expression { $$ = make_expression($1, $3, Operation::OP_EQUAL);}
| expression BOOL_IS NOT expression { $$ = make_expression($1, $4, Operation::OP_NEQUAL);}
| expression BOOL_LOWER BOOL_THAN expression { $$ = make_expression($1, $4, Operation::OP_LOWER);}
| expression BOOL_GREATER BOOL_THAN expression { $$ = make_expression($1, $4, Operation::OP_GREATER);}
| expression BOOL_UP BOOL_TO BOOL_LOWER BOOL_THAN expression { $$ = make_expression($1, $6, Operation::OP_LOWEREQ);}
| expression BOOL_DOWN BOOL_TO BOOL_GREATER BOOL_THAN expression { $$ = make_expression($1, $6, Operation::OP_GREATEREQ);}
| complex_identifier { $$ = make_value($1); }
| NONE { $$ = make_value(new std::string("$INT"), new int(0)); }
| ONE { $$ = make_value(new std::string("$INT"), new int(1)); }
| NUMBER { $$ = make_value(new std::string("$INT"), new int(INT($1))); }
| STRING { $$ = make_value(new std::string("$STR"), new std::string(STR($1))); }
| BOOL_FALSE { $$ = make_value(new std::string("$BOOL"), new bool(BOOL($1))); }
| BOOL_TRUE { $$ = make_value(new std::string("$BOOL"), new bool(BOOL($1))); }
| BOOK '<' expression_list '>' { $$ = make_value(new std::string("$LIST"), $3); }
| BOOK '<' '>' { $$ = make_value(new std::string("$LIST"), nullptr); }
| input { $$ = $1; }
| verbose { $$ = $1; }
| numeric { $$ = $1; }
| to_list { $$ = $1; }
| length_of { $$ = $1; }
| function_call { $$ = $1; }
;
output:
QUOTE '<' expression '>' { $$ = make_output($3); }
;
input:
SUMMON '<' complex_identifier '>'
{
$$ = make_input($3);
}
;
verbose:
VERBOSE '<' expression '>'
{
$$ = make_conversion($3, Type::CONV_VERBOSE);
}
;
numeric:
NUMERIC '<' expression '>'
{
$$ = make_conversion($3, Type::CONV_NUMERIC);
}
;
to_list:
ENLIST '<' expression '>'
{
$$ = make_conversion($3, Type::CONV_LIST);
}
;
length_of:
LENGTH OF '<' expression '>'
{
$$ = make_length($4);
}
;
if_statement:
ASK WHETHER '<' expression '>' THEN '<' statements '>'
{
$$ = make_if($4, $8, nullptr);
}
| ASK WHETHER '<' expression '>' THEN '<' statements '>' OTHERWISE '<' statements '>'
{
$$ = make_if($4, $8, $12);
}
;
while_statement:
SINCE expression '<' statements '>'
{
$$ = make_while($2, $4);
}
;
for_statement:
FOR assign ',' SINCE expression ',' assign '<' statements '>'
{
$$ = make_for($2, $5, $7, $9);
}
;
flow_break:
EXEUNT expression '!' { $$ = make_flow_break(Type::RETURN, $2); }
| EXEUNT '!' { $$ = make_flow_break(Type::RETURN, nullptr); }
| FLOW_BREAK FLOW_FREE { $$ = make_flow_break(Type::BREAK, nullptr); }
| FLOW_CONTINUE { $$ = make_flow_break(Type::CONTINUE, nullptr); }
;
function_decl_item:
var_type ID
{
$$ = make_function_decl_item(new std::string(STR($1)), new std::string(STR($2)), false);
}
| FIXED var_type ID
{
// TODO Should have value
$$ = make_function_decl_item(new std::string(STR($2)), new std::string(STR($3)), true);
}
;
function_decl_list:
function_decl_item
{
$$ = make_function_decl_list(nullptr, $1);
}
| function_decl_list ',' function_decl_item
{
$$ = make_function_decl_list($1, $3);
}
;
function_decl:
DESCRIBE ID AS var_type ADVENTURE OF '<' function_decl_list '>' '<' statements '>'
{
$$ = make_function_declaration(
new std::string(STR($2)),
new std::string(STR($4)), $8, $11);
}
| DESCRIBE ID AS var_type ADVENTURE '<' statements '>'
{
$$ = make_function_declaration(
new std::string(STR($2)),
new std::string(STR($4)), nullptr, $7);
}
;
function_call:
CRY FOR ID '<' expression_list '>'
{
$$ = make_function_call(new std::string(STR($3)), $5);
}
| CRY FOR ID '!'
{
$$ = make_function_call(new std::string(STR($3)), nullptr);
}
;
expression_list:
expression
{
$$ = make_expression_list(nullptr, $1);
}
| expression_list ',' expression
{
$$ = make_expression_list($1, $3);
}
%%
int main(int argc, char** argv){
yyin=fopen(argv[1],"r");
yyparse();
return 0;
}