diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-21 16:22:16 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-21 16:22:16 +0100 |
commit | 12c1d179f32c38a929fcc9adb326a9f44c8288ae (patch) | |
tree | d685922ed93708cbaf01357a95989283a7b86443 /subex/arithmetic.go | |
parent | 5d8582711936cae3c42f2645d0f304418b17fb7e (diff) | |
download | stred-go-12c1d179f32c38a929fcc9adb326a9f44c8288ae.tar |
Replaces the interfaces implementation of Atom with a tagged union based implementation
Diffstat (limited to 'subex/arithmetic.go')
-rw-r--r-- | subex/arithmetic.go | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/subex/arithmetic.go b/subex/arithmetic.go index a7dc73a..1ebd1a6 100644 --- a/subex/arithmetic.go +++ b/subex/arithmetic.go @@ -39,9 +39,9 @@ func sumValues(atoms []walk.Atom) ([]walk.Atom, error) { } } if allBools { - return []walk.Atom{walk.ValueBool(any)}, nil + return []walk.Atom{walk.NewAtomBool(any)}, nil } else { - return []walk.Atom{walk.ValueNumber(sum)}, nil + return []walk.Atom{walk.NewAtomNumber(sum)}, nil } } @@ -80,9 +80,9 @@ func multiplyValues(atoms []walk.Atom) ([]walk.Atom, error) { } } if allBools { - return []walk.Atom{walk.ValueBool(all)}, nil + return []walk.Atom{walk.NewAtomBool(all)}, nil } else { - return []walk.Atom{walk.ValueNumber(product)}, nil + return []walk.Atom{walk.NewAtomNumber(product)}, nil } } @@ -96,19 +96,19 @@ func negateValues(atoms []walk.Atom) ([]walk.Atom, error) { for _, value := range values { switch v := value.(type) { case walk.ValueNull: - negatedNumbers = append(negatedNumbers, walk.ValueNumber(0)) + negatedNumbers = append(negatedNumbers, walk.NewAtomNumber(0)) case walk.ValueBool: if bool(v) { - negatedNumbers = append(negatedNumbers, walk.ValueNumber(-1)) + negatedNumbers = append(negatedNumbers, walk.NewAtomNumber(-1)) } else { - negatedNumbers = append(negatedNumbers, walk.ValueNumber(0)) + negatedNumbers = append(negatedNumbers, walk.NewAtomNumber(0)) } case walk.ValueNumber: - negatedNumbers = append(negatedNumbers, walk.ValueNumber(-v)) + negatedNumbers = append(negatedNumbers, walk.NewAtomNumber(-float64(v))) case walk.ValueString: num, err := strconv.ParseFloat(string(v), 64) if err == nil { - negatedNumbers = append(negatedNumbers, walk.ValueNumber(-num)) + negatedNumbers = append(negatedNumbers, walk.NewAtomNumber(-num)) } else { return nil, errors.New("Tried to negate non-castable string") } @@ -133,16 +133,16 @@ func reciprocalValues(atoms []walk.Atom) ([]walk.Atom, error) { return nil, errors.New("Tried to take reciprocal of null") case walk.ValueBool: if bool(v) { - reciprocals = append(reciprocals, walk.ValueNumber(1)) + reciprocals = append(reciprocals, walk.NewAtomNumber(1)) } else { return nil, errors.New("Tried to take reciprocal of false") } case walk.ValueNumber: - reciprocals = append(reciprocals, walk.ValueNumber(1 / v)) + reciprocals = append(reciprocals, walk.NewAtomNumber(1 / float64(v))) case walk.ValueString: num, err := strconv.ParseFloat(string(v), 64) if err == nil { - reciprocals = append(reciprocals, walk.ValueNumber(1 / num)) + reciprocals = append(reciprocals, walk.NewAtomNumber(1 / num)) } else { return nil, errors.New("Tried to take reciprocal of non-castable string") } @@ -163,13 +163,13 @@ func notValues(atoms []walk.Atom) (notted []walk.Atom, err error) { for _, value := range values { switch v := value.(type) { case walk.ValueNull: - notted = append(notted, walk.ValueBool(true)) + notted = append(notted, walk.NewAtomBool(true)) case walk.ValueBool: - notted = append(notted, walk.ValueBool(!v)) + notted = append(notted, walk.NewAtomBool(!bool(v))) case walk.ValueNumber: - notted = append(notted, walk.ValueBool(v == 0)) + notted = append(notted, walk.NewAtomBool(v == 0)) case walk.ValueString: - notted = append(notted, walk.ValueBool(len(v) == 0)) + notted = append(notted, walk.NewAtomBool(len(v) == 0)) default: return nil, errors.New("Tried to NOT non-boolean") } |