Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
divided monolithic `syntactic-closure' namespace into separate namesp…
…aces
- Loading branch information
Showing
6 changed files
with
205 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
(in-ns 'syntactic-closure) | ||
|
||
(use '[syntactic-closure.util :only [error var->qualified-symbol add-meta macro?]] | ||
'[syntactic-closure.environment :only [lookup]]) | ||
|
||
(declare compile compile-exprs syntactic-closure? compile-special special?) | ||
|
||
(defn- compile-symbol [env sym] | ||
(if (namespace sym) | ||
sym | ||
(let [var (lookup env sym)] | ||
(cond (nil? var) (error "unknown identifier: " sym) | ||
(var? var) (var->qualified-symbol var) | ||
:else var)))) | ||
|
||
(defn- compile-seq [env exp] | ||
(let [op (first exp), m (lookup env op)] | ||
(cond (and (var? m) (macro? m)) | ||
(compile env (apply m (add-meta exp {::env env}) env (rest exp))) | ||
|
||
(special? op) | ||
(compile-special env exp) | ||
|
||
:else (compile-exprs env exp)))) | ||
|
||
(defn- compile-syntactic-closure [env sc] | ||
(sc env)) | ||
|
||
(defn compile [env exp] | ||
(cond (syntactic-closure? exp) (compile-syntactic-closure env exp) | ||
(symbol? exp) (compile-symbol env exp) | ||
(and (list? exp) (empty? exp)) '() | ||
(seq? exp) (compile-seq env exp) | ||
(vector? exp) (vec (compile-exprs env exp)) | ||
(map? exp) (reduce (fn [map [key val]] | ||
(assoc map (compile env key) (compile env val))) | ||
{} | ||
exp) | ||
(set? exp) (compile-exprs env exp) | ||
:else exp)) | ||
|
||
(defn- compile-exprs [env exprs] | ||
(map #(compile env %) exprs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(ns syntactic-closure.environment) | ||
|
||
;; | ||
;; syntactic environment | ||
;; | ||
(defn- toplevel-env [] | ||
(ns-map *ns*)) | ||
|
||
(defn lookup [env name] | ||
(or (env name) | ||
((toplevel-env) name))) | ||
|
||
(defn extend-environment [env ids] | ||
(reduce (fn [env id] (assoc env id (gensym id))) env ids)) | ||
|
||
(defn filter-environment [names names-env else-env] | ||
(reduce (fn [env [name val]] | ||
(if (names name) | ||
(assoc env name val) | ||
env)) | ||
else-env | ||
names-env)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
(in-ns 'syntactic-closure) | ||
|
||
;; | ||
;; self-made quasiquote, because Clojure's syntax-quote meddles too much | ||
;; | ||
(def ^:private UNQUOTE 'clojure.core/unquote) | ||
(def ^:private UNQUOTE-SPLICING 'clojure.core/unquote-splicing) | ||
|
||
(defn- unquote? [x] | ||
(and (seq? x) (= (first x) UNQUOTE))) | ||
|
||
(defn- unquote-splicing? [x] | ||
(and (seq? x) (= (first x) UNQUOTE-SPLICING))) | ||
|
||
(defn expand [xxs depth] | ||
(cond (and (unquote? xxs) (= depth 0)) (second xxs) | ||
|
||
(seq? xxs) | ||
(let [[x & [x' :as xs]] xxs] | ||
(cond (and (unquote-splicing? x) (= depth 0)) | ||
`(concat ~(second x) ~(expand xs depth)) | ||
|
||
(not (nil? x)) | ||
(cond (= x 'qq) | ||
`(list '~'qq ~(expand x' (inc depth))) | ||
|
||
(and (= x UNQUOTE) (> depth 0)) | ||
`(list '~UNQUOTE ~(expand x' (dec depth))) | ||
|
||
(and (= x UNQUOTE-SPLICING) (> depth 0)) | ||
`(list '~UNQUOTE-SPLICING ~(expand x' (dec depth))) | ||
|
||
:else `(cons ~(expand x depth) ~(expand xs depth))))) | ||
|
||
(vector? xxs) | ||
`(vec ~(expand (seq xxs) depth)) | ||
|
||
(map? xxs) | ||
`(apply conj {} (map vec (partition 2 ~(expand (apply concat xxs) depth)))) | ||
|
||
:else `'~xxs)) | ||
|
||
(defmacro qq [x] | ||
(expand x 0)) |
Oops, something went wrong.