From c1306d943aa9c19d6bbccc1a5c72cddb5af7e564 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 8 Apr 2025 21:48:33 -0700 Subject: expand-hook: process DWIM brackets forms. It turns out we need this for infix. * eval.c (do_expand): Call the expand hook for forms headed by the dwim operator, passing that operator symbol as the type indicator. * txr.1: Document this and also that the expand hook doesn't receive symbol macros. --- eval.c | 20 +++++++++++++++----- txr.1 | 19 ++++++++++++------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/eval.c b/eval.c index 9c35d6c5..24e4dd92 100644 --- a/eval.c +++ b/eval.c @@ -5362,12 +5362,22 @@ again: } else if (sym == symacrolet_s) { return expand_symacrolet(form, menv); } else if (sym == dwim_s) { - val args = rest(form); - val args_ex = expand_forms_lisp1(dot_to_apply(args, t), menv); + val eh = expand_hook; + if (eh) { + val eform = funcall3(eh, form, menv, dwim_s); + if (eform != form) { + form = rlcp_tree(eform, form); + goto again; + } + } + { + val args = rest(form); + val args_ex = expand_forms_lisp1(dot_to_apply(args, t), menv); - if (args == args_ex) - return form; - return rlcp(cons(sym, args_ex), form); + if (args == args_ex) + return form; + return rlcp(cons(sym, args_ex), form); + } } else if (sym == switch_s) { return expand_switch(form, menv); } else if (!macro && (macro = lookup_mac(menv, sym))) { diff --git a/txr.1 b/txr.1 index 388616fd..5db0bbef 100644 --- a/txr.1 +++ b/txr.1 @@ -44021,11 +44021,13 @@ a function which takes three arguments: .mono .meti (lambda >> ( form < env << type-keyword ) ...) .onom -Whenever the macro expander encounters the invocation of a symbol macro, -macro, function, or invalid form, it calls the function stored in +Whenever the macro expander encounters the invocation of a macro, +macro, function, DWIM brackets form, or an invalid form, it calls the function +stored in .code *expand-hook* if that variable isn't .codn nil . +The hook is not invoked for symbol macro forms. If the function returns an expression other than .metn form , @@ -44054,9 +44056,10 @@ and others in that category. The .meta type-keyword -argument takes on one of three values: +argument takes on one of four values: .codn :fun , -.code :macro +.codn :macro , +.code dwim or .codn nil . If the value is @@ -44070,8 +44073,10 @@ If the value is .codn :macro , then .meta form -is a macro invocation: if it is a symbol, then it is a symbol macro form, -otherwise an ordinary macro form. +is a macro invocation. If the value is +.codn dwim , +it is a bracket form: a compound form headed by the same symbol +.codn dwim . If .meta type-keyword is @@ -54018,7 +54023,7 @@ atoms, considering them to be the tokens of infix syntax. The .code ifx macro establishes an expansion hook as if by binding the variable -.codn *expander-hook* . +.codn *expand-hook* . Whenever the expansion hook transforms a form, the expression's constituent forms are then recursively visited, and subject to automatic infix expansion. -- cgit v1.2.3