Пример #1
0
    def visitPrimary(self, ctx):
        """ それぞれのPrimaryの値をUnitXObjectにラップして,応答する.

            Identifier: variable or function
            literal: number, string, boolean, none
            PAREN=(): expression
            BRACK=[]: list
        """
        unit = Unit()
        if ctx.unit(): unit = self.visitUnit(ctx.unit())

        if ctx.Identifier():
            # Here
            varname = ctx.Identifier().getText()
            unitx_obj = UnitXObject(value=None, varname=varname, unit=unit)
            """
            current_scope = self.get_scopes().peek()
            if varname in current_scope:
                unitx_obj = current_scope[varname]
                if not unit.is_empty():
                    unitx_obj.unit = unit
            else:
                unitx_obj = UnitXObject(value=None, varname=varname, unit=unit)
            """

            found_scope = self.get_scopes().peek().find_scope_of(varname)
            if found_scope:
                unitx_obj = found_scope[varname]
                if not unit.is_empty():
                    unitx_obj.unit = unit
            else:
                unitx_obj = UnitXObject(value=None, varname=varname, unit=unit)
            unitx_obj.token = ctx.Identifier().getSymbol()

        elif ctx.literal():
            unitx_obj = self.visitLiteral(ctx.literal())
            unitx_obj.unit = unit

        elif ctx.start.type == UnitXLexer.LPAREN:
            unitx_obj = self.visitExpression(ctx.expression(i=0))
            if not unit.is_empty():
                unitx_obj.unit = unit
            unitx_obj.token = ctx.start

        elif ctx.start.type == UnitXLexer.LBRACK:
            unitx_objs = []
            for an_expr in ctx.expression():
                an_obj = self.visitExpression(an_expr)
                if not unit.is_empty():
                    an_obj.unit = unit
                unitx_objs.append(an_obj)

            unitx_obj = UnitXObject(value = unitx_objs, varname = None, unit=unit, token=ctx.start)

        else:
            if not self.is_intaractive_run:
                raise Exception("Syntax error. EvalVisitor#visitPrimary") # Never happen.

        assert(isinstance(unitx_obj, UnitXObject))
        return unitx_obj