Skip to content
Browse files

Start !cursor_start refactor.

Currently it is the only thing in the NQP or Rakudo codebase that uses
Parrot's multiple return values feature. This splits it up into two
routines, one for things that really only want the cursor anyway (and
so this will be an efficiency win for them) and one for generated code
that wants all the things (which we can still do without allocating).
  • Loading branch information...
1 parent 029bf5c commit c56d32262d87ed4d84090706294ce90d4a788ee3 @jnthn jnthn committed Feb 11, 2013
Showing with 48 additions and 0 deletions.
  1. +48 −0 src/QRegex/Cursor.nqp
View
48 src/QRegex/Cursor.nqp
@@ -120,6 +120,54 @@ role NQPCursorRole is export {
0
)
}
+
+ # Starts a new Cursor, returning all information relating to it in an array.
+ # The array is valid until the next call to !cursor_start_all.
+ my $NO_RESTART := 0;
+ my $RESTART := 1;
+ my @start_result;
+ method !cursor_start_all() {
+ my $new := nqp::create(self);
+ my $sub := nqp::callercode();
+ nqp::bindattr($new, $?CLASS, '$!shared', $!shared);
+ nqp::bindattr($new, $?CLASS, '$!regexsub', nqp::ifnull(nqp::getcodeobj($sub), $sub));
+ if nqp::defined($!restart) {
+ nqp::bindattr_i($new, $?CLASS, '$!pos', $!pos);
+ nqp::bindattr($new, $?CLASS, '$!cstack', nqp::clone($!cstack)) if $!cstack;
+ nqp::bindpos(@start_result, 0, $new);
+ nqp::bindpos(@start_result, 1, nqp::getattr_s($!shared, ParseShared, '$!target'));
+ nqp::bindpos(@start_result, 2, nqp::bindattr_i($new, $?CLASS, '$!from', $!from));
+ nqp::bindpos(@start_result, 3, $?CLASS);
+ nqp::bindpos(@start_result, 4, nqp::bindattr($new, $?CLASS, '$!bstack', nqp::clone($!bstack)));
+ nqp::bindpos(@start_result, 5, $RESTART);
+ @start_result
+ }
+ else {
+ nqp::bindattr_i($new, $?CLASS, '$!pos', -3);
+ nqp::bindpos(@start_result, 0, $new);
+ nqp::bindpos(@start_result, 1, nqp::getattr_s($!shared, ParseShared, '$!target'));
+ nqp::bindpos(@start_result, 2, nqp::bindattr_i($new, $?CLASS, '$!from', $!pos));
+ nqp::bindpos(@start_result, 3, $?CLASS);
+ nqp::bindpos(@start_result, 4, nqp::bindattr($new, $?CLASS, '$!bstack', nqp::list_i()));
+ nqp::bindpos(@start_result, 5, $NO_RESTART);
+ @start_result
+ }
+ }
+
+ # Starts a new cursor, returning nothing but the cursor.
+ method !cursor_start_cur() {
+ my $new := nqp::create(self);
+ my $sub := nqp::callercode();
+ nqp::bindattr($new, $?CLASS, '$!shared', $!shared);
+ nqp::bindattr($new, $?CLASS, '$!regexsub', nqp::ifnull(nqp::getcodeobj($sub), $sub));
+ if nqp::defined($!restart) {
+ nqp::die("!cursor_start_cur cannot restart a cursor");
+ }
+ nqp::bindattr_i($new, $?CLASS, '$!pos', -3);
+ nqp::bindattr_i($new, $?CLASS, '$!from', $!pos);
+ nqp::bindattr($new, $?CLASS, '$!bstack', nqp::list_i());
+ $new
+ }
method !cursor_start_subcapture($from) {
my $new := nqp::create(self);

0 comments on commit c56d322

Please sign in to comment.
Something went wrong with that request. Please try again.