Created
July 9, 2012 22:59
-
-
Save jnthn/3079610 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use Perl6::Grammar; | |
use Perl6::Actions; | |
use Perl6::Compiler; | |
class Perl6ToJSON is Perl6::Compiler { | |
method json_dump($parse, *%adverbs) { | |
say(to_json($parse)); | |
nqp::exit(0); | |
} | |
sub to_json($parse) { | |
my @list; | |
my @hash; | |
for $parse.list -> $m { | |
if nqp::islist($m) { | |
@list.push(array_of($m)); | |
} | |
else { | |
@list.push(to_json($m)); | |
} | |
} | |
for $parse.hash -> $m { | |
if nqp::islist($m.value) { | |
@hash.push('"' ~ $m.key ~ '": ' ~ array_of($m.value)); | |
} | |
else { | |
@hash.push('"' ~ $m.key ~ '": ' ~ to_json($m.value)); | |
} | |
} | |
'{ "text": "' ~ $parse ~ '", "list": [' ~ | |
nqp::join(', ', @list) ~ '], "hash": {' ~ | |
nqp::join(', ', @hash) ~ '} }'; | |
} | |
sub array_of(@stuff) { | |
my @elems; | |
for @stuff -> $m { | |
@elems.push(to_json($m)); | |
} | |
'[ ' ~ nqp::join(', ', @elems) ~ ' ]' | |
} | |
} | |
sub MAIN(@ARGS) { | |
# Initialize dynops. | |
pir::rakudo_dynop_setup__v(); | |
# Bump up Parrot's recursion limit | |
pir::getinterp__P().recursion_limit(100000); | |
# Create and configure compiler object. | |
my $comp := Perl6ToJSON.new(); | |
$comp.language('perl6'); | |
$comp.parsegrammar(Perl6::Grammar); | |
$comp.parseactions(Perl6::Actions); | |
$comp.addstage('json_dump', :before<past>); | |
hll-config($comp.config); | |
my $COMPILER_CONFIG := $comp.config; | |
# Add extra command line options. | |
my @clo := $comp.commandline_options(); | |
@clo.push('parsetrace'); | |
@clo.push('setting=s'); | |
@clo.push('n'); | |
@clo.push('p'); | |
@clo.push('doc=s?'); | |
@clo.push('optimize=s?'); | |
@clo.push('c'); | |
@clo.push('I=s'); | |
@clo.push('M=s'); | |
# Set up module loading trace | |
my @*MODULES := []; | |
# Set up END block list, which we'll run at exit. | |
my @*END_PHASERS := []; | |
# Enter the compiler. | |
$comp.command_line(@ARGS, :encoding('utf8'), :transcode('ascii iso-8859-1')); | |
# Run any END blocks before exiting. | |
for @*END_PHASERS { $_() } | |
} |
This file contains hidden or 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
Take a Rakudo repo. Place the above file in src/json.nqp. Build it like this: | |
nqp --vmlibs=perl6_group,perl6_ops --target=pir --combine src/json.nqp src/gen/main-version.nqp > src/json.pir | |
Then you can do: | |
install\bin\parrot.exe src\json.pir -e "say 42" | |
Which will dump the parse tree as JSON. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment