<- 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:10:24 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2023-04-19 13:10:24 +0100
commitb48dcb0d37bd3db854927df25e6ff41d07501026 (patch)
treeae2059a0f9a66d48c9bdf3cf41c367e5c0848ad6 /subex/arithmetic.go
parent0072e6aad2f9969348d5315a692f1f5e2ebc075d (diff)
downloadstred-go-b48dcb0d37bd3db854927df25e6ff41d07501026.tar
Combines sum and product into an arithmetic state that contains a function for it's operation
Creates arithmetic.go which will house all of these functions
Diffstat (limited to 'subex/arithmetic.go')
-rw-r--r--subex/arithmetic.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/subex/arithmetic.go b/subex/arithmetic.go
new file mode 100644
index 0000000..7200ac7
--- /dev/null
+++ b/subex/arithmetic.go
@@ -0,0 +1,87 @@
+package subex
+
+import (
+ "main/walk"
+ "errors"
+ "strconv"
+)
+
+func sumValues(atoms []walk.Atom) ([]walk.Atom, error) {
+ allBools := true
+ var sum float64 = 0
+ var any bool = false
+ values, err := walk.MemoryCompound(atoms)
+ if err != nil {
+ return nil, err
+ }
+ for _, value := range values {
+ switch v := value.(type) {
+ case walk.ValueNull:
+ allBools = false
+ case walk.ValueBool:
+ if bool(v) {
+ sum += 1
+ any = true
+ }
+ case walk.ValueNumber:
+ allBools = false
+ sum += float64(v)
+ case walk.ValueString:
+ 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.Atom{walk.ValueBool(any)}, nil
+ } else {
+ return []walk.Atom{walk.ValueNumber(sum)}, nil
+ }
+}
+
+// Compounds atoms into values, if all values are booleans, does AND, if not, tries to cast to numbers and multiply
+func multiplyValues(atoms []walk.Atom) ([]walk.Atom, error) {
+ allBools := true
+ var product float64 = 1
+ var all bool = false
+ values, err := walk.MemoryCompound(atoms)
+ if err != nil {
+ return nil, err
+ }
+ for _, value := range values {
+ switch v := value.(type) {
+ case walk.ValueNull:
+ allBools = false
+ product *= 0
+ case walk.ValueBool:
+ if !bool(v) {
+ product *= 0
+ all = false
+ }
+ case walk.ValueNumber:
+ allBools = false
+ product *= float64(v)
+ case walk.ValueString:
+ allBools = false
+ num, err := strconv.ParseFloat(string(v), 64)
+ if err == nil {
+ product *= 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.Atom{walk.ValueBool(all)}, nil
+ } else {
+ return []walk.Atom{walk.ValueNumber(product)}, nil
+ }
+}