<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/subex/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'subex/main.go')
-rw-r--r--subex/main.go66
1 files changed, 34 insertions, 32 deletions
diff --git a/subex/main.go b/subex/main.go
index 138de9a..fd59047 100644
--- a/subex/main.go
+++ b/subex/main.go
@@ -8,31 +8,33 @@ import (
"strings"
)
-// A part of an insertion, either a datum or a slot from which to load
+// A part of an insertion, either an Atom or a slot from which to load
// TODO rename this
type TransducerOutput interface {
- // Given the current store, return the []Datum produced by the TransducerOutput
- build(Store) []walk.Datum
+ // Given the current store, return the []Atom produced by the TransducerOutput
+ build(Store) []walk.Atom
}
-// A TransducerOutput which is just a datum literal
-type TransducerReplacementRune struct {
- datum walk.Datum
+// A TransducerOutput which is just an Atom literal
+type TransducerReplacementAtom struct {
+ atom walk.Atom
}
-func (replacement TransducerReplacementRune) build(store Store) []walk.Datum {
- return []walk.Datum{replacement.datum}
+func (replacement TransducerReplacementAtom) build(store Store) []walk.Atom {
+ return []walk.Atom{replacement.atom}
}
+// TODO should be a single field called slot with a type of rune
// A TransducerOutput which is a slot that is loaded from
type TransducerReplacementLoad struct {
- datum walk.Datum
+ atom walk.Atom
}
-func (replacement TransducerReplacementLoad) build(store Store) []walk.Datum {
- return store[replacement.datum]
+func (replacement TransducerReplacementLoad) build(store Store) []walk.Atom {
+ return store[replacement.atom]
}
// Where slots are stored
-type Store map[walk.Datum][]walk.Datum
+// TODO should map from runes as only runes can be slots
+type Store map[walk.Atom][]walk.Atom
// Return a new store with all the data from this one
func (store Store) clone() Store {
newStore := make(Store)
@@ -42,7 +44,7 @@ func (store Store) clone() Store {
return newStore
}
// Return a copy of this store but with an additional slot set
-func (store Store) withValue(key walk.Datum, value []walk.Datum) Store {
+func (store Store) withValue(key walk.Atom, value []walk.Atom) Store {
newStore := store.clone()
newStore[key] = value
return newStore
@@ -60,17 +62,17 @@ type SubexBranch struct {
// State in this branch
state SubexState
// Output so far in this branch
- output []walk.Datum
+ output []walk.Atom
}
// Read a single character and return all the branches resulting from this branch consuming it
-func (pair SubexBranch) eat(char walk.Datum) []SubexBranch {
+func (pair SubexBranch) eat(char walk.Atom) []SubexBranch {
states := pair.state.eat(pair.store, char)
for i := range states {
states[i].output = walk.ConcatData(pair.output, states[i].output)
}
return states
}
-func (pair SubexBranch) accepting() [][]walk.Datum {
+func (pair SubexBranch) accepting() [][]walk.Atom {
return pair.state.accepting(pair.store)
}
@@ -94,7 +96,7 @@ func pruneStates(states []SubexBranch) (newStates []SubexBranch) {
}
// Run the subex transducer
-func RunTransducer(transducer SubexState, input <-chan walk.Datum) (output []walk.Datum, err bool) {
+func RunTransducer(transducer SubexState, input <-chan walk.Atom) (output []walk.Atom, err bool) {
states := []SubexBranch{{
state: transducer,
output: nil,
@@ -129,8 +131,8 @@ func Main() {
program := os.Args[1]
ast := Parse(program)
transducer := CompileTransducer(ast)
- pieces := make(chan walk.Datum)
- go func(out chan<- walk.Datum, input []walk.WalkValue) {
+ pieces := make(chan walk.Atom)
+ go func(out chan<- walk.Atom, input []walk.WalkValue) {
for _, value := range input {
value.Pieces(out)
}
@@ -138,21 +140,21 @@ func Main() {
}(pieces, tokens)
output, err := RunTransducer(transducer, pieces)
if !err {
- dataIn := make(chan walk.Datum)
- go func(out chan<- walk.Datum, in []walk.Datum) {
- for _, datum := range in {
- out<-datum
+ dataIn := make(chan walk.Atom)
+ go func(out chan<- walk.Atom, in []walk.Atom) {
+ for _, atom := range in {
+ out<-atom
}
close(out)
}(dataIn, output)
valueOut := make(chan walk.WalkValue)
- go func(out chan<- walk.WalkValue, in <-chan walk.Datum) {
+ go func(out chan<- walk.WalkValue, in <-chan walk.Atom) {
for {
- datum, hasDatum := <-in
- if !hasDatum {
+ atom, hasAtom := <-in
+ if !hasAtom {
break
}
- switch v := datum.(type) {
+ switch v := atom.(type) {
case walk.TerminalValue:
out<-v
continue
@@ -171,22 +173,22 @@ func Main() {
panic("Error! subex output an EndString before BeginString")
case walk.StartString:
default:
- panic("Unknown datum type")
+ panic("Unknown atom type")
}
// Handle string start
var builder strings.Builder
loop: for {
- datum, hasDatum := <-in
- if !hasDatum {
+ atom, hasAtom := <-in
+ if !hasAtom {
panic("Missing EndString")
}
- switch v := datum.(type) {
+ switch v := atom.(type) {
case walk.EndString:
break loop
case rune:
builder.WriteRune(v)
default:
- panic("Invalid datum in string")
+ panic("Invalid atom in string")
}
}
out<-walk.ValueString(builder.String())