Re: Strange machine-level exception

new topic     » goto parent     » topic index » view thread      » older message » newer message

Daryl Border wrote:

> I also think the routines
> should return -1 for an out of bounds condition instead of 0. The current 
> behavior makes a return value of 0 ambiguous.

Maybe, but that is likely to break a lot of code that does this sort of thing
...

    if find_from(a,b,c) = 0 then
        doNotFound()
    end if


Anyhow, below is my version of what the code in be_runtime.c should be doing ...

long find_from(object a, s1_ptr b, object c)
/* find object a as an element of sequence b starting from c*/
{
    register object_ptr bp;
    register object_ptr b_end;
    register object bv;

    if (!IS_SEQUENCE(b))
        RTFatal("second argument of find_from() must be a sequence");

    if (!IS_ATOM_INT(c))
        RTFatal("third argument of find_from() must be an integer");

    b = SEQ_PTR(b);
    if (b->length == 0)
        return 0;

    if (b->length < c) // should this be an error instead?
        return 0;
    if (0 >= c) // should this be an error instead?
        return 0;

    bp = b->base; // Point to start of 'b'
    bp += c - 1;  // Point to start of scan within 'b'
    b_end = b->base + length - 1; // Point to last element in 'b'
    if (IS_ATOM_INT(a)) {
        while (bp <= b_end) {
            bv = *bp;
            bp++;
            if (IS_ATOM_INT(bv)) {
                if (a == bv)
                    return bp - (object_ptr)b->base;
            }
            else if (bv == NOVALUE) {
                return 0;
            }
            else if (compare(a, bv) == 0) {  /* not INT-INT case */
                return bp - (object_ptr)b->base;
            }
        }
    }
    else if (IS_SEQUENCE(a)) {
        long a_len;

        a_len = SEQ_PTR(a)->length;
        if (a_len > 0) {
            while(bp <= b_end) {
                bv = *bp;
                bp++;
                if (IS_SEQUENCE(bv)) {
                    if (a_len == SEQ_PTR(bv)->length) {
                        /* a is SEQUENCE => not INT-INT case */
                        if (compare(a, bv) == 0)
                            return bp - (object_ptr)b->base;
                    }
                }
            }
        }
    }
    else {
        while (bp <= b_end) {
            bv = *bp;
            bp++;
            if ( !IS_SEQUENCE(bv)) {
                if (compare(a, bv) == 0) {
                    return bp - (object_ptr)b->base;
                }
            }
        }
    }
    
    return(0);
}


-- 
Derek Parnell
Melbourne, Australia
Skype name: derek.j.parnell

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu