Skip to content

Instantly share code, notes, and snippets.

@nunorc
Created August 27, 2010 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nunorc/553150 to your computer and use it in GitHub Desktop.
Save nunorc/553150 to your computer and use it in GitHub Desktop.
$ 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