<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/command.go
blob: c61b0cda3d9ba90566565d1ed21dd0e1e2e85db9 (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
package main

import (
	"main/walk"
)

type PrintValueCommand struct {}
func (cmd PrintValueCommand) exec(state *ProgramState) {
	for _, item := range state.space {
		state.out <- item
	}
}

type ToggleTerminalCommand struct {}
func (cmd ToggleTerminalCommand) exec(state *ProgramState) {
	toggled := map[walk.TerminalValue]walk.TerminalValue {
		walk.ArrayBegin: walk.MapBegin,
		walk.ArrayEnd: walk.MapEnd,
		walk.MapBegin: walk.ArrayBegin,
		walk.MapEnd: walk.ArrayEnd,
	}

	for i := range state.space {
		terminal, isTerminal := state.space[i].Value.(walk.TerminalValue)
		if !isTerminal {
			continue
		}
		state.space[i].Value = toggled[terminal]
	}
}

type FilteredCommand struct {
	filter Filter
	command Command
}
func (cmd FilteredCommand) exec(state *ProgramState) {
	for _, item := range state.space {
		if cmd.filter.exec(item) {
			cmd.command.exec(state)
			return
		}
	}
}

type SequenceCommand struct {
	commands []Command
}
func (cmd SequenceCommand) exec(state *ProgramState) {
	for _, command := range cmd.commands {
		command.exec(state)
	}
}

type AppendLiteralCommand struct {
	values []walk.WalkValue
}
func (cmd AppendLiteralCommand) exec(state *ProgramState) {
	for _, value := range cmd.values {
		state.space = append(state.space, walk.WalkItem {
			Path: nil,
			Value: value,
		})
	}
}

type PrependLiteralCommand struct {
	values []walk.WalkValue
}
func (cmd PrependLiteralCommand) exec(state *ProgramState) {
	var newItems []walk.WalkItem
	for _, value := range cmd.values {
		newItems = append(newItems, walk.WalkItem {
			Path: nil,
			Value: value,
		})
	}
	state.space = append(newItems, state.space...)
}

type NextCommand struct {}
func (cmd NextCommand) exec(state *ProgramState) {
	nextItem := <- state.in
	state.space = []walk.WalkItem{nextItem}
}

type AppendNextCommand struct {}
func (cmd AppendNextCommand) exec(state *ProgramState) {
	nextItem := <- state.in
	state.space = append(state.space, nextItem)
}

type PrintLiteralsCommand struct {
	items []walk.WalkItem
}
func (cmd PrintLiteralsCommand) exec(state *ProgramState) {
	for _, item := range cmd.items {
		state.out <- item
	}
}

type DeleteAllCommand struct {}
func (cmd DeleteAllCommand) exec(state *ProgramState) {
	state.space = nil
}

type Command interface {
	exec(*ProgramState)
}