shad-go/forth/main.go

45 lines
683 B
Go
Raw Normal View History

2022-02-10 22:06:57 +00:00
//go:build !change
2021-02-18 20:13:55 +00:00
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
const exitCommand = "bye"
func main() {
e := NewEvaluator()
fmt.Printf("Welcome to Forth evaluator! To exit type %q.\n", exitCommand)
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print(">")
scanner.Scan()
text := scanner.Text()
if text == exitCommand {
break
}
stack, err := e.Process(text)
if err != nil {
fmt.Printf("Evaluation error: %s\n", err)
}
printStack(stack)
}
}
func printStack(stack []int) {
s := make([]string, 0, len(stack))
for _, n := range stack {
s = append(s, strconv.Itoa(n))
}
fmt.Printf("Stack: %s\n", strings.Join(s, ", "))
}