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

// operator-binary.go
package expr

//-------- NOT term

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

func evalBinaryNot(ctx ExprContext, opTerm *term) (v any, err error) {
	var value any

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

	if IsInteger(value) {
		i, _ := value.(int64)
		v = ^i
	} else {
		err = opTerm.errIncompatibleType(value)
	}
	return
}

//-------- Binary AND term

func newBinAndTerm(tk *Token) (inst *term) {
	return &term{
		tk:       *tk,
		children: make([]*term, 0, 2),
		position: posInfix,
		priority: priAnd,
		evalFunc: evalBinaryAnd,
	}
}

func evalBinaryAnd(ctx ExprContext, self *term) (v any, err error) {
	var leftValue, rightValue any
	var leftInt, rightInt int64
	var lok, rok bool

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

	leftInt, lok = leftValue.(int64)
	rightInt, rok = rightValue.(int64)

	if lok && rok {
		v = leftInt & rightInt
	} else {
		err = self.errIncompatibleTypes(leftValue, rightValue)
	}
	return
}

//-------- Binary OR term

func newBinOrTerm(tk *Token) (inst *term) {
	return &term{
		tk:       *tk,
		children: make([]*term, 0, 2),
		position: posInfix,
		priority: priOr,
		evalFunc: evalBinaryOr,
	}
}

func evalBinaryOr(ctx ExprContext, self *term) (v any, err error) {
	var leftValue, rightValue any
	var leftInt, rightInt int64
	var lok, rok bool

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

	leftInt, lok = leftValue.(int64)
	rightInt, rok = rightValue.(int64)

	if lok && rok {
		v = leftInt | rightInt
	} else {
		err = self.errIncompatibleTypes(leftValue, rightValue)
	}
	return
}

// init
func init() {
	registerTermConstructor(SymTilde, newBinNotTerm)
	registerTermConstructor(SymAmpersand, newBinAndTerm)
	// registerTermConstructor(SymVertBar, newBinOrTerm)
}