Ejemplo n.º 1
0
 def evaluate(self, state: EvaluationState = None):
     if state is None:
         return self.evaluate(EvaluationState())
     val, ret = self.__expression.evaluate(state)
     if self.__unary_op_type == UnaryOperatorType.NOT:
         if type(val) is not bool:
             raise EvaluatorException("INTERPRETATION ERROR: Expected boolean got {0}".format(type(val)))
         return not val, Evaluator.ReturnType.CONTINUE
     if self.__unary_op_type == UnaryOperatorType.MINUS:
         if type(val) is not int:
             raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val)))
         return -val, Evaluator.ReturnType.CONTINUE
     raise EvaluatorException("INTERPRETATION ERROR: Invalid unary operator expression")
Ejemplo n.º 2
0
 def evaluate(self, state: EvaluationState = None):
     if state is None:
         return self.evaluate(EvaluationState())
     if self.__boolean == "false":
         return False, Evaluator.ReturnType.CONTINUE
     if self.__boolean == "true":
         return True, Evaluator.ReturnType.CONTINUE
     raise EvaluatorException("INTERPRETATION ERROR: expected boolean value got {0}".format(self.__boolean))
Ejemplo n.º 3
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
 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 EvaluatorException(msg)
Ejemplo n.º 5
0
 def evaluate(self, state: EvaluationState = None):
     if state is None:
         return self.evaluate(state)
     if not state.has(str(self.__identifier)):
         raise EvaluatorException("INTERPRETATION ERROR: undeclared variable")
     return state.lookup(str(self.__identifier)), Evaluator.ReturnType.CONTINUE
Ejemplo n.º 6
0
    def evaluate(self, state: EvaluationState = None):
        if state is None:
            return self.evaluate(EvaluationState())

        val1, ret = self.__left.evaluate(state)
        val2, ret = self.__right.evaluate(state)

        if self.__bin_op_type == BinaryOperatorType.OR:
            if type(val1) is not bool:
                raise EvaluatorException("INTERPRETATION ERROR: Expected boolean got {0}".format(type(val1)))
            if val1:
                return True, Evaluator.ReturnType.CONTINUE
            if type(val2) is not bool:
                raise EvaluatorException("INTERPRETATION ERROR: Expected boolean got {0}".format(type(val2)))
            return val2, Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.AND:
            if type(val1) is not bool:
                raise EvaluatorException("INTERPRETATION ERROR: Expected boolean got {0}".format(type(val1)))
            if not val1:
                return False, Evaluator.ReturnType.CONTINUE
            if type(val2) is not bool:
                raise EvaluatorException("INTERPRETATION ERROR: Expected boolean got {0}".format(type(val2)))
            return val1 and val2, Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.NOT_EQUALS:
            if type(val1) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val1)))
            if type(val2) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val2)))
            return val1 != val2, Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.LOWER_EQ:
            if type(val1) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val1)))
            if type(val2) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val2)))
            return val1 <= val2, Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.GREATER_EQUAL:
            if type(val1) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val1)))
            if type(val2) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val2)))
            return val1 >= val2, Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.LOWER:
            if type(val1) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val1)))
            if type(val2) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val2)))
            return val1 < val2, Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.GREATER:
            if type(val1) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val1)))
            if type(val2) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val2)))
            return val1 > val2, Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.PLUS:
            if type(val1) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val1)))
            if type(val2) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val2)))
            return val1 + val2, Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.MINUS:
            if type(val1) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val1)))
            if type(val2) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val2)))
            return val1 - val2, Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.MULTIPLY:
            if type(val1) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val1)))
            if type(val2) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val2)))
            return val1 * val2, Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.DIVIDE:
            if type(val1) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val1)))
            if type(val2) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val2)))
            return int(val1 / val2), Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.MODULUS:
            if type(val1) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val1)))
            if type(val2) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val2)))
            return int(val1 % val2), Evaluator.ReturnType.CONTINUE
        if self.__bin_op_type == BinaryOperatorType.EQUALS:
            if type(val1) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val1)))
            if type(val2) is not int:
                raise EvaluatorException("INTERPRETATION ERROR: Expected integer got {0}".format(type(val2)))
            return val1 == val2, Evaluator.ReturnType.CONTINUE
        raise EvaluatorException("INTERPRETATION ERROR: Invalid binary operator expression")