<- Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/main/main.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2022-12-23 15:53:24 +0000
committerCharlie Stanton <charlie@shtanton.xyz>2022-12-23 15:53:24 +0000
commitc19df3ff75e7693e38940f20a5f3b40931be424a (patch)
tree5af34ca9683f3b66f3506a5e53df5e0cfd541b81 /main/main.go
parentfb1f51d1da22f34509cf3fe1d174296d36a0f0ad (diff)
downloadsubex-c19df3ff75e7693e38940f20a5f3b40931be424a.tar
Changes main to read input from stdin. Prunes states that won't wouldn't ever reach the front of the state priority queue.
Diffstat (limited to 'main/main.go')
-rw-r--r--main/main.go34
1 files changed, 28 insertions, 6 deletions
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)
}