From b80b72cfe2e02ce7b9467e161703a47e433f992d Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Wed, 19 Apr 2023 16:00:04 +0100 Subject: 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) --- main/command.go | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) (limited to 'main/command.go') 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 { -- cgit v1.2.3