comparison python/c3/parser.py @ 272:e64bae57cda8

refactor ir
author Windel Bouwman
date Sat, 31 Aug 2013 17:58:54 +0200
parents bd26dc13f270
children 02385f62f250
comparison
equal deleted inserted replaced
271:cf7d5fb7d9c8 272:e64bae57cda8
122 df = astnodes.DefinedType(typename.val, newtype, typename.loc) 122 df = astnodes.DefinedType(typename.val, newtype, typename.loc)
123 self.addDeclaration(df) 123 self.addDeclaration(df)
124 124
125 # Variable declarations: 125 # Variable declarations:
126 def parseVarDef(self): 126 def parseVarDef(self):
127 self.Consume('var') 127 self.Consume('var')
128 t = self.parseTypeSpec() 128 t = self.parseTypeSpec()
129 def parseVar(): 129 def parseVar():
130 name = self.Consume('ID') 130 name = self.Consume('ID')
131 v = astnodes.Variable(name.val, t) 131 v = astnodes.Variable(name.val, t)
132 v.loc = name.loc 132 v.loc = name.loc
133 if self.hasConsumed('='): 133 if self.hasConsumed('='):
134 v.ival = self.Expression() 134 v.ival = self.Expression()
135 self.addDeclaration(v) 135 self.addDeclaration(v)
136 parseVar() 136 parseVar()
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():
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.Variable(name.val, typ) 170 param = astnodes.FormalParameter(name.val, typ)
171 param.loc = name.loc 171 param.loc = name.loc
172 self.addDeclaration(param) 172 self.addDeclaration(param)
173 parameters.append(param) 173 parameters.append(param)
174 parseParameter()
175 while self.hasConsumed(','):
176 parseParameter() 174 parseParameter()
177 self.Consume(')') 175 while self.hasConsumed(','):
176 parseParameter()
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
187 self.Consume('(') 187 self.Consume('(')
188 condition = self.Expression() 188 condition = self.Expression()
189 self.Consume(')') 189 self.Consume(')')
190 yes = self.parseCompoundStatement() 190 yes = self.parseCompoundStatement()
191 if self.hasConsumed('else'): 191 if self.hasConsumed('else'):
192 no = self.parseCompoundStatement() 192 no = self.parseCompoundStatement()
193 else: 193 else:
194 no = None 194 no = None
195 return astnodes.IfStatement(condition, yes, no, loc) 195 return astnodes.IfStatement(condition, yes, no, loc)
196 196
197 def parseWhileStatement(self): 197 def parseWhileStatement(self):
198 loc = self.Consume('while').loc 198 loc = self.Consume('while').loc
199 self.Consume('(') 199 self.Consume('(')
200 condition = self.Expression() 200 condition = self.Expression()