diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-19 16:00:04 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-19 16:00:04 +0100 |
commit | b80b72cfe2e02ce7b9467e161703a47e433f992d (patch) | |
tree | 81ad3202715f807cb4bf2987b8068e73f44e2a1a /main | |
parent | dae74317d84471f6a859117fcbba1cf546be8ea1 (diff) | |
download | stred-go-b80b72cfe2e02ce7b9467e161703a47e433f992d.tar |
Replaces the workspace with 3 distinct registers: path, value and xreg
workspace contained a list of WalkItems, pairs of paths and values.
The new system can still hold a list of values but only one path, which is in itself a list of values.
The X register is miscellaneous. All 3 hold a list of values (which are JSON tokens)
Diffstat (limited to 'main')
-rw-r--r-- | main/command.go | 49 | ||||
-rw-r--r-- | main/main.go | 13 |
2 files changed, 34 insertions, 28 deletions
diff --git a/main/command.go b/main/command.go index 8053b86..511bda8 100644 --- a/main/command.go +++ b/main/command.go @@ -7,8 +7,12 @@ import ( type PrintValueCommand struct {} func (cmd PrintValueCommand) exec(state *ProgramState) { - for _, item := range state.space { - state.out <- item + path := walk.PathFromWalkValues(state.path) + for _, value := range state.value { + state.out <- walk.WalkItem { + Value: value, + Path: path, + } } } @@ -21,12 +25,12 @@ func (cmd ToggleTerminalCommand) exec(state *ProgramState) { walk.MapEnd: walk.ArrayEnd, } - for i := range state.space { - terminal, isTerminal := state.space[i].Value.(walk.TerminalValue) + for i := range state.value { + terminal, isTerminal := state.value[i].(walk.TerminalValue) if !isTerminal { continue } - state.space[i].Value = toggled[terminal] + state.value[i] = toggled[terminal] } } @@ -35,8 +39,12 @@ type FilteredCommand struct { command Command } func (cmd FilteredCommand) exec(state *ProgramState) { - for _, item := range state.space { - if cmd.filter.exec(item) { + path := walk.PathFromWalkValues(state.path) + for _, value := range state.value { + if cmd.filter.exec(walk.WalkItem { + Value: value, + Path: path, + }) { cmd.command.exec(state) return } @@ -56,38 +64,30 @@ 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, - }) - } + state.value = append(state.value, cmd.values...) } 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...) + var newItems []walk.WalkValue + newItems = append(newItems, cmd.values...) + state.value = append(newItems, state.value...) } type NextCommand struct {} func (cmd NextCommand) exec(state *ProgramState) { nextItem := <- state.in - state.space = []walk.WalkItem{nextItem} + state.value = []walk.WalkValue{nextItem.Value} + state.path = nextItem.Path.ToWalkValues() } type AppendNextCommand struct {} func (cmd AppendNextCommand) exec(state *ProgramState) { nextItem := <- state.in - state.space = append(state.space, nextItem) + state.value = append(state.value, nextItem.Value) + state.path = nextItem.Path.ToWalkValues() } type PrintLiteralsCommand struct { @@ -101,7 +101,8 @@ func (cmd PrintLiteralsCommand) exec(state *ProgramState) { type DeleteAllCommand struct {} func (cmd DeleteAllCommand) exec(state *ProgramState) { - state.space = nil + state.path = nil + state.value = nil } type SubstituteCommand struct { diff --git a/main/main.go b/main/main.go index 902c5b9..923ffa6 100644 --- a/main/main.go +++ b/main/main.go @@ -9,7 +9,7 @@ import ( type Program []Command type ProgramState struct { - space []walk.WalkItem + path, value, xreg []walk.WalkValue in chan walk.WalkItem out chan walk.WalkItem program []Command @@ -50,13 +50,18 @@ func main() { go func () { for walkItem := range dataStream { - state.space = []walk.WalkItem{walkItem} + state.value = []walk.WalkValue{walkItem.Value} + state.path = walkItem.Path.ToWalkValues() for _, cmd := range state.program { cmd.exec(&state) } if !quiet { - for _, item := range state.space { - state.out <- item + path := walk.PathFromWalkValues(state.path) + for _, value := range state.value { + state.out <- walk.WalkItem { + Value: value, + Path: path, + } } } } |