Created
August 27, 2010 10:19
-
-
Save nunorc/553150 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat ABC.pm | |
grammar ABC::Grammar is HLL::Grammar { | |
rule TOP { <expr> } | |
rule expr { <integer> <addop> <integer> }; | |
token addop { '+' | '-' } | |
token integer { \d+ } | |
} | |
class ABC::Actions is HLL::Actions { | |
method TOP($/) { make $<expr>.ast; } | |
method expr($/) { | |
my $pirop := $<addop> eq '-' ?? 'sub' !! 'add'; | |
make PAST::Op.new( :pirop($pirop), +$<integer>[0], +$<integer>[1]); | |
} | |
} | |
class ABC::Compiler is HLL::Compiler { | |
ABC::Compiler.language('ABC'); | |
ABC::Compiler.parsegrammar(ABC::Grammar); | |
ABC::Compiler.parseactions(ABC::Actions); | |
} | |
my @ARGS := (pir::getinterp__p)[2]; | |
ABC::Compiler.command_line(@ARGS); | |
$ ./nqp ABC.pm --target=parse | |
> 1 + 2 | |
"parse" => PMC 'Regex;Match' => "1 + 2\n" @ 0 { | |
<expr> => PMC 'Regex;Match' => "1 + 2\n" @ 0 { | |
<integer> => ResizablePMCArray (size:2) [ | |
PMC 'Regex;Match' => "1" @ 0, | |
PMC 'Regex;Match' => "2" @ 4 | |
] | |
<addop> => PMC 'Regex;Match' => "+" @ 2 | |
} | |
} | |
$ ./nqp ABC.pm | |
> 1 + 2 | |
Method 'ast' not found for invocant of class 'Undef' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment