comparison 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
comparison
equal deleted inserted replaced
314:38f5f298ce0e 315:084cccaa5deb
1 import logging 1 import logging
2 from ppci import CompilerError 2 from ppci import CompilerError
3 from .astnodes import Member, Literal, TypeCast, Unop, Binop 3 from .astnodes import Member, Literal, TypeCast, Unop, Binop
4 from .astnodes import Assignment, ExpressionStatement, Compound 4 from .astnodes import Assignment, ExpressionStatement, Compound
5 from .astnodes import Return, While, If, Empty 5 from .astnodes import Return, While, If, Empty, For
6 from .astnodes import FunctionType, Function, FormalParameter 6 from .astnodes import FunctionType, Function, FormalParameter
7 from .astnodes import StructureType, DefinedType, PointerType 7 from .astnodes import StructureType, DefinedType, PointerType
8 from .astnodes import Constant, Variable 8 from .astnodes import Constant, Variable
9 from .astnodes import StructField, Deref 9 from .astnodes import StructField, Deref
10 from .astnodes import Package 10 from .astnodes import Package
202 condition = self.Expression() 202 condition = self.Expression()
203 self.Consume(')') 203 self.Consume(')')
204 statements = self.Statement() 204 statements = self.Statement()
205 return While(condition, statements, loc) 205 return While(condition, statements, loc)
206 206
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
207 def parseReturn(self): 219 def parseReturn(self):
208 loc = self.Consume('return').loc 220 loc = self.Consume('return').loc
209 if self.Peak == ';': 221 if self.Peak == ';':
210 expr = Literal(0, loc) 222 expr = Literal(0, loc)
211 else: 223 else:
224 # Determine statement type based on the pending token: 236 # Determine statement type based on the pending token:
225 if self.Peak == 'if': 237 if self.Peak == 'if':
226 return self.parseIf() 238 return self.parseIf()
227 elif self.Peak == 'while': 239 elif self.Peak == 'while':
228 return self.parseWhile() 240 return self.parseWhile()
241 elif self.Peak == 'for':
242 return self.parseFor()
229 elif self.Peak == '{': 243 elif self.Peak == '{':
230 return self.parseCompound() 244 return self.parseCompound()
231 elif self.hasConsumed(';'): 245 elif self.hasConsumed(';'):
232 return Empty() 246 return Empty()
233 elif self.Peak == 'var': 247 elif self.Peak == 'var':
345 else: 359 else:
346 return self.PostFixExpression() 360 return self.PostFixExpression()
347 361
348 def PostFixExpression(self): 362 def PostFixExpression(self):
349 pfe = self.PrimaryExpression() 363 pfe = self.PrimaryExpression()
350 while self.Peak in ['[', '.', '->', '(']: 364 while self.Peak in ['[', '.', '->', '(', '++']:
351 if self.hasConsumed('['): 365 if self.hasConsumed('['):
352 raise NotImplementedError('Array not yet implemented') 366 raise NotImplementedError('Array not yet implemented')
353 elif self.hasConsumed('->'): 367 elif self.hasConsumed('->'):
354 field = self.Consume('ID') 368 field = self.Consume('ID')
355 pfe = Deref(pfe, pfe.loc) 369 pfe = Deref(pfe, pfe.loc)
356 pfe = Member(pfe, field.val, field.loc) 370 pfe = Member(pfe, field.val, field.loc)
357 elif self.hasConsumed('.'): 371 elif self.hasConsumed('.'):
358 field = self.Consume('ID') 372 field = self.Consume('ID')
359 pfe = Member(pfe, field.val, field.loc) 373 pfe = Member(pfe, field.val, field.loc)
374 elif self.Peak == '++':
375 loc = self.Consume('++').loc
376 pfe = Unop('++', pfe, loc)
360 elif self.hasConsumed('('): 377 elif self.hasConsumed('('):
361 # Function call 378 # Function call
362 args = [] 379 args = []
363 if not self.hasConsumed(')'): 380 if not self.hasConsumed(')'):
364 args.append(self.Expression()) 381 args.append(self.Expression())