Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Heh, the practice of reading language specs selectively. You're in good company, that has failed me more than once...

Am I missing something?

"... [Whenever] takes the program code and treats each line as an item in a to-do list. The interpreter chooses an item from the list at random, with equal probability, to execute, and executes the statement. ... When the interpreter is finished with a statement, it chooses another at random from the to-do list. ..."

This would mean the following could happen

1) all lines are placed on the todo list [1,2,3,4,5,6,7,8]

2) pick line 7 -> tests as false -> print "F", add line 2 on todo list, make sure line 4 is only once on todo list, add line 5 to todo list, remove line 7 from todo list [1,2,2,3,4,5,5,6,8]

3) pick line 2 -> tests as false -> print "A B", remove or make sure line 3 is only once on todo list (assume read is 0 to remove line 3 from todo list), make sure line 7 is only once on todo list [1,2,4,5,5,6,7,8]

4) pick line 1 -> print "statues" [2,4,5,5,6,7,8]

5) pick line 2 -> print "A B", remove or make sure line 3 is only once on todo list (assume read is 0 to remove line 3 from todo list), make sure line 7 is only once on todo list [4,5,5,6,7,8]

6) pick line 4 -> print "C" [5,5,6,7,8]

7) pick line 5 -> print "D E", assume read is 1 [5,6,7,8]

7) pick line 5 -> print "D E", assume read is 1 [6,7,8]

8) pick line 8 -> print "G" [6]

9) pick line 6 [6] ... endless loop

Output was "F A B A B C D E D E G"

Line 7 can't be the first to execute because its defer expression initially evaluates to true.

The longer version:

  • "In some cases, the statement will contain a clause that specifies that it cannot be executed until certain conditions apply. This results in the statement being deferred and placed back on the to-do list."
  • "The defer statement takes a boolean argument. If the argument is true, the statement is deferred - i.e. the remainder of the statement is not executed and the line number of the defer statement remains on the to-do list."
  • "The following standard [...] boolean operators work as expected: [...] &&, ||, !."
  • "An integer used in a boolean context evaluates to false if the line of that number is not currently on the to-do list and to true if it is on the to-do list at least once."

The initial to-do list includes all lines so all defer expressions evaluate to true:

  • Line 2: (T && T) --> T
  • Lines 4 and 8: (T || T) --> T
  • Line 5: ((T || !T) && T) --> T
  • Line 7: (T || !T) --> T

Therefore, the executable lines are {1,3,6}. Executing lines 3 and 6 any number of times doesn't modify the to-do list, so they can be ignored (except for determining halting). "Eventually", line 1 will be picked, executed and removed from the to-do list.

Once that is done, the defer exression of line 2 will be the only one that evaluates to false:

  • (1 && 7) --> F && T --> F

Therefore, the executable lines will be {2,3,6}, line 2 will ultimately get picked, and "A B" will be printed.

Then, depending on whether read() returns 1 or 0, we'll have a to-do list of either [3,4,5,6,7,8] or [4,5,6,7,8], the executable lines will be either {3,5,6} or {4,6}, and either "D E" or "C" will get printed (all respectively). And so on...