Author Topic: C++ style FOR block  (Read 624 times)

0 Members and 1 Guest are viewing this topic.

Brian Alvarez

  • Hero Member
  • *****
  • Posts: 643
    • PluriBASIC
C++ style FOR block
« on: August 14, 2019, 10:08:54 PM »
 Hello Charles. I have this code in PluriBASIC (database support):

Code: [Select]
    FOR EACH Usr WHERE "admin=1"
        printbr "Record #" & format$( & " retrieved correctly."     
    NEXT Usr

It is being converted to something like this:

Code: [Select]
   for (database.load(Err, 1, @usr, "admin=1");, 1, @usr); database.count()) {
       print "Record #" + ¤FORMAT(EXE.lng(@usr, 0), byval 0) + " retrieved correctly."

But oxygen is complaining about this:

Code: [Select]
ERROR: ERROR: Missing '='
WORD: database.load
IN: pbmain
LINE: 2157
FILE: "main source

I am guessing Oxygen is expecting a counter to be setup... but not always a counter is setup in C++ style blocks.

AFAIK, any statement should be allowed there, as well as the incrementer, any statement should be allowed there. In other words
the c style for/next blocks should allow three sections (in this order):

  • An initial setup statement (not necessarily a counter setup like: i=0).
  • A situation that should be TRUE so that the loop continues (loop should break as soon as it becomes false).
  • Anything that can be used as incrementer, even a function call.

 In this case, i am using database.load() as the initial setup. Then returns TRUE as long as there are more records loaded. and database.counter() which currently does nothing but is prepared for future features.

Note that all functions work properly individually... but the for initializer is expecting me to setup a counter variable (i am guessing).

 Not forcing a counter setup (for example: counter = 0), would make this statement more versatile.

I got it to compile by setting a dummy (unused) counter like this:

Code: [Select]
for (¤CNTR = database.load(Err, 1, @usr, "admin=1");, 1, @usr); database.count()) {
 But IMO, i think even the following could be a valid statement:

Code: [Select]
for (;1;) {
   ' determine conditions for breaking the loop here.

« Last Edit: August 14, 2019, 11:15:39 PM by Brian Alvarez »

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 4409
    • Oxygen Basic
Re: C++ style FOR block
« Reply #1 on: August 15, 2019, 03:38:26 AM »
Hi Brian,

Yes I can rearrange o2 iteration-compiling quite easily to allow any expression, or none, at the beginning. (c-style):

my test cases:
Code: [Select]
int i
int bg(){i=1}         'begin
int en(){return i<=3} 'end
int st(){i++}         'step
int it(){print i}     'action
for bg();en();st(){it()}
'for ;en();st(){it()}