github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/cmds/exp/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 forth package debugging using log.Printf")
    20  
    21  func main() {
    22  	var b = make([]byte, 512)
    23  	flag.Parse()
    24  	if *debug {
    25  		forth.Debug = log.Printf
    26  	}
    27  	f := forth.New()
    28  	for {
    29  		fmt.Printf("%sOK\n", f.Stack())
    30  		n, err := os.Stdin.Read(b)
    31  		if err != nil {
    32  			if err != io.EOF {
    33  				log.Fatal(err)
    34  			}
    35  			// Silently exit on EOF. It's the unix way.
    36  			break
    37  		}
    38  		if err := forth.EvalString(f, string(b[:n])); err != nil {
    39  			fmt.Printf("%v\n", err)
    40  		}
    41  	}
    42  }