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

// operator-post-inc.go
package expr

// -------- post increment term

func newPostIncTerm(tk *Token) *term {
	return &term{
		tk:       *tk,
		parent:   nil,
		children: make([]*term, 0, 1),
		position: posPostfix,
		priority: priIncDec,
		evalFunc: evalPostInc,
	}
}

func evalPostInc(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.Next()
	} else if IsInteger(childValue) && self.children[0].symbol() == SymVariable {
		v = childValue
		i, _ := childValue.(int64)
		ctx.SetVar(self.children[0].source(), i+1)
	} else {
		err = self.errIncompatibleType(childValue)
	}
	return
}

// init
func init() {
	registerTermConstructor(SymDoublePlus, newPostIncTerm)
}
