annotate 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
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()
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
64 elif self.Peak == 'const':
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
65 self.parseConstDef()
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
66 else:
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
67 self.Error('Expected function or variable')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
68
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
69 def parseDesignator(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
70 """ A designator designates an object """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
71 name = self.Consume('ID')
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
72 return self.sema.actOnDesignator(name.val, name.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
73
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
74 # Type system
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
75 def parseType(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
76 d = self.parseDesignator()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
77 return d
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
78
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
79 # Variable declarations:
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
80 def parseVarDef(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
81 self.Consume('var')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
82 t = self.parseType()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
83 def parseVar():
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
84 name = self.Consume('ID')
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
85 ival = None
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
86 if self.hasConsumed('='):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
87 ival = self.parseExpression()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
88 self.sema.actOnVarDef(name.val, name.loc, t, ival)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
89 parseVar()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
90 while self.hasConsumed(','):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
91 parseVar()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
92 self.Consume(';')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
93
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
94 def parseConstDef(self):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
95 self.Consume('const')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
96 t = self.parseType()
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
97 def parseConst():
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
98 name = self.Consume('ID')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
99 self.Consume('=')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
100 val = self.parseExpression()
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
101 self.sema.actOnConstDef(name.val, name.loc, t, val)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
102 parseConst()
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
103 while self.hasConsumed(','):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
104 parseConst()
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
105 self.Consume(';')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
106
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
107 # Procedures
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
108 def parseFunctionDefinition(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
109 self.Consume('function')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
110 returntype = self.parseType()
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
111 pname = self.Consume('ID')
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
112 self.sema.actOnFuncDef1(pname.val, pname.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
113 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
114 parameters = []
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
115 if not self.hasConsumed(')'):
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
116 def parseParameter():
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
117 typ = self.parseType()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
118 name = self.Consume('ID')
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
119 parameters.append(self.sema.actOnParameter(name.val, name.loc, typ))
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
120 parseParameter()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
121 while self.hasConsumed(','):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
122 parseParameter()
148
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 body = self.parseCompoundStatement()
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
125 self.sema.actOnFuncDef2(parameters, returntype, body)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
126
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
127 # Statements:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
128 def parseAssignment(self, lval):
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
129 lval = self.sema.actOnVariableUse(lval)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
130 self.Consume('=')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
131 rval = self.parseExpression()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
132 self.Consume(';')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
133 return astnodes.Assignment(lval, rval)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
134
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
135 def parseProcedureCall(self, procedure):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
136 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
137 args = []
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
138 if not self.hasConsumed(')'):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
139 args.append(self.parseExpression())
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
140 while self.hasConsumed(','):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
141 args.append(self.parseExpression())
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
142 self.Consume(')')
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
143 return astnodes.ProcedureCall(procedure, args)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
144
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
145 def parseIfStatement(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
146 self.Consume('if')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
147 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
148 condition = self.parseExpression()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
149 self.Consume(')')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
150 yes = self.parseCompoundStatement()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
151 if self.hasConsumed('else'):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
152 no = self.parseCompoundStatement()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
153 return astnodes.IfStatement(condition, yes, no)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
154 return astnodes.IfStatement(condition, yes, astnodes.EmptyStatement())
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 parseWhileStatement(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
157 self.Consume('while')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
158 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
159 condition = self.parseExpression()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
160 self.Consume(')')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
161 statements = self.parseCompoundStatement()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
162 return astnodes.WhileStatement(condition, statements)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
163
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
164 def parseReturnStatement(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
165 self.Consume('return')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
166 expr = self.parseExpression()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
167 self.Consume(';')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
168 return astnodes.ReturnStatement(expr)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
169
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
170 def parseCompoundStatement(self):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
171 self.Consume('{')
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
172 statements = []
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
173 while not self.hasConsumed('}'):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
174 statements.append(self.parseStatement())
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
175 return astnodes.CompoundStatement(statements)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
176
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
177 def parseStatement(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
178 # Determine statement type based on the pending token:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
179 if self.Peak == 'if':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
180 return self.parseIfStatement()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
181 elif self.Peak == 'while':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
182 return self.parseWhileStatement()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
183 elif self.Peak == '{':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
184 return self.parseCompoundStatement()
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
185 elif self.hasConsumed(';'):
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
186 return astnodes.EmptyStatement()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
187 elif self.Peak == 'var':
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
188 self.parseVarDef()
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
189 return astnodes.EmptyStatement()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
190 elif self.Peak == 'return':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
191 return self.parseReturnStatement()
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 designator = self.parseDesignator()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
194 if self.Peak == '(':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
195 return self.parseProcedureCall(designator)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
196 elif self.Peak == '=':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
197 return self.parseAssignment(designator)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
198 self.Error('Unable to determine statement')
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 # Parsing expressions:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
201 def parseExpression(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
202 return self.parseBinopRhs(self.parsePrimary(), 0)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
203 def parsePrimary(self):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
204 if self.hasConsumed('('):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
205 e = self.parseExpression()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
206 self.Consume(')')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
207 return e
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
208 elif self.Peak == 'NUMBER':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
209 val = self.Consume('NUMBER')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
210 return self.sema.actOnNumber(val.val, val.loc)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
211 elif self.Peak == 'REAL':
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
212 val = self.Consume('REAL')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
213 return self.sema.actOnNumber(val.val, val.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
214 elif self.Peak == 'ID':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
215 d = self.parseDesignator()
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
216 if self.Peak == '(':
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
217 return self.parseProcedureCall(d)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
218 else:
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
219 return self.sema.actOnVariableUse(d)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
220 self.Error('Expected NUM, ID or (expr), got {0}'.format(self.Peak))
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
221
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
222 def parseBinopRhs(self, lhs, min_prec):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
223 while self.PeakPrec >= min_prec:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
224 op_prec = self.PeakPrec
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
225 op = self.Consume(self.Peak)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
226 rhs = self.parsePrimary()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
227 while self.PeakPrec > op_prec:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
228 rhs = self.parseBinopRhs(rhs, self.PeakPrec)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
229 lhs = self.sema.actOnBinop(lhs, op.typ, rhs, op.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
230 return lhs
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
231