annotate python/c3/parser.py @ 149:74241ca312cc

Fixes on parser and semantics
author Windel Bouwman
date Fri, 01 Mar 2013 11:43:52 +0100
parents e5263f74b287
children 4ae0e02599de
rev   line source
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
1 from . import astnodes, lexer, semantics
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
2 from ppci.errors import CompilerException, SourceLocation
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
3
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
4 # binop precedence for expressions:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
5 binopPrecs = {'or': 5, 'and': 10, \
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
6 '<': 20, '>': 20, '==': 20, '<=': 20, '>=': 20, '!=': 20, \
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
7 '+': 30, '-': 30, '*': 40, '/': 40 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
8
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
9 class Parser:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
10 """ Parses sourcecode into an abstract syntax tree (AST) """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
11 def __init__(self, sema, diag):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
12 self.sema = sema
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13 self.diag = diag
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14 def parseSource(self, source):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 self.initLex(source)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
16 try:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17 self.parsePackage()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
18 except CompilerException as e:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19 self.diag.diag(e)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
20 def Error(self, msg):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
21 raise CompilerException(msg, self.token.loc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
22 def skipToSemiCol(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
23 while not (self.Peak == ';' or self.Peak == 'END'):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
24 self.NextToken()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
25 # Lexer helpers:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
26 def Consume(self, typ):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
27 if self.Peak == typ:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
28 return self.NextToken()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
29 else:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
30 self.Error('Excected: "{0}", got "{1}"'.format(typ, self.Peak))
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
31 @property
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
32 def Peak(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
33 return self.token.typ
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
34 @property
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
35 def PeakPrec(self):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
36 if self.Peak in binopPrecs:
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
37 return binopPrecs[self.Peak]
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
38 return -1
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
39 def hasConsumed(self, typ):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40 if self.Peak == typ:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41 self.Consume(typ)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
42 return True
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
43 return False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
44 def NextToken(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
45 t = self.token
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
46 if t.typ != 'END':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
47 self.token = self.tokens.__next__()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
48 return t
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
49 def initLex(self, source):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
50 self.tokens = lexer.tokenize(source) # Lexical stage
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
51 self.token = self.tokens.__next__()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
52
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
53 def parsePackage(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
54 self.Consume('package')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
55 name = self.Consume('ID')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
56 self.Consume(';')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
57 self.sema.handlePackage(name.val, name.loc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
58 # TODO: parse uses
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
59 while self.Peak != 'END':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
60 self.parseTopLevel()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
61 self.Consume('END')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
62
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
63 def parseTopLevel(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
64 if self.Peak == 'function':
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
65 self.parseFunctionDefinition()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
66 elif self.Peak == 'var':
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
67 self.parseVarDef()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
68 self.Consume(';')
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
69 else:
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
70 self.Error('Expected function or variable')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
71
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
72 def parseDesignator(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
73 """ A designator designates an object """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
74 name = self.Consume('ID')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
75 return name.val
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
76
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
77 # Type system
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
78 def parseType(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
79 d = self.parseDesignator()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
80 return d
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
81
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
82 # Variable declarations:
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
83 def parseVarDef(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
84 self.Consume('var')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
85 t = self.parseType()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
86 def parseVar():
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
87 name = self.Consume('ID')
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
88 ival = None
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
89 if self.hasConsumed('='):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
90 ival = self.parseExpression()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
91 self.sema.actOnVarDef(name.val, name.loc, t, ival)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
92 parseVar()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
93 while self.hasConsumed(','):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
94 parseVar()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
95
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
96 # Procedures
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
97 def parseFunctionDefinition(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
98 self.Consume('function')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
99 returntype = self.parseType()
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
100 pname = self.Consume('ID')
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
101 self.sema.actOnFuncDef1(pname.val, pname.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
102 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
103 parameters = []
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
104 if not self.hasConsumed(')'):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
105 typ = self.parseType()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
106 name = self.Consume('ID')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
107 parameters.append(astnodes.Parameter(name, typ))
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
108 while self.hasConsumed(','):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
109 typ = self.parseType()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
110 name = self.Consume('ID')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
111 parameters.append(astnodes.Parameter(name, typ))
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
112 self.Consume(')')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
113 body = self.parseCompoundStatement()
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
114 self.sema.actOnFuncDef2(parameters, returntype, body)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
115
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
116 # Statements:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
117 def parseAssignment(self, lval):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
118 self.Consume('=')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
119 rval = self.parseExpression()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
120 return astnodes.Assignment(lval, rval)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
121
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
122 def parseProcedureCall(self, procedure):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
123 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
124 args = []
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
125 if not self.hasConsumed(')'):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
126 args.append(self.parseExpression())
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
127 while self.hasConsumed(','):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
128 args.append(self.parseExpression())
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
129 self.Consume(')')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
130 return ProcedureCall(procedure, args)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
131
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
132 def parseIfStatement(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
133 self.Consume('if')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
134 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
135 condition = self.parseExpression()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
136 self.Consume(')')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
137 yes = self.parseCompoundStatement()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
138 if self.hasConsumed('else'):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
139 no = self.parseCompoundStatement()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
140 return astnodes.IfStatement(condition, yes, no)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
141 return astnodes.IfStatement(condition, yes)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
142
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
143 def parseWhileStatement(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
144 self.Consume('while')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
145 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
146 condition = self.parseExpression()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
147 self.Consume(')')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
148 statements = self.parseCompoundStatement()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
149 return astnodes.WhileStatement(condition, statements)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
150
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
151 def parseReturnStatement(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
152 self.Consume('return')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
153 expr = self.parseExpression()
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
154 return astnodes.ReturnStatement(expr)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
155
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
156 def parseCompoundStatement(self):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
157 self.Consume('{')
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
158 statements = [self.parseStatement()]
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
159 while self.hasConsumed(';'):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
160 statements.append(self.parseStatement())
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
161 self.Consume('}')
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
162 return astnodes.CompoundStatement(statements)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
163
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
164 def parseStatement(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
165 # Determine statement type based on the pending token:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
166 if self.Peak == 'if':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
167 return self.parseIfStatement()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
168 elif self.Peak == 'while':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
169 return self.parseWhileStatement()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
170 elif self.Peak == '{':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
171 return self.parseCompoundStatement()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
172 elif self.Peak == 'var':
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
173 return self.parseVarDef()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
174 elif self.Peak == 'return':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
175 return self.parseReturnStatement()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
176 elif self.Peak == 'ID':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
177 designator = self.parseDesignator()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
178 if self.Peak == '(':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
179 return self.parseProcedureCall(designator)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
180 elif self.Peak == '=':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
181 return self.parseAssignment(designator)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
182 self.Error('Unable to determine statement')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
183
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
184 # Parsing expressions:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
185 def parseExpression(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
186 return self.parseBinopRhs(self.parsePrimary(), 0)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
187 def parsePrimary(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
188 if self.hasConsumed('('):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
189 e = self.parseExpression()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
190 self.Consume(')')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
191 return e
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
192 elif self.Peak == 'NUMBER':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
193 val = self.Consume('NUMBER')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
194 return self.sema.actOnNumber(val.val, val.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
195 elif self.Peak == 'ID':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
196 d = self.parseDesignator()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
197 return d
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
198 self.Error('Expected NUM, ID or (expr), got {0}'.format(self.Peak))
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
199
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
200 def parseBinopRhs(self, lhs, min_prec):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
201 while self.PeakPrec >= min_prec:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
202 op_prec = self.PeakPrec
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
203 op = self.Consume(self.Peak)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
204 rhs = self.parsePrimary()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
205 while self.PeakPrec > op_prec:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
206 rhs = self.parseBinopRhs(rhs, self.PeakPrec)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
207 lhs = self.sema.actOnBinop(lhs, op.typ, rhs, op.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
208 return lhs
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
209