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:
+57
-13
@@ -6,6 +6,8 @@ package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// -------- iterator term
|
||||
@@ -41,6 +43,44 @@ func evalTermArray(ctx ExprContext, a []*term) (values []any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// func getDataSourceDict(ctx ExprContext, self *term) (ds map[string]Functor, err error) {
|
||||
// var value any
|
||||
// if len(self.children) < 1 || self.children[0] == nil {
|
||||
// err = self.Errorf("missing the data-source parameter")
|
||||
// return
|
||||
// }
|
||||
|
||||
// if value, err = self.children[0].compute(ctx); err != nil {
|
||||
// return
|
||||
// }
|
||||
|
||||
// if dictAny, ok := value.(map[any]any); ok {
|
||||
// ds = make(map[string]Functor)
|
||||
// // required functions
|
||||
// for _, k := range []string{currentName, nextName} {
|
||||
// if item, exists := dictAny[k]; exists && item != nil {
|
||||
// if functor, ok := item.(*funcDefFunctor); ok {
|
||||
// ds[k] = functor
|
||||
// }
|
||||
// } else {
|
||||
// err = fmt.Errorf("the data-source must provide a non-nil %q operator", k)
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
// // Optional functions
|
||||
// for _, k := range []string{initName, resetName, cleanName} {
|
||||
// if item, exists := dictAny[k]; exists && item != nil {
|
||||
// if functor, ok := item.(*funcDefFunctor); ok {
|
||||
// ds[k] = functor
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// err = self.Errorf("the first param (data-source) of an iterator must be a dict, not a %T", value)
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
func getDataSourceDict(ctx ExprContext, self *term) (ds map[string]Functor, err error) {
|
||||
var value any
|
||||
if len(self.children) < 1 || self.children[0] == nil {
|
||||
@@ -52,26 +92,30 @@ func getDataSourceDict(ctx ExprContext, self *term) (ds map[string]Functor, err
|
||||
return
|
||||
}
|
||||
|
||||
requiredFields := []string{currentName, nextName}
|
||||
fieldsMask := 0b11
|
||||
foundFields := 0
|
||||
if dictAny, ok := value.(map[any]any); ok {
|
||||
ds = make(map[string]Functor)
|
||||
// required functions
|
||||
for _, k := range []string{currentName, nextName} {
|
||||
if item, exists := dictAny[k]; exists && item != nil {
|
||||
for keyAny, item := range dictAny {
|
||||
if key, ok := keyAny.(string); ok {
|
||||
if functor, ok := item.(*funcDefFunctor); ok {
|
||||
ds[k] = functor
|
||||
ds[key] = functor
|
||||
if index := slices.Index(requiredFields, key); index >= 0 {
|
||||
foundFields |= 1 << index
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf("the data-source must provide a non-nil %q operator", k)
|
||||
return
|
||||
}
|
||||
}
|
||||
// Optional functions
|
||||
for _, k := range []string{initName, resetName, cleanName} {
|
||||
if item, exists := dictAny[k]; exists && item != nil {
|
||||
if functor, ok := item.(*funcDefFunctor); ok {
|
||||
ds[k] = functor
|
||||
// check required functions
|
||||
if foundFields != fieldsMask {
|
||||
missingFields := make([]string, 0, len(requiredFields))
|
||||
for index, field := range requiredFields {
|
||||
if (foundFields & (1 << index)) == 0 {
|
||||
missingFields = append(missingFields, field)
|
||||
}
|
||||
}
|
||||
err = fmt.Errorf("the data-source must provide a non-nil %q operator(s)", strings.Join(missingFields, ", "))
|
||||
}
|
||||
} else {
|
||||
err = self.Errorf("the first param (data-source) of an iterator must be a dict, not a %T", value)
|
||||
@@ -86,7 +130,7 @@ func evalIterator(ctx ExprContext, self *term) (v any, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
dc := newDataCursor(ctx)
|
||||
dc := newDataCursor(ctx, ds)
|
||||
|
||||
if initFunc, exists := ds[initName]; exists && initFunc != nil {
|
||||
var args []any
|
||||
|
||||
Reference in New Issue
Block a user