Mercurial > lcfOS
annotate python/c3/astnodes.py @ 226:240111e0456f
Work on named types
author | Windel Bouwman |
---|---|
date | Fri, 12 Jul 2013 17:25:31 +0200 |
parents | 1c7364bd74c7 |
children | 82dfe6a32717 |
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: | |
35 - base type | |
36 - struct type | |
37 - pointer type | |
38 - typedef type | |
39 - function type | |
148 | 40 """ |
41 | |
149 | 42 class Type(Node): |
148 | 43 def isType(self, b): |
44 return isType(self, b) | |
45 | |
46 class BaseType(Type): | |
47 def __init__(self, name): | |
48 self.name = name | |
49 def __repr__(self): | |
212 | 50 return '{}'.format(self.name) |
148 | 51 |
52 class FunctionType(Type): | |
167 | 53 def __init__(self, parametertypes, returntype): |
54 self.parametertypes = parametertypes | |
148 | 55 self.returntype = returntype |
56 def __repr__(self): | |
167 | 57 params = ', '.join([str(v) for v in self.parametertypes]) |
165 | 58 return '{1} f({0})'.format(params, self.returntype) |
148 | 59 |
213 | 60 class PointerType(Type): |
61 def __init__(self, ptype): | |
225 | 62 assert isinstance(ptype, Type) or isinstance(ptype, Designator) |
213 | 63 self.ptype = ptype |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
64 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
65 return '({}*)'.format(self.ptype) |
213 | 66 |
67 class StructureType(Type): | |
68 def __init__(self, mems): | |
69 self.mems = mems | |
226 | 70 for ft, fn in mems: |
71 assert type(fn) is str | |
225 | 72 def hasField(self, name): |
226 | 73 for ft, fn in self.mems: |
225 | 74 if name == fn: |
75 return True | |
76 return False | |
77 def fieldType(self, name): | |
226 | 78 for ft, fn in self.mems: |
225 | 79 if name == fn: |
80 return ft | |
81 raise Exception() | |
82 | |
213 | 83 |
148 | 84 class DefinedType(Type): |
225 | 85 def __init__(self, name, typ, loc): |
86 assert isinstance(name, str) | |
87 self.name = name | |
88 self.typ = typ | |
89 self.loc = loc | |
90 def __repr__(self): | |
91 return 'Named type {0} of type {1}'.format(self.name, self.typ) | |
148 | 92 |
222 | 93 class TypeCast(Node): |
94 def __init__(self, to_type, x): | |
95 self.to_type = to_type | |
96 self.a = x | |
97 def __repr__(self): | |
98 return 'TYPECAST' | |
221 | 99 |
148 | 100 # Variables, parameters, local variables, constants: |
101 class Symbol(Node): | |
213 | 102 def __init__(self, name): |
163 | 103 self.name = name |
104 self.refs = [] | |
213 | 105 def addRef(self, r): |
163 | 106 self.refs.append(r) |
213 | 107 @property |
108 def References(self): | |
163 | 109 return self.refs |
148 | 110 |
111 class Constant(Symbol): | |
213 | 112 def __init__(self, name, typ, value): |
163 | 113 super().__init__(name) |
114 self.typ = typ | |
148 | 115 self.value = value |
213 | 116 def __repr__(self): |
148 | 117 return 'CONSTANT {0} = {1}'.format(self.name, self.value) |
118 | |
119 class Variable(Symbol): | |
213 | 120 def __init__(self, name, typ): |
163 | 121 super().__init__(name) |
148 | 122 self.typ = typ |
213 | 123 self.ival = None |
148 | 124 self.isLocal = False |
125 self.isReadOnly = False | |
126 self.isParameter = False | |
127 def __repr__(self): | |
225 | 128 return 'Var {} [{}]'.format(self.name, self.typ) |
148 | 129 |
150 | 130 # Procedure types |
131 class Function(Symbol): | |
213 | 132 """ Actual implementation of a function """ |
133 def __init__(self, name, loc): | |
134 super().__init__(name) | |
135 self.loc = loc | |
215 | 136 self.declarations = [] |
213 | 137 |
138 def __repr__(self): | |
217 | 139 return 'Func {}'.format(self.name) |
148 | 140 |
163 | 141 # Operations / Expressions: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
142 class Expression(Node): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
143 pass |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
144 |
225 | 145 class Deref(Expression): |
146 def __init__(self, ptr, loc): | |
147 assert isinstance(ptr, Expression) | |
148 self.ptr = ptr | |
149 self.loc = loc | |
150 def __repr__(self): | |
151 return 'DEREF {}'.format(self.ptr) | |
152 | |
153 class FieldRef(Expression): | |
154 def __init__(self, base, field, loc): | |
155 assert isinstance(base, Expression) | |
156 assert isinstance(field, str) | |
157 self.base = base | |
158 self.field = field | |
159 self.loc = loc | |
160 def __repr__(self): | |
161 return 'FIELD {}.{}'.format(self.base, self.field) | |
162 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
163 class Unop(Expression): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
164 def __init__(self, op, a, loc): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
165 assert isinstance(a, Expression) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
166 assert isinstance(op, str) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
167 self.a = a |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
168 self.op = op |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
169 self.loc = loc |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
170 def __repr__(self): |
212 | 171 return 'UNOP {}'.format(self.op) |
148 | 172 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
173 class Binop(Expression): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
174 def __init__(self, a, op, b, loc): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
175 assert isinstance(a, Expression), type(a) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
176 assert isinstance(b, Expression) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
177 assert isinstance(op, str) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
178 self.a = a |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
179 self.b = b |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
180 self.op = op # Operation: '+', '-', '*', '/', 'mod' |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
181 self.loc = loc |
148 | 182 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
183 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
184 return 'BINOP {}'.format(self.op) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
185 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
186 class VariableUse(Expression): |
213 | 187 def __init__(self, target, loc): |
150 | 188 self.target = target |
213 | 189 self.loc = loc |
150 | 190 def __repr__(self): |
163 | 191 nm = self.target.name if hasattr(self.target, 'name') else '' |
212 | 192 return 'VAR USE {}'.format(nm) |
163 | 193 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
194 class Literal(Expression): |
213 | 195 def __init__(self, val, loc): |
163 | 196 self.val = val |
213 | 197 self.loc = loc |
163 | 198 def __repr__(self): |
212 | 199 return 'LITERAL {}'.format(self.val) |
150 | 200 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
201 class FunctionCall(Expression): |
213 | 202 def __init__(self, proc, args, loc): |
148 | 203 self.proc = proc |
204 self.args = args | |
213 | 205 self.loc = loc |
148 | 206 def __repr__(self): |
207 return 'CALL {0} '.format(self.proc) | |
208 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
209 # Statements |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
210 class Statement(Node): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
211 pass |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
212 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
213 class CompoundStatement(Statement): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
214 def __init__(self, statements): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
215 self.statements = statements |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
216 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
217 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
218 return 'COMPOUND STATEMENT' |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
219 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
220 class EmptyStatement(Statement): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
221 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
222 return 'NOP' |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
223 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
224 class ReturnStatement(Statement): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
225 def __init__(self, expr, loc): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
226 self.expr = expr |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
227 self.loc = loc |
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 'RETURN STATEMENT' |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
230 |
222 | 231 class Assignment(Statement): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
232 def __init__(self, lval, rval, loc): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
233 assert isinstance(lval, Node) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
234 assert isinstance(rval, Node) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
235 assert isinstance(loc, SourceLocation) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
236 self.lval = lval |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
237 self.rval = rval |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
238 self.loc = loc |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
239 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
240 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
241 return 'ASSIGNMENT' |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
242 |
222 | 243 class ExpressionStatement(Statement): |
244 def __init__(self, ex, loc): | |
245 self.ex = ex | |
246 self.loc = loc | |
247 assert isinstance(loc, SourceLocation) | |
248 def __repr__(self): | |
249 return 'Epression' | |
250 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
251 class IfStatement(Statement): |
213 | 252 def __init__(self, condition, truestatement, falsestatement, loc): |
148 | 253 self.condition = condition |
254 self.truestatement = truestatement | |
255 self.falsestatement = falsestatement | |
213 | 256 self.loc = loc |
148 | 257 def __repr__(self): |
258 return 'IF-statement' | |
259 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
260 class WhileStatement(Statement): |
213 | 261 def __init__(self, condition, statement, loc): |
148 | 262 self.condition = condition |
213 | 263 self.statement = statement |
264 self.loc = loc | |
148 | 265 def __repr__(self): |
266 return 'WHILE-statement' | |
267 |