This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.py
615 lines (528 loc) · 20.8 KB
/
syntax.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
from tree import Tree
import traceback
'''
class Node:
def __init__(self, nodes, annotation):
#NOTE(Noah): Currently, we don't support more than one child node
self.children = nodes
self.NULL = 0
self.annotation = annotation
def NextNode(self):
if type(self.children[0]) == Node:
return self.children[0]
else:
return self.NULL
def GetChildren(self):
children = []
for child in self.children:
if type(child) == Node:
children.append(child)
if len(children) > 0:
return children
return False
def Climb(self):
result = []
currentNode = self
while currentNode:
result.append(currentNode)
currentNode = currentNode.NextNode()
result = result[1:]
return result
class AbstractSyntaxTree:
def __init__(self, rootNode):
self.rootNode = rootNode
def DebugPrint(self, logger):
currentNode = self.rootNode
logger.Log("Root: " + currentNode.annotation)
self.DebugSearch(logger, currentNode.GetChildren(), 1)
def DebugSearch(self, logger, children, indentation):
if children:
#indentation += 1
for child in children:
logger.Log(" " * indentation + "Node: " + str(child.annotation))
self.DebugSearch(logger, child.GetChildren(), indentation)
'''
def IsInteger(string):
for char in string:
if char not in "0123456789":
return False
return True
def IsUnary(char):
if char in "!-":
return True
return False
def IsBinary(char):
if char in "+-/*%":
return True
return False
def Parse_Factor(tokens, logger):
token = tokens.Next()
if token.type == "SYMBOL": #check if a literal, a variable, or a function call
if IsInteger(token.value):
return (Tree(token.value, 0), tokens)
#NOTE(Noah): beware of what "not being an integer" means...
else:
symbol = token.value
token = tokens.Query()
tree = Tree("call:" + symbol, 0)
if token.type == "PART" and token.value == "(":
token = tokens.Next() #actually advance
token = tokens.Query()
if not (token.type == "PART" and token.value == ")"):
expression, tokens = Parse_Expression(tokens, logger)
if not expression:
return (False, tokens)
tree.Adopt(expression)
token = tokens.Query()
while(token.type == "PART" and token.value == ","):
token = tokens.Next()
expression, tokens = Parse_Expression(tokens, logger)
if not expression:
return (False, tokens)
#params.append(token.value)
tree.Adopt(expression)
token = tokens.Query()
token = tokens.Next()
if not (token.type == "PART" and token.value == ")"):
logger.Error("line " + str(token.line) + " expected )")
return (False, tokens)
return (tree, tokens)
else:
return (Tree("var:" + symbol, 0), tokens)
elif token.type == "OP" and IsUnary(token.value): #check if <unary> <factor>
#the expression is a unary expression
factor, tokens = Parse_Factor(tokens, logger)
if not factor:
return (False, tokens)
result = Tree(token.value, 0)
result.Adopt(factor)
return (result, tokens)
elif token.type == "PART" and token.value == "(": #check if (<exp>)
#the next token must be an expression
expression, tokens = Parse_Expression(tokens, logger)
if not expression:
return (False, tokens)
#then there must be another bracket
token = tokens.Next()
if not (token.type == "PART" and token.value == ")"):
logger.Error("line " + str(token.line) + " expected )")
for line in traceback.format_stack():
logger.Log(line.strip())
return (False, tokens)
return (expression, tokens)
elif token.type == "KEY" and token.value == "true":
return (Tree("1", 0), tokens)
elif token.type == "KEY" and token.value == "false":
return (Tree("0", 0), tokens)
else:
logger.Error("line " + str(token.line) + " expected factor")
return (False, tokens)
def Parse_Term(tokens, logger):
#print("term")
#the first token must be a factor
factor, tokens = Parse_Factor(tokens, logger)
if not factor:
return (False, tokens)
token = tokens.Query()
lastFactor = factor
while(token.type == "OP" and (token.value == "*" or token.value == "/")):
token = tokens.Next() #actually advance
factor, tokens = Parse_Factor(tokens, logger)
if not factor:
return (False, tokens)
tree = Tree(token.value, 0)
tree.Adopt(lastFactor)
tree.Adopt(factor)
lastFactor = tree
token = tokens.Query()
return (lastFactor, tokens)
def Parse_Additive_Expression(tokens, logger):
#print("Parse_Additive")
#the first token must be a term
term, tokens = Parse_Term(tokens, logger)
if not term:
return (False, tokens)
token = tokens.Query()
lastTerm = term
while(token.type == "OP" and (token.value == "+" or token.value == "-")):
token = tokens.Next() #actually advance
term, tokens = Parse_Term(tokens, logger)
if not term:
return (False, tokens)
tree = Tree(token.value, 0)
tree.Adopt(lastTerm)
tree.Adopt(term)
lastTerm = tree
token = tokens.Query()
return (lastTerm, tokens)
def Parse_Relational_Operator(tokens):
#it can be <, >, <=, >=
#the function should return how many times to advance, and also the operator
token1 = tokens.Query()
if token1.type == "OP" and (token1.value == "<" or token1.value == ">"):
token1 = tokens.Next()
token2 = tokens.Query()
if token2.type == "OP" and token2.value == "=":
token2 = tokens.Next()
return (token1.value + token2.value, tokens)
else:
return (token1.value, tokens)
else:
return (False, tokens)
def Parse_Relational_Expression(tokens, logger):
#print("Parse_Relational_Expression")
term, tokens = Parse_Additive_Expression(tokens, logger)
if not term:
return (False, tokens)
lastTerm = term
op, tokens = Parse_Relational_Operator(tokens)
#print("op: " + str(op))
while op:
term, tokens = Parse_Additive_Expression(tokens, logger)
if not term:
return (False, tokens)
tree = Tree(op, 0)
tree.Adopt(lastTerm)
tree.Adopt(term)
lastTerm = tree
op, tokens = Parse_Relational_Operator(tokens)
return (lastTerm, tokens)
def Parse_Equality_Operator(tokens):
token1 = tokens.Query()
if token1.type == "OP" and (token1.value == "!" or token1.value == "="):
token1 = tokens.Next()
token2 = tokens.Query()
if token2.type == "OP" and token2.value == "=":
token2 = tokens.Next()
return (token1.value + token2.value, tokens)
else:
return (token1.value, tokens)
else:
return (False, tokens)
def Parse_Equality_Expression(tokens, logger):
term, tokens = Parse_Relational_Expression(tokens, logger)
if not term:
return (False, tokens)
lastTerm = term
op, tokens = Parse_Equality_Operator(tokens)
#print("op: " + str(op))
while op:
term, tokens = Parse_Relational_Expression(tokens, logger)
if not term:
return (False, tokens)
tree = Tree(op, 0)
tree.Adopt(lastTerm)
tree.Adopt(term)
lastTerm = tree
op, tokens = Parse_Equality_Operator(tokens)
return (lastTerm, tokens)
def Parse_Logical_And_Operator(tokens):
token1 = tokens.Query()
if token1.type == "OP" and token1.value == "&":
token1 = tokens.Next()
token2 = tokens.Query()
if token2.type == "OP" and token2.value == "&":
token2 = tokens.Next()
return ("&&", tokens)
else:
return (False, tokens)
else:
return (False, tokens)
def Parse_Logical_And_Expression(tokens, logger):
term, tokens = Parse_Equality_Expression(tokens, logger)
if not term:
return (False, tokens)
lastTerm = term
op, tokens = Parse_Logical_And_Operator(tokens)
#print("op: " + str(op))
while op:
term, tokens = Parse_Equality_Expression(tokens, logger)
if not term:
return (False, tokens)
tree = Tree(op, 0)
tree.Adopt(lastTerm)
tree.Adopt(term)
lastTerm = tree
op, tokens = Parse_Logical_And_Operator(tokens)
return (lastTerm, tokens)
def Parse_Logical_Or_Operator(tokens):
token1 = tokens.Query()
if token1.type == "OP" and token1.value == "|":
token1 = tokens.Next()
token2 = tokens.Query()
if token2.type == "OP" and token2.value == "|":
token2 = tokens.Next()
return ("||", tokens)
else:
return (False, tokens)
else:
return (False, tokens)
def Parse_Logical_Or_Expression(tokens, logger):
#print("expression")
term, tokens = Parse_Logical_And_Expression(tokens, logger)
if not term:
return (False, tokens)
lastTerm = term
op, tokens = Parse_Logical_Or_Operator(tokens)
#print("op: " + str(op))
while op:
term, tokens = Parse_Logical_And_Expression(tokens, logger)
if not term:
return (False, tokens)
tree = Tree(op, 0)
tree.Adopt(lastTerm)
tree.Adopt(term)
lastTerm = tree
op, tokens = Parse_Logical_Or_Operator(tokens)
return (lastTerm, tokens)
def Parse_Conditional_Expression(tokens, logger):
term, tokens = Parse_Logical_Or_Expression(tokens, logger)
if not term:
return (False, tokens)
token = tokens.Query()
if token.type == "OP" and token.value == "?":
tree = Tree("conditional", 0)
tree.Adopt(term)
token = tokens.Next()
expression, tokens = Parse_Expression(tokens, logger)
if not expression:
return (False, tokens)
tree.Adopt(expression)
token = tokens.Next()
if not (token.type == "PART" and token.value == ":"):
logger.Error("line " + str(token.line) + " expected :")
return (False, tokens)
conditional, tokens = Parse_Conditional_Expression(tokens, logger)
if not conditional:
return (False, tokens)
tree.Adopt(conditional)
return (tree, tokens)
return (term, tokens)
def Parse_Expression(tokens, logger):
conditional, tokens = Parse_Conditional_Expression(tokens, logger)
if not conditional:
return (False, tokens)
return (conditional, tokens)
def Parse_Block(tokens, logger, name):
name += ":block"
statement, tokens = Parse_Statement(tokens, logger)
if not statement:
return (False, tokens)
#TODO(Noah): I think this while loop can be done a little better
block = Tree(name, 0)
while statement:
block.Adopt(statement)
token = tokens.Query()
if not (token.type == "PART" and token.value == "}"):
statement, tokens = Parse_Statement(tokens, logger)
else:
break
token = tokens.Next()
if not (token.type == "PART" and token.value == "}"):
logger.Error("line " + str(token.line) + " expected }")
return (False, tokens)
return (block, tokens)
def Parse_Statement(tokens, logger, end=True):
statement = Tree("statement", 0)
token = tokens.Next()
if token.type == "KEY" and token.value == "return": #return statement
statement.data = "return"
expression, tokens = Parse_Expression(tokens, logger)
if not expression:
return (False, tokens)
statement.Adopt(expression)
elif token.type == "KEY" and token.value == "int": #variable declaration
statement.data = "declaration"
token = tokens.Next()
if not token.type == "SYMBOL":
logger.Error("line " + str(token.line) + " expected variable name")
return (False, tokens)
statement.GiveBirth(token.value)
token = tokens.Query()
if token.type == "OP" and token.value == "=":
token = tokens.Next()
expression, tokens = Parse_Expression(tokens, logger)
if not expression:
return (False, tokens)
statement.Adopt(expression)
elif token.type == "KEY" and token.value == "if": #if statement
statement.data = "if"
token = tokens.Next()
if not (token.type == "PART" and token.value == "("):
logger.Error("line " + str(token.line) + " expected (")
return (False, tokens)
expression, tokens = Parse_Expression(tokens, logger)
if not expression:
return (False, tokens)
statement.Adopt(expression)
token = tokens.Next()
if not (token.type == "PART" and token.value == ")"):
logger.Error("line " + str(token.line) + " expected )")
return (False, tokens)
body, tokens = Parse_Statement(tokens, logger)
if not body:
return (False, tokens)
statement.Adopt(body)
token = tokens.Query()
if token.type == "KEY" and token.value == "else":
token == tokens.Next()
tree = Tree("else", 0)
body, tokens = Parse_Statement(tokens, logger)
if not body:
return (False, tokens)
tree.Adopt(body)
statement.Adopt(tree)
#NOTE(Noah): If statements do not end with a semicolon
return (statement, tokens)
elif token.type == "KEY" and token.value == "for":
statement.data = "for"
token = tokens.Next()
if not (token.type == "PART" and token.value == "("):
logger.Error("line " + str(token.line) + " expected (")
return (False, tokens)
init, tokens = Parse_Statement(tokens, logger)
if not init:
return (False, tokens)
statement.Adopt(init)
condition, tokens = Parse_Expression(tokens, logger)
if not condition:
return (False, tokens)
statement.Adopt(condition)
token = tokens.Next()
if not token.type == "END":
logger.Error("line " + str(token.line) + " expected END")
for line in traceback.format_stack():
logger.Log(line.strip())
return (False, tokens)
#NOTE(Noah): The following is a bodge. That's because I don't want to make variable declarations expressions.
end, tokens = Parse_Statement(tokens, logger, False)
if not end:
return (False, tokens)
statement.Adopt(end)
token = tokens.Next()
if not (token.type == "PART" and token.value == ")"):
logger.Error("line " + str(token.line) + " expected )")
return (False, tokens)
body, tokens = Parse_Statement(tokens, logger)
if not body:
return (False, tokens)
statement.Adopt(body)
return (statement, tokens)
elif token.type == "SYMBOL": #varible assignment
statement.GiveBirth(token.value)
statement.data = "assignment"
token = tokens.Next()
if not (token.type == "OP" and token.value == "="):
logger.Error("line " + str(token.line) + " expected =")
return (False, tokens)
expression, tokens = Parse_Expression(tokens, logger)
if not expression:
return (False, tokens)
statement.Adopt(expression)
elif token.type == "PART" and token.value == "{": #compound statement
block, tokens = Parse_Block(tokens, logger, "default")
if not block:
return (False, tokens)
statement = block
#NOTE(Noah): compound statements do not end with a semicolon
return (statement, tokens)
elif token.type == "KEY" and token.value == "break":
statement.data = "break"
elif token.type == "KEY" and token.value == "continue":
statement.data = "continue"
elif token.type == "KEY" and token.value == "while":
statement.data = "while"
token = tokens.Next()
if not (token.type == "PART" and token.value == "("):
logger.Error("line " + str(token.line) + " expected (")
return (False, tokens)
expression, tokens = Parse_Expression(tokens, logger)
if not expression:
return (False, tokens)
statement.Adopt(expression)
token = tokens.Next()
if not (token.type == "PART" and token.value == ")"):
logger.Error("line " + str(token.line) + " expected )")
return (False, tokens)
body, tokens = Parse_Statement(tokens, logger)
if not body:
return (False, tokens)
statement.Adopt(body)
return (statement, tokens)
else:
logger.Error("line " + str(token.line) + " expected return, symbol, or int")
return (False, tokens)
if end:
token = tokens.Next()
if not token.type == "END":
logger.Error("line " + str(token.line) + " expected END")
for line in traceback.format_stack():
logger.Log(line.strip())
return (False, tokens)
return (statement, tokens)
def Parse_Function(tokens, logger):
params = []
token = tokens.Next()
if not (token.type == "KEY" and token.value == "int"):
logger.Error("line " + str(token.line) + " expected int")
return (False, tokens)
token = tokens.Next()
if not token.type == "SYMBOL":
logger.Error("line " + str(token.line) + " expected symbol")
return (False, tokens)
name = token.value
token = tokens.Next()
if not (token.type == "PART" and token.value == "("):
logger.Error("line " + str(token.line) + " expected (")
return (False, tokens)
token = tokens.Query()
if token.type == "KEY" and token.value == "int":
token = tokens.Next()
token = tokens.Next()
if not token.type == "SYMBOL":
logger.Error("line " + str(token.line) + " expected symbol")
return (False, tokens)
params.append(token.value)
token = tokens.Query()
while(token.type == "PART" and token.value == ","):
token = tokens.Next()
token = tokens.Next()
if not (token.type == "KEY" and token.value == "int"):
logger.Error("line " + str(token.line) + " expected int")
return (False, tokens)
token = tokens.Next()
if not token.type == "SYMBOL":
logger.Error("line " + str(token.line) + " expected symbol")
return (False, tokens)
params.append(token.value)
token = tokens.Query()
token = tokens.Next()
if not (token.type == "PART" and token.value == ")"):
logger.Error("line " + str(token.line) + " expected )")
return (False, tokens)
token = tokens.Next()
if not (token.type == "PART" and token.value == "{"):
logger.Error("line " + str(token.line) + " expected {")
return (False, tokens)
block, tokens = Parse_Block(tokens, logger, "default")
if not block:
return (False, tokens)
function = Tree(name, 0)
for param in params:
function.GiveBirth(param)
function.Adopt(block)
return (function, tokens)
def Parse_Program(tokens, logger):
program = Tree("program", 0)
while(tokens.Len()):
function, tokens = Parse_Function(tokens, logger)
if not function:
#NOTE(Noah): We are going to return false if we get a half function
return (False, tokens)
program.Adopt(function)
return (program, tokens)
def Run(tokens, logger):
program, tokens = Parse_Program(tokens, logger)
if not program:
return False
return program