- TI-BASIC Control Flow Puzzle
- 07 Aug 2024 04:21:25 pm
- Last edited by iPhoenix on 30 Sep 2024 11:15:36 am; edited 4 times in total
Directory:
You may be aware that TI-BASIC's control flow is somewhat unique. Unlike many languages, which are pre-parsed to identify which blocks of code belong to each conditional, TI-BASIC handles control flow line-by-line, as the program is running. You're likely familiar with the fact that you shouldn't put Goto's in If-Then blocks because of a memory leak; this is because of the parser's efforts to keep track of control flow.
To implement the control flow that programmers are familiar with, many statements in TI-BASIC can jump to the nearest End at the same depth as the original statement: If-Then without Else, the Else statement itself, and For( and While if their loop condition is not met.
Because of incorrect assumptions in the TI-BASIC parser, it is possible for two different control flow statements to find the same End when looking for a jump destination. The puzzle is to construct a program where this actually happens. A further puzzle asks if it is possible for three or more different control flow statements to find the same End, etc. No illegal tokens, no crazy parser bugs, etc.
I think the easiest way to keep this puzzle spoiler-free is for you to DM me your solutions, and I'll evaluate them myself.
A final list of people who sent me submissions that passed my tests, in order of my receiving them:
I'll release my solution to this puzzle on 8/10 or after two correct submissions, whichever is later; feel free to discuss your observations in this thread.
A text file with the solution I will post has md5 hash 0a3c7e77d61ecb02cf2f4f8e22a12354.
- End-seeking weirdness: problem statement, problem solution
- Chaining simple Ifs: problem statement, no solution available yet.
You may be aware that TI-BASIC's control flow is somewhat unique. Unlike many languages, which are pre-parsed to identify which blocks of code belong to each conditional, TI-BASIC handles control flow line-by-line, as the program is running. You're likely familiar with the fact that you shouldn't put Goto's in If-Then blocks because of a memory leak; this is because of the parser's efforts to keep track of control flow.
To implement the control flow that programmers are familiar with, many statements in TI-BASIC can jump to the nearest End at the same depth as the original statement: If-Then without Else, the Else statement itself, and For( and While if their loop condition is not met.
Because of incorrect assumptions in the TI-BASIC parser, it is possible for two different control flow statements to find the same End when looking for a jump destination. The puzzle is to construct a program where this actually happens. A further puzzle asks if it is possible for three or more different control flow statements to find the same End, etc. No illegal tokens, no crazy parser bugs, etc.
I think the easiest way to keep this puzzle spoiler-free is for you to DM me your solutions, and I'll evaluate them myself.
A final list of people who sent me submissions that passed my tests, in order of my receiving them:
- Zeroko, using a different method than my solution.
- fghsgh, using the same technique as Zeroko.
- commandz, using the same technique as Zeroko.
- LogicalJoe, using the same method as my solution.
- pi644721, using the same method as my solution.
- Wavejumper3, using the same method as my solution.
- Zeroko and fghsgh devised a joint solution using the same method as my solution.
- Kerm, using the same method as my solution.
- Merth, using the same method as my solution but ??? TI-82 only???
- linkjt9, using the same method as my solution.
I'll release my solution to this puzzle on 8/10 or after two correct submissions, whichever is later; feel free to discuss your observations in this thread.
A text file with the solution I will post has md5 hash 0a3c7e77d61ecb02cf2f4f8e22a12354.