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{
|
|
|
|
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
|
|
|
|
if v, exists = ctx.GetValue(self.tk.source); !exists {
|
|
|
|
err = fmt.Errorf("undefined variable %q", self.tk.source)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// init
|
|
|
|
func init() {
|
|
|
|
registerTermConstructor(SymIdentifier, newVarTerm)
|
|
|
|
}
|