diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-19 12:41:10 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-19 12:41:10 +0100 |
commit | 053a403a77e5b4c46e82932e94d5fb7a4117ce43 (patch) | |
tree | 377270ecbb72054e8e712a520e0d6806831700cb | |
parent | 61fd31368f26637a353cde6402b9a353b6b82e8b (diff) | |
download | stred-go-053a403a77e5b4c46e82932e94d5fb7a4117ce43.tar |
Adjusts the sum operator to act as boolean OR when all inputs are booleans
-rw-r--r-- | subex/subexstate.go | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/subex/subexstate.go b/subex/subexstate.go index dbc8340..0a4f1bb 100644 --- a/subex/subexstate.go +++ b/subex/subexstate.go @@ -174,33 +174,43 @@ func (state SubexRangeState) accepting(store Store, outputStack OutputStack) []O return nil } -func sumValues(atoms []walk.Atom) (walk.ValueNumber, error) { +func sumValues(atoms []walk.Atom) (walk.WalkValue, error) { + allBools := true var sum float64 = 0 + var any bool = false values, err := walk.MemoryCompound(atoms) if err != nil { - return 0, err + return walk.ValueNull{}, 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 0, errors.New("Tried to sum non-castable string") + return walk.ValueNull{}, errors.New("Tried to sum non-castable string") } default: - return 0, errors.New("Tried to sum non-number") + return walk.ValueNull{}, errors.New("Tried to sum non-number") } } - return walk.ValueNumber(sum), nil + if allBools { + return walk.ValueBool(any), nil + } else { + return walk.ValueNumber(sum), nil + } } // At the start of a sum, just pushes to the OutputStack allowing the end to capture what was output in the middle |