Esempio n. 1
0
def p_snp_br_html_tag(p):
    """snp_br_html_tag : empty"""
    html_tag = p[-1].upper()
    # For debbuging
    # print("Html tag: ", html_tag)

    quad_helper.add_quad(token_to_code.get("eval"), -1, -1, token_to_code.get(html_tag))
Esempio n. 2
0
def p_snp_class_quad(p):
    """snp_class_quad : empty"""
    class_str = p[-1]
    cte_s = memory.get_or_set_addr_const(class_str, "str")
    quad_helper.add_quad(
        token_to_code.get("eval"), -1, cte_s, token_to_code.get("CLASS")
    )
Esempio n. 3
0
def exec_quad(quad):
    """
        Description: Evaluates a quadruple, taking into consideration its operation code (token).
            This function identifies the operation code and calls the appropiate actions to execute.
            This func is called/used in VM run_code()
        Params:
            quad (Quadruple): The quadruple that will be executed. (token, operand1, operand2, operand3)
        Return:
    """
    if quad.token in ARTITHMETIC:
        arithmetic(quad)
    elif quad.token in RELATIONAL:
        relational(quad)
    elif quad.token in LOGICAL:
        logical(quad)
    elif quad.token == token_to_code.get("eval"):
        eval(quad)
    elif quad.token in JUMPS:
        jumps(quad)
    elif quad.token == token_to_code.get("VER"):
        is_arr_out_of_bounds(quad)
    elif quad.token in MODULES:
        modules(quad)
    else:
        return
Esempio n. 4
0
def p_snp_href_quad(p):
    """snp_href_quad : empty"""
    href_str = p[-1]
    cte_s = memory.get_or_set_addr_const(href_str, "str")
    quad_helper.add_quad(
        token_to_code.get("eval"), -1, cte_s, token_to_code.get("HREF")
    )
Esempio n. 5
0
def p_snp_check_precedence_and_create_quadruple_for_sign(p):
    """snp_check_precedence_and_create_quadruple_for_sign : empty"""
    top = quad_helper.top_token()
    add = token_to_code.get("+")
    sub = token_to_code.get("-")
    is_sign = (top is add) or (top is sub)
    if is_sign:
        add_quadruple_expression()
Esempio n. 6
0
def p_snp_close_html_tag(p):
    """snp_close_html_tag : empty"""
    html_tag = quad_helper.pop_tag()
    closing_tag = html_tag + 1
    # Checks that the respective html_tag has a closing tag
    # for example: img doesn't have one, but h1 yes (<h1></h1>)
    if html_tag is not token_to_code.get("IMG"):
        quad_helper.add_quad(token_to_code.get("eval"), -1, -1, closing_tag)
Esempio n. 7
0
def p_snp_add_gosub(p):
    """snp_add_gosub : empty"""
    # Get the last call from the stack and its queue_params
    module_name = parser_helper.stack_calls.top()
    module_queue_params = parser_helper.get_queue_params(module_name)
    param_pointer = parser_helper.stack_param_pointers.top()
    # snp #5 Module Call
    # Verify that last parameter points to null (coherence in number of params)
    if module_queue_params is not None:
        # Clear the stack and pointer after call ends
        module_name = parser_helper.stack_calls.pop()
        parser_helper.stack_param_pointers.pop()
        parser_helper.stack_param_pointers.push(0)
        if param_pointer is len(module_queue_params):
            # Clear the stack and pointer after call ends
            module_quad_num = parser_helper.get_starting_quad(module_name)
            # Generate a quad with GOSUB, next_quad after this call, -1, quad where VM needs to jump
            quad_helper.add_quad(
                token_to_code.get("GOSUB"),
                quad_helper.quad_cont + 1,
                -1,
                module_quad_num,
            )
            # Assign return temporal to module
            module_type = parser_helper.get_module_type(module_name)
            # print("THE MODULE TYPE ", module_type)
            if module_type is not "void":
                # get the memory_address for the module variable
                if parser_helper.is_var_declared(module_name):
                    module_address = parser_helper.get_var_address_from_dir(module_name)
                    # get the memory_address for the return variable
                    temp_memory_address = memory.set_addr_temp(module_type)
                    # add the quad
                    quad_helper.add_quad(
                        token_to_code.get("="), module_address, -1, temp_memory_address
                    )
                    # For debbuging
                    # print(
                    #     "added quadd: ",
                    #     token_to_code.get("="),
                    #     module_address,
                    #     -1,
                    #     temp_memory_address,
                    # )
                    # expression
                    quad_helper.push_operand(temp_memory_address)
                    # add the result type (temp var) to the type stack
                    quad_helper.push_type(module_type)
                else:
                    print(
                        "HEL:LWDHkjSHD"
                    )  # TODO: lanzar un error de var/module not defined?
                    exit(1)
        else:
            error_helper.add_error(
                304,
                f"This function was expecting {len(module_queue_params)} params, but received {param_pointer}",
            )
