<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/subex
diff options
context:
space:
mode:
Diffstat (limited to 'subex')
-rw-r--r--subex/main.go92
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)
}
}