def p_range(p): """range : ID DOUBLE_DOT ID | NUMCONST DOUBLE_DOT NUMCONST | arithmetic_expressions DOUBLE_DOT arithmetic_expressions""" if p.slice[1].type == "ID": symbol_table.check_variable_declaration(p[1], p.slice[1]) code_array.check_variable_is_not_array(p[1], p.slice[1]) p[0] = {"from": p[1], "to": p[3]} return
def p_statement_print(p): """statement : PRINT ID | PRINT ID LBRACK expressions RBRACK""" symbol_table.check_variable_declaration(p[2], p.slice[2]) if len(p) == 6: var_copy = code_array.setup_array_variable(p[2], p[4], p.slice[2]) code_array.emit("print", None, var_copy, None) else: code_array.check_variable_is_not_array(p[2], p.slice[2]) code_array.emit("print", None, p[2], None) return
def p_statement_assignment(p): """statement : ID ASSIGNMENT_SIGN expressions | ID LBRACK expressions RBRACK ASSIGNMENT_SIGN expressions""" symbol_table.check_variable_declaration(p[1], p.slice[1]) p[3] = code_array.store_boolean_expression_in_variable(p[3]) if len(p) == 4: code_array.check_variable_is_not_array(p[1], p.slice[1]) code_array.emit("=", p[1], p[3], None) else: p[6] = code_array.store_boolean_expression_in_variable(p[6]) var_copy = code_array.setup_array_variable(p[1], p[3], p.slice[1]) code_array.emit("=", var_copy, p[6], None) return
def p_statement_for(p): """statement : FOR ID ASSIGNMENT_SIGN counter DO qis_1 statement""" symbol_table.check_variable_declaration(p[2], p.slice[2]) code_array.check_variable_is_not_array(p[2], p.slice[2]) code_array.emit(p[4]["opt"], p[2], p[2], {"value": 1, "type": "int"}) code_array.emit("goto", None, code_array.get_next_quad_index() + 2, None) code_array.backpatch_e_list(p[6]["goto_quad_index"], code_array.get_next_quad_index()) code_array.emit("=", p[2], p[4]["from"], None) code_array.emit(">", None, p[2], p[4]["to"]) code_array.emit("goto", None, code_array.get_next_quad_index() + 2, None) code_array.emit("goto", None, p[6]["quad_index"], None) if p[7] and "exit_when_quad_index" in p[7]: code_array.backpatch_e_list(p[7]["exit_when_quad_index"], code_array.get_next_quad_index()) return
def p_expressions(p): """expressions : constant_expressions | bool_expressions | arithmetic_expressions | ID | ID LBRACK expressions RBRACK | LPAR expressions RPAR""" if p.slice[1].type == "ID": symbol_table.check_variable_declaration(p[1], p.slice[1]) if len(p) == 5 and p.slice[3].type == "expressions": p[0] = code_array.setup_array_variable(p[1], p[3], p.slice[1]) else: code_array.check_variable_is_not_array(p[1], p.slice[1]) p[0] = p[1] elif p.slice[1].type == "constant_expressions": p[0] = p[1] elif p.slice[1].type == "LPAR": p[0] = p[2] else: p[0] = p[1] return