comparison python/c3/parser.py @ 280:02385f62f250

Rework from str interface to Instruction interface
author Windel Bouwman
date Sat, 02 Nov 2013 10:03:26 +0100
parents e64bae57cda8
children 05184b95fa16
comparison
equal deleted inserted replaced
279:2ccd57b1d78c 280:02385f62f250
137 while self.hasConsumed(','): 137 while self.hasConsumed(','):
138 parseVar() 138 parseVar()
139 self.Consume(';') 139 self.Consume(';')
140 140
141 def parseConstDef(self): 141 def parseConstDef(self):
142 self.Consume('const') 142 self.Consume('const')
143 t = self.parseTypeSpec() 143 t = self.parseTypeSpec()
144 def parseConst(): 144 def parseConst():
145 name = self.Consume('ID') 145 name = self.Consume('ID')
146 self.Consume('=') 146 self.Consume('=')
147 val = self.Expression() 147 val = self.Expression()
148 c = astnodes.Constant(name.val, t, val) 148 c = astnodes.Constant(name.val, t, val)
149 c.loc = name.loc 149 c.loc = name.loc
150 parseConst() 150 parseConst()
151 while self.hasConsumed(','): 151 while self.hasConsumed(','):
152 parseConst() 152 parseConst()
153 self.Consume(';') 153 self.Consume(';')
154 154
155 # Procedures 155 # Procedures
156 def parseFunctionDef(self): 156 def parseFunctionDef(self):
157 loc = self.Consume('function').loc 157 loc = self.Consume('function').loc
158 returntype = self.parseTypeSpec() 158 returntype = self.parseTypeSpec()
159 fname = self.Consume('ID').val 159 fname = self.Consume('ID').val
160 f = astnodes.Function(fname, loc) 160 f = astnodes.Function(fname, loc)
161 self.addDeclaration(f) 161 self.addDeclaration(f)
162 savePart = self.currentPart 162 savePart = self.currentPart
163 self.currentPart = f 163 self.currentPart = f
164 self.Consume('(') 164 self.Consume('(')
165 parameters = [] 165 parameters = []
166 if not self.hasConsumed(')'): 166 if not self.hasConsumed(')'):
167 def parseParameter(): 167 def parseParameter():
168 typ = self.parseTypeSpec() 168 typ = self.parseTypeSpec()
169 name = self.Consume('ID') 169 name = self.Consume('ID')
170 param = astnodes.FormalParameter(name.val, typ) 170 param = astnodes.FormalParameter(name.val, typ)
171 param.loc = name.loc 171 param.loc = name.loc
173 parameters.append(param) 173 parameters.append(param)
174 parseParameter() 174 parseParameter()
175 while self.hasConsumed(','): 175 while self.hasConsumed(','):
176 parseParameter() 176 parseParameter()
177 self.Consume(')') 177 self.Consume(')')
178 paramtypes = [p.typ for p in parameters] 178 paramtypes = [p.typ for p in parameters]
179 f.typ = astnodes.FunctionType(paramtypes, returntype) 179 f.typ = astnodes.FunctionType(paramtypes, returntype)
180 f.body = self.parseCompoundStatement() 180 f.body = self.parseCompoundStatement()
181 self.currentPart = savePart 181 self.currentPart = savePart
182 182
183 # Statements: 183 # Statements:
184 184
185 def parseIfStatement(self): 185 def parseIfStatement(self):
186 loc = self.Consume('if').loc 186 loc = self.Consume('if').loc
202 statements = self.parseCompoundStatement() 202 statements = self.parseCompoundStatement()
203 return astnodes.WhileStatement(condition, statements, loc) 203 return astnodes.WhileStatement(condition, statements, loc)
204 204
205 def parseReturnStatement(self): 205 def parseReturnStatement(self):
206 loc = self.Consume('return').loc 206 loc = self.Consume('return').loc
207 expr = self.Expression() 207 if self.Peak == ';':
208 expr = astnodes.Literal(0, loc)
209 else:
210 expr = self.Expression()
208 self.Consume(';') 211 self.Consume(';')
209 return astnodes.ReturnStatement(expr, loc) 212 return astnodes.ReturnStatement(expr, loc)
210 213
211 def parseCompoundStatement(self): 214 def parseCompoundStatement(self):
212 self.Consume('{') 215 self.Consume('{')