split iterator files
This commit is contained in:
parent
b79fd4f7ef
commit
575d969956
6 changed files with 211 additions and 204 deletions
65
objects/array_iterator.go
Normal file
65
objects/array_iterator.go
Normal file
|
@ -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 "<array-iterator>"
|
||||
}
|
||||
|
||||
// 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]
|
||||
}
|
|
@ -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())
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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 "<array-iterator>"
|
||||
}
|
||||
|
||||
// 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 "<map-iterator>"
|
||||
}
|
||||
|
||||
// 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 "<string-iterator>"
|
||||
}
|
||||
|
||||
// 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]}
|
||||
}
|
||||
|
|
76
objects/map_iterator.go
Normal file
76
objects/map_iterator.go
Normal file
|
@ -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 "<map-iterator>"
|
||||
}
|
||||
|
||||
// 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]
|
||||
}
|
67
objects/string_iterator.go
Normal file
67
objects/string_iterator.go
Normal file
|
@ -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 "<string-iterator>"
|
||||
}
|
||||
|
||||
// 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]}
|
||||
}
|
Loading…
Reference in a new issue