[Parrot-users] Numerical and string comparisons in Squaak (and PIR)

Patrick R. Michaud pmichaud at pobox.com
Wed Feb 2 16:48:45 UTC 2011


On Wed, Feb 02, 2011 at 12:10:30PM +0100, Fredrik Rothamel wrote:
> "<" for example is implemented using pirop "islt PMC, PMC"
> 
> How do I make "islt" use the string PMCs cmp_num method instead of
> the default cmp when the first parameter is a string and the second
> parameter is an integer?

FWIW, NQP handles by first storing each argument into num registers
and then using the islt_n_n opcode.

> Do I need to inherit and modify the string PMC or are there other ways?

The current implementation of 'islt' and other PMC comparisons does
indeed produce somewhat counter-intuitive results at times.  Consider:

    pmichaud at orange:~/parrot$ cat islt.pir
    .sub 'main'
        $P0 = box 3
        $P1 = box '12'
    
        $I0 = islt $P0, $P1    # 3 < '12'
        say $I0
    
        $I0 = islt $P1, $P0    # '12' < 3
        say $I0
    .end

    pmichaud at orange:~/parrot$ ./parrot islt.pir
    1
    1

In other words, the type of comparison performed always depends on
the type of the first IN operand, and you have to do explicit casting
if you want some other form of comparison.

Pm


More information about the Parrot-users mailing list