github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/xcmds/forth/forth.go (about) 1 // Copyright 2012-2017 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Forth is a forth interpreter. 6 // It reads a line at a time and puts it through the interpreter. 7 package main 8 9 import ( 10 "flag" 11 "fmt" 12 "io" 13 "log" 14 "os" 15 16 "github.com/u-root/u-root/pkg/forth" 17 ) 18 19 var debug = flag.Bool("d", false, "Turn on stack dump after each Eval") 20 21 func main() { 22 var b = make([]byte, 512) 23 flag.Parse() 24 f := forth.New() 25 for { 26 n, err := os.Stdin.Read(b) 27 if err != nil { 28 if err != io.EOF { 29 log.Fatal(err) 30 } 31 // Silently exit on EOF. It's the unix way. 32 break 33 } 34 // NOTE: should be f.Eval. Why did I not do that? There was a reason ... 35 // I don't remember what it was 36 s, err := forth.Eval(f, string(b[:n])) 37 if err != nil { 38 fmt.Printf("%v\n", err) 39 } 40 if *debug { 41 fmt.Printf("%v", f.Stack()) 42 } 43 fmt.Printf("%s\n", s) 44 // And push it back. It's much more convenient to have it 45 // always on TOS. 46 f.Push(s) 47 } 48 }