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

Fredrik Rothamel fredrik.rothamel at bahnhof.se
Thu Feb 3 09:18:21 UTC 2011


Patrick R. Michaud skrev 2011-02-02 17:48:
> 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.

Yes, the simplest way would probably be to change the grammar to use 
different ops for number and string comparisons ("<" vs "lt").

>> 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.

Nice example. Describes the behaviour very clearly.

I was thinking of how to best implement casting in the second comparison 
above.
Should I create a new string class overriding the cmp method?

BR,
Fredrik


More information about the Parrot-users mailing list