<- 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.go63
1 files changed, 60 insertions, 3 deletions
diff --git a/subex/main.go b/subex/main.go
index 9dbe5df..e53a3c6 100644
--- a/subex/main.go
+++ b/subex/main.go
@@ -5,6 +5,7 @@ import (
"fmt"
"bufio"
"main/walk"
+ "strings"
)
// A part of an insertion, either a datum or a slot from which to load
@@ -136,10 +137,66 @@ func Main() {
close(out)
}(pieces, tokens)
output, err := RunTransducer(transducer, pieces)
- // TODO recombine data into values and then convert into items with empty paths
if !err {
- fmt.Print(output)
+ dataIn := make(chan walk.Datum)
+ go func(out chan<- walk.Datum, in []walk.Datum) {
+ for _, datum := range in {
+ out<-datum
+ }
+ close(out)
+ }(dataIn, output)
+ valueOut := make(chan walk.WalkValue)
+ go func(out chan<- walk.WalkValue, in <-chan walk.Datum) {
+ for {
+ datum, hasDatum := <-in
+ if !hasDatum {
+ break
+ }
+ switch v := datum.(type) {
+ case walk.TerminalValue:
+ out<-v
+ continue
+ case walk.ValueNull:
+ out<-v
+ continue
+ case walk.ValueBool:
+ out<-v
+ continue
+ case walk.ValueNumber:
+ out<-v
+ continue
+ case rune:
+ panic("Error! Rune output by subex but not in a string")
+ case walk.EndString:
+ panic("Error! subex output an EndString before BeginString")
+ case walk.StartString:
+ default:
+ panic("Unknown datum type")
+ }
+ // Handle string start
+ var builder strings.Builder
+ loop: for {
+ datum, hasDatum := <-in
+ if !hasDatum {
+ panic("Missing EndString")
+ }
+ switch v := datum.(type) {
+ case walk.EndString:
+ break loop
+ case rune:
+ builder.WriteRune(v)
+ default:
+ panic("Invalid datum in string")
+ }
+ }
+ out<-walk.ValueString(builder.String())
+ }
+ close(out)
+ }(valueOut, dataIn)
+ for value := range valueOut {
+ fmt.Println(value)
+ }
} else {
- fmt.Print("Error")
+ fmt.Println("Error")
}
}