Created
August 14, 2019 19:26
-
-
Save robertlemmen/52dd17e2a6fd2251a206ef765072fb69 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
use nqp; | |
my %type-map = ('QAST::IVal' => 'I', 'QAST::Var' => 'V', 'QAST::Op' => 'O', 'QAST::Stmts' => 'S'); | |
my $prog = 'my $aa = 4; say "([$aa]" ~ (3 + $aa);'; | |
my @hl = ' ' xx $prog.chars; | |
sub traverse($indent, Mu $item) { | |
if $item.^name eq "QAST::SVal" { | |
say (" " x $indent) ~ $item.^name ~ " '" ~ $item.value ~ "' "; | |
} | |
elsif nqp::can($item, "dump_annotations") { | |
say (" " x $indent) ~ $item.^name ~ " " ~ $item.dump_annotations; | |
} | |
else { | |
say (" " x $indent) ~ $item.^name; | |
} | |
if nqp::can($item, "node") { | |
if $item.node.^name eq "Perl6::Grammar" { | |
say (" " x $indent) ~ "# " ~ $item.node.from ~ "-" ~ $item.node.to; | |
for $item.node.from..$item.node.pos-1 -> $i { | |
@hl[$i] = %type-map{$item.^name} // '?'; | |
} | |
} | |
} | |
if $item.^name eq "Str" { | |
say (" " x $indent) ~ "'" ~ $item ~ "'"; | |
return; | |
} | |
if nqp::can($item, "list") { | |
for @($item.list) -> $c { | |
traverse($indent + 1, $c); | |
} | |
} | |
} | |
my $item = nqp::getcomp('perl6').compile($prog, compunit_ok => 1, target => 'ast'); | |
traverse(0, $item); | |
say $prog; | |
say @hl.join; | |
say ""; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment