// 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: priDereference,
		evalFunc: evalIterValue,
	}
}

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

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

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

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