diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-18 17:07:25 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-18 17:07:25 +0100 |
commit | 7b48ab9f51a14a540b36722e34b844d21622fcc7 (patch) | |
tree | b6acda783d4c7139a419b773272a34ff9024d2c8 /subex | |
parent | e84cf4ec822c32b4de5098353db2dab700b92bfa (diff) | |
download | stred-go-7b48ab9f51a14a540b36722e34b844d21622fcc7.tar |
Creates functions for compounding atoms back into values in the walk module and uses them in subex/main
Diffstat (limited to 'subex')
-rw-r--r-- | subex/main.go | 92 |
1 files changed, 19 insertions, 73 deletions
diff --git a/subex/main.go b/subex/main.go index 0b87dc9..593dbbd 100644 --- a/subex/main.go +++ b/subex/main.go @@ -5,7 +5,6 @@ import ( "fmt" "bufio" "main/walk" - "strings" ) // Where slots are stored @@ -97,83 +96,30 @@ func Main() { if len(os.Args) != 2 { panic("Expected: program [subex]") } - stdin := bufio.NewReader(os.Stdin); - jsonStream := walk.Json(stdin); - var tokens []walk.WalkValue; - for token := range jsonStream { - tokens = append(tokens, token.Value); - } program := os.Args[1] ast := Parse(program) transducer := CompileTransducer(ast) - pieces := make(chan walk.Atom) - go func(out chan<- walk.Atom, input []walk.WalkValue) { - for _, value := range input { - value.Pieces(out) + + stdin := bufio.NewReader(os.Stdin); + jsonStream := walk.Json(stdin); + tokenStream := make(chan walk.WalkValue) + go func(in <-chan walk.WalkItem, out chan<- walk.WalkValue) { + for item := range in { + out<-item.Value } close(out) - }(pieces, tokens) - output, err := RunTransducer(transducer, pieces) - if !err { - 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.Atom) { - for { - atom, hasAtom := <-in - if !hasAtom { - break - } - switch v := atom.(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 atom type") - } - // Handle string start - var builder strings.Builder - loop: for { - atom, hasAtom := <-in - if !hasAtom { - panic("Missing EndString") - } - switch v := atom.(type) { - case walk.EndString: - break loop - case rune: - builder.WriteRune(v) - default: - panic("Invalid atom in string") - } - } - out<-walk.ValueString(builder.String()) - } - close(out) - }(valueOut, dataIn) - for value := range valueOut { - fmt.Println(value) - } - } else { + }(jsonStream, tokenStream) + + atoms := walk.Atomise(tokenStream) + + output, err := RunTransducer(transducer, atoms) + if err { fmt.Println("Error") + return + } + + valueOut := walk.MemoryCompound(output) + for _, value := range valueOut { + fmt.Println(value) } } |