from .utils import * LABEL_ASM = "({label})\n" GOTO_ASM = """@{label} 0;JMP """ IF_GOTO_ASM = """@SP AM=M-1 D=M @{label} D;JNE """ BRANCHING_ASM = { "label": LABEL_ASM, "goto": GOTO_ASM, "if-goto": IF_GOTO_ASM, } def translate_branching(action, label, prog, verbose=False): asm = f"// {action} {label}\n" if verbose else "" asm += BRANCHING_ASM[action].format(label=f"{prog}${label}") return asm