annotate python/c3/parser.py @ 213:003c8a976fff

Merge of semantics and parser again ..
author Windel Bouwman
date Fri, 05 Jul 2013 11:18:48 +0200
parents 46d62dadd61b
children c1ccb1cb4cef
rev   line source
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
1 from . import astnodes, lexer
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:
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
10 """ Parses sourcecode into an abstract syntax tree (AST) """
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
11 def __init__(self, diag):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
12 self.diag = diag
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
13
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
14 def parseSource(self, source):
148
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()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
18 return self.mod
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 150
diff changeset
19 except CompilerError as e:
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 150
diff changeset
20 self.diag.addDiag(e)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
21 def Error(self, msg):
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 150
diff changeset
22 raise CompilerError(msg, self.token.loc)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
23 # Lexer helpers:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
24 def Consume(self, typ):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
25 if self.Peak == typ:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
26 return self.NextToken()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
27 else:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
28 self.Error('Excected: "{0}", got "{1}"'.format(typ, self.Peak))
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
29 @property
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
30 def Peak(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
31 return self.token.typ
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
32 @property
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
33 def PeakPrec(self):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
34 if self.Peak in binopPrecs:
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
35 return binopPrecs[self.Peak]
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
36 return -1
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
37 def hasConsumed(self, typ):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
38 if self.Peak == typ:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
39 self.Consume(typ)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40 return True
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41 return False
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
42
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
43 def NextToken(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
44 t = self.token
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
45 if t.typ != 'END':
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
46 self.token = self.tokens.__next__()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
47 return t
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
48
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
49 def initLex(self, source):
148
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__()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
52
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
53 def parseUses(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
54 pass
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
55
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
56 def parsePackage(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
57 self.Consume('package')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
58 name = self.Consume('ID')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
59 self.Consume(';')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
60 self.mod = astnodes.Package(name.val, name.loc)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
61 self.parseUses()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
62 # TODO: parse uses
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
63 while self.Peak != 'END':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
64 self.parseTopLevel()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
65 self.Consume('END')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
66
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
67 def parseTopLevel(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
68 if self.Peak == 'function':
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
69 self.parseFunctionDef()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
70 elif self.Peak == 'var':
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
71 self.parseVarDef()
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
72 elif self.Peak == 'const':
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
73 self.parseConstDef()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
74 elif self.Peak == 'type':
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
75 self.parseTypeDef()
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
76 else:
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
77 self.Error('Expected function, var, const or type')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
78
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
79 def parseDesignator(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
80 """ A designator designates an object """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
81 name = self.Consume('ID')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
82 d = astnodes.Designator(name.val, name.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
83 return d
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
84
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
85 # Type system
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
86 def parseTypeSpec(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
87 # For now, do simple type spec, just parse an ID:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
88 return self.parseDesignator()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
89 if self.Peak == 'struct':
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
90 self.Consume('struct')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
91 self.Consume('{')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
92 mems = []
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
93 while self.Peak != '}':
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
94 mem_t = self.parseTypeSpec()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
95 mem_n = self.Consume('ID')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
96 mems.append((mem_t, mem_n))
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
97 while self.hasConsumed(','):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
98 mem_n = self.Consume('ID')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
99 mems.append((mem_t, mem_n))
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
100 self.Consume(';')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
101 self.Consume('}')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
102 theT = astnodes.StructureType(mems)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
103 else:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
104 theT = self.parseDesignator()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
105 # Check for pointer suffix:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
106 while self.hasConsumed('*'):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
107 theT = astnodes.PointerType(theT)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
108 return theT
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
109
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
110 def parseTypeDef(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
111 self.Consume('type')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
112 newtype = self.parseTypeSpec()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
113 typename = self.Consume('ID')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
114 # TODO: action here :)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
115 self.Consume(';')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
116 return astnodes.DefinedType(typename, newtype)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
117
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
118 # Variable declarations:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
119 def parseVarDef(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
120 self.Consume('var')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
121 t = self.parseTypeSpec()
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
122 def parseVar():
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
123 name = self.Consume('ID')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
124 v = astnodes.Variable(name.val, t)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
125 v.loc = name.loc
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
126 if self.hasConsumed('='):
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
127 v.ival = self.parseExpression()
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
128 parseVar()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
129 while self.hasConsumed(','):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
130 parseVar()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
131 self.Consume(';')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
132
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
133 def parseConstDef(self):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
134 self.Consume('const')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
135 t = self.parseTypeSpec()
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
136 def parseConst():
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
137 name = self.Consume('ID')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
138 self.Consume('=')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
139 val = self.parseExpression()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
140 c = astnodes.Constant(name.val, t, val)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
141 c.loc = name.loc
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
142 parseConst()
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
143 while self.hasConsumed(','):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
144 parseConst()
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
145 self.Consume(';')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
146
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
147 # Procedures
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
148 def parseFunctionDef(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
149 loc = self.Consume('function').loc
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
150 returntype = self.parseTypeSpec()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
151 fname = self.Consume('ID').val
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
152 f = astnodes.Function(fname, loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
153 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
154 parameters = []
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
155 if not self.hasConsumed(')'):
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
156 def parseParameter():
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
157 typ = self.parseTypeSpec()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
158 name = self.Consume('ID')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
159 param = astnodes.Variable(name.val, typ)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
160 param.loc = name.loc
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
161 parameters.append(param)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
162 parseParameter()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
163 while self.hasConsumed(','):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
164 parseParameter()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
165 self.Consume(')')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
166 body = self.parseCompoundStatement()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
167
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
168 # Statements:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
169 def parseAssignment(self, lval):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
170 lval = astnodes.VariableUse(lval, lval.loc)
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
171 loc = self.Consume('=').loc
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
172 rval = self.parseExpression()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
173 self.Consume(';')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
174 return astnodes.Assignment(lval, rval, loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
175
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
176 def parseCall(self, func):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
177 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
178 args = []
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
179 if not self.hasConsumed(')'):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
180 args.append(self.parseExpression())
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
181 while self.hasConsumed(','):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
182 args.append(self.parseExpression())
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
183 self.Consume(')')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
184 return astnodes.FunctionCall(func, args, func.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
185
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
186 def parseIfStatement(self):
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
187 loc = self.Consume('if').loc
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
188 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
189 condition = self.parseExpression()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
190 self.Consume(')')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
191 yes = self.parseCompoundStatement()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
192 if self.hasConsumed('else'):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
193 no = self.parseCompoundStatement()
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
194 else:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
195 no = astnodes.EmptyStatement()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
196 return astnodes.IfStatement(condition, yes, no, loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
197
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
198 def parseWhileStatement(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
199 loc = self.Consume('while').loc
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
200 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
201 condition = self.parseExpression()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
202 self.Consume(')')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
203 statements = self.parseCompoundStatement()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
204 return astnodes.WhileStatement(condition, statements, loc)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
205
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
206 def parseReturnStatement(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
207 self.Consume('return')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
208 expr = self.parseExpression()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
209 self.Consume(';')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
210 return astnodes.ReturnStatement(expr)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
211
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
212 def parseCompoundStatement(self):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
213 self.Consume('{')
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
214 statements = []
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
215 while not self.hasConsumed('}'):
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
216 s = self.parseStatement()
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
217 if not type(s) is astnodes.EmptyStatement:
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
218 statements.append(s)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
219 return astnodes.CompoundStatement(statements)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
220
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
221 def parseStatement(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
222 # Determine statement type based on the pending token:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
223 if self.Peak == 'if':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
224 return self.parseIfStatement()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
225 elif self.Peak == 'while':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
226 return self.parseWhileStatement()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
227 elif self.Peak == '{':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
228 return self.parseCompoundStatement()
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
229 elif self.hasConsumed(';'):
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
230 return astnodes.EmptyStatement()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
231 elif self.Peak == 'var':
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
232 self.parseVarDef()
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
233 return astnodes.EmptyStatement()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
234 elif self.Peak == 'return':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
235 return self.parseReturnStatement()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
236 else:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
237 designator = self.parseDesignator()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
238 if self.Peak == '(':
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
239 return self.parseCall(designator)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
240 elif self.Peak == '=':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
241 return self.parseAssignment(designator)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
242 else:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
243 self.Error('Unable to determine statement')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
244
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
245 # Parsing expressions:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
246 def parseExpression(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
247 return self.parseBinopRhs(self.parsePrimary(), 0)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
248
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
249 def parsePrimary(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
250 if self.hasConsumed('('):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
251 e = self.parseExpression()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
252 self.Consume(')')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
253 return e
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
254 elif self.Peak == 'NUMBER':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
255 val = self.Consume('NUMBER')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
256 return astnodes.Literal(val.val, val.loc)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
257 elif self.Peak == 'REAL':
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
258 val = self.Consume('REAL')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
259 return astnodes.Literal(val.val, val.loc)
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
260 elif self.Peak == 'true':
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
261 val = self.Consume('true')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
262 return astnodes.Literal(True, val.loc)
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
263 elif self.Peak == 'false':
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
264 val = self.Consume('false')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
265 return astnodes.Literal(False, val.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
266 elif self.Peak == 'ID':
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
267 d = self.parseDesignator()
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
268 if self.Peak == '(':
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
269 return self.parseCall(d)
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
270 else:
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
271 return astnodes.VariableUse(d, d.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
272 self.Error('Expected NUM, ID or (expr), got {0}'.format(self.Peak))
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
273
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
274 def parseBinopRhs(self, lhs, min_prec):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
275 while self.PeakPrec >= min_prec:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
276 op_prec = self.PeakPrec
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
277 op = self.Consume(self.Peak)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
278 rhs = self.parsePrimary()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
279 while self.PeakPrec > op_prec:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
280 rhs = self.parseBinopRhs(rhs, self.PeakPrec)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
281 lhs = astnodes.Binop(lhs, op.typ, rhs, op.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
282 return lhs
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
283