Esempio n. 8
0
def p_snp_check_precedence_and_create_quadruple_for_logic(p):
    """snp_check_precedence_and_create_quadruple_for_logic : empty"""
    top = quad_helper.top_token()
    and_op = token_to_code.get("and")
    or_op = token_to_code.get("or")

    is_logic = (top is and_op) or (top is or_op)

    if is_logic:
        add_quadruple_expression()
Esempio n. 9
0
def p_snp_check_precedence_and_create_quadruple_for_op(p):
    """snp_check_precedence_and_create_quadruple_for_op : empty"""

    top = quad_helper.top_token()
    division = token_to_code.get("/")
    product = token_to_code.get("*")
    is_op = (top is division) or (top is product)

    if is_op:
        add_quadruple_expression()
Esempio n. 10
0
def modules(quad):
    global instruction_pointer
    if quad.token == token_to_code.get("ERA"):  # Activation Record
        # ERA, func_name,  -1,  -1
        # Start a Local Memory Context
        curr_l_memory = RuntimeMemory(scope_to_code.get("local"))
        # Push to Call Stack
        call_context_stack.push(curr_l_memory)
    elif quad.token == token_to_code.get("PARAMETER"):  #Set param value
        # PARAMETER, addr_value_being_sent, -1, param_addr
        # Get value being sent from address (param value)
        param_value = get_value_from_address(quad.operand1)
        param_addr = quad.operand3
        # Set the param value to the corresponding address in the most recent call
        call_context_stack.top().set_value(param_value, param_addr)
    elif quad.token == token_to_code.get("GOSUB"):  #Go to subroutine
        # Remove from call stack and push to memory_context_stack
        curr_l_memory = call_context_stack.pop()
        memory_context_stack.push(curr_l_memory)
        # GOSUB, next_quad, -1, destination
        memory_context_stack.top().return_quad = quad.operand1
        # print("return to: ", memory_context_stack.top().return_quad)
        instruction_pointer = quad.operand3 - 1
    elif quad.token == token_to_code.get("ENDPROC"):  #Void module ends
        # The function ends, IP must return to where call was made
        # ENDPROC will only appear in void functions

        # Get the return quad number from the curr context
        instruction_pointer = memory_context_stack.top().return_quad - 1
        # Pop the module context from the stack
        memory_context_stack.pop()

    elif quad.token == token_to_code.get("RET"):  # Return module ends
        # RET, return_val , -1, -1
        # The function ends, IP must return to where call was made
        # RET will appear only un returning functions

        # Get the return value addr
        return_value = get_value_from_address(quad.operand1)
        # Get the return quad number from the curr context
        instruction_pointer = memory_context_stack.top().return_quad
        # Pop the module context from the stack
        memory_context_stack.pop()
        # Relocate IP and get call assignation quad from the quads
        quad = queue_quad[instruction_pointer]
        # Assign the resut to the func_var_addr
        # =, func_var_addr, -1, temp
        set_value_to_address(return_value, quad.operand1)
        arithmetic(
            quad
        )  # Vm will move unto whatever quad comes after the assignment of func call var to a temp
Esempio n. 11
0
def arithmetic(quad):
    if quad.token == token_to_code.get("+"):  # Addition
        # +, left_op, right_op, result
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # Execute addition
        res_val = left_op + right_op
        # print(f"Added: {left_op} + {right_op}")
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
    elif quad.token == token_to_code.get("-"):  # Substraction
        # -, left_op, right_op, result
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # Execute substraction
        res_val = left_op - right_op
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
    elif quad.token == token_to_code.get("*"):  # Multiplication
        # *, left_op, right_op, result
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # Execute multiplication
        res_val = left_op * right_op
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
    elif quad.token == token_to_code.get("/"):  # Division
        # /, left_op, right_op, result
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # Execute division
        if right_op != 0:
            res_val = left_op / right_op
        else:
            print("YOU ARE DIVIDING BY 0"
                  )  # TODO : add nice error message (zero_division: 401)
            exit(1)
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
    else:  # Assignment '='
        # =, value, -1, variable
        # Get value from memory
        value = get_value_from_address(quad.operand1)
        # Assign by storing value in memory
        set_value_to_address(value, quad.operand3)
