Author Topic: for/next fundamental flaw  (Read 2613 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4419
    • Oxygen Basic
Re: for/next fundamental flaw
« Reply #15 on: July 19, 2019, 02:16:06 PM »
To handle all contingencies, I can redesign the basic for loop, decomposing it first to a do loop

There is a slight performance hit, because the stepper value is iteratively tested for +/-.

Code: [Select]
for i=b to e step s
  ...
next

----->

i=b
._for
  if s>0
    if i>e
      jmp fwd ._end_for
    end if
  else 's<=0
    if i<e
      jmp fwd _end_for
    endif
  endif
  '
  ...
  '
  ._next
  i+=s
  jmp _for
._end_for
« Last Edit: July 19, 2019, 02:26:11 PM by Charles Pegge »

Brian Alvarez

  • Hero Member
  • *****
  • Posts: 653
    • PluriBASIC
Re: for/next fundamental flaw
« Reply #16 on: July 19, 2019, 03:01:40 PM »
Hello Charles,

  That is kind of how i fixed it here. No problems here anymore. :)
 Yes, there is a performance hit, but it is barely noticeable.

 Besides, you still have the control and it can be optimized by still
supporting the explicit - operator at compilation time, or ommitting the
generation of this code for the for/next blocks that do not have a STEP
parameter.

Thoughts: I personally think it is better to define the STEP at runtime
because if a - operator is provided with a variable, and the variable is
a negative value... the conditions for the end of the loop will never be met.
« Last Edit: July 19, 2019, 06:19:43 PM by Brian Alvarez »

John

  • Hero Member
  • *****
  • Posts: 3809
Re: for/next fundamental flaw
« Reply #17 on: July 20, 2019, 11:17:24 AM »
Quote from: Charles
To handle all contingencies, I can redesign the basic for loop, decomposing it first to a do loop.

That's how I see FOR. DO with more options. (range and step)

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4419
    • Oxygen Basic
Re: for/next fundamental flaw
« Reply #18 on: July 22, 2019, 11:32:04 AM »
Released 0.2.4 in OxygenBasicProgress.zip, with unscoped blocks, and new for block with runtime step testing as disscussed.

Brian Alvarez

  • Hero Member
  • *****
  • Posts: 653
    • PluriBASIC
Re: for/next fundamental flaw
« Reply #19 on: July 22, 2019, 02:12:32 PM »
Thanks Charles, i just downloaded, everything seems great.  :)