36 lines
811 B
Go
36 lines
811 B
Go
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// operand-expr.go
|
|
package expr
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.portale-stac.it/go-pkg/expr/kern"
|
|
"git.portale-stac.it/go-pkg/expr/scan"
|
|
)
|
|
|
|
// -------- expr term
|
|
func newExprTerm(root *scan.Term) *scan.Term {
|
|
tk := scan.NewValueToken(root.Tk.Row(), root.Tk.Col(), scan.SymExpression, root.Source(), root)
|
|
return &scan.Term{
|
|
Tk: *tk,
|
|
Parent: nil,
|
|
Children: nil,
|
|
Position: scan.PosLeaf,
|
|
Priority: scan.PriValue,
|
|
EvalFunc: evalExpr,
|
|
}
|
|
}
|
|
|
|
// -------- eval expr
|
|
func evalExpr(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
|
if ast, ok := opTerm.Value().(*scan.Term); ok {
|
|
v, err = ast.Compute(ctx)
|
|
} else {
|
|
err = fmt.Errorf("expression expected, got %T", opTerm.Value())
|
|
}
|
|
return
|
|
}
|