Simplified branch jumps

This commit is contained in:
Eduard Urbach 2024-07-07 19:44:54 +02:00
parent 9f341a6146
commit 6aad74d6dd
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0

View File

@ -16,23 +16,44 @@ func (f *Function) CompileIf(branch *ast.If) error {
} }
endLabel := fmt.Sprintf("%s_end_if_%d", f.Name, f.count.branch) endLabel := fmt.Sprintf("%s_end_if_%d", f.Name, f.count.branch)
f.JumpIfFalse(branch.Condition.Token.Text(), endLabel)
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)
}
defer f.assembler.Label(asm.LABEL, endLabel) defer f.assembler.Label(asm.LABEL, endLabel)
f.count.branch++ f.count.branch++
return f.CompileAST(branch.Body) 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)
}
}