<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/subex/filter.go
blob: 87c83a42cbf9247299f3614c568a180409dbffa3 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package subex

import (
	"main/walk"
	"math"
	"fmt"
)

type valueFilter interface {
	valueFilter(value walk.Value) bool
}

type selectScalarFilter struct {
	scalar walk.Scalar
}
func (scalar selectScalarFilter) valueFilter(value walk.Value) bool {
	return value == scalar.scalar
}

type anyNumberFilter struct {}
func (_ anyNumberFilter) valueFilter(value walk.Value) bool {
	_, isNumber := value.(walk.NumberValue)
	return isNumber
}

type anyBoolFilter struct {}
func (_ anyBoolFilter) valueFilter(value walk.Value) bool {
	_, isBool := value.(walk.BoolValue)
	return isBool
}

type simpleValueFilter struct {}
func (_ simpleValueFilter) valueFilter(value walk.Value) bool {
	switch value := value.(type) {
	case walk.NullValue:
		return true
	case walk.BoolValue:
		return true
	case walk.NumberValue:
		return true
	case walk.StringValue:
		return true
	case walk.ArrayValue:
		return len(value) == 0
	case walk.MapValue:
		return len(value) == 0
	default:
		panic("Invalid value type")
	}
}

type anyValueFilter struct {}
func (_ anyValueFilter) valueFilter(value walk.Value) bool {
	return true
}

type anyArrayFilter struct {}
func (_ anyArrayFilter) valueFilter(value walk.Value) bool {
	_, isArray := value.(walk.ArrayValue)
	return isArray
}

type anyMapFilter struct {}
func (_ anyMapFilter) valueFilter(value walk.Value) bool {
	_, isMap := value.(walk.MapValue)
	return isMap
}

type anyStringFilter struct {}
func (_ anyStringFilter) valueFilter(value walk.Value) bool {
	_, isString := value.(walk.StringValue)
	return isString
}


type runeFilter interface {
	runeFilter(r rune) bool
}

type anyRuneFilter struct {}
func (_ anyRuneFilter) runeFilter(r rune) bool {
	return true
}

type selectRuneFilter struct {
	r rune
}
func (f selectRuneFilter) runeFilter(r rune) bool {
	return f.r == r
}

type numberFilter interface {
	add(m float64) numberFilter
	multiply(m float64) numberFilter
	numberFilter(n float64) bool
}

func (_ anyNumberFilter) numberFilter(n float64) bool {
	return true
}
func (_ anyNumberFilter) add(m float64) numberFilter {
	return anyNumberFilter{}
}
func (_ anyNumberFilter) multiply(m float64) numberFilter {
	if m == 0.0 {
		return equalNumberFilter {0.0}
	} else {
		return anyNumberFilter{}
	}
}
func (_ anyNumberFilter) String() string {
	return "r"
}

type divisibleNumberFilter struct {
	divisor float64
	target float64
}
func (d divisibleNumberFilter) numberFilter(n float64) bool {
	mod := math.Mod(n, d.divisor)
	if mod < 0 {
		mod += d.divisor
	}
	return mod == d.target
}
func (d divisibleNumberFilter) add(m float64) numberFilter {
	mod := math.Mod(m + d.target, d.divisor)
	if mod < 0 {
		mod += d.divisor
	}
	return divisibleNumberFilter {
		divisor: d.divisor,
		target: mod,
	}
}
func (d divisibleNumberFilter) multiply(m float64) numberFilter {
	if m == 0.0 {
		return equalNumberFilter {0.0}
	}

	target := d.target
	if m < 0 {
		target = d.divisor - target
		m = -m
	}

	return divisibleNumberFilter {
		divisor: d.divisor * m,
		target: target * m,
	}
}
func (d divisibleNumberFilter) String() string {
	return fmt.Sprintf("(x %% %v == %v)", d.divisor, d.target)
}

type andNumberFilter struct {
	lhs, rhs numberFilter
}
func (a andNumberFilter) numberFilter(n float64) bool {
	return a.lhs.numberFilter(n) && a.rhs.numberFilter(n)
}
func (a andNumberFilter) add(m float64) numberFilter {
	return andNumberFilter {
		lhs: a.lhs.add(m),
		rhs: a.rhs.add(m),
	}
}
func (a andNumberFilter) multiply(m float64) numberFilter {
	return andNumberFilter {
		lhs: a.lhs.multiply(m),
		rhs: a.rhs.multiply(m),
	}
}
func (a andNumberFilter) String() string {
	return fmt.Sprintf("(%v && %v)", a.lhs, a.rhs)
}

type orNumberFilter struct {
	lhs, rhs numberFilter
}
func (o orNumberFilter) numberFilter(n float64) bool {
	return o.lhs.numberFilter(n) || o.rhs.numberFilter(n)
}
func (o orNumberFilter) String() string {
	return fmt.Sprintf("(%v || %v)", o.lhs, o.rhs)
}

type notNumberFilter struct {
	operand numberFilter
}
func (no notNumberFilter) numberFilter(n float64) bool {
	return !no.operand.numberFilter(n)
}
func (no notNumberFilter) add(m float64) numberFilter {
	return notNumberFilter {no.operand.add(m)}
}
func (no notNumberFilter) multiply(m float64) numberFilter {
	return notNumberFilter {no.operand.multiply(m)}
}
func (no notNumberFilter) String() string {
	return fmt.Sprintf("(!%v)", no.operand)
}

type lessThanNumberFilter struct {
	rhs float64
}
func (l lessThanNumberFilter) numberFilter(n float64) bool {
	return n < l.rhs
}
func (l lessThanNumberFilter) add(m float64) numberFilter {
	return lessThanNumberFilter {l.rhs + m}
}
func (l lessThanNumberFilter) multiply(m float64) numberFilter {
	if m > 0 {
		return lessThanNumberFilter {l.rhs * m}
	} else if m < 0 {
		return greaterThanNumberFilter {l.rhs * m}
	} else {
		return equalNumberFilter {0}
	}
}
func (l lessThanNumberFilter) String() string {
	return fmt.Sprintf("(x < %v)", l.rhs)
}

type greaterThanNumberFilter struct {
	rhs float64
}
func (g greaterThanNumberFilter) numberFilter(n float64) bool {
	return n > g.rhs
}
func (g greaterThanNumberFilter) add(m float64) numberFilter {
	return greaterThanNumberFilter {g.rhs + m}
}
func (g greaterThanNumberFilter) multiply(m float64) numberFilter {
	if m > 0 {
		return greaterThanNumberFilter {g.rhs * m}
	} else if m < 0 {
		return lessThanNumberFilter {g.rhs * m}
	} else {
		return equalNumberFilter {0}
	}
}
func (g greaterThanNumberFilter) String() string {
	return fmt.Sprintf("(x > %v)", g.rhs)
}

type equalNumberFilter struct {
	rhs float64
}
func (e equalNumberFilter) numberFilter(n float64) bool {
	return n == e.rhs
}
func (e equalNumberFilter) add(m float64) numberFilter {
	return equalNumberFilter {e.rhs + m}
}
func (e equalNumberFilter) multiply(m float64) numberFilter {
	return equalNumberFilter {e.rhs * m}
}