New syntax is 'qs', aka quote sub, which is similar to q, except that it
interpolates all of: ${..} @{..} and %{..}
All subroutines which are interpolated, are interpolated as regular text,
with no bindings, so that they get lexically scoped in the code they are
returned as part of.
Then macros essentially return a string which gets interpolated at the
call site.

macro while ($cond, $body) {
  return qs{
    GENLABEL:
    goto ENDLABEL if( ${ $cond } );
    ${ $body };
    goto GENLABEL;
    ENDLABEL:
  }
}

$a = 10;
while {$a>0}
{
  $a--;
  print $a;
}

Or maybe, if 'macro while' is defined as:
macro while ($cond as paren-sub, $body) {
....

while( $a> 0 ) {
....
}

macro for qw( '(', $var, $low, $high, ')', $body ) {
  return qs{
    my ${ $var } = $low;
    my $end = ${ high };
    GENLABEL:
    goto ENDLABEL if (${ $var } > $end);
    ${ $body };
    ${ $var }++;
    goto GENLABEL;
    ENDLABEL;
  }
}

for ($i 1 10) {

}

or alternately, if 'macro for' is defined as:
macro for ( $var, '=', $low, 'to', $high, $body ) {
....

for $i = 1 to 10 {
....
}

The syntax isn't as powerful as scheme's despite the above extensions to
the parameter list, because scheme has such a 'simple' syntax, so there is
no real confusion about argument parameters. Another possible parameter
syntax is something that's along the lines of a grammar, so it can have a
better idea about where to start, stop, and handle errrors, for parsing
calls to macros.

The ugliness of the symbols is necessary for the more lowlevel macros that
would compile down to very simple ops, and thus make translation to the
parrot bytecode that much easier. One additional thing the above need is
that either GENLABEL and ENDLABEL need to be renamed by qs() so that
while's within while's don't have problems, or we'd need to follow
Scheme's example of gensym, which constructs unique symbols.

In theory, it might be possible to define the language in terms of macros.
Then you just write a macro parser, and you've got your (probably slow as
hell to compile) language. ;)

Anyways, I'm trying to move beyond the stage of getting others excited
about macros, and trying to propose syntax, so they aren't just wishful
thinking. I understand the above syntax probably does have ambiguities
which aren't easily resolved, but I'm trying to get the ball rolling. I'm
just hoping Larry has some wonderful macro syntax up his sleeve, waiting
to wow us all with. :)

Mike Lambert

Reply via email to