Iterators now support generic operation interface based on methods HasOperation() and CallOperation().

Also fixed mul() function that called doAdd() instead of doMul().
This commit is contained in:
2024-04-28 06:45:20 +02:00
parent fc0e1ffaee
commit aa66d07caa
5 changed files with 100 additions and 16 deletions
+21 -2
View File
@@ -18,7 +18,7 @@ const (
)
type dataCursor struct {
ds map[any]*term
ds map[string]Functor
ctx ExprContext
index int
resource any
@@ -28,8 +28,9 @@ type dataCursor struct {
currentFunc Functor
}
func newDataCursor(ctx ExprContext) (dc *dataCursor) {
func newDataCursor(ctx ExprContext, ds map[string]Functor) (dc *dataCursor) {
dc = &dataCursor{
ds: ds,
index: -1,
ctx: ctx.Clone(),
}
@@ -40,6 +41,24 @@ func (dc *dataCursor) String() string {
return "$(...)"
}
func (dc *dataCursor) HasOperation(name string) (exists bool) {
f, ok := dc.ds[name]
exists = ok && isFunctor(f)
return
}
func (dc *dataCursor) CallOperation(name string) (value any, err error) {
if functor, ok := dc.ds[name]; ok && isFunctor(functor) {
ctx := cloneContext(dc.ctx)
value, err = functor.Invoke(ctx, name, []any{})
exportObjects(dc.ctx, ctx)
} else {
err = errNoOperation(name)
}
return
}
func (dc *dataCursor) Reset() (err error) {
if dc.resetFunc != nil {
ctx := cloneContext(dc.ctx)