iterator interface chenged index and count members from int to tint 64
This commit is contained in:
parent
75ed88915d
commit
dacbec677a
@ -34,7 +34,7 @@ func doImport(ctx kern.ExprContext, name string, dirList []string, it kern.Itera
|
|||||||
var sourceFilepath string
|
var sourceFilepath string
|
||||||
|
|
||||||
for v, err = it.Next(); err == nil; v, err = it.Next() {
|
for v, err = it.Next(); err == nil; v, err = it.Next() {
|
||||||
if err = checkStringParamExpected(name, v, it.Index()); err != nil {
|
if err = checkStringParamExpected(name, v, int(it.Index())); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if sourceFilepath, err = makeFilepath(v.(string), dirList); err != nil {
|
if sourceFilepath, err = makeFilepath(v.(string), dirList); err != nil {
|
||||||
|
|||||||
@ -41,7 +41,7 @@ func doAdd(ctx kern.ExprContext, name string, it kern.Iterator, count, level int
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if err = checkNumberParamExpected(name, v, count, level, it.Index()); err != nil {
|
} else if err = checkNumberParamExpected(name, v, count, level, int(it.Index())); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
count++
|
count++
|
||||||
@ -116,7 +116,7 @@ func doMul(ctx kern.ExprContext, name string, it kern.Iterator, count, level int
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err = checkNumberParamExpected(name, v, count, level, it.Index()); err != nil {
|
if err = checkNumberParamExpected(name, v, count, level, int(it.Index())); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,8 +17,8 @@ const fileReadTextIteratorType = "fileReadTextIterator"
|
|||||||
|
|
||||||
type fileReadTextIterator struct {
|
type fileReadTextIterator struct {
|
||||||
osReader *osReader
|
osReader *osReader
|
||||||
index int
|
index int64
|
||||||
count int
|
count int64
|
||||||
line string
|
line string
|
||||||
autoClose bool
|
autoClose bool
|
||||||
}
|
}
|
||||||
@ -38,7 +38,7 @@ func (it *fileReadTextIterator) String() string {
|
|||||||
return fmt.Sprintf("$(%s@<nil>)", fileReadTextIteratorType)
|
return fmt.Sprintf("$(%s@<nil>)", fileReadTextIteratorType)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *fileReadTextIterator) Count() int {
|
func (it *fileReadTextIterator) Count() int64 {
|
||||||
return it.count
|
return it.count
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ func (it *fileReadTextIterator) Current() (item any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *fileReadTextIterator) Index() int {
|
func (it *fileReadTextIterator) Index() int64 {
|
||||||
return it.index
|
return it.index
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,8 +16,8 @@ type dataCursor struct {
|
|||||||
ctx kern.ExprContext
|
ctx kern.ExprContext
|
||||||
initState bool // true if no item has produced yet (this replace di initial Next() call in the contructor)
|
initState bool // true if no item has produced yet (this replace di initial Next() call in the contructor)
|
||||||
// cursorValid bool // true if resource is nil or if clean has not yet been called
|
// cursorValid bool // true if resource is nil or if clean has not yet been called
|
||||||
index int
|
index int64
|
||||||
count int
|
count int64
|
||||||
current any
|
current any
|
||||||
lastErr error
|
lastErr error
|
||||||
resource any
|
resource any
|
||||||
@ -298,10 +298,10 @@ func (dc *dataCursor) Next() (current any, err error) { // must return io.EOF af
|
|||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func (dc *dataCursor) Index() int {
|
func (dc *dataCursor) Index() int64 {
|
||||||
return dc.index - 1
|
return dc.index - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dc *dataCursor) Count() int {
|
func (dc *dataCursor) Count() int64 {
|
||||||
return dc.count
|
return dc.count
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,8 +23,8 @@ const (
|
|||||||
|
|
||||||
type DictIterator struct {
|
type DictIterator struct {
|
||||||
a *kern.DictType
|
a *kern.DictType
|
||||||
count int
|
count int64
|
||||||
index int
|
index int64
|
||||||
keys []any
|
keys []any
|
||||||
iterMode dictIterMode
|
iterMode dictIterMode
|
||||||
}
|
}
|
||||||
@ -127,9 +127,9 @@ func NewMapIterator(m map[any]any) (it *DictIterator) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (it *DictIterator) String() string {
|
func (it *DictIterator) String() string {
|
||||||
var l = 0
|
var l = int64(0)
|
||||||
if it.a != nil {
|
if it.a != nil {
|
||||||
l = len(*it.a)
|
l = int64(len(*it.a))
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("$({#%d})", l)
|
return fmt.Sprintf("$({#%d})", l)
|
||||||
}
|
}
|
||||||
@ -159,13 +159,13 @@ func (it *DictIterator) CallOperation(name string, args map[string]any) (v any,
|
|||||||
case kern.CountName:
|
case kern.CountName:
|
||||||
v = it.count
|
v = it.count
|
||||||
case kern.KeyName:
|
case kern.KeyName:
|
||||||
if it.index >= 0 && it.index < len(it.keys) {
|
if it.index >= 0 && it.index < int64(len(it.keys)) {
|
||||||
v = it.keys[it.index]
|
v = it.keys[it.index]
|
||||||
} else {
|
} else {
|
||||||
err = io.EOF
|
err = io.EOF
|
||||||
}
|
}
|
||||||
case kern.ValueName:
|
case kern.ValueName:
|
||||||
if it.index >= 0 && it.index < len(it.keys) {
|
if it.index >= 0 && it.index < int64(len(it.keys)) {
|
||||||
a := *(it.a)
|
a := *(it.a)
|
||||||
v = a[it.keys[it.index]]
|
v = a[it.keys[it.index]]
|
||||||
} else {
|
} else {
|
||||||
@ -178,7 +178,7 @@ func (it *DictIterator) CallOperation(name string, args map[string]any) (v any,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (it *DictIterator) Current() (item any, err error) {
|
func (it *DictIterator) Current() (item any, err error) {
|
||||||
if it.index >= 0 && it.index < len(it.keys) {
|
if it.index >= 0 && it.index < int64(len(it.keys)) {
|
||||||
switch it.iterMode {
|
switch it.iterMode {
|
||||||
case dictIterModeKeys:
|
case dictIterModeKeys:
|
||||||
item = it.keys[it.index]
|
item = it.keys[it.index]
|
||||||
@ -204,11 +204,11 @@ func (it *DictIterator) Next() (item any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *DictIterator) Index() int {
|
func (it *DictIterator) Index() int64 {
|
||||||
return it.index
|
return it.index
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *DictIterator) Count() int {
|
func (it *DictIterator) Count() int64 {
|
||||||
return it.count
|
return it.count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -30,8 +30,8 @@ type Iterator interface {
|
|||||||
fmt.Stringer
|
fmt.Stringer
|
||||||
Next() (item any, err error) // must return io.EOF after the last item
|
Next() (item any, err error) // must return io.EOF after the last item
|
||||||
Current() (item any, err error)
|
Current() (item any, err error)
|
||||||
Index() int
|
Index() int64
|
||||||
Count() int
|
Count() int64
|
||||||
HasOperation(name string) bool
|
HasOperation(name string) bool
|
||||||
CallOperation(name string, args map[string]any) (value any, err error)
|
CallOperation(name string, args map[string]any) (value any, err error)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -75,3 +75,14 @@ func ToGoInt(value any, description string) (i int, err error) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToGoInt64(value any, description string) (i int64, err error) {
|
||||||
|
if valueInt64, ok := value.(int64); ok {
|
||||||
|
i = valueInt64
|
||||||
|
} else if valueInt, ok := value.(int); ok {
|
||||||
|
i = int64(valueInt)
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("%s expected integer, got %s (%v)", description, TypeName(value), value)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@ -14,36 +14,36 @@ import (
|
|||||||
|
|
||||||
type ListIterator struct {
|
type ListIterator struct {
|
||||||
a *kern.ListType
|
a *kern.ListType
|
||||||
count int
|
count int64
|
||||||
index int
|
index int64
|
||||||
start int
|
start int64
|
||||||
stop int
|
stop int64
|
||||||
step int
|
step int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewListIterator(list *kern.ListType, args []any) (it *ListIterator) {
|
func NewListIterator(list *kern.ListType, args []any) (it *ListIterator) {
|
||||||
var argc int = 0
|
var argc int = 0
|
||||||
listLen := len(([]any)(*list))
|
listLen := int64(len(([]any)(*list)))
|
||||||
if args != nil {
|
if args != nil {
|
||||||
argc = len(args)
|
argc = len(args)
|
||||||
}
|
}
|
||||||
it = &ListIterator{a: list, count: 0, index: -1, start: 0, stop: listLen - 1, step: 1}
|
it = &ListIterator{a: list, count: 0, index: -1, start: 0, stop: listLen - 1, step: 1}
|
||||||
if argc >= 1 {
|
if argc >= 1 {
|
||||||
if i, err := kern.ToGoInt(args[0], "start index"); err == nil {
|
if i, err := kern.ToGoInt64(args[0], "start index"); err == nil {
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
i = listLen + i
|
i = listLen + i
|
||||||
}
|
}
|
||||||
it.start = i
|
it.start = i
|
||||||
}
|
}
|
||||||
if argc >= 2 {
|
if argc >= 2 {
|
||||||
if i, err := kern.ToGoInt(args[1], "stop index"); err == nil {
|
if i, err := kern.ToGoInt64(args[1], "stop index"); err == nil {
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
i = listLen + i
|
i = listLen + i
|
||||||
}
|
}
|
||||||
it.stop = i
|
it.stop = i
|
||||||
}
|
}
|
||||||
if argc >= 3 {
|
if argc >= 3 {
|
||||||
if i, err := kern.ToGoInt(args[2], "step"); err == nil {
|
if i, err := kern.ToGoInt64(args[2], "step"); err == nil {
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
i = -i
|
i = -i
|
||||||
}
|
}
|
||||||
@ -61,14 +61,14 @@ func NewListIterator(list *kern.ListType, args []any) (it *ListIterator) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewArrayIterator(array []any) (it *ListIterator) {
|
func NewArrayIterator(array []any) (it *ListIterator) {
|
||||||
it = &ListIterator{a: (*kern.ListType)(&array), count: 0, index: -1, start: 0, stop: len(array) - 1, step: 1}
|
it = &ListIterator{a: (*kern.ListType)(&array), count: 0, index: -1, start: 0, stop: int64(len(array)) - 1, step: 1}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *ListIterator) String() string {
|
func (it *ListIterator) String() string {
|
||||||
var l = 0
|
var l = int64(0)
|
||||||
if it.a != nil {
|
if it.a != nil {
|
||||||
l = len(*it.a)
|
l = int64(len(*it.a))
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("$([#%d])", l)
|
return fmt.Sprintf("$([#%d])", l)
|
||||||
}
|
}
|
||||||
@ -106,13 +106,13 @@ func (it *ListIterator) CallOperation(name string, args map[string]any) (v any,
|
|||||||
func (it *ListIterator) Current() (item any, err error) {
|
func (it *ListIterator) Current() (item any, err error) {
|
||||||
a := *(it.a)
|
a := *(it.a)
|
||||||
if it.start <= it.stop {
|
if it.start <= it.stop {
|
||||||
if it.stop < len(a) && it.index >= it.start && it.index <= it.stop {
|
if it.stop < int64(len(a)) && it.index >= it.start && it.index <= it.stop {
|
||||||
item = a[it.index]
|
item = a[it.index]
|
||||||
} else {
|
} else {
|
||||||
err = io.EOF
|
err = io.EOF
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if it.start < len(a) && it.index >= it.stop && it.index <= it.start {
|
if it.start < int64(len(a)) && it.index >= it.stop && it.index <= it.start {
|
||||||
item = a[it.index]
|
item = a[it.index]
|
||||||
} else {
|
} else {
|
||||||
err = io.EOF
|
err = io.EOF
|
||||||
@ -130,11 +130,11 @@ func (it *ListIterator) Next() (item any, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *ListIterator) Index() int {
|
func (it *ListIterator) Index() int64 {
|
||||||
return it.index
|
return it.index
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *ListIterator) Count() int {
|
func (it *ListIterator) Count() int64 {
|
||||||
return it.count
|
return it.count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -136,6 +136,11 @@ func evalIterator(ctx kern.ExprContext, opTerm *term) (v any, err error) {
|
|||||||
if args, err = evalSibling(ctx, opTerm.children, nil); err == nil {
|
if args, err = evalSibling(ctx, opTerm.children, nil); err == nil {
|
||||||
v = NewListIterator(list, args)
|
v = NewListIterator(list, args)
|
||||||
}
|
}
|
||||||
|
} else if intVal, ok := firstChildValue.(int64); ok {
|
||||||
|
var args []any
|
||||||
|
if args, err = evalSibling(ctx, opTerm.children, intVal); err == nil {
|
||||||
|
v, err = NewIntIterator(args)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
var list []any
|
var list []any
|
||||||
if list, err = evalSibling(ctx, opTerm.children, firstChildValue); err == nil {
|
if list, err = evalSibling(ctx, opTerm.children, firstChildValue); err == nil {
|
||||||
|
|||||||
@ -14,7 +14,7 @@ func TestFuncRun(t *testing.T) {
|
|||||||
inputs := []inputType{
|
inputs := []inputType{
|
||||||
/* 1 */ {`builtin "iterator"; it=$(1,2,3); run(it)`, nil, nil},
|
/* 1 */ {`builtin "iterator"; it=$(1,2,3); run(it)`, nil, nil},
|
||||||
/* 2 */ {`builtin "iterator"; run($(1,2,3), func(index,item){item+10})`, nil, nil},
|
/* 2 */ {`builtin "iterator"; run($(1,2,3), func(index,item){item+10})`, nil, nil},
|
||||||
/* 3 */ {`builtin "iterator"; run($(1,2,3), func(index,item){status=status+item; true}, {"status":0})`, int64(6), nil},
|
/* 3 */ {`builtin "iterator"; run($(4), func(index,item){status=status+item; true}, {"status":0})`, int64(6), nil},
|
||||||
/* 4 */ {`builtin ["iterator", "fmt"]; run($(1,2,3), func(index,item){println(item+10)})`, nil, nil},
|
/* 4 */ {`builtin ["iterator", "fmt"]; run($(1,2,3), func(index,item){println(item+10)})`, nil, nil},
|
||||||
/* 5 */ {`builtin "iterator"; run(nil)`, nil, `paramter "iterator" must be an iterator, passed <nil> [nil]`},
|
/* 5 */ {`builtin "iterator"; run(nil)`, nil, `paramter "iterator" must be an iterator, passed <nil> [nil]`},
|
||||||
/* 6 */ {`builtin "iterator"; run($(1,2,3), nil)`, nil, nil},
|
/* 6 */ {`builtin "iterator"; run($(1,2,3), nil)`, nil, nil},
|
||||||
@ -26,6 +26,6 @@ func TestFuncRun(t *testing.T) {
|
|||||||
|
|
||||||
//t.Setenv("EXPR_PATH", ".")
|
//t.Setenv("EXPR_PATH", ".")
|
||||||
|
|
||||||
runTestSuiteSpec(t, section, inputs, 10)
|
// runTestSuiteSpec(t, section, inputs, 3)
|
||||||
// runTestSuite(t, section, inputs)
|
runTestSuite(t, section, inputs)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,8 +38,13 @@ func TestIteratorParser(t *testing.T) {
|
|||||||
/* 23 */ {`builtin "os.file"; fileReadIterator("test-file.txt") map ${_index}`, kern.NewList([]any{int64(0), int64(1)}), nil},
|
/* 23 */ {`builtin "os.file"; fileReadIterator("test-file.txt") map ${_index}`, kern.NewList([]any{int64(0), int64(1)}), nil},
|
||||||
/* 24 */ {`builtin "os.file"; #(fileReadIterator("test-file.txt") filter (#${_} == 2))`, int64(0), nil},
|
/* 24 */ {`builtin "os.file"; #(fileReadIterator("test-file.txt") filter (#${_} == 2))`, int64(0), nil},
|
||||||
/* 25 */ {`builtin "os.file"; #(fileReadIterator("test-file.txt") filter (#${_} == 3))`, int64(2), nil},
|
/* 25 */ {`builtin "os.file"; #(fileReadIterator("test-file.txt") filter (#${_} == 3))`, int64(2), nil},
|
||||||
|
/* 26 */ {`#($(10) map ${_})`, int64(10), nil},
|
||||||
|
/* 27 */ {`#($(10,0) map ${_})`, int64(10), nil},
|
||||||
|
/* 28 */ {`$(10) digest ${_}`, int64(9), nil},
|
||||||
|
/* 29 */ {`$(10,0) digest ${_}`, int64(1), nil},
|
||||||
|
/* 30 */ {`$(10,0,-2) digest ${_}`, int64(2), nil},
|
||||||
}
|
}
|
||||||
|
|
||||||
// runTestSuiteSpec(t, section, inputs, 23)
|
// runTestSuiteSpec(t, section, inputs, 10)
|
||||||
runTestSuite(t, section, inputs)
|
runTestSuite(t, section, inputs)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -152,7 +152,7 @@ func TestSpecialParser(t *testing.T) {
|
|||||||
section := "Parser"
|
section := "Parser"
|
||||||
|
|
||||||
inputs := []inputType{
|
inputs := []inputType{
|
||||||
/* 1 */ {`()`, nil, `[1:2] expected expression, got "()"`},
|
/* 1 */ {`()`, nil, "[1:2] expected expression, got `()`"},
|
||||||
}
|
}
|
||||||
|
|
||||||
// t.Setenv("EXPR_PATH", ".")
|
// t.Setenv("EXPR_PATH", ".")
|
||||||
|
|||||||
2
token.go
2
token.go
@ -121,6 +121,6 @@ func (tk *Token) ErrorExpectedGotString(symbol, got string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tk *Token) ErrorExpectedGotStringWithPrefix(prefix, symbol, got string) (err error) {
|
func (tk *Token) ErrorExpectedGotStringWithPrefix(prefix, symbol, got string) (err error) {
|
||||||
err = fmt.Errorf("[%d:%d] %s %s, got %q", tk.row, tk.col, prefix, symbol, got)
|
err = fmt.Errorf("[%d:%d] %s %s, got `%s`", tk.row, tk.col, prefix, symbol, got)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user