diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2024-03-30 22:15:57 +0000 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2024-03-30 22:15:57 +0000 |
commit | 256450cc3dcdd9a9b92a33642739f7143526e9b9 (patch) | |
tree | 149e1e681a5834187c395fb3b13e45291fce0828 /main/main.go | |
parent | fd79fd18c6c32884e757e91b8629c87af4cbf34e (diff) | |
download | stred-go-256450cc3dcdd9a9b92a33642739f7143526e9b9.tar |
Add main tests
Diffstat (limited to 'main/main.go')
-rw-r--r-- | main/main.go | 64 |
1 files changed, 40 insertions, 24 deletions
diff --git a/main/main.go b/main/main.go index b7ef568..3a864ad 100644 --- a/main/main.go +++ b/main/main.go @@ -1,6 +1,7 @@ package main import ( + "io" "os" "bufio" "main/walk" @@ -15,32 +16,19 @@ type ProgramState struct { pc int } -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 - } - if !hasInput { - panic("Missing program") - } +type config struct { + quiet bool + program string + in io.Reader + out io.Writer +} - tokens := Lex(input) +func run(config config) { + tokens := Lex(config.program) program := Parse(tokens) - stdin := bufio.NewReader(os.Stdin) - stdout := bufio.NewWriter(os.Stdout) + stdin := bufio.NewReader(config.in) + stdout := bufio.NewWriter(config.out) state := ProgramState { in: json.NewJSONReader(stdin), @@ -58,7 +46,7 @@ func main() { for state.pc < len(state.program) { state.program[state.pc].exec(&state) } - if !quiet { + if !config.quiet { for _, value := range state.value { err := state.out.Write(value) if err != nil { @@ -71,3 +59,31 @@ func main() { state.in.AssertDone() state.out.AssertDone() } + +func main() { + config := config { + quiet: false, + in: os.Stdin, + out: os.Stdout, + } + hasInput := false + + for i := 1; i < len(os.Args); i += 1 { + switch os.Args[i] { + case "-n": + config.quiet = true + continue + } + if i < len(os.Args) - 1 { + panic("Unexpected arguments after program") + } + config.program = os.Args[i] + hasInput = true + } + if !hasInput { + panic("Missing program") + } + + run(config) + +} |