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

// operator-bitwise.go
package expr

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

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

func evalBitwiseNot(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
}

//-------- Bitwise AND term

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

func bitwiseAnd(opTerm *term, leftValue, rightValue any) (v any, err error) {
	var leftInt, rightInt int64
	var lok, rok bool

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

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

func evalBitwiseAnd(ctx ExprContext, opTerm *term) (v any, err error) {
	var leftValue, rightValue any

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

//-------- Bitwise OR term

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

func bitwiseOr(opTerm *term, leftValue, rightValue any) (v any, err error) {
	var leftInt, rightInt int64
	var lok, rok bool

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

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

func evalBitwiseOr(ctx ExprContext, opTerm *term) (v any, err error) {
	var leftValue, rightValue any

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

	v, err = bitwiseOr(opTerm, leftValue, rightValue)
	return
}

//-------- Bitwise XOR term

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

func bitwiseXor(opTerm *term, leftValue, rightValue any) (v any, err error) {
	var leftInt, rightInt int64
	var lok, rok bool

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

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

func evalBitwiseXor(ctx ExprContext, opTerm *term) (v any, err error) {
	var leftValue, rightValue any

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

	v, err = bitwiseXor(opTerm, leftValue, rightValue)
	return
}

// init
func init() {
	registerTermConstructor(SymTilde, newBitwiseNotTerm)
	registerTermConstructor(SymAmpersand, newBitwiseAndTerm)
	registerTermConstructor(SymVertBar, newBitwiseOrTerm)
	registerTermConstructor(SymCaret, newBitwiseXorTerm)
}
