Skip to content

Instantly share code, notes, and snippets.

@FROGGS

FROGGS/rakvar.pl Secret

Last active December 12, 2015 12: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 FROGGS/666966d963bb26011115 to your computer and use it in GitHub Desktop.
Save FROGGS/666966d963bb26011115 to your computer and use it in GitHub Desktop.
Quest: Where is the thinko?
class Perl6::RegexActions is QRegex::P6Regex::Actions does STDActions {
method metachar:sym<rakvar>($/) {
print("method metachar:sym<rakvar>($/)\n");
my $past := $<var>.ast;
# for @( ... )
if $past.isa(QAST::Op) && $past.op eq 'callmethod' && $past.name eq 'list' {
#if nqp::substr(~$/, 0, 2) eq '@(' {
# using compile time values (we actually dont have compile time values for /@(<a b c>)/ for some reason
if 0 {
$past := QAST::Regex.new( :rxtype<alt>, :node($/) );
my $nib := $past[0];
my @nibs := +@($nib) ?? @($nib) !! [$nib];
for @nibs {
if $_.has_compile_time_value {
$past.push(%*RX<i>
?? QAST::Regex.new( $_.compile_time_value, :rxtype<literal>, :subtype<ignorecase> )
!! QAST::Regex.new( $_.compile_time_value, :rxtype<literal> ));
}
}
}
# doing it manually just to see what will work (and it does)
elsif 0 {
print("method metachar:sym<rakvar>($/) islist\n");
my @alts;
@alts.push(QAST::Regex.new( "f", :rxtype<literal>, :node($/) ));
@alts.push(QAST::Regex.new( "o", :rxtype<literal>, :node($/) ));
@alts.push(QAST::Regex.new( '.CCLASS_WHITESPACE', :rxtype<cclass>, :subtype('d'), :negate(0), :node($/)));
@alts.push(QAST::Regex.new( "be", :rxtype<literal>, :node($/) ));
$past := QAST::Regex.new( :rxtype<altseq>, |@alts );
}
# trying to build the ast like the block before, but doing all the magic at runtime
elsif 1 {
my $alts := $past.unique('alts');
#$past := QAST::Regex.new( :rxtype<alt>, :node($/) );
$past := QAST::Stmts.new(
# create regex
QAST::Op.new(:op<bind>,
QAST::Var.new(:name($alts), :scope<local>, :decl<var>),
QAST::Regex.new( :rxtype<alt>, :node($/) ),
),
# loop
QAST::Op.new(
:op<call>, :name<&eager>,
QAST::Op.new(:op<callmethod>, :name<map>,
QAST::Op.new( :op<call>, :name<&flat>,
$<var>.ast[0]
),
# push
QAST::Stmts.new(
# create a regex
QAST::Regex.new( "f", :rxtype<literal>, :node($/) ),
# push the created regex to @alts
QAST::Op.new(
:op('callmethod'), :name('push'),
QAST::Var.new(:name<$_>),
QAST::Var.new( :scope('local'), :name($alts) ) # , :decl('var')
#$past
),
#QAST::Op.new( :op<call>, :name<&say>, QAST::Var.new(:name<$_>) )
),
),
),
# return it
QAST::Regex.new( :rxtype<altseq>,
QAST::Var.new( :name($alts), :scope('local') )
)
);
}
}
else {
$past := QAST::Node.new(
QAST::SVal.new( :value('INTERPOLATE') ),
QAST::Op.new( :op<call>, :name<&MAKE_REGEX>, $<var>.ast ) );
$past := QAST::Regex.new( $past,
:rxtype<subrule>, :subtype<method>, :node($/));
}
make $past;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment