package main import ( "os" "bufio" "main/walk" "main/json" ) type ProgramState struct { value, xreg, yreg, zreg []walk.Value 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 } if !hasInput { panic("Missing program") } tokens := Lex(input) program := Parse(tokens) stdin := bufio.NewReader(os.Stdin) stdout := bufio.NewWriter(os.Stdout) state := ProgramState { in: json.NewJSONReader(stdin), out: json.NewJSONWriter(stdout), program: program, } for { walkItem, err := state.in.Read() if err != nil { break } state.value = []walk.Value{walkItem.Value} state.pc = 0 for state.pc < len(state.program) { state.program[state.pc].exec(&state) } if !quiet { for _, value := range state.value { err := state.out.Write(value) if err != nil { panic("Error while outputting") } } } } state.in.AssertDone() state.out.AssertDone() }