<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/main/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main/main.go')
-rw-r--r--main/main.go119
1 files changed, 34 insertions, 85 deletions
diff --git a/main/main.go b/main/main.go
index 31e46c6..5503fb1 100644
--- a/main/main.go
+++ b/main/main.go
@@ -1,7 +1,6 @@
package main
import (
- "fmt"
"os"
"bufio"
)
@@ -23,117 +22,67 @@ type ValueString string
type WalkValue interface {}
+type WalkItem struct {
+ value WalkValue
+ path Path
+}
+
type Program []Command
type ProgramState struct {
- space WalkItem
+ space []WalkItem
in chan WalkItem
out chan WalkItem
program []Command
}
-type StringSegmentPathFilterAST struct {
- index string
-}
-func (ast StringSegmentPathFilterAST) compileWith(next PathFilterState) PathFilterState {
- return StringSegmentPathFilter {
- index: ast.index,
- next: next,
+func main() {
+ 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
}
-}
-
-type RepeatPathFilterAST struct {
- content PathFilterAST
-}
-func (ast RepeatPathFilterAST) compileWith(next PathFilterState) PathFilterState {
- nextGroup := &GroupPathFilter{}
- repeatStart := ast.content.compileWith(nextGroup)
- nextGroup.filters = []PathFilterState{next, repeatStart}
- return nextGroup
-}
-
-type SequencePathFilterAST struct {
- sequence []PathFilterAST
-}
-func (ast SequencePathFilterAST) compileWith(next PathFilterState) PathFilterState {
- for i := len(ast.sequence) - 1; i >= 0; i -= 1 {
- next = ast.sequence[i].compileWith(next)
+ if !hasInput {
+ panic("Missing program")
}
- return next
-}
-type AnySegmentPathFilterAST struct {}
-func (ast AnySegmentPathFilterAST) compileWith(next PathFilterState) PathFilterState {
- return AnySegmentPathFilter{next: next}
-}
+ tokens := Lex(input)
+ program := Parse(tokens)
-type PathFilterAST interface {
- compileWith(PathFilterState) PathFilterState
-}
-
-func compilePathFilterAST(ast PathFilterAST) PathFilter {
- return PathFilter{
- initial: ast.compileWith(NonePathFilter{}),
- }
-}
-
-func main() {
- if len(os.Args) < 2 {
- fmt.Println("Missing program arg")
- return
- }
- //input := os.Args[1]
- //tokens := Lex(input)
- //program := Parse(tokens)
-
stdin := bufio.NewReader(os.Stdin)
dataStream := Json(stdin)
-
- var allRemainingPathFilter AnySegmentPathFilter
- {
- g := GroupPathFilter {
- filters: []PathFilterState{NonePathFilter{}},
- }
- allRemainingPathFilter = AnySegmentPathFilter {
- next: PathFilterState(&g),
- }
- g.filters = append(g.filters, PathFilterState(&allRemainingPathFilter))
- }
state := ProgramState {
in: dataStream,
out: make(chan WalkItem),
- program: []Command {
- FilteredCommand {
- filter: compilePathFilterAST(
- StringSegmentPathFilterAST {"people"},
- ),
- command: PrintValueCommand{},
- },
- FilteredCommand {
- filter: compilePathFilterAST(
- SequencePathFilterAST {
- []PathFilterAST{
- StringSegmentPathFilterAST {"people"},
- AnySegmentPathFilterAST{},
- StringSegmentPathFilterAST {"age"},
- },
- },
- ),
- command: PrintValueCommand{},
- },
- },
+ program: program,
}
go func () {
for walkItem := range dataStream {
- state.space = walkItem
+ 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)
-}
+} \ No newline at end of file