<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/command.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2023-04-20 12:05:39 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2023-04-20 12:05:39 +0100
commite2774a2ca2d58d4dd507b210d4a62a23a538e833 (patch)
tree4341ba920ebf1d1893a73d37f613bc772c331f73 /main/command.go
parentc1c33227ab72de1e5f21a08ee74c3df667148343 (diff)
downloadstred-go-e2774a2ca2d58d4dd507b210d4a62a23a538e833.tar
Adds a substitute path command: S
Diffstat (limited to 'main/command.go')
-rw-r--r--main/command.go43
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 {}