diff options
Diffstat (limited to 'main/command.go')
-rw-r--r-- | main/command.go | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/main/command.go b/main/command.go index 7d44309..5e77dd0 100644 --- a/main/command.go +++ b/main/command.go @@ -5,6 +5,10 @@ import ( "main/subex" ) +type Command interface { + exec(*ProgramState) +} + type PrintValueCommand struct {} func (cmd PrintValueCommand) exec(state *ProgramState) { path := walk.PathFromWalkValues(state.path) @@ -45,33 +49,50 @@ func (cmd DeleteAllCommand) exec(state *ProgramState) { state.value = nil } -type SubstituteCommand struct { - subex subex.SubexState - next Command -} -func (cmd SubstituteCommand) exec(state *ProgramState) { +func runSubex(state subex.SubexState, in []walk.WalkValue) (out []walk.WalkValue, error bool) { valueStream := make(chan walk.WalkValue) go func(in []walk.WalkValue, out chan<- walk.WalkValue) { for _, value := range in { out <- value } close(out) - }(state.value, valueStream) + }(in, valueStream) atomStream := walk.Atomise(valueStream) - atomsOut, error := subex.RunTransducer(cmd.subex, atomStream) + atomsOut, error := subex.RunTransducer(state, atomStream) if error { - return + return nil, true } valuesOut, err := walk.MemoryCompound(atomsOut) if err != nil { + return nil, true + } + return valuesOut, false +} + +type SubstituteValueCommand struct { + subex subex.SubexState + next Command +} +func (cmd SubstituteValueCommand) exec(state *ProgramState) { + newValue, err := runSubex(cmd.subex, state.value) + if err { return } - state.value = valuesOut + state.value = newValue cmd.next.exec(state) } -type Command interface { - exec(*ProgramState) +type SubstitutePathCommand struct { + subex subex.SubexState + next Command +} +func (cmd SubstitutePathCommand) exec(state *ProgramState) { + newPath, err := runSubex(cmd.subex, state.path) + if err { + return + } + state.path = newPath + cmd.next.exec(state) } type NoopCommand struct {} |