patternModerate
TI-BASIC interpreter for Fishstacks
Viewed 0 times
interpreterfishstacksforbasic
Problem
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
Also, in order not to push anything to the screen until something falls off the stack, I've had to initialize variables to
By convention, I've represented the
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->I
:-2->A
:A->B
:B->C
:C->D
:Input Str1
:While II
:sub(Str1,I,1)->Str2
:If Str2="I"
:A+1->A
:If Str2="D"
:A-1->A
:If Str2="S"
:A^2->A
:If Str2="P" and D>-1
:Then
:Disp D
:C->D
:B->C
:A->B
:0->A
:End
:If A=256 or A=-1
:0->A
:If A=-2
:Stop
:EndBy convention, I've represented the
STO→ character, → with ->Solution
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
Improvements
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 IL1
:0->L1(4
:End
:L1(4
:If Ans=256 or AnsL1(4
:EndImprovements
- Saved bytes by removing several unneeded ending parentheses and quotes
- Used
DelVarsince it does not need a following colon
- Used an array instead of four variables
- Used a complex expression
:(Str2="I")-(Str2="Dsaved toAnsto save space
- Used
ΔList(cumSum(L1->L1to getL1minus its first element
- Several others I'm not listing due to their minisculity; mainly using
Ansto save space and speed
Code Snippets
:ClrHome
:Disp "FISHSTACKS IDSP","INTERPRETER
:DelVar IInput Str1
:DelVar L1~{2,2,2,2→L1
:While I<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->L1
:0->L1(4
:End
:L1(4
:If Ans=256 or Ans<0
:0->L1(4
:EndContext
StackExchange Code Review Q#39431, answer score: 17
Revisions (0)
No revisions yet.