Author Topic: Out of the LOOP  (Read 2271 times)

Robert

  • Hero Member
  • *****
  • Posts: 1190
    • View Profile
Out of the LOOP
« on: August 26, 2021, 02:59:04 AM »
BCX 7.7.5.1 has 29 EXIT from DO ... LOOP.

27 are EXIT DO

2 are EXIT LOOP

I wish the 2 EXIT LOOP were EXIT DO.

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2039
    • View Profile
Re: Out of the LOOP
« Reply #1 on: August 26, 2021, 07:01:02 PM »
BCX 7.7.5.1 has 29 EXIT from DO ... LOOP.

27 are EXIT DO

2 are EXIT LOOP

I wish the 2 EXIT LOOP were EXIT DO.

Done ... it's funny, TurboBasic got me hooked on EXIT LOOP. 

EXIT DO seems weird to me but it aligns perfectly with EXIT FOR.
I've never seen, for example, EXIT NEXT - that would be really weird.

Robert

  • Hero Member
  • *****
  • Posts: 1190
    • View Profile
Re: Out of the LOOP
« Reply #2 on: August 26, 2021, 09:05:17 PM »
BCX 7.7.5.1 has 29 EXIT from DO ... LOOP.

27 are EXIT DO

2 are EXIT LOOP

I wish the 2 EXIT LOOP were EXIT DO.

Done ... it's funny, TurboBasic got me hooked on EXIT LOOP. 

EXIT DO seems weird to me but it aligns perfectly with EXIT FOR.
I've never seen, for example, EXIT NEXT - that would be really weird.

I really do not understand how EXIT LOOP works. I suspect that somewhere in the machine EXIT LOOP is converted to EXIT DO before processing in in FUNCTION Emit_ControlFlow

Reading through the SELECT CASE flow of CASE "exit" starting, in BCX 7.7.5.1, at line 15971 there is

CASE "sub"
CASE "function"
CASE "for"
CASE "xfor"
CASE "do"
CASE "while"
CASE "select"
CASE "nest"
and
CASE ELSE

There is no CASE "loop"

When translating my UTF-8 identifier enabled version of BCX, when the translator hits those two EXIT LOOPS in the BCX source code, a warning is thrown by BCX, the CASE ELSE warning,  which in these instances is

"Exiting from unnamed control loop. Inner most control loop is DO"

EXIT LOOP works, as anyone can see by checking the example from the Help file

Code: [Select]
DIM a
 
 DO
   a++
   IF a > 45 THEN
     EXIT LOOP
   END IF
   ?  a
 LOOP

but I am wondering what is causing that warning in my UTF-8 BCX but not in the standard BCX.


MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2039
    • View Profile
Re: Out of the LOOP
« Reply #3 on: August 26, 2021, 11:09:05 PM »
Hi Robert,

Using the short Help file example, when entering Emit_ControlFlow,
Stk$[2] definitely contains the word "LOOP".  It proceeds to
the CASE ELSE clause, as you pointed out.  The line is not being
translated  to "EXIT DO" as you speculated.

At that point, DoGoto() takes over and is only looking at the queue
of loop types, it is not looking at strings anymore.  A "DO" loop is enumerated
type 1, so as long as the queue is not empty and it contains a valid loop type,
BCX creates and emits a GOTO statement ( L1000 ) in this example.

I don't have enough info to speculate on your UTF-8 connection to this subject.

Below is the enum for the loop types used by DoGoto()


ENUM   ' Types used in LoopType
  lt_DOLOOP = 1
  lt_DOWHILELOOP
  lt_DOUNTILLOOP
  lt_WHILEWEND
  lt_FORNEXT
  lt_SELECT
  lt_FORXNEXT
END ENUM


 TYPE tagLoopLoc
  DIM iLoopType                    AS INT
  DIM iLoopLine                    AS INT
  DIM iJumpTo                      AS INT
  DIM szUseNeedLabel[cSizeOfLabel] AS CHAR
END TYPE


Robert

  • Hero Member
  • *****
  • Posts: 1190
    • View Profile
Re: Out of the LOOP
« Reply #4 on: August 26, 2021, 11:24:19 PM »
Hi Robert,

Using the short Help file example, when entering Emit_ControlFlow,
Stk$[2] definitely contains the word "LOOP".  It proceeds to
the CASE ELSE clause, as you pointed out.  The line is not being
translated  to "EXIT DO" as you speculated.

At that point, DoGoto() takes over and is only looking at the queue
of loop types, it is not looking at strings anymore.  A "DO" loop is enumerated
type 1, so as long as the queue is not empty and it contains a valid loop type,
BCX creates and emits a GOTO statement ( L1000 ) in this example.

I don't have enough info to speculate on your UTF-8 connection to this subject.

Below is the enum for the loop types used by DoGoto()


ENUM   ' Types used in LoopType
  lt_DOLOOP = 1
  lt_DOWHILELOOP
  lt_DOUNTILLOOP
  lt_WHILEWEND
  lt_FORNEXT
  lt_SELECT
  lt_FORXNEXT
END ENUM


 TYPE tagLoopLoc
  DIM iLoopType                    AS INT
  DIM iLoopLine                    AS INT
  DIM iJumpTo                      AS INT
  DIM szUseNeedLabel[cSizeOfLabel] AS CHAR
END TYPE



Hi MrBCX:

Thank you for this information. It has prompted me to dig a little deeper.

Robert

  • Hero Member
  • *****
  • Posts: 1190
    • View Profile
