comparison python/ppci/c3/parser.py @ 394:988f3fb861e4

c3 code generator rewrite
author Windel Bouwman
date Thu, 22 May 2014 08:14:12 +0200
parents 6ae782a085e0
children fb3c1f029b30
comparison
equal deleted inserted replaced
393:6ae782a085e0 394:988f3fb861e4
21 def parseSource(self, tokens): 21 def parseSource(self, tokens):
22 self.logger.debug('Parsing source') 22 self.logger.debug('Parsing source')
23 self.tokens = tokens 23 self.tokens = tokens
24 self.token = self.tokens.__next__() 24 self.token = self.tokens.__next__()
25 try: 25 try:
26 self.parsePackage() 26 self.parse_package()
27 self.mod.ok = True # Valid until proven wrong :) 27 self.mod.ok = True # Valid until proven wrong :)
28 return self.mod 28 return self.mod
29 except CompilerError as e: 29 except CompilerError as e:
30 self.diag.addDiag(e) 30 self.diag.addDiag(e)
31 31
66 self.Consume('import') 66 self.Consume('import')
67 name = self.Consume('ID').val 67 name = self.Consume('ID').val
68 self.mod.imports.append(name) 68 self.mod.imports.append(name)
69 self.Consume(';') 69 self.Consume(';')
70 70
71 def parsePackage(self): 71 def parse_package(self):
72 """ Parse a package definition """
72 self.Consume('module') 73 self.Consume('module')
73 name = self.Consume('ID') 74 name = self.Consume('ID')
74 self.Consume(';') 75 self.Consume(';')
75 self.mod = Package(name.val, name.loc) 76 self.mod = Package(name.val, name.loc)
76 self.currentPart = self.mod 77 self.currentPart = self.mod
77 while self.Peak != 'END': 78 while self.Peak != 'END':
78 self.parseTopLevel() 79 self.parse_top_level()
79 self.Consume('END') 80 self.Consume('END')
80 81
81 def parseTopLevel(self): 82 def parse_top_level(self):
83 """ Parse toplevel declaration """
82 if self.Peak == 'function': 84 if self.Peak == 'function':
83 self.parse_function_def() 85 self.parse_function_def()
84 elif self.Peak == 'var': 86 elif self.Peak == 'var':
85 self.parseVarDef() 87 self.parse_variable_def()
86 # TODO handle variable initialization 88 # TODO handle variable initialization
87 elif self.Peak == 'const': 89 elif self.Peak == 'const':
88 self.parseConstDef() 90 self.parseConstDef()
89 elif self.Peak == 'type': 91 elif self.Peak == 'type':
90 self.parseTypeDef() 92 self.parse_type_def()
91 elif self.Peak == 'import': 93 elif self.Peak == 'import':
92 self.parseImport() 94 self.parseImport()
93 else: 95 else:
94 self.Error('Expected function, var, const or type') 96 self.Error('Expected function, var, const or type')
95 97
153 theT = ArrayType(theT, size) 155 theT = ArrayType(theT, size)
154 else: 156 else:
155 raise Exception() 157 raise Exception()
156 return theT 158 return theT
157 159
158 def parseTypeDef(self): 160 def parse_type_def(self):
159 self.Consume('type') 161 self.Consume('type')
160 newtype = self.parse_type_spec() 162 newtype = self.parse_type_spec()
161 typename = self.Consume('ID') 163 typename = self.Consume('ID')
162 self.Consume(';') 164 self.Consume(';')
163 df = DefinedType(typename.val, newtype, typename.loc) 165 df = DefinedType(typename.val, newtype, typename.loc)
164 self.addDeclaration(df) 166 self.addDeclaration(df)
165 167
166 # Variable declarations: 168 # Variable declarations:
167 def parseVarDef(self): 169 def parse_variable_def(self):
170 """ Parse variable declaration """
168 self.Consume('var') 171 self.Consume('var')
169 t = self.parse_type_spec() 172 t = self.parse_type_spec()
170 for name in self.parseIdSequence(): 173 for name in self.parseIdSequence():
171 v = Variable(name.val, t) 174 v = Variable(name.val, t)
172 v.loc = name.loc 175 v.loc = name.loc
173 self.addDeclaration(v) 176 self.addDeclaration(v)
174 self.Consume(';') 177 self.Consume(';')
175 return Empty()
176 178
177 def parseConstDef(self): 179 def parseConstDef(self):
178 self.Consume('const') 180 self.Consume('const')
179 t = self.parse_type_spec() 181 t = self.parse_type_spec()
180 while True: 182 while True:
232 self.Consume('(') 234 self.Consume('(')
233 condition = self.Expression() 235 condition = self.Expression()
234 self.Consume(')') 236 self.Consume(')')
235 return Switch(condition, loc) 237 return Switch(condition, loc)
236 238
237 def parseWhile(self): 239 def parse_while(self):
238 loc = self.Consume('while').loc 240 loc = self.Consume('while').loc
239 self.Consume('(') 241 self.Consume('(')
240 condition = self.Expression() 242 condition = self.Expression()
241 self.Consume(')') 243 self.Consume(')')
242 statements = self.Statement() 244 statements = self.Statement()
273 def Statement(self): 275 def Statement(self):
274 # Determine statement type based on the pending token: 276 # Determine statement type based on the pending token:
275 if self.Peak == 'if': 277 if self.Peak == 'if':
276 return self.parse_if() 278 return self.parse_if()
277 elif self.Peak == 'while': 279 elif self.Peak == 'while':
278 return self.parseWhile() 280 return self.parse_while()
279 elif self.Peak == 'for': 281 elif self.Peak == 'for':
280 return self.parse_for() 282 return self.parse_for()
281 elif self.Peak == 'switch': 283 elif self.Peak == 'switch':
282 return self.parse_switch() 284 return self.parse_switch()
283 elif self.Peak == '{': 285 elif self.Peak == '{':
284 return self.parseCompound() 286 return self.parseCompound()
285 elif self.hasConsumed(';'): 287 elif self.hasConsumed(';'):
286 return Empty() 288 return Empty()
287 elif self.Peak == 'var': 289 elif self.Peak == 'var':
288 return self.parseVarDef() 290 self.parse_variable_def()
291 return Empty()
289 elif self.Peak == 'return': 292 elif self.Peak == 'return':
290 return self.parseReturn() 293 return self.parseReturn()
291 else: 294 else:
292 x = self.UnaryExpression() 295 x = self.UnaryExpression()
293 if self.Peak == '=': 296 if self.Peak == '=':
389 return self.sizeof_expression() 392 return self.sizeof_expression()
390 else: 393 else:
391 return self.UnaryExpression() 394 return self.UnaryExpression()
392 395
393 def sizeof_expression(self): 396 def sizeof_expression(self):
397 """ Compiler internal function to determine size of a type """
394 loc = self.Consume('sizeof').loc 398 loc = self.Consume('sizeof').loc
395 self.Consume('(') 399 self.Consume('(')
396 typ = self.parse_type_spec() 400 typ = self.parse_type_spec()
397 self.Consume(')') 401 self.Consume(')')
398 return Sizeof(typ, loc) 402 return Sizeof(typ, loc)