From e2774a2ca2d58d4dd507b210d4a62a23a538e833 Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Thu, 20 Apr 2023 12:05:39 +0100 Subject: Adds a substitute path command: S --- main/command.go | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'main/command.go') 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 {} -- cgit v1.2.3