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

// operator-sign.go
package expr

//-------- plus sign term

func newPlusSignTerm(tk *Token) (inst *term) {
	return &term{
		tk: *tk,
		// class:    classOperator,
		// kind:     kindUnknown,
		children: make([]*term, 0, 1),
		position: posPrefix,
		priority: priSign,
		evalFunc: evalSign,
	}
}

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

func evalSign(ctx ExprContext, self *term) (v any, err error) {
	var rightValue any

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

	if IsFloat(rightValue) {
		if self.tk.Sym == SymChangeSign {
			f, _ := rightValue.(float64)
			v = -f
		} else {
			v = rightValue
		}
	} else if IsInteger(rightValue) {
		if self.tk.Sym == SymChangeSign {
			i, _ := rightValue.(int64)
			v = -i
		} else {
			v = rightValue
		}
	} else {
		err = self.errIncompatibleType(rightValue)
	}
	return
}

// init
func init() {
	registerTermConstructor(SymUnchangeSign, newPlusSignTerm)
	registerTermConstructor(SymChangeSign, newMinusSignTerm)
}
