<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/subex/arithmetic.go
blob: 9e5e53023f1195ac0373112a7b2a9f63d69ce71f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package subex

import (
	"main/walk"
	"errors"
	"strconv"
)

func sumValues(values walk.ValueList) (walk.ValueList, error) {
	allBools := true
	var sum float64 = 0
	var any bool = false
	for _, value := range values {
		switch v := value.(type) {
			case walk.NullScalar:
				allBools = false
			case walk.BoolScalar:
				if v {
					sum += 1
					any = true
				}
			case walk.NumberScalar:
				allBools = false
				sum += float64(v)
			case walk.StringStructure:
				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.ValueList{walk.BoolScalar(any)}, nil
	} else {
		return walk.ValueList{walk.NumberScalar(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) {
	allBools := true
	var product float64 = 1
	var all bool = false
	for _, value := range values {
		switch v := value.(type) {
			case walk.NullScalar:
				allBools = false
				product *= 0
			case walk.BoolScalar:
				if !v {
					product *= 0
					all = false
				}
			case walk.NumberScalar:
				allBools = false
				product *= float64(v)
			case walk.StringStructure:
				allBools = false
				num, err := strconv.ParseFloat(string(v), 64)
				if err == nil {
					product *= num
				} else {
					return nil, errors.New("Tried to multiply non-castable string")
				}
			default:
				return nil, errors.New("Tried to multiply non-number")
		}
	}
	if allBools {
		return walk.ValueList{walk.BoolScalar(all)}, nil
	} else {
		return walk.ValueList{walk.NumberScalar(product)}, nil
	}
}

// Does tries to cast all to numbers and negates them
func negateValues(values walk.ValueList) (walk.ValueList, error) {
	var negatedNumbers walk.ValueList
	for _, value := range values {
		switch v := value.(type) {
			case walk.NullScalar:
				negatedNumbers = append(negatedNumbers, walk.NumberScalar(0))
			case walk.BoolScalar:
				if v {
					negatedNumbers = append(negatedNumbers, walk.NumberScalar(-1))
				} else {
					negatedNumbers = append(negatedNumbers, walk.NumberScalar(0))
				}
			case walk.NumberScalar:
				negatedNumbers = append(negatedNumbers, walk.NumberScalar(-float64(v)))
			case walk.StringStructure:
				num, err := strconv.ParseFloat(string(v), 64)
				if err == nil {
					negatedNumbers = append(negatedNumbers, walk.NumberScalar(-num))
				} else {
					return nil, errors.New("Tried to negate non-castable string")
				}
			default:
				return nil, errors.New("Tried to negate non-number")
		}
	}
	return negatedNumbers, nil
}

// 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
	for _, value := range values {
		switch v := value.(type) {
			case walk.NullScalar:
				return nil, errors.New("Tried to take reciprocal of null")
			case walk.BoolScalar:
				if v {
					reciprocals = append(reciprocals, walk.NumberScalar(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:
				num, err := strconv.ParseFloat(string(v), 64)
				if err == nil {
					reciprocals = append(reciprocals, walk.NumberScalar(1 / num))
				} else {
					return nil, errors.New("Tried to take reciprocal of non-castable string")
				}
			default:
				return nil, errors.New("Tried to take reciprocal of non-number")
		}
	}
	return reciprocals, nil
}

// If all are castable to booleans, NOTs all and returns them
// Else errors
func notValues(values walk.ValueList) (notted walk.ValueList, 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))
			default:
				return nil, errors.New("Tried to NOT non-boolean")
		}
	}
	return notted, nil
}