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
 def exitExpr(self, ctx: ExprParser.ExprContext):
     """Este método obtém as informações dos nós filhos e seus valores.
     Calcula o valor do nó conforme o operador e os operandos.
     
     Arguments:
         ctx {ExprParser.ExprContext} -- o contexto do nó
     """
     if (ctx.value is None):
         if (ctx.children and len(ctx.children) == 3):
             left = ctx.children[0]
             right = ctx.children[2]
             valor = None
             op = ctx.children[1].getText()
             operando1 = left.value
             operando2 = right.value
             if (op == '*'):
                 valor = operando1 * operando2
             elif (op == '/'):
                 valor = operando1 / operando2
             elif (op == '+'):
                 valor = operando1 + operando2
             else:
                 valor = operando1 - operando2
             ctx.value = valor
     if (ctx.value is not None
             and isinstance(ctx.parentCtx, ExprParser.ProgContext)):
         ctx.parentCtx.values.append({
             'expr': ctx.getText(),
             'value': ctx.value
         })
 def enterExpr(self, ctx: ExprParser.ExprContext):
     """Se o não-terminal `ctx` tiver um `INT`, então
     o valor do nó é o valor inteiro do `INT`. Caso contrário,
     o valor é `None`
     
     Arguments:
         ctx {ExprParser.ExprContext} -- o contexto do nó
     """
     if (ctx.INT() is not None):
         ctx.value = int(ctx.INT().getText())
     else:
         ctx.value = None
Beispiel #4
0
 def visitExpr(self, ctx: ExprParser.ExprContext):
     if ctx.getChildCount() == 1:
         n = next(ctx.getChildren())
         return int(n.getText())
     elif ctx.getChildCount() == 3:
         g = ctx.getChildren()
         l = [next(g) for i in range(3)]
         if (ExprParser.symbolicNames[l[1].getSymbol().type] == "MES"):
             return self.visit(l[0]) + self.visit(l[2])
         elif (ExprParser.symbolicNames[l[1].getSymbol().type] == "RES"):
             return self.visit(l[0]) - self.visit(l[2])
         elif (ExprParser.symbolicNames[l[1].getSymbol().type] == "MUL"):
             return self.visit(l[0]) * self.visit(l[2])
         elif (ExprParser.symbolicNames[l[1].getSymbol().type] == "DIV"):
             return self.visit(l[0]) / self.visit(l[2])
         elif (ExprParser.symbolicNames[l[1].getSymbol().type] == "POW"):
             return self.visit(l[0])**self.visit(l[2])
Beispiel #5
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])
Beispiel #6
0
    def visitExpr(self, ctx:ExprParser.ExprContext):
        
        l = [n for n in ctx.getChildren()]

        # Si solo hay un nodo, este es un numero
        if len(l) == 1: 
            n = l[0]
            print("| " * self.nivell +
                  ExprParser.symbolicNames[n.getSymbol().type] +
                  '(' +n.getText() + ')')
            self.nivell -= 1
        
        elif len(l) == 3:
            left = l[0]
            op = l[1]
            right = l[2]
            print('| ' *  self.nivell + '(' + op.getText() + ')')
            self.nivell += 1
            self.visit(left)
            self.nivell += 1
            self.visit(right)
            self.nivell -= 1
Beispiel #7
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:
         l = [i for i in ctx.getChildren()]
         print(' ' * self.nivell +
               ExprParser.symbolicNames[l[1].getSymbol().type] + "(" +
               l[1].getText() + ")")
         self.nivell += 1
         self.visit(ctx.expr(0))
         self.nivell += 1
         self.visit(ctx.expr(1))
         self.nivell -= 1
Beispiel #8
0
 def visitExpr(self, ctx: ExprParser.ExprContext):
     n = next(ctx.getChildren())
     return self.visit(n)