<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/subex/subexstate.go
blob: 3bcbdee3a40fbdc7312f3883da6a977aa95ab9d9 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package subex

// TODO: Simplify this implementation by combining similar states into one type
// e.g. Combine all of the copy states into a single type that has a filter function

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

type SubexState interface {
}

type SubexEpsilonState interface {
	SubexState
	epsilon(aux auxiliaryState) []SubexBranch
}

// A state of execution for the transducer
type SubexEatState interface {
	SubexState
	// Eat a Atom and transition to any number of new states
	eat(aux auxiliaryState, edible walk.Edible) []SubexBranch
	// Find accepting states reachable through epsilon transitions and return their outputs
	accepting(aux auxiliaryState) []OutputStack
}

// Try first, if it fails then try second
type SubexGroupState struct {
	first, second SubexState
}
func (state SubexGroupState) epsilon(aux auxiliaryState) []SubexBranch {
	otherAux := aux.cloneStore()
	return []SubexBranch {
		{
			state: state.first,
			aux: aux,
		},
		{
			state: state.second,
			aux: otherAux,
		},
	}
}
func (state SubexGroupState) String() string {
	return fmt.Sprintf("{%T %p, %T %p}", state.first, state.first, state.second, state.second)
}

type SubexCopyState struct {
	next SubexState
	filter valueFilter
}
func (state SubexCopyState) eat(aux auxiliaryState, edible walk.Edible) []SubexBranch {
	value, isValue := edible.(walk.Value)
	if !isValue || !state.filter.valueFilter(value) {
		return nil
	}
	return []SubexBranch{{
		state: state.next,
		aux: aux.topAppend([]walk.Value{value}),
	}}
}
func (state SubexCopyState) accepting(aux auxiliaryState) []OutputStack {
	return nil
}

type SubexCopyRuneState struct {
	next SubexState
	filter runeFilter
}
func (state SubexCopyRuneState) eat(aux auxiliaryState, edible walk.Edible) []SubexBranch {
	r, isRune := edible.(walk.RuneEdible)
	if !isRune || !state.filter.runeFilter(rune(r)) {
		return nil
	}
	return []SubexBranch{{
		state: state.next,
		aux: aux.topAppendRune([]rune{rune(r)}),
	}}
}
func (state SubexCopyRuneState) accepting(aux auxiliaryState) []OutputStack {
	return nil
}
func (state SubexCopyRuneState) String() string {
	return fmt.Sprintf("SubexCopyRuneState[%v]", state.filter)
}

type SubexCopyNumberState struct {
	next SubexState
	filter numberFilter
}
func (state SubexCopyNumberState) eat(aux auxiliaryState, edible walk.Edible) []SubexBranch {
	number, isNumber := edible.(walk.NumberValue)
	if !isNumber || !state.filter.numberFilter(float64(number)) {
		return nil
	}
	return []SubexBranch {{
		state: state.next,
		aux: aux.topAppend([]walk.Value {number}),
	}}
}
func (state SubexCopyNumberState) accepting(aux auxiliaryState) []OutputStack {
	return nil
}

// Just pushes to the OutputStack and hands over to the next state
// Used to capture the output of the state being handed over to
type SubexCaptureBeginState struct {
	next SubexState
}
func (state SubexCaptureBeginState) epsilon(aux auxiliaryState) []SubexBranch {
	return []SubexBranch {{
		state: state.next,
		aux: aux.pushOutput(nil),
	}}
}
func (state SubexCaptureBeginState) String() string {
	return "CaptureBeginState"
}

type SubexCaptureRunesBeginState struct {
	next SubexState
}
func (state SubexCaptureRunesBeginState) epsilon(aux auxiliaryState) []SubexBranch {
	return []SubexBranch {{
		state: state.next,
		aux: aux.pushOutputRunes(nil),
	}}
}

