diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-01-18 07:33:07 -0800 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-01-18 07:33:07 -0800 |
commit | fbe8228a05d169c539cd36218b466e5d298923ba (patch) | |
tree | 851ac538e6b5adab5349bbad5cdba5f3baf6f87b /eval.c | |
parent | a756991b21d35c2b72d521ed0a9ae69eac0105a9 (diff) | |
download | txr-fbe8228a05d169c539cd36218b466e5d298923ba.tar.gz txr-fbe8228a05d169c539cd36218b466e5d298923ba.tar.bz2 txr-fbe8228a05d169c539cd36218b466e5d298923ba.zip |
quasiquote: support @,expr hack.
For better or worse, TXR Lisp has a dichotomy of
representation that @<atom> produces sys:var syntax, whereas
@<compound> produces sys:expr. This can cause an issue in
backquoting. Suppose you want to use backquote to generate
sytax like (a @b) where the b comes from a variable.
The problem is that (let ((x 'b)) ^(a @,x)) doesn't do
what you might expect: it produces (sys:expr b) rather
than (sys:var b).
This patch adds a hack into the quasiquote expander which
causes it to generate code to do what you expect.
Old behavior:
1> (expand '^(a @,x))
(list 'a (list 'sys:expr x))
New behavior:
1> (expand '^(a @,x))
(list 'a (let ((#:g0012 x))
(if (atom #:g0012)
(list 'sys:var #:g0012)
(list 'sys:expr #:g0012))))
In other words, x will be evaluted, and the based on the
type of the object which emerges, either sys:var or
sys:expr syntax is generated.
* eval.c (expand_qquote_rec): Implement the above hack.
We are careful to only do this when this exact shape occurs
in the syntax: (sys:expr (sys:unquote item)).
* tests/010/qquote.tl: New file.
* txr.1: Documented.
Diffstat (limited to 'eval.c')
-rw-r--r-- | eval.c | 13 |
1 files changed, 13 insertions, 0 deletions
@@ -3661,6 +3661,7 @@ static val expand_qquote_rec(val qquoted_form, val qq, val unq, val spl) return cons(quote_s, cons(qquoted_form, nil)); } else { val sym = car(qquoted_form); + val args, uqform; if (sym == spl) { val error_msg = if3(spl == sys_splice_s, @@ -3691,6 +3692,18 @@ static val expand_qquote_rec(val qquoted_form, val qq, val unq, val spl) val opts = expand_qquote(second(qquoted_form), qq, unq, spl); val keys = expand_qquote(rest(rest(qquoted_form)), qq, unq, spl); return rlcp(list(tree_construct_s, opts, keys, nao), qquoted_form); + } else if (sym == expr_s && consp((args = cdr(qquoted_form))) + && !cdr(args) && consp((uqform = car(args))) && + car(uqform) == unq && consp(cdr(uqform)) && !cddr(uqform)) + { + val gs = gensym(nil); + val ret = list(let_s, cons(list(gs, cadr(uqform), nao), nil), + list(if_s, list(atom_s, gs, nao), + list(list_s, list(quote_s, var_s, nao), + gs, nao), + list(list_s, list(quote_s, expr_s, nao), + gs, nao), nao), nao); + return rlcp_tree(ret, qquoted_form); } else { val f = sym; val r = cdr(qquoted_form); |