<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main/main.go')
-rw-r--r--main/main.go111
1 files changed, 73 insertions, 38 deletions
diff --git a/main/main.go b/main/main.go
index 8e8c369..bfa1afe 100644
--- a/main/main.go
+++ b/main/main.go
@@ -1,48 +1,53 @@
package main
import (
- "os"
"bufio"
- "main/walk"
+ "io"
"main/json"
+ "main/walk"
+ "os"
)
-type Program []Command
-
type ProgramState struct {
- path, value, xreg, yreg, zreg walk.ValueList
+ value, xreg, yreg, zreg []walk.Value
+ start, prevStart, end, nextEnd bool
+ // TODO: This will only ever have 0 or 1 values, it is a slice out of laziness
+ peekStack []walk.WalkItem
in walk.StredReader
out walk.StredWriter
program []Command
pc int
}
-
-func main() {
- quiet := false
- var input string
- hasInput := false
-
- for i := 1; i < len(os.Args); i += 1 {
- switch os.Args[i] {
- case "-n":
- quiet = true
- continue
- }
- if i < len(os.Args) - 1 {
- panic("Unexpected arguments after program")
- }
- input = os.Args[i]
- hasInput = true
+func (state *ProgramState) Read() (walk.WalkItem, error) {
+ if len(state.peekStack) > 0 {
+ item := state.peekStack[len(state.peekStack) - 1]
+ state.peekStack = state.peekStack[:len(state.peekStack) - 1]
+ return item, nil
}
- if !hasInput {
- panic("Missing program")
+ return state.in.Read()
+}
+func (state *ProgramState) Peek() (walk.WalkItem, error) {
+ item, err := state.Read()
+ if err != nil {
+ return walk.WalkItem{}, err
}
+ state.peekStack = append(state.peekStack, item)
+ return item, nil
+}
+
+type config struct {
+ quiet bool
+ program string
+ in io.Reader
+ out io.Writer
+}
- tokens := Lex(input)
+func run(config config) {
+ tokens := Lex(config.program)
program := Parse(tokens)
- stdin := bufio.NewReader(os.Stdin)
- stdout := bufio.NewWriter(os.Stdout)
+ stdin := bufio.NewReader(config.in)
+ stdout := bufio.NewWriter(config.out)
state := ProgramState {
in: json.NewJSONReader(stdin),
@@ -51,27 +56,57 @@ func main() {
}
for {
- walkItem, err := state.in.Read()
+ walkItem, err := state.Read()
if err != nil {
break
}
- state.value = walkItem.Value
- state.path = walkItem.Path
+ state.value = []walk.Value{walkItem.Value}
+ state.start = walkItem.Start
+ state.prevStart = walkItem.PrevStart
+ state.end = walkItem.End
+ state.nextEnd = walkItem.NextEnd
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")
+ if !config.quiet {
+ 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
+}
+
+func main() {
+ config := config {
+ quiet: false,
+ in: os.Stdin,
+ out: os.Stdout,
+ }
+ hasInput := false
+
+ for i := 1; i < len(os.Args); i += 1 {
+ switch os.Args[i] {
+ case "-n":
+ config.quiet = true
+ continue
+ }
+ if i < len(os.Args) - 1 {
+ panic("Unexpected arguments after program")
+ }
+ config.program = os.Args[i]
+ hasInput = true
+ }
+ if !hasInput {
+ panic("Missing program")
+ }
+
+ run(config)
+
+}