bmx-ng / bcc

A next-generation bcc parser for BlitzMax
zlib License
33 stars 13 forks source link

For loops with negative steps and unsigned iterators fail #622

Closed GWRon closed 1 year ago

GWRon commented 1 year ago
SuperStrict
Framework Brl.StandardIO

Local maxValue:UInt = 10
For Local i:Long = maxValue - 1 To 0 Step -1
    print i
Next

print "----"

For Local i:Byte = maxValue - 1 To 0 Step -1
    print i
Next

print "----"
'For Local i:UInt = maxValue - 1 To 0 Step -1
'   print i
'Next

With i:Byte nothing is print With i:UInt it prints a lot :)

Kerntrick commented 1 year ago
BBUINT bbt_maxValue=10U;
{
    BBLONG bbt_i=((BBLONG)bbt_maxValue);
    for(;(bbt_i>=0LL);bbt_i=(bbt_i+-1LL)){
        brl_standardio_Print(bbStringFromLong(bbt_i));
    }
}
brl_standardio_Print(&_s1);
{
    BBBYTE bbt_i2=((BBBYTE)bbt_maxValue);
    for(;(bbt_i2<=0);bbt_i2=(bbt_i2+255)){
        brl_standardio_Print(bbStringFromInt(bbt_i2));
    }
}
brl_standardio_Print(&_s2);
{
    BBUINT bbt_i3=bbt_maxValue;
    for(;(bbt_i3>=0U);bbt_i3=(bbt_i3+-1U)){
        brl_standardio_Print(bbStringFromUInt(bbt_i3));
    }
}

The loop generated for the byte iterator is interesting.

GWRon commented 1 year ago

I told it Brucey some hours ago (prior creating the issue) and he found the "+-1U" interesting :D

woollybah commented 1 year ago

Be aware that due to the nature of unsigned numbers, For Local i:Byte = maxValue - 1 To 0 Step -1 will result in i wrapping at zero and the loop will carry on indefinitely.