GLBasic forum

Main forum => GLBasic - en => Topic started by: MrPlow on 2021-Aug-08

Title: Nest loops parsing and grouping...
Post by: MrPlow on 2021-Aug-08
Hi All,
It's been a while and Im looking to improve one of my apps.
I am hoping fix a bug that involves creating a nested loop relationship and I'm stuck on this problem...

Commands should run from top to bottom and loops within loops should complete before moving on other solo commands and additional loops further down.

Code (glbasic) Select
Loop 1
  command 1
  Loop 2 (1st nest)
    command 2
    command 3
  Endloop
[b] command 4[/b]
  Loop 3
     command 5
     command 6
  Endloop 
[b] command 6[/b]
Endloop


My problem is that command 4 and command 6 are linked to the last group that was closed off - I can fix them recursively but I am thinking I might be making it more complex than it needs to be.
Has anyone come across or solved this type of issue?
i know similar json parsing rules might be a place to look too?

I currently track groupid which increments and groupno (which is supposed to catch groups at the same level (within a parent group))



Title: Re: Nest loops parsing and grouping...
Post by: spacefractal on 2021-Aug-08
im have no idea what tis happens happens. Do a short example code.but howover make sure you using different variables for the loops, or its mighrbeen confusion and skips it or do wierd behaiver.
Title: Re: Nest loops parsing and grouping...
Post by: MrPlow on 2021-Aug-12
If say I had...

Code (glbasic) Select
pu rt 69 fd 80 pd
repeat 90
   repeat 3
       repeat 2
         fd 20
         rt 35
         fd 20
         lt 35
      end
   rt 180
   repeat 3
      fd 20
      rt 35
      fd 20
      lt 35
   end
lt 90
end
rt 180
lt 60
end


And I want to loop through in objects[] array and
run each repeat independently?

Currently i have 3 levels of nested repeat loops - but I want the function to be fully recursive - I just have a block trying to figure an easy was to do without having to create class instances etc.


Current repeat top level repeat looks something like...

Code (glbasic) Select
FUNCTION runactions_repeat: repid,qty

FOR r = 1 TO qty-1
FOREACH aa IN actions[]

IF aa.cmd$="REPEAT" AND aa.repid = masterrep+1 //AND aa.repid > repid //AND nesting=TRUE

//fixing repeats - only running correct group
onlyrungroup = aa.groupno
// onlyrungroup = aa.groupid


//run select level of repeat
addlog("repeat "+aa.repid+1+" "+ aa.param)
runactions_repeat2(aa.repid+1,aa.param)

onlyrungroup = aa.groupno // -1
addlog("End of repeat")
addlog("groupno = " + onlyrungroup)


addlog("groupid set to " + aa.groupid)

ENDIF
// new add GP August 2
   // IF aa.cmd$="END" THEN onlyrungroup = aa.groupno -1

IF onlyrungroup = aa.groupno

... process all commands in this repeat

ENDIF

ENDFUNCTION




Title: Re: Nest loops parsing and grouping...
Post by: MrPlow on 2021-Aug-14
Super - I solved the issue
my recursive solution...

Code (glbasic) Select

FUNCTION processrepeats:seq,loop


LOCAL myline = seq
LOCAL returnline = 0
FOR lp = 1 TO loop-1 // repeat x times
FOR hh = seq TO LEN(actions[])-1 //begin at next command in list

IF actions[hh].cmd$="REPEAT"

    processrepeats(hh+1,actions[hh].param)

ELSEIF actions[hh].cmd$="END"

BREAK

ELSE

LOCAL cm AS action
cm.cmd$=actions[hh].cmd$
cm.param = actions[hh].param
cm.id = LEN(exp_actions[])+1
DIMPUSH exp_actions[], cm

ENDIF


NEXT

NEXT

ENDFUNCTION