Beispiel #1
0
    def visitExpr(self, ctx: ExprParser.ExprContext):
        if ctx.getChildCount() == 1:
            n = next(ctx.getChildren())
            print("  " * self.nivell +
                  ExprParser.symbolicNames[n.getSymbol().type] + '(' +
                  n.getText() + ')')
            self.nivell -= 1
        elif ctx.getChildCount() == 3:
            op = 'NOT DEFINED IN TreeVisitor.py'
            if ctx.MES():
                op = 'MES(+)'
            elif ctx.DIV():
                op = 'DIV(/)'
            elif ctx.MUL():
                op = 'MUL(*)'
            elif ctx.MENYS():
                op = 'MENYS(-)'
            elif ctx.POW():
                op = 'POW(^)'

            print('  ' * self.nivell + op)
            self.nivell += 1
            self.visit(ctx.expr(0))
            self.nivell += 1
            self.visit(ctx.expr(1))
            self.nivell -= 1
Beispiel #2
0
 def visitExpr(self, ctx: ExprParser.ExprContext):
     l = [n for n in ctx.getChildren()]
     if len(l) == 1:
         return int(l[0].getText())
     elif len(l) == 3:
         if ctx.MES():
             return self.visit(l[0]) + self.visit(l[2])
         elif ctx.MENYS():
             return self.visit(l[0]) - self.visit(l[2])
         elif ctx.MUL():
             return self.visit(l[0]) * self.visit(l[2])
         elif ctx.DIV():
             return self.visit(l[0]) / self.visit(l[2])
         elif ctx.POW():
             return self.visit(l[0])**self.visit(l[2])