from .utils import * # template for lt, gt, and eq COMPARE_ASM = """@SP AM=M-1 D=M A=A-1 D=M-D M=0 @END_{tag} D;{jmp} @SP A=M-1 M=-1 (END_{tag}) """ # exact complement of VM command which leaves top of stack false JMP = { "lt": "JGE", "eq": "JNE", "gt": "JLE", } tag_idx = 0 def translate_compare(command, verbose=False): global tag_idx asm = f"// {command}\n" if verbose else "" asm += COMPARE_ASM.format(tag=f"{command.upper()}_{tag_idx}", jmp=JMP[command]) tag_idx += 1 return asm