1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
package main
import (
"os"
"fmt"
)
type TransducerOutput interface {
build(Store) string
}
type TransducerReplacementRune rune
func (replacement TransducerReplacementRune) build(store Store) string {
return string(replacement)
}
type TransducerReplacementLoad rune
func (replacement TransducerReplacementLoad) build(store Store) string {
return store[rune(replacement)]
}
type Store map[rune]string
func (store Store) clone() Store {
newStore := make(Store)
for key, val := range store {
newStore[key] = val
}
return newStore
}
func compileTransducer(transducerAst SubexAST) SubexState {
return transducerAst.compileWith(SubexNoneState{})
}
type SubexBranch struct {
store Store
state SubexState
output string
}
func (pair SubexBranch) eat(char rune) []SubexBranch {
states := pair.state.eat(pair.store, char)
for i := range states {
states[i].output = pair.output + states[i].output
}
return states
}
func (pair SubexBranch) accepting() []string {
return pair.state.accepting(pair.store)
}
func runTransducer(transducer SubexState, input string) (output string, err bool) {
states := []SubexBranch{{
state: transducer,
output: "",
store: make(Store),
}}
for _, char := range input {
var newStates []SubexBranch
for _, state := range states {
newStates = append(newStates, state.eat(char)...)
}
states = newStates
}
for _, state := range states {
outputEnds := state.accepting()
for _, outputEnd := range outputEnds {
return state.output + outputEnd, false
}
}
return "", true
}
func main() {
if len(os.Args) != 3 {
panic("Expected: program [input] [subex]")
}
input := os.Args[1]
program := os.Args[2]
ast := parse(program)
transducer := compileTransducer(ast)
output, err := runTransducer(transducer, input)
if err {
output = input
}
fmt.Println(output)
}
|