From 1771ab50b87c745181e4e30f94b63e3f23d33dac Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Thu, 9 Oct 2014 22:10:15 -0500 Subject: go: update README. Backport Func usage. --- go/src/step8_macros/step8_macros.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'go/src/step8_macros') 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) } -- cgit v1.2.3