Thursday, April 27, 2006

April 26th, 2006 -- The Second Beginning

I mainly focused on parser hacking in the development one year ago, so that this time when I asked Audrey, she remember that I've added the parsing function for :key(val) (an alternative pair constructing syntax) and "?? !!" ("?? ::" when I proposed it, in fact I didn't really added it. I wrote an analysis and she added it). Fortunately, she was refactoring Parser.hs preparing to replace it by a perl 6 rule file generated one.

So that's my task. I'm now writing a translator in perl 5 which accepts a perl 6 rule file (with some restrictions on the production rules). Here's an example. (source code of pugs can be obtained from http://svn.openfoundry.org/pugs/)
{- From src/Pugs/Parser/Literal.hs line 24 -}
yadaLiteral :: RuleParser Exp
yadaLiteral = expRule $ do
sym <- choice . map symbol $ words " ... ??? !!! "
return $ App (Var $ doYada sym) Nothing [Val $ VStr (sym ++ " - not yet implemented")]
where
doYada "..." = "&fail_" -- XXX rename to fail() eventually
doYada "???" = "&warn"
doYada "!!!" = "&die"
doYada _ = error "Bad yada symbol"
Can be written as (perl 6 rule format)
rule yadaLiteral {
$<sym> := [ <'...'> | <'???'> | <'!!!'> ]
{ return App(
Var( doYada( $<sym> ) ),
Nothing,
[ Val( VStr( $<sym> ~ ' - not yet implemented') ) ]
)
}
}
Now, there is already a perl 6 rule parser module written in perl 5: Pugs::Compiler::Rule. Currently it emits recursive-descent perl 5 parsing code. I'm going to add a Haskell backend for it.
< autreyt> so, your task, should you choose to accept it, is to use whatever the most expedient way to produce a parser for such a language, and emit Haskell code for it
< scw> in perl 5, but using three languages at once :D
< audreyt> :D
< scw> so what I could do is adding a backend for Pugs::Compiler::Rule
< scw> which targets on Haskell
How is the progress? Have you seen that it's April 27th now in +0800 time zone? What does that mean? Oh, nothing, just a joke. The idea of creating a new blog for this didn't come up until this morning. I finally understood how to use Pugs::Compiler::Rule last night, which was the goal I set for my self yesterday, then went back to the VFX ( Digital Visual Effects) project again. So that's all. I can now write a small peace of perl 5 code
use Pugs::Compiler::Rule;
my $rule = Pugs::Compiler::Rule->compile("moose { return 23 }");
print $rule->{perl5}
Which converts
rule foo{ moose { return 23 } }
into
do {
package Pugs::Runtime::Rule;
my $matcher =
concat(
constant( q!m! )
, concat(
constant( q!o! )
, concat(
constant( q!o! )
, concat(
constant( q!s! )
, concat(
constant( q!e! )
, abort(
sub {
return { bool => 1, tail => $_[0], return => sub { return 23 } };
}
)
)
)
)
)
)
;
sub {
my $grammar = shift;
my $tree;
rule_wrapper( $_[0],
$matcher->( $_[0], $_[1], $tree, $tree, $grammar, 0, $_[0], $_[2] )
);
}
}
Scary, isn't it? However, my goal is
ruleFoo = do{ symbol "moose"; return $ VInt 23 }
Much clearer now! Hope I can, and see you next time.

1 Comments:

Anonymous Anonymous said...

great to see more pugs blogging. audrey has set the bar very high. i look forward to more!

4/29/2006 1:14 PM  

Post a Comment

Links to this post:

Create a Link

<< Home