diff options
Diffstat (limited to 'subex/arithmetic.go')
-rw-r--r-- | subex/arithmetic.go | 94 |
1 files changed, 47 insertions, 47 deletions
diff --git a/subex/arithmetic.go b/subex/arithmetic.go index 4cbc9db..4c87d5f 100644 --- a/subex/arithmetic.go +++ b/subex/arithmetic.go @@ -6,23 +6,23 @@ import ( "strconv" ) -func sumValues(values walk.ValueList) (walk.ValueList, error) { +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.NullScalar: + case walk.NullValue: allBools = false - case walk.BoolScalar: + case walk.BoolValue: if v { sum += 1 any = true } - case walk.NumberScalar: + case walk.NumberValue: allBools = false sum += float64(v) - case walk.StringStructure: + case walk.StringValue: allBools = false num, err := strconv.ParseFloat(string(v), 64) if err == nil { @@ -35,31 +35,31 @@ func sumValues(values walk.ValueList) (walk.ValueList, error) { } } if allBools { - return walk.ValueList{walk.BoolScalar(any)}, nil + return []walk.Value{walk.BoolValue(any)}, nil } else { - return walk.ValueList{walk.NumberScalar(sum)}, nil + 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.ValueList) (walk.ValueList, error) { +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.NullScalar: + case walk.NullValue: allBools = false product *= 0 - case walk.BoolScalar: + case walk.BoolValue: if !v { product *= 0 all = false } - case walk.NumberScalar: + case walk.NumberValue: allBools = false product *= float64(v) - case walk.StringStructure: + case walk.StringValue: allBools = false num, err := strconv.ParseFloat(string(v), 64) if err == nil { @@ -72,31 +72,31 @@ func multiplyValues(values walk.ValueList) (walk.ValueList, error) { } } if allBools { - return walk.ValueList{walk.BoolScalar(all)}, nil + return []walk.Value{walk.BoolValue(all)}, nil } else { - return walk.ValueList{walk.NumberScalar(product)}, nil + return []walk.Value{walk.NumberValue(product)}, nil } } // Does tries to cast all to numbers and negates them -func negateValues(values walk.ValueList) (walk.ValueList, error) { - var negatedNumbers walk.ValueList +func negateValues(values []walk.Value) ([]walk.Value, error) { + var negatedNumbers []walk.Value for _, value := range values { switch v := value.(type) { - case walk.NullScalar: - negatedNumbers = append(negatedNumbers, walk.NumberScalar(0)) - case walk.BoolScalar: + case walk.NullValue: + negatedNumbers = append(negatedNumbers, walk.NumberValue(0)) + case walk.BoolValue: if v { - negatedNumbers = append(negatedNumbers, walk.NumberScalar(-1)) + negatedNumbers = append(negatedNumbers, walk.NumberValue(-1)) } else { - negatedNumbers = append(negatedNumbers, walk.NumberScalar(0)) + negatedNumbers = append(negatedNumbers, walk.NumberValue(0)) } - case walk.NumberScalar: - negatedNumbers = append(negatedNumbers, walk.NumberScalar(-float64(v))) - case walk.StringStructure: + 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.NumberScalar(-num)) + negatedNumbers = append(negatedNumbers, walk.NumberValue(-num)) } else { return nil, errors.New("Tried to negate non-castable string") } @@ -109,24 +109,24 @@ func negateValues(values walk.ValueList) (walk.ValueList, error) { // If all are castable to numbers, takes reciprocals of all and returns them // Else errors -func reciprocalValues(values walk.ValueList) (walk.ValueList, error) { - var reciprocals walk.ValueList +func reciprocalValues(values []walk.Value) ([]walk.Value, error) { + var reciprocals []walk.Value for _, value := range values { switch v := value.(type) { - case walk.NullScalar: + case walk.NullValue: return nil, errors.New("Tried to take reciprocal of null") - case walk.BoolScalar: + case walk.BoolValue: if v { - reciprocals = append(reciprocals, walk.NumberScalar(1)) + reciprocals = append(reciprocals, walk.NumberValue(1)) } else { return nil, errors.New("Tried to take reciprocal of false") } - case walk.NumberScalar: - reciprocals = append(reciprocals, walk.NumberScalar(1 / float64(v))) - case walk.StringStructure: + 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.NumberScalar(1 / num)) + reciprocals = append(reciprocals, walk.NumberValue(1 / num)) } else { return nil, errors.New("Tried to take reciprocal of non-castable string") } @@ -139,17 +139,17 @@ func reciprocalValues(values walk.ValueList) (walk.ValueList, error) { // If all are castable to booleans, NOTs all and returns them // Else errors -func notValues(values walk.ValueList) (notted walk.ValueList, err error) { +func notValues(values []walk.Value) (notted []walk.Value, err error) { for _, value := range values { switch v := value.(type) { - case walk.NullScalar: - notted = append(notted, walk.BoolScalar(true)) - case walk.BoolScalar: - notted = append(notted, walk.BoolScalar(!bool(v))) - case walk.NumberScalar: - notted = append(notted, walk.BoolScalar(v == 0)) - case walk.StringStructure: - notted = append(notted, walk.BoolScalar(len(v) == 0)) + 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") } @@ -158,16 +158,16 @@ func notValues(values walk.ValueList) (notted walk.ValueList, err error) { } // Returns true if all values are equal, false if not -func equalValues(values walk.ValueList) (walk.ValueList, error) { +func equalValues(values []walk.Value) ([]walk.Value, error) { if len(values) == 0 { - return walk.ValueList{walk.BoolScalar(true)}, nil + 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.ValueList{walk.BoolScalar(false)}, nil + return []walk.Value{walk.BoolValue(false)}, nil } } - return walk.ValueList{walk.BoolScalar(true)}, nil + return []walk.Value{walk.BoolValue(true)}, nil } |