expr/operand-var.go

42 lines
796 B
Go
Raw 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 {
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
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 {
info := ctx.GetFuncInfo(name)
if info != nil {
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)
}