diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2023-02-26 08:57:32 +0000 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-02-26 08:57:32 +0000 |
commit | 9ba3b1d97e8fd2a2e3c4bb08fe350c8dd5f9733e (patch) | |
tree | 9969939626ef40adb02631d919795bb40cc30e69 /subex/main.go | |
parent | a2636b27fdadb2b7951fa35fe301e8e6b41fc28a (diff) | |
download | stred-go-9ba3b1d97e8fd2a2e3c4bb08fe350c8dd5f9733e.tar |
Converts subex output back into WalkValues and prints for easier debugging
Diffstat (limited to 'subex/main.go')
-rw-r--r-- | subex/main.go | 63 |
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") } } |