Kara-Moon Forum
March 28, 2024, 08:03:46 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: You can go back to the main site here: Kara-Moon site
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Loop variables  (Read 2270 times)
sciurius
Sr. Member
****
Posts: 443



« on: January 08, 2019, 09:38:51 AM »

I propose the following variables for use within Repeat/EndRepeat:

  • _Loop.Max
    The number of times this Repeat/EndRepeat will repeat.
    This variable can be set to permanently change the number of repeats of this Repeat/EndRepeat (but read on).
  • _Loop.Iter
    The current iteration, a number between 1 and $_Loop.Max.
  • _Loop.Last
    Defined only in the last iteration of the loop.

This would making long jumps (e.g. D.C. al Fine) easier to write:

Code:
Label Capo

1 C

Repeat
  2 D
  3 E
  If > $_Loop.Max 1
    If Def $_Loop.Last
      Set $_Loop.Max 1  // next time no repeat
      Goto Capo         // D.C. al Fine
    EndIf
  EndIf
  4 F
EndRepeat

5 G

Label Fine

An alternative way to prevent the next execution of this Repeat/EndRepeat to not repeat (i.e. Set $_Loop.Max 1 in the above example) would be okay too. Mutable internal variables are currently alien to MMA.
Logged
bvdp
Kara-Moon Master
****
Posts: 1436


WWW
« Reply #1 on: January 08, 2019, 04:44:45 PM »

I'm assuming that in:

Code:
Repeat
   stuff
Repeatend 4

The mythical variable will get set to 4.

What happens with nested repeats?

Also, it doesn't quite work out very easily due to the way repeat currently works ... when a repeat is found we suck input to repeatend, without expanding anything (including macros). When repeatend/ending is found we gather up the stuff we've sucked out and push that back into the input stream the proper number of times.

Basically what's happening is that the repeat/end is being treated as special input and actually duplicated for later processing. So, now with a special variable, do we expand it as we create the copies or do we leave expansion until the copies are being processed?  If we expand as we gather, we end up with a special case in the repeat loop which might be the best way ... leaving it will do not much other than have the same variable multiple times.

So, I guess what would need to be done is:

  1. examine each line of the repeat chunk while the repeat block is being created,
  2. see if our special loop counter is present anywhere in the line,
  3. expand it and substitute,
  3. continue.

It will not help in your example code which involves jumping out of a loop. Unless we completely rewrite the way it works, and I see no need for that when there are perfectly workable solutions right now (just a matter of setting a variable after the RepeatEnd. Remember, using RepeatEnd 0 will skip the section completely and RepeatEnd 1 will basically skip repeating.

Am I missing a problem?
Logged

My online life: http://www.mellowood.ca
sciurius
Sr. Member
****
Posts: 443



« Reply #2 on: January 08, 2019, 06:02:23 PM »

Basically what's happening is that the repeat/end is being treated as special input and actually duplicated for later processing.

Yeah... That indeed makes it hard to implement these variables. It's a pity but not a disaster...

Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.037 seconds with 19 queries.