git @ Cat's Eye Technologies Strelnokoff / d7fed56
Refactor grammar, introducing Expression3 production. Chris Pressey 1 year, 4 months ago
2 changed file(s) with 24 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
1919 Assignment = Variable [Index] "=" Expression0.
2020 Expression0 = Expression1 {"=" Expression1 | ">" Expression1}.
2121 Expression1 = Expression2 {"+" Expression2 | "-" Expression2}.
22 Expression2 = Primitive {"*" Primitive | "/" Primitive}.
23 Primitive = ["PRINT" | "INPUT"] ["CHAR"]
24 ( Variable [Index]
25 | IntegerLiteral | CharLiteral
26 | "(" Expression0 ")"
27 ).
22 Expression2 = Expression3 {"*" Expression3 | "/" Expression3}.
23 Expression3 = ["PRINT" | "INPUT"] ["CHAR"] Primitive.
24 Primitive = Variable [Index]
25 | IntegerLiteral | CharLiteral
26 | "(" Expression0 ")".
2827 Index = "[" Expression0 {"," Expression0} "]".
2928
3029 ### Historical Notes
223223 return t
224224 end
225225
226 -- Strelnokoff = {Assignment}.
227 -- Assignment = Variable [Index] "=" Expression0.
228 -- Expression0 = Expression1 {"=" Expression1 | ">" Expression1}.
229 -- Expression1 = Expression2 {"+" Expression2 | "-" Expression2}.
230 -- Expression2 = Primitive {"*" Primitive | "/" Primitive}.
231 -- Primitive = ["PRINT" | "INPUT"] ["CHAR"] Variable [Index]
232 -- | IntegerLiteral | CharLiteral
233 -- | "(" Expression0 ")".
234 -- Index = "[" Expression0 {"," Expression0} "]".
235
236226 function Parser:program()
237227 local assignments = {}
238228 while self.scanner.toktype ~= "EOF" do
278268
279269 function Parser:expression2()
280270 local expr, t, rhs
281 expr = self:primitive()
271 expr = self:expression3()
282272 assert(expr ~= nil)
283273 while self.scanner.token == "*" or self.scanner.token == "/" do
284274 t = self:consume_token()
285 rhs = self:primitive()
275 rhs = self:expression3()
286276 assert(rhs ~= nil)
287277 expr = BinOp(t, expr, rhs)
288278 end
289279 return expr
290280 end
291281
292 function Parser:primitive()
282 function Parser:expression3()
293283 local action
294284 local mode = "int"
295285
296 -- FIXME I feel like this should probably change to be an operator...
297286 if self.scanner.token == "PRINT" then
298287 action = "PRINT"
299288 self.scanner:scan()
307296 self.scanner:scan()
308297 end
309298
299 local expr = self:primitive()
300
301 if action == "PRINT" then
302 return Action("print", mode, expr)
303 else
304 -- FIXME what about "INPUT" ... ?
305 return expr
306 end
307 end
308
309 function Parser:primitive()
310310 debug("parse", "primitive " .. self.scanner.toktype)
311311 if self.scanner.toktype == "number" then
312312 local val = tonumber(self.scanner.token)
313313 local expr = Literal(val)
314314 self.scanner:scan()
315 if action == "PRINT" then
316 return Action("print", mode, expr)
317 else
318 -- FIXME what about "INPUT" ... ?
319 return expr
320 end
315 return expr
321316 elseif self.scanner.toktype == "char" then
322317 local val = string.byte(self.scanner.token, 2)
323318 local expr = Literal(val)
324 -- FIXME just as above
325 self.scanner:scan()
326 if action == "PRINT" then
327 return Action("print", mode, expr)
328 else
329 -- FIXME what about "INPUT" ... ?
330 return expr
331 end
319 self.scanner:scan()
320 return expr
332321 elseif self.scanner.token == "(" then
333322 self.scanner:scan()
334323 local expr = self:expression0()
335324 self.scanner:expect(")")
336 -- FIXME just as above
337 if action == "PRINT" then
338 return Action("print", mode, expr)
339 else
340 -- FIXME what about "INPUT" ... ?
341 return expr
342 end
325 return expr
343326 else
344327 local name = self:expect_toktype("ident")
345328 local varindex = "" -- problematic for AST helpers if nil so we use this
347330 varindex = self:varindex()
348331 end
349332 debug("parse", "primitive varaccess " .. name .. "[]" .. tostring(varindex))
350 local expr = VarAccess(name, varindex)
351 if action == "PRINT" then
352 expr = Action("print", mode, expr)
353 end
354 debug("parse", "primitive varaccess expr " .. render(expr))
355 -- FIXME what about "INPUT" ... ?
356 return expr
333 return VarAccess(name, varindex)
357334 end
358335 end
359336