There was some recent discussion [1] [2] on p6l about BEGIN blocks and
constant, which is executed at compile time too.
Parrot has since quite a time the @IMMEDIATE subroutine pragma, which
causes execution of subs during compilation. But these subroutines can't
return a result so it's rather useless now besides testing that printing
something inside thus sub is really done before a print statement in main.
Here is a plan to implement most of the needed bits.
# constant pi = 4 * atan2(1,1);
translates to
.sub anon_1 @IMMEDIATE, @ANON
$N0 = atan 1.0, 1.0
$P0 = new .Float
$P0 = $N0
.return ($P0)
.end
During compilation this subroutine is executed, the resulting PMC gets
frozen and replaces the PMC constant slot of the immediate subroutine.
The constant declaration is then just
.local pmc pi
set_p_pc pi, anon_1
or with a shortcut:
.const pmc pi = anon_1
As PMC constants are already represented as frozen images in PBC this
would work for nested data strucuters, objects, and their classes too.
Comments welcome,
leo
[1] BEGIN {...} and IO
[2] my $pi is constant = 3;