<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/command.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2024-04-21 17:16:01 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2024-04-21 17:16:01 +0100
commit1e66aaece6ea7cd3c705ca56ce5558e8f87681b8 (patch)
tree2fff87e4abfb0727e028854c006577eccfeee370 /main/command.go
parent7162ae8c641314846f0b565d7614ac8d71dbd628 (diff)
downloadstred-go-1e66aaece6ea7cd3c705ca56ce5558e8f87681b8.tar
Add substitute next commands
Diffstat (limited to 'main/command.go')
-rw-r--r--main/command.go73
1 files changed, 70 insertions, 3 deletions
diff --git a/main/command.go b/main/command.go
index 6d75974..04ac7f6 100644
--- a/main/command.go
+++ b/main/command.go
@@ -27,11 +27,18 @@ func (cmd PrintValueCommand) String() string {
type NextCommand struct {}
func (cmd NextCommand) exec(state *ProgramState) {
- nextItem, err := state.in.Read()
+ nextItem, err := state.Read()
if err != nil {
panic("Missing next value")
}
+
+ state.prevStart = nextItem.PrevStart
+ state.start = nextItem.Start
+ state.end = nextItem.End
+ state.nextEnd = nextItem.NextEnd
+
state.value = []walk.Value{nextItem.Value}
+
state.pc++
}
func (cmd NextCommand) String() string {
@@ -40,7 +47,7 @@ func (cmd NextCommand) String() string {
type AppendNextCommand struct {}
func (cmd AppendNextCommand) exec(state *ProgramState) {
- nextItem, err := state.in.Read()
+ nextItem, err := state.Read()
if err != nil {
panic("Missing next value")
}
@@ -58,9 +65,61 @@ func (cmd AppendNextCommand) String() string {
return "N"
}
+type SubstituteNextCommand struct {
+ subex subex.Transducer
+}
+func (cmd SubstituteNextCommand) exec(state *ProgramState) {
+ item, err := state.Peek()
+ if err != nil {
+ panic("Missing next value")
+ }
+
+ newValue, notOk := runSubex(cmd.subex, []walk.Value{item.Value})
+
+ if notOk {
+ state.pc++
+ } else {
+ state.prevStart = item.PrevStart
+ state.start = item.Start
+ state.end = item.End
+ state.nextEnd = item.NextEnd
+ state.pc += 2
+ state.value = newValue
+ }
+}
+func (cmd SubstituteNextCommand) String() string {
+ return "n/.../"
+}
+
+type SubstituteAppendNextCommand struct {
+ subex subex.Transducer
+}
+func (cmd SubstituteAppendNextCommand) exec(state *ProgramState) {
+ item, err := state.Peek()
+ if err != nil {
+ panic("Missing next value")
+ }
+
+ newValue, notOk := runSubex(cmd.subex, []walk.Value{item.Value})
+
+ if notOk {
+ state.pc++
+ } else {
+ state.prevStart = item.PrevStart
+ state.start = item.Start
+ state.end = item.End
+ state.nextEnd = item.NextEnd
+ state.pc += 2
+ state.value = append(state.value, newValue...)
+ }
+}
+func (cmd SubstituteAppendNextCommand) String() string {
+ return "N/.../"
+}
+
type MergeCommand struct {}
func (cmd MergeCommand) exec(state *ProgramState) {
- nextItem, err := state.in.Read()
+ nextItem, err := state.Read()
if err != nil {
panic("Missing next value")
}
@@ -85,6 +144,14 @@ func (cmd MergeCommand) String() string {
return "m"
}
+type FullMergeCommand struct {}
+func (cmd FullMergeCommand) exec(state *ProgramState) {
+ panic("Unimplemented")
+}
+func (cmd FullMergeCommand) String() string {
+ return "M"
+}
+
type DeleteValueCommand struct {}
func (cmd DeleteValueCommand) exec(state *ProgramState) {
state.value = nil