From fbe8228a05d169c539cd36218b466e5d298923ba Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 18 Jan 2022 07:33:07 -0800 Subject: quasiquote: support @,expr hack. For better or worse, TXR Lisp has a dichotomy of representation that @ produces sys:var syntax, whereas @ 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. --- tests/010/qquote.tl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/010/qquote.tl (limited to 'tests') diff --git a/tests/010/qquote.tl b/tests/010/qquote.tl new file mode 100644 index 00000000..e6daad3a --- /dev/null +++ b/tests/010/qquote.tl @@ -0,0 +1,30 @@ +(let ((nullsym nil) + (sym 's) + (atom "abc") + (cons '(x y z)) + (dwim '[])) + (tree-bind (x y (op arg)) ^(a b @,nullsym) + (assert (eq op 'sys:var)) + (assert (eq arg nullsym))) + (tree-bind (x y (op arg)) ^(a b @,sym) + (assert (eq op 'sys:var)) + (assert (eq arg sym))) + (tree-bind (x y (op arg)) ^(a b @,atom) + (assert (eq op 'sys:var)) + (assert (eq arg atom))) + (tree-bind (x y (op arg)) ^(a b @,cons) + (assert (eq op 'sys:expr)) + (assert (eq arg cons))) + (tree-bind (x y (op arg)) ^(a b @,dwim) + (assert (eq op 'sys:expr)) + (assert (eq arg dwim))) + (tree-bind (x y (op arg . tail)) ^(a b (sys:expr ,sym . foo)) + (assert (eq op 'sys:expr)) + (assert (eq arg sym)) + (assert (eq tail 'foo))) + (tree-bind (x y (op arg0 arg1)) ^(a b (sys:expr ,sym foo)) + (assert (eq op 'sys:expr)) + (assert (eq arg0 sym)) + (assert (eq arg1 'foo))) + (tree-bind (x y (op)) ^(a b (sys:expr)) + (assert (eq op 'sys:expr)))) -- cgit v1.2.3