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