summaryrefslogtreecommitdiff
path: root/internal/rpn/boolean_ops.go
blob: d07aa2628863dc1a0e10a56f0a9edd37eb2c899d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Paul Buetow

package rpn

import (
	"fmt"
)

// BooleanOperations provides boolean comparison operator implementations.
type BooleanOperations struct {
}

// NewBooleanOperations creates a new BooleanOperations instance.
func NewBooleanOperations() *BooleanOperations {
	return &BooleanOperations{}
}

// GT pops two values from stack, compares (a > b), and pushes a boolean result.
func (o *BooleanOperations) GT(stack *Stack) error {
	b, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for gt: %w", err)
	}

	a, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for gt: %w", err)
	}

	stack.Push(NewFloatFromBool(a.Float64() > b.Float64()))
	return nil
}

// LT pops two values from stack, compares (a < b), and pushes a boolean result.
func (o *BooleanOperations) LT(stack *Stack) error {
	b, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for lt: %w", err)
	}

	a, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for lt: %w", err)
	}

	stack.Push(NewFloatFromBool(a.Float64() < b.Float64()))
	return nil
}

// GTE pops two values from stack, compares (a >= b), and pushes a boolean result.
func (o *BooleanOperations) GTE(stack *Stack) error {
	b, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for gte: %w", err)
	}

	a, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for gte: %w", err)
	}

	stack.Push(NewFloatFromBool(a.Float64() >= b.Float64()))
	return nil
}

// LTE pops two values from stack, compares (a <= b), and pushes a boolean result.
func (o *BooleanOperations) LTE(stack *Stack) error {
	b, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for lte: %w", err)
	}

	a, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for lte: %w", err)
	}

	stack.Push(NewFloatFromBool(a.Float64() <= b.Float64()))
	return nil
}

// EQ pops two values from stack, compares (a == b), and pushes a boolean result.
func (o *BooleanOperations) EQ(stack *Stack) error {
	b, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for eq: %w", err)
	}

	a, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for eq: %w", err)
	}

	stack.Push(NewFloatFromBool(a.Float64() == b.Float64()))
	return nil
}

// NEQ pops two values from stack, compares (a != b), and pushes a boolean result.
func (o *BooleanOperations) NEQ(stack *Stack) error {
	b, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for neq: %w", err)
	}

	a, err := stack.Pop()
	if err != nil {
		return fmt.Errorf("insufficient operands for neq: %w", err)
	}

	stack.Push(NewFloatFromBool(a.Float64() != b.Float64()))
	return nil
}