def __init__(self, is_intaractive_run, an_errhandler): """ EvalVisitorを初期化して応答する. """ self.is_test = False self.is_intaractive_run = is_intaractive_run self.errhandler = an_errhandler self.scopes = ScopeList() self.is_break = False self.is_return = False self.return_value = UnitXObject(value=None, varname=None, unit=None, token=None, is_none=True) this_dir, _ = os.path.split(__file__) data_path = os.path.join(this_dir, Constants.SYSTEM_UNIT_DATA) self.unit_manager = UnitManager(data_path) # Sets a database(data/unit_table.dat) for calculating units. self.stdlib = Stdlib() self.NULL_UNITX_OBJ = UnitXObject(value=None, varname=None, is_none=True, unit=Unit(), token=None) # # Sets a mediator to each classes for a management, # because this class is a mediator class. # Also, UnitXObject, Unit, Scope classes have to create many new instances. # So, We set a mediator by using classmethod. # self.scopes.set_mediator(self) self.unit_manager.set_mediator(self) self.stdlib.set_mediator(self) UnitXObject.set_mediator(self) Unit.set_mediator(self) Scope.set_mediator(self) DefinedFunction.set_mediator(self) UnitXObject.manager = self.get_unit_manager() UnitXObject.scopes = self.get_scopes()
def build_stdlib(self): """ """ for func in self.stdlib.funcs: #func.code = self.get_errlistener().get_code() var_unitx_obj = UnitXObject(value=None, varname=func.name, unit=Unit()) unitx_obj = UnitXObject(value=func, varname=func.name, unit=Unit()) var_unitx_obj.assign(unitx_obj, None)
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
def visitFunctionDeclaration(self, ctx): """ 関数宣言をする. """ if self._is_passing_block(): return func_token = ctx.Identifier().getSymbol() func_name = func_token.text func_args = self.visitFormalParameters(ctx.formalParameters()) code = self.get_errlistener().get_code() def_func = DefinedFunction(func_name, func_args, ctx, code) var_unitx_obj = UnitXObject(value=None, varname=func_name, unit=Unit(), token=func_token) unitx_obj = UnitXObject(value=def_func, varname=func_name, unit=Unit(), token=func_token) var_unitx_obj.assign(unitx_obj, None) return
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