ucl/cmdlang/env.go

41 lines
692 B
Go
Raw Normal View History

2024-04-10 10:45:58 +00:00
package cmdlang
import (
"errors"
)
type evalCtx struct {
parent *evalCtx
currentStream stream
commands map[string]invokable
}
func (ec *evalCtx) withCurrentStream(s stream) *evalCtx {
return &evalCtx{
parent: ec,
currentStream: s,
}
2024-04-10 10:45:58 +00:00
}
func (ec *evalCtx) addCmd(name string, inv invokable) {
if ec.commands == nil {
ec.commands = make(map[string]invokable)
}
ec.commands[name] = inv
}
func (ec *evalCtx) lookupCmd(name string) (invokable, error) {
for e := ec; e != nil; e = e.parent {
if e.commands == nil {
2024-04-10 10:45:58 +00:00
continue
}
if cmd, ok := e.commands[name]; ok {
2024-04-10 10:45:58 +00:00
return cmd, nil
}
}
return nil, errors.New("name " + name + " not found")
}