expr/operand-var.go

41 lines
789 B
Go
Raw Permalink Normal View History

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 {
t := &term{
tk: *tk,
2024-03-26 07:00:53 +01:00
parent: nil,
children: nil,
position: posLeaf,
priority: priValue,
evalFunc: evalVar,
}
t.tk.Sym = SymVariable
return t
2024-03-26 07:00:53 +01:00
}
// -------- eval func
2024-07-09 07:50:06 +02:00
func evalVar(ctx ExprContext, opTerm *term) (v any, err error) {
2024-03-26 07:00:53 +01:00
var exists bool
2024-07-09 07:50:06 +02:00
name := opTerm.source()
if v, exists = GetVar(ctx, name); !exists {
if info, exists := GetFuncInfo(ctx, 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)
}