From 1aa08f927c7043a643e847c434399fc76d053df0 Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Wed, 26 Apr 2023 14:41:25 +0100 Subject: Store stred programs as a flat list of commands with no nesting, using a new jump command to simulate command blocks --- main/command.go | 100 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 83 insertions(+), 17 deletions(-) (limited to 'main/command.go') diff --git a/main/command.go b/main/command.go index 296ad69..44fd9eb 100644 --- a/main/command.go +++ b/main/command.go @@ -3,10 +3,12 @@ package main import ( "main/walk" "main/subex" + "fmt" ) type Command interface { exec(*ProgramState) + String() string } type PrintValueCommand struct {} @@ -17,15 +19,10 @@ func (cmd PrintValueCommand) exec(state *ProgramState) { } path := walk.PathFromWalkValues(pathValues) state.out.Print(path, state.value) + state.pc++ } - -type SequenceCommand struct { - commands []Command -} -func (cmd SequenceCommand) exec(state *ProgramState) { - for _, command := range cmd.commands { - command.exec(state) - } +func (cmd PrintValueCommand) String() string { + return "p" } type NextCommand struct {} @@ -36,6 +33,10 @@ func (cmd NextCommand) exec(state *ProgramState) { } state.value = nextItem.Value state.path = nextItem.Path + state.pc++ +} +func (cmd NextCommand) String() string { + return "n" } type AppendNextCommand struct {} @@ -46,16 +47,28 @@ func (cmd AppendNextCommand) exec(state *ProgramState) { } state.value = walk.ConcatData(state.value, nextItem.Value) state.path = nextItem.Path + state.pc++ +} +func (cmd AppendNextCommand) String() string { + return "N" } type DeleteValueCommand struct {} func (cmd DeleteValueCommand) exec(state *ProgramState) { state.value = nil + state.pc++ +} +func (cmd DeleteValueCommand) String() string { + return "d" } type DeletePathCommand struct {} func (cmd DeletePathCommand) exec(state *ProgramState) { state.path = nil + state.pc++ +} +func (cmd DeletePathCommand) String() string { + return "D" } func runSubex(state subex.Transducer, in []walk.Atom) (out []walk.Atom, error bool) { @@ -68,43 +81,62 @@ func runSubex(state subex.Transducer, in []walk.Atom) (out []walk.Atom, error bo type SubstituteValueCommand struct { subex subex.Transducer - next Command } func (cmd SubstituteValueCommand) exec(state *ProgramState) { newValue, err := runSubex(cmd.subex, state.value) if err { - return + state.pc++ + } else { + state.pc += 2 + state.value = newValue } - state.value = newValue - cmd.next.exec(state) +} +func (cmd SubstituteValueCommand) String() string { + return "s/.../" } type SubstitutePathCommand struct { subex subex.Transducer - next Command } func (cmd SubstitutePathCommand) exec(state *ProgramState) { newPath, err := runSubex(cmd.subex, state.path) if err { - return + state.pc++ + } else { + state.pc += 2 + state.path = newPath } - state.path = newPath - cmd.next.exec(state) +} +func (cmd SubstitutePathCommand) String() string { + return "S/.../" } type NoopCommand struct {} -func (cmd NoopCommand) exec(state *ProgramState) {} +func (cmd NoopCommand) exec(state *ProgramState) { + state.pc++ +} +func (cmd NoopCommand) String() string { + return "o" +} type SwapXRegCommand struct {} func (cmd SwapXRegCommand) exec(state *ProgramState) { v := state.value state.value = state.xreg state.xreg = v + state.pc++ +} +func (cmd SwapXRegCommand) String() string { + return "x" } type AppendXRegCommand struct {} func (cmd AppendXRegCommand) exec(state *ProgramState) { state.xreg = append(state.xreg, state.value...) + state.pc++ +} +func (cmd AppendXRegCommand) String() string { + return "X" } type SwapYRegCommand struct {} @@ -112,11 +144,19 @@ func (cmd SwapYRegCommand) exec(state *ProgramState) { v := state.value state.value = state.yreg state.yreg = v + state.pc++ +} +func (cmd SwapYRegCommand) String() string { + return "y" } type AppendYRegCommand struct {} func (cmd AppendYRegCommand) exec(state *ProgramState) { state.yreg = append(state.yreg, state.value...) + state.pc++ +} +func (cmd AppendYRegCommand) String() string { + return "Y" } type SwapZRegCommand struct {} @@ -124,11 +164,19 @@ func (cmd SwapZRegCommand) exec(state *ProgramState) { v := state.value state.value = state.zreg state.zreg = v + state.pc++ +} +func (cmd SwapZRegCommand) String() string { + return "z" } type AppendZRegCommand struct {} func (cmd AppendZRegCommand) exec(state *ProgramState) { state.zreg = append(state.zreg, state.value...) + state.pc++ +} +func (cmd AppendZRegCommand) String() string { + return "Z" } type SwapPathCommand struct {} @@ -136,9 +184,27 @@ func (cmd SwapPathCommand) exec(state *ProgramState) { v := state.value state.value = state.path state.path = v + state.pc++ +} +func (cmd SwapPathCommand) String() string { + return "k" } type AppendPathCommand struct {} func (cmd AppendPathCommand) exec(state *ProgramState) { state.path = walk.ConcatData(state.path, state.value) + state.pc++ +} +func (cmd AppendPathCommand) String() string { + return "K" +} + +type JumpCommand struct { + destination int +} +func (cmd JumpCommand) exec(state *ProgramState) { + state.pc = cmd.destination +} +func (cmd JumpCommand) String() string { + return fmt.Sprintf("b%v", cmd.destination) } \ No newline at end of file -- cgit v1.2.3