diff options
| author | Joel Martin <github@martintribe.org> | 2014-03-24 16:32:24 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-03-24 16:32:24 -0500 |
| commit | 3169070063b2cb877200117ebb384269d73bcb93 (patch) | |
| tree | 23de3db1ea5c37afd21a45b6ed7771f56a08c0c4 /clojure/src/reader.clj | |
| download | mal-3169070063b2cb877200117ebb384269d73bcb93.tar.gz mal-3169070063b2cb877200117ebb384269d73bcb93.zip | |
Current state of mal for Clojure West lighting talk.
Diffstat (limited to 'clojure/src/reader.clj')
| -rw-r--r-- | clojure/src/reader.clj | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/clojure/src/reader.clj b/clojure/src/reader.clj new file mode 100644 index 0000000..8f14767 --- /dev/null +++ b/clojure/src/reader.clj @@ -0,0 +1,32 @@ +(ns reader + (:refer-clojure :exclude [read-string]) + (:require [clojure.tools.reader :as r] + [clojure.tools.reader.reader-types :as rt])) + +;; change tools.reader syntax-quote to quasiquote +(defn- wrap [sym] + (fn [rdr _] (list sym (#'r/read rdr true nil true)))) + +(defn- wrap-with [sym] + (fn [rdr arg _] (list sym (#'r/read rdr true nil true) arg))) + +;; Override some tools.reader reader macros so that we can do our own +;; metadata and quasiquote handling +(alter-var-root #'r/macros + (fn [f] + (fn [ch] + (case ch + \` (wrap 'quasiquote) + \~ (fn [rdr comma] + (if-let [ch (rt/peek-char rdr)] + (if (identical? \@ ch) + ((wrap 'splice-unquote) (doto rdr rt/read-char) \@) + ((wrap 'unquote) rdr \~)))) + \^ (fn [rdr comma] + (let [m (#'r/read rdr)] + ((wrap-with 'with-meta) rdr m \^))) + \@ (wrap 'deref) + (f ch))))) + +(defn read-string [s] + (r/read-string s)) |
