<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/main.go
blob: 46b83e70dc7e24d0d5395c962817eb9f7bf8818a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main

import (
	"os"
	"bufio"
	"fmt"
	"main/subex"
)

type PathSegment interface {}
type Path []PathSegment

type TerminalValue int
const (
	ArrayBegin TerminalValue = iota
	ArrayEnd
	MapBegin
	MapEnd
)
type ValueNull struct {}
type ValueBool bool
type ValueNumber float64
type ValueString string

type WalkValue interface {}

type WalkItem struct {
	value WalkValue
	path Path
}

type Program []Command

type ProgramState struct {
	space []WalkItem
	in chan WalkItem
	out chan WalkItem
	program []Command
}

func main() {
	if len(os.Args) != 3 {
		panic("Expected: stred [input] [subex]")
	}
	input := os.Args[1]
	program := os.Args[2]
	ast := subex.Parse(program)
	transducer := subex.CompileTransducer(ast)
	output, err := subex.RunTransducer(transducer, input)
	if err {
		output = input
	}
	fmt.Println(output)
}

func mainISH() {
	quiet := false
	var input string
	hasInput := false
	
	for i := 1; i < len(os.Args); i += 1 {
		switch os.Args[i] {
			case "-n":
				quiet = true
				continue
		}
		if i < len(os.Args) - 1 {
			panic("Unexpected arguments after program")
		}
		input = os.Args[i]
		hasInput = true
	}
	if !hasInput {
		panic("Missing program")
	}

	tokens := Lex(input)
	program := Parse(tokens)

	stdin := bufio.NewReader(os.Stdin)
	dataStream := Json(stdin)

	state := ProgramState {
		in: dataStream,
		out: make(chan WalkItem),
		program: program,
	}
	
	go func () {
		for walkItem := range dataStream {
			state.space = []WalkItem{walkItem}
			for _, cmd := range state.program {
				cmd.exec(&state)
			}
			if !quiet {
				for _, item := range state.space {
					state.out <- item
				}
			}
		}
		close(state.out)
	}()
	
	JsonOut(state.out)
}