annotate python/c3/parser.py @ 153:e05b2b216bfc

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