Mercurial > lcfOS
comparison python/c3/parser.py @ 150:4ae0e02599de
Added type check start and analyze phase
author | Windel Bouwman |
---|---|
date | Fri, 01 Mar 2013 16:53:22 +0100 |
parents | 74241ca312cc |
children | b73bc14a3aa3 |
comparison
equal
deleted
inserted
replaced
149:74241ca312cc | 150:4ae0e02599de |
---|---|
17 self.parsePackage() | 17 self.parsePackage() |
18 except CompilerException as e: | 18 except CompilerException as e: |
19 self.diag.diag(e) | 19 self.diag.diag(e) |
20 def Error(self, msg): | 20 def Error(self, msg): |
21 raise CompilerException(msg, self.token.loc) | 21 raise CompilerException(msg, self.token.loc) |
22 def skipToSemiCol(self): | |
23 while not (self.Peak == ';' or self.Peak == 'END'): | |
24 self.NextToken() | |
25 # Lexer helpers: | 22 # Lexer helpers: |
26 def Consume(self, typ): | 23 def Consume(self, typ): |
27 if self.Peak == typ: | 24 if self.Peak == typ: |
28 return self.NextToken() | 25 return self.NextToken() |
29 else: | 26 else: |
41 self.Consume(typ) | 38 self.Consume(typ) |
42 return True | 39 return True |
43 return False | 40 return False |
44 def NextToken(self): | 41 def NextToken(self): |
45 t = self.token | 42 t = self.token |
46 if t.typ != 'END': | 43 if t.typ != 'END': self.token = self.tokens.__next__() |
47 self.token = self.tokens.__next__() | |
48 return t | 44 return t |
49 def initLex(self, source): | 45 def initLex(self, source): |
50 self.tokens = lexer.tokenize(source) # Lexical stage | 46 self.tokens = lexer.tokenize(source) # Lexical stage |
51 self.token = self.tokens.__next__() | 47 self.token = self.tokens.__next__() |
52 | 48 |
70 self.Error('Expected function or variable') | 66 self.Error('Expected function or variable') |
71 | 67 |
72 def parseDesignator(self): | 68 def parseDesignator(self): |
73 """ A designator designates an object """ | 69 """ A designator designates an object """ |
74 name = self.Consume('ID') | 70 name = self.Consume('ID') |
75 return name.val | 71 return self.sema.actOnDesignator(name.val, name.loc) |
76 | 72 |
77 # Type system | 73 # Type system |
78 def parseType(self): | 74 def parseType(self): |
79 d = self.parseDesignator() | 75 d = self.parseDesignator() |
80 return d | 76 return d |
100 pname = self.Consume('ID') | 96 pname = self.Consume('ID') |
101 self.sema.actOnFuncDef1(pname.val, pname.loc) | 97 self.sema.actOnFuncDef1(pname.val, pname.loc) |
102 self.Consume('(') | 98 self.Consume('(') |
103 parameters = [] | 99 parameters = [] |
104 if not self.hasConsumed(')'): | 100 if not self.hasConsumed(')'): |
105 typ = self.parseType() | 101 def parseParameter(): |
106 name = self.Consume('ID') | |
107 parameters.append(astnodes.Parameter(name, typ)) | |
108 while self.hasConsumed(','): | |
109 typ = self.parseType() | 102 typ = self.parseType() |
110 name = self.Consume('ID') | 103 name = self.Consume('ID') |
111 parameters.append(astnodes.Parameter(name, typ)) | 104 parameters.append(self.sema.actOnParameter(name.val, name.loc, typ)) |
105 parseParameter() | |
106 while self.hasConsumed(','): | |
107 parseParameter() | |
112 self.Consume(')') | 108 self.Consume(')') |
113 body = self.parseCompoundStatement() | 109 body = self.parseCompoundStatement() |
114 self.sema.actOnFuncDef2(parameters, returntype, body) | 110 self.sema.actOnFuncDef2(parameters, returntype, body) |
115 | 111 |
116 # Statements: | 112 # Statements: |
117 def parseAssignment(self, lval): | 113 def parseAssignment(self, lval): |
114 lval = astnodes.VariableUse(lval) | |
118 self.Consume('=') | 115 self.Consume('=') |
119 rval = self.parseExpression() | 116 rval = self.parseExpression() |
120 return astnodes.Assignment(lval, rval) | 117 return astnodes.Assignment(lval, rval) |
121 | 118 |
122 def parseProcedureCall(self, procedure): | 119 def parseProcedureCall(self, procedure): |
192 elif self.Peak == 'NUMBER': | 189 elif self.Peak == 'NUMBER': |
193 val = self.Consume('NUMBER') | 190 val = self.Consume('NUMBER') |
194 return self.sema.actOnNumber(val.val, val.loc) | 191 return self.sema.actOnNumber(val.val, val.loc) |
195 elif self.Peak == 'ID': | 192 elif self.Peak == 'ID': |
196 d = self.parseDesignator() | 193 d = self.parseDesignator() |
197 return d | 194 return self.sema.actOnVariableUse(d) |
198 self.Error('Expected NUM, ID or (expr), got {0}'.format(self.Peak)) | 195 self.Error('Expected NUM, ID or (expr), got {0}'.format(self.Peak)) |
199 | 196 |
200 def parseBinopRhs(self, lhs, min_prec): | 197 def parseBinopRhs(self, lhs, min_prec): |
201 while self.PeakPrec >= min_prec: | 198 while self.PeakPrec >= min_prec: |
202 op_prec = self.PeakPrec | 199 op_prec = self.PeakPrec |