From 6aad74d6dd91c56282f0d96c769e9bc4d127d4cc Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 7 Jul 2024 19:44:54 +0200 Subject: [PATCH] Simplified branch jumps --- src/build/core/CompileIf.go | 53 ++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/src/build/core/CompileIf.go b/src/build/core/CompileIf.go index de43bec..0b9b584 100644 --- a/src/build/core/CompileIf.go +++ b/src/build/core/CompileIf.go @@ -16,23 +16,44 @@ func (f *Function) CompileIf(branch *ast.If) error { } endLabel := fmt.Sprintf("%s_end_if_%d", f.Name, f.count.branch) - - switch branch.Condition.Token.Text() { - case "==": - f.assembler.Label(asm.JNE, endLabel) - case "!=": - f.assembler.Label(asm.JE, endLabel) - case ">": - f.assembler.Label(asm.JLE, endLabel) - case "<": - f.assembler.Label(asm.JGE, endLabel) - case ">=": - f.assembler.Label(asm.JL, endLabel) - case "<=": - f.assembler.Label(asm.JG, endLabel) - } - + f.JumpIfFalse(branch.Condition.Token.Text(), endLabel) defer f.assembler.Label(asm.LABEL, endLabel) f.count.branch++ return f.CompileAST(branch.Body) } + +// JumpIfFalse jumps to the label if the previous comparison was false. +func (f *Function) JumpIfFalse(operator string, label string) { + switch operator { + case "==": + f.assembler.Label(asm.JNE, label) + case "!=": + f.assembler.Label(asm.JE, label) + case ">": + f.assembler.Label(asm.JLE, label) + case "<": + f.assembler.Label(asm.JGE, label) + case ">=": + f.assembler.Label(asm.JL, label) + case "<=": + f.assembler.Label(asm.JG, label) + } +} + +// JumpIfTrue jumps to the label if the previous comparison was true. +func (f *Function) JumpIfTrue(operator string, label string) { + switch operator { + case "==": + f.assembler.Label(asm.JE, label) + case "!=": + f.assembler.Label(asm.JNE, label) + case ">": + f.assembler.Label(asm.JG, label) + case "<": + f.assembler.Label(asm.JL, label) + case ">=": + f.assembler.Label(asm.JGE, label) + case "<=": + f.assembler.Label(asm.JLE, label) + } +}