while loop and variables/constants
Variables(should actually call them constants) within the while loop should be scoped or it will have a random behaviour! This should at least be mentioned in the documentation, and it might be good to add a warning of sorts!
Look at the following code:
rules(lex-to-dr-Count := 0) ; number := 0 ; while( not(<eq> (number,<lex-to-dr-Count>)), <debug> "a";lex-to-dr-Count;debug ; x := <build-xeger> sort(s) ; <debug> "b" ; dr := <generate-terminals> x ; <debug> "c" ; newCount := <addi> (<lex-to-dr-Count>,1) ; <debug> "d" ; rules( Terminals :+ s -> dr lex-to-dr-Count:= newCount ) ; <debug> "e" )
Executing this a couple of times yielded results such as(,’s replacing newline):
a, 0, b, c, d, e, a, 1, a, 0, b, c, d, e, a, 1, b, a, 0, b, c, d, e, a, 1, b, a, 0, b …This is probably due to the fact that in stratego you can not rewrite a variable(constant). Normally this yields warnings, but it doesnt with a while loop, since the constant is only physically written down once, but it is very probable the assignment will occur more than once, otherwise you wouldnt use a while loop.
Scoping these variables solves the problem.
To avoid people from running head first into walls, it might be a good idea to either add a warning to the observer for this or at least add a small note in the stratego documentation of while and probably repeat aswell?
Submitted by André Vieira on 3 January 2012 at 16:18
Log in to post comments