<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
Diffstat (limited to 'main')
-rw-r--r--main/command.go66
-rw-r--r--main/main.go20
-rw-r--r--main/parse.go17
3 files changed, 19 insertions, 84 deletions
diff --git a/main/command.go b/main/command.go
index 5a898e2..1d089ee 100644
--- a/main/command.go
+++ b/main/command.go
@@ -13,12 +13,11 @@ type Command interface {
type PrintValueCommand struct {}
func (cmd PrintValueCommand) exec(state *ProgramState) {
- err := state.out.Write(walk.WalkItem {
- Path: state.path,
- Value: state.value,
- })
- if err != nil {
- panic("Error while outputting")
+ for _, value := range state.value {
+ err := state.out.Write(value)
+ if err != nil {
+ panic("Error while outputting")
+ }
}
state.pc++
}
@@ -32,8 +31,7 @@ func (cmd NextCommand) exec(state *ProgramState) {
if err != nil {
panic("Missing next value")
}
- state.value = nextItem.Value
- state.path = nextItem.Path
+ state.value = []walk.Value{nextItem.Value}
state.pc++
}
func (cmd NextCommand) String() string {
@@ -46,8 +44,7 @@ func (cmd AppendNextCommand) exec(state *ProgramState) {
if err != nil {
panic("Missing next value")
}
- state.value = append(state.value, nextItem.Value...)
- state.path = nextItem.Path
+ state.value = append(state.value, nextItem.Value)
state.pc++
}
func (cmd AppendNextCommand) String() string {
@@ -63,16 +60,7 @@ func (cmd DeleteValueCommand) String() string {
return "d"
}
-type DeletePathCommand struct {}
-func (cmd DeletePathCommand) exec(state *ProgramState) {
- state.path = nil
- state.pc++
-}
-func (cmd DeletePathCommand) String() string {
- return "D"
-}
-
-func runSubex(state subex.Transducer, in walk.ValueList) (walk.ValueList, bool) {
+func runSubex(state subex.Transducer, in []walk.Value) ([]walk.Value, bool) {
out, error := subex.RunTransducer(state, in)
if error {
return nil, true
@@ -96,22 +84,6 @@ func (cmd SubstituteValueCommand) String() string {
return "s/.../"
}
-type SubstitutePathCommand struct {
- subex subex.Transducer
-}
-func (cmd SubstitutePathCommand) exec(state *ProgramState) {
- newPath, err := runSubex(cmd.subex, state.path)
- if err {
- state.pc++
- } else {
- state.pc += 2
- state.path = newPath
- }
-}
-func (cmd SubstitutePathCommand) String() string {
- return "S/.../"
-}
-
type NoopCommand struct {}
func (cmd NoopCommand) exec(state *ProgramState) {
state.pc++
@@ -180,26 +152,6 @@ func (cmd AppendZRegCommand) String() string {
return "Z"
}
-type SwapPathCommand struct {}
-func (cmd SwapPathCommand) exec(state *ProgramState) {
- v := state.value
- state.value = state.path
- state.path = v
- state.pc++
-}
-func (cmd SwapPathCommand) String() string {
- return "k"
-}
-
-type AppendPathCommand struct {}
-func (cmd AppendPathCommand) exec(state *ProgramState) {
- state.path = append(state.path, state.value...)
- state.pc++
-}
-func (cmd AppendPathCommand) String() string {
- return "K"
-}
-
type JumpCommand struct {
destination int
}
@@ -220,4 +172,4 @@ func (cmd BranchPlaceholderCommand) exec(state *ProgramState) {
}
func (cmd BranchPlaceholderCommand) String() string {
return fmt.Sprintf("b%c", cmd.label)
-} \ No newline at end of file
+}
diff --git a/main/main.go b/main/main.go
index 8e8c369..b7ef568 100644
--- a/main/main.go
+++ b/main/main.go
@@ -7,10 +7,8 @@ import (
"main/json"
)
-type Program []Command
-
type ProgramState struct {
- path, value, xreg, yreg, zreg walk.ValueList
+ value, xreg, yreg, zreg []walk.Value
in walk.StredReader
out walk.StredWriter
program []Command
@@ -55,23 +53,21 @@ func main() {
if err != nil {
break
}
- state.value = walkItem.Value
- state.path = walkItem.Path
+ state.value = []walk.Value{walkItem.Value}
state.pc = 0
for state.pc < len(state.program) {
state.program[state.pc].exec(&state)
}
if !quiet {
- err := state.out.Write(walk.WalkItem {
- Path: state.path,
- Value: state.value,
- })
- if err != nil {
- panic("Error while outputting")
+ for _, value := range state.value {
+ err := state.out.Write(value)
+ if err != nil {
+ panic("Error while outputting")
+ }
}
}
}
state.in.AssertDone()
state.out.AssertDone()
-} \ No newline at end of file
+}
diff --git a/main/parse.go b/main/parse.go
index 141ae7e..9c7a437 100644
--- a/main/parse.go
+++ b/main/parse.go
@@ -65,23 +65,14 @@ func (p *parser) parseBasicCommand(commands []Command, commandChar rune) []Comma
return append(commands, PrintValueCommand{})
case 'd':
return append(commands, DeleteValueCommand{})
- case 'D':
- return append(commands, DeletePathCommand{})
case 'n':
return append(commands, NextCommand{})
case 'N':
return append(commands, AppendNextCommand{})
- case 's', 'S':
+ case 's':
ast := p.parseSubex()
subex := subex.CompileTransducer(ast)
- switch commandChar {
- case 's':
- return append(commands, SubstituteValueCommand {subex}, JumpCommand {len(commands) + 3})
- case 'S':
- return append(commands, SubstitutePathCommand {subex}, JumpCommand {len(commands) + 3})
- default:
- panic("Unreachable!?!?")
- }
+ return append(commands, SubstituteValueCommand {subex}, JumpCommand {len(commands) + 3})
case 'o':
return append(commands, NoopCommand{})
case 'x':
@@ -96,10 +87,6 @@ func (p *parser) parseBasicCommand(commands []Command, commandChar rune) []Comma
return append(commands, SwapZRegCommand{})
case 'Z':
return append(commands, AppendZRegCommand{})
- case 'k':
- return append(commands, SwapPathCommand{})
- case 'K':
- return append(commands, AppendPathCommand{})
case ':':
labelToken := p.next()
if labelToken.typ != TokenLabel {