Esempio n. 12
0
def p_snp_init_slice_1d(p):
    """snp_init_slice_1d : empty"""
    # print("curr slice", parser_helper.curr_slice)
    slice_name = parser_helper.curr_slice
    slice_type = parser_helper.get_var_type_from_dir(slice_name)

    if slice_type in type_to_code:
        upper_limit = int(parser_helper.get_upper_limit(slice_name, 1))

        counter = 0
        default_value = type_to_init_value.get(slice_type)
        default_initial_value_address = memory.get_or_set_addr_const(
            default_value, slice_type
        )
        operand_address = parser_helper.get_var_address_from_dir(slice_name)

        while upper_limit > counter:
            quad_helper.add_quad(
                token_to_code.get("="),
                default_initial_value_address,
                -1,
                operand_address,
            )
            # for debugging
            # print("=", default_initial_value_address, -1, operand_address)
            counter += 1
            operand_address += 1

    else:
        print("Error: Invalid type")
Esempio n. 13
0
def p_snp_add_module(p):
    """snp_add_module : empty"""
    # global parser_helper.procedure_directory, parser_helper.curr_scope
    module_name = p[-1]  # get the last symbol read (left from this neural point)
    # Check if module already exists and add it to the directory
    # debbuging
    # print("TABLEE! parser_helper.procedure_directory", parser_helper.procedure_directory, "module to add:", module_name, "\n")
    if module_name in parser_helper.procedure_directory:
        error_message = f"Module {module_name} has already been declared"
        error_helper.add_error(0, error_message)
    else:
        parser_helper.procedure_directory[module_name] = {
            "type": None,
            "scope_type": scope_to_code.get("local"),
            "params_count": 0,
            "queue_params": [],
            "queue_params_addresses": [],
            "starting_quad": -1,
            "var_table": {},
            "dim_list": {},
        }  # TODO : add more info later on
        parser_helper.curr_scope = module_name
        memory.curr_scope_type = scope_to_code.get("local")
        quad_helper.add_quad(token_to_code.get("GOTO"), -1, -1, "pending_endproc")
        quad_helper.push_jump(quad_helper.quad_cont - 1)
Esempio n. 14
0
def p_snp_while_3(p):
    """snp_while_3 : empty"""
    end = quad_helper.pop_jump()
    ret = quad_helper.pop_jump()
    quad_helper.add_quad(token_to_code.get("GOTO"), -1, -1, ret)
    count = quad_helper.quad_cont
    quad_helper.fill(end, count)
Esempio n. 15
0
def eval(quad):
    global instruction_pointer
    if quad.operand3 == token_to_code.get("CLASS"):
        # eval, -1, class_name, CLASS
        class_name = get_value_from_address(quad.operand2)
        instruction_pointer += 1
        quad = queue_quad[instruction_pointer]

        head_pretty = """
        <!-- Compiled and minified CSS -->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
        <!--Let browser know website is optimized for mobile-->
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <title> Trendlit - Cloud Based Programming Language</title>
        """

        if (quad.operand3 is token_to_code.get("HEAD")) and (
                class_name.lower() == "pretty"):
            value = "<" + code_to_token.get(
                quad.operand3).lower() + f"> \n {head_pretty}"
        else:
            value = "<" + code_to_token.get(
                quad.operand3).lower() + f" class=\"{class_name}\">"

    elif quad.operand3 == token_to_code.get("HREF"):
        # eval, -1, href_name, HREF
        href_name = get_value_from_address(quad.operand2)
        instruction_pointer += 1
        quad = queue_quad[instruction_pointer]
        value = f"<a href=\"{href_name}\">"
    elif quad.operand3 == token_to_code.get("SRC"):
        # eval, -1, src_name, HREF
        src_name = get_value_from_address(quad.operand2)
        instruction_pointer += 1
        quad = queue_quad[instruction_pointer]
        value = f"<img src=\"{src_name}\">"
    elif quad.operand3 >= 600 and quad.operand3 <= 699:  # html tag
        # eval, -1, -1, TAG
        value = "<" + code_to_token.get(quad.operand3).lower() + ">"
    else:
        # eval, -1, -1, 16000
        value = get_value_from_address(quad.operand3)
        # print(f"VLUE TO PRINT: {value}, ADDRE: {quad.operand3}")
    vmh.queue_results.append(str(value))