// Discard the top of the OutputStack
type SubexDiscardState struct {
	next SubexState
}
func (state SubexDiscardState) epsilon(aux auxiliaryState) []SubexBranch {
	return []SubexBranch {{
		state: state.next,
		aux: aux.popDiscardOutput(),
	}}
}

// Pop the top of the OutputStack which contains the stuff outputted since the start of the store
// This outputted data gets stored in a slot
type SubexStoreEndState struct {
	slot int
	next SubexState
}
func (state SubexStoreEndState) epsilon(aux auxiliaryState) []SubexBranch {
	toStore, aux := aux.popOutput()
	return []SubexBranch {{
		state: state.next,
		aux: aux.withValue(state.slot, toStore),
	}}
}

type SubexStoreRunesEndState struct {
	slot int
	next SubexState
}
func (state SubexStoreRunesEndState) epsilon(aux auxiliaryState) []SubexBranch {
	toStore, aux := aux.popOutputRunes()
	aux.store = aux.store.withRunes(state.slot, toStore)
	return []SubexBranch {{
		state: state.next,
		aux: aux,
	}}
}

type SubexOutputValueLiteralState struct {
	literal walk.Scalar
	next SubexState
}
func (state SubexOutputValueLiteralState) epsilon(aux auxiliaryState) []SubexBranch {
	return []SubexBranch {{
		state: state.next,
		aux: aux.topAppend([]walk.Value {state.literal}),
	}}
}

type SubexOutputValueLoadState struct {
	slot int
	next SubexState
}
func (state SubexOutputValueLoadState) epsilon(aux auxiliaryState) []SubexBranch {
	return []SubexBranch {{
		state: state.next,
		aux: aux.topAppend(aux.store.values[state.slot]),
	}}
}

type SubexOutputRuneLiteralState struct {
	literal rune
	next SubexState
}
func (state SubexOutputRuneLiteralState) epsilon(aux auxiliaryState) []SubexBranch {
	return []SubexBranch {{
		state: state.next,
		aux: aux.topAppendRune([]rune {state.literal}),
	}}
}

type SubexOutputRuneLoadState struct {
	slot int
	next SubexState
}
func (state SubexOutputRuneLoadState) epsilon(aux auxiliaryState) []SubexBranch {
	return []SubexBranch {{
		state: state.next,
		aux: aux.topAppendRune(aux.store.runes[state.slot]),
	}}
}

// A final state, transitions to nothing but is accepting
type SubexNoneState struct {}
func (state SubexNoneState) eat(aux auxiliaryState, edible walk.Edible) []SubexBranch {
	return nil
}
func (state SubexNoneState) accepting(aux auxiliaryState) []OutputStack {
	return []OutputStack{aux.outputStack}
}

// A dead end state, handy for making internals work nicer but technically redundant
type SubexDeadState struct {}
func (state SubexDeadState) eat(aux auxiliaryState, edible walk.Edible) []SubexBranch {
	return nil
}
func (state SubexDeadState) accepting (aux auxiliaryState) []OutputStack {
	return nil
}

// Read in an Atom and apply a map to generate an Atom to output
// If the input isn't in the map transition to nothing
// TODO
// type SubexRangeState struct {
// 	parts map[walk.Atom]walk.Atom
// 	next SubexState
// }
// func (state SubexRangeState) eat(aux auxiliaryState, char walk.Atom) []SubexBranch {
// 	out, exists := state.parts[char]
// 	if !exists {
// 		return nil
// 	} else {
// 		return []SubexBranch{{
// 			state: state.next,
// 			outputStack: topAppend(outputStack, []walk.Atom{out}),
// 			store: store,
// 		}}
// 	}
// }
// func (state SubexRangeState) accepting(aux auxiliaryState) []OutputStack {
// 	return nil
// }


type SubexArithmeticEndState struct {
	next SubexState
	calculate func([]walk.Value) ([]walk.Value, error)
}
func (state SubexArithmeticEndState) epsilon(aux auxiliaryState) []SubexBranch {
	values, aux := aux.popOutput()
	result, err := state.calculate(values)
	if err != nil {
		return nil
	}
	return []SubexBranch {{
		state: state.next,
		aux: aux.topAppend(result),
	}}
}

