Mercurial > lcfOS
annotate python/ppci/c3/parser.py @ 392:bb4289c84907
Added some sort of drop event test
author | Windel Bouwman |
---|---|
date | Fri, 16 May 2014 13:05:10 +0200 |
parents | c05ab629976a |
children | 6ae782a085e0 |
rev | line source |
---|---|
254 | 1 import logging |
354 | 2 from .. import CompilerError |
306 | 3 from .astnodes import Member, Literal, TypeCast, Unop, Binop |
307 | 4 from .astnodes import Assignment, ExpressionStatement, Compound |
315 | 5 from .astnodes import Return, While, If, Empty, For |
296 | 6 from .astnodes import FunctionType, Function, FormalParameter |
354 | 7 from .astnodes import StructureType, DefinedType, PointerType, ArrayType |
296 | 8 from .astnodes import Constant, Variable |
354 | 9 from .astnodes import StructField, Deref, Index |
306 | 10 from .astnodes import Package |
11 from .astnodes import Identifier | |
12 from .astnodes import FunctionCall | |
148 | 13 |
288 | 14 |
148 | 15 class Parser: |
213 | 16 """ Parses sourcecode into an abstract syntax tree (AST) """ |
17 def __init__(self, diag): | |
254 | 18 self.logger = logging.getLogger('c3') |
213 | 19 self.diag = diag |
20 | |
306 | 21 def parseSource(self, tokens): |
334 | 22 self.logger.debug('Parsing source') |
306 | 23 self.tokens = tokens |
24 self.token = self.tokens.__next__() | |
254 | 25 try: |
26 self.parsePackage() | |
306 | 27 self.mod.ok = True # Valid until proven wrong :) |
254 | 28 return self.mod |
29 except CompilerError as e: | |
30 self.diag.addDiag(e) | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
31 |
213 | 32 def Error(self, msg): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
33 raise CompilerError(msg, self.token.loc) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
34 |
213 | 35 # Lexer helpers: |
36 def Consume(self, typ): | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
37 if self.Peak == typ: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
38 return self.NextToken() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
39 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
40 self.Error('Excected: "{0}", got "{1}"'.format(typ, self.Peak)) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
41 |
213 | 42 @property |
43 def Peak(self): | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
44 return self.token.typ |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
45 |
228 | 46 @property |
47 def CurLoc(self): | |
48 return self.token.loc | |
49 | |
213 | 50 def hasConsumed(self, typ): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
51 if self.Peak == typ: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
52 self.Consume(typ) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
53 return True |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
54 return False |
213 | 55 |
56 def NextToken(self): | |
219 | 57 t = self.token |
58 if t.typ != 'END': | |
59 self.token = self.tokens.__next__() | |
60 return t | |
213 | 61 |
215 | 62 def addDeclaration(self, decl): |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
334
diff
changeset
|
63 self.currentPart.add_declaration(decl) |
288 | 64 |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
65 def parseImport(self): |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
66 self.Consume('import') |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
67 name = self.Consume('ID').val |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
68 self.mod.imports.append(name) |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
69 self.Consume(';') |
213 | 70 |
71 def parsePackage(self): | |
284 | 72 self.Consume('module') |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
73 name = self.Consume('ID') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
74 self.Consume(';') |
296 | 75 self.mod = Package(name.val, name.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
76 self.currentPart = self.mod |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
77 while self.Peak != 'END': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
78 self.parseTopLevel() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
79 self.Consume('END') |
148 | 80 |
213 | 81 def parseTopLevel(self): |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
82 if self.Peak == 'function': |
362 | 83 self.parse_function_def() |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
84 elif self.Peak == 'var': |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
85 self.parseVarDef() |
306 | 86 # TODO handle variable initialization |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
87 elif self.Peak == 'const': |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
88 self.parseConstDef() |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
89 elif self.Peak == 'type': |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
90 self.parseTypeDef() |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
91 elif self.Peak == 'import': |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
92 self.parseImport() |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
93 else: |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
94 self.Error('Expected function, var, const or type') |
148 | 95 |
213 | 96 def parseDesignator(self): |
301 | 97 """ A designator designates an object with a name. """ |
225 | 98 name = self.Consume('ID') |
306 | 99 return Identifier(name.val, name.loc) |
100 | |
101 def parseIdSequence(self): | |
102 ids = [self.Consume('ID')] | |
103 while self.hasConsumed(','): | |
104 ids.append(self.Consume('ID')) | |
105 return ids | |
148 | 106 |
213 | 107 # Type system |
354 | 108 def PostFixId(self): |
109 pfe = self.PrimaryExpression_Id() | |
110 while self.Peak in ['.']: | |
111 if self.hasConsumed('.'): | |
112 field = self.Consume('ID') | |
113 pfe = Member(pfe, field.val, field.loc) | |
114 else: | |
115 raise Exception() | |
116 return pfe | |
117 | |
118 def PrimaryExpression_Id(self): | |
119 if self.Peak == 'ID': | |
120 return self.parseDesignator() | |
121 self.Error('Expected ID, got {0}'.format(self.Peak)) | |
122 | |
123 def parse_type_spec(self): | |
124 """ Parse type specification """ | |
213 | 125 if self.Peak == 'struct': |
126 self.Consume('struct') | |
127 self.Consume('{') | |
128 mems = [] | |
129 while self.Peak != '}': | |
354 | 130 mem_t = self.parse_type_spec() |
306 | 131 for i in self.parseIdSequence(): |
132 mems.append(StructField(i.val, mem_t)) | |
213 | 133 self.Consume(';') |
134 self.Consume('}') | |
296 | 135 theT = StructureType(mems) |
306 | 136 elif self.Peak == 'enum': |
137 # TODO) | |
138 raise NotImplementedError() | |
213 | 139 else: |
354 | 140 theT = self.PostFixId() |
141 | |
142 # Check for pointer or array suffix: | |
143 while self.Peak in ['*', '[']: | |
144 if self.hasConsumed('*'): | |
145 theT = PointerType(theT) | |
146 elif self.hasConsumed('['): | |
147 if self.Peak == ']': | |
148 size = 0 | |
149 self.Consume(']') | |
150 else: | |
151 size = self.Expression() | |
152 self.Consume(']') | |
153 theT = ArrayType(theT, size) | |
154 else: | |
155 raise Exception() | |
213 | 156 return theT |
157 | |
158 def parseTypeDef(self): | |
159 self.Consume('type') | |
354 | 160 newtype = self.parse_type_spec() |
213 | 161 typename = self.Consume('ID') |
162 self.Consume(';') | |
296 | 163 df = DefinedType(typename.val, newtype, typename.loc) |
225 | 164 self.addDeclaration(df) |
213 | 165 |
166 # Variable declarations: | |
167 def parseVarDef(self): | |
272 | 168 self.Consume('var') |
354 | 169 t = self.parse_type_spec() |
306 | 170 for name in self.parseIdSequence(): |
296 | 171 v = Variable(name.val, t) |
272 | 172 v.loc = name.loc |
173 self.addDeclaration(v) | |
174 self.Consume(';') | |
307 | 175 return Empty() |
148 | 176 |
213 | 177 def parseConstDef(self): |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
178 self.Consume('const') |
354 | 179 t = self.parse_type_spec() |
306 | 180 while True: |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
181 name = self.Consume('ID') |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
182 self.Consume('=') |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
183 val = self.Expression() |
296 | 184 c = Constant(name.val, t, val) |
313 | 185 self.addDeclaration(c) |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
186 c.loc = name.loc |
306 | 187 if not self.hasConsumed(','): |
188 break | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
189 self.Consume(';') |
288 | 190 |
362 | 191 def parse_function_def(self): |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
192 loc = self.Consume('function').loc |
354 | 193 returntype = self.parse_type_spec() |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
194 fname = self.Consume('ID').val |
296 | 195 f = Function(fname, loc) |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
196 self.addDeclaration(f) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
197 savePart = self.currentPart |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
198 self.currentPart = f |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
199 self.Consume('(') |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
200 parameters = [] |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
201 if not self.hasConsumed(')'): |
306 | 202 while True: |
354 | 203 typ = self.parse_type_spec() |
272 | 204 name = self.Consume('ID') |
296 | 205 param = FormalParameter(name.val, typ) |
272 | 206 param.loc = name.loc |
207 self.addDeclaration(param) | |
208 parameters.append(param) | |
306 | 209 if not self.hasConsumed(','): |
210 break | |
272 | 211 self.Consume(')') |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
212 paramtypes = [p.typ for p in parameters] |
296 | 213 f.typ = FunctionType(paramtypes, returntype) |
362 | 214 if self.Peak == ';': |
215 self.Consume(';') | |
216 f.body = None | |
217 else: | |
218 f.body = self.parseCompound() | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
219 self.currentPart = savePart |
148 | 220 |
354 | 221 def parse_if(self): |
272 | 222 loc = self.Consume('if').loc |
223 self.Consume('(') | |
224 condition = self.Expression() | |
225 self.Consume(')') | |
306 | 226 yes = self.Statement() |
307 | 227 no = self.Statement() if self.hasConsumed('else') else Empty() |
228 return If(condition, yes, no, loc) | |
148 | 229 |
307 | 230 def parseWhile(self): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
231 loc = self.Consume('while').loc |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
232 self.Consume('(') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
233 condition = self.Expression() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
234 self.Consume(')') |
306 | 235 statements = self.Statement() |
307 | 236 return While(condition, statements, loc) |
149 | 237 |
354 | 238 def parse_for(self): |
315 | 239 loc = self.Consume('for').loc |
240 self.Consume('(') | |
241 init = self.Statement() | |
242 self.Consume(';') | |
243 condition = self.Expression() | |
244 self.Consume(';') | |
245 final = self.Statement() | |
246 self.Consume(')') | |
247 statements = self.Statement() | |
248 return For(init, condition, final, statements, loc) | |
249 | |
307 | 250 def parseReturn(self): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
251 loc = self.Consume('return').loc |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
252 if self.Peak == ';': |
296 | 253 expr = Literal(0, loc) |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
254 else: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
255 expr = self.Expression() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
256 self.Consume(';') |
307 | 257 return Return(expr, loc) |
148 | 258 |
307 | 259 def parseCompound(self): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
260 self.Consume('{') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
261 statements = [] |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
262 while not self.hasConsumed('}'): |
306 | 263 statements.append(self.Statement()) |
307 | 264 return Compound(statements) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
265 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
266 def Statement(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
267 # Determine statement type based on the pending token: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
268 if self.Peak == 'if': |
354 | 269 return self.parse_if() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
270 elif self.Peak == 'while': |
307 | 271 return self.parseWhile() |
315 | 272 elif self.Peak == 'for': |
354 | 273 return self.parse_for() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
274 elif self.Peak == '{': |
307 | 275 return self.parseCompound() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
276 elif self.hasConsumed(';'): |
307 | 277 return Empty() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
278 elif self.Peak == 'var': |
306 | 279 return self.parseVarDef() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
280 elif self.Peak == 'return': |
307 | 281 return self.parseReturn() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
282 else: |
306 | 283 x = self.UnaryExpression() |
284 if self.Peak == '=': | |
285 # We enter assignment mode here. | |
286 loc = self.Consume('=').loc | |
287 rhs = self.Expression() | |
288 return Assignment(x, rhs, loc) | |
289 else: | |
290 return ExpressionStatement(x, x.loc) | |
148 | 291 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
292 # Expression section: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
293 # We not implement these C constructs: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
294 # a(2), f = 2 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
295 # and this: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
296 # a = 2 < x : 4 ? 1; |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
297 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
298 def Expression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
299 exp = self.LogicalAndExpression() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
300 while self.Peak == 'or': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
301 loc = self.Consume('or').loc |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
302 e2 = self.LogicalAndExpression() |
296 | 303 exp = Binop(exp, 'or', e2, loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
304 return exp |
148 | 305 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
306 def LogicalAndExpression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
307 o = self.EqualityExpression() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
308 while self.Peak == 'and': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
309 loc = self.Consume('and').loc |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
310 o2 = self.EqualityExpression() |
296 | 311 o = Binop(o, 'and', o2, loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
312 return o |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
313 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
314 def EqualityExpression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
315 ee = self.SimpleExpression() |
300 | 316 while self.Peak in ['<', '==', '>', '>=', '<=', '!=']: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
317 op = self.Consume(self.Peak) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
318 ee2 = self.SimpleExpression() |
296 | 319 ee = Binop(ee, op.typ, ee2, op.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
320 return ee |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
321 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
322 def SimpleExpression(self): |
232 | 323 """ Shift operations before + and - ? """ |
324 e = self.AddExpression() | |
325 while self.Peak in ['>>', '<<']: | |
326 op = self.Consume(self.Peak) | |
327 e2 = self.AddExpression() | |
296 | 328 e = Binop(e, op.typ, e2, op.loc) |
232 | 329 return e |
330 | |
331 def AddExpression(self): | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
332 e = self.Term() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
333 while self.Peak in ['+', '-']: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
334 op = self.Consume(self.Peak) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
335 e2 = self.Term() |
296 | 336 e = Binop(e, op.typ, e2, op.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
337 return e |
213 | 338 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
339 def Term(self): |
221 | 340 t = self.BitwiseOr() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
341 while self.Peak in ['*', '/']: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
342 op = self.Consume(self.Peak) |
221 | 343 t2 = self.BitwiseOr() |
296 | 344 t = Binop(t, op.typ, t2, op.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
345 return t |
221 | 346 |
347 def BitwiseOr(self): | |
348 a = self.BitwiseAnd() | |
306 | 349 while self.Peak == '|': |
221 | 350 op = self.Consume(self.Peak) |
351 b = self.BitwiseAnd() | |
296 | 352 a = Binop(a, op.typ, b, op.loc) |
221 | 353 return a |
354 | |
355 def BitwiseAnd(self): | |
356 a = self.CastExpression() | |
306 | 357 while self.Peak == '&': |
221 | 358 op = self.Consume(self.Peak) |
359 b = self.CastExpression() | |
296 | 360 a = Binop(a, op.typ, b, op.loc) |
221 | 361 return a |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
362 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
363 # Domain of unary expressions: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
364 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
365 def CastExpression(self): |
228 | 366 """ |
367 the C-style type cast conflicts with '(' expr ')' | |
368 so introduce extra keyword 'cast' | |
369 """ | |
221 | 370 if self.Peak == 'cast': |
230 | 371 loc = self.Consume('cast').loc |
221 | 372 self.Consume('<') |
354 | 373 t = self.parse_type_spec() |
221 | 374 self.Consume('>') |
375 self.Consume('(') | |
232 | 376 ce = self.Expression() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
377 self.Consume(')') |
296 | 378 return TypeCast(t, ce, loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
379 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
380 return self.UnaryExpression() |
230 | 381 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
382 def UnaryExpression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
383 if self.Peak in ['&', '*']: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
384 op = self.Consume(self.Peak) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
385 ce = self.CastExpression() |
225 | 386 if op.val == '*': |
296 | 387 return Deref(ce, op.loc) |
225 | 388 else: |
296 | 389 return Unop(op.typ, ce, op.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
390 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
391 return self.PostFixExpression() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
392 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
393 def PostFixExpression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
394 pfe = self.PrimaryExpression() |
315 | 395 while self.Peak in ['[', '.', '->', '(', '++']: |
308 | 396 if self.hasConsumed('['): |
354 | 397 i = self.Expression() |
398 self.Consume(']') | |
399 pfe = Index(pfe, i, i.loc) | |
308 | 400 elif self.hasConsumed('->'): |
401 field = self.Consume('ID') | |
402 pfe = Deref(pfe, pfe.loc) | |
403 pfe = Member(pfe, field.val, field.loc) | |
404 elif self.hasConsumed('.'): | |
405 field = self.Consume('ID') | |
406 pfe = Member(pfe, field.val, field.loc) | |
315 | 407 elif self.Peak == '++': |
408 loc = self.Consume('++').loc | |
409 pfe = Unop('++', pfe, loc) | |
308 | 410 elif self.hasConsumed('('): |
411 # Function call | |
412 args = [] | |
413 if not self.hasConsumed(')'): | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
414 args.append(self.Expression()) |
308 | 415 while self.hasConsumed(','): |
416 args.append(self.Expression()) | |
417 self.Consume(')') | |
418 pfe = FunctionCall(pfe, args, pfe.loc) | |
419 else: | |
420 raise Exception() | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
421 return pfe |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
422 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
423 def PrimaryExpression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
424 if self.hasConsumed('('): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
425 e = self.Expression() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
426 self.Consume(')') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
427 return e |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
428 elif self.Peak == 'NUMBER': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
429 val = self.Consume('NUMBER') |
296 | 430 return Literal(val.val, val.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
431 elif self.Peak == 'REAL': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
432 val = self.Consume('REAL') |
296 | 433 return Literal(val.val, val.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
434 elif self.Peak == 'true': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
435 val = self.Consume('true') |
296 | 436 return Literal(True, val.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
437 elif self.Peak == 'false': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
438 val = self.Consume('false') |
296 | 439 return Literal(False, val.loc) |
311 | 440 elif self.Peak == 'STRING': |
441 val = self.Consume('STRING') | |
442 return Literal(val.val, val.loc) | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
443 elif self.Peak == 'ID': |
306 | 444 return self.parseDesignator() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
445 self.Error('Expected NUM, ID or (expr), got {0}'.format(self.Peak)) |
354 | 446 |