-
Notifications
You must be signed in to change notification settings - Fork 0
/
parshonker.py
623 lines (504 loc) · 13.9 KB
/
parshonker.py
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
from lexhonker import tokens
from functionDirectory import FunctionDirectory
from quadManager import QuadManager
funcDir = FunctionDirectory()
quads = QuadManager(funcDir)
# Parsing productions
# PROGRAMA
def p_program(p):
"program : UNTITLED ID GAME found_program_name HONK vars functions main body"
# Finish parsing
quads.addEndQuad()
p[0] = quads
# Build .o
quads.build()
# Make a GOTO quad to main()
def p_found_program_name(p):
'found_program_name : empty'
if quads.debug:
print("\n|==|==|==|==|==|==|==|==| START DEBUG LOG |==|==|==|==|==|==|==|==|\n")
funcDir.addFunction('main')
funcDir.setGlobalFunction()
quads.addMainQuad()
# VARS
# Variable declaration
def p_vars(p):
"""vars : POND found_var var_declare
| empty"""
pass
def p_found_var(p):
"found_var : empty"
funcDir.createVarTable()
def p_var_declare(p):
"""var_declare : type var_name HONK var_declare
| type var_name HONK"""
pass
def p_var_name(p):
"""var_name : variable_declare var_dims MOAR var_name
| variable_declare var_dims"""
pass
# Create var upon finding its ID
def p_variable_declare(p):
"variable_declare : ID"
var = p[1]
if funcDir.varAvailable(var):
funcDir.addVar(var, quads.vDir.generateVirtualAddress(funcDir.currentFunc, funcDir.currentType))
funcDir.setVarHelper(var)
else:
s_error(f'Variable "{var}" already exists!"')
def p_var_no_dims(p):
"var_dims : empty"
pass
# Make space for variable array/matrix
def p_var_dims(p):
"""var_dims : dim dim
| dim"""
if len(p) == 3:
funcDir.setVarDims([p[1], p[2]])
p[0] = p[1] * p[2]
else:
funcDir.setVarDims([p[1]])
p[0] = p[1]
quads.vDir.makeSpaceForArray(funcDir.currentFunc, funcDir.currentType, p[0])
# TYPE
def p_type(p):
"""type : type_int
| type_float
| type_char
| type_bool"""
p[0] = p[1]
funcDir.setCurrentType(p[0])
def p_type_int(p):
"type_int : WHOLE GOOSE"
p[0] = 'int'
def p_type_float(p):
"type_float : PART GOOSE"
p[0] = 'float'
def p_type_char(p):
"type_char : LETTER GOOSE"
p[0] = 'char'
def p_type_bool(p):
"type_bool : DUCK OR GOOSE"
p[0] = 'bool'
# FUNCTIONS
def p_functions(p):
"""functions : TASK func_type ID found_func_name func_dims HONK func_params HONK vars found_func_start body found_func_end functions
| empty"""
pass
# Set function type
def p_func_type(p):
"func_type : type"
pass
# Set function type
def p_func_void(p):
"func_type : MY SOUL"
funcDir.setCurrentType('void')
# Create function
def p_found_func_name(p):
"found_func_name : empty"
func = p[-1]
if funcDir.functionExists(func):
s_error(f'Function "{func}" already exists!"')
else:
funcDir.addFunction(func)
funcDir.createVarTable()
def p_func_no_dims(p):
"func_dims : empty"
pass
# Set return dimensions
def p_func_dims(p):
"""func_dims : dim dim
| dim"""
if len(p) == 3:
funcDir.setReturnDims([p[1], p[2]])
else:
funcDir.setReturnDims([p[1]])
def p_dim(p):
"dim : OPEN BOX CTE_INT CLOSE BOX"
if p[3] <= 0:
s_error(f'Zero or negative indexes are not allowed! [{p[3]}]')
p[0] = p[3]
def p_func_params(p):
"""func_params : func_param
| empty"""
pass
def p_func_param(p):
"""func_param : type ID found_func_param param_dims MOAR func_param
| type ID found_func_param param_dims"""
pass
# Add function parameter
def p_found_func_param(p):
"found_func_param : empty"
param = p[-1]
if funcDir.varAvailable(param):
funcDir.setVarHelper(param)
vAddr = quads.vDir.generateVirtualAddress(funcDir.currentFunc, funcDir.currentType)
funcDir.addVar(param, vAddr)
funcDir.addFuncParam(vAddr)
else:
s_error(f'Multiple declaration of "{param}"!')
def p_param_no_dims(p):
"param_dims : empty"
pass
# Add dimensions to function parameter
def p_param_dims(p):
"""param_dims : dim dim
| dim"""
space = None
if len(p) == 3:
p[0] = [p[1], p[2]]
space = p[1] * p[2]
else:
p[0] = [p[1]]
space = p[1]
funcDir.addFuncParamDims(p[0])
funcDir.setVarDims(p[0])
quads.vDir.makeSpaceForArray(funcDir.currentFunc, funcDir.currentType, space)
# Set quad start for start of function
def p_found_func_start(p):
"found_func_start : empty"
funcDir.setQuadStart(quads.getQuadCount())
# Prepare end of function
def p_found_func_end(p):
"found_func_end : empty"
quads.addEndFuncQuad()
funcDir.deleteVarTable()
# MAIN
def p_main(p):
"main : PRESS Y TO HONK_LOWERCASE"
quads.completeMainQuad()
# BODY
def p_body(p):
"body : OPEN FANCY GATE statements CLOSE FANCY GATE"
pass
def p_statements(p):
"""statements : statement statements
| empty"""
pass
def p_statement(p):
"""statement : assignment
| call_func
| return
| read
| print
| if
| from
| while
| break
| empty"""
pass
## ASSIGNMENT
def p_assignment(p):
"assignment : expr_var AM found_assignment_op found_expr_duo_op expr found_assignment_end HONK"
pass
def p_found_assignment_op(p):
"found_assignment_op : empty"
p[0] = '='
# Add assignment quadruple
def p_found_assignment_end(p):
"found_assignment_end : empty"
quads.addAssignQuad()
## RETURN
def p_return(p):
"return : GOT BELL expr HONK"
quads.addReturnQuad()
## READ
def p_read(p):
"read : HO '-' read_params '-' ONK HONK"
pass
def p_read_params(p):
"""read_params : expr_var found_read_param MOAR read_params
| expr_var found_read_param"""
pass
# Add read quadruple
def p_found_read_param(p):
"found_read_param : empty"
quads.addReadQuad()
## PRINT
def p_print(p):
"print : SHOW ON TV print_params HONK"
pass
def p_print_params(p):
"""print_params : print_param MOAR print_params
| print_param"""
pass
# Add print quadruple
def p_print_param(p):
"print_param : expr"
quads.addPrintQuad(False)
# Add print quadruple
def p_print_string(p):
"print_param : STRING"
quads.addPrintQuad(p[1])
## IF
def p_if(p):
"""if : HONK '?' expr found_if_expr HONK '!' body
| HONK '?' expr found_if_expr HONK '!' body else"""
quads.completeIfQuad()
def p_found_if_expr(p):
"found_if_expr : empty"
quads.addGoToFQuad()
## ELSE
def p_else(p):
"else : BONK found_else body"
pass
def p_found_else(p):
"found_else : empty"
quads.addElseQuad()
## FROM
def p_from(p):
"from : INHALES ID found_from_iterator AM expr found_from_start HOOOONK expr found_from_cond HOONK body"
quads.addFromEndQuads()
def p_found_from_iterator(p):
"found_from_iterator : empty"
quads.addFromIteratorQuads(p[-1])
def p_found_from_start(p):
"found_from_start : empty"
quads.addFromStartQuad()
def p_found_from_cond(p):
"found_from_cond : empty"
quads.addFromCondQuads()
## WHILE
def p_while(p):
"while : HONK HONK found_while expr found_while_expr HOONK body"
quads.completeLoopQuad()
def p_found_while(p):
"found_while : empty"
quads.prepareLoop()
def p_found_while_expr(p):
"found_while_expr : empty"
quads.addGoToFQuad()
## BREAK
def p_break(p):
"break : PEACE WAS NEVER AN OPTION HONK"
quads.addBreakQuad()
# EXPRESSION -> Order of operator precedence:
# - Matrix operations ($, !, ?)
# - Factors (*, /, %, .)
# - Arithmetic (+, -)
# - Comparison (==, !=, <, <=, >, >=)
# - Logic (&, |)
def p_expr(p):
"expr : expr_logic"
pass
def p_expr_logic(p):
"expr_logic : expr_compare found_expr_logic expr_logic2"
pass
def p_found_expr_logic(p):
"found_expr_logic : empty"
quads.addDualOpQuad(['&', '|'])
def p_expr_logic2(p):
"""expr_logic2 : and_op found_expr_duo_op expr_logic
| or_op found_expr_duo_op expr_logic
| empty"""
pass
def p_and_op(p):
"and_op : TOGETHER FOREVER"
p[0] = '&'
def p_or_op(p):
"or_op : POLE"
p[0] = '|'
def p_expr_compare(p):
"expr_compare : expr_arith found_expr_compare expr_compare2"
pass
def p_found_expr_compare(p):
"found_expr_compare : empty"
quads.addDualOpQuad(['==', '!=', '<', '<=', '>', '>='])
def p_expr_compare2(p):
"""expr_compare2 : eq_op found_expr_duo_op expr_compare
| noeq_op found_expr_duo_op expr_compare
| less_op found_expr_duo_op expr_compare
| lesseq_op found_expr_duo_op expr_compare
| more_op found_expr_duo_op expr_compare
| moreeq_op found_expr_duo_op expr_compare
| empty"""
pass
def p_eq_op(p):
"eq_op : AM GOOSE '?'"
p[0] = '=='
def p_noeq_op(p):
"noeq_op : NOT GOOSE '?' '!'"
p[0] = '!='
def p_less_op(p):
"less_op : INFERIOR"
p[0] = '<'
def p_lesseq_op(p):
"lesseq_op : INFERIOR MAYBE"
p[0] = '<='
def p_more_op(p):
"more_op : SUPERIOR"
p[0] = '>'
def p_moreeq_op(p):
"moreeq_op : SUPERIOR MAYBE"
p[0] = '>='
def p_expr_arith(p):
"expr_arith : expr_factor found_expr_arith expr_arith2"
pass
def p_found_expr_arith(p):
"found_expr_arith : empty"
quads.addDualOpQuad(['+', '-'])
def p_expr_arith2(p):
"""expr_arith2 : plus_op found_expr_duo_op expr_arith
| minus_op found_expr_duo_op expr_arith
| empty"""
pass
def p_plus_op(p):
"plus_op : MORE GOOSE"
p[0] = '+'
def p_minus_op(p):
"minus_op : LESS GOOSE"
p[0] = '-'
def p_expr_factor(p):
"expr_factor : expr_mono found_expr_factor expr_factor2"
pass
def p_found_expr_factor(p):
"found_expr_factor : empty"
quads.addDualOpQuad(['*', '/', '%', '.'])
def p_expr_factor2(p):
"""expr_factor2 : mul_op found_expr_duo_op expr_factor
| div_op found_expr_duo_op expr_factor
| mod_op found_expr_duo_op expr_factor
| dot_op found_expr_duo_op expr_factor
| empty"""
pass
def p_mul_op(p):
"mul_op : GOOSETIPLY"
p[0] = '*'
def p_div_op(p):
"div_op : GOOSIVIDE"
p[0] = '/'
def p_mod_op(p):
"mod_op : LEFTOVERS"
p[0] = '%'
def p_dot_op(p):
"dot_op : DOOT"
p[0] = '.'
def p_found_expr_duo_op(p):
"found_expr_duo_op : empty"
quads.pushOperator(p[-1])
def p_expr_mono(p):
"expr_mono : expr_atom expr_mono_op"
pass
def p_expr_mono_op(p):
"""expr_mono_op : det_op found_expr_mono_op expr_mono_op
| trans_op found_expr_mono_op expr_mono_op
| inv_op found_expr_mono_op expr_mono_op
| empty"""
pass
def p_det_op(p):
"det_op : GOOSECOIN"
p[0] = '$'
def p_trans_op(p):
"trans_op : SURPRISE"
p[0] = '!'
def p_inv_op(p):
"inv_op : WH"
p[0] = '?'
def p_found_expr_mono_op(p):
"found_expr_mono_op : empty"
quads.addMonoOpQuad(p[-1])
def p_expr_atom(p):
"""expr_atom : expr_group
| expr_call_func
| expr_var"""
pass
def p_expr_group(p):
"expr_group : OPEN GATE found_expr_group found_expr_duo_op expr CLOSE GATE"
quads.popOperator()
def p_found_expr_group(p):
"found_expr_group : empty"
p[0] = '('
def p_expr_var(p):
"expr_var : expr_var_name expr_var_dims"
pass
def p_expr_var_name(p):
"expr_var_name : ID"
var = funcDir.getVar(p[1])
funcDir.setVarHelper(var.name)
quads.pushVar(var)
def p_expr_var_no_dims(p):
"expr_var_dims : empty"
pass
def p_expr_var_dims(p):
"""expr_var_dims : found_expr_var_dims expr_var_dim expr_var_dim
| found_expr_var_dims expr_var_dim"""
quads.addBaseAddressQuad()
def p_found_expr_var_dims(p):
"found_expr_var_dims : empty"
quads.sDims.append((funcDir.varHelper, 0))
def p_expr_var_dim(p):
"expr_var_dim : OPEN BOX expr_var_open_dim expr CLOSE BOX"
quads.addArrQuads()
def p_expr_var_open_dim(p):
"expr_var_open_dim : empty"
aux = quads.sDims.pop()
if aux[1] + 1 > len(funcDir.getDimensionsOfVar(aux[0])):
raise Exception(f'{aux[0]} has {len(funcDir.getDimensionsOfVar(aux[0]))} dimension(s)! -> {aux[1]}')
quads.sDims.append((aux[0], aux[1] + 1))
quads.sOperators.append('(')
def p_cte(p):
"""expr_var : CTE_INT
| CTE_FLOAT
| CTE_BOOL
| CTE_CHAR"""
t = None
if (type(p[1]) is int):
t = 'int'
elif (type(p[1]) is float):
t = 'float'
elif (type(p[1]) is str):
t = 'char'
elif (type(p[1] is bool)):
t = 'bool'
cte = quads.upsertCte(p[1], t)
quads.pushCte(cte)
def p_expr_call_func(p):
"expr_call_func : HOOONK ID found_call_func_name OPEN GATE call_func_params CLOSE GATE found_call_func_end"
quads.addAssignFuncQuad()
## CALL_FUNCTION
def p_call_func(p):
"call_func : HOOONK ID found_call_func_name OPEN GATE call_func_params CLOSE GATE found_call_func_end HONK"
func = quads.popFunction()
if quads.funcDir.getReturnTypeOfFunc(func) != 'void':
raise Exception(f"This function is non-void, therefore it can't be used as an expression! -> {func}")
def p_found_call_func_name(p):
"found_call_func_name : empty"
func = p[-1]
if funcDir.functionExists(func):
quads.addEraQuad(func)
quads.pushFunction(func)
quads.sOperators.append('(')
else:
raise Exception(f'Function {func}() does not exist!')
def p_call_func_params(p):
"""call_func_params : call_func_param
| empty"""
pass
def p_call_func_param(p):
"""call_func_param : expr func_single_step MOAR call_func_param
| expr func_single_step"""
pass
def p_func_single_step(p):
"func_single_step : empty"
target_param = funcDir.getParamOfFunc(quads.getTopFunction())
quads.addParamQuad(target_param, funcDir.paramCount)
funcDir.incrementParamCount()
pass
def p_found_call_func_end(p):
"found_call_func_end : empty"
func = quads.getTopFunction()
if funcDir.verifyParamCount(func):
funcDir.resetParamCount()
quads.addGoSubQuad(func, funcDir.getQuadStartOfFunc(func))
quads.popOperator()
else:
raise Exception(f'Wrong number of parameters in {func}!')
def p_empty(p):
"empty :"
pass
# Error handling
def p_error(p):
raise Exception(f'({p.lineno}) Syntax error at "{p.value}"')
# Manual error (line number & position are kinda broken...)
def s_error(msg):
raise Exception(msg)