objpascal, r: Support catchless try*.
[jackhill/mal.git] / r / step8_macros.r
index d0d6c7e..d8d3ba1 100644 (file)
@@ -59,6 +59,13 @@ eval_ast <- function(ast, env) {
         new.listl(lapply(ast, function(a) EVAL(a, env)))
     } else if (.vector_q(ast)) {
         new.vectorl(lapply(ast, function(a) EVAL(a, env)))
+    } else if (.hash_map_q(ast)) {
+        lst <- list()
+        for(k in ls(ast)) {
+            lst[[length(lst)+1]] = k
+            lst[[length(lst)+1]] = EVAL(ast[[k]], env)
+        }
+        new.hash_mapl(lst)
     } else {
         ast
     }
@@ -68,13 +75,12 @@ EVAL <- function(ast, env) {
     repeat {
 
     #cat("EVAL: ", .pr_str(ast,TRUE), "\n", sep="")
-    if (!.list_q(ast)) {
-        return(eval_ast(ast, env))
-    }
+    if (!.list_q(ast)) { return(eval_ast(ast, env)) }
+    if (length(ast) == 0) { return(ast) }
 
     # 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) },
@@ -142,7 +148,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)))")
@@ -151,6 +157,12 @@ 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)))
+    . <- rep(concat("(load-file \"", args[[1]], "\")"))
+    quit(save="no", status=0)
+}
 
 repeat {
     line <- readline("user> ")