Add newop operator
parent
f13c785ad7
commit
5b246e6238
@ -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"
|
||||
}
|
Loading…
Reference in New Issue