type SubexDiscardTerminalState struct {
	terminal walk.Terminal
	next SubexState
}
func (state SubexDiscardTerminalState) eat(aux auxiliaryState, edible walk.Edible) []SubexBranch {
	if edible != state.terminal {
		return nil
	}
	return []SubexBranch{{
		state: state.next,
		aux: aux,
	}}
}
func (state SubexDiscardTerminalState) accepting(aux auxiliaryState) []OutputStack {
	return nil
}

type SubexConstructArrayState struct {
	next SubexState
}
func (state SubexConstructArrayState) epsilon(aux auxiliaryState) []SubexBranch {
	values, aux := aux.popOutput()
	var array walk.ArrayValue
	if len(values) % 2 != 0 {
		panic("Tried to construct array with odd length input")
	}
	for i := 0; i < len(values); i += 2 {
		index, isNum := values[i].(walk.NumberValue)
		if !isNum {
			panic("Tried to construct array with non-numeric index")
		}
		array = append(array, walk.ArrayElement {
			Index: int(index),
			Value: values[i + 1],
		})
	}
	return []SubexBranch {{
		state: state.next,
		aux: aux.topAppend([]walk.Value {array}),
	}}
}

type SubexConstructArrayValuesState struct {
	next SubexState
}
func (state SubexConstructArrayValuesState) epsilon(aux auxiliaryState) []SubexBranch {
	values, aux := aux.popOutput()
	var array walk.ArrayValue
	for _, v := range values {
		array = append(array, walk.ArrayElement {
			Index: 0,
			Value: v,
		})
	}
	return []SubexBranch {{
		state: state.next,
		aux: aux.topAppend([]walk.Value {array}),
	}}
}

type SubexConstructMapState struct {
	next SubexState
}
func (state SubexConstructMapState) epsilon(aux auxiliaryState) []SubexBranch {
	values, aux := aux.popOutput()
	var m walk.MapValue
	if len(values) % 2 != 0 {
		panic("Tried to construct array with odd length input")
	}
	for i := 0; i < len(values); i += 2 {
		key, isNum := values[i].(walk.StringValue)
		if !isNum {
			panic("Tried to construct array with non-numeric index")
		}
		m = append(m, walk.MapElement {
			Key: string(key),
			Value: values[i + 1],
		})
	}
	return []SubexBranch {{
		state: state.next,
		aux: aux.topAppend([]walk.Value {m}),
	}}
}

type SubexConstructStringState struct {
	next SubexState
}
func (state SubexConstructStringState) construct(runes []rune) []walk.Value {
	var builder strings.Builder
	for _, r := range runes {
		builder.WriteRune(r)
	}
	return []walk.Value{walk.StringValue(builder.String())}
}
func (state SubexConstructStringState) epsilon(aux auxiliaryState) []SubexBranch {
	runes, aux := aux.popOutputRunes()
	return []SubexBranch {{
		state: state.next,
		aux: aux.topAppend(state.construct(runes)),
	}}
}
func (state SubexConstructStringState) String() string {
	return "SubexConstructStringState"
}

type SubexIncrementNestState struct {
	keys bool
	next SubexState
}
func (state SubexIncrementNestState) epsilon(aux auxiliaryState) []SubexBranch {
	aux.nesting = append(aux.nesting, state.keys)
	return []SubexBranch {{
		state: state.next,
		aux: aux,
	}}
}
func (state SubexIncrementNestState) String() string {
	return "IncrementNestState"
}

type SubexDecrementNestState struct {
	next SubexState
}
func (state SubexDecrementNestState) epsilon(aux auxiliaryState) []SubexBranch {
	aux.nesting = aux.nesting[:len(aux.nesting) - 1]
	// aux.nestingValue will be set in addStates
	return []SubexBranch {{
		state: state.next,
		aux: aux,
	}}
}