Esempio n. 16
0
def add_ret_endproc_quad():
    curr_module_type = parser_helper.procedure_directory[parser_helper.curr_scope][
        "type"
    ]
    if curr_module_type is "void":  # VOID MODULE
        # Delete var table for the module that ended
        quad_helper.add_quad(token_to_code.get("ENDPROC"), -1, -1, -1)
    else:  # RETURNING MODULE
        return_value = quad_helper.pop_operand()
        return_type = code_to_type.get(quad_helper.pop_type())
        if return_type != curr_module_type:
            print(
                f"return_type: {return_type}, return_value: {return_value}, curr_module_type: {curr_module_type}"
            )
            error_helper.add_error(301, f"Return type is a mismatch")
        else:
            quad_helper.add_quad(
                token_to_code.get("RET"), return_value, -1, "memory address"
            )
Esempio n. 17
0
def p_snp_conditional_statement_3(p):
    """snp_conditional_statement_3 : empty"""
    quad_helper.add_quad(token_to_code.get("GOTO"), -1, -1, "pending")
    false = quad_helper.pop_jump()
    count = quad_helper.quad_cont
    quad_helper.push_jump(count - 1)
    # debbuging
    # print(count-1, false)
    # print(quad_helper.top_jump())
    quad_helper.fill(false, count)
Esempio n. 18
0
def logical(quad):
    if quad.token == token_to_code.get("and"):
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # Execute comparison &&
        res_val = left_op and right_op
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
    elif quad.token == token_to_code.get("or"):
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # Execute comparison ||
        res_val = left_op or right_op
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
    else:
        print("Invalid logical operator.")
        sys.exit(1)
Esempio n. 19
0
def jumps(quad):
    global instruction_pointer
    if quad.token == token_to_code.get("GOTO"):
        # GOTO, -1, -1, destination
        # Change inst pointer to point to destination quad
        instruction_pointer = quad.operand3 - 1
    elif quad.token == token_to_code.get("GOTOF"):
        # GOTOF, trigger, -1, destination
        # Get trigger (result of condition)
        trigger = get_value_from_address(quad.operand1)
        # print("TRIGGER ", trigger)
        # Change inst = destination quad IF trigger is FALSE
        if not trigger:
            instruction_pointer = quad.operand3 - 1
    elif quad.token == token_to_code.get("GOTOT"):
        # GOTOT, trigger, -1, destination
        # Get trigger (result of condition)
        trigger = get_value_from_address(quad.operand1)
        # print("TRIGGER ", trigger)
        # Change inst = destination quad IF trigger is TRUE
        if trigger:
            instruction_pointer = quad.operand3 - 1
Esempio n. 20
0
def p_snp_end_module(p):
    """snp_end_module : empty"""
    # add_ret_endproc_quad()
    curr_module_type = parser_helper.procedure_directory[parser_helper.curr_scope][
        "type"
    ]
    if curr_module_type is "void":  # VOID MODULE
        # Delete var table for the module that ended
        quad_helper.add_quad(token_to_code.get("ENDPROC"), -1, -1, -1)
    reset_local_contex()
    end = quad_helper.pop_jump()
    cont = quad_helper.quad_cont
    quad_helper.fill(end, cont)
Esempio n. 21
0
def p_snp_do_while_gotot(p):
    """snp_do_while_gotot : empty"""
    top_type_code = quad_helper.pop_type()
    result = quad_helper.pop_operand()
    ret = quad_helper.pop_jump()

    # Check if top_oper's type is type bool(1)
    if code_to_type.get(top_type_code) is "bool":
        quad_helper.add_quad(token_to_code.get("GOTOT"), result, -1, ret)
    else:
        error_helper.add_error(
            0, "Type Missmatch: Expression is not a bool"
        )  # TODO define code and custom error message
