abstract the output of the last few places I missed

This commit is contained in:
David 2021-02-20 17:12:34 -05:00
parent 6efa404712
commit 06ad13dd28
2 changed files with 20 additions and 11 deletions

View File

@ -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
}
}

10
main.go
View File

@ -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)})