47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
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 := f.Variable(name)
|
|
|
|
if variable == nil {
|
|
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.Scope().MustFindFree(f.cpu.General)
|
|
err := f.ExpressionToRegister(left, tmp)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer f.Scope().Free(tmp)
|
|
return f.Execute(comparison.Token, tmp, right)
|
|
}
|