diff --git a/builtins.go b/builtins.go index 6af28d4..fac4261 100644 --- a/builtins.go +++ b/builtins.go @@ -321,20 +321,26 @@ func (b *Builtins) Rot(s *Stack) func() error { } // Words outputs a list of all known words in the dictionary -func (b *Builtins) Words(d Dictionary) func() error { +func (b *Builtins) Words(out io.Writer, d Dictionary) func() error { + if out == nil { + out = os.Stdout + } return func() error { for n := range d { - fmt.Printf("%s ", n) + fmt.Fprintf(out, "%s ", n) } return nil } } // Flags outputs a list of all flags -func (b *Builtins) Flags(c Context) func() error { +func (b *Builtins) Flags(out io.Writer, c Context) func() error { + if out == nil { + out = os.Stdout + } return func() error { for n := range c.Flags { - fmt.Printf("%s %v\n", n, c.Flags.GetFlag(n)) + fmt.Fprintf(out, "%s %v\n", n, c.Flags.GetFlag(n)) } return nil } @@ -529,9 +535,12 @@ func (b *Builtins) Quit() func() error { } // Debug prints the stack without modifying it -func (b *Builtins) Debug(s *Stack) func() error { +func (b *Builtins) Debug(out io.Writer, s *Stack) func() error { + if out == nil { + out = os.Stdout + } return func() error { - fmt.Print(s.values, " ") + fmt.Fprint(out, s.values, " ") return nil } } diff --git a/main.go b/main.go index 3ebea2b..ecec366 100644 --- a/main.go +++ b/main.go @@ -60,11 +60,11 @@ func main() { dict.AddWord("DROP", Word{Name: "DROP", Impl: b.Drop(&stack)}) dict.AddWord("ROT", Word{Name: "ROT", Impl: b.Rot(&stack)}) // debugging - dict.AddWord("WORDS", Word{Name: "WORDS", Impl: b.Words(dict)}) - dict.AddWord("FLAGS", Word{Name: "FLAGS", Impl: b.Flags(c)}) - dict.AddWord(".S", Word{Name: ".S", Impl: b.Debug(&stack)}) - dict.AddWord(".R", Word{Name: ".R", Impl: b.Debug(&rstack)}) - dict.AddWord(".I", Word{Name: ".I", Impl: b.Debug(&ifstack)}) + dict.AddWord("WORDS", Word{Name: "WORDS", Impl: b.Words(os.Stdout, dict)}) + dict.AddWord("FLAGS", Word{Name: "FLAGS", Impl: b.Flags(os.Stdout, c)}) + dict.AddWord(".S", Word{Name: ".S", Impl: b.Debug(os.Stdout, &stack)}) + dict.AddWord(".R", Word{Name: ".R", Impl: b.Debug(os.Stdout, &rstack)}) + dict.AddWord(".I", Word{Name: ".I", Impl: b.Debug(os.Stdout, &ifstack)}) dict.AddWord("R>", Word{Name: "R>", Impl: b.RFrom(&stack, &rstack)}) dict.AddWord(">R", Word{Name: ">R", Impl: b.ToR(&stack, &rstack)}) dict.AddWord("R@", Word{Name: "R@", Impl: b.RFetch(&stack, &rstack)})