40 lines
1012 B
Go
40 lines
1012 B
Go
package core
|
|
|
|
import (
|
|
"git.urbach.dev/cli/q/src/ast"
|
|
"git.urbach.dev/cli/q/src/errors"
|
|
"git.urbach.dev/cli/q/src/expression"
|
|
"git.urbach.dev/cli/q/src/token"
|
|
)
|
|
|
|
// Evaluate evaluates an expression and returns a register that contains the value of the expression.
|
|
func (f *Function) Evaluate(expr *expression.Expression) (Value, bool, error) {
|
|
if expr.Token.Kind == token.Identifier {
|
|
name := expr.Token.Text(f.File.Bytes)
|
|
variable := f.VariableByName(name)
|
|
|
|
if variable == nil {
|
|
return Value{}, false, errors.New(&errors.UnknownIdentifier{Name: name}, f.File, expr.Token.Position)
|
|
}
|
|
|
|
if variable.Alive == 1 {
|
|
f.UseVariable(variable)
|
|
return Value{variable.Type, variable.Register}, false, nil
|
|
}
|
|
}
|
|
|
|
if ast.IsFunctionCall(expr) {
|
|
types, err := f.CompileCall(expr)
|
|
|
|
if err != nil {
|
|
return Value{}, false, err
|
|
}
|
|
|
|
return Value{types[0], f.CPU.Output[0]}, false, nil
|
|
}
|
|
|
|
tmp := f.NewRegister()
|
|
typ, err := f.ExpressionToRegister(expr, tmp)
|
|
return Value{typ, tmp}, true, err
|
|
}
|