Mercurial > lcfOS
comparison python/ppci/c3/parser.py @ 301:6753763d3bec
merge codegen into ppci package
author | Windel Bouwman |
---|---|
date | Thu, 05 Dec 2013 17:02:38 +0100 |
parents | 158068af716c |
children | b145f8e6050b |
comparison
equal
deleted
inserted
replaced
300:158068af716c | 301:6753763d3bec |
---|---|
1 import logging | 1 import logging |
2 from ppci import CompilerError | |
2 from .lexer import Lexer | 3 from .lexer import Lexer |
3 from .astnodes import FieldRef, Literal, TypeCast, Unop, Binop | 4 from .astnodes import FieldRef, Literal, TypeCast, Unop, Binop |
4 from .astnodes import Assignment, ExpressionStatement, CompoundStatement | 5 from .astnodes import Assignment, ExpressionStatement, CompoundStatement |
5 from .astnodes import ReturnStatement, WhileStatement, IfStatement | 6 from .astnodes import ReturnStatement, WhileStatement, IfStatement |
6 from .astnodes import FunctionType, Function, FormalParameter | 7 from .astnodes import FunctionType, Function, FormalParameter |
7 from .astnodes import StructureType, DefinedType, PointerType | 8 from .astnodes import StructureType, DefinedType, PointerType |
8 from .astnodes import Constant, Variable | 9 from .astnodes import Constant, Variable |
9 from .astnodes import StructField, Deref | 10 from .astnodes import StructField, Deref |
10 from .astnodes import Package, ImportDesignator | 11 from .astnodes import Package, ImportDesignator |
11 from .astnodes import Designator, VariableUse, FunctionCall | 12 from .astnodes import Designator, VariableUse, FunctionCall |
12 from ppci import CompilerError | |
13 | 13 |
14 | 14 |
15 class Parser: | 15 class Parser: |
16 """ Parses sourcecode into an abstract syntax tree (AST) """ | 16 """ Parses sourcecode into an abstract syntax tree (AST) """ |
17 def __init__(self, diag): | 17 def __init__(self, diag): |
94 self.parseImport() | 94 self.parseImport() |
95 else: | 95 else: |
96 self.Error('Expected function, var, const or type') | 96 self.Error('Expected function, var, const or type') |
97 | 97 |
98 def parseDesignator(self): | 98 def parseDesignator(self): |
99 """ A designator designates an object """ | 99 """ A designator designates an object with a name. """ |
100 name = self.Consume('ID') | 100 name = self.Consume('ID') |
101 if self.hasConsumed(':'): | 101 if self.hasConsumed(':'): |
102 name2 = self.Consume('ID') | 102 name2 = self.Consume('ID') |
103 return ImportDesignator(name.val, name2.val, name.loc) | 103 return ImportDesignator(name.val, name2.val, name.loc) |
104 else: | 104 else: |
105 return Designator(name.val, name.loc) | 105 return Designator(name.val, name.loc) |
106 | 106 |
107 # Type system | 107 # Type system |
108 def parseTypeSpec(self): | 108 def parseTypeSpec(self): |
109 # For now, do simple type spec, just parse an ID: | 109 # For now, do simple type spec, just parse an ID: |
110 #return self.parseDesignator() | |
111 if self.Peak == 'struct': | 110 if self.Peak == 'struct': |
112 self.Consume('struct') | 111 self.Consume('struct') |
113 self.Consume('{') | 112 self.Consume('{') |
114 mems = [] | 113 mems = [] |
115 while self.Peak != '}': | 114 while self.Peak != '}': |
366 else: | 365 else: |
367 return self.PostFixExpression() | 366 return self.PostFixExpression() |
368 | 367 |
369 def PostFixExpression(self): | 368 def PostFixExpression(self): |
370 pfe = self.PrimaryExpression() | 369 pfe = self.PrimaryExpression() |
371 while self.Peak in ['[', '(', '.', '->']: | 370 if self.hasConsumed('('): |
372 if self.hasConsumed('['): | 371 # Function call |
373 pass | 372 args = [] |
374 elif self.hasConsumed('('): | 373 if not self.hasConsumed(')'): |
375 # Function call | 374 args.append(self.Expression()) |
376 args = [] | 375 while self.hasConsumed(','): |
377 if not self.hasConsumed(')'): | |
378 args.append(self.Expression()) | 376 args.append(self.Expression()) |
379 while self.hasConsumed(','): | 377 self.Consume(')') |
380 args.append(self.Expression()) | 378 pfe = FunctionCall(pfe, args, pfe.loc) |
381 self.Consume(')') | 379 else: |
382 pfe = FunctionCall(pfe, args, pfe.loc) | 380 while self.Peak in ['[', '.', '->']: |
383 elif self.hasConsumed('->'): | 381 if self.hasConsumed('['): |
384 field = self.Consume('ID') | 382 raise NotImplementedError('Array not yet implemented') |
385 pfe = Deref(pfe, pfe.loc) | 383 elif self.hasConsumed('->'): |
386 pfe = FieldRef(pfe, field.val, field.loc) | 384 field = self.Consume('ID') |
387 elif self.hasConsumed('.'): | 385 pfe = Deref(pfe, pfe.loc) |
388 field = self.Consume('ID') | 386 pfe = FieldRef(pfe, field.val, field.loc) |
389 pfe = FieldRef(pfe, field.val, field.loc) | 387 elif self.hasConsumed('.'): |
390 else: | 388 field = self.Consume('ID') |
391 raise Exception() | 389 pfe = FieldRef(pfe, field.val, field.loc) |
390 else: | |
391 raise Exception() | |
392 return pfe | 392 return pfe |
393 | 393 |
394 def PrimaryExpression(self): | 394 def PrimaryExpression(self): |
395 if self.hasConsumed('('): | 395 if self.hasConsumed('('): |
396 e = self.Expression() | 396 e = self.Expression() |