Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expose syntax-quote macro
  • Loading branch information
NoahTheDuke committed Nov 30, 2023
commit 6f0c9447bc340d90a847ca4a567322e476b4e9bd
21 changes: 21 additions & 0 deletions src/clj/clojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,27 @@
(cat (concat x y) zs))))

;;;;;;;;;;;;;;;;at this point all the support for syntax-quote exists;;;;;;;;;;;;;;;;;;;;;;
(defmacro syntax-quote
"Return the given form as if wrapped in quote ('), with handling for
unqoute (~) and unquote-splicing (~@).

Recursively walks the form: For all forms other than symbols, lists, vectors,
sets and maps, `x is the same as 'x. For symbols, syntax-quote resolves the
symbol in the current context, yielding a fully-qualified symbol.
Non-qualified symbols ending in # are resolved to a generated symbol and all
references to that symbol within the form will use that generated symbol.

For lists, vectors, sets, and maps, syntax-quote establishes a template of the
corresponding data structure. Within the template, unqualified forms behave as
if recursively syntax-quoted, but forms can be exempted from such recursive
quoting by qualifying them with unquote or unquote-splicing, in which case
they will be treated as expressions and be replaced in the template by their
value, or sequence of values, respectively."
{:added "1.13"}
([] ())
([form]
(. clojure.lang.RT syntaxQuote form)))

(defmacro delay
"Takes a body of expressions and yields a Delay object that will
invoke the body only the first time it is forced (with force or deref/@), and
Expand Down
7 changes: 5 additions & 2 deletions src/jvm/clojure/lang/LispReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -988,12 +988,15 @@ else if(!(meta instanceof IPersistentMap))
public static class SyntaxQuoteReader extends AFn{
public Object invoke(Object reader, Object backquote, Object opts, Object pendingForms) {
PushbackReader r = (PushbackReader) reader;
Object form = read(r, true, null, true, opts, ensurePending(pendingForms));
return parseSyntaxQuote(form);
}

public static Object parseSyntaxQuote(Object form) {
try
{
Var.pushThreadBindings(
RT.map(GENSYM_ENV, PersistentHashMap.EMPTY));

Object form = read(r, true, null, true, opts, ensurePending(pendingForms));
return syntaxQuote(form);
}
finally
Expand Down
3 changes: 3 additions & 0 deletions src/jvm/clojure/lang/RT.java
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,9 @@ public static void loadLibrary(String libname){
System.loadLibrary(libname);
}

public static Object syntaxQuote(Object form){
return LispReader.SyntaxQuoteReader.parseSyntaxQuote(form);
}

////////////// Collections support /////////////////////////////////

Expand Down