-
Notifications
You must be signed in to change notification settings - Fork 394
/
forth.go
42 lines (37 loc) · 857 Bytes
/
forth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright 2012-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Forth is a forth interpreter.
// It reads a line at a time and puts it through the interpreter.
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"github.com/u-root/u-root/pkg/forth"
)
var debug = flag.Bool("d", false, "Turn on forth package debugging using log.Printf")
func main() {
b := make([]byte, 512)
flag.Parse()
if *debug {
forth.Debug = log.Printf
}
f := forth.New()
for {
fmt.Printf("%vOK\n", f.Stack())
n, err := os.Stdin.Read(b)
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
// Silently exit on EOF. It's the unix way.
break
}
if err := forth.EvalString(f, string(b[:n])); err != nil {
fmt.Printf("%v\n", err)
}
}
}