def evaluate(self, state: Evaluator.EvaluationState = None): if self.__variable_type == "int": val = 0 else: val = False state.bind(str(self.__identifier), val) val, ret = self.__identifier.evaluate(state) return val, Evaluator.ReturnType.CONTINUE
def evaluate(self, state: EvaluationState = None): if state is None: return self.evaluate(EvaluationState()) if str(self.__identifier) == "print": val, ret = self.__expression.evaluate(state) for elem in val: print(elem, end=" ") print() return None, Evaluator.ReturnType.CONTINUE if not state.has_function(str(self.__identifier)): super().evaluatorException( "INTERPRETATION ERROR: Undefined function {0}".format( str(self.__identifier))) fun = state.lookup_function(str(self.__identifier)) params = fun[1] if params: params, ret = params.evaluate(state) passed_values, ret = self.__expression.evaluate(state) if (params is not None and passed_values is None) or (params is None and passed_values is not None) or \ (params is not None and passed_values is not None and len(params) != len(passed_values)): super().evaluatorException( "INTERPRETATION ERROR: number of parameters not valid") state.enter_scope() if passed_values is not None and params is not None: for i in range(len(passed_values)): state.bind(params[i][1], passed_values[i]) if fun[2]: res, ret = fun[2].evaluate(state) if ret == Evaluator.ReturnType.RETURN and type( res).__name__ != fun[0]: provided_type = type(res).__name__ if provided_type == "NoneType": provided_type = "void" super().evaluatorException( "INTERPRETATION ERROR: Return type of '{0}': {1}, provided {2} " "instead".format(str(self.__identifier), fun[0], provided_type)) else: res = None ret = Evaluator.ReturnType.CONTINUE state.exit_scope() return res, ret