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