Esempio n. 22
0
def p_snp_slice_access_3(p):
    """ snp_slice_access_3 : empty """
    s = quad_helper.pop_operand()  # No se saca

    slice_name = parser_helper.curr_slice
    slice_type = parser_helper.get_var_type_from_dir(slice_name)
    # print(f"slice_name: {slice_name}, dim: {parser_helper.curr_dimension_counter}")

    # Add VER quad
    lower_limit = 0
    upper_limit = parser_helper.get_upper_limit(
        slice_name, parser_helper.curr_dimension_counter
    )

    lower_limit_addr = memory.get_or_set_addr_const("0", "int")
    upper_limit_addr = memory.get_or_set_addr_const(upper_limit, "int")
    # print(f"lower_limit: {lower_limit}, upper_limit: {upper_limit}")
    quad_helper.add_quad(
        token_to_code.get("VER"), lower_limit_addr, upper_limit_addr, s
    )

    # Add base address to quad : +dirB(slice)
    base_dir = parser_helper.get_var_address_from_dir(slice_name)
    # assign memory address to temporary result variable and increase counter for temp/result vars
    temp_memory_address = memory.set_addr_temp("int")  # should always be an int
    # add the quad
    # assigns a constant to the base_dir so that VM can read it properly
    base_dir_addr = memory.get_or_set_addr_const(str(base_dir), "int")
    quad_helper.add_quad(token_to_code.get("+"), s, base_dir_addr, temp_memory_address)

    # Agregar (dircasilla) [pointer like address]
    # Push a la pila con la (dircasilla) para que el siguiente cuadruplo la use
    ptr_addr_cell = memory.set_addr_ptr(temp_memory_address)
    # For debugging
    # print(
    #     f"curr scope: {code_to_scope.get(parser_helper.get_scope_type(parser_helper.curr_scope))} addr_ptr: {ptr_addr_cell}"
    # )
    quad_helper.push_operand(ptr_addr_cell)
Esempio n. 23
0
def p_snp_conditional_statement_1(p):
    """snp_conditional_statement_1 : empty"""
    top_type_code = quad_helper.pop_type()
    result = quad_helper.pop_operand()

    # Check if top_oper's type is type bool(1)
    if code_to_type.get(top_type_code) is "bool":
        # debbuging
        quad_helper.add_quad(token_to_code.get("GOTOF"), result, -1, "pending")
        quad_helper.push_jump(quad_helper.quad_cont - 1)
        # debbuging
        # print("Jump", quad_helper.quad_cont)
    else:
        error_helper.add_error(
            0, "Type Missmatch: Expression is not a bool"
        )  # TODO define code and custom error message
Esempio n. 24
0
def relational(quad):
    if quad.token == token_to_code.get("is"):
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # Execute comparison ==
        res_val = left_op == right_op
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
    elif quad.token == token_to_code.get("not"):
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # Execute comparison !=
        res_val = left_op != right_op
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
    elif quad.token == token_to_code.get(">="):
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # Execute comparison >=
        res_val = left_op >= right_op
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
    elif quad.token == token_to_code.get("<="):
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # Execute comparison <=
        res_val = left_op <= right_op
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
    elif quad.token == token_to_code.get(">"):
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # Execute comparison >
        res_val = left_op > right_op
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
    elif quad.token == token_to_code.get("<"):  # '<'
        # <, left_op, right_op, result
        # Get value from memory
        left_op = get_value_from_address(quad.operand1)
        right_op = get_value_from_address(quad.operand2)
        # For debbuging
        # print(f"left_op {left_op} right_op {right_op}")
        # Execute comparison <
        res_val = left_op < right_op
        # Save result in memory
        set_value_to_address(res_val, quad.operand3)
Esempio n. 25
0
def p_snp_push_solitary_operand(p):
    """snp_push_solitary_operand : empty"""

    """
    Variables declared without a corresponding initialization are zero-valued.
    For example, the zero value for an int is 0
    """
    operand_id = p[-2]
    operand_address = parser_helper.get_var_address_from_dir(operand_id)
    type = parser_helper.procedure_directory[parser_helper.curr_scope]["var_table"][
        operand_id
    ]["type"]
    default_initial_value = type_to_init_value.get(type)
    default_initial_value_address = memory.get_or_set_addr_const(
        default_initial_value, type
    )
    operator = token_to_code.get("=")

    quad_helper.add_quad(operator, default_initial_value_address, -1, operand_address)
