Commits

Anonymous committed 3d7ffcd

rewrite syntax-quote to not expand to Scheme quasiquotation (easier on the eyes)

Comments (0)

Files changed (1)

 
 (define +nil+ (make-clojure-symbol #f 'nil))
 
+(define (compile-keyword form)
+  `(~make-clojure-keyword
+    ',(clojure-keyword-namespace form)
+    ',(clojure-keyword-name form)))
+
 (define (compile-syntax-quote form)
   (match form
     ((? clojure-symbol?)
      (debug syntax-quote-sym: form)
      (cond ((clojure-reserved-symbol? form)
-            (list 'unquote (clojure-compile form)))
+            (clojure-compile form))
            ((auto-gensym-name form) => 
             (lambda (name)
-              (list 'unquote `(~clojure-gensym ',name ~gensyms))))
+              `(~clojure-gensym ',name ~gensyms)))
            (else 
             (let ((var (clojure-var-ref* form)))
-              (list 'unquote
-                    `(~make-clojure-symbol
-                      ',(cond
-                         ((clojure-primitive? var)
-                          #f)
-                         (var
-                          (clojure-namespace-name (clojure-var-namespace var)))
-                         ((clojure-symbol-namespace form) => identity)
-                         (else
-                          (clojure-namespace-name (current-clojure-namespace))))
-                      ',(clojure-symbol-name form)))))))
-    (((and (or 'unquote 'unquote-splicing) unquote*) form)
-     (list unquote* (clojure-compile form)))
+              `(~make-clojure-symbol
+                ',(cond
+                   ((clojure-primitive? var)
+                    ;; TODO: return 'clojure.core instead?
+                    #f)
+                   (var
+                    (clojure-namespace-name (clojure-var-namespace var)))
+                   ((clojure-symbol-namespace form) => identity)
+                   (else
+                    (clojure-namespace-name (current-clojure-namespace))))
+                ',(clojure-symbol-name form))))))
+    (((or 'unquote) form)
+     (clojure-compile form))
     (((or 'quote 'quasiquote) form)
-     (list 'quote (compile-syntax-quote form)))
+     `(~list 'quote ,(compile-syntax-quote form)))
     ((? list?)
-     (map compile-syntax-quote form))
+     (let loop ((forms form))
+       (match forms
+         (() ''())
+         ((('unquote-splicing form) rest ...)
+          (list '~append
+                (clojure-compile form)
+                (loop rest)))
+         ((form rest ...)
+          (list '~cons
+                (compile-syntax-quote form)
+                (loop rest))))))
     ((? clojure-vector?)
-     (list 'unquote
-           `(~make-clojure-vector
-             (~quasiquote ,(map compile-syntax-quote
-                                (clojure-vector-elements form))))))
+     `(~make-clojure-vector
+       (~list . ,(map compile-syntax-quote
+                      (clojure-vector-elements form)))))
     ((? clojure-map?)
-     (list 'unquote
-           `(~make-clojure-map
-             (~quasiquote
-              ,(map (lambda (x)
-                      (list 'unquote
-                            `(~cons
-                              (~quasiquote ,(compile-syntax-quote (car x)))
-                              (~quasiquote ,(compile-syntax-quote (cdr x))))))
-                    (clojure-map-entries form))))))
+     `(~make-clojure-map
+       (~list . ,(map (lambda (x)
+                        (list '~cons
+                              (compile-syntax-quote (car x))
+                              (compile-syntax-quote (cdr x))))
+                      (clojure-map-entries form)))))
+    ((? clojure-keyword?)
+     (compile-keyword form))
+    ((or (? number?) (? string?))
+     form)
     (else
-     ;; TODO: throw an error instead?
-     ;; (error 'compile-syntax-quote "don't know how to syntax-quote" form)
-     (list 'unquote (clojure-compile form)))))
+     (error 'compile-syntax-quote "don't know how to syntax-quote" form))))
 
 (define compile-defmacro
   (match-lambda
     (() '(quote ()))
     (('quasiquote form)
      `(~let ((~gensyms (~list (~list))))
-            (~quasiquote ,(compile-syntax-quote form))))
+            ,(compile-syntax-quote form)))
     ((? clojure-dispatch?)
      (compile-dispatch form))
     ((? clojure-map?)
      `(~make-clojure-vector
        (~list . ,(map clojure-compile (clojure-vector-elements form)))))
     ((? clojure-keyword?)
-     `(~make-clojure-keyword
-       ',(clojure-keyword-namespace form)
-       ',(clojure-keyword-name form)))
+     (compile-keyword form))
     ((? clojure-symbol? sym)
      (compile-symbol sym))
     (((? clojure-keyword? key) map rest ...)