Beispiel #1
0
    def make_special_function_input(self):

        exp = self._operand.pop()

        if exp is None:
            raise BigError("No given value to save input")
        self._quadruple.add(QuadruplePack(SpecialFunction.INPUT, result=exp))
Beispiel #2
0
    def eval_dim(self):
        """
              Function to evaluate the expression for each dimension.

              Generates the quadruple of VER (Verification of dimension) and the multiplication of the expression times
              the m value
        """
        exp = self._operand.pop()
        dim = self._top_dim()
        if exp.type_var != Types.INT:
            raise BigError.invalid_value(
                "The value of the array {}  is not an Integer".format(
                    exp.id_var))

        if exp is not None:

            dimS = FluffyVariable(None, Types.INT, addr=dim.size)
            dimM = self.add_constants(dim.m, Types.INT)  # m dim
            self._operand.pop()
            dimInf = self.add_constants(0, Types.INT)  # Dimension inferior
            self._operand.pop()
            tem = FluffyVariable(None,
                                 exp.type_var,
                                 addr=address.set_addr(exp.type_var))
            #  Validate dim and generate VER
            self._quadruple.add(QuadruplePack(Operator.VER, exp, dimInf, dimS))
            self._quadruple.add(QuadruplePack(Operator.TIMES, exp, dimM,
                                              tem))  # x m
            self._operand.append(tem)
        else:
            raise BigError("Error in array expression")
Beispiel #3
0
    def function_return(self):

        return_value = self._operand.pop()

        if return_value is None:
            raise BigError("Error on retrun data")

        check_data = cube.compare_types(Operator.RETURN, return_value.type_var,
                                        self.active_function.return_type)

        if check_data:
            result = FluffyVariable(
                None,
                type_var=self.active_function.return_type,
                addr=address.set_addr(kind=self.active_function.return_type))

            quadruple = QuadruplePack(Operator.RETURN,
                                      l_value=return_value,
                                      r_value=None,
                                      result=result)

            self._quadruple.add(quadruple)
        else:
            raise BigError.mismatch_operator(
                "Return type {} does't correspond to the return type of function {} "
                .format(return_value.type_var,
                        self.active_function.return_type))
Beispiel #4
0
    def _fill(self, line=0):

        actual_quadruple = self._jumps.pop()
        if actual_quadruple is None:
            raise BigError("Error, pending quadruples")
        else:
            available_quadruple = self._quadruple.index + line
            address_quadruple = FluffyVariable(None,
                                               None,
                                               addr=available_quadruple)
            self._quadruple.fill(actual_quadruple, address_quadruple)
Beispiel #5
0
    def set_addr(self, kind, g_var=False):
        if kind not in self.local_address:
            raise BigError("Error type not defined")
        # Global variable
        if g_var:
            actual_value = self.global_address[kind]
            self.global_address[kind] += 1
        # Local variables
        else:
            actual_value = self.local_address[kind]
            self.local_address[kind] += 1

        return actual_value
Beispiel #6
0
    def create_assignation(self):
        expression = self._operand.pop()
        variable = self._operand.pop()

        if variable is None or expression is None:
            raise BigError('None Value to assign')
        comparison = cube.compare_types(Operator.EQUAL, expression.type_var,
                                        variable.type_var)
        if comparison:
            q = QuadruplePack(Operator.EQUAL, expression, None, variable)
            self._quadruple.add(q)

        else:
            raise BigError.mismatch_assignation(
                "The expression has type {} and expression has type {} they're incompatible"
                .format(expression.type_var.name, variable.type_var.name))
Beispiel #7
0
    def jump_false(self):
        # if len(self._operand) == 0:

        self._jumps.append(self._quadruple.index)

        result = self._operand.pop()

        if result is None:
            print(self._operand)
            raise BigError("The stack is empty")

        if result.type_var != Types.BOOLEAN:
            raise BigError.mismatch_operator(
                "The operation doesn't return a boolean value")

        # Generate GotoFalse, return to fill the address
        self._quadruple.add(QuadruplePack(Operator.GOTOF, result, None, None))
Beispiel #8
0
    def make_special_function(self, action_name, expected_type=None):
        """
        Generic function for crating almost all special function
        :param action_name:
        :param expected_type:
        :return: None, Insert quadruple in stack
        """
        action_name = self.text_to_special_operator(action_name)
        exp = self._operand.pop()

        if exp is None:
            raise BigError("Error getting value for function")

        if expected_type is not None:
            if exp.type_var not in expected_type:
                print(action_name)
                raise BigError.invalid_type(
                    "Function {} only accepts expression of type {} ".format(
                        action_name.name,
                        [item.name for item in expected_type]))

        q = QuadruplePack(action_name, None, None, exp)
        self._quadruple.add(q)
Beispiel #9
0
 def text_to_type(tp):
     for t in list(Types):
         if tp.upper() == t.name:
             return t
     raise BigError("Type {} is not a valid one".format(tp))
Beispiel #10
0
 def text_to_special_operator(op):
     for e in list(SpecialFunction):
         if op.upper() == e.name:
             return e
     raise BigError("Operator {} is not a valid one".format(op))
Beispiel #11
0
 def text_to_operator(op):
     op = match_operators.get(op)
     for e in list(Operator):
         if op == e.name:
             return e
     raise BigError("Operator {} is not a valid one".format(op))
Beispiel #12
0
 def get_addr(self, kind, g_var=False):
     if kind not in self.local_address:
         raise BigError("Error type not defined")
     if g_var:
         return self.global_address[kind]
     return self.local_address[kind]