TI-Basic


TI-Basic is the name unofficial of the programming language of Texas Instruments graphing calculators . This term is never used by Texas Instruments in official documents. By its philosophy and its abilities, this language comes closer to the BASIC , hence its name. However, its syntax is substantially different from that of BASIC . The TI-Basic calculators based on the Motorola 68000 (“68K”) is not the same as the calculators based on the Z80 . Each model has special features that can be used by programs, but the core of the language is common.

Depending on the model of the calculator, the instructions can be either chosen from menus or typed on an alphanumeric keypad. In the first case, no lexemization phase is necessary, and the program is stored directly in a bytecode form . In the second case, a lexemisation is carried out during the first execution which follows a modification. In all cases, the interpreter itself works on the bytecode .

Brief description of the language

Data Types

The usable variables are those that can be accessed from the calculation screens. For example, in the case of TI-82 :

  • Floating-point numbers ;
  • matrices ;
  • Lists of numbers;
  • Graph functions;
  • System variables;
  • The data tables.

The TI-82 does not allow the storage and manipulation of other types of data, such as strings. It is only possible to display strings. String variables have appeared on newer models.

The TI-Basic is a strong typing language , considering that it is not possible to modify or choose the type of values ​​of the variables. The variables are global and do not need to be declared before being used in an expression.

Syntax

On Z80-based calculators, programs are not entered in alphanumeric form but via the calculator menus. Letters always represent numerical variables, the product being implicit. Thus, the letters “FOR” typed one after the other do not indicate the beginning of a loop but the products of variables F, O and R. An expression ends with a double-point.

The control and loop commands must be terminated using the End keyword , which serves as the end-of-block marker (in the same way as the bracket in the C family languages ).

As with other errors, syntax errors cause the program to stop with an error message.

Controls

The TI-Basic supports the basic concepts of control, namely:

  • The assignment with the arrow to the right (→): the expression to the left of the arrow is evaluated, and the result is stored in the variable to the right;
  • The conditional branching instructions If , Then , Else , EndIf ;
  • The loops : While (Z80), Repeat (Z80) or Loop (68K), For ;
  • The connections, with the instruction Goto and the labels designated by Lbl . In addition, a Menu (Z80) or ToolBar (68K) command displays a menu of options, each of which makes a connection to a specified label.

Inputs / Outputs

The input / output instructions are:

  • Input and Prompt , in order to ask the user to type a value to be assigned to a variable;
  • Output , to display a variable or string at a location on the screen, unlike the following;
  • Disp , which only allows to output a variable in the stream of the main screen;
  • DispGraph , DispTable , which display the current graph or table;
  • Input also retrieves the cursor coordinates displayed on the screen;
  • ClrHome , ClrTable , which clears the main screen or the current table;
  • PrintScreen (z80), prints the current screen on an external device if connected;
  • Get and Send to which are added GetCalc and SendCalc on 68K and SendChat for new 68K; They export or import a variable from an external device (another machine);
  • GetKey finally gets the code ( scancode ) corresponding to the last key pressed.

Creating functions and programs

In calculators based on the Motorola 68000, a function or program can be created directly using the Define function , its syntax is:

Define my_function (v1, .., vN) = Func: function ...: EndFunc

This can be used within another function and thus implements local functions and programs.

In calculators based on the Z80, this is impossible. The only possibility is to call other programs recorded on the machine, and these do not take arguments: any change of value is made via the global variables.

A possible strategy to simulate the function call, taking one or more arguments, and returning one or more values ​​is as follows: given a PrgmA program, we want to call a PrgmB program, we can write:

: {I, X, N} # stored in Ans
: PrgmB
: Ans → {I, X, N, Y}

Ans Represents the value of the last calculated expression.

The PrgmB program can then have the following form:

: Years (1) → I
: Ans (2) → X
: Years (3) → N
: X → Y
: For (I, 1, N)
: X + cos (I * (I + 1)) → X
: End
: {I, X, N, Y}

However, the value of this method is limited, given that all variables are global.

Miscellaneous

The numerous mathematical functions of the various machines are available in the instructions.

It is possible to exchange programs by connecting two calculators by cable, or by connecting the calculator to a computer; This also makes it possible to use a computer emulator in order to facilitate the typing and the development of a program.

Examples of programs

Hello world

The Disp keyword displays the traditional Hello world on the screen:

: Disp "HELLO, WORLD!"

But you can also use another function, which will display hello world at the coordinates defined by X and Y (coordinates in pixels for TI-89 or higher, inline / character columns for the oldest):

: Output (Y, X, "HELLO WORLD")

Note: Since Ti-Basic programs only perform one instruction per line, the two previous examples are simplified respectively by:

: Disp "HELLO, WORLD!
: Output (Y, X, "HELLO WORLD

It is important to clarify that the last line is not an error: the TI-Basic interpreter accepts that commands are not perfectly parenthesized; And so it is a common optimization that do not write the quotation marks and parentheses at the end of the line.

The only known weakness of this optimization is when a command ends with a quotation mark, omitted, and the next line starts with a Lbl [0-9] .

Stack or face play

This program is a game that simulates several times the throw of a piece. It falls either on the battery side or on the face side. It shows how to use the random integer generation function, in other words randInt(. Note that this program is the root of the game, and that it is then possible to add features.

: D Lbl Label D t the beginning of the program.
: 0 → N Initialization of variable N (STO key → For →)
: Menu ("BATTERY OR FACE", "BATTERY", A1, "FACE", A2, "QUIT", Q Preprogrammed menu.
: Lbl A1 Choice label PILE
: 1 → J The variable J is set to 1. The player has chosen PILE
: Goto B Go to label B
: Lbl A2 Selection label FACE
: 0 → J The variable J takes the value 0. The player chose FACE
: Lbl B Label B
: randInt (0,1) → N The variable N takes the value of an integer (0,1) between 0 and 1.
: If N = J If the random number is equal to the number chosen by the player.
: Then Then ...
: Disp "WIN! Show WINS!
: Else Otherwise ...
: Disp "LOST! Show LOST!
: End End Loop Si
: Pause Maintain the displayed result.
: D Go to Goto label D t the beginning of the program.
: Lbl Q Label Q uit the game.
: Output (1,1, "Removes the Done when the program is stopped.

NB: If the order Menu(is not available, it is possible to use:

: Disp "0. BATTERY
: Disp "1. FACE
: Input "YOUR CHOICE", J

Find the divisors of a number (in ℕ)

This program finds the divisors of a number. It shows several essential structures of TI-Basic programming. It stores these divisors in an L1 list that it displays at the end of execution.

: 0 → P Initialization of the variable
: Input "N =", N Requires the number whose divisors are to be found
: ClrList L1 Delete list L1 (if existing)
: For I, 1, N Beginning of the loop For : for I varying from 1 to N
: N / I → A Divides the number chosen initially by the Input by a possible divisor
: If fPart (A) = 0 If the decimal part of A is zero ...
: Then ... Then ...
: P + 1 → P ... One increments the variable ...
: I → L1 (P) ... And we store the divisor in L1
: End End of the loop If
: End End of the loop For
: Pause L1 End the program by pausing the values ​​of L1 (the dividers of N), press ENTER

NB: It is useless to test all the numbers from 1 to N, we can content ourselves with going to N / 2, or better {\ Displaystyle {\ sqrt {N}}}, (We must not forget to put N, which is a divisor, in the list)

Find the real roots of a trinomial function

This program makes it possible to find the real roots of a function of the second degree. It performs the calculation of the discriminant, and according to it it calculates the possible roots.

: Lbl 1  To be put only if you write the loop enter / cancel (see end program)
: EffErr Clears the screen
: Disp "FORM AX² + BX + C" The user is prompted for the letters A, B and C requested later
: Prompt A, B, C Same as: Input "A =", A but less gourmand in characters therefore in memory; Request A, B and C
: B²-4AC → D Calculation of the discriminant Δ (Delta)
: Disp "DELTA =", D The user is informed of what the discriminant is worth
: If D <0 If D is less than 0, then ...
: Disp "NO ROOTS" Written ...
: If D> 0: Then If D is greater than 0, then ...
: Disp "THERE ARE 2 ROOTS" Written ...
: ((- B + √ (D)) / (2A) → E Computation and storage of the first root
: ((- B-√ (D)) / (2A) → F Calculating and storing the second root
: Disp E, F The two roots
: End End of condition
: If D = 0: Then If D is equal to 0, then ...
: Disp "THERE A 1 ROOT" Written ...
: (- B) / (2A) → G Root computation and storage
: Disp G Show root
: End End of condition
Optional (in italics): pressing "canceled" to exit the program, on "enter" to start the 
: Repeat K = 105  As long as the user does not press enter ... 
: KeyCode K →  K variable Is assimilated to the key code 
: If K = 45: Goto 2  Pressing the cancel key sends to Lbl 2 
: End  End of loop 
: Goto 1  Return to start 
: Lbl 2  After pressing "Cancel" 
: EffEr  Delete screen 
: Output (1,1, "  Avoids" Done "on the screen