annotate python/c3/parser.py @ 296:9417caea2eb3

Directorized some backend files
author Windel Bouwman
date Sun, 01 Dec 2013 13:36:58 +0100
parents 6aa721e7b10b
children
rev   line source
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
1 import logging
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
2 from .lexer import Lexer
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
3 from .astnodes import FieldRef, Literal, TypeCast, Unop, Binop
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
4 from .astnodes import Assignment, ExpressionStatement, CompoundStatement
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
5 from .astnodes import ReturnStatement, WhileStatement, IfStatement
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
6 from .astnodes import FunctionType, Function, FormalParameter
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
7 from .astnodes import StructureType, DefinedType, PointerType
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
8 from .astnodes import Constant, Variable
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
9 from .astnodes import StructField, Deref
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
10 from .astnodes import Package, ImportDesignator
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
11 from .astnodes import Designator, VariableUse, FunctionCall
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 150
diff changeset
12 from ppci import CompilerError
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
14
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 class Parser:
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
16 """ Parses sourcecode into an abstract syntax tree (AST) """
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
17 def __init__(self, diag):
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
18 self.logger = logging.getLogger('c3')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
19 self.diag = diag
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
20 self.lexer = Lexer(diag)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
21
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
22 def parseSource(self, source):
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
23 self.logger.info('Parsing source')
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
24 self.initLex(source)
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
25 try:
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
26 self.parsePackage()
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
27 return self.mod
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
28 except CompilerError as e:
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
29 self.diag.addDiag(e)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
30
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
31 def Error(self, msg):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
32 raise CompilerError(msg, self.token.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
33
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
34 # Lexer helpers:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
35 def Consume(self, typ):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
36 if self.Peak == typ:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
37 return self.NextToken()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
38 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
39 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
40
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
41 @property
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
42 def Peak(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
43 return self.token.typ
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
44
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
45 @property
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
46 def CurLoc(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
47 return self.token.loc
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
48
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
49 def hasConsumed(self, typ):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
50 if self.Peak == typ:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
51 self.Consume(typ)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
52 return True
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
53 return False
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
54
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
55 def NextToken(self):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
56 t = self.token
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
57 if t.typ != 'END':
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
58 self.token = self.tokens.__next__()
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
59 return t
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
60
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
61 def initLex(self, source):
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
62 self.tokens = self.lexer.tokenize(source)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
63 self.token = self.tokens.__next__()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
64
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
65 def addDeclaration(self, decl):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
66 self.currentPart.declarations.append(decl)
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
67
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
68 def parseImport(self):
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
69 self.Consume('import')
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
70 name = self.Consume('ID').val
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
71 self.mod.imports.append(name)
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
72 self.Consume(';')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
73
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
74 def parsePackage(self):
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
75 self.Consume('module')
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
76 name = self.Consume('ID')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
77 self.Consume(';')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
78 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
79 self.currentPart = self.mod
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
80 while self.Peak != 'END':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
81 self.parseTopLevel()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
82 self.Consume('END')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
83
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
84 def parseTopLevel(self):
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
85 if self.Peak == 'function':
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
86 self.parseFunctionDef()
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
87 elif self.Peak == 'var':
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
88 self.parseVarDef()
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
89 elif self.Peak == 'const':
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
90 self.parseConstDef()
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
91 elif self.Peak == 'type':
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
92 self.parseTypeDef()
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
93 elif self.Peak == 'import':
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
94 self.parseImport()
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
95 else:
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
96 self.Error('Expected function, var, const or type')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
97
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
98 def parseDesignator(self):
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
99 """ A designator designates an object """
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
100 name = self.Consume('ID')
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
101 if self.hasConsumed(':'):
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
102 name2 = self.Consume('ID')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
103 return ImportDesignator(name.val, name2.val, name.loc)
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
104 else:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
105 return Designator(name.val, name.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
106
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
107 # Type system
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
108 def parseTypeSpec(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
109 # For now, do simple type spec, just parse an ID:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
110 #return self.parseDesignator()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
111 if self.Peak == 'struct':
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
112 self.Consume('struct')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
113 self.Consume('{')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
114 mems = []
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
115 while self.Peak != '}':
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
116 mem_t = self.parseTypeSpec()
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
117 mem_n = self.Consume('ID').val
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
118 mems.append(StructField(mem_n, mem_t))
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
119 while self.hasConsumed(','):
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
120 mem_n = self.Consume('ID').val
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
121 mems.append(StructField(mem_n, mem_t))
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
122 self.Consume(';')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
123 self.Consume('}')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
124 theT = StructureType(mems)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
125 else:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
126 theT = self.parseDesignator()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
127 # Check for pointer suffix:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
128 while self.hasConsumed('*'):
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
129 theT = PointerType(theT)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
130 return theT
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
131
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
132 def parseTypeDef(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
133 self.Consume('type')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
134 newtype = self.parseTypeSpec()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
135 typename = self.Consume('ID')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
136 self.Consume(';')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
137 df = DefinedType(typename.val, newtype, typename.loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
138 self.addDeclaration(df)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
139
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
140 # Variable declarations:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
141 def parseVarDef(self):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
142 self.Consume('var')
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
143 t = self.parseTypeSpec()
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
144 def parseVar():
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
145 name = self.Consume('ID')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
146 v = Variable(name.val, t)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
147 v.loc = name.loc
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
148 if self.hasConsumed('='):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
149 v.ival = self.Expression()
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
150 self.addDeclaration(v)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
151 parseVar()
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
152 while self.hasConsumed(','):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
153 parseVar()
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
154 self.Consume(';')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
155
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
156 def parseConstDef(self):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
157 self.Consume('const')
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
158 t = self.parseTypeSpec()
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
159 def parseConst():
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
160 name = self.Consume('ID')
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
161 self.Consume('=')
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
162 val = self.Expression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
163 c = Constant(name.val, t, val)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
164 c.loc = name.loc
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
165 parseConst()
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
166 while self.hasConsumed(','):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
167 parseConst()
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
168 self.Consume(';')
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
169
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
170 # Procedures
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
171 def parseFunctionDef(self):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
172 loc = self.Consume('function').loc
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
173 returntype = self.parseTypeSpec()
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
174 fname = self.Consume('ID').val
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
175 f = Function(fname, loc)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
176 self.addDeclaration(f)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
177 savePart = self.currentPart
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
178 self.currentPart = f
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
179 self.Consume('(')
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
180 parameters = []
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
181 if not self.hasConsumed(')'):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
182 def parseParameter():
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
183 typ = self.parseTypeSpec()
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
184 name = self.Consume('ID')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
185 param = FormalParameter(name.val, typ)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
186 param.loc = name.loc
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
187 self.addDeclaration(param)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
188 parameters.append(param)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
189 parseParameter()
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
190 while self.hasConsumed(','):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
191 parseParameter()
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
192 self.Consume(')')
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
193 paramtypes = [p.typ for p in parameters]
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
194 f.typ = FunctionType(paramtypes, returntype)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
195 f.body = self.parseCompoundStatement()
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
196 self.currentPart = savePart
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
197
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
198 # Statements:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
199
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
200 def parseIfStatement(self):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
201 loc = self.Consume('if').loc
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
202 self.Consume('(')
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
203 condition = self.Expression()
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
204 self.Consume(')')
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
205 yes = self.parseCompoundStatement()
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
206 if self.hasConsumed('else'):
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
207 no = self.parseCompoundStatement()
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
208 else:
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
209 no = None
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
210 return IfStatement(condition, yes, no, loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
211
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
212 def parseWhileStatement(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
213 loc = self.Consume('while').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
214 self.Consume('(')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
215 condition = self.Expression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
216 self.Consume(')')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
217 statements = self.parseCompoundStatement()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
218 return WhileStatement(condition, statements, loc)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
219
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
220 def parseReturnStatement(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
221 loc = self.Consume('return').loc
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
222 if self.Peak == ';':
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
223 expr = Literal(0, loc)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
224 else:
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
225 expr = self.Expression()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
226 self.Consume(';')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
227 return ReturnStatement(expr, loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
228
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
229 def parseCompoundStatement(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
230 self.Consume('{')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
231 statements = []
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
232 while not self.hasConsumed('}'):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
233 s = self.Statement()
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
234 if s is None:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
235 continue
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
236 statements.append(s)
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
237 return CompoundStatement(statements)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
238
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
239 def Statement(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
240 # 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
241 if self.Peak == 'if':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
242 return self.parseIfStatement()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
243 elif self.Peak == 'while':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
244 return self.parseWhileStatement()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
245 elif self.Peak == '{':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
246 return self.parseCompoundStatement()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
247 elif self.hasConsumed(';'):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
248 pass
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
249 elif self.Peak == 'var':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
250 self.parseVarDef()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
251 elif self.Peak == 'return':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
252 return self.parseReturnStatement()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
253 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
254 return self.AssignmentOrCall()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
255
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
256 def AssignmentOrCall(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
257 x = self.UnaryExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
258 if self.Peak == '=':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
259 # We enter assignment mode here.
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
260 loc = self.Consume('=').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
261 rhs = self.Expression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
262 return Assignment(x, rhs, loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
263 else:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
264 return ExpressionStatement(x, x.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
265
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
266 # Expression section:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
267 # We not implement these C constructs:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
268 # a(2), f = 2
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
269 # and this:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
270 # a = 2 < x : 4 ? 1;
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
271
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
272 def Expression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
273 exp = self.LogicalAndExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
274 while self.Peak == 'or':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
275 loc = self.Consume('or').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
276 e2 = self.LogicalAndExpression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
277 exp = Binop(exp, 'or', e2, loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
278 return exp
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
279
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
280 def LogicalAndExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
281 o = self.EqualityExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
282 while self.Peak == 'and':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
283 loc = self.Consume('and').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
284 o2 = self.EqualityExpression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
285 o = Binop(o, 'and', o2, loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
286 return o
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
287
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
288 def EqualityExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
289 ee = self.SimpleExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
290 while self.Peak in ['<', '==', '>']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
291 op = self.Consume(self.Peak)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
292 ee2 = self.SimpleExpression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
293 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
294 return ee
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
295
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
296 def SimpleExpression(self):
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
297 """ Shift operations before + and - ? """
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
298 e = self.AddExpression()
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
299 while self.Peak in ['>>', '<<']:
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
300 op = self.Consume(self.Peak)
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
301 e2 = self.AddExpression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
302 e = Binop(e, op.typ, e2, op.loc)
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
303 return e
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
304
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
305 def AddExpression(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
306 e = self.Term()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
307 while self.Peak in ['+', '-']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
308 op = self.Consume(self.Peak)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
309 e2 = self.Term()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
310 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
311 return e
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
312
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
313 def Term(self):
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
314 t = self.BitwiseOr()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
315 while self.Peak in ['*', '/']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
316 op = self.Consume(self.Peak)
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
317 t2 = self.BitwiseOr()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
318 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
319 return t
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
320
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
321 def BitwiseOr(self):
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
322 a = self.BitwiseAnd()
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
323 while self.Peak in ['|']:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
324 op = self.Consume(self.Peak)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
325 b = self.BitwiseAnd()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
326 a = Binop(a, op.typ, b, op.loc)
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
327 return a
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
328
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
329 def BitwiseAnd(self):
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
330 a = self.CastExpression()
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
331 while self.Peak in ['&']:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
332 op = self.Consume(self.Peak)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
333 b = self.CastExpression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
334 a = Binop(a, op.typ, b, op.loc)
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
335 return a
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
336
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
337 # Domain of unary expressions:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
338
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
339 def CastExpression(self):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
340 """
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
341 the C-style type cast conflicts with '(' expr ')'
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
342 so introduce extra keyword 'cast'
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
343 """
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
344 if self.Peak == 'cast':
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
345 loc = self.Consume('cast').loc
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
346 self.Consume('<')
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
347 t = self.parseTypeSpec()
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
348 self.Consume('>')
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
349 self.Consume('(')
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
350 ce = self.Expression()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
351 self.Consume(')')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
352 return TypeCast(t, ce, loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
353 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
354 return self.UnaryExpression()
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
355
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
356 def UnaryExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
357 if self.Peak in ['&', '*']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
358 op = self.Consume(self.Peak)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
359 ce = self.CastExpression()
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
360 if op.val == '*':
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
361 return Deref(ce, op.loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
362 else:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
363 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
364 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
365 return self.PostFixExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
366
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
367 def PostFixExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
368 pfe = self.PrimaryExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
369 while self.Peak in ['[', '(', '.', '->']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
370 if self.hasConsumed('['):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
371 pass
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
372 elif self.hasConsumed('('):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
373 # Function call
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
374 args = []
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
375 if not self.hasConsumed(')'):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
376 args.append(self.Expression())
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
377 while self.hasConsumed(','):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
378 args.append(self.Expression())
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
379 self.Consume(')')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
380 pfe = FunctionCall(pfe, args, pfe.loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
381 elif self.hasConsumed('->'):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
382 field = self.Consume('ID')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
383 pfe = Deref(pfe, pfe.loc)
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
384 pfe = FieldRef(pfe, field.val, field.loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
385 elif self.hasConsumed('.'):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
386 field = self.Consume('ID')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
387 pfe = FieldRef(pfe, field.val, field.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
388 else:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
389 raise Exception()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
390 return pfe
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
391
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
392 def PrimaryExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
393 if self.hasConsumed('('):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
394 e = self.Expression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
395 self.Consume(')')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
396 return e
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
397 elif self.Peak == 'NUMBER':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
398 val = self.Consume('NUMBER')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
399 return Literal(val.val, val.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
400 elif self.Peak == 'REAL':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
401 val = self.Consume('REAL')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
402 return Literal(val.val, val.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
403 elif self.Peak == 'true':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
404 val = self.Consume('true')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
405 return Literal(True, val.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
406 elif self.Peak == 'false':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
407 val = self.Consume('false')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
408 return Literal(False, val.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
409 elif self.Peak == 'ID':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
410 d = self.parseDesignator()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
411 return VariableUse(d, d.loc)
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))