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

// operator-fact.go
package expr

import "fmt"

//-------- fact term

func newFactTerm(tk *Token) (inst *term) {
	return &term{
		tk:       *tk,
		children: make([]*term, 0, 1),
		position: posPostfix,
		priority: priFact,
		evalFunc: evalFact,
	}
}

func evalFact(ctx ExprContext, self *term) (v any, err error) {
	var leftValue any

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

	if IsInteger(leftValue) {
		if i, _ := leftValue.(int64); i >= 0 {
			f := int64(1)
			for k := int64(1); k <= i; k++ {
				f *= k
			}
			v = f
		} else {
			err = fmt.Errorf("factorial of a negative integer (%d) is not allowed", i)
		}
	} else {
		err = self.errIncompatibleType(leftValue)
	}
	return
}

// init
func init() {
	registerTermConstructor(SymExclamation, newFactTerm)
}
