From c19df3ff75e7693e38940f20a5f3b40931be424a Mon Sep 17 00:00:00 2001 From: Charlie Stanton Date: Fri, 23 Dec 2022 15:53:24 +0000 Subject: Changes main to read input from stdin. Prunes states that won't wouldn't ever reach the front of the state priority queue. --- main/main.go | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'main/main.go') diff --git a/main/main.go b/main/main.go index 0466371..cc671ee 100644 --- a/main/main.go +++ b/main/main.go @@ -3,6 +3,7 @@ package main import ( "os" "fmt" + "io" ) type TransducerOutput interface { @@ -53,6 +54,23 @@ func (pair SubexBranch) accepting() []string { return pair.state.accepting(pair.store) } +func equalStates(left SubexBranch, right SubexBranch) bool { + // Only care about if they are the same pointer + return left.state == right.state +} + +func pruneStates(states []SubexBranch) (newStates []SubexBranch) { + outer: for _, state := range states { + for _, newState := range newStates { + if equalStates(state, newState) { + continue outer + } + } + newStates = append(newStates, state) + } + return newStates +} + func runTransducer(transducer SubexState, input string) (output string, err bool) { states := []SubexBranch{{ state: transducer, @@ -64,7 +82,7 @@ func runTransducer(transducer SubexState, input string) (output string, err bool for _, state := range states { newStates = append(newStates, state.eat(char)...) } - states = newStates + states = pruneStates(newStates) } for _, state := range states { outputEnds := state.accepting() @@ -76,16 +94,20 @@ func runTransducer(transducer SubexState, input string) (output string, err bool } func main() { - if len(os.Args) != 3 { - panic("Expected: program [input] [subex]") + if len(os.Args) != 2 { + panic("Expected: program [subex]") + } + inputBytes, inputErr := io.ReadAll(os.Stdin) + input := string(inputBytes) + if inputErr != nil { + fmt.Println("Error reading") } - input := os.Args[1] - program := os.Args[2] + program := os.Args[1] ast := parse(program) transducer := compileTransducer(ast) output, err := runTransducer(transducer, input) if err { output = input } - fmt.Println(output) + fmt.Print(output) } -- cgit v1.2.3