Shop OBEX P1 Docs P2 Docs Learn Events
SPIN IF Condition execution... — Parallax Forums

SPIN IF Condition execution...

RaymanRayman Posts: 14,162
edited 2007-11-16 20:16 in Propeller 1
Anybody know if the SPIN IF command processes all it's conditions or aborts on the first false one?

E.g., if I have two methods (doThis(x):bOk, doThat(y):bOk) that return a boolean and I have

IF·doThis(x) AND doThat(y)
·· ...

And, it happens that doThis(x) returns false.· Will the doThat(y) method still be called?

I know that in C++ it wouldn't be.· But, the manual wasn't terribly clear about this...

Also, are these processed in the order their written?· (I assuming that doThis(x) would be called first, but I guess it not necessarily a given...)

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-11-16 15:56
    It's not documented anywhere, so you have to assume that "short circuit" Boolean evaluation is not used. There's also no guarantee of the order of processing since it's not documented. If you want to guarantee the evaluation order, you'll have to split the statements up yourself:
    if doThis(x)
       if doThat(y)
          ' do something
    
    


    There's an advantage to not documenting this in that Parallax can change it in the future without breaking existing programs.
  • hippyhippy Posts: 1,981
    edited 2007-11-16 17:08
    Bytecode generation appears to be left to right with no short-circuit evaluation for just AND's and OR's, but note that AND takes precedence over OR so "if A or B and C" evaluates as "if A or ( B and C )"

    ====                                  ; PUB Main
    ====                                  ;   if DoThis(a) And DoThat(b)
    ====                                  ;     a := true
    
    0020         00 40 05 02     S5       CALLFUN  F7 ( L9.LONG )
    0024         00 44 05 03              CALLFUN  F8 ( L10.LONG )
    0028         F0                       LOG_AND
    0029         0A 02                    JPF      N6
    002B         34 41                    LET      L9.LONG, #-1
    002D         32              N6       RETURN   
    
    ====                                  ; PRI DoThis( n )
    
    002E         32              F7       RETURN   
    
    ====                                  ; PRI DoThat( n )
    
    002F         32              F8       RETURN   
    
    0030         00 00 00 00     L9       LONG     0
    0034         00 00 00 00     L10      LONG     0
    



    It is possible that short-circuit evaluation could be added in the future, but the compiler doesn't even optimise away "+0" at present and adding it would likely break a lot of existing code.
  • RaymanRayman Posts: 14,162
    edited 2007-11-16 18:42
    Thanks.
    Hippy: Do you have some kind of SPIN decompiler?
  • hippyhippy Posts: 1,981
    edited 2007-11-16 20:02
    Rayman said...
    Hippy: Do you have some kind of SPIN decompiler?

    You missed it !? rolleyes.gif

    http://forums.parallax.com/showthread.php?p=665019

    Enjoy
  • RaymanRayman Posts: 14,162
    edited 2007-11-16 20:16
    Very cool. I think that was way over my head back then... What, 2.5 months ago... That's about when I got my first chips...
Sign In or Register to comment.