You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
995 B
Go
50 lines
995 B
Go
3 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"strconv"
|
||
|
"os"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func prelude() {
|
||
|
dictionary[":"] = operation{op: newop, readuntil: ";"}
|
||
|
dictionary["base"] = operation{op: base}
|
||
|
dictionary["+"] = operation{op: add}
|
||
|
dictionary["quit"] = operation{op: quit}
|
||
|
dictionary["."] = operation{op: forth("10 base")}
|
||
|
}
|
||
|
|
||
|
func newop(st *[]int, stp *int, code []string) string {
|
||
|
if len(code) > 0 {
|
||
|
dictionary[code[0]] = operation{op: forth(strings.Join(code[1:], " "))}
|
||
|
}
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
func base(st *[]int, stp *int, _ []string) string {
|
||
|
base, _ := getvalue(st, stp)
|
||
|
r, err := getvalue(st, stp)
|
||
|
if err != nil {
|
||
|
return err.Error()
|
||
|
}
|
||
|
return strconv.FormatInt(int64(r), base)
|
||
|
}
|
||
|
|
||
|
func add(st *[]int, stp *int, _ []string) string {
|
||
|
n1, _ := getvalue(st, stp)
|
||
|
n2, err := getvalue(st, stp)
|
||
|
if err != nil {
|
||
|
return err.Error()
|
||
|
}
|
||
|
err = putvalue(st, stp, n1+n2)
|
||
|
if err != nil {
|
||
|
return err.Error()
|
||
|
}
|
||
|
return "*"
|
||
|
}
|
||
|
|
||
|
func quit(_ *[]int, _ *int, _ []string) string {
|
||
|
os.Exit(0)
|
||
|
return "should have quit"
|
||
|
}
|