Mercurial > lcfOS
comparison python/c3/parser.py @ 215:c1ccb1cb4cef
Major changes in c3 frontend
author | Windel Bouwman |
---|---|
date | Fri, 05 Jul 2013 13:00:03 +0200 |
parents | 003c8a976fff |
children | 1fa3e0050b49 |
comparison
equal
deleted
inserted
replaced
214:6875360e8390 | 215:c1ccb1cb4cef |
---|---|
47 return t | 47 return t |
48 | 48 |
49 def initLex(self, source): | 49 def initLex(self, source): |
50 self.tokens = lexer.tokenize(source) # Lexical stage | 50 self.tokens = lexer.tokenize(source) # Lexical stage |
51 self.token = self.tokens.__next__() | 51 self.token = self.tokens.__next__() |
52 def addDeclaration(self, decl): | |
53 self.currentPart.declarations.append(decl) | |
52 | 54 |
53 def parseUses(self): | 55 def parseUses(self): |
54 pass | 56 pass |
55 | 57 |
56 def parsePackage(self): | 58 def parsePackage(self): |
57 self.Consume('package') | 59 self.Consume('package') |
58 name = self.Consume('ID') | 60 name = self.Consume('ID') |
59 self.Consume(';') | 61 self.Consume(';') |
60 self.mod = astnodes.Package(name.val, name.loc) | 62 self.mod = astnodes.Package(name.val, name.loc) |
63 self.currentPart = self.mod | |
61 self.parseUses() | 64 self.parseUses() |
62 # TODO: parse uses | 65 # TODO: parse uses |
63 while self.Peak != 'END': | 66 while self.Peak != 'END': |
64 self.parseTopLevel() | 67 self.parseTopLevel() |
65 self.Consume('END') | 68 self.Consume('END') |
123 name = self.Consume('ID') | 126 name = self.Consume('ID') |
124 v = astnodes.Variable(name.val, t) | 127 v = astnodes.Variable(name.val, t) |
125 v.loc = name.loc | 128 v.loc = name.loc |
126 if self.hasConsumed('='): | 129 if self.hasConsumed('='): |
127 v.ival = self.parseExpression() | 130 v.ival = self.parseExpression() |
131 self.addDeclaration(v) | |
128 parseVar() | 132 parseVar() |
129 while self.hasConsumed(','): | 133 while self.hasConsumed(','): |
130 parseVar() | 134 parseVar() |
131 self.Consume(';') | 135 self.Consume(';') |
132 | 136 |
148 def parseFunctionDef(self): | 152 def parseFunctionDef(self): |
149 loc = self.Consume('function').loc | 153 loc = self.Consume('function').loc |
150 returntype = self.parseTypeSpec() | 154 returntype = self.parseTypeSpec() |
151 fname = self.Consume('ID').val | 155 fname = self.Consume('ID').val |
152 f = astnodes.Function(fname, loc) | 156 f = astnodes.Function(fname, loc) |
157 self.addDeclaration(f) | |
158 savePart = self.currentPart | |
159 self.currentPart = f | |
153 self.Consume('(') | 160 self.Consume('(') |
154 parameters = [] | 161 parameters = [] |
155 if not self.hasConsumed(')'): | 162 if not self.hasConsumed(')'): |
156 def parseParameter(): | 163 def parseParameter(): |
157 typ = self.parseTypeSpec() | 164 typ = self.parseTypeSpec() |
158 name = self.Consume('ID') | 165 name = self.Consume('ID') |
159 param = astnodes.Variable(name.val, typ) | 166 param = astnodes.Variable(name.val, typ) |
160 param.loc = name.loc | 167 param.loc = name.loc |
168 self.addDeclaration(param) | |
161 parameters.append(param) | 169 parameters.append(param) |
162 parseParameter() | 170 parseParameter() |
163 while self.hasConsumed(','): | 171 while self.hasConsumed(','): |
164 parseParameter() | 172 parseParameter() |
165 self.Consume(')') | 173 self.Consume(')') |
166 body = self.parseCompoundStatement() | 174 paramtypes = [p.typ for p in parameters] |
175 f.typ = astnodes.FunctionType(paramtypes, returntype) | |
176 f.body = self.parseCompoundStatement() | |
177 self.currentPart = savePart | |
167 | 178 |
168 # Statements: | 179 # Statements: |
169 def parseAssignment(self, lval): | 180 def parseAssignment(self, lval): |
170 lval = astnodes.VariableUse(lval, lval.loc) | 181 lval = astnodes.VariableUse(lval, lval.loc) |
171 loc = self.Consume('=').loc | 182 loc = self.Consume('=').loc |