2024-03-26 08:45:18 +01:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
2024-03-26 07:00:53 +01:00
|
|
|
// operand-var.go
|
|
|
|
package expr
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// -------- variable term
|
|
|
|
func newVarTerm(tk *Token) *term {
|
|
|
|
return &term{
|
2024-04-09 05:32:50 +02:00
|
|
|
tk: *tk,
|
|
|
|
// class: classVar,
|
|
|
|
// kind: kindUnknown,
|
2024-03-26 07:00:53 +01:00
|
|
|
parent: nil,
|
|
|
|
children: nil,
|
|
|
|
position: posLeaf,
|
|
|
|
priority: priValue,
|
|
|
|
evalFunc: evalVar,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------- eval func
|
2024-04-08 23:17:56 +02:00
|
|
|
func evalVar(ctx ExprContext, self *term) (v any, err error) {
|
2024-03-26 07:00:53 +01:00
|
|
|
var exists bool
|
2024-04-21 14:24:56 +02:00
|
|
|
name := self.source()
|
|
|
|
if v, exists = ctx.GetVar(name); !exists {
|
2024-04-26 04:36:03 +02:00
|
|
|
if info, exists := ctx.GetFuncInfo(name); exists {
|
2024-04-21 14:24:56 +02:00
|
|
|
v = info.Functor()
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("undefined variable or function %q", name)
|
|
|
|
}
|
2024-03-26 07:00:53 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// init
|
|
|
|
func init() {
|
|
|
|
registerTermConstructor(SymIdentifier, newVarTerm)
|
|
|
|
}
|