summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-10-13 06:57:48 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-10-13 06:57:48 -0700
commitae644e2046349d2fdb83da88a33bedb565b99dce (patch)
tree89b837c6b5cf2e645d20b05731e85186a72050d0
parent3c4bf5fa931c0620473efe8530ed2de786292af5 (diff)
downloadtxr-ae644e2046349d2fdb83da88a33bedb565b99dce.tar.gz
txr-ae644e2046349d2fdb83da88a33bedb565b99dce.tar.bz2
txr-ae644e2046349d2fdb83da88a33bedb565b99dce.zip
New function: macroexpand-params.
* stdlib/pmac.tl (macroexpand-params): New function, implemented using newly exposed sys:expand-param-macro. * autoload.c (pmac_set_entries): Trigger pmac.tl autload on macroexpand-params symbol. * eval.c (eval_init): Register existing expand_param_macro function as sys:expand-param-macro. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
-rw-r--r--autoload.c2
-rw-r--r--eval.c1
-rw-r--r--stdlib/doc-syms.tl1
-rw-r--r--stdlib/pmac.tl7
-rw-r--r--txr.166
5 files changed, 76 insertions, 1 deletions
diff --git a/autoload.c b/autoload.c
index c8db544b..a3430a63 100644
--- a/autoload.c
+++ b/autoload.c
@@ -504,7 +504,7 @@ static val tagbody_instantiate(void)
static val pmac_set_entries(val fun)
{
val name[] = {
- lit("define-param-expander"), nil
+ lit("define-param-expander"), lit("macroexpand-params"), nil
};
autoload_set(al_fun, name, fun);
return nil;
diff --git a/eval.c b/eval.c
index acf3b2e2..8d7ea7e4 100644
--- a/eval.c
+++ b/eval.c
@@ -7107,6 +7107,7 @@ void eval_init(void)
reg_fun(intern(lit("macroexpand-lisp1"), user_package),
func_n2o(macroexpand_lisp1, 1));
reg_fun(intern(lit("expand-params"), system_package), func_n5(expand_params));
+ reg_fun(intern(lit("expand-param-macro"), system_package), func_n4(expand_param_macro));
reg_fun(intern(lit("constantp"), user_package), func_n2o(constantp, 1));
reg_fun(intern(lit("make-env"), user_package), func_n3o(make_env_intrinsic, 0));
reg_fun(intern(lit("env-fbind"), user_package), func_n3(env_fbind));
diff --git a/stdlib/doc-syms.tl b/stdlib/doc-syms.tl
index 5cc46e6b..d6c001c1 100644
--- a/stdlib/doc-syms.tl
+++ b/stdlib/doc-syms.tl
@@ -1186,6 +1186,7 @@
("macroexpand-1" "N-02ED5471")
("macroexpand-1-lisp1" "N-01E62179")
("macroexpand-lisp1" "N-01E62179")
+ ("macroexpand-params" "N-037EB49A")
("macrolet" "N-00AC12C0")
("madv-dontneed" "N-027D1E84")
("madv-normal" "N-027D1E84")
diff --git a/stdlib/pmac.tl b/stdlib/pmac.tl
index e530fddc..b78426ba 100644
--- a/stdlib/pmac.tl
+++ b/stdlib/pmac.tl
@@ -33,3 +33,10 @@
(lambda (,parms ,body ,env ,form)
,*forms))
,keyword))
+
+(defun macroexpand-params (prototype-form : env)
+ (tree-case prototype-form
+ ((name params . body)
+ (cons name (sys:expand-param-macro params body env prototype-form)))
+ (else (error "~s: invalid prototype-form argument ~s"
+ %fun% prototype-form))))
diff --git a/txr.1 b/txr.1
index 2cdbd06a..5d2f076f 100644
--- a/txr.1
+++ b/txr.1
@@ -41675,6 +41675,72 @@ Boolean indicator params:
(keyfun) -> (10 nil 20 nil)
.brev
+.coNP Function @ macroexpand-params
+.synb
+.mets (expand-params < proto-form <> [ env ])
+.syne
+.desc
+The
+.code expand-param
+function expands all of the parameter list macros expressed in the
+.I "prototype form"
+.metn proto-form ,
+returning an expanded version of the form.
+
+The
+.meta proto-form
+is a compound for which has a shape very similar to a lambda
+expression, and may be a lambda expression.
+
+The first element of
+.meta proto-form
+is a name, which is an arbitrary object, though the use of a symbol
+is strongly recommended. This object plays no role in
+.code expand-params
+other than for composing diagnostic messages if errors occur.
+
+The second element of
+.meta proto-form
+is the parameter list.
+
+The remaining elements of
+.meta proto-form
+are zero or more body forms.
+
+If
+.meta proto-form
+contains no parameter macro invocations, then it is returned.
+
+The optional
+.meta env
+parmeter specifies the macro environment which is passed to the
+parameter macro expanders, which they can receive via the
+.code :env
+parameter. The default value
+.code nil
+sepcifies the top-level environment.
+
+.TP* Examples:
+
+.verb
+;; No expansion: argument is returned
+(macroexpand-params '(foo (arg) body)) -> (foo (arg) body)
+
+;; Expand :key macro
+(macroexpand-params '(bar (:key a b c -- d (e 1234 f-p)) body))
+--> (bar (a b c . #:g0014)
+ (let (d e f-p)
+ (let ((#:g0015 (memp :d #:g0014)))
+ (when #:g0015
+ (set d (cadr #:g0015))))
+ (let ((#:g0015 (memp :e #:g0014)))
+ (cond
+ (#:g0015 (set e (cadr #:g0015))
+ (set f-p t))
+ (t (set e 1234))))
+ body))
+.brev
+
.SS* Mutation of Syntactic Places
.coNP Macro @ set
.synb