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