Author Topic: Feature Request: LOOPS keywords and additional functionality  (Read 3419 times)

0 Members and 1 Guest are viewing this topic.

efgee

  • Guest
Feature Request: LOOPS keywords and additional functionality
« on: October 28, 2011, 10:52:36 AM »
Hello Charles,
Here is my proposal:

Add the each and the when keywords and allow this:

Code: [Select]
do
  ' some loop code...
end do

Code: [Select]
do while x <= 42
  ' some loop code...
end do

Code: [Select]
do
  ' some loop code...
end do when x == 42

Code: [Select]
do for x = 0 to x == 42 do x++
  ' some loop code...
end do

Code: [Select]
do for each x[]
  ' some loop code...
end do

Why this would be cool?

It not only adds syntactic sugar but allowes to add aliases to loops.
What are loop aliases you ask?

Imagine you have some nice loops inside loops inside etc.
A problem arises when you want to break out of the loop and do:

Code: [Select]
do
  ' some loop code...
  if x == 42 then break end if
  ' more loop code...
end do

This is easy, but what about this:

Code: [Select]
do
  ' some loop code...
  if x == 42 then
    break
  ' more loop code...
  else
    do
      ' some loop code...
      if y == 84 then break end if
      ' more loop code...
   end do
      ' even more loop code...
end do

If y equals 84 the code breaks out of the inner loop, but what if you want to break out of the outer loop as well, or you have more than 2 loops and several conditionals?

One way is to use goto and depending on the size of the code you end up with spaghetti code.

The other solution is to use loop aliases. Example:
(keep in mind: I don't insist on the curly brackets...)

Code: [Select]
do {banana}
  ' some loop code...
  if x == 42 then
    break
  ' more loop code...
  else
    do {apple}
      ' some loop code...
      if y == 84 then exit {apple} end if
      ' more loop code...
   end do
      ' even more loop code...
    do {citroen}
      ' some loop code...
      if z == 21 then exit {banana} end if
      ' more loop code...
   end do
      ' even more loop code...
end do

And then the following code can start right after the loop and you don't have to jump around your code with a bunch of gotos.

BTW: Stuff like this should work as well:

Code: [Select]
do {banana} while x <= 42
  ' some loop code...
end do


Thanks for reading.
efgee

EDIT: changed the loop keyword to do.
« Last Edit: October 29, 2011, 05:26:14 PM by efgee »

Charles Pegge

  • Guest
Re: Feature Request: LOOPS keywords and additional functionality
« Reply #1 on: October 28, 2011, 11:33:54 AM »
Hi Frank,

The 'Loop' word is used in legacy x86 assembler. Along with other instructions like Loopnz. (These are sort of microcoded macros and considered disruptive  to the instruction pipeline. I think they disappear completely in 64 bit mode).

So I am not sure whether to reuse this word.

'For each .. is a good candidate for your constructs. We already have an exit for.

It would work nicely with static arrays of objects. But we need a protocol for handling dynamic arrays or lists.

Charles




efgee

  • Guest
Re: Feature Request: LOOPS keywords and additional functionality
« Reply #2 on: October 28, 2011, 12:02:02 PM »
We already have dynamic arrays?


That's a bummer that loop is already used as I don't know of any other usable keyword for loops.
Loop would make so much sense because it really describes what it is, like function etc.
Suppose do would work as well but it's not as descriptive...

Some programming languages have repeat...

Code: [Select]
repeat for each x[]
  ' some loop code...
end repeat

or:

Code: [Select]
repeat {banana}
  ' some loop code...
  if x == 42 then
    break
  ' more loop code...
  else
    repeat {apple}
      ' some loop code...
      if y == 84 then exit {apple} end if
      ' more loop code...
   end repeat
      ' even more loop code...
    repeat {citroen}
      ' some loop code...
      if z == 21 then exit {banana} end if
   end repeat
      ' even more loop code...
end repeat

Looks strange but it could work (if repeat is not used elsewhere)

bye

JRS

  • Guest
Re: Feature Request: LOOPS keywords and additional functionality
« Reply #3 on: October 28, 2011, 04:29:51 PM »
ScriptBasic supports the following.

Code: [Select]
repeat
  ...
  commands to repeat
  ...
until expression

Charles Pegge

  • Guest
Re: Feature Request: LOOPS keywords and additional functionality
« Reply #4 on: October 28, 2011, 06:40:41 PM »
repeat is used in oxygen's generic control structure. This is the foundation for all control structures. It is used in Assembler and also by the compiler itself.

(
  ... exit
  ... repeat
)


Oxygen does not have a repeat..until

do..end do is almost as concise

i=0
do
  i++
 '...
  if i>=10 then exit do
end do

print i


Charles
« Last Edit: October 28, 2011, 06:48:15 PM by Charles Pegge »

efgee

  • Guest
Re: Feature Request: LOOPS keywords and additional functionality
« Reply #5 on: October 29, 2011, 05:27:41 PM »
OK, do it is then.
Changed my request in the first post.
(doesn't look that bad...)

Thanks

Charles Pegge

  • Guest
Re: Feature Request: LOOPS keywords and additional functionality
« Reply #6 on: October 30, 2011, 12:40:33 AM »
One idea I am exploring. The construct ForEach is user defined and works with today's Oxygen-progress.

It is made possible by a small tweak to typeof.

Code: OxygenBasic
  1. 'works with:
  2. 'static arrays of primitives and objects
  3. '08:29 30/10/2011
  4.  
  5.  
  6. def ForEach
  7. scope
  8. sys i_, p_
  9. for i_=1 to spanof %1
  10.   p_=@%1[i_]
  11.   typeof %1 %1 at p_ 'link
  12. end def
  13.  
  14. def EndForEach
  15. next
  16. end scope
  17. end def
  18.  
  19.  
  20.  
  21. 'TEST PRIMITIVES
  22. '===============
  23.  
  24.  
  25. sys d[10]
  26. d<=1,2,3,4,5,6
  27.  
  28. t=0
  29.  
  30. ForEach d
  31.   t+=d
  32. EndForEach
  33.  
  34. print t
  35.  
  36. 'TEST OBJECTS
  37. '============
  38.  
  39. class fruit
  40.   string nm
  41.   sys quantity
  42.   method name(string n)
  43.   nm=n
  44.   end method
  45.   method name() as string
  46.   return nm
  47.   end method
  48. end class
  49.  
  50.  
  51. fruit fr[6]
  52.  
  53. fr<="banana",1,"apple",2,"orange",3,"lemon",4,"pear",5,"mango",6
  54.  
  55. cr=chr(13)+chr(10)
  56. tab=chr(9)
  57. pr="List" cr cr
  58.  
  59. t=0
  60. ForEach fr
  61.   pr+=fr.name() tab fr.quantity cr
  62.   t+=fr.quantity
  63. EndForEach
  64.  
  65. print pr  cr "Total:" tab t
  66.  
  67.  

Charles



« Last Edit: October 30, 2011, 12:53:42 AM by Charles Pegge »