EXIT statement

Does Matrix Brandy support EXIT (and if not, could it)? It's one of the few surprising omissions from ARM BASIC V (adding WHILE ... ENDWHILE loops but not EXIT, or BREAK or some equivalent, seems extraordinary).

I consider it so important that I've implemented EXIT in BBC BASIC (Z80) v5, giving it priority over some ARM BASIC V features that I've not bothered to implement. There really aren't any 'elegant' workarounds to its omission, in my opinion.

The best you can do is probably to use a dummy PROC, so rather than:
      FOR I = 1 TO 10
        IF I = 5 EXIT FOR
      NEXT I
      PRINT I
      END
you could do this:
      PROCdummy
      PRINT I
      END

      DEF PROCdummy
      FOR I = 1 TO 10
        IF I = 5 ENDPROC
      NEXT I
      ENDPROC
but it's messy.

Comments

  • It is not something that is currently supported, but I will investigate adding this.
  • Soruk wrote: »
    It is not something that is currently supported, but I will investigate adding this.
    There's very little code needed to implement it in my BASICs (one reason why it got included in the new Z80 version) because to 'search forward' to find the end of the loop it simply calls the same routine that WHILE uses when searching for the ENDWHILE.

    I know that Brandy is a little different from other versions of BBC BASIC in (probably) finding the destination ENDWHILE during the initial parsing phase rather than at run time - an obvious optimisation if there is such an initial pass. That could well impact on the way EXIT would be implemented.

    But hopefully the similarity between WHILE FALSE and EXIT would be just as easy to exploit as it is in my BASICs.
  • Soruk
    edited June 9
    After a bit of trial and error, I think I have it working - it certainly did what I expected it to do based on your BBCSDL documentation. Nightly builds have been built, they're over here http://brandy.matrixnetwork.co.uk/nightly/

    One small difference with yours is that EXIT FOR does not take a control variable, it expects FOR..NEXT to be properly nested.
  • Soruk wrote: »
    One small difference with yours is that EXIT FOR does not take a control variable
    The variable is optional; you only need it when you want to exit an outer level of nesting than the current one:
          FOR I = 1 TO 5
            FOR J = 1 TO 5
              FOR K = 1 TO 5
                IF I = 1 AND J = 2 AND K = 3 EXIT FOR I
              NEXT K
            NEXT J
          NEXT I
          PRINT I, J, K
    


  • It would be very messy in Matrix Brandy trying to unwind the stack to do this with any degree of reliability.

    I guess this approach may work:
          break%=FALSE
          FOR I = 1 TO 5
            FOR J = 1 TO 5
              FOR K = 1 TO 5
                IF I = 1 AND J = 2 AND K = 3 break%=TRUE:EXIT FOR
              NEXT K
              IF break% EXIT FOR
            NEXT J
            IF break% EXIT FOR
          NEXT I
          PRINT I, J, K
    
  • Soruk wrote: »
    I guess this approach may work:
    Yes, it's pretty unusual to want to exit multiple levels at once and I only support it in my BASICs because it was easy - it uses effectively the same mechanism as NEXT does to exit multiple levels:
       10 FOR dummy = 1 TO 1
       20   FOR I = 1 TO 5
       30     FOR J = 1 TO 5
       40       FOR K = 1 TO 5
       50         IF I = 1 AND J = 2 AND K = 3 THEN 90
       60       NEXT K
       70     NEXT J
       80   NEXT I
       90 NEXT dummy
      100 PRINT I, J, K
    
    Here the dummy outer loop ensures that nothing gets left on the stack, which would happen otherwise.
  • I'll have a look at that approach to see how easy it would be to implement.