TI-BASIC interpreter for Fishstacks Fishstacks is a deadfish derivative based on a stack the stack can only hold four elements when a fifth element is pushed the bottom element is kicked out and eventually printed out to the screen.

I've come up with an interpreter in TI-BASIC for my calculator. However, I fell slightly guilty about using all four variables ABCD. I wonder if there could be a better way? Maybe with lists (arrays). I've just started learning about them, so I don't feel confident switching over yet. Any guidance would be appreciated.

Also, in order not to push anything to the screen until something falls off the stack, I've had to initialize variables to -2. This is the main reason I have not converted to arrays yet; I don't see how they could address this issue. (maybe even throw a DOMAIN error).

:ClrHome
:Disp "FISHSTACKS IDSP","INTERPRETER"
:0-&gt;I
:-2-&gt;A
:A-&gt;B
:B-&gt;C
:C-&gt;D
:Input Str1
:While I&lt;length(Str1)
:I+1-&gt;I
:sub(Str1,I,1)-&gt;Str2
:If Str2="I"
:A+1-&gt;A
:If Str2="D"
:A-1-&gt;A
:If Str2="S"
:A^2-&gt;A
:If Str2="P" and D&gt;-1
:Then
:Disp D
:C-&gt;D
:B-&gt;C
:A-&gt;B
:0-&gt;A
:End
:If A=256 or A=-1
:0-&gt;A
:If A=-2
:Stop
:End


By convention, I've represented the STO→ character, → with -&gt;
I was asked by @Timtech to join + post an improvement. Using arrays is a lot easier to understand, and there were so many optimizations that I decided to completely re-write the code. I'm sure it could be improved by other advanced programmers like me, as I'm using several long expressions here... note that L1 represents list #1 (2nd + 1 on the calculator) and ~ is the negative token.

:ClrHome
:Disp "FISHSTACKS IDSP","INTERPRETER
:DelVar IInput Str1
:DelVar L1~{2,2,2,2→L1
:While I&lt;length(Str1
:I+1→I
:sub(Str1,I,1→Str2
:(Str2="I")-(Str2="D
:Ans+L1(4→L1(4
:If Str2="S
:L1(4)²→L1(4
:If Str2="P
:Then
:L1(1
:If 2+Ans
:Disp Ans
:ΔList(cumSum(L1-&gt;L1
:0-&gt;L1(4
:End
:L1(4
:If Ans=256 or Ans&lt;0
:0-&gt;L1(4
:End


Improvements


Saved bytes by removing several unneeded ending parentheses and quotes
Used DelVar since it does not need a following colon
Used an array instead of four variables
Used a complex expression :(Str2="I")-(Str2="D saved to Ans to save space
Used ΔList(cumSum(L1-&gt;L1 to get L1 minus its first element
Several others I'm not listing due to their minisculity; mainly using Ans to save space and speed