For Next parsing bug on trans

Monkey Forums/Monkey Bug Reports/For Next parsing bug on trans

ziggy(Posted 2014) [#1]
This should not compile:
Function Main()
	For Local i:Int = 0 To 100
		Print i
	Next x
End

Because x is not defined. IMHO, If Next [variableName] is allowed, the variable name should be checked for correctness.


marksibly(Posted 2014) [#2]
I personally never use the 'Next var' form of next...does anyone these days?


rIKmAN(Posted 2014) [#3]
It's a little unrelated (though still with For...Next), but I discovered the other day that I couldn't pass a value to a method and have it used as the "Step" in a For...Next loop.
ie.
Method Test:Void(stepsize:Int)
    For Local a:= 0 to 100 Step stepsize
        DebugLog "a: "+a
    Next
End Method

I meant to make a thread and ask why not, but then I saw this and thought it was as good a place as any, sorry for the hijack Ziggy! :)

Anyone know why not?
I can't remember the error now, but I think it was "unresolved" something....or I may be completely making that up as it's late.


Danilo(Posted 2014) [#4]
I personally never use the 'Next var' form of next...does anyone these days?

Sometimes (optional), to make code more read-able. Especially with (long) nested loops, so everybody
immediately sees the "Next" is corresponding to i, j, x, y, or z.
Function Main:Int()
    For Local x:Int = 0 To 10
        For Local y:Int = 0 To 10
            For Local z:Int = 0 To 2
	        Print String(x)+":"+String(y)+":"+String(z)
    Next z Next y End For
End



marksibly(Posted 2014) [#5]
> I discovered the other day that I couldn't pass a value to a method and have it used as the "Step" in a For...Next loop.

It's really an efficiency thing - if step size were variable, loops would have to look something like:

var=from_value
Repeat
   If step_value>0              'if step_value is non-const, have to do this at runtime!
       If var>to_value Exit
   Else
       If var<to_value Exit
   Endif
   '
   'contents of loop
   '
   var+=step_value
Forever

It's different in 'C' etc where you explicitly supply the comparison function, whereas monkey (and b3d/bmx) implicitly derive the comparison from the sign of 'step_value'.

IMO, this is not nice code for a compiler to dump in your loops without warning just because you used a non-const step_value, so monkey doesn't.


Floyd(Posted 2014) [#6]
I personally never use the 'Next var' form of next...does anyone these days?


I've used that sort of thing occasionally, just for readability in complicated code. But it doesn't have to be part of the language. Comments work just as well.
	Next 'k
Next 'n


I just noticed that a code block won't indent the first line of code. Later ones are okay.


therevills(Posted 2014) [#7]
I personally never use the 'Next var' form of next...does anyone these days?


Not since the 80's/Early 90's :)


ziggy(Posted 2014) [#8]
I don't use it much, but if it's supported in the language, IMHO it should be well supported, or removed. Whatever you choose is ok, but leaving it wrong is not nice and causes confusion.


dawlane(Posted 2014) [#9]
I would have to side with ziggy. Either it has to be supported (or supported in strict mode?) or be flagged as a compile error. Though personal I do miss typing the extra identifier.


rIKmAN(Posted 2014) [#10]
IMO, this is not nice code for a compiler to dump in your loops without warning just because you used a non-const step_value, so monkey doesn't.


Ahhh I see, thanks for the info Mark.