Esempio n. 26
0
def p_snp_check_param(p):
    """snp_check_param : empty"""

    # # Get the last read parameter from the call
    input_param = quad_helper.pop_operand()
    input_param_type = quad_helper.pop_type()

    # parser_helper.stack_param_calls([])
    # top_queue = parser_helper.stack_param_calls.top()
    # top_queue.append(input_param_type)

    # Get the last call from the stack and its queue_params
    module_name = parser_helper.stack_calls.top()
    module_queue_params = parser_helper.get_queue_params(module_name)
    module_queue_params_addresses = parser_helper.get_queue_params_addresses(
        module_name
    )
    pointer = parser_helper.stack_param_pointers.top()

    # Check if param matches the type/order
    is_not_none = module_queue_params is not None
    if (
        is_not_none
        and pointer <= len(module_queue_params) - 1
        and input_param_type is module_queue_params[pointer]
    ):
        param_assigned_addr = module_queue_params_addresses[pointer]
        # print("MODULE PARAM ADDRES: ", module_queue_params_addresses)
        quad_helper.add_quad(
            token_to_code.get("PARAMETER"), input_param, -1, param_assigned_addr
        )  # assignation
    else:
        error_helper.add_error(301, "Params do not match type")
        # For debbuging
        # print("Expected: ",module_queue_params[pointer], " Received: ", input_param_type)
    # snp #4 Module Call - Increase pointer after check
    # Move to the next parameter (k++)
    param_pointer = parser_helper.stack_param_pointers.pop()
    parser_helper.stack_param_pointers.push(param_pointer + 1)
Esempio n. 27
0
def add_quadruple_expression():
    right_operand = quad_helper.pop_operand()  # TODO: type int
    right_operand_type = quad_helper.pop_type()
    # TODO: TESTTTT
    left_operand = quad_helper.pop_operand()  # TODO: type str
    left_operand_type = quad_helper.pop_type()
    false_bottom = token_to_code.get("(")
    # debbuging
    # print("quad_helper.top_token()", quad_helper.top_token())
    if quad_helper.top_token() is false_bottom:
        print("false bottom:", quad_helper.top_token())
        quad_helper.pop_token()
        token = quad_helper.pop_token()
    else:
        token = quad_helper.pop_token()
    # token = quad_helper.pop_token()

    # debbuging HERE
    # print("SMEMANTIC CUBE: ", right_operand, left_operand, token)
    # print("TYPES: ", right_operand_type, left_operand_type)
    if semantic_cube.is_in_cube(right_operand_type, left_operand_type, token):  # baila?
        # add the result (temp var) to the operand stack
        result_type = semantic_cube.cube[right_operand_type, left_operand_type, token]
        # assign memory address to temporary result variable and increase counter for temp/result vars
        temp_memory_address = memory.set_addr_temp(code_to_type.get(result_type))
        # add the quad
        quad_helper.add_quad(token, left_operand, right_operand, temp_memory_address)
        # expression
        quad_helper.push_operand(temp_memory_address)
        # add the result type (temp var) to the type stack
        quad_helper.push_type(code_to_type.get(result_type))
        # For debbuging
        # print("OPERAND", quad_helper.top_operand())
        # print("TYPE", quad_helper.top_type())
    else:
        error_helper.add_error(301)
Esempio n. 28
0
def p_snp_check_precedence_and_create_quadruple_for_rel(p):
    """snp_check_precedence_and_create_quadruple_for_rel : empty"""

    top = quad_helper.top_token()
    eq = token_to_code.get("is")
    noteq = token_to_code.get("not")
    get = token_to_code.get(">=")
    let = token_to_code.get("<=")
    gt = token_to_code.get(">")
    lt = token_to_code.get("<")

    is_rel = (
        (top is eq)
        or (top is noteq)
        or (top is get)
        or (top is let)
        or (top is gt)
        or (top is lt)
    )

    if is_rel:
        add_quadruple_expression()
Esempio n. 29
0
def p_snp_img_quad(p):
    """snp_img_quad : empty"""
    img_str = p[-1]
    cte_s = memory.get_or_set_addr_const(img_str, "str")
    quad_helper.add_quad(token_to_code.get("eval"), -1, cte_s, token_to_code.get("SRC"))
Esempio n. 30
0
import sys
from semantic_cube.semantic_cube_helper import (
    token_to_code,
    scope_to_code,
    code_to_token,
)
from runtime_memory.runtime_memory import RuntimeMemory
from stack.stack import Stack
from virtual_machine.virtual_machine_helper import VMH

vmh = VMH()

ARTITHMETIC = [
    token_to_code.get("+"),
    token_to_code.get("-"),
    token_to_code.get("*"),
    token_to_code.get("/"),
    token_to_code.get("="),
]
RELATIONAL = [
    token_to_code.get("is"),
    token_to_code.get("not"),
    token_to_code.get(">="),
    token_to_code.get("<="),
    token_to_code.get(">"),
    token_to_code.get("<"),
]
LOGICAL = [
    token_to_code.get("and"),
    token_to_code.get("or"),
]