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
404
405
406
407
408
409
410
411
412
|
package main
import (
"main/subex"
"main/walk"
"fmt"
)
type Command interface {
exec(*ProgramState)
String() string
}
type PrintValueCommand struct {}
func (cmd PrintValueCommand) exec(state *ProgramState) {
for _, value := range state.value {
err := state.out.Write(value)
if err != nil {
panic("Error while outputting")
}
}
state.pc++
}
func (cmd PrintValueCommand) String() string {
return "p"
}
type NextCommand struct {}
func (cmd NextCommand) exec(state *ProgramState) {
nextItem, err := state.Read()
if err != nil {
panic("Missing next value")
}
state.prevStart = nextItem.PrevStart
state.start = nextItem.Start
state.end = nextItem.End
state.nextEnd = nextItem.NextEnd
state.value = []walk.Value{nextItem.Value}
state.pc++
}
func (cmd NextCommand) String() string {
return "n"
}
type AppendNextCommand struct {}
func (cmd AppendNextCommand) exec(state *ProgramState) {
nextItem, err := state.Read()
if err != nil {
panic("Missing next value")
}
state.prevStart = nextItem.PrevStart
state.start = nextItem.Start
state.end = nextItem.End
state.nextEnd = nextItem.NextEnd
state.value = append(state.value, nextItem.Value)
state.pc++
}
func (cmd AppendNextCommand) String() string {
return "N"
}
type SubstituteNextCommand struct {
subex subex.Transducer
}
func (cmd SubstituteNextCommand) exec(state *ProgramState) {
item, err := state.Peek()
if err != nil {
panic("Missing next value")
}
newValue, notOk := runSubex(cmd.subex, []walk.Value{item.Value})
if notOk {
state.pc++
} else {
state.prevStart = item.PrevStart
state.start = item.Start
state.end = item.End
state.nextEnd = item.NextEnd
state.pc += 2
state.value = newValue
}
}
func (cmd SubstituteNextCommand) String() string {
return "n/.../"
}
type SubstituteAppendNextCommand struct {
subex subex.Transducer
}
func (cmd SubstituteAppendNextCommand) exec(state *ProgramState) {
item, err := state.Peek()
if err != nil {
panic("Missing next value")
}
newValue, notOk := runSubex(cmd.subex, []walk.Value{item.Value})
if notOk {
state.pc++
} else {
state.prevStart = item.PrevStart
state.start = item.Start
state.end = item.End
state.nextEnd = item.NextEnd
state.pc += 2
state.value = append(state.value, newValue...)
}
}
func (cmd SubstituteAppendNextCommand) String() string {
return "N/.../"
}
type MergeCommand struct {}
func (cmd MergeCommand) exec(state *ProgramState) {
if len(state.value) <= 1 {
state.pc++
return
}
newVals := walk.Merge(state.value[len(state.value) - 2], state.value[len(state.value) - 1])
state.value = append(
state.value[:len(state.value) - 2],
newVals...
)
state.pc++
}
func (cmd MergeCommand) String() string {
return "m"
}
type FullMergeCommand struct {}
func (cmd FullMergeCommand) exec(state *ProgramState) {
panic("Unimplemented")
}
func (cmd FullMergeCommand) String() string {
return "M"
}
type DeleteValueCommand struct {}
func (cmd DeleteValueCommand) exec(state *ProgramState) {
state.value = nil
state.pc++
}
func (cmd DeleteValueCommand) String() string {
return "d"
}
func runSubex(state subex.Transducer, in []walk.Value) ([]walk.Value, bool) {
out, error := subex.RunTransducer(state, in)
if error {
return nil, true
}
return out, false
}
type SubstituteValueCommand struct {
subex subex.Transducer
}
func (cmd SubstituteValueCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
state.pc++
} else {
state.pc += 2
state.value = newValue
}
}
func (cmd SubstituteValueCommand) String() string {
return "s/.../"
}
type IsStartCommand struct {}
func (cmd IsStartCommand) exec(state *ProgramState) {
if state.start {
state.pc += 2
} else {
state.pc += 1
}
}
func (cmd IsStartCommand) String() string {
return "a"
}
type IsPrevStartCommand struct {}
func (cmd IsPrevStartCommand) exec(state *ProgramState) {
if state.prevStart {
state.pc += 2
} else {
state.pc += 1
}
}
func (cmd IsPrevStartCommand) String() string {
return "A"
}
type IsEndCommand struct {}
func (cmd IsEndCommand) exec(state *ProgramState) {
if state.end {
state.pc += 2
} else {
state.pc += 1
}
}
func (cmd IsEndCommand) String() string {
return "e"
}
type IsNextEndCommand struct {}
func (cmd IsNextEndCommand) exec(state *ProgramState) {
if state.nextEnd {
state.pc += 2
} else {
state.pc += 1
}
}
func (cmd IsNextEndCommand) String() string {
return "E"
}
type NoopCommand struct {}
func (cmd NoopCommand) exec(state *ProgramState) {
state.pc++
}
func (cmd NoopCommand) String() string {
return "o"
}
type SwapXRegCommand struct {}
func (cmd SwapXRegCommand) exec(state *ProgramState) {
v := state.value
state.value = state.xreg
state.xreg = v
state.pc++
}
func (cmd SwapXRegCommand) String() string {
return "x"
}
type AppendXRegCommand struct {}
func (cmd AppendXRegCommand) exec(state *ProgramState) {
state.xreg = append(state.xreg, state.value...)
state.pc++
}
func (cmd AppendXRegCommand) String() string {
return "X"
}
type SubstituteToXRegCommand struct {
subex subex.Transducer
}
func (cmd SubstituteToXRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
state.pc++
} else {
state.pc += 2
state.xreg = newValue
}
}
func (cmd SubstituteToXRegCommand) String() string {
return "x/.../"
}
type SubstituteAppendXRegCommand struct {
subex subex.Transducer
}
func (cmd SubstituteAppendXRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
state.pc++
} else {
state.pc += 2
state.xreg = append(state.xreg, newValue...)
}
}
func (cmd SubstituteAppendXRegCommand) String() string {
return "X/.../"
}
type SwapYRegCommand struct {}
func (cmd SwapYRegCommand) exec(state *ProgramState) {
v := state.value
state.value = state.yreg
state.yreg = v
state.pc++
}
func (cmd SwapYRegCommand) String() string {
return "y"
}
type AppendYRegCommand struct {}
func (cmd AppendYRegCommand) exec(state *ProgramState) {
state.yreg = append(state.yreg, state.value...)
state.pc++
}
func (cmd AppendYRegCommand) String() string {
return "Y"
}
type SubstituteToYRegCommand struct {
subex subex.Transducer
}
func (cmd SubstituteToYRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
state.pc++
} else {
state.pc += 2
state.yreg = newValue
}
}
func (cmd SubstituteToYRegCommand) String() string {
return "y/.../"
}
type SubstituteAppendYRegCommand struct {
subex subex.Transducer
}
func (cmd SubstituteAppendYRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
state.pc++
} else {
state.pc += 2
state.yreg = append(state.xreg, newValue...)
}
}
func (cmd SubstituteAppendYRegCommand) String() string {
return "Y/.../"
}
type SwapZRegCommand struct {}
func (cmd SwapZRegCommand) exec(state *ProgramState) {
v := state.value
state.value = state.zreg
state.zreg = v
state.pc++
}
func (cmd SwapZRegCommand) String() string {
return "z"
}
type AppendZRegCommand struct {}
func (cmd AppendZRegCommand) exec(state *ProgramState) {
state.zreg = append(state.zreg, state.value...)
state.pc++
}
func (cmd AppendZRegCommand) String() string {
return "Z"
}
type SubstituteToZRegCommand struct {
subex subex.Transducer
}
func (cmd SubstituteToZRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
state.pc++
} else {
state.pc += 2
state.zreg = newValue
}
}
func (cmd SubstituteToZRegCommand) String() string {
return "z/.../"
}
type SubstituteAppendZRegCommand struct {
subex subex.Transducer
}
func (cmd SubstituteAppendZRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
state.pc++
} else {
state.pc += 2
state.zreg = append(state.xreg, newValue...)
}
}
func (cmd SubstituteAppendZRegCommand) String() string {
return "Z/.../"
}
type JumpCommand struct {
destination int
}
func (cmd JumpCommand) exec(state *ProgramState) {
state.pc = cmd.destination
}
func (cmd JumpCommand) String() string {
return fmt.Sprintf("b%v", cmd.destination)
}
// Placeholder as the branch destination may not have been parsed when the branch command is
// Should never appear in a program to actually be run
type BranchPlaceholderCommand struct {
label rune
}
func (cmd BranchPlaceholderCommand) exec(state *ProgramState) {
panic("Tried to execute a BranchPlaceholderCommand!!!")
}
func (cmd BranchPlaceholderCommand) String() string {
return fmt.Sprintf("b%c", cmd.label)
}
|