def write_bool_jumps(jump_cond): eq_label_true = true_label() eq_label_false = false_label() add_line('@' + eq_label_true) add_line('D;' + jump_cond) add_line('@' + eq_label_false) add_line('0;JMP') add_symbol_link(eq_label_true) set_bool_to_stack(True) add_next_symbol() add_symbol_link(eq_label_false) set_bool_to_stack(False) symb_next = add_next_symbol() add_symbol_link(symb_next) increase_stack_pointer() global next_counter next_counter += 1 global equality_counter equality_counter += 1
def unconditional_jump(location): add_line('@' + location) add_line('0;JMP')
def write_to_stack(source): point_stack_current() add_line("M=" + source) increase_stack_pointer()
def read_stack_to(dest: str, discrease_pointer=True): if discrease_pointer: descrease_stack_pointer() point_stack_current() add_line(dest + '=M')
def point_top_stack_val(): point_sp() add_line('A=M')
def descrease_stack_pointer(): global stack_pointer stack_pointer -= 1 point_sp() add_line("M=M-1")
def increase_stack_pointer(): global stack_pointer stack_pointer += 1 point_sp() add_line("M=M+1")
def add_symbol_link(label): add_line(get_symbol_link(label))
def point_to_local_tmp(): add_line('@lcl_temp')
def point_sp(): add_line('@SP')
def point_to_base(base, location): add_line('@' + str(base + location))
def point_static(name, location): add_line('@' + name + '.' + location)
def write_const_to(const, dest): add_line('@' + const) write_c_command(dest, 'A')
def read_stack_and_write(dest, op): descrease_stack_pointer() point_stack_current() add_line(dest + '=' + op) if dest == "M": increase_stack_pointer()
def write_c_command(dest, source): add_line(dest + '=' + source)
def point_stack_current(): global stack_pointer point_sp() add_line('A=M')
def init_stack_pointer(): add_line('@' + str(stack_pointer)) add_line('D=A') point_sp() add_line('M=D')