def array(self, ctx):
     """
     Name: array
     Description: Gets info from an array.
     Parameters:
         ctx: Holds the context of the array rule.
     Returns: Information about an array.
     Important methods where its called:
         extended_literal: To get the information about the array literal.
     """
     item = int(ctx.Number().getText())
     rest_items = self.array_dimension(ctx.array_dimension())
     items = [item] + rest_items
     final_items = []
     size = 1
     for item in items:
         size = size * item
     temp = size
     new_dir = memory.get_last_constant()
     memory.constant_data.append(size)
     for item in items:
         temp = temp / item
         final_items.append({"m": memory.get_last_constant(), "size": item})
         memory.constant_data.append(temp)
     return Variable("", "LIST", None, (size, final_items, new_dir))
 def definition_function(self, ctx):
     """
     Name: definition_function
     Description: Compiles the definition of a user defined function.
     Parameters:
         ctx: Holds the context of the definition rule.
     Returns: NA
     Important methods where its called:
         definition: To define a new user defined function.
     """
     memory.add_quadruple(Operator.STARTPROC, None, None, None)
     initial_virtual_direction = memory.get_last_code()
     literal = self.extended_literal(ctx.extended_literal())
     return_direction = memory.get_last_global()
     if literal.type == 'LIST':
         (size, _, _) = literal.array_info
         literal.virtual_direction = return_direction
         literal.constant_direction = memory.get_last_constant()
         memory.add_constant(return_direction, 'NUMBER')
         for _ in range(0, size):
             gl.add_memory(None)
     else:
         memory.global_data.append(None)
         memory.add_quadruple(Operator.ASSIGN, literal.virtual_direction,
                              None, return_direction)
     (function_name, parameters) = self.definition_function_name(
         ctx.definition_function_name())
     # Change scope inside of the new function
     gl.current_scope = function_name
     parameters = definition_function_parameters(self, ctx, parameters)
     create_function(self, ctx, function_name, parameters,
                     initial_virtual_direction, literal, return_direction)
def create_variable(variable_name, literal):
    """
    Name: create_variable
    Description: Creates a variable in the current scope.
    Parameters:
        variable_name: The name of the variable to create.
        literal: The information of the content of the variable.
    Returns: NA
    Important methods where its called:
        definition_variable: To define an array or variable.
    """
    check_variable_type(variable_name, literal)
    variable = gl.get_variable(variable_name)
    if not variable:
        # Add variable if not already declared.
        new_dir = gl.get_last_data()
        if literal.type == 'LIST':
            (size, _, _) = literal.array_info
            literal.virtual_direction = new_dir
            literal.name = variable_name
            literal.constant_direction = memory.get_last_constant()
            memory.add_constant(new_dir, 'NUMBER')
            new_variable = literal
            for _ in range(0, size):
                gl.add_memory(None)
        else:
            gl.add_memory(None)
            new_variable = Variable(variable_name, literal.type, new_dir)
        gl.add_variable(variable_name, new_variable)
        variable = gl.get_variable(variable_name)
    else:
        if literal.type == 'LIST':
            new_dir = gl.get_last_data()
            (size, _, _) = literal.array_info
            literal.virtual_direction = new_dir
            literal.name = variable_name
            literal.constant_direction = memory.get_last_constant()
            memory.add_constant(new_dir, 'NUMBER')
            new_variable = literal
            for _ in range(0, size):
                gl.add_memory(None)
            gl.add_variable(variable_name, new_variable)
            variable = gl.get_variable(variable_name)
    if literal.type != 'LIST':
        memory.add_assign(literal.virtual_direction,
                          variable.virtual_direction)
def create_literal(self, ctx):
    """
    Name: create_literal
    Description: Gets or creates a literal adding it to memory (if needed).
    Parameters:
        ctx: Holds the context of the basic_literal rule.
    Returns: Information about a basic literal (string, number, etc).
    Important methods where its called:
        basic_literal: To create and get the information of a literal.
    """
    if ctx.identification() != None:
        id = self.identification(ctx.identification())
        check_variable_exits(id)
        return gl.get_all_variables()[id]
    elif ctx.array_access() != None:
        (id, direction) = self.array_access(ctx.array_access())
        return Variable("", 'ANY', direction)
    elif ctx.execution() != None:
        (function_name, virtual_direction) = self.execution(ctx.execution())
        exection_type = gl.functions[function_name].type
        array_info = gl.functions[function_name].return_array_info
        if virtual_direction == None:
            return_direction = gl.functions[function_name].return_direction
        else:
            return_direction = virtual_direction
        return Variable("", exection_type, return_direction, array_info)
    elif ctx.String() != None:
        memory.add_constant(ctx.String().getText(), "STRING")
        return Variable("", "STRING", memory.get_last_constant() - 1)
    elif ctx.Boolean() != None:
        memory.add_constant(ctx.Boolean().getText(), "BOOLEAN")
        return Variable("", "BOOLEAN", memory.get_last_constant() - 1)
    elif ctx.Number() != None:
        memory.add_constant(ctx.Number().getText(), "NUMBER")
        return Variable("", "NUMBER", memory.get_last_constant() - 1)
    elif ctx.Empty() != None:
        memory.add_constant(None, "ANY")
        return Variable("", "ANY", memory.get_last_constant() - 1)
 def defintion_parameter(self, ctx):
     """
     Name: defintion_parameter
     Description: Generates quadruples for parameters of a function with its default value.
     Parameters:
         ctx: Holds the context of the definition_function rule.
     Returns: NA
     Important methods where its called:
         get_parameters_data: To access information about a parameter.
     """
     variable_name = self.identification(ctx.identification())
     literal = self.extended_literal(ctx.extended_literal())
     new_dir = gl.get_last_data()
     gl.add_memory(None)
     if literal.type == 'LIST':
         new_dir = gl.get_last_data()
         literal.constant_direction = memory.get_last_constant()
         memory.add_constant(new_dir, 'NUMBER')
         (size, _, _) = literal.array_info
         for _ in range(0, size):
             gl.add_memory(None)
     else:
         memory.add_assign(literal.virtual_direction, new_dir)
     return (variable_name, literal, new_dir)