Hi all,

I am returning to the topic of search_lex. I ask to be excused because
this is another "I need this op" mail, but I think the feature I request
here has merit for other languages as well...

In my case my "problem" (challenge) is that I want to generate some
parrot code that is capable of searching the lexical pads for the
existence of a lexical. In my particular case this code executes
frequently.

My current code generator generates code to trap the LEX_NOT_FOUND
exception. However, the "set_eh" instruction is *terribly* slow...

I hacked around var.ops a bit to introduce a set of ops called
'search_lex' which just return a null PMC if the lexical is not found
(see the bottom of this mail for the implementation. It is quite
trivial). 

With these ops, detection of a non-existing lexical is orders of
magnitude faster and easier, so I would like to plead the gods of parrot
to contemplate them for inclusion in the basic operation set... (please!
please! :-)

++Jos.nl

P.S. There is a strong NLP case for this op as well, since the verb
     "search" implies the possibility of failure, whereas "find" does
     not. When asked what I am doing (while scurrying around the
     room) I like to say that I am finding my sandwich instead of
     searching it.... :-)


###############################################################################

=item B<search_lex>(out PMC, in STR)

Search the lexical variable named $2 (at any depth) and store it in $1.
Returns null if the lexical is not found.

=item B<search_lex>(out PMC, in INT, in STR)

Search the lexical variable named $3 at depth $2 and store it in $1.
If $2 is negative the, count out from the current lexical scope;
otherwise, count up from the outermost scope.
Returns null if the lexical is not found.

=item B<search_lex>(out PMC, in INT, in INT)

Search the lexical variable at position $3 at depth $2 and store it in
$1. If $2 is negative the, count out from the current lexical scope;
otherwise, count up from the outermost scope.
Returns null if the lexical is not found.

=cut

op search_lex(out PMC, in STR) {
    PMC * pad = scratchpad_get_current(interpreter);
    $1 = scratchpad_get(interpreter, pad, $2, 0);
    goto NEXT();
}

op search_lex(out PMC, in INT) {
    PMC * pad = scratchpad_get_current(interpreter);
    $1 = scratchpad_get(interpreter, pad, NULL, $2);
    goto NEXT();
}

op search_lex(out PMC, in INT, in STR) {
    PMC * pad = scratchpad_get_current(interpreter);
    $1 = scratchpad_get_index(interpreter, pad, $2, $3, 0);
    goto NEXT();
}

op search_lex(out PMC, in INT, in INT) {
    PMC * pad = scratchpad_get_current(interpreter);
    $1 = scratchpad_get_index(interpreter, pad, $2, NULL, $3);
    goto NEXT();
}

=back

=cut

-- 
ek is so lug jy vlieg deur my
sonder jou is ek sonder patroon
   "Breyten Breytenbach"

Reply via email to