148
|
1 """
|
|
2 AST nodes for the c3 language.
|
|
3 """
|
|
4
|
|
5 class Node:
|
|
6 location = None
|
|
7 def getChildren(self):
|
|
8 children = []
|
|
9 members = dir(self)
|
|
10 for member in members:
|
|
11 member = getattr(self, member)
|
|
12 if isinstance(member, Node):
|
|
13 children.append(member)
|
|
14 elif type(member) is list:
|
|
15 for mi in member:
|
|
16 if isinstance(mi, Node):
|
|
17 children.append(mi)
|
|
18 return children
|
|
19
|
150
|
20 class Designator(Node):
|
|
21 def __init__(self, tname):
|
|
22 self.tname = tname
|
148
|
23 def __repr__(self):
|
150
|
24 return 'DESIGNATOR {0}'.format(self.tname)
|
148
|
25
|
|
26 """
|
|
27 Type classes
|
|
28 """
|
|
29 def isType(a, b):
|
|
30 """ Compare types a and b and check if they are equal """
|
|
31 if type(a) is type(b):
|
|
32 if type(a) is BaseType:
|
|
33 return (a.name == b.name) and (a.size == b.size)
|
|
34 elif type(a) is ProcedureType:
|
|
35 if len(a.parameters) != len(b.parameters):
|
|
36 print('Number of parameters does not match')
|
|
37 return False
|
|
38 for aparam, bparam in zip(a.parameters, b.parameters):
|
|
39 if not isType(aparam.typ, bparam.typ):
|
|
40 print('Parameter {0} does not match parameter {1}'.format(aparam, bparam))
|
|
41 return False
|
|
42 if a.result is None:
|
|
43 # TODO: how to handle a None return type??
|
|
44 pass
|
|
45 if not isType(a.result, b.result):
|
|
46 print('Procedure return value mismatch {0} != {1}'.format(a.result, b.result))
|
|
47 return False
|
|
48 return True
|
|
49 else:
|
|
50 print(a)
|
|
51 print(b)
|
|
52 Error('Not implemented {0}'.format(a))
|
|
53 else:
|
|
54 return False
|
|
55
|
149
|
56 class Type(Node):
|
148
|
57 def isType(self, b):
|
|
58 return isType(self, b)
|
|
59
|
|
60 class BaseType(Type):
|
|
61 def __init__(self, name):
|
|
62 self.name = name
|
|
63 def __repr__(self):
|
|
64 return '[TYPE {0}]'.format(self.name)
|
|
65
|
|
66 class FunctionType(Type):
|
|
67 def __init__(self, parameters, returntype):
|
|
68 self.parameters = parameters
|
|
69 self.returntype = returntype
|
|
70 def __repr__(self):
|
|
71 return '[PROCTYPE {0} RET {1}]'.format(self.parameters, self.returntype)
|
|
72
|
|
73 class DefinedType(Type):
|
|
74 def __init__(self, name, typ):
|
|
75 self.name = name
|
|
76 self.typ = typ
|
|
77 def __repr__(self):
|
|
78 return 'Named type {0} of type {1}'.format(self.name, self.typ)
|
|
79
|
|
80 # Variables, parameters, local variables, constants:
|
|
81 class Symbol(Node):
|
|
82 pass
|
|
83
|
|
84 class Constant(Symbol):
|
149
|
85 def __init__(self, value, name=None):
|
148
|
86 self.name = name
|
|
87 self.value = value
|
|
88 def __repr__(self):
|
|
89 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
|
|
90
|
|
91 class Variable(Symbol):
|
149
|
92 def __init__(self, name, typ):
|
148
|
93 self.name = name
|
|
94 self.typ = typ
|
|
95 self.isLocal = False
|
|
96 self.isReadOnly = False
|
|
97 self.isParameter = False
|
|
98 def __repr__(self):
|
149
|
99 return 'VAR {0} : {1}'.format(self.name, self.typ)
|
148
|
100
|
150
|
101 # Procedure types
|
|
102 class Function(Symbol):
|
|
103 """ Actual implementation of a function """
|
|
104 def __init__(self, name, typ=None, block=None):
|
148
|
105 self.name = name
|
150
|
106 self.body = block
|
148
|
107 self.typ = typ
|
|
108 def __repr__(self):
|
150
|
109 return 'PROCEDURE {0} {1}'.format(self.name, self.typ)
|
148
|
110
|
|
111 # Operations:
|
|
112 class Unop(Node):
|
|
113 def __init__(self, a, op):
|
|
114 self.a = a
|
|
115 self.op = op
|
|
116 def __repr__(self):
|
|
117 return 'UNOP {0}'.format(self.op)
|
|
118
|
|
119 class Binop(Node):
|
|
120 def __init__(self, a, op, b):
|
|
121 self.a = a
|
|
122 self.b = b
|
|
123 self.op = op # Operation: '+', '-', '*', '/', 'mod'
|
|
124 def __repr__(self):
|
149
|
125 return 'BINOP {0}'.format(self.op)
|
148
|
126
|
150
|
127 class VariableUse(Node):
|
|
128 def __init__(self, target):
|
|
129 self.target = target
|
|
130 def __repr__(self):
|
|
131 return 'VAR USE {0}'.format(self.target)
|
|
132
|
148
|
133 # Modules
|
|
134 class Package(Node):
|
|
135 def __init__(self, name):
|
|
136 self.name = name
|
|
137 def __repr__(self):
|
|
138 return 'PACKAGE {0}'.format(self.name)
|
|
139
|
|
140 # Statements
|
|
141 class CompoundStatement(Node):
|
|
142 def __init__(self, statements):
|
|
143 self.statements = statements
|
|
144 def __repr__(self):
|
|
145 return 'COMPOUND STATEMENT'
|
|
146
|
155
|
147 class EmptyStatement(Node):
|
|
148 pass
|
|
149
|
149
|
150 class ReturnStatement(Node):
|
|
151 def __init__(self, expr):
|
|
152 self.expr = expr
|
148
|
153 def __repr__(self):
|
149
|
154 return 'RETURN STATEMENT'
|
148
|
155
|
|
156 class Assignment(Node):
|
|
157 def __init__(self, lval, rval):
|
|
158 self.lval = lval
|
|
159 self.rval = rval
|
|
160 def __repr__(self):
|
|
161 return 'ASSIGNMENT'
|
|
162
|
|
163 class ProcedureCall(Node):
|
|
164 def __init__(self, proc, args):
|
|
165 self.proc = proc
|
|
166 self.args = args
|
|
167 def __repr__(self):
|
|
168 return 'CALL {0} '.format(self.proc)
|
|
169
|
|
170 class IfStatement(Node):
|
|
171 def __init__(self, condition, truestatement, falsestatement=None):
|
|
172 self.condition = condition
|
|
173 self.truestatement = truestatement
|
|
174 self.falsestatement = falsestatement
|
|
175 def __repr__(self):
|
|
176 return 'IF-statement'
|
|
177
|
|
178 class WhileStatement(Node):
|
|
179 def __init__(self, condition, statements):
|
|
180 self.condition = condition
|
|
181 self.dostatements = statements
|
|
182 def __repr__(self):
|
|
183 return 'WHILE-statement'
|
|
184
|