# Visit a parse tree produced by LabeledExprParser#prog. defvisitProg(self, ctx:LabeledExprParser.ProgContext): return self.visitChildren(ctx)
# Visit a parse tree produced by LabeledExprParser#printExpr. defvisitPrintExpr(self, ctx:LabeledExprParser.PrintExprContext): value = self.visit(ctx.expr()) print(value) return0
# Visit a parse tree produced by LabeledExprParser#assign. defvisitAssign(self, ctx:LabeledExprParser.AssignContext): var = ctx.ID().getText() value = self.visit(ctx.expr()) self.vars[var] = value return value
# Visit a parse tree produced by LabeledExprParser#blank. defvisitBlank(self, ctx:LabeledExprParser.BlankContext): return self.visitChildren(ctx)
# Visit a parse tree produced by LabeledExprParser#parens. defvisitParens(self, ctx:LabeledExprParser.ParensContext): return self.visit(ctx.expr())
# Visit a parse tree produced by LabeledExprParser#MulDiv. defvisitMulDiv(self, ctx:LabeledExprParser.MulDivContext): left = self.visit(ctx.expr(0)) right = self.visit(ctx.expr(0)) if ctx.op.getType() == LabeledExprParser.MUL: return left * right return left / right
# Visit a parse tree produced by LabeledExprParser#AddSub. defvisitAddSub(self, ctx:LabeledExprParser.AddSubContext): left = self.visit(ctx.expr(0)) right = self.visit(ctx.expr(1)) if ctx.op.type == LabeledExprParser.ADD: return left + right return left - right
# Visit a parse tree produced by LabeledExprParser#id. defvisitId(self, ctx:LabeledExprParser.IdContext): var = ctx.ID().getText() if var in self.vars: # use before assignment? return self.vars[var] return0
# Visit a parse tree produced by LabeledExprParser#int. defvisitInt(self, ctx:LabeledExprParser.IntContext): returnint(ctx.INT().getText())
import sys from antlr4 import * from LabeledExprLexer import LabeledExprLexer from LabeledExprParser import LabeledExprParser from LabeledExprVisitor import LabeledExprVisitor