package main import ( "os" "bufio" "main/walk" ) type Program []Command type ProgramState struct { path, value, xreg []walk.Atom in chan walk.WalkItem out chan walk.WalkItem program []Command } 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) dataStream := walk.Json(stdin) state := ProgramState { in: dataStream, out: make(chan walk.WalkItem), 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, } } } } close(state.out) }() walk.JsonOut(state.out) }