// Copyright (c) 2024-2026 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"
	"git.portale-stac.it/go-pkg/expr/sym"
)

// -------- expr term
func newExprTerm(root *scan.Term) *scan.Term {
	tk := scan.NewValueToken(root.Tk.Row(), root.Tk.Col(), sym.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
}
