Mercurial > lcfOS
annotate python/c3/astnodes.py @ 228:7f18ed9b6b7e
Removal of emptystatement class
author | Windel Bouwman |
---|---|
date | Sat, 13 Jul 2013 11:12:24 +0200 |
parents | 82dfe6a32717 |
children | 88a1e0baef65 |
rev | line source |
---|---|
148 | 1 """ |
213 | 2 AST (abstract syntax tree) nodes for the c3 language. |
3 The tree is build by the parser. | |
4 Then it is checked | |
5 Finally code is generated from it. | |
148 | 6 """ |
7 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
8 from ppci import SourceLocation |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
9 |
148 | 10 class Node: |
163 | 11 pass |
148 | 12 |
213 | 13 # Modules |
14 class Package(Node): | |
15 def __init__(self, name, loc): | |
16 self.name = name | |
17 self.loc = loc | |
18 self.declarations = [] | |
19 def __repr__(self): | |
20 return 'PACKAGE {}'.format(self.name) | |
21 | |
150 | 22 class Designator(Node): |
213 | 23 def __init__(self, tname, loc): |
24 self.tname = tname | |
25 self.loc = loc | |
26 def __repr__(self): | |
27 return 'DESIGNATOR {}'.format(self.tname) | |
148 | 28 |
29 """ | |
30 Type classes | |
213 | 31 |
32 types must be comparable. | |
33 | |
34 There are the following types: | |
228 | 35 - base type -> basic type (built in) |
36 - struct type -> a composite type that contains a list of named fields | |
37 of other types | |
38 - pointer type -> a type that points to some other type | |
39 - typedef type -> a named type indicating another type | |
213 | 40 - function type |
148 | 41 """ |
42 | |
149 | 43 class Type(Node): |
148 | 44 def isType(self, b): |
45 return isType(self, b) | |
46 | |
47 class BaseType(Type): | |
48 def __init__(self, name): | |
49 self.name = name | |
50 def __repr__(self): | |
212 | 51 return '{}'.format(self.name) |
148 | 52 |
53 class FunctionType(Type): | |
167 | 54 def __init__(self, parametertypes, returntype): |
55 self.parametertypes = parametertypes | |
148 | 56 self.returntype = returntype |
57 def __repr__(self): | |
167 | 58 params = ', '.join([str(v) for v in self.parametertypes]) |
165 | 59 return '{1} f({0})'.format(params, self.returntype) |
148 | 60 |
213 | 61 class PointerType(Type): |
62 def __init__(self, ptype): | |
225 | 63 assert isinstance(ptype, Type) or isinstance(ptype, Designator) |
213 | 64 self.ptype = ptype |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
65 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
66 return '({}*)'.format(self.ptype) |
213 | 67 |
227 | 68 class StructField: |
69 def __init__(self, name, typ): | |
70 self.name = name | |
71 self.typ = typ | |
72 | |
213 | 73 class StructureType(Type): |
74 def __init__(self, mems): | |
75 self.mems = mems | |
227 | 76 for mem in mems: |
77 assert type(mem) is StructField | |
78 assert type(mem.name) is str | |
225 | 79 def hasField(self, name): |
227 | 80 for mem in self.mems: |
81 if name == mem.name: | |
225 | 82 return True |
83 return False | |
84 def fieldType(self, name): | |
227 | 85 for mem in self.mems: |
86 if name == mem.name: | |
87 return mem.typ | |
225 | 88 raise Exception() |
89 | |
213 | 90 |
148 | 91 class DefinedType(Type): |
225 | 92 def __init__(self, name, typ, loc): |
93 assert isinstance(name, str) | |
94 self.name = name | |
95 self.typ = typ | |
96 self.loc = loc | |
97 def __repr__(self): | |
98 return 'Named type {0} of type {1}'.format(self.name, self.typ) | |
148 | 99 |
222 | 100 class TypeCast(Node): |
101 def __init__(self, to_type, x): | |
102 self.to_type = to_type | |
103 self.a = x | |
104 def __repr__(self): | |
105 return 'TYPECAST' | |
221 | 106 |
148 | 107 # Variables, parameters, local variables, constants: |
108 class Symbol(Node): | |
213 | 109 def __init__(self, name): |
163 | 110 self.name = name |
111 self.refs = [] | |
213 | 112 def addRef(self, r): |
163 | 113 self.refs.append(r) |
213 | 114 @property |
115 def References(self): | |
163 | 116 return self.refs |
148 | 117 |
118 class Constant(Symbol): | |
213 | 119 def __init__(self, name, typ, value): |
163 | 120 super().__init__(name) |
121 self.typ = typ | |
148 | 122 self.value = value |
213 | 123 def __repr__(self): |
148 | 124 return 'CONSTANT {0} = {1}'.format(self.name, self.value) |
125 | |
126 class Variable(Symbol): | |
213 | 127 def __init__(self, name, typ): |
163 | 128 super().__init__(name) |
148 | 129 self.typ = typ |
213 | 130 self.ival = None |
148 | 131 self.isLocal = False |
132 self.isReadOnly = False | |
133 self.isParameter = False | |
134 def __repr__(self): | |
225 | 135 return 'Var {} [{}]'.format(self.name, self.typ) |
148 | 136 |
150 | 137 # Procedure types |
138 class Function(Symbol): | |
213 | 139 """ Actual implementation of a function """ |
140 def __init__(self, name, loc): | |
141 super().__init__(name) | |
142 self.loc = loc | |
215 | 143 self.declarations = [] |
213 | 144 |
145 def __repr__(self): | |
217 | 146 return 'Func {}'.format(self.name) |
148 | 147 |
163 | 148 # Operations / Expressions: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
149 class Expression(Node): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
150 pass |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
151 |
225 | 152 class Deref(Expression): |
153 def __init__(self, ptr, loc): | |
154 assert isinstance(ptr, Expression) | |
155 self.ptr = ptr | |
156 self.loc = loc | |
157 def __repr__(self): | |
158 return 'DEREF {}'.format(self.ptr) | |
159 | |
160 class FieldRef(Expression): | |
161 def __init__(self, base, field, loc): | |
162 assert isinstance(base, Expression) | |
163 assert isinstance(field, str) | |
164 self.base = base | |
165 self.field = field | |
166 self.loc = loc | |
167 def __repr__(self): | |
168 return 'FIELD {}.{}'.format(self.base, self.field) | |
169 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
170 class Unop(Expression): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
171 def __init__(self, op, a, loc): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
172 assert isinstance(a, Expression) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
173 assert isinstance(op, str) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
174 self.a = a |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
175 self.op = op |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
176 self.loc = loc |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
177 def __repr__(self): |
212 | 178 return 'UNOP {}'.format(self.op) |
148 | 179 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
180 class Binop(Expression): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
181 def __init__(self, a, op, b, loc): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
182 assert isinstance(a, Expression), type(a) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
183 assert isinstance(b, Expression) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
184 assert isinstance(op, str) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
185 self.a = a |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
186 self.b = b |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
187 self.op = op # Operation: '+', '-', '*', '/', 'mod' |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
188 self.loc = loc |
148 | 189 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
190 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
191 return 'BINOP {}'.format(self.op) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
192 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
193 class VariableUse(Expression): |
228 | 194 def __init__(self, target, loc): |
195 self.target = target | |
196 self.loc = loc | |
197 def __repr__(self): | |
198 nm = self.target | |
199 return 'VAR USE {}'.format(nm) | |
163 | 200 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
201 class Literal(Expression): |
228 | 202 def __init__(self, val, loc): |
203 self.val = val | |
204 self.loc = loc | |
205 def __repr__(self): | |
206 return 'LITERAL {}'.format(self.val) | |
150 | 207 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
208 class FunctionCall(Expression): |
228 | 209 def __init__(self, proc, args, loc): |
210 self.proc = proc | |
211 self.args = args | |
212 self.loc = loc | |
213 def __repr__(self): | |
214 return 'CALL {0} '.format(self.proc) | |
148 | 215 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
216 # Statements |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
217 class Statement(Node): |
228 | 218 def __init__(self, loc): |
219 self.loc = loc | |
220 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
221 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
222 class CompoundStatement(Statement): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
223 def __init__(self, statements): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
224 self.statements = statements |
228 | 225 for s in self.statements: |
226 assert isinstance(s, Statement) | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
227 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
228 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
229 return 'COMPOUND STATEMENT' |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
230 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
231 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
232 class ReturnStatement(Statement): |
228 | 233 def __init__(self, expr, loc): |
234 super().__init__(loc) | |
235 self.expr = expr | |
236 | |
237 def __repr__(self): | |
238 return 'RETURN STATEMENT' | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
239 |
222 | 240 class Assignment(Statement): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
241 def __init__(self, lval, rval, loc): |
228 | 242 super().__init__(loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
243 assert isinstance(lval, Node) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
244 assert isinstance(rval, Node) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
245 self.lval = lval |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
246 self.rval = rval |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
247 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
248 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
249 return 'ASSIGNMENT' |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
250 |
222 | 251 class ExpressionStatement(Statement): |
252 def __init__(self, ex, loc): | |
228 | 253 super().__init__(loc) |
222 | 254 self.ex = ex |
228 | 255 |
222 | 256 def __repr__(self): |
257 return 'Epression' | |
258 | |
228 | 259 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
260 class IfStatement(Statement): |
228 | 261 def __init__(self, condition, truestatement, falsestatement, loc): |
262 super().__init__(loc) | |
263 self.condition = condition | |
264 self.truestatement = truestatement | |
265 self.falsestatement = falsestatement | |
266 | |
267 def __repr__(self): | |
268 return 'IF-statement' | |
269 | |
148 | 270 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
271 class WhileStatement(Statement): |
228 | 272 def __init__(self, condition, statement, loc): |
273 super().__init__(loc) | |
274 self.condition = condition | |
275 self.statement = statement | |
148 | 276 |
228 | 277 def __repr__(self): |
278 return 'WHILE-statement' | |
279 |