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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
package subex
import (
"main/walk"
"errors"
"strconv"
)
func sumValues(values []walk.Value) ([]walk.Value, error) {
allBools := true
var sum float64 = 0
var any bool = false
for _, value := range values {
switch v := value.(type) {
case walk.NullValue:
allBools = false
case walk.BoolValue:
if v {
sum += 1
any = true
}
case walk.NumberValue:
allBools = false
sum += float64(v)
case walk.StringValue:
allBools = false
num, err := strconv.ParseFloat(string(v), 64)
if err == nil {
sum += num
} else {
return nil, errors.New("Tried to sum non-castable string")
}
default:
return nil, errors.New("Tried to sum non-number")
}
}
if allBools {
return []walk.Value{walk.BoolValue(any)}, nil
} else {
return []walk.Value{walk.NumberValue(sum)}, nil
}
}
// Compounds atoms into values, if all values are booleans, does AND, if not, tries to cast to numbers and multiply
func multiplyValues(values []walk.Value) ([]walk.Value, error) {
allBools := true
var product float64 = 1
var all bool = false
for _, value := range values {
switch v := value.(type) {
case walk.NullValue:
allBools = false
product *= 0
case walk.BoolValue:
if !v {
product *= 0
all = false
}
case walk.NumberValue:
allBools = false
product *= float64(v)
case walk.StringValue:
allBools = false
num, err := strconv.ParseFloat(string(v), 64)
if err == nil {
product *= num
} else {
return nil, errors.New("Tried to multiply non-castable string")
}
default:
return nil, errors.New("Tried to multiply non-number")
}
}
if allBools {
return []walk.Value{walk.BoolValue(all)}, nil
} else {
return []walk.Value{walk.NumberValue(product)}, nil
}
}
// Does tries to cast all to numbers and negates them
func negateValues(values []walk.Value) ([]walk.Value, error) {
var negatedNumbers []walk.Value
for _, value := range values {
switch v := value.(type) {
case walk.NullValue:
negatedNumbers = append(negatedNumbers, walk.NumberValue(0))
case walk.BoolValue:
if v {
negatedNumbers = append(negatedNumbers, walk.NumberValue(-1))
} else {
negatedNumbers = append(negatedNumbers, walk.NumberValue(0))
}
case walk.NumberValue:
negatedNumbers = append(negatedNumbers, walk.NumberValue(-float64(v)))
case walk.StringValue:
num, err := strconv.ParseFloat(string(v), 64)
if err == nil {
negatedNumbers = append(negatedNumbers, walk.NumberValue(-num))
} else {
return nil, errors.New("Tried to negate non-castable string")
}
default:
return nil, errors.New("Tried to negate non-number")
}
}
return negatedNumbers, nil
}
// If all are castable to numbers, takes reciprocals of all and returns them
// Else errors
func reciprocalValues(values []walk.Value) ([]walk.Value, error) {
var reciprocals []walk.Value
for _, value := range values {
switch v := value.(type) {
case walk.NullValue:
return nil, errors.New("Tried to take reciprocal of null")
case walk.BoolValue:
if v {
reciprocals = append(reciprocals, walk.NumberValue(1))
} else {
return nil, errors.New("Tried to take reciprocal of false")
}
case walk.NumberValue:
reciprocals = append(reciprocals, walk.NumberValue(1 / float64(v)))
case walk.StringValue:
num, err := strconv.ParseFloat(string(v), 64)
if err == nil {
reciprocals = append(reciprocals, walk.NumberValue(1 / num))
} else {
return nil, errors.New("Tried to take reciprocal of non-castable string")
}
default:
return nil, errors.New("Tried to take reciprocal of non-number")
}
}
return reciprocals, nil
}
// If all are castable to booleans, NOTs all and returns them
// Else errors
func notValues(values []walk.Value) (notted []walk.Value, err error) {
for _, value := range values {
switch v := value.(type) {
case walk.NullValue:
notted = append(notted, walk.BoolValue(true))
case walk.BoolValue:
notted = append(notted, walk.BoolValue(!bool(v)))
case walk.NumberValue:
notted = append(notted, walk.BoolValue(v == 0))
case walk.StringValue:
notted = append(notted, walk.BoolValue(len(v) == 0))
default:
return nil, errors.New("Tried to NOT non-boolean")
}
}
return notted, nil
}
// Returns true if all values are equal, false if not
func equalValues(values []walk.Value) ([]walk.Value, error) {
if len(values) == 0 {
return []walk.Value{walk.BoolValue(true)}, nil
}
first := values[0]
for _, value := range values[1:] {
// TODO: Refine the equality check
if value != first {
return []walk.Value{walk.BoolValue(false)}, nil
}
}
return []walk.Value{walk.BoolValue(true)}, nil
}
|