From 575d96995666f6ed1a32b4a1c7b9d3df75e82fde Mon Sep 17 00:00:00 2001 From: Daniel Kang Date: Mon, 14 Jan 2019 23:52:52 -0800 Subject: [PATCH] split iterator files --- objects/array_iterator.go | 65 ++++++++++++ objects/builtin_len.go | 4 +- objects/int.go | 2 +- objects/iterator.go | 201 ------------------------------------- objects/map_iterator.go | 76 ++++++++++++++ objects/string_iterator.go | 67 +++++++++++++ 6 files changed, 211 insertions(+), 204 deletions(-) create mode 100644 objects/array_iterator.go create mode 100644 objects/map_iterator.go create mode 100644 objects/string_iterator.go diff --git a/objects/array_iterator.go b/objects/array_iterator.go new file mode 100644 index 0000000..69f7009 --- /dev/null +++ b/objects/array_iterator.go @@ -0,0 +1,65 @@ +package objects + +import "github.com/d5/tengo/compiler/token" + +// ArrayIterator is an iterator for an array. +type ArrayIterator struct { + v []Object + i int + l int +} + +// NewArrayIterator creates an ArrayIterator. +func NewArrayIterator(v *Array) Iterator { + return &ArrayIterator{ + v: v.Value, + l: len(v.Value), + } +} + +// TypeName returns the name of the type. +func (i *ArrayIterator) TypeName() string { + return "array-iterator" +} + +func (i *ArrayIterator) String() string { + return "" +} + +// BinaryOp returns another object that is the result of +// a given binary operator and a right-hand side object. +func (i *ArrayIterator) BinaryOp(op token.Token, rhs Object) (Object, error) { + return nil, ErrInvalidOperator +} + +// IsFalsy returns true if the value of the type is falsy. +func (i *ArrayIterator) IsFalsy() bool { + return true +} + +// Equals returns true if the value of the type +// is equal to the value of another object. +func (i *ArrayIterator) Equals(Object) bool { + return false +} + +// Copy returns a copy of the type. +func (i *ArrayIterator) Copy() Object { + return &ArrayIterator{v: i.v, i: i.i, l: i.l} +} + +// Next returns true if there are more elements to iterate. +func (i *ArrayIterator) Next() bool { + i.i++ + return i.i <= i.l +} + +// Key returns the key or index value of the current element. +func (i *ArrayIterator) Key() Object { + return &Int{Value: int64(i.i - 1)} +} + +// Value returns the value of the current element. +func (i *ArrayIterator) Value() Object { + return i.v[i.i-1] +} diff --git a/objects/builtin_len.go b/objects/builtin_len.go index 0c4ce00..51a2b10 100644 --- a/objects/builtin_len.go +++ b/objects/builtin_len.go @@ -11,9 +11,9 @@ func builtinLen(args ...Object) (Object, error) { switch arg := args[0].(type) { case *Array: - return &Int{int64(len(arg.Value))}, nil + return &Int{Value: int64(len(arg.Value))}, nil case *String: - return &Int{int64(len(arg.Value))}, nil + return &Int{Value: int64(len(arg.Value))}, nil default: return nil, fmt.Errorf("unsupported type for 'len' function: %s", arg.TypeName()) } diff --git a/objects/int.go b/objects/int.go index ecf9477..18d9b0b 100644 --- a/objects/int.go +++ b/objects/int.go @@ -151,7 +151,7 @@ func (o *Int) BinaryOp(op token.Token, rhs Object) (Object, error) { // Copy returns a copy of the type. func (o *Int) Copy() Object { - return &Int{o.Value} + return &Int{Value: o.Value} } // IsFalsy returns true if the value of the type is falsy. diff --git a/objects/iterator.go b/objects/iterator.go index e7fa67c..01522ba 100644 --- a/objects/iterator.go +++ b/objects/iterator.go @@ -1,7 +1,5 @@ package objects -import "github.com/d5/tengo/compiler/token" - // Iterator represents an iterator for underlying data type. type Iterator interface { Object @@ -15,202 +13,3 @@ type Iterator interface { // Value returns the value of the current element. Value() Object } - -// ArrayIterator is an iterator for an array. -type ArrayIterator struct { - v []Object - i int - l int -} - -// NewArrayIterator creates an ArrayIterator. -func NewArrayIterator(v *Array) Iterator { - return &ArrayIterator{ - v: v.Value, - l: len(v.Value), - } -} - -// TypeName returns the name of the type. -func (i *ArrayIterator) TypeName() string { - return "array-iterator" -} - -func (i *ArrayIterator) String() string { - return "" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (i *ArrayIterator) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// IsFalsy returns true if the value of the type is falsy. -func (i *ArrayIterator) IsFalsy() bool { - return true -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (i *ArrayIterator) Equals(Object) bool { - return false -} - -// Copy returns a copy of the type. -func (i *ArrayIterator) Copy() Object { - return &ArrayIterator{v: i.v, i: i.i, l: i.l} -} - -// Next returns true if there are more elements to iterate. -func (i *ArrayIterator) Next() bool { - i.i++ - return i.i <= i.l -} - -// Key returns the key or index value of the current element. -func (i *ArrayIterator) Key() Object { - return &Int{int64(i.i - 1)} -} - -// Value returns the value of the current element. -func (i *ArrayIterator) Value() Object { - return i.v[i.i-1] -} - -// MapIterator represents an iterator for the map. -type MapIterator struct { - v map[string]Object - k []string - i int - l int -} - -// NewMapIterator creates a map iterator. -func NewMapIterator(v *Map) Iterator { - var keys []string - for k := range v.Value { - keys = append(keys, k) - } - - return &MapIterator{ - v: v.Value, - k: keys, - l: len(keys), - } -} - -// TypeName returns the name of the type. -func (i *MapIterator) TypeName() string { - return "map-iterator" -} - -func (i *MapIterator) String() string { - return "" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (i *MapIterator) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// IsFalsy returns true if the value of the type is falsy. -func (i *MapIterator) IsFalsy() bool { - return true -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (i *MapIterator) Equals(Object) bool { - return false -} - -// Copy returns a copy of the type. -func (i *MapIterator) Copy() Object { - return &MapIterator{v: i.v, k: i.k, i: i.i, l: i.l} -} - -// Next returns true if there are more elements to iterate. -func (i *MapIterator) Next() bool { - i.i++ - return i.i <= i.l -} - -// Key returns the key or index value of the current element. -func (i *MapIterator) Key() Object { - k := i.k[i.i-1] - - return &String{Value: k} -} - -// Value returns the value of the current element. -func (i *MapIterator) Value() Object { - k := i.k[i.i-1] - - return i.v[k] -} - -// StringIterator represents an iterator for a string. -type StringIterator struct { - v []rune - i int - l int -} - -// NewStringIterator creates a string iterator. -func NewStringIterator(v *String) Iterator { - r := []rune(v.Value) - - return &StringIterator{ - v: r, - l: len(r), - } -} - -// TypeName returns the name of the type. -func (i *StringIterator) TypeName() string { - return "string-iterator" -} - -func (i *StringIterator) String() string { - return "" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (i *StringIterator) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// IsFalsy returns true if the value of the type is falsy. -func (i *StringIterator) IsFalsy() bool { - return true -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (i *StringIterator) Equals(Object) bool { - return false -} - -// Copy returns a copy of the type. -func (i *StringIterator) Copy() Object { - return &StringIterator{v: i.v, i: i.i, l: i.l} -} - -// Next returns true if there are more elements to iterate. -func (i *StringIterator) Next() bool { - i.i++ - return i.i <= i.l -} - -// Key returns the key or index value of the current element. -func (i *StringIterator) Key() Object { - return &Int{int64(i.i - 1)} -} - -// Value returns the value of the current element. -func (i *StringIterator) Value() Object { - return &Char{Value: i.v[i.i-1]} -} diff --git a/objects/map_iterator.go b/objects/map_iterator.go new file mode 100644 index 0000000..17137dd --- /dev/null +++ b/objects/map_iterator.go @@ -0,0 +1,76 @@ +package objects + +import "github.com/d5/tengo/compiler/token" + +// MapIterator represents an iterator for the map. +type MapIterator struct { + v map[string]Object + k []string + i int + l int +} + +// NewMapIterator creates a map iterator. +func NewMapIterator(v *Map) Iterator { + var keys []string + for k := range v.Value { + keys = append(keys, k) + } + + return &MapIterator{ + v: v.Value, + k: keys, + l: len(keys), + } +} + +// TypeName returns the name of the type. +func (i *MapIterator) TypeName() string { + return "map-iterator" +} + +func (i *MapIterator) String() string { + return "" +} + +// BinaryOp returns another object that is the result of +// a given binary operator and a right-hand side object. +func (i *MapIterator) BinaryOp(op token.Token, rhs Object) (Object, error) { + return nil, ErrInvalidOperator +} + +// IsFalsy returns true if the value of the type is falsy. +func (i *MapIterator) IsFalsy() bool { + return true +} + +// Equals returns true if the value of the type +// is equal to the value of another object. +func (i *MapIterator) Equals(Object) bool { + return false +} + +// Copy returns a copy of the type. +func (i *MapIterator) Copy() Object { + return &MapIterator{v: i.v, k: i.k, i: i.i, l: i.l} +} + +// Next returns true if there are more elements to iterate. +func (i *MapIterator) Next() bool { + i.i++ + return i.i <= i.l +} + +// Key returns the key or index value of the current element. +func (i *MapIterator) Key() Object { + k := i.k[i.i-1] + + return &String{Value: k} +} + +// Value returns the value of the current element. +func (i *MapIterator) Value() Object { + k := i.k[i.i-1] + + return i.v[k] +} diff --git a/objects/string_iterator.go b/objects/string_iterator.go new file mode 100644 index 0000000..923ccf2 --- /dev/null +++ b/objects/string_iterator.go @@ -0,0 +1,67 @@ +package objects + +import "github.com/d5/tengo/compiler/token" + +// StringIterator represents an iterator for a string. +type StringIterator struct { + v []rune + i int + l int +} + +// NewStringIterator creates a string iterator. +func NewStringIterator(v *String) Iterator { + r := []rune(v.Value) + + return &StringIterator{ + v: r, + l: len(r), + } +} + +// TypeName returns the name of the type. +func (i *StringIterator) TypeName() string { + return "string-iterator" +} + +func (i *StringIterator) String() string { + return "" +} + +// BinaryOp returns another object that is the result of +// a given binary operator and a right-hand side object. +func (i *StringIterator) BinaryOp(op token.Token, rhs Object) (Object, error) { + return nil, ErrInvalidOperator +} + +// IsFalsy returns true if the value of the type is falsy. +func (i *StringIterator) IsFalsy() bool { + return true +} + +// Equals returns true if the value of the type +// is equal to the value of another object. +func (i *StringIterator) Equals(Object) bool { + return false +} + +// Copy returns a copy of the type. +func (i *StringIterator) Copy() Object { + return &StringIterator{v: i.v, i: i.i, l: i.l} +} + +// Next returns true if there are more elements to iterate. +func (i *StringIterator) Next() bool { + i.i++ + return i.i <= i.l +} + +// Key returns the key or index value of the current element. +func (i *StringIterator) Key() Object { + return &Int{Value: int64(i.i - 1)} +} + +// Value returns the value of the current element. +func (i *StringIterator) Value() Object { + return &Char{Value: i.v[i.i-1]} +}