<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2023-04-24 13:16:06 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2023-04-24 13:16:06 +0100
commit8e80185508a697ddfcfed4a04d3f4e1ac5a330a9 (patch)
treeae02915068a6f562ae2e6d154fc48d1106b9d826 /main
parent86ee39f44266cb314ab36c4f941377620fc0fead (diff)
downloadstred-go-8e80185508a697ddfcfed4a04d3f4e1ac5a330a9.tar
WalkItems are now made of Atoms instead of WalkValues, and I have rolled my own JSON parser and serialiser
These changes improve performance
Diffstat (limited to 'main')
-rw-r--r--main/command.go29
-rw-r--r--main/main.go58
2 files changed, 38 insertions, 49 deletions
diff --git a/main/command.go b/main/command.go
index a0ac35e..c7b1aa9 100644
--- a/main/command.go
+++ b/main/command.go
@@ -16,16 +16,7 @@ func (cmd PrintValueCommand) exec(state *ProgramState) {
panic("Tried to convert invalid atoms to values")
}
path := walk.PathFromWalkValues(pathValues)
- values, err := walk.Compound(state.value)
- if err != nil {
- panic("Tried to convert invalid atoms to values")
- }
- for _, value := range values {
- state.out <- walk.WalkItem {
- Value: value,
- Path: path,
- }
- }
+ state.out.Print(path, state.value)
}
type SequenceCommand struct {
@@ -39,16 +30,22 @@ func (cmd SequenceCommand) exec(state *ProgramState) {
type NextCommand struct {}
func (cmd NextCommand) exec(state *ProgramState) {
- nextItem := <- state.in
- state.value = walk.Atomise([]walk.WalkValue{nextItem.Value})
- state.path = walk.Atomise(nextItem.Path.ToWalkValues())
+ nextItem, err := state.in.Read()
+ if err != nil {
+ panic("Missing next value")
+ }
+ state.value = nextItem.Value
+ state.path = nextItem.Path
}
type AppendNextCommand struct {}
func (cmd AppendNextCommand) exec(state *ProgramState) {
- nextItem := <- state.in
- state.value = append(state.value, walk.Atomise([]walk.WalkValue{nextItem.Value})...)
- state.path = walk.Atomise(nextItem.Path.ToWalkValues())
+ nextItem, err := state.in.Read()
+ if err != nil {
+ panic("Missing next value")
+ }
+ state.value = append(state.value, nextItem.Value...)
+ state.path = nextItem.Path
}
type DeleteValueCommand struct {}
diff --git a/main/main.go b/main/main.go
index 564d14a..e6730de 100644
--- a/main/main.go
+++ b/main/main.go
@@ -10,8 +10,8 @@ type Program []Command
type ProgramState struct {
path, value, xreg []walk.Atom
- in chan walk.WalkItem
- out chan walk.WalkItem
+ in walk.JSONIn
+ out walk.JSONOut
program []Command
}
@@ -40,41 +40,33 @@ func main() {
program := Parse(tokens)
stdin := bufio.NewReader(os.Stdin)
- dataStream := walk.Json(stdin)
state := ProgramState {
- in: dataStream,
- out: make(chan walk.WalkItem),
+ in: walk.NewJSONIn(stdin),
+ out: walk.NewJSONOut(),
program: program,
}
-
- go func () {
- for walkItem := range dataStream {
- state.value = walk.Atomise([]walk.WalkValue{walkItem.Value})
- state.path = walk.Atomise(walkItem.Path.ToWalkValues())
- for _, cmd := range state.program {
- cmd.exec(&state)
- }
- if !quiet {
- pathValues, err := walk.Compound(state.path)
- if err != nil {
- panic("Tried to convert invalid atoms to values")
- }
- path := walk.PathFromWalkValues(pathValues)
- values, err := walk.Compound(state.value)
- if err != nil {
- panic("Tried to convert invalid atoms to values")
- }
- for _, value := range values {
- state.out <- walk.WalkItem {
- Value: value,
- Path: path,
- }
- }
+
+ for {
+ walkItem, err := state.in.Read()
+ if err != nil {
+ break
+ }
+ state.value = walkItem.Value
+ state.path = walkItem.Path
+ for _, cmd := range state.program {
+ cmd.exec(&state)
+ }
+ if !quiet {
+ pathValues, err := walk.Compound(state.path)
+ if err != nil {
+ panic("Tried to convert invalid atoms to values")
}
+ path := walk.PathFromWalkValues(pathValues)
+ state.out.Print(path, state.value)
}
- close(state.out)
- }()
-
- walk.JsonOut(state.out)
+ }
+
+ state.in.AssertDone()
+ state.out.AssertDone()
} \ No newline at end of file