46 lines
928 B
Go
46 lines
928 B
Go
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// operand-var.go
|
|
package expr
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.portale-stac.it/go-pkg/expr/kern"
|
|
"git.portale-stac.it/go-pkg/expr/scan"
|
|
)
|
|
|
|
// -------- variable term
|
|
func newVarTerm(tk *scan.Token) *scan.Term {
|
|
t := &scan.Term{
|
|
Tk: *tk,
|
|
Parent: nil,
|
|
Children: nil,
|
|
Position: scan.PosLeaf,
|
|
Priority: scan.PriValue,
|
|
EvalFunc: evalVar,
|
|
}
|
|
t.Tk.Sym = scan.SymVariable
|
|
return t
|
|
}
|
|
|
|
// -------- eval func
|
|
func evalVar(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
|
var exists bool
|
|
name := opTerm.Source()
|
|
if v, exists = ctx.GetVar(name); !exists {
|
|
if info, exists := ctx.GetFuncInfo(name); exists {
|
|
v = info.Functor()
|
|
} else {
|
|
err = fmt.Errorf("undefined variable or function %q", name)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// init
|
|
func init() {
|
|
scan.RegisterTermConstructor(scan.SymIdentifier, newVarTerm)
|
|
}
|