r: Fix setting *ARGV*
[jackhill/mal.git] / r / step9_try.r
index 64c65b5..80c4948 100644 (file)
@@ -81,7 +81,7 @@ EVAL <- function(ast, env) {
 
     # apply list
     ast <- macroexpand(ast, env)
-    if (!.list_q(ast)) return(ast)
+    if (!.list_q(ast)) return(eval_ast(ast, env))
 
     switch(paste("l",length(ast),sep=""),
            l0={ return(ast) },
@@ -163,7 +163,7 @@ rep <- function(str) return(PRINT(EVAL(READ(str), repl_env)))
 # core.r: defined using R
 for(k in names(core_ns)) { Env.set(repl_env, k, core_ns[[k]]) }
 Env.set(repl_env, "eval", function(ast) EVAL(ast, repl_env))
-Env.set(repl_env, "*ARGV*", function(ast) EVAL(ast, repl_env))
+Env.set(repl_env, "*ARGV*", new.list())
 
 # core.mal: defined using the language itself
 . <- rep("(def! not (fn* (a) (if a false true)))")
@@ -172,6 +172,16 @@ Env.set(repl_env, "*ARGV*", function(ast) EVAL(ast, repl_env))
 . <- 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))))))))")
 
 
+args <- commandArgs(trailingOnly = TRUE)
+if (length(args) > 0) {
+    Env.set(repl_env, "*ARGV*", new.listl(slice(as.list(args),2)))
+    tryCatch({
+        . <- rep(concat("(load-file \"", args[[1]], "\")"))
+    }, error=function(err) {
+        cat("Error: ", get_error(err),"\n", sep="")
+    })
+    quit(save="no", status=0)
+}
 
 repeat {
     line <- readline("user> ")