-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAST.py
More file actions
45 lines (36 loc) · 1.03 KB
/
AST.py
File metadata and controls
45 lines (36 loc) · 1.03 KB
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
class Node:
def to_str(self):
pass
class BinaryNode(Node):
def __init__(self, op, left, right, *sub):
if sub == tuple():
self.sub = None
else:
self.sub = sub[0]
self.parent = None
self.op = op
self.left = left
self.right = right
self.visited = False
def to_str(self):
return "(" + self.left.to_str() + ") " + self.op + " (" + self.right.to_str() + ")"
class UnaryNode(Node):
def __init__(self, op, child, *sub):
if sub == tuple():
self.sub = None
else:
self.sub = sub[0]
self.parent = None
self.op = op
self.child = child
self.visited = False
def to_str(self):
return self.op + " (" + self.child.to_str() + ")"
class Leaf(Node):
def __init__(self, typ, value):
self.parent = None
self.typ = typ
self.value = value
self.visited = False
def to_str(self):
return self.typ + ": " + self.value