xgo/objects/immutable_map_iterator.go

63 lines
1.5 KiB
Go
Raw Normal View History

2019-01-17 12:56:05 +03:00
package objects
import "github.com/d5/tengo/compiler/token"
2019-01-18 12:43:46 +03:00
// ImmutableMapIterator represents an iterator for the immutable map.
type ImmutableMapIterator struct {
2019-01-17 12:56:05 +03:00
v map[string]Object
k []string
i int
l int
}
// TypeName returns the name of the type.
2019-01-18 12:43:46 +03:00
func (i *ImmutableMapIterator) TypeName() string {
2019-01-17 12:56:05 +03:00
return "module-iterator"
}
2019-01-18 12:43:46 +03:00
func (i *ImmutableMapIterator) String() string {
2019-01-17 12:56:05 +03:00
return "<module-iterator>"
}
// BinaryOp returns another object that is the result of
// a given binary operator and a right-hand side object.
2019-01-18 12:43:46 +03:00
func (i *ImmutableMapIterator) BinaryOp(op token.Token, rhs Object) (Object, error) {
2019-01-17 12:56:05 +03:00
return nil, ErrInvalidOperator
}
// IsFalsy returns true if the value of the type is falsy.
2019-01-18 12:43:46 +03:00
func (i *ImmutableMapIterator) IsFalsy() bool {
2019-01-17 12:56:05 +03:00
return true
}
// Equals returns true if the value of the type
// is equal to the value of another object.
2019-01-18 12:43:46 +03:00
func (i *ImmutableMapIterator) Equals(Object) bool {
2019-01-17 12:56:05 +03:00
return false
}
// Copy returns a copy of the type.
2019-01-18 12:43:46 +03:00
func (i *ImmutableMapIterator) Copy() Object {
return &ImmutableMapIterator{v: i.v, k: i.k, i: i.i, l: i.l}
2019-01-17 12:56:05 +03:00
}
// Next returns true if there are more elements to iterate.
2019-01-18 12:43:46 +03:00
func (i *ImmutableMapIterator) Next() bool {
2019-01-17 12:56:05 +03:00
i.i++
return i.i <= i.l
}
// Key returns the key or index value of the current element.
2019-01-18 12:43:46 +03:00
func (i *ImmutableMapIterator) Key() Object {
2019-01-17 12:56:05 +03:00
k := i.k[i.i-1]
return &String{Value: k}
}
// Value returns the value of the current element.
2019-01-18 12:43:46 +03:00
func (i *ImmutableMapIterator) Value() Object {
2019-01-17 12:56:05 +03:00
k := i.k[i.i-1]
return i.v[k]
}