<- Back to shtanton's homepage
summaryrefslogtreecommitdiff
path: root/main/main.go
diff options
context:
space:
mode:
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)
}