Пример #1
0
    def visitExpression(self, ctx):
        """ UnitXObject同士を計算した結果を返す.
            return: UnitXObject
        """
        if ctx.expression(i=0):
            x = self.visitExpression(ctx.expression(i=0)) # x,y: UnitXObject

            if ctx.start.type == UnitXLexer.INC:
                unitx_obj = x.increment()

            elif ctx.start.type == UnitXLexer.DEC:
                unitx_obj = x.decrement()

            elif ctx.getChild(i=1).getSymbol().type == UnitXLexer.LPAREN:
                #
                # Calls a function.
                # x: A UnitXObject of called function.
                #
                called_func_name = x.varname
                called_args = []
                if ctx.expressionList():
                    called_args = self.visitExpressionList(ctx.expressionList())

                found_scope = self.get_scopes().peek().find_scope_of(called_func_name)

                if found_scope:
                    def_func = found_scope[called_func_name].get_value()
                    self.get_scopes().new_scope()

                    called_func = self.__find_called_func(ctx)
                    self.get_errlistener().set_last_called_func(x.get_value())
                    unitx_obj = def_func.call(called_args, x, called_func)
                    self.get_errlistener().set_last_called_func(None)

                    self.get_scopes().del_scope()
                else:
                    msg = Constants.NAME_ERR % called_func_name
                    self.get_parser().notifyErrorListeners(msg, x.token, Exception(msg))
                    unitx_obj = UnitXObject(value=None, varname=None, unit=None, token=x, is_none=True)

                self.is_return = False

            else:
                second_token = ctx.getChild(i=1).getSymbol()
                y = self.visitExpression(ctx.expression(i=1))
                if second_token.type == UnitXLexer.ADD: unitx_obj = x.add(y, second_token)
                elif second_token.type == UnitXLexer.SUB: unitx_obj = x.subtract(y, second_token)
                elif second_token.type == UnitXLexer.MUL: unitx_obj = x.multiply(y, second_token)
                elif second_token.type == UnitXLexer.DIV: unitx_obj = x.divide(y, second_token)
                elif second_token.type == UnitXLexer.MOD: unitx_obj = x.modulo(y, second_token)
                elif second_token.type == UnitXLexer.ASSIGN: unitx_obj = x.assign(y, second_token)
                elif second_token.type == UnitXLexer.ADD_ASSIGN: unitx_obj = x.add_assign(y, second_token)
                elif second_token.type == UnitXLexer.SUB_ASSIGN: unitx_obj = x.substract_assign(y, second_token)
                elif second_token.type == UnitXLexer.MUL_ASSIGN: unitx_obj = x.multiply_assign(y, second_token)
                elif second_token.type == UnitXLexer.DIV_ASSIGN: unitx_obj = x.divide_assign(y, second_token)
                elif second_token.type == UnitXLexer.MOD_ASSIGN: unitx_obj = x.modulo_assign(y, second_token)
                elif second_token.type == UnitXLexer.EQUAL: unitx_obj = x.equals(y)
                elif second_token.type == UnitXLexer.EQUAL_X: unitx_obj = x.equals(y)
                elif second_token.type == UnitXLexer.NOTEQUAL:
                    unitx_obj = x.equals(y)
                    unitx_obj.set_value(not unitx_obj.get_value())
                else: unitx_obj = None

        elif ctx.primary(): unitx_obj = self.visitPrimary(ctx.primary())
        else:
            if not self.is_intaractive_run:
                raise Exception("Syntax error. EvalVisitor#visitExpression") # Never happen.

        assert(isinstance(unitx_obj, UnitXObject))

        return unitx_obj