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

View File

@ -9,16 +9,16 @@ import (
// CompileIf compiles a branch instruction.
func (f *Function) CompileIf(branch *ast.If) error {
startLabel := fmt.Sprintf("%s_if_start_%d", f.Name, f.count.branch)
endLabel := fmt.Sprintf("%s_if_end_%d", f.Name, f.count.branch)
err := f.CompileCondition(branch.Condition, startLabel, endLabel)
success := fmt.Sprintf("%s_if_%d_true", f.Name, f.count.branch)
fail := fmt.Sprintf("%s_if_%d_false", f.Name, f.count.branch)
err := f.CompileCondition(branch.Condition, success, fail)
if err != nil {
return err
}
f.assembler.Label(asm.LABEL, startLabel)
defer f.assembler.Label(asm.LABEL, endLabel)
f.assembler.Label(asm.LABEL, success)
defer f.assembler.Label(asm.LABEL, fail)
f.count.branch++
return f.CompileAST(branch.Body)
}

View File

@ -21,8 +21,9 @@ type state struct {
// counter stores how often a certain statement appeared so we can generate a unique label from it.
type counter struct {
loop int
branch int
loop int
branch int
subBranch int
}
// PrintInstructions shows the assembly instructions.