Re: Out of the LOOP
« Reply #5 on: August 26, 2021, 11:34:18 PM »
Hi Robert,

Using the short Help file example, when entering Emit_ControlFlow,
Stk$[2] definitely contains the word "LOOP".  It proceeds to
the CASE ELSE clause, as you pointed out.  The line is not being
translated  to "EXIT DO" as you speculated.

At that point, DoGoto() takes over and is only looking at the queue
of loop types, it is not looking at strings anymore.  A "DO" loop is enumerated
type 1, so as long as the queue is not empty and it contains a valid loop type,
BCX creates and emits a GOTO statement ( L1000 ) in this example.

I don't have enough info to speculate on your UTF-8 connection to this subject.

Below is the enum for the loop types used by DoGoto()


ENUM   ' Types used in LoopType
  lt_DOLOOP = 1
  lt_DOWHILELOOP
  lt_DOUNTILLOOP
  lt_WHILEWEND
  lt_FORNEXT
  lt_SELECT
  lt_FORXNEXT
END ENUM


 TYPE tagLoopLoc
  DIM iLoopType                    AS INT
  DIM iLoopLine                    AS INT
  DIM iJumpTo                      AS INT
  DIM szUseNeedLabel[cSizeOfLabel] AS CHAR
END TYPE



Hi MrBCX:

Thank you for this information. It has prompted me to dig a little deeper.

O.K. That didn't take long. On my UTF-8 translate compile batch file I have the -w flag set on BCX. Thats why I got the warnings.

I really appreciate your analysis as it has helped relieve me of that niggling doubt, you know the one, "Something is wrong ... something is wrong."

Good show !

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2039
    • View Profile
Re: Out of the LOOP
« Reply #6 on: August 27, 2021, 01:48:17 PM »
The next version of BCX will include the keyword "loop" in Emit_ControlFlow() enabling
us to use -w without throwing a warning when EXIT LOOP is encountered.


   CASE "do", "loop"
        DIM RAW szGL$
        DIM RAW i

        XFOR i = LoopTypeCnt-iLoops2 _
          UNTIL LoopType.iLoopType = lt_DOLOOP OR _
          LoopType.iLoopType = lt_DOUNTILLOOP  OR _
          LoopType.iLoopType = lt_DOWHILELOOP  OR i = 0 BY i--
        XNEXT

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2039
    • View Profile
Re: Out of the LOOP
« Reply #7 on: August 28, 2021, 11:52:57 PM »
The next version of BCX will include the keyword "loop" in Emit_ControlFlow() enabling
us to use -w without throwing a warning when EXIT LOOP is encountered.

   CASE "do", "loop"
 

I've decided against this change, as there is long standing code this will break.  It has,
for example, always been allowed to use EXIT LOOP inside a WHILE-WEND loop.  The
change I proposed would cause the translation to fail and would force users to edit
their programs.  I'm not going to impose all that for the sake of a minor warning.

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2039
    • View Profile
Re: Out of the LOOP
« Reply #8 on: August 29, 2021, 11:08:09 AM »
 Not wanting to seem unsympathetic to this issue, I've modified the warning that BCX
 throws when the -w switch is supplied.  This will hopefully paint a clearer picture of
 what BCX is trying to warn us about.

  CASE ELSE
   DIM RAW szGL$
   szGL$ = "Exiting from a misnamed or unnamed control loop. Expected: EXIT "


Using this modified example:

Code: [Select]
DIM a
DO
  a++
  IF a > 45 THEN
    EXIT LOOP
  END IF
  ?  a
LOOP

WHILE a > 0
  a--
  IF a = 0 THEN
    EXIT LOOP
  END IF
  ?  a
WEND

Produces ...

Warnings! :
 Line 5 in Module: a.bas - Exiting from a misnamed or unnamed control loop.  Expected: EXIT DO

 Line 15 in Module: a.bas - Exiting from a misnamed or unnamed control loop.  Expected: EXIT WHILE

Robert

  • Hero Member
  • *****
  • Posts: 1190
    • View Profile
Re: Out of the LOOP
« Reply #9 on: August 29, 2021, 03:50:51 PM »
Not wanting to seem unsympathetic to this issue, I've modified the warning that BCX
 throws when the -w switch is supplied.  This will hopefully paint a clearer picture of
 what BCX is trying to warn us about.

  CASE ELSE
   DIM RAW szGL$
   szGL$ = "Exiting from a misnamed or unnamed control loop. Expected: EXIT "


Using this modified example:

Code: [Select]
DIM a
DO
  a++
  IF a > 45 THEN
    EXIT LOOP
  END IF
  ?  a
LOOP

WHILE a > 0
  a--
  IF a = 0 THEN
    EXIT LOOP
  END IF
  ?  a
WEND

Produces ...

Warnings! :
 Line 5 in Module: a.bas - Exiting from a misnamed or unnamed control loop.  Expected: EXIT DO

 Line 15 in Module: a.bas - Exiting from a misnamed or unnamed control loop.  Expected: EXIT WHILE

Hi MrBCX:

I like this !

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2039
    • View Profile
Re: Out of the LOOP
« Reply #10 on: April 08, 2022, 12:36:39 PM »
What started as an experiment will be seen in BCX 783

I replaced literally every WHILE-WEND with DO WHILE - LOOP

Code: [Select]

WHILE *s <> c_DblQt
       IF *s = 0 THEN EXIT WHILE
       INCR s
WEND



Code: [Select]

DO WHILE *s <> c_DblQt
      IF *s = 0 THEN EXIT DO
      INCR s
LOOP