Improved branch compilation

This commit is contained in:
2024-07-07 21:55:32 +02:00
parent 6aad74d6dd
commit ee16774fe7
9 changed files with 160 additions and 113 deletions

View File

@ -0,0 +1,82 @@
package core
import (
"git.akyoto.dev/cli/q/src/build/asm"
"git.akyoto.dev/cli/q/src/build/expression"
)
// CompileCondition inserts code to jump to the start label or end label depending on the truth of the condition.
func (f *Function) CompileCondition(condition *expression.Expression, startLabel string, endLabel string) error {
switch condition.Token.Text() {
case "||":
left := condition.Children[0]
err := f.CompileCondition(left, startLabel, endLabel)
if err != nil {
return err
}
f.JumpIfTrue(left.Token.Text(), startLabel)
right := condition.Children[1]
err = f.CompileCondition(right, startLabel, endLabel)
if err != nil {
return err
}
if condition.Parent == nil {
f.JumpIfFalse(right.Token.Text(), endLabel)
} else {
f.JumpIfTrue(right.Token.Text(), startLabel)
}
return nil
case "&&":
return nil
default:
err := f.Compare(condition)
if condition.Parent == nil {
f.JumpIfFalse(condition.Token.Text(), endLabel)
}
return err
}
}
// 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)
}
}