Skip to content

Commit

Permalink
Streamline Cursor a bit + new features
Browse files Browse the repository at this point in the history
- streamline Bool using nqp only
- move bulk of MATCH into !MATCH so that if the Cursor already has
  a Match object, it can return quickly and be inlined
- create STR method: return Str for cursor without need of Match object
- create FROMLEN method: return Pair with from position and length
  without the need of a Match object
  • Loading branch information
lizmat committed Oct 24, 2016
1 parent 4fc17df commit 46abb3b
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions src/core/Cursor.pm
Expand Up @@ -4,13 +4,60 @@ my class Cursor does NQPCursorRole {
my Mu $NO_CAPS := nqp::hash();

multi method Bool(Cursor:D:) {
nqp::getattr_i(self, Cursor, '$!pos') >= nqp::getattr_i(self, Cursor, '$!from')
nqp::p6bool(
nqp::isge_i(
nqp::getattr_i(self, Cursor, '$!pos'),
nqp::getattr_i(self, Cursor, '$!from')
)
)
}

method FROMLEN() {
nqp::if(
nqp::isgt_i(
nqp::getattr_i(self,Cursor,'$!pos'),
nqp::getattr_i(self,Cursor,'$!from')
),
Pair.new(
nqp::getattr_i(self,Cursor,'$!from'),
nqp::sub_i(
nqp::getattr_i(self,Cursor,'$!pos'),
nqp::getattr_i(self,Cursor,'$!from')
)
),
Nil
)
}

method STR() {
nqp::if(
nqp::isgt_i(
nqp::getattr_i(self,Cursor,'$!pos'),
nqp::getattr_i(self,Cursor,'$!from')
),
nqp::substr(
nqp::findmethod(self,'orig')(self),
nqp::getattr_i(self,Cursor,'$!from'),
nqp::sub_i(
nqp::getattr_i(self,Cursor,'$!pos'),
nqp::getattr_i(self,Cursor,'$!from')
)
),
Nil
)
}

method MATCH() {
my $match := nqp::getattr(self, Cursor, '$!match');
return $match if nqp::istype($match, Match) && nqp::isconcrete($match);
$match := nqp::create(Match);
nqp::if(
nqp::istype((my $match := nqp::getattr(self,Cursor,'$!match')),Match)
&& nqp::isconcrete($match),
$match,
self!MATCH
)
}

method !MATCH() {
my $match := nqp::create(Match);
nqp::bindattr($match, Match, '$!orig', nqp::findmethod(self, 'orig')(self));
my int $from = nqp::getattr_i(self, Cursor, '$!from');
my int $to = nqp::getattr_i(self, Cursor, '$!pos');
Expand Down

0 comments on commit 46abb3b

Please sign in to comment.