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
|
149
|
147 class ReturnStatement(Node):
|
|
148 def __init__(self, expr):
|
|
149 self.expr = expr
|
148
|
150 def __repr__(self):
|
149
|
151 return 'RETURN STATEMENT'
|
148
|
152
|
|
153 class Assignment(Node):
|
|
154 def __init__(self, lval, rval):
|
|
155 self.lval = lval
|
|
156 self.rval = rval
|
|
157 def __repr__(self):
|
|
158 return 'ASSIGNMENT'
|
|
159
|
|
160 class ProcedureCall(Node):
|
|
161 def __init__(self, proc, args):
|
|
162 self.proc = proc
|
|
163 self.args = args
|
|
164 def __repr__(self):
|
|
165 return 'CALL {0} '.format(self.proc)
|
|
166
|
|
167 class IfStatement(Node):
|
|
168 def __init__(self, condition, truestatement, falsestatement=None):
|
|
169 self.condition = condition
|
|
170 self.truestatement = truestatement
|
|
171 self.falsestatement = falsestatement
|
|
172 def __repr__(self):
|
|
173 return 'IF-statement'
|
|
174
|
|
175 class WhileStatement(Node):
|
|
176 def __init__(self, condition, statements):
|
|
177 self.condition = condition
|
|
178 self.dostatements = statements
|
|
179 def __repr__(self):
|
|
180 return 'WHILE-statement'
|
|
181
|