comparison python/c3/parser.py @ 157:8f3924b6076e

Added some code generator things
author Windel Bouwman
date Sun, 03 Mar 2013 18:14:35 +0100
parents b28a11c01dbe
children 9683a4cd848f
comparison
equal deleted inserted replaced
156:1b4a85bdd99c 157:8f3924b6076e
59 def parseTopLevel(self): 59 def parseTopLevel(self):
60 if self.Peak == 'function': 60 if self.Peak == 'function':
61 self.parseFunctionDefinition() 61 self.parseFunctionDefinition()
62 elif self.Peak == 'var': 62 elif self.Peak == 'var':
63 self.parseVarDef() 63 self.parseVarDef()
64 self.Consume(';')
65 else: 64 else:
66 self.Error('Expected function or variable') 65 self.Error('Expected function or variable')
67 66
68 def parseDesignator(self): 67 def parseDesignator(self):
69 """ A designator designates an object """ 68 """ A designator designates an object """
86 ival = self.parseExpression() 85 ival = self.parseExpression()
87 self.sema.actOnVarDef(name.val, name.loc, t, ival) 86 self.sema.actOnVarDef(name.val, name.loc, t, ival)
88 parseVar() 87 parseVar()
89 while self.hasConsumed(','): 88 while self.hasConsumed(','):
90 parseVar() 89 parseVar()
90 self.Consume(';')
91 91
92 # Procedures 92 # Procedures
93 def parseFunctionDefinition(self): 93 def parseFunctionDefinition(self):
94 self.Consume('function') 94 self.Consume('function')
95 returntype = self.parseType() 95 returntype = self.parseType()
112 # Statements: 112 # Statements:
113 def parseAssignment(self, lval): 113 def parseAssignment(self, lval):
114 lval = astnodes.VariableUse(lval) 114 lval = astnodes.VariableUse(lval)
115 self.Consume('=') 115 self.Consume('=')
116 rval = self.parseExpression() 116 rval = self.parseExpression()
117 self.Consume(';')
117 return astnodes.Assignment(lval, rval) 118 return astnodes.Assignment(lval, rval)
118 119
119 def parseProcedureCall(self, procedure): 120 def parseProcedureCall(self, procedure):
120 self.Consume('(') 121 self.Consume('(')
121 args = [] 122 args = []
146 return astnodes.WhileStatement(condition, statements) 147 return astnodes.WhileStatement(condition, statements)
147 148
148 def parseReturnStatement(self): 149 def parseReturnStatement(self):
149 self.Consume('return') 150 self.Consume('return')
150 expr = self.parseExpression() 151 expr = self.parseExpression()
152 self.Consume(';')
151 return astnodes.ReturnStatement(expr) 153 return astnodes.ReturnStatement(expr)
152 154
153 def parseCompoundStatement(self): 155 def parseCompoundStatement(self):
154 self.Consume('{') 156 self.Consume('{')
155 statements = [self.parseStatement()] 157 statements = []
156 while self.hasConsumed(';'): 158 while not self.hasConsumed('}'):
157 if self.Peak == '}': break # Permit last ';'
158 statements.append(self.parseStatement()) 159 statements.append(self.parseStatement())
159 self.Consume('}')
160 return astnodes.CompoundStatement(statements) 160 return astnodes.CompoundStatement(statements)
161 161
162 def parseStatement(self): 162 def parseStatement(self):
163 # Determine statement type based on the pending token: 163 # Determine statement type based on the pending token:
164 if self.Peak == 'if': 164 if self.Peak == 'if':
166 elif self.Peak == 'while': 166 elif self.Peak == 'while':
167 return self.parseWhileStatement() 167 return self.parseWhileStatement()
168 elif self.Peak == '{': 168 elif self.Peak == '{':
169 return self.parseCompoundStatement() 169 return self.parseCompoundStatement()
170 elif self.Peak == ';': 170 elif self.Peak == ';':
171 self.Consume(';')
171 return astnodes.EmptyStatement() 172 return astnodes.EmptyStatement()
172 elif self.Peak == 'var': 173 elif self.Peak == 'var':
173 return self.parseVarDef() 174 return self.parseVarDef()
174 elif self.Peak == 'return': 175 elif self.Peak == 'return':
175 return self.parseReturnStatement() 176 return self.parseReturnStatement()