예제 #1
0
 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(state)
     if not state.has(str(self.__identifier)):
         super().evaluatorException(
             "INTERPRETATION ERROR: undeclared variable")
     return state.lookup(str(
         self.__identifier)), Evaluator.ReturnType.CONTINUE
 def evaluate(self, state: EvaluationState = None):
     if state is None:
         return self.evaluate(EvaluationState())
     if type(self.__num) != int:
         super().evaluatorException(
             "INTERPRETATION ERROR: expected integer value got {0}".format(
                 self.__num))
     return int(self.__num), Evaluator.ReturnType.CONTINUE
예제 #4
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))
 def evaluate(self, state: EvaluationState = None):
     if state is None:
         return self.evaluate(EvaluationState())
     if self.__expression is not None:
         val, ret = self.__expression.evaluate(state)
     else:
         val = list()
         ret = Evaluator.ReturnType.CONTINUE
     return val, ret
 def evaluate(self, state: EvaluationState = None):
     if state is None:
         return self.evaluate(EvaluationState())
     if self.__next_expression is None:
         val, ret = self.__current_expression.evaluate(state)
         return [val], Evaluator.ReturnType.CONTINUE
     val, ret = self.__next_expression.evaluate(state)
     val2, ret = self.__current_expression.evaluate(state)
     val.insert(0, val2)
     return val, Evaluator.ReturnType.CONTINUE
예제 #7
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")
 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
 def evaluate(self, state: EvaluationState = None):
     if state is None:
         return self.evaluate(EvaluationState())
     res, ret = self.__expression.evaluate(state)
     return res, ret
 def evaluate(self, state: EvaluationState = None):
     if state is None:
         return self.evaluate(EvaluationState())
    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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected boolean got {0}".format(
                        type(val1)))
            if val1:
                return True, Evaluator.ReturnType.CONTINUE
            if type(val2) is not bool:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected boolean got {0}".format(
                        type(val1)))
            if not val1:
                return False, Evaluator.ReturnType.CONTINUE
            if type(val2) is not bool:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val1)))
            if type(val2) is not int:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val1)))
            if type(val2) is not int:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val1)))
            if type(val2) is not int:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val1)))
            if type(val2) is not int:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val1)))
            if type(val2) is not int:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val1)))
            if type(val2) is not int:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val1)))
            if type(val2) is not int:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val1)))
            if type(val2) is not int:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val1)))
            if type(val2) is not int:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val1)))
            if type(val2) is not int:
                super().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:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val1)))
            if type(val2) is not int:
                super().evaluatorException(
                    "INTERPRETATION ERROR: Expected integer got {0}".format(
                        type(val2)))
            return val1 == val2, Evaluator.ReturnType.CONTINUE
        super().evaluatorException(
            "INTERPRETATION ERROR: Invalid binary operator expression")
예제 #12
0
 def evaluate(self, state: EvaluationState = None):
     if state is None:
         return self.evaluate(EvaluationState())
     return int(self.__num), Evaluator.ReturnType.CONTINUE