summaryrefslogtreecommitdiff
path: root/operators.go
diff options
context:
space:
mode:
Diffstat (limited to 'operators.go')
-rw-r--r--operators.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/operators.go b/operators.go
new file mode 100644
index 0000000..9fd3769
--- /dev/null
+++ b/operators.go
@@ -0,0 +1,49 @@
+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"
+}