<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/subex/arithmetic.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2023-04-19 13:58:13 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2023-04-19 13:58:13 +0100
commitca161b26e9b6a253837e5ec4e0cf318bd0ee7903 (patch)
treec3863c145024fd1124943e1eab525230c3236aaa /subex/arithmetic.go
parentd3d17484012dc603ae326bec419cff990898e6a0 (diff)
downloadstred-go-ca161b26e9b6a253837e5ec4e0cf318bd0ee7903.tar
Adds the NOT operator
Diffstat (limited to 'subex/arithmetic.go')
-rw-r--r--subex/arithmetic.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/subex/arithmetic.go b/subex/arithmetic.go
index 4404a1c..52f576d 100644
--- a/subex/arithmetic.go
+++ b/subex/arithmetic.go
@@ -152,3 +152,27 @@ func reciprocalValues(atoms []walk.Atom) ([]walk.Atom, error) {
}
return reciprocals, nil
}
+
+// If all are castable to booleans, NOTs all and returns them
+// Else errors
+func notValues(atoms []walk.Atom) (notted []walk.Atom, err error) {
+ values, err := walk.MemoryCompound(atoms)
+ if err != nil {
+ return nil, err
+ }
+ for _, value := range values {
+ switch v := value.(type) {
+ case walk.ValueNull:
+ notted = append(notted, walk.ValueBool(true))
+ case walk.ValueBool:
+ notted = append(notted, walk.ValueBool(!v))
+ case walk.ValueNumber:
+ notted = append(notted, walk.ValueBool(v == 0))
+ case walk.ValueString:
+ notted = append(notted, walk.ValueBool(len(v) == 0))
+ default:
+ return nil, errors.New("Tried to NOT non-boolean")
+ }
+ }
+ return notted, nil
+}