41 lines
798 B
Go
41 lines
798 B
Go
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// operand-var.go
|
|
package expr
|
|
|
|
import "fmt"
|
|
|
|
// -------- variable term
|
|
func newVarTerm(tk *Token) *term {
|
|
return &term{
|
|
tk: *tk,
|
|
// class: classVar,
|
|
// kind: kindUnknown,
|
|
parent: nil,
|
|
children: nil,
|
|
position: posLeaf,
|
|
priority: priValue,
|
|
evalFunc: evalVar,
|
|
}
|
|
}
|
|
|
|
// -------- eval func
|
|
func evalVar(ctx ExprContext, self *term) (v any, err error) {
|
|
var exists bool
|
|
name := self.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() {
|
|
registerTermConstructor(SymIdentifier, newVarTerm)
|
|
}
|