I mentioned this idea before, and I've managed to implement a bit of it.
This is a compiler implementing a (very small) subset of Perl 6; it puts
the assembler code on STDOUT. Control flow and loops aren't implemented
yet. It uses Parse::RecDescent for parsing, and has a lot of 5.6-isms
in it. Here's a sample of input and output:
INPUT:
our string($hello);
our string($hi);
our string($hola);
our string($shalom);
$hello="H"~"ello, ";
$hi="Hi, ";
$hola="Hola, ";
$shalom="Shalom, ";
print($hello);
print($hi);
print($hola);
print("and ");
print($shalom);
print("world!");
our int($one);
$one=1;
our int($two);
$two=2;
our int($three);
$three=$one+$two;
print($three);
our int($six);
$six=$one+$two+$three;
print($six);
OUTPUT:
#File: test.bpl
#Variables:
# $hello(S) => S5
# $hi(S) => S6
# $hola(S) => S7
# $shalom(S) => S8
# $one(I) => I5
# $two(I) => I6
# $three(I) => I7
# $six(I) => I8
concat S0, "H", "ello, "
set S5, S0
set S6, "Hi, "
set S7, "Hola, "
set S8, "Shalom, "
print S5
print S6
print S7
print "and "
print S8
print "world!"
set I5, 1
set I6, 2
add I0, I5, I6
set I7, I0
print I7
add I0, I6, I7
add I0, I5, I0
set I8, I0
print I8
end
That isn't quite valid Parrot bytecode, because of the concat. Concat
is currently documented to take only two arguments. This will make the
compiler's life more difficult, as it requires another special case. I
recommend that we add a three-argument form of concat for consistency
with ops like add. babyperl.pl is attached; this is _really_ messy
code, and will probably be mostly rewritten before being added to the
little_languages directory of the Parrot distribution. Note that the
following things are still missing:
-Comments
-Interpolation
-Operator precedence
-Comparison operators
-And and Or
-Anything not already implemented in Parrot
print() currently only takes one parameter, but you can use ~ to
concatenate things together. It spits out a crapload of debugging
information--symbol table dumps and stack dumps--but it's all on STDERR
so you can redirect that to your null device to get rid of it.
Share and enjoy!
--Brent Dax
[EMAIL PROTECTED]
Configure pumpking for Perl 6
They *will* pay for what they've done.
babyperl.pl