Execute Brain****/TI-89 BASIC: Difference between revisions

From Rosetta Code
Content added Content deleted
m (lang tags added)
m (Fixed syntax highlighting.)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}
{{implementation|Brainf***}}{{collection|RCBF}}


{{trans subpage|TI-83 BASIC}}
{{trans|TI-83 BASIC|subpage=1}}


This implementation (in [[TI-89 BASIC]]) is basically a direct translation of the [[RCBF/TI-83 BASIC|TI-83 BASIC example]]. It makes use of some TI-89 features; the program and memory size are given as parameters, and all variables are declared local.
This implementation (in [[TI-89 BASIC]]) is basically a direct translation of the [[RCBF/TI-83 BASIC|TI-83 BASIC example]]. It makes use of some TI-89 features; the program and memory size are given as parameters, and all variables are declared local.
Line 7: Line 7:
IO is performed with numbers; character IO could be done (using char() and ord() to convert) but the TI-89 has no cursor for user program IO, so it would be necessary to either implement one or buffer output until the program exits or waits for input, in order to display more than one character per line.
IO is performed with numbers; character IO could be done (using char() and ord() to convert) but the TI-89 has no cursor for user program IO, so it would be necessary to either implement one or buffer output until the program exits or waits for input, in order to display more than one character per line.


<lang ti89b>Define bf(Raw) = Prgm
<syntaxhighlight lang="ti89b">Define bf(Raw) = Prgm
Local valid, raw, BFprog, inst, memory, ip, brackets, memp
Local valid, raw, BFprog, inst, memory, ip, brackets, memp
"+-.,[]<>" → valid
"+-.,[]<>" → valid
Line 55: Line 55:
While ip≥0 and brackets ≠ 0
While ip≥0 and brackets ≠ 0
If mid(BFprog,ip,1) = "[" Then
If mid(BFprog,ip,1) = "[" Then
brackets-1 → brackets
ElseIf mid(BFprog,ip,1) = "]" Then
brackets+1 → brackets
brackets+1 → brackets
ElseIf mid(BFprog,ip,1) = "]" Then
brackets-1 → brackets
EndIf
EndIf
ip-1 → ip
ip-1 → ip
Line 73: Line 73:
EndIf
EndIf
EndFor
EndFor
EndPrgm</lang>
EndPrgm</syntaxhighlight>

Latest revision as of 12:04, 1 September 2022

Execute Brain****/TI-89 BASIC is an implementation of Brainf***. Other implementations of Brainf***.
Execute Brain****/TI-89 BASIC is part of RCBF. You may find other members of RCBF at Category:RCBF.
Translation of: TI-83 BASIC

This implementation (in TI-89 BASIC) is basically a direct translation of the TI-83 BASIC example. It makes use of some TI-89 features; the program and memory size are given as parameters, and all variables are declared local.

IO is performed with numbers; character IO could be done (using char() and ord() to convert) but the TI-89 has no cursor for user program IO, so it would be necessary to either implement one or buffer output until the program exits or waits for input, in order to display more than one character per line.

Define bf(Raw) = Prgm
  Local valid, raw, BFprog, inst, memory, ip, brackets, memp
  "+-.,[]<>" → valid
  "" → BFprog
  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} → memory
  0 → brackets
  1 → memp
  For ip, 1, dim(raw)
    If inString(valid,mid(raw,ip,1))>0 Then
      BFprog & mid(raw,ip,1) → BFprog
      If mid(raw,ip,1) = "["
        brackets+1 → brackets
      If mid(raw,ip,1) = "]"
        brackets-1 → brackets
    EndIf
  EndFor
  If brackets ≠ 0 Then
    Disp "Uneven brackets"
    Stop
  EndIf
  For ip, 2, dim(BFprog)
    mid(BFprog,ip,1) → inst
    If inst = "+" Then
      memory[memp]+1 → memory[memp]
    ElseIf inst = "-" Then
      memory[memp]-1 → memory[memp]
    ElseIf inst = "." Then
      Disp memory[memp]
    ElseIf inst = "," Then
      Input value
      value → memory[memp]
    ElseIf inst = "[" and memory[memp] = 0 Then
      1 → brackets
      ip+1 → ip
      While ip ≤ dim(BFprog) and brackets ≠ 0
        If mid(BFprog,ip,1) = "[" Then
          brackets+1 → brackets
        ElseIf mid(BFprog,ip,1) = "]" Then
          brackets-1 → brackets
        EndIf
        ip+1 → ip
      EndWhile
      ip-1 → ip
    ElseIf inst = "]" and memory[memp] ≠ 0 Then
      1 → brackets
      ip-1 → ip
      While ip≥0 and brackets ≠ 0
        If mid(BFprog,ip,1) = "[" Then
          brackets+1 → brackets
        ElseIf mid(BFprog,ip,1) = "]" Then
          brackets-1 → brackets
        EndIf
        ip-1 → ip
      EndWhile
    ElseIf inst = "<" Then
      memp-1 → memp
      If memp ≤ 0 Then
        Disp "Memory pointer out of range"
        Stop
      EndIf
    ElseIf inst = ">" Then
      memp+1 → memp
      If memp > dim(memory)
        0 → memory[memp]
    EndIf
  EndFor
EndPrgm