-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoklas.y
433 lines (315 loc) · 9.21 KB
/
toklas.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
%{
#include "astprogram.h"
#include <stdio.h>
extern program* parsed_program;
extern symboltable symtab;
extern typelibrary typelib;
int yylex(void);
void yyerror(char *);
%}
%token TO_RUN
%token TO_CONSTRUCT
%token TO_GET
%token TO_SET
%token CHOOSE_ONE
%token DO_THIS
%token ARRAY_OF
%token INITIALIZED_TO
%token OF
%token CALLED
%token WHILE
%token REPEAT
%token STOP
%token LET
%token THE
%token IS
%token CAN
%token A
%token BE
%token HAS
%token PAREN_TH
%token OTHERWISE
%token COMMA_AND
%token WITH
%token FROM
%token TO
/*
%token WHICH_IS_A
%token AS_A
%token REPEAT_LETTING
%token A_NEW
%token THE
%token OF
%token IT
*/
%token AND
%token OR
%token LEQ
%token GEQ
%token <str> ID
%token <intnum> INTNUMBER
%token <floatnum> FLOATNUMBER
%token <intnumord> ORDINALNUMBER
%token <mathop> MATHOP
%token <compop> COMPOP
/*
%left '+'
%left '-'
%left '*'
%left '/'
%left '%'
*/
%union {
char* str;
int intnum;
float floatnum;
int intnumord;
symbolid stref;
char mathop;
program* _yprogram;
unit* _yunit;
preplist* _ypreplist;
statementlist* _ystatementlist;
statement* _ystatement;
literal* _yliteral;
ordinal* _yordinal;
expression* _yexpression;
aExpr* _yaexpr;
theExpr* _ytheexpr;
preposition* _ypreposition;
prepvallist* _yprepvallist;
prepositionval* _yprepositionval;
}
%type <_yprogram> unitlist;
%type <_yunit> unit;
%type <_ypreplist> prepositions;
%type <_ypreplist> preplist;
%type <_ypreposition> prep;
%type <_yprepvallist> prepositionval;
%type <_yprepvallist> prepvallist;
%type <_yprepositionval> prepval;
%type <_yaexpr> typespec
%type <_yaexpr> aexpr
%type <_yaexpr> typebase
%type <_ystatementlist> statementlist;
%type <_ystatementlist> block;
%type <_ystatementlist> dolist;
%type <_ystatement> statement;
%type <_ystatement> letstatement;
%type <_ystatement> dostatement;
%type <_ystatement> choosestatement;
%type <_ystatement> callstatement;
%type <_ystatement> otherwise;
%type <_yexpression> expression;
%type <_yexpression> sum;
%type <_yexpression> term;
%type <_yexpression> callvalue;
%type <_yexpression> singlevalue;
%type <_yexpression> literal;
%type <_yexpression> conditional;
%type <_yexpression> logdisj;
%type <_yexpression> logconj;
%type <_yordinal> ordinal;
%type <_ytheexpr> theexpr;
%type <stref> id
%%
top : unitlist { parsed_program = $1; }
;
unitlist : unitlist unit { $1->addUnit($2); $$ = $1; }
| { $$ = new program(); }
;
/* top-level units */
unit : TO id aexpr prepositions ':' statementlist { $$ = new proceduredefine($2, $3, $4, $6); }
| TO_RUN ':' statementlist { $$ = new run($3); }
| TO_GET theexpr OF aexpr prepositions ':' statementlist { $$ = new methoddefineunit($2, $4, $5, $7); }
| TO_CONSTRUCT aexpr prepositions ':' statementlist { $$ = new constructordefine($2, $3, $5); }
| aexpr HAS aexpr CALLED id '.' { $$ = new propertydefine($1, $3, $5); }
| aexpr IS aexpr '.' { $$ = new subclassdefine($1, $3); }
| aexpr CAN id prepositions '.' { $$ = new proceduredeclare($1, $3, $4); }
| theexpr OF aexpr prepositions IS aexpr '.' { $$ = new functiondeclare($1, $3, $4, $6); }
;
/* right now prepositions is set up only for declarations
when it is used for CALLING functions it will create conflicts with AND used in conjunctions
answer is not to allow conjunctions to be used as parameters to functions
a Blank CAN foo with this and with that and with that.
*/
prepositions : preplist { $$ = $1; }
| { $$ = new preplist(); }
;
preplist : preplist conj prep { $$ = $1; $1->addPreposition($3); }
| prep { $$ = new preplist(); $$->addPreposition($1); }
;
prep : WITH id typespec { $$ = new preposition($2, $3); }
| FROM id typespec { $$ = new preposition($2, $3); }
| TO id typespec { $$ = new preposition($2, $3); }
| WITH id { $$ = new preposition($2); }
| FROM id { $$ = new preposition($2); }
| TO id { $$ = new preposition($2); }
;
/* lists of statements */
statementlist : statementlist statement { $1->addStatement($2); $$ = $1; }
| { $$ = new statementlist(); }
;
statement : letstatement { $$ = $1; }
| dostatement { $$ = $1; }
| choosestatement { $$ = $1; }
| callstatement { $$ = $1; }
;
callstatement : id callvalue '.' { $$ = new callstatement($1, $2); }
;
letstatement : LET expression BE expression '.' { $$ = new letstatement($2, $4); }
;
dostatement : DO_THIS block { $$ = new dostatement($2); }
| WHILE expression DO_THIS block { $$ = new dostatement($2, $4); }
| DO_THIS WHILE expression block { $$ = new dostatement($3, $4); }
;
choosestatement : CHOOSE_ONE ':' dolist otherwise { $3->addStatement($4); $$ = new choosestatement($3); }
;
dolist : dolist dostatement { $1->addStatement($2); $$ = $1; }
| dostatement { $$ = new statementlist(); $$->addStatement($1); }
;
otherwise : OTHERWISE DO_THIS block { $$ = new dostatement($3); }
;
block : ':' statementlist REPEAT '.' { $2->setRepeat(true); $$ = $2; }
| ':' statementlist STOP '.' { $2->setRepeat(false); $$ = $2; }
;
;
/* expressions */
expression : logdisj { $$ = $1; }
;
logdisj : logdisj OR logconj { $$ = new conditionalExpression(OPTYPE_OR, $1, $3); }
| logconj { $$ = $1; }
;
logconj : logconj AND conditional { $$ = new conditionalExpression(OPTYPE_AND, $1, $3); }
| conditional { $$ = $1; }
;
conditional : sum '<' sum { $$ = new conditionalExpression(OPTYPE_LT, $1, $3); }
| sum '>' sum { $$ = new conditionalExpression(OPTYPE_GT, $1, $3); }
| sum '=' sum { $$ = new conditionalExpression(OPTYPE_EQ, $1, $3); }
| sum LEQ sum { $$ = new conditionalExpression(OPTYPE_LTE, $1, $3); }
| sum GEQ sum { $$ = new conditionalExpression(OPTYPE_GTE, $1, $3); }
| sum { $$ = $1; }
;
sum : sum '+' term { $$ = new binaryExpression(OPTYPE_ADD, $1, $3); }
| sum '-' term { $$ = new binaryExpression(OPTYPE_SUB, $1, $3); }
| term { $$ = $1; }
;
term : term '*' callvalue { $$ = new binaryExpression(OPTYPE_MULT, $1, $3); }
| term '/' callvalue { $$ = new binaryExpression(OPTYPE_DIV, $1, $3); }
| term '%' callvalue { $$ = new binaryExpression(OPTYPE_MOD, $1, $3); }
| callvalue { $$ = $1; }
;
callvalue : theexpr OF singlevalue prepositionval { $$ = new call($1, $3, $4); }
| aexpr prepositionval { $$ = new constructExpression($1, $2); }
| singlevalue { $$ = $1; }
;
singlevalue : literal { $$ = $1; }
| id { $$ = new varRef($1); }
| theexpr { $$ = $1; }
| '(' expression ')' { $$ = $2; }
;
prepositionval : prepvallist { $$ = $1; }
| { $$ = new prepvallist(); }
;
prepvallist : prepvallist conj prepval { $$ = $1; $1->addPrepositionValue($3); }
| prepval { $$ = new prepvallist(); $$->addPrepositionValue($1); }
;
prepval : WITH singlevalue { $$ = new prepositionval($2); }
| FROM singlevalue { $$ = new prepositionval($2); }
| TO singlevalue { $$ = new prepositionval($2); }
;
theexpr : THE id { $$ = new theExpr($2); }
| THE ordinal { $$ = new theExpr($2); }
;
ordinal : ORDINALNUMBER { $$ = new ordinal(new literal(typelib.getTypeByName("int"), datum($1))); }
| '(' expression PAREN_TH { $$ = new ordinal($2); }
;
literal : INTNUMBER { $$ = new literal(typelib.getTypeByName("int"), datum($1)); }
| FLOATNUMBER { $$ = new literal(typelib.getTypeByName("float"), datum($1)); }
;
/* general rules */
conj : COMMA_AND
| ','
;
typespec : '(' aexpr ')' { $$ = $2; }
;
aexpr : A typebase { $$ = $2; }
;
typebase : id { $$ = new aExpr($1); }
| ARRAY_OF INTNUMBER typebase { $$ = new arrayAExpr($2, $3); }
;
id : ID { int ref = symtab.addSymbol($1) ; $$ = ref; }
;
%%
/*
classpropdef: A id HAS ':' classproplist
;
classproplist: classproplist vdecl
| classproplist vdecl init
:
;
vdecl: id '(' A id ')'
;
init: INITIALIZED_TO expression
;
classverbdef: TO id A id prepositions IS_TO ':' statementlist
;
prepositions: prepositions WITH id vdecl conjunction
|
;
conjunction : AND
| ','
| ';'
;
classgetdef: TO_GET_THE id OF_A id prepositions IS_TO ':' statementlist
;
classsetdef: TO_SET_THE id OF_A id prepositions IS_TO ':' statementlist
;
statementlist : statementlist '.' statement
| statementlist conjunction statement
|
;
statement: letstatement
| verbstatement
| dostatement
| repeatstatement
| ifstatement
| stopstatement
;
letstatement : LET id BE expression
| LET setexpression BE expression
;
verbstatement : id expression prepositions
;
whilecondition : WHILE expression
;
repeatstatement : REPEAT
| REPEAT ',' letstatement
;
ifstatement : IF expression THEN_DO_THIS ':'
;
stopstatement : STOP
;
expression : mathexpression
| getexpression
| convertexpression
;
convertexpression : expression AS id
;
getexpression : THE id OF id prepositions
;
setexpression : THE id OF id prepositions
;
*/
void yyerror(char *s)
{
printf("%s\n", s);
}
/*
int main(void)
{
int result = yyparse();
printf("done parsing... result: %s\n", (result ? "error" : "success"));
return 0;
}
*/