def evaluate(self, state: Evaluator.Evaluator.EvaluationState = None):
     if state is None:
         state = Evaluator.Evaluator.EvaluationState()
         self.evaluate(state)
         if not state.has_function("main"):
             raise Evaluator.EvaluatorException("INTERPRETATION ERROR: Required function 'main' not found")
         main = state.lookup_function("main")
         if main[2]:
             val, ret = main[2].evaluate(state)
             provided_type = type(val).__name__
             if provided_type == "NoneType":
                 provided_type = "void"
             if ret == Evaluator.ReturnType.RETURN and provided_type != main[0]:
                 raise Evaluator.EvaluatorException("INTERPRETATION ERROR: Return type of 'main': {0}, provided {1} "
                                                    "instead".format(main[0], provided_type))
         return
     val, ret = self.__head.evaluate(state)
     if self.__tail is not None and self.__tail != "" and ret != Evaluator.ReturnType.RETURN:
         val, ret = self.__tail.evaluate(state)
     return val, ret
Ejemplo n.º 2
0
 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)):
         raise 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)):
         raise 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"
             raise Evaluator.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
Ejemplo n.º 3
0
 def evaluatorException(self, msg: str):
     if self.__line is not None:
         msg += " on line: " + str(self.__line)
         if self.__column is not None:
             msg += " column: " + str(self.__column)
     raise Evaluator.EvaluatorException(msg)