// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.

// operator-iter-value.go
package expr

//-------- iter value term

func newIterValueTerm(tk *Token) (inst *term) {
	return &term{
		tk:       *tk,
		children: make([]*term, 0, 1),
		position: posPrefix,
		priority: priIterValue,
		evalFunc: evalIterValue,
	}
}

func evalIterValue(ctx ExprContext, self *term) (v any, err error) {
	var childValue any

	if childValue, err = self.evalPrefix(ctx); err != nil {
		return
	}

	if it, ok := childValue.(Iterator); ok {
		v, err = it.Current()
	} else {
		err = self.errIncompatibleType(childValue)
	}
	return
}

// init
func init() {
	registerTermConstructor(SymOpenClosedRound, newIterValueTerm)
}
