Improved branch compilation
This commit is contained in:
47
src/build/core/Compare.go
Normal file
47
src/build/core/Compare.go
Normal file
@ -0,0 +1,47 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"git.akyoto.dev/cli/q/src/build/ast"
|
||||
"git.akyoto.dev/cli/q/src/build/errors"
|
||||
"git.akyoto.dev/cli/q/src/build/expression"
|
||||
"git.akyoto.dev/cli/q/src/build/token"
|
||||
)
|
||||
|
||||
// Compare evaluates a boolean expression.
|
||||
func (f *Function) Compare(comparison *expression.Expression) error {
|
||||
left := comparison.Children[0]
|
||||
right := comparison.Children[1]
|
||||
|
||||
if left.IsLeaf() && left.Token.Kind == token.Identifier {
|
||||
name := left.Token.Text()
|
||||
variable, exists := f.variables[name]
|
||||
|
||||
if !exists {
|
||||
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, left.Token.Position)
|
||||
}
|
||||
|
||||
defer f.useVariable(variable)
|
||||
return f.Execute(comparison.Token, variable.Register, right)
|
||||
}
|
||||
|
||||
if ast.IsFunctionCall(left) && right.IsLeaf() {
|
||||
err := f.CompileCall(left)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return f.Execute(comparison.Token, f.cpu.Output[0], right)
|
||||
}
|
||||
|
||||
tmp := f.cpu.MustFindFree(f.cpu.General)
|
||||
err := f.ExpressionToRegister(left, tmp)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.cpu.Use(tmp)
|
||||
defer f.cpu.Free(tmp)
|
||||
return f.Execute(comparison.Token, tmp, right)
|
||||
}
|
Reference in New Issue
Block a user