Permalink
Browse files

unify-gensyms

  • Loading branch information...
1 parent 163c396 commit 0052b99e14f20737f5cb9f1a5646457089d09368 @ztellman committed Feb 1, 2012
Showing with 32 additions and 1 deletion.
  1. +4 −1 src/potemkin.clj
  2. +28 −0 src/potemkin/macros.clj
View
5 src/potemkin.clj
@@ -9,13 +9,16 @@
(ns potemkin
(:require
[potemkin.namespace :as namespace]
- [potemkin.map :as map]))
+ [potemkin.map :as map]
+ [potemkin.macro :as macro]))
(namespace/import-macro namespace/import-macro) ;; totally meta
(import-macro namespace/import-fn)
(import-macro map/def-custom-map)
+(import-fn macro/unify-gensyms)
+
View
28 src/potemkin/macros.clj
@@ -0,0 +1,28 @@
+;; Copyright (c) Zachary Tellman. All rights reserved.
+;; The use and distribution terms for this software are covered by the
+;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+;; which can be found in the file epl-v10.html at the root of this distribution.
+;; By using this software in any fashion, you are agreeing to be bound by
+;; the terms of this license.
+;; You must not remove this notice, or any other, from this software.
+
+(ns potemkin.macros
+ (:use [clojure walk]))
+
+(def gensym-regex #"([a-zA-Z\-]+)__\d+__auto__$")
+
+(defn gensym? [s]
+ (and
+ (symbol? s)
+ (re-find gensym-regex (str s))))
+
+(defn un-gensym [s]
+ (second (re-find gensym-regex (str s))))
+
+(defn unify-gensyms [body]
+ (let [gensym* (memoize gensym)]
+ (postwalk
+ #(if (gensym? %)
+ (symbol (str (gensym* (str (un-gensym %) "__")) "__auto__"))
+ %)
+ body)))

0 comments on commit 0052b99

Please sign in to comment.