<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/subex/subexstate.go
blob: 997e6ce8567f49d4b3955366b5c0fd1b03a59560 (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
package subex

import (
	"main/walk"
)

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

// Try first, if it fails then try second
type SubexGroupState struct {
	first, second SubexState
}
func (state SubexGroupState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	otherStore := store.clone()
	return append(state.first.eat(store, outputStack, char), state.second.eat(otherStore, outputStack, char)...)
}
func (state SubexGroupState) accepting(store Store, outputStack OutputStack) []OutputStack {
	return append(state.first.accepting(store, outputStack), state.second.accepting(store, outputStack)...)
}

// 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) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	return state.next.eat(store, outputStack.push(nil), char)
}
func (state SubexCaptureBeginState) accepting(store Store, outputStack OutputStack) []OutputStack {
	return state.next.accepting(store, outputStack.push(nil))
}

// 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 rune
	next SubexState
}
func (state SubexStoreEndState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	toStore, newStack := outputStack.pop()
	return state.next.eat(store.withValue(state.slot, toStore), newStack, char)
}
func (state SubexStoreEndState) accepting(store Store, outputStack OutputStack) []OutputStack {
	toStore, newStack := outputStack.pop()
	return state.next.accepting(store.withValue(state.slot, toStore), newStack)
}

// A part of an output literal, either an Atom or a slot from which to load
type OutputContent interface {
	// Given the current store, return the []Atom produced by the TransducerOutput
	build(Store) []walk.Atom
}

// An OutputContent which is just an Atom literal
type OutputAtomLiteral struct {
	atom walk.Atom
}
func (replacement OutputAtomLiteral) build(store Store) []walk.Atom {
	return []walk.Atom{replacement.atom}
}

// An OutputContent which is a slot that is loaded from
type OutputLoad struct {
	slot rune
}
func (replacement OutputLoad) build(store Store) []walk.Atom {
	return store[replacement.slot]
}

// Don't read in anything, just output the series of data and slots specified
type SubexOutputState struct {
	content []OutputContent
	next SubexState
}
// Given a store, return what is outputted by an epsilon transition from this state
func (state SubexOutputState) build(store Store) []walk.Atom {
	var result []walk.Atom
	for _, part := range state.content {
		result = append(result, part.build(store)...)
	}
	return result
}
func (state SubexOutputState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	content := state.build(store)
	nextStates := state.next.eat(store, topAppend(outputStack, content), char)
	return nextStates
}
func (state SubexOutputState) accepting(store Store, outputStack OutputStack) []OutputStack {
	content := state.build(store)
	outputStacks := state.next.accepting(store, topAppend(outputStack, content))
	return outputStacks
}

// A final state, transitions to nothing but is accepting
type SubexNoneState struct {}
func (state SubexNoneState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	return nil
}
func (state SubexNoneState) accepting(store Store, outputStack OutputStack) []OutputStack {
	return []OutputStack{outputStack}
}

// A dead end state, handy for making internals work nicer but technically redundant
type SubexDeadState struct {}
func (state SubexDeadState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	return nil
}
func (state SubexDeadState) accepting (store Store, outputStack OutputStack) []OutputStack {
	return nil
}

// Read in a specific Atom and output it
type SubexCopyAtomState struct {
	atom walk.Atom
	next SubexState
}
func (state SubexCopyAtomState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	// TODO can I compare Atom values with == ?
	if char == state.atom {
		return []SubexBranch{{
			state: state.next,
			outputStack: topAppend(outputStack, []walk.Atom{char}),
			store: store,
		}}
	}
	return nil
}
func (state SubexCopyAtomState) accepting(store Store, outputStack OutputStack) []OutputStack {
	return nil
}

// Read in a boolean atom and output it
type SubexCopyBoolState struct {
	next SubexState
}
func (state SubexCopyBoolState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	_, isBool := char.(walk.ValueBool)
	if isBool {
		return []SubexBranch{{
			state: state.next,
			outputStack: topAppend(outputStack, []walk.Atom{char}),
			store: store,
		}}
	}
	return nil
}
func (state SubexCopyBoolState) accepting(store Store, outputStack OutputStack) []OutputStack {
	return nil
}

// Read in a number atom and output it
type SubexCopyNumberState struct {
	next SubexState
}
func (state SubexCopyNumberState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	_, isNumber := char.(walk.ValueNumber)
	if isNumber {
		return []SubexBranch{{
			state: state.next,
			outputStack: topAppend(outputStack, []walk.Atom{char}),
			store: store,
		}}
	}
	return nil
}
func (state SubexCopyNumberState) accepting(store Store, outputStack OutputStack) []OutputStack {
	return nil
}

// Read in a string atom and output it
type SubexCopyStringAtomState struct {
	next SubexState
}
func (state SubexCopyStringAtomState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	_, isStringAtom := char.(walk.StringAtom)
	if isStringAtom {
		return []SubexBranch{{
			state: state.next,
			outputStack: topAppend(outputStack, []walk.Atom{char}),
			store: store,
		}}
	}
	return nil
}
func (state SubexCopyStringAtomState) accepting(store Store, outputStack OutputStack) []OutputStack {
	return nil
}

// Read in an atom and copy it out as long as it is not part of a string
type SubexCopyNonStringAtomState struct {
	next SubexState
}
func (state SubexCopyNonStringAtomState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	_, isStringAtom := char.(walk.StringAtom)
	_, isStringTerminal := char.(walk.StringTerminal)
	if isStringAtom || isStringTerminal {
		return nil
	}
	return []SubexBranch{{
		state: state.next,
		outputStack: topAppend(outputStack, []walk.Atom{char}),
		store: store,
	}}
}
func (state SubexCopyNonStringAtomState) accepting(store Store, outputStack OutputStack) []OutputStack {
	return nil
}

// Read in any Atom and output it
type SubexCopyAnyState struct {
	next SubexState
}
func (state SubexCopyAnyState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	return []SubexBranch{{
		state: state.next,
		outputStack: topAppend(outputStack, []walk.Atom{char}),
		store: store,
	}}
}
func (state SubexCopyAnyState) accepting(store Store, outputStack OutputStack) []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
type SubexRangeState struct {
	parts map[walk.Atom]walk.Atom
	next SubexState
}
func (state SubexRangeState) eat(store Store, outputStack OutputStack, 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(store Store, outputStack OutputStack) []OutputStack {
	return nil
}


type SubexArithmeticEndState struct {
	next SubexState
	calculate func([]walk.Atom) ([]walk.Atom, error)
}
func (state SubexArithmeticEndState) eat(store Store, outputStack OutputStack, char walk.Atom) []SubexBranch {
	toCompute, newStack := outputStack.pop()
	result, err := state.calculate(toCompute)
	if err != nil {
		return nil
	}
	return state.next.eat(store, topAppend(newStack, result), char)
}
func (state SubexArithmeticEndState) accepting(store Store, outputStack OutputStack) []OutputStack {
	toCompute, newStack := outputStack.pop()
	result, err := state.calculate(toCompute)
	if err != nil {
		return nil
	}
	return state.next.accepting(store, topAppend(newStack, result))
}