This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
astbool.go
144 lines (127 loc) · 2.71 KB
/
astbool.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import "fmt"
type boolNode interface {
getBool() (bool, error)
execute() (interface{}, error)
}
type boolLeaf struct {
val bool
}
func (l boolLeaf) getBool() (bool, error) {
return l.val, nil
}
func (l boolLeaf) execute() (interface{}, error) {
return l.val, nil
}
type equalityNode struct {
op string
left node
right node
}
func (n *equalityNode) getBool() (bool, error) {
left, err := n.left.execute()
if err != nil {
return false, err
}
right, err := n.right.execute()
if err != nil {
return false, err
}
switch n.op {
case "==":
return left == right, nil
case "!=":
return left != right, nil
}
return false, fmt.Errorf("Invalid equality node operator : " + n.op)
}
func (n *equalityNode) execute() (interface{}, error) {
return n.getBool()
}
type comparatorNode struct {
op string
left node
right node
}
func (n *comparatorNode) getBool() (bool, error) {
left, err := n.left.execute()
if err != nil {
return false, err
}
leftNum, err := getFloat(left)
if err != nil {
return false, fmt.Errorf("left expression should return a number")
}
right, err := n.right.execute()
if err != nil {
return false, err
}
rightNum, err := getFloat(right)
if err != nil {
return false, fmt.Errorf("right expression should return a number")
}
switch n.op {
case "<":
return leftNum < rightNum, nil
case "<=":
return leftNum <= rightNum, nil
case ">":
return leftNum > rightNum, nil
case ">=":
return leftNum >= rightNum, nil
}
return false, fmt.Errorf("Invalid comparison operator : " + n.op)
}
func (n *comparatorNode) execute() (interface{}, error) {
return n.getBool()
}
type logicalNode struct {
op string
left node
right node
}
func (n *logicalNode) getBool() (bool, error) {
left, err := n.left.execute()
if err != nil {
return false, err
}
leftBool, ok := left.(bool)
if !ok {
return false, fmt.Errorf("Left expression should return a boolean")
}
right, err := n.right.execute()
if err != nil {
return false, err
}
rightBool, ok := right.(bool)
if !ok {
return false, fmt.Errorf("Right expression should return a boolean")
}
switch n.op {
case "||":
return leftBool || rightBool, nil
case "&&":
return leftBool && rightBool, nil
}
return false, fmt.Errorf("Invalid logical operator : " + n.op)
}
func (n *logicalNode) execute() (interface{}, error) {
return n.getBool()
}
type negateBoolNode struct {
expr node
}
func (n *negateBoolNode) getBool() (bool, error) {
val, err := n.expr.execute()
if err != nil {
return false, err
}
b, ok := val.(bool)
if !ok {
return false, fmt.Errorf("Expression should return a boolean to be negated")
}
return !b, nil
}
func (n *negateBoolNode) execute() (interface{}, error) {
return n.getBool()
}