plsql: add dockerfile. Lots of cleanup/renaming.
[jackhill/mal.git] / guile / step8_macros.scm
index fc86f76..af36411 100644 (file)
 
 (define *toplevel*
   (receive (b e) (unzip2 core.ns)
-    (make-Env #:binds b #:exprs (map (lambda (x) (make-callable #f x)) e))))
+    (make-Env #:binds b #:exprs (map make-func e))))
 
 (define (READ)
-  (read_str (readline "user> ")))
+  (read_str (_readline "user> ")))
 
 (define (eval_ast ast env)
   (define (_eval x) (EVAL x env))
@@ -42,7 +42,7 @@
    ((func? (car ast))
     => (lambda (c)
          (callable-apply c (map _eval (cdr ast)))))
-   (else 'mal-error (format #f "'~a' is not a valid form to apply!" ast))))
+   (else (throw 'mal-error (format #f "'~a' not found" (car ast))))))
 
 (define (eval_seq ast env)
   (cond
 
 (define (is_macro_call ast env)
   (and (list? ast)
+       (> (length ast) 0)
        (and=> (env-check (car ast) env) is-macro?)))
 
 (define (_macroexpand ast env)
   (cond
    ((is_macro_call ast env)
     => (lambda (c)
-         ;;(format #t "AAA: ~a, ~a~%" ast (_macroexpand (callable-apply c (cdr ast)) env))
-         ;;(format #t "BBB: ~a~%" (_macroexpand (callable-apply c (cdr ast)) env))
          ;; NOTE: Macros are normal-order, so we shouldn't eval args here.
          ;;       Or it's applicable-order.
          (_macroexpand (callable-apply c (cdr ast)) env)))
   ;;       TCO in Scheme to implement TCO, but it's the same principle with normal loop.
   ;;       If you're Lispy enough, there's no recursive at all while you saw named let loop.
   (let tco-loop((ast ast) (env env)) ; expand as possible
-    ;;(format #t "CCC: ~a === ~a~%" ast (_macroexpand ast env))
     (let ((ast (_macroexpand ast env)))
       (match ast
         ((? non-list?) (eval_ast ast env))
+        (() ast)
         (('defmacro! k v)
          (let ((c (EVAL v env)))
            (callable-is_macro-set! c #t)
 
 (define (PRINT exp)
   (and (not (eof-object? exp))
-       ;;(add-history str)
        (format #t "~a~%" (pr_str exp #t))))
 
 (define (LOOP continue?)
 (EVAL-string "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
 (EVAL-string "(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)))))))")
 (EVAL-string "(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))))))))")
-(REPL)
+
+(let ((args (cdr (command-line))))
+  (cond
+   ((> (length args) 0)
+    ((*toplevel* 'set) '*ARGV* (cdr args))
+    (EVAL-string (string-append "(load-file \"" (car args) "\")")))
+   (else (REPL))))