Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
allow ASTs to be stringified
Still not sure .Str is the best way to expose this functionality, but it's the
current only suggestion, and there's nothing obviously wrong with it.
  • Loading branch information
Carl Masak committed Jul 18, 2014
1 parent 0aaba7e commit 9b8e9c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Perl6/Actions.nqp
Expand Up @@ -4258,7 +4258,7 @@ class Perl6::Actions is HLL::Actions does STDActions {
if $<args><semiarglist> {
for $<args><semiarglist><arglist> {
if $_<EXPR> {
add_macro_arguments($_<EXPR>.ast, @argument_asts);
add_macro_arguments($_<EXPR>.ast, @argument_asts, ~$<args>);
}
}
}
Expand All @@ -4273,13 +4273,14 @@ class Perl6::Actions is HLL::Actions does STDActions {
}
}

sub add_macro_arguments($expr, @argument_asts) {
sub add_macro_arguments($expr, @argument_asts, $code_string) {
my $ast_class := $*W.find_symbol(['AST']);

sub wrap_and_add_expr($expr) {
my $quasi_ast := $ast_class.new();
my $wrapped := QAST::Op.new( :op('call'), make_thunk_ref($expr, $expr.node) );
nqp::bindattr($quasi_ast, $ast_class, '$!past', $wrapped);
nqp::bindattr($quasi_ast, $ast_class, '$!Str', $code_string);
@argument_asts.push($quasi_ast);
}

Expand Down Expand Up @@ -4337,13 +4338,13 @@ class Perl6::Actions is HLL::Actions does STDActions {
if $<args><semiarglist> {
for $<args><semiarglist><arglist> {
if $_<EXPR> {
add_macro_arguments($_<EXPR>.ast, @argument_asts);
add_macro_arguments($_<EXPR>.ast, @argument_asts, ~$<args>);
}
}
}
elsif $<args><arglist> {
if $<args><arglist><EXPR> {
add_macro_arguments($<args><arglist><EXPR>.ast, @argument_asts);
add_macro_arguments($<args><arglist><EXPR>.ast, @argument_asts, ~$<args>);
}
}
return @argument_asts;
Expand Down Expand Up @@ -4827,7 +4828,7 @@ class Perl6::Actions is HLL::Actions does STDActions {
make expand_macro($macro, $name, $/, sub () {
my @argument_asts := [];
for @($/) {
add_macro_arguments($_.ast, @argument_asts);
add_macro_arguments($_.ast, @argument_asts, '');
}
return @argument_asts;
});
Expand Down Expand Up @@ -5950,6 +5951,7 @@ class Perl6::Actions is HLL::Actions does STDActions {
my $quasi_ast := $ast_class.new();
my $past := $<block>.ast<past_block>.pop;
nqp::bindattr($quasi_ast, $ast_class, '$!past', $past);
nqp::bindattr($quasi_ast, $ast_class, '$!Str', $/.Str());
$*W.add_object($quasi_ast);
my $throwaway_block := QAST::Block.new();
my $quasi_context := block_closure(
Expand Down
5 changes: 5 additions & 0 deletions src/core/AST.pm
Expand Up @@ -3,6 +3,7 @@
my class AST {
has $!past;
has $!quasi_context;
has $!Str;

submethod BUILD(:$past) {
$!past := $past;
Expand Down Expand Up @@ -32,6 +33,10 @@ my class AST {
method is_quasi_ast {
so $!quasi_context;
}

method Str {
$!Str;
}
}

# vim: ft=perl6 expandtab sw=4

0 comments on commit 9b8e9c5

Please sign in to comment.