aboutsummaryrefslogtreecommitdiff
path: root/go/src/step8_macros
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-10-09 22:10:15 -0500
committerJoel Martin <github@martintribe.org>2015-01-06 21:57:02 -0600
commit1771ab50b87c745181e4e30f94b63e3f23d33dac (patch)
treec1c9b5d80ba9261c5f16bab1b52c1bb0f559407c /go/src/step8_macros
parentf2544a9467ea032aff505b3ced3b4b3510a828fe (diff)
downloadmal-1771ab50b87c745181e4e30f94b63e3f23d33dac.tar.gz
mal-1771ab50b87c745181e4e30f94b63e3f23d33dac.zip
go: update README. Backport Func usage.
Diffstat (limited to 'go/src/step8_macros')
-rw-r--r--go/src/step8_macros/step8_macros.go17
1 files changed, 11 insertions, 6 deletions
diff --git a/go/src/step8_macros/step8_macros.go b/go/src/step8_macros/step8_macros.go
index 0feb739..3264159 100644
--- a/go/src/step8_macros/step8_macros.go
+++ b/go/src/step8_macros/step8_macros.go
@@ -206,9 +206,9 @@ func EVAL(ast MalType, env EnvType) (MalType, error) {
env, e = NewEnv(fn.Env, fn.Params, List{el.(List).Val[1:],nil})
if e != nil { return nil, e }
} else {
- fn, ok := f.(func([]MalType)(MalType, error))
+ fn, ok := f.(Func)
if !ok { return nil, errors.New("attempt to call non-function") }
- return fn(el.(List).Val[1:])
+ return fn.Fn(el.(List).Val[1:])
}
}
@@ -237,15 +237,17 @@ func rep(str string) (MalType, error) {
func main() {
// core.go: defined using go
for k, v := range core.NS {
- repl_env.Set(k, v)
+ repl_env.Set(k, Func{v.(func([]MalType)(MalType,error)),nil})
}
- repl_env.Set("eval", func(a []MalType) (MalType, error) {
- return EVAL(a[0], repl_env) })
+ repl_env.Set("eval", Func{func(a []MalType) (MalType, error) {
+ return EVAL(a[0], repl_env) },nil})
repl_env.Set("*ARGV*", List{})
// core.mal: defined using the language itself
rep("(def! not (fn* (a) (if a false true)))")
rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
+ rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))")
+ rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))")
// called with mal script to load and eval
if len(os.Args) > 1 {
@@ -254,7 +256,10 @@ func main() {
args = append(args, a)
}
repl_env.Set("*ARGV*", List{args,nil})
- rep("(load-file \"" + os.Args[1] + "\")")
+ if _,e := rep("(load-file \"" + os.Args[1] + "\")"); e != nil {
+ fmt.Printf("Error: %v\n", e)
+ os.Exit(1)
+ }
os.Exit(0)
}