Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/command.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2025-10-29 18:14:52 +0000
committerCharlie Stanton <charlie@shtanton.xyz>2025-10-29 18:14:52 +0000
commite4e454665bf7e21db21763c65bf35d23665b17cf (patch)
tree121d749fe4487739ebfb239dc40d5cc88e0097dd /main/command.go
parentb2ce005d227a10a9b8a6f5362c87a0e34ee07acc (diff)
downloadstred-go-e4e454665bf7e21db21763c65bf35d23665b17cf.tar
Improve internals of command control flowHEADmain
Diffstat (limited to 'main/command.go')
-rw-r--r--main/command.go104
1 files changed, 71 insertions, 33 deletions
diff --git a/main/command.go b/main/command.go
index bbbb036..832a236 100644
--- a/main/command.go
+++ b/main/command.go
@@ -67,6 +67,7 @@ func (cmd AppendNextCommand) String() string {
type SubstituteNextCommand struct {
subex subex.Transducer
+ elseJump int
}
func (cmd SubstituteNextCommand) exec(state *ProgramState) {
item, err := state.Peek()
@@ -77,14 +78,14 @@ func (cmd SubstituteNextCommand) exec(state *ProgramState) {
newValue, notOk := runSubex(cmd.subex, []walk.Value{item.Value})
if notOk {
- state.pc++
+ state.pc += cmd.elseJump
} else {
+ state.pc++
state.Read()
state.prevStart = item.PrevStart
state.start = item.Start
state.end = item.End
state.nextEnd = item.NextEnd
- state.pc += 2
state.value = newValue
}
}
@@ -94,6 +95,7 @@ func (cmd SubstituteNextCommand) String() string {
type SubstituteAppendNextCommand struct {
subex subex.Transducer
+ elseJump int
}
func (cmd SubstituteAppendNextCommand) exec(state *ProgramState) {
item, err := state.Peek()
@@ -104,14 +106,14 @@ func (cmd SubstituteAppendNextCommand) exec(state *ProgramState) {
newValue, notOk := runSubex(cmd.subex, []walk.Value{item.Value})
if notOk {
- state.pc++
+ state.pc += cmd.elseJump
} else {
state.Read()
state.prevStart = item.PrevStart
state.start = item.Start
state.end = item.End
state.nextEnd = item.NextEnd
- state.pc += 2
+ state.pc++
state.value = append(state.value, newValue...)
}
}
@@ -140,15 +142,16 @@ func (cmd MergeCommand) String() string {
type FullMergeCommand struct {
subex subex.Transducer
+ elseJump int
}
func (cmd FullMergeCommand) exec(state *ProgramState) {
_, notOk := runSubex(cmd.subex, state.value)
if notOk {
- state.pc++
+ state.pc += cmd.elseJump
return
}
if !state.start {
- state.pc += 2
+ state.pc++
return
}
@@ -170,7 +173,7 @@ func (cmd FullMergeCommand) exec(state *ProgramState) {
state.start = item.Start
state.end = item.End
state.nextEnd = item.NextEnd
- state.pc += 2
+ state.pc++
return
}
}
@@ -198,13 +201,14 @@ func runSubex(state subex.Transducer, in []walk.Value) ([]walk.Value, bool) {
type SubstituteValueCommand struct {
subex subex.Transducer
+ elseJump int
}
func (cmd SubstituteValueCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
- state.pc++
+ state.pc += cmd.elseJump
} else {
- state.pc += 2
+ state.pc++
state.value = newValue
}
}
@@ -212,54 +216,72 @@ func (cmd SubstituteValueCommand) String() string {
return "s/.../"
}
-type IsStartCommand struct {}
+type IsStartCommand struct {
+ elseJump int
+}
func (cmd IsStartCommand) exec(state *ProgramState) {
if state.start {
- state.pc += 2
+ state.pc++
} else {
- state.pc += 1
+ state.pc += cmd.elseJump
}
}
func (cmd IsStartCommand) String() string {
return "a"
}
-type IsPrevStartCommand struct {}
+type IsPrevStartCommand struct {
+ elseJump int
+}
func (cmd IsPrevStartCommand) exec(state *ProgramState) {
if state.prevStart {
- state.pc += 2
+ state.pc++
} else {
- state.pc += 1
+ state.pc += cmd.elseJump
}
}
func (cmd IsPrevStartCommand) String() string {
return "A"
}
-type IsEndCommand struct {}
+type IsEndCommand struct {
+ elseJump int
+}
func (cmd IsEndCommand) exec(state *ProgramState) {
if state.end {
- state.pc += 2
+ state.pc++
} else {
- state.pc += 1
+ state.pc += cmd.elseJump
}
}
func (cmd IsEndCommand) String() string {
return "e"
}
-type IsNextEndCommand struct {}
+type IsNextEndCommand struct {
+ elseJump int
+}
func (cmd IsNextEndCommand) exec(state *ProgramState) {
if state.nextEnd {
- state.pc += 2
+ state.pc++
} else {
- state.pc += 1
+ state.pc += cmd.elseJump
}
}
func (cmd IsNextEndCommand) String() string {
return "E"
}
+type LabelCommand struct {
+ label rune
+}
+func (cmd LabelCommand) exec(state *ProgramState) {
+ state.pc++
+}
+func (cmd LabelCommand) String() string {
+ return fmt.Sprintf(":%c", cmd.label)
+}
+
type NoopCommand struct {}
func (cmd NoopCommand) exec(state *ProgramState) {
state.pc++
@@ -290,13 +312,14 @@ func (cmd AppendXRegCommand) String() string {
type SubstituteToXRegCommand struct {
subex subex.Transducer
+ elseJump int
}
func (cmd SubstituteToXRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
- state.pc++
+ state.pc += cmd.elseJump
} else {
- state.pc += 2
+ state.pc++
state.xreg = newValue
}
}
@@ -306,13 +329,14 @@ func (cmd SubstituteToXRegCommand) String() string {
type SubstituteAppendXRegCommand struct {
subex subex.Transducer
+ elseJump int
}
func (cmd SubstituteAppendXRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
- state.pc++
+ state.pc += cmd.elseJump
} else {
- state.pc += 2
+ state.pc++
state.xreg = append(state.xreg, newValue...)
}
}
@@ -342,13 +366,14 @@ func (cmd AppendYRegCommand) String() string {
type SubstituteToYRegCommand struct {
subex subex.Transducer
+ elseJump int
}
func (cmd SubstituteToYRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
- state.pc++
+ state.pc += cmd.elseJump
} else {
- state.pc += 2
+ state.pc++
state.yreg = newValue
}
}
@@ -358,13 +383,14 @@ func (cmd SubstituteToYRegCommand) String() string {
type SubstituteAppendYRegCommand struct {
subex subex.Transducer
+ elseJump int
}
func (cmd SubstituteAppendYRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
- state.pc++
+ state.pc += cmd.elseJump
} else {
- state.pc += 2
+ state.pc++
state.yreg = append(state.xreg, newValue...)
}
}
@@ -394,13 +420,14 @@ func (cmd AppendZRegCommand) String() string {
type SubstituteToZRegCommand struct {
subex subex.Transducer
+ elseJump int
}
func (cmd SubstituteToZRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
- state.pc++
+ state.pc += cmd.elseJump
} else {
- state.pc += 2
+ state.pc++
state.zreg = newValue
}
}
@@ -410,13 +437,14 @@ func (cmd SubstituteToZRegCommand) String() string {
type SubstituteAppendZRegCommand struct {
subex subex.Transducer
+ elseJump int
}
func (cmd SubstituteAppendZRegCommand) exec(state *ProgramState) {
newValue, err := runSubex(cmd.subex, state.value)
if err {
- state.pc++
+ state.pc += cmd.elseJump
} else {
- state.pc += 2
+ state.pc++
state.zreg = append(state.xreg, newValue...)
}
}
@@ -424,6 +452,16 @@ func (cmd SubstituteAppendZRegCommand) String() string {
return "Z/.../"
}
+type RelativeJumpCommand struct {
+ destination int
+}
+func (cmd RelativeJumpCommand) exec(state *ProgramState) {
+ state.pc += cmd.destination
+}
+func (cmd RelativeJumpCommand) String() string {
+ return fmt.Sprintf("b+%v", cmd.destination)
+}
+
type JumpCommand struct {
destination int
}