Skip to content

Commit 4248b7f

Browse files
committed
Update P5Regex to avoid BlockMemo/nqpattr.
1 parent e0d7900 commit 4248b7f

File tree

2 files changed

+77
-31
lines changed

2 files changed

+77
-31
lines changed

src/QRegex/P5Regex/Actions.nqp

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
class QRegex::P5Regex::Actions is HLL::Actions {
22
method TOP($/) {
3-
make qbuildsub($<nibbler>.ast, :anon(1), :addself(1));
3+
make QAST::CompUnit.new(
4+
:hll('P6Regex'),
5+
:sc($*W.sc()),
6+
:code_ref_blocks($*W.code_ref_blocks()),
7+
:compilation_mode(0),
8+
:pre_deserialize($*W.load_dependency_tasks()),
9+
:post_deserialize($*W.fixup_tasks()),
10+
self.qbuildsub($<nibbler>.ast, :anon(1), :addself(1))
11+
);
412
}
513

614
method nibbler($/) { make $<alternation>.ast }
@@ -219,7 +227,7 @@ class QRegex::P5Regex::Actions is HLL::Actions {
219227
:rxtype<subrule>, :subtype<zerowidth>, :negate($<neg> eq '!'), :node($/),
220228
QAST::Node.new(
221229
QAST::SVal.new( :value('after') ),
222-
qbuildsub(self.flip_ast($<nibbler>.ast), :anon(1), :addself(1))
230+
self.qbuildsub(self.flip_ast($<nibbler>.ast), :anon(1), :addself(1))
223231
));
224232
}
225233
else {
@@ -233,7 +241,7 @@ class QRegex::P5Regex::Actions is HLL::Actions {
233241
:rxtype<subrule>, :subtype<zerowidth>, :node($/),
234242
QAST::Node.new(
235243
QAST::SVal.new( :value('before') ),
236-
qbuildsub($<nibbler>.ast, :anon(1), :addself(1))
244+
self.qbuildsub($<nibbler>.ast, :anon(1), :addself(1))
237245
));
238246
}
239247
else {
@@ -247,7 +255,7 @@ class QRegex::P5Regex::Actions is HLL::Actions {
247255
:rxtype<subrule>, :subtype<zerowidth>, :negate(1), :node($/),
248256
QAST::Node.new(
249257
QAST::SVal.new( :value('before') ),
250-
qbuildsub($<nibbler>.ast, :anon(1), :addself(1))
258+
self.qbuildsub($<nibbler>.ast, :anon(1), :addself(1))
251259
));
252260
}
253261
else {
@@ -308,44 +316,31 @@ class QRegex::P5Regex::Actions is HLL::Actions {
308316
$ast;
309317
}
310318

311-
our sub qbuildsub($qast, $block = QAST::Block.new(), :$anon, :$addself) {
312-
my $blockid := $block.cuid;
313-
my $hashpast := QAST::Op.new( :op<hash> );
314-
for capnames($qast, 0) {
315-
if $_.key gt '' {
316-
$hashpast.push(QAST::SVal.new( :value($_.key) ));
317-
$hashpast.push(QAST::IVal.new( :value(
318-
nqp::iscclass(pir::const::CCLASS_NUMERIC, $_.key, 0) + ($_.value > 1) * 2) ));
319-
}
320-
}
321-
my $initpast := QAST::Stmts.new();
322-
if $addself {
323-
$initpast.push(QAST::Var.new( :name('self'), :scope('local'), :decl('param') ));
324-
}
325-
my $capblock := QAST::BlockMemo.new( :name($blockid ~ '_caps'), $hashpast );
326-
$initpast.push(QAST::Stmt.new($capblock));
319+
method qbuildsub($qast, $block = QAST::Block.new(), :$anon, :$addself, *%rest) {
320+
my $code_obj := nqp::existskey(%rest, 'code_obj')
321+
?? %rest<code_obj>
322+
!! self.create_regex_code_object($block);
327323

328-
my $nfapast := QRegex::NFA.new.addnode($qast).qast;
329-
if $nfapast {
330-
my $nfablock := QAST::BlockMemo.new( :name($blockid ~ '_nfa'), $nfapast);
331-
$initpast.push(QAST::Stmt.new($nfablock));
324+
if $addself {
325+
$block.push(QAST::Var.new( :name('self'), :scope('local'), :decl('param') ));
332326
}
333-
334327
unless $block.symbol('') {
335-
$initpast.push(QAST::Var.new(:name<>, :scope<lexical>, :decl('var')));
328+
$block.push(QAST::Var.new(:name<>, :scope<lexical>, :decl('var')));
336329
$block.symbol('', :scope<lexical>);
337330
}
338331

332+
self.store_regex_caps($code_obj, $block, capnames($qast, 0));
333+
self.store_regex_nfa($code_obj, $block, QRegex::NFA.new.addnode($qast));
334+
339335
$block<orig_qast> := $qast;
340-
341336
$qast := QAST::Regex.new( :rxtype<concat>,
342337
QAST::Regex.new( :rxtype<scan> ),
343338
$qast,
344339
($anon ??
345340
QAST::Regex.new( :rxtype<pass> ) !!
346341
QAST::Regex.new( :rxtype<pass>, :name(%*RX<name>) )));
347-
$block.push($initpast);
348342
$block.push($qast);
343+
349344
$block;
350345
}
351346

@@ -414,7 +409,22 @@ class QRegex::P5Regex::Actions is HLL::Actions {
414409
}
415410
$qast
416411
}
417-
412+
413+
# This is overridden by a compiler that wants to create code objects
414+
# for regexes. We just use the standard NQP one in standalone mode.
415+
method create_regex_code_object($block) {
416+
$*W.create_code($block, $block.name);
417+
}
418+
419+
# Stores the captures info for a regex.
420+
method store_regex_caps($code_obj, $block, %caps) {
421+
$code_obj.SET_CAPS(%caps);
422+
}
423+
424+
# Override this to store the overall NFA for a regex. (Standalone mode doesn't need
425+
# it, as it only supports executing individual regexes).
426+
method store_regex_nfa($code_obj, $block, $nfa) {
427+
}
418428

419429
# XXX Below here copied from p6regex; needs review
420430

@@ -571,8 +581,8 @@ class QRegex::P5Regex::Actions is HLL::Actions {
571581
}
572582
elsif $<nibbler> {
573583
$name eq 'after' ??
574-
$qast[0].push(qbuildsub(self.flip_ast($<nibbler>[0].ast), :anon(1), :addself(1))) !!
575-
$qast[0].push(qbuildsub($<nibbler>[0].ast, :anon(1), :addself(1)));
584+
$qast[0].push(self.qbuildsub(self.flip_ast($<nibbler>[0].ast), :anon(1), :addself(1))) !!
585+
$qast[0].push(self.qbuildsub($<nibbler>[0].ast, :anon(1), :addself(1)));
576586
}
577587
}
578588
make $qast;

src/QRegex/P5Regex/Grammar.nqp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,45 @@ use QRegex;
22
use NQPHLL;
33
use QAST;
44

5+
class QRegex::P5Regex::World is HLL::World {
6+
method create_code($past, $name) {
7+
# Create a fresh stub code, and set its name.
8+
my $dummy := pir::nqp_fresh_stub__PP(-> { nqp::die("Uncompiled code executed") });
9+
pir::assign__vPS($dummy, $name);
10+
11+
# Tag it as a static code ref and add it to the root code refs set.
12+
pir::setprop__vPsP($dummy, 'STATIC_CODE_REF', $dummy);
13+
self.add_root_code_ref($dummy, $past);
14+
15+
# Create code object.
16+
my $code_obj := nqp::create(NQPRegex);
17+
nqp::bindattr($code_obj, NQPRegex, '$!do', $dummy);
18+
my $slot := self.add_object($code_obj);
19+
20+
# Add fixup of the code object and the $!do attribute.
21+
my $fixups := QAST::Stmt.new();
22+
$fixups.push(QAST::Op.new(
23+
:op('bindattr'),
24+
QAST::WVal.new( :value($code_obj) ),
25+
QAST::WVal.new( :value(NQPRegex) ),
26+
QAST::SVal.new( :value('$!do') ),
27+
QAST::BVal.new( :value($past) )
28+
));
29+
$fixups.push(QAST::Op.new(
30+
:op('setcodeobj'),
31+
QAST::BVal.new( :value($past) ),
32+
QAST::WVal.new( :value($code_obj) )
33+
));
34+
self.add_fixup_task(:fixup_past($fixups));
35+
36+
$code_obj
37+
}
38+
}
39+
540
grammar QRegex::P5Regex::Grammar is HLL::Grammar {
641
token TOP {
742
:my %*RX;
43+
:my $*W := QRegex::P6Regex::World.new(:handle(nqp::sha1(self.target)));
844
<nibbler>
945
[ $ || <.panic: 'Confused'> ]
1046
}

0 commit comments

Comments
 (0)