Commits

Moritz Heidkamp committed 7ea2e9b

add support for namespaced symbols and lists

Comments (0)

Files changed (1)

 
 (import chicken scheme)
 (use sexpressive sexpressive-clojure srfi-69
-     matchable data-structures environments)
+     matchable data-structures environments extras)
 
 (define-record clojure-namespace
   name module)
         (eval `(module ,name ()
                        (import
                         (prefix chicken chicken/)
-                        (prefix scheme scheme/))))
+                        (prefix scheme scheme/)
+                        (only scheme quote))))
         (##sys#switch-module mod)
         (module-ref name))))
 
+;; TODO: name: foo.bar, module: foo/bar
 (define (clojure-namespace-ref name #!optional (module name))
   (or (hash-table-ref/default namespaces name #f)
       (let ((ns (make-clojure-namespace name (module-ref module))))
 (define current-namespace
   (make-parameter #f))
 
+(define (clojure-symbol-namespace->module-name sym)
+  (let ((ns (clojure-symbol-namespace sym)))
+    (if ns
+        (string->symbol
+         (string-intersperse
+          (map symbol->string ns) "/"))
+        (error 'clojure-symbol-namespace-module-name
+               "can't get module name of unqualified symbol"
+               sym))))
+
 (define (clojure-symbol->namespace-name sym)
   (let ((ns (clojure-symbol-namespace sym))
         (name (clojure-symbol-name sym)))
     (if (and ns name)
         (error 'clojure-symbol->namespace-name "invalid namespace name" sym)
-        (or name (string->symbol (string-intersperse (map symbol->string ns) "."))))))
+        (or name (clojure-symbol->symbol sym)))))
 
 (define (clojure-symbol->var-name sym)
   (if (clojure-symbol-namespace sym)
   (match form
     ((? eof-object?) (exit))
     ((? string?) form)
+    (('quote expr) form)
     ((? number?) form)
     ((? clojure-symbol? sym)
      ;; TODO: handle namespaced symbols
-     (clojure-symbol-name sym))
+     (let ((var (clojure-symbol->symbol sym)))
+       (if (clojure-symbol-namespace sym)
+           (let ((mod (clojure-symbol-namespace->module-name sym)))
+             `(scheme/begin
+               (scheme/require-library ,mod)
+               (scheme/import (prefix ,mod ,(string->symbol (sprintf "~A/" mod))))
+               ,var))
+           var)))
     ;; (('use (? clojure-symbol? namespaces ...))
     ;;  (for-each namespace-import namespaces))
     (((? clojure-symbol? sym) rest ...)
      ;; TODO: handle namespaced symbols
-     (match (cons (clojure-symbol-name sym) rest)
+     (match (cons (clojure-symbol->symbol sym) rest)
        (('def def ...) (compile-def def))
        (('ns (? clojure-symbol? name))
         (let ((ns (clojure-symbol->namespace-name name)))
           (current-namespace
            (clojure-namespace-ref ns))
-          `(scheme/quote ,ns)))))
+          `'(namespace ,ns)))
+       (else
+        (map compile form))))
     (else (error 'compile "unknown form" form))))
 
 (define (compile-file port)