Implemented boolean operators

This commit is contained in:
2024-07-08 13:38:44 +02:00
parent ee16774fe7
commit b5bb291e47
9 changed files with 198 additions and 24 deletions

View File

@ -1,44 +1,74 @@
package core
import (
"fmt"
"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 {
func (f *Function) CompileCondition(condition *expression.Expression, successLabel string, failLabel string) error {
switch condition.Token.Text() {
case "||":
leftFailLabel := fmt.Sprintf("%s_false_%d", f.Name, f.count.subBranch)
f.count.subBranch++
// Left
left := condition.Children[0]
err := f.CompileCondition(left, startLabel, endLabel)
err := f.CompileCondition(left, successLabel, leftFailLabel)
if err != nil {
return err
}
f.JumpIfTrue(left.Token.Text(), startLabel)
f.JumpIfTrue(left.Token.Text(), successLabel)
// Right
f.assembler.Label(asm.LABEL, leftFailLabel)
right := condition.Children[1]
err = f.CompileCondition(right, startLabel, endLabel)
err = f.CompileCondition(right, successLabel, failLabel)
if condition.Parent != nil && condition.Parent.Token.Text() == "||" && condition != condition.Parent.LastChild() {
f.JumpIfTrue(right.Token.Text(), successLabel)
} else {
f.JumpIfFalse(right.Token.Text(), failLabel)
}
return err
case "&&":
leftSuccessLabel := fmt.Sprintf("%s_true_%d", f.Name, f.count.subBranch)
f.count.subBranch++
// Left
left := condition.Children[0]
err := f.CompileCondition(left, leftSuccessLabel, failLabel)
if err != nil {
return err
}
if condition.Parent == nil {
f.JumpIfFalse(right.Token.Text(), endLabel)
f.JumpIfFalse(left.Token.Text(), failLabel)
// Right
f.assembler.Label(asm.LABEL, leftSuccessLabel)
right := condition.Children[1]
err = f.CompileCondition(right, successLabel, failLabel)
if condition.Parent != nil && condition.Parent.Token.Text() == "||" && condition != condition.Parent.LastChild() {
f.JumpIfTrue(right.Token.Text(), successLabel)
} else {
f.JumpIfTrue(right.Token.Text(), startLabel)
f.JumpIfFalse(right.Token.Text(), failLabel)
}
return nil
case "&&":
return nil
return err
default:
err := f.Compare(condition)
if condition.Parent == nil {
f.JumpIfFalse(condition.Token.Text(), endLabel)
f.JumpIfFalse(condition.Token.Text(), failLabel)
}
return err