def visitProgram(self, ctx: BKITParser.ProgramContext): if ctx.getChildCount() == 1: return Program([]) else: listDecl = [] for i in range(ctx.getChildCount() - 1): listDecl += self.visit(ctx.getChild(i)) return Program(listDecl)
def visitExp2(self, ctx: BKITParser.ProgramContext): if (ctx.getChildCount() == 1): return ctx.exp3().accept(self) return BinaryOp( ctx.getChild(1).getText(), ctx.exp2().accept(self), ctx.exp3().accept(self))
def visitVariable_assign(self, ctx: BKITParser.ProgramContext): variable = ctx.variable().accept(self) # variable ASSIGN literals if (ctx.getChildCount() == 3): literal = ctx.literal().accept(self) return VarDecl(variable['id'], variable['dimension'], ctx.literal().accept(self)) # variable return VarDecl(variable['id'], variable['dimension'], None)
def visitExp8(self, ctx: BKITParser.ProgramContext): if (ctx.ID()): return Id(ctx.ID().getText()) if (ctx.getChildCount() == 3): return ctx.exp().accept(self) return ctx.literal().accept(self)
def visitIndex_exp(self, ctx: BKITParser.ProgramContext): exp7 = ctx.exp7().accept(self) if (ctx.getChildCount() == 1): return exp7 exp_list = list(map(lambda x: x.accept(self), ctx.exp())) return ArrayCell(exp7, exp_list)
def visitExp3(self, ctx: BKITParser.ProgramContext): # right association should be calced first ? if (ctx.getChildCount() == 1): return ctx.exp4().accept(self) return BinaryOp( ctx.getChild(1).getText(), ctx.exp3().accept(self), ctx.exp4().accept(self))
def visitExp(self, ctx: BKITParser.ProgramContext): # exp1 if (ctx.getChildCount() == 1): return ctx.exp1(0).accept(self) # exp1 INT_EQ exp1 return BinaryOp( ctx.getChild(1).getText(), ctx.exp1(0).accept(self), ctx.exp1(1).accept(self))
def visitProgram(self, ctx: BKITParser.ProgramContext): lstdecl = [] for x in range(0, ctx.getChildCount() - 1): decl = self.visit(ctx.getChild(x)) if type(decl) == type([]): lstdecl += decl else: lstdecl.append(decl) return Program(lstdecl)
def visitExp7(self, ctx: BKITParser.ProgramContext): if (ctx.getChildCount() == 1): return ctx.exp8().accept(self) exps_list = ctx.exps_list().accept(self) return CallExpr(Id(ctx.ID().getText()), exps_list)
def visitExp5(self, ctx: BKITParser.ProgramContext): # index_exp if (ctx.getChildCount() == 1): return ctx.index_exp().accept(self) return UnaryOp(ctx.getChild(0).getText(), ctx.exp5().accept(self))
def visitExp4(self, ctx: BKITParser.ProgramContext): if (ctx.getChildCount() == 1): return ctx.exp5().accept(self) # return UnaryOp("[]", ArrayCell(None, [IntLiteral(value=34)])) return UnaryOp(ctx.getChild(0).getText(), ctx.exp4().accept(self))