Mercurial > lcfOS
annotate python/ppci/c3/parser.py @ 315:084cccaa5deb
Added console and screen
author | Windel Bouwman |
---|---|
date | Sat, 21 Dec 2013 10:03:01 +0100 |
parents | 04cf4d26a3bc |
children | 6f4753202b9a |
rev | line source |
---|---|
254 | 1 import logging |
301 | 2 from ppci 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 |
7 from .astnodes import StructureType, DefinedType, PointerType | |
8 from .astnodes import Constant, Variable | |
9 from .astnodes import StructField, Deref | |
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): |
254 | 22 self.logger.info('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): |
63 self.currentPart.declarations.append(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': |
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
232
diff
changeset
|
83 self.parseFunctionDef() |
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 |
108 def parseTypeSpec(self): | |
109 # For now, do simple type spec, just parse an ID: | |
110 if self.Peak == 'struct': | |
111 self.Consume('struct') | |
112 self.Consume('{') | |
113 mems = [] | |
114 while self.Peak != '}': | |
115 mem_t = self.parseTypeSpec() | |
306 | 116 for i in self.parseIdSequence(): |
117 mems.append(StructField(i.val, mem_t)) | |
213 | 118 self.Consume(';') |
119 self.Consume('}') | |
296 | 120 theT = StructureType(mems) |
306 | 121 elif self.Peak == 'enum': |
122 # TODO) | |
123 raise NotImplementedError() | |
213 | 124 else: |
306 | 125 theT = self.PostFixExpression() |
213 | 126 # Check for pointer suffix: |
127 while self.hasConsumed('*'): | |
296 | 128 theT = PointerType(theT) |
213 | 129 return theT |
130 | |
131 def parseTypeDef(self): | |
132 self.Consume('type') | |
133 newtype = self.parseTypeSpec() | |
134 typename = self.Consume('ID') | |
135 self.Consume(';') | |
296 | 136 df = DefinedType(typename.val, newtype, typename.loc) |
225 | 137 self.addDeclaration(df) |
213 | 138 |
139 # Variable declarations: | |
140 def parseVarDef(self): | |
272 | 141 self.Consume('var') |
142 t = self.parseTypeSpec() | |
306 | 143 for name in self.parseIdSequence(): |
296 | 144 v = Variable(name.val, t) |
272 | 145 v.loc = name.loc |
146 self.addDeclaration(v) | |
147 self.Consume(';') | |
307 | 148 return Empty() |
148 | 149 |
213 | 150 def parseConstDef(self): |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
151 self.Consume('const') |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
152 t = self.parseTypeSpec() |
306 | 153 while True: |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
154 name = self.Consume('ID') |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
155 self.Consume('=') |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
156 val = self.Expression() |
296 | 157 c = Constant(name.val, t, val) |
313 | 158 self.addDeclaration(c) |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
159 c.loc = name.loc |
306 | 160 if not self.hasConsumed(','): |
161 break | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
162 self.Consume(';') |
288 | 163 |
213 | 164 def parseFunctionDef(self): |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
165 loc = self.Consume('function').loc |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
166 returntype = self.parseTypeSpec() |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
167 fname = self.Consume('ID').val |
296 | 168 f = Function(fname, loc) |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
169 self.addDeclaration(f) |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
170 savePart = self.currentPart |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
171 self.currentPart = f |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
172 self.Consume('(') |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
173 parameters = [] |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
174 if not self.hasConsumed(')'): |
306 | 175 while True: |
272 | 176 typ = self.parseTypeSpec() |
177 name = self.Consume('ID') | |
296 | 178 param = FormalParameter(name.val, typ) |
272 | 179 param.loc = name.loc |
180 self.addDeclaration(param) | |
181 parameters.append(param) | |
306 | 182 if not self.hasConsumed(','): |
183 break | |
272 | 184 self.Consume(')') |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
185 paramtypes = [p.typ for p in parameters] |
296 | 186 f.typ = FunctionType(paramtypes, returntype) |
307 | 187 f.body = self.parseCompound() |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
188 self.currentPart = savePart |
148 | 189 |
307 | 190 def parseIf(self): |
272 | 191 loc = self.Consume('if').loc |
192 self.Consume('(') | |
193 condition = self.Expression() | |
194 self.Consume(')') | |
306 | 195 yes = self.Statement() |
307 | 196 no = self.Statement() if self.hasConsumed('else') else Empty() |
197 return If(condition, yes, no, loc) | |
148 | 198 |
307 | 199 def parseWhile(self): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
200 loc = self.Consume('while').loc |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
201 self.Consume('(') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
202 condition = self.Expression() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
203 self.Consume(')') |
306 | 204 statements = self.Statement() |
307 | 205 return While(condition, statements, loc) |
149 | 206 |
315 | 207 def parseFor(self): |
208 loc = self.Consume('for').loc | |
209 self.Consume('(') | |
210 init = self.Statement() | |
211 self.Consume(';') | |
212 condition = self.Expression() | |
213 self.Consume(';') | |
214 final = self.Statement() | |
215 self.Consume(')') | |
216 statements = self.Statement() | |
217 return For(init, condition, final, statements, loc) | |
218 | |
307 | 219 def parseReturn(self): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
220 loc = self.Consume('return').loc |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
221 if self.Peak == ';': |
296 | 222 expr = Literal(0, loc) |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
223 else: |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
272
diff
changeset
|
224 expr = self.Expression() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
225 self.Consume(';') |
307 | 226 return Return(expr, loc) |
148 | 227 |
307 | 228 def parseCompound(self): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
229 self.Consume('{') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
230 statements = [] |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
231 while not self.hasConsumed('}'): |
306 | 232 statements.append(self.Statement()) |
307 | 233 return Compound(statements) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
234 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
235 def Statement(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
236 # 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
|
237 if self.Peak == 'if': |
307 | 238 return self.parseIf() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
239 elif self.Peak == 'while': |
307 | 240 return self.parseWhile() |
315 | 241 elif self.Peak == 'for': |
242 return self.parseFor() | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
243 elif self.Peak == '{': |
307 | 244 return self.parseCompound() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
245 elif self.hasConsumed(';'): |
307 | 246 return Empty() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
247 elif self.Peak == 'var': |
306 | 248 return self.parseVarDef() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
249 elif self.Peak == 'return': |
307 | 250 return self.parseReturn() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
251 else: |
306 | 252 x = self.UnaryExpression() |
253 if self.Peak == '=': | |
254 # We enter assignment mode here. | |
255 loc = self.Consume('=').loc | |
256 rhs = self.Expression() | |
257 return Assignment(x, rhs, loc) | |
258 else: | |
259 return ExpressionStatement(x, x.loc) | |
148 | 260 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
261 # Expression section: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
262 # We not implement these C constructs: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
263 # a(2), f = 2 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
264 # and this: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
265 # a = 2 < x : 4 ? 1; |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
266 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
267 def Expression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
268 exp = self.LogicalAndExpression() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
269 while self.Peak == 'or': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
270 loc = self.Consume('or').loc |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
271 e2 = self.LogicalAndExpression() |
296 | 272 exp = Binop(exp, 'or', e2, loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
273 return exp |
148 | 274 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
275 def LogicalAndExpression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
276 o = self.EqualityExpression() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
277 while self.Peak == 'and': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
278 loc = self.Consume('and').loc |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
279 o2 = self.EqualityExpression() |
296 | 280 o = Binop(o, 'and', o2, loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
281 return o |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
282 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
283 def EqualityExpression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
284 ee = self.SimpleExpression() |
300 | 285 while self.Peak in ['<', '==', '>', '>=', '<=', '!=']: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
286 op = self.Consume(self.Peak) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
287 ee2 = self.SimpleExpression() |
296 | 288 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
|
289 return ee |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
290 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
291 def SimpleExpression(self): |
232 | 292 """ Shift operations before + and - ? """ |
293 e = self.AddExpression() | |
294 while self.Peak in ['>>', '<<']: | |
295 op = self.Consume(self.Peak) | |
296 e2 = self.AddExpression() | |
296 | 297 e = Binop(e, op.typ, e2, op.loc) |
232 | 298 return e |
299 | |
300 def AddExpression(self): | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
301 e = self.Term() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
302 while self.Peak in ['+', '-']: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
303 op = self.Consume(self.Peak) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
304 e2 = self.Term() |
296 | 305 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
|
306 return e |
213 | 307 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
308 def Term(self): |
221 | 309 t = self.BitwiseOr() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
310 while self.Peak in ['*', '/']: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
311 op = self.Consume(self.Peak) |
221 | 312 t2 = self.BitwiseOr() |
296 | 313 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
|
314 return t |
221 | 315 |
316 def BitwiseOr(self): | |
317 a = self.BitwiseAnd() | |
306 | 318 while self.Peak == '|': |
221 | 319 op = self.Consume(self.Peak) |
320 b = self.BitwiseAnd() | |
296 | 321 a = Binop(a, op.typ, b, op.loc) |
221 | 322 return a |
323 | |
324 def BitwiseAnd(self): | |
325 a = self.CastExpression() | |
306 | 326 while self.Peak == '&': |
221 | 327 op = self.Consume(self.Peak) |
328 b = self.CastExpression() | |
296 | 329 a = Binop(a, op.typ, b, op.loc) |
221 | 330 return a |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
331 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
332 # Domain of unary expressions: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
333 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
334 def CastExpression(self): |
228 | 335 """ |
336 the C-style type cast conflicts with '(' expr ')' | |
337 so introduce extra keyword 'cast' | |
338 """ | |
221 | 339 if self.Peak == 'cast': |
230 | 340 loc = self.Consume('cast').loc |
221 | 341 self.Consume('<') |
342 t = self.parseTypeSpec() | |
343 self.Consume('>') | |
344 self.Consume('(') | |
232 | 345 ce = self.Expression() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
346 self.Consume(')') |
296 | 347 return TypeCast(t, ce, loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
348 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
349 return self.UnaryExpression() |
230 | 350 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
351 def UnaryExpression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
352 if self.Peak in ['&', '*']: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
353 op = self.Consume(self.Peak) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
354 ce = self.CastExpression() |
225 | 355 if op.val == '*': |
296 | 356 return Deref(ce, op.loc) |
225 | 357 else: |
296 | 358 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
|
359 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
360 return self.PostFixExpression() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
361 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
362 def PostFixExpression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
363 pfe = self.PrimaryExpression() |
315 | 364 while self.Peak in ['[', '.', '->', '(', '++']: |
308 | 365 if self.hasConsumed('['): |
366 raise NotImplementedError('Array not yet implemented') | |
367 elif self.hasConsumed('->'): | |
368 field = self.Consume('ID') | |
369 pfe = Deref(pfe, pfe.loc) | |
370 pfe = Member(pfe, field.val, field.loc) | |
371 elif self.hasConsumed('.'): | |
372 field = self.Consume('ID') | |
373 pfe = Member(pfe, field.val, field.loc) | |
315 | 374 elif self.Peak == '++': |
375 loc = self.Consume('++').loc | |
376 pfe = Unop('++', pfe, loc) | |
308 | 377 elif self.hasConsumed('('): |
378 # Function call | |
379 args = [] | |
380 if not self.hasConsumed(')'): | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
381 args.append(self.Expression()) |
308 | 382 while self.hasConsumed(','): |
383 args.append(self.Expression()) | |
384 self.Consume(')') | |
385 pfe = FunctionCall(pfe, args, pfe.loc) | |
386 else: | |
387 raise Exception() | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
388 return pfe |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
389 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
390 def PrimaryExpression(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
391 if self.hasConsumed('('): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
392 e = self.Expression() |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
393 self.Consume(')') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
394 return e |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
395 elif self.Peak == 'NUMBER': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
396 val = self.Consume('NUMBER') |
296 | 397 return Literal(val.val, val.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
398 elif self.Peak == 'REAL': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
399 val = self.Consume('REAL') |
296 | 400 return Literal(val.val, val.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
401 elif self.Peak == 'true': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
402 val = self.Consume('true') |
296 | 403 return Literal(True, val.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
404 elif self.Peak == 'false': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
405 val = self.Consume('false') |
296 | 406 return Literal(False, val.loc) |
311 | 407 elif self.Peak == 'STRING': |
408 val = self.Consume('STRING') | |
409 return Literal(val.val, val.loc) | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
410 elif self.Peak == 'ID': |
306 | 411 return self.parseDesignator() |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
219
diff
changeset
|
412 self.Error('Expected NUM, ID or (expr), got {0}'.format(self.Peak)) |