diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2022-08-26 11:51:46 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2022-08-26 11:51:46 +0100 |
commit | ce5c224211a94bfd4c898b51d15febdf2ed9d6f2 (patch) | |
tree | 8d1c9db463d9c1793bd3aad2b6875a22d4add90c /main/command.go | |
parent | ececdecdaf6c6f6295d31a92f0663d703e7760dd (diff) | |
download | stred-go-ce5c224211a94bfd4c898b51d15febdf2ed9d6f2.tar |
Refactors some stuff and adds lexing and parsing
Diffstat (limited to 'main/command.go')
-rw-r--r-- | main/command.go | 84 |
1 files changed, 69 insertions, 15 deletions
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 |