prosper/main.go

75 lines
1.9 KiB
Go
Raw Normal View History

2021-02-14 02:09:56 +00:00
package main
import (
"bufio"
2021-02-14 02:37:56 +00:00
"errors"
2021-02-14 02:09:56 +00:00
"fmt"
"os"
"strings"
)
func main() {
stack := Stack{values: []int{}}
2021-02-14 16:58:43 +00:00
rstack := Stack{values: []int{}}
2021-02-14 02:09:56 +00:00
dict := Dictionary{}
b := &Builtins{}
dict.AddWord("+", b.Add(&stack), nil)
dict.AddWord("-", b.Sub(&stack), nil)
dict.AddWord("*", b.Mul(&stack), nil)
dict.AddWord("/", b.Div(&stack), nil)
dict.AddWord(".", b.Print(&stack), nil)
dict.AddWord("=", b.Eq(&stack), nil)
dict.AddWord("0=", nil, []string{"0", "="})
dict.AddWord("<>", b.NEq(&stack), nil)
dict.AddWord(">", b.Gt(&stack), nil)
dict.AddWord("<", b.Lt(&stack), nil)
dict.AddWord(">=", b.GtEq(&stack), nil)
dict.AddWord("<=", b.LtEq(&stack), nil)
dict.AddWord("0<", nil, []string{"0", "<"})
dict.AddWord("0>", nil, []string{"0", ">"})
dict.AddWord("DUP", b.Dup(&stack), nil)
dict.AddWord("SWAP", b.Swap(&stack), nil)
dict.AddWord("OVER", b.Over(&stack), nil)
dict.AddWord("DROP", b.Drop(&stack), nil)
dict.AddWord("ROT", b.Rot(&stack), nil)
dict.AddWord("WORDS", b.Words(dict), nil)
dict.AddWord(".S", b.Debug(&stack), nil)
dict.AddWord("EMIT", b.Emit(&stack), nil)
dict.AddWord("R>", b.RFrom(&stack, &rstack), nil)
dict.AddWord(">R", b.ToR(&stack, &rstack), nil)
dict.AddWord("R@", b.RFetch(&stack, &rstack), nil)
dict.AddWord("CR", b.CR(), nil)
dict.AddWord("QUIT", b.Quit(), nil)
2021-02-14 02:09:56 +00:00
c := Context{
Dictionary: dict,
Stack: &stack,
2021-02-14 16:58:43 +00:00
RStack: &rstack,
2021-02-14 02:09:56 +00:00
}
reader := bufio.NewReader(os.Stdin)
fmt.Print("prosper")
// read loop
for {
fmt.Print("\n> ")
line, err := reader.ReadString('\n')
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
line = strings.TrimSpace(line)
err = c.Eval(line + " ") // append a space to make sure we always close out our parse loop
2021-02-14 02:37:56 +00:00
if errors.Is(err, ErrExit) {
fmt.Printf("bye\n")
os.Exit(0)
} else if err != nil {
fmt.Printf("error in evaluation: %v", err)
} else {
fmt.Print("ok")
2021-02-14 02:09:56 +00:00
}
}
}