main.go (1279B)
1 package main 2 3 import ( 4 "os" 5 "bufio" 6 "main/walk" 7 "main/json" 8 ) 9 10 type Program []Command 11 12 type ProgramState struct { 13 path, value, xreg, yreg, zreg walk.ValueList 14 in walk.StredReader 15 out walk.StredWriter 16 program []Command 17 pc int 18 } 19 20 func main() { 21 quiet := false 22 var input string 23 hasInput := false 24 25 for i := 1; i < len(os.Args); i += 1 { 26 switch os.Args[i] { 27 case "-n": 28 quiet = true 29 continue 30 } 31 if i < len(os.Args) - 1 { 32 panic("Unexpected arguments after program") 33 } 34 input = os.Args[i] 35 hasInput = true 36 } 37 if !hasInput { 38 panic("Missing program") 39 } 40 41 tokens := Lex(input) 42 program := Parse(tokens) 43 44 stdin := bufio.NewReader(os.Stdin) 45 stdout := bufio.NewWriter(os.Stdout) 46 47 state := ProgramState { 48 in: json.NewJSONReader(stdin), 49 out: json.NewJSONWriter(stdout), 50 program: program, 51 } 52 53 for { 54 walkItem, err := state.in.Read() 55 if err != nil { 56 break 57 } 58 state.value = walkItem.Value 59 state.path = walkItem.Path 60 state.pc = 0 61 for state.pc < len(state.program) { 62 state.program[state.pc].exec(&state) 63 } 64 if !quiet { 65 err := state.out.Write(walk.WalkItem { 66 Path: state.path, 67 Value: state.value, 68 }) 69 if err != nil { 70 panic("Error while outputting") 71 } 72 } 73 } 74 75 state.in.AssertDone() 76 state.out.AssertDone() 77 }