Merge pull request #383 from asarhaddon/ada2tco-do
[jackhill/mal.git] / mal / step6_file.mal
index 34acd67..23df09a 100644 (file)
@@ -1,5 +1,5 @@
-(load-file "../mal/types.mal")
 (load-file "../mal/env.mal")
+(load-file "../mal/core.mal")
 
 ;; read
 (def! READ (fn* [strng]
@@ -37,6 +37,9 @@
     ;; apply list
     (let* [a0 (first ast)]
       (cond
+        (nil? a0)
+        ast
+
         (= 'def! a0)
         (env-set env (nth ast 1) (EVAL (nth ast 2) env))
 
@@ -57,7 +60,7 @@
               (EVAL (nth ast 3) env)
               nil)
             (EVAL (nth ast 2) env)))
-      
+
         (= 'fn* a0)
         (fn* [& args]
           (EVAL (nth ast 2) (new-env env (nth ast 1) args)))
 ;; repl
 (def! repl-env (new-env))
 (def! rep (fn* [strng]
-  (PRINT (EVAL (READ strng), repl-env))))
-
-(def! _ref (fn* [k v] (env-set repl-env k v)))
+  (PRINT (EVAL (READ strng) repl-env))))
 
-;; Import types related functions
-(map (fn* [data] (_ref (nth data 0) (nth data 1))) types_ns)
-
-;; Defined using the language itself
-(_ref 'read-string read-string)
-(_ref 'eval (fn* [ast] (EVAL ast repl-env)))
-(_ref 'slurp slurp)
-(_ref 'slurp-do slurp-do)
+;; core.mal: defined directly using mal
+(map (fn* [data] (env-set repl-env (nth data 0) (nth data 1))) core_ns)
+(env-set repl-env 'eval (fn* [ast] (EVAL ast repl-env)))
+(env-set repl-env '*ARGV* (rest *ARGV*))
 
+;; core.mal: defined using the new language itself 
 (rep "(def! not (fn* [a] (if a false true)))")
-(rep "(def! load-file (fn* [f] (eval (read-string (slurp-do f)))))")
+(rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
 
-(def! -main (fn* [] 
+;; repl loop
+(def! repl-loop (fn* []
   (let* [line (readline "mal-user> ")]
     (if line
       (do
         (if (not (= "" line))
           (try*
-            (let* [res (rep line)]
-              (println res))
+            (println (rep line))
             (catch* exc
               (println "Uncaught exception:" exc))))
-        (-main))))))
-(-main)
+        (repl-loop))))))
+
+(def! -main (fn* [& args] 
+  (if (> (count args) 0)
+    (rep (str "(load-file \"" (first args) "\")"))
+    (repl-loop))))
+(apply -main *ARGV*)