From ce5c224211a94bfd4c898b51d15febdf2ed9d6f2 Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Fri, 26 Aug 2022 11:51:46 +0100 Subject: Refactors some stuff and adds lexing and parsing --- main/command.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 15 deletions(-) (limited to 'main/command.go') diff --git a/main/command.go b/main/command.go index 560d3c3..bad5b1e 100644 --- a/main/command.go +++ b/main/command.go @@ -2,24 +2,26 @@ package main type PrintValueCommand struct {} func (cmd PrintValueCommand) exec(state *ProgramState) { - state.out <- state.space + for _, item := range state.space { + state.out <- item + } } type ToggleTerminalCommand struct {} func (cmd ToggleTerminalCommand) exec(state *ProgramState) { - terminal, isTerminal := state.space.value.(TerminalValue) - if !isTerminal { - return + toggled := map[TerminalValue]TerminalValue { + ArrayBegin: MapBegin, + ArrayEnd: MapEnd, + MapBegin: ArrayBegin, + MapEnd: ArrayEnd, } - switch terminal { - case ArrayBegin: - state.space.value = MapBegin - case ArrayEnd: - state.space.value = MapEnd - case MapBegin: - state.space.value = ArrayBegin - case MapEnd: - state.space.value = ArrayEnd + + for i := range state.space { + terminal, isTerminal := state.space[i].value.(TerminalValue) + if !isTerminal { + continue + } + state.space[i].value = toggled[terminal] } } @@ -28,11 +30,63 @@ type FilteredCommand struct { command Command } func (cmd FilteredCommand) exec(state *ProgramState) { - if cmd.filter.exec(state) { - cmd.command.exec(state) + 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 AppendCommand struct { + values []WalkValue +} +func (cmd AppendCommand) exec(state *ProgramState) { + for _, value := range cmd.values { + state.space = append(state.space, WalkItem { + path: nil, + value: value, + }) } } +type PrependCommand struct { + values []WalkValue +} +func (cmd PrependCommand) exec(state *ProgramState) { + var newItems []WalkItem + for _, value := range cmd.values { + newItems = append(newItems, WalkItem { + path: nil, + value: value, + }) + } + state.space = append(newItems, state.space...) +} + +type PrintLiteralsCommand struct { + items []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) } \ No newline at end of file -- cgit v1.2.3