Mercurial > lcfOS
comparison python/c3/astnodes.py @ 220:3f6c30a5d234
Major change in expression parsing to enable pointers and structs
author | Windel Bouwman |
---|---|
date | Sat, 06 Jul 2013 21:32:20 +0200 |
parents | 8b2e5f3cd579 |
children | 848c4b15fd0b |
comparison
equal
deleted
inserted
replaced
219:1fa3e0050b49 | 220:3f6c30a5d234 |
---|---|
2 AST (abstract syntax tree) nodes for the c3 language. | 2 AST (abstract syntax tree) nodes for the c3 language. |
3 The tree is build by the parser. | 3 The tree is build by the parser. |
4 Then it is checked | 4 Then it is checked |
5 Finally code is generated from it. | 5 Finally code is generated from it. |
6 """ | 6 """ |
7 | |
8 from ppci import SourceLocation | |
7 | 9 |
8 class Node: | 10 class Node: |
9 pass | 11 pass |
10 | 12 |
11 # Modules | 13 # Modules |
56 return '{1} f({0})'.format(params, self.returntype) | 58 return '{1} f({0})'.format(params, self.returntype) |
57 | 59 |
58 class PointerType(Type): | 60 class PointerType(Type): |
59 def __init__(self, ptype): | 61 def __init__(self, ptype): |
60 self.ptype = ptype | 62 self.ptype = ptype |
63 def __repr__(self): | |
64 return '({}*)'.format(self.ptype) | |
61 | 65 |
62 class StructureType(Type): | 66 class StructureType(Type): |
63 def __init__(self, mems): | 67 def __init__(self, mems): |
64 self.mems = mems | 68 self.mems = mems |
65 | 69 |
110 | 114 |
111 def __repr__(self): | 115 def __repr__(self): |
112 return 'Func {}'.format(self.name) | 116 return 'Func {}'.format(self.name) |
113 | 117 |
114 # Operations / Expressions: | 118 # Operations / Expressions: |
115 class Unop(Node): | 119 class Expression(Node): |
116 def __init__(self, a, op): | 120 pass |
117 self.a = a | 121 |
118 self.op = op | 122 class Unop(Expression): |
119 def __repr__(self): | 123 def __init__(self, op, a, loc): |
124 assert isinstance(a, Expression) | |
125 assert isinstance(op, str) | |
126 self.a = a | |
127 self.op = op | |
128 self.loc = loc | |
129 def __repr__(self): | |
120 return 'UNOP {}'.format(self.op) | 130 return 'UNOP {}'.format(self.op) |
121 | 131 |
122 class Binop(Node): | 132 class Binop(Expression): |
123 def __init__(self, a, op, b, loc): | 133 def __init__(self, a, op, b, loc): |
124 self.a = a | 134 assert isinstance(a, Expression), type(a) |
125 self.b = b | 135 assert isinstance(b, Expression) |
126 self.op = op # Operation: '+', '-', '*', '/', 'mod' | 136 assert isinstance(op, str) |
127 self.loc = loc | 137 self.a = a |
128 def __repr__(self): | 138 self.b = b |
129 return 'BINOP {}'.format(self.op) | 139 self.op = op # Operation: '+', '-', '*', '/', 'mod' |
130 | 140 self.loc = loc |
131 class VariableUse(Node): | 141 |
142 def __repr__(self): | |
143 return 'BINOP {}'.format(self.op) | |
144 | |
145 class VariableUse(Expression): | |
132 def __init__(self, target, loc): | 146 def __init__(self, target, loc): |
133 self.target = target | 147 self.target = target |
134 self.loc = loc | 148 self.loc = loc |
135 def __repr__(self): | 149 def __repr__(self): |
136 nm = self.target.name if hasattr(self.target, 'name') else '' | 150 nm = self.target.name if hasattr(self.target, 'name') else '' |
137 return 'VAR USE {}'.format(nm) | 151 return 'VAR USE {}'.format(nm) |
138 | 152 |
139 class Literal(Node): | 153 class Literal(Expression): |
140 def __init__(self, val, loc): | 154 def __init__(self, val, loc): |
141 self.val = val | 155 self.val = val |
142 self.loc = loc | 156 self.loc = loc |
143 def __repr__(self): | 157 def __repr__(self): |
144 return 'LITERAL {}'.format(self.val) | 158 return 'LITERAL {}'.format(self.val) |
145 | 159 |
146 | 160 class FunctionCall(Expression): |
147 # Statements | |
148 class CompoundStatement(Node): | |
149 def __init__(self, statements): | |
150 self.statements = statements | |
151 def __repr__(self): | |
152 return 'COMPOUND STATEMENT' | |
153 | |
154 class EmptyStatement(Node): | |
155 def __repr__(self): | |
156 return 'NOP' | |
157 | |
158 class ReturnStatement(Node): | |
159 def __init__(self, expr): | |
160 self.expr = expr | |
161 def __repr__(self): | |
162 return 'RETURN STATEMENT' | |
163 | |
164 class Assignment(Node): | |
165 def __init__(self, lval, rval, loc): | |
166 self.lval = lval | |
167 self.rval = rval | |
168 self.loc = loc | |
169 def __repr__(self): | |
170 return 'ASSIGNMENT' | |
171 | |
172 class FunctionCall(Node): | |
173 def __init__(self, proc, args, loc): | 161 def __init__(self, proc, args, loc): |
174 self.proc = proc | 162 self.proc = proc |
175 self.args = args | 163 self.args = args |
176 self.loc = loc | 164 self.loc = loc |
177 def __repr__(self): | 165 def __repr__(self): |
178 return 'CALL {0} '.format(self.proc) | 166 return 'CALL {0} '.format(self.proc) |
179 | 167 |
180 class IfStatement(Node): | 168 # Statements |
169 class Statement(Node): | |
170 pass | |
171 | |
172 class CompoundStatement(Statement): | |
173 def __init__(self, statements): | |
174 self.statements = statements | |
175 | |
176 def __repr__(self): | |
177 return 'COMPOUND STATEMENT' | |
178 | |
179 class EmptyStatement(Statement): | |
180 def __repr__(self): | |
181 return 'NOP' | |
182 | |
183 class ReturnStatement(Statement): | |
184 def __init__(self, expr, loc): | |
185 self.expr = expr | |
186 self.loc = loc | |
187 def __repr__(self): | |
188 return 'RETURN STATEMENT' | |
189 | |
190 class Assignment(Node): | |
191 def __init__(self, lval, rval, loc): | |
192 assert isinstance(lval, Node) | |
193 assert isinstance(rval, Node) | |
194 assert isinstance(loc, SourceLocation) | |
195 self.lval = lval | |
196 self.rval = rval | |
197 self.loc = loc | |
198 | |
199 def __repr__(self): | |
200 return 'ASSIGNMENT' | |
201 | |
202 class IfStatement(Statement): | |
181 def __init__(self, condition, truestatement, falsestatement, loc): | 203 def __init__(self, condition, truestatement, falsestatement, loc): |
182 self.condition = condition | 204 self.condition = condition |
183 self.truestatement = truestatement | 205 self.truestatement = truestatement |
184 self.falsestatement = falsestatement | 206 self.falsestatement = falsestatement |
185 self.loc = loc | 207 self.loc = loc |
186 def __repr__(self): | 208 def __repr__(self): |
187 return 'IF-statement' | 209 return 'IF-statement' |
188 | 210 |
189 class WhileStatement(Node): | 211 class WhileStatement(Statement): |
190 def __init__(self, condition, statement, loc): | 212 def __init__(self, condition, statement, loc): |
191 self.condition = condition | 213 self.condition = condition |
192 self.statement = statement | 214 self.statement = statement |
193 self.loc = loc | 215 self.loc = loc |
194 def __repr__(self): | 216 def __repr__(self): |