How to write perl6 macro to enter text?

I am looking to create a macro in P6 that converts its argument to a string. Here is my macro:

macro tfilter($expr) {
        quasi {
                my $str = Q ({{{$expr}}});
                filter-sub $str;   
        };
}

And here is what I call it:

my @some = tfilter(age < 50);

However, when I run the program, I get an error:

Unable to parse expression in quote words; couldn't find final '>'

How to fix it?

+6
source share
1 answer

Your use case for converting some code to a string using a macro is very reasonable. There is no API installed for this (even in my head), although I came across and thought about the same use case. It would be nice in such cases as:

assert a ** 2 + b ** 2 == c ** 2;

assert , , . . ( , .)

(Edit: 007 - Perl 6.)

, 007, Q (AST), AST, , :

$ bin/007 -e='say(~quasi { 2 + 2 })'
Q::Infix::Addition {
    identifier: Q::Identifier "infix:+",
    lhs: Q::Literal::Int 2,
    rhs: Q::Literal::Int 2
}

, . , , . ( . " Qtrees" .)

, , Q, .source - . :

$ bin/007 -e='say((quasi { 2 + 2 }).source)'
 2 + 2 

(: .)

, .source Qtrees. ? <black box source>? ?

, :

my $str = Q ({{{$expr}}});

, ( AST ). , - . , - - à la C. , , {{{$expr}}} ( ), , AST. AST node , .

, !

(PS: , , , filter-sub . ? ? AST, .)

(PPS: Moritz ++ #perl6 , age < 50 ​​ . Perl 6 , , . Perl 6 , age - , , <. , > , ( . # 159 .))

+7

Source: https://habr.com/ru/post/1016700/


All Articles