comparison python/c3/parser.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents 9683a4cd848f
children e023d3ce1d63
comparison
equal deleted inserted replaced
162:d8c735dc31f9 163:8104fc8b5e90
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 elif self.Peak == 'const':
65 self.parseConstDef()
64 else: 66 else:
65 self.Error('Expected function or variable') 67 self.Error('Expected function or variable')
66 68
67 def parseDesignator(self): 69 def parseDesignator(self):
68 """ A designator designates an object """ 70 """ A designator designates an object """
87 parseVar() 89 parseVar()
88 while self.hasConsumed(','): 90 while self.hasConsumed(','):
89 parseVar() 91 parseVar()
90 self.Consume(';') 92 self.Consume(';')
91 93
94 def parseConstDef(self):
95 self.Consume('const')
96 t = self.parseType()
97 def parseConst():
98 name = self.Consume('ID')
99 self.Consume('=')
100 val = self.parseExpression()
101 self.sema.actOnConstDef(name.val, name.loc, t, val)
102 parseConst()
103 while self.hasConsumed(','):
104 parseConst()
105 self.Consume(';')
106
92 # Procedures 107 # Procedures
93 def parseFunctionDefinition(self): 108 def parseFunctionDefinition(self):
94 self.Consume('function') 109 self.Consume('function')
95 returntype = self.parseType() 110 returntype = self.parseType()
96 pname = self.Consume('ID') 111 pname = self.Consume('ID')
191 self.Consume(')') 206 self.Consume(')')
192 return e 207 return e
193 elif self.Peak == 'NUMBER': 208 elif self.Peak == 'NUMBER':
194 val = self.Consume('NUMBER') 209 val = self.Consume('NUMBER')
195 return self.sema.actOnNumber(val.val, val.loc) 210 return self.sema.actOnNumber(val.val, val.loc)
211 elif self.Peak == 'REAL':
212 val = self.Consume('REAL')
213 return self.sema.actOnNumber(val.val, val.loc)
196 elif self.Peak == 'ID': 214 elif self.Peak == 'ID':
197 d = self.parseDesignator() 215 d = self.parseDesignator()
198 if self.Peak == '(': 216 if self.Peak == '(':
199 return self.parseProcedureCall(d) 217 return self.parseProcedureCall(d)
200 else: 218 else: