[SUBDIRMK RFC 6/7] README: v2: Draft - macro assistance facility

Ian Jackson ijackson at chiark.greenend.org.uk
Thu Dec 12 16:32:50 GMT 2019


Ian Jackson writes ("[SUBDIRMK RFC 6/7] README: v2: Draft - macro assistance facility"):
> Not implemented yet.  Also no S-o-b at this time!
> 
> Very much RFC.
> 
> In particular I wonder if the author of a &:macro will ever want a way
> to write (say) "$(dirname $3)".  If all $ not in front of digits are
> doubled in &:macro, this is impossible.  Maybe we need a literal $.
> "&$@" ?  "&\$" ?  "&$$" ?  Should we provide "&\&" rather than &@ ?
> 
> Maybe "&$(" should be special ?  "&$(dirname $3)" seems quite nice
> compared to "&\$(dirname $3)" or something.  Even if we do that
> logically we still need provide something that expands to just the $.
> 
> Do we need something to turn on and off $-doubling ?  &$ SPC or &$$ or
> something maybe.  The &:macro commit proposes two kinds of $-doubling
> which means we would need multiple ways to turn it on and off.  (I'm a
> bit uncomfortable with burying this $-doubling feature in a cooked
> thing like &:macro.)
> 
> Maybe macro arguments are &1..&9 which expand to $1..$9, which avoids
> the need for two subtly different $-doubling modes.

I was thinking about this (instead of the world being on fire) and:

* Providing a way to turn on and off $-doubling is a good idea.
* There should only be one $-doubling mode.
* This means there must be a syntax for $N in $-doubling which
  is relatively convenient.

Proposal:

  &$VARIABLE    where VARIABLE starts with a letter and matches \w+
                when not $-doubling $(sub_dir_VARIABLE)
                when $-doubling    $$(sub_dir_VARIABLE)
                ie, result is as if you wrote $(&VARIABLE)

  &$+           turn $-doubling on
  &$-           turn $-doubling off

  &$NN          where NN is \d+: when $-doubling "$(NN)", error otherwise
  &$(           when $-doubling "$(", error otherwise

No general syntax for un-doubled $ in $-doubling.  If this is wanted,
instead use &$- to turn $-doubling off temporarily.  This scheme
avoids making the $-doubling regime (which is a new syntax) any more
complex than seems strictly necessary.

The general theory is
  &$something   =>  $something with undoubled $, allowed while $-doubling
so &$ rather than & means "do this expansion at macro expansion time"
but with one exception
  &$VARIABLE    $ will be doubled
because that's just too convenient a syntax to waste on a relatively
obscure macro feature.  To avoid user errors we limit "&$ for
undoubled $" to the two particular syntaxes, and contexts, we expect.

I choose &$+ and &$- rather than (say) &${ &$} because one use case
for this is inside &:macro, where it would be odd to say

  &:macro wombat
  &$} $(foreach x,$3, something complicated

whereas

  &:macro wombat
  &$- $(foreach x,$3, something complicated

seems to make more sense.  But note that normally you could do

  &:macro wombat
  &$(foreach x,&$3, something involving various &$(...)


An alternative would be to say that &$ is always something special and
to have a different syntax for undoubled $.  For example:

  &NN           => $(NN)                error if not doubling
  &(            => $(                   error if not doubling
  &[macroname   => $(eval $(call %macroname etc.

But I think variable references with no $ are just too weird.


Note that this $-doubling regime might be useful for commands.
You would have to write (say) "&$(@)" rather than $@.  But you could
write
        perl -pe $+'perl code involving $ signs'$- $< >$@
or whatever.

Ian.



More information about the sgo-software-discuss mailing list