TI-BASIC

From Wikipedia, the free encyclopedia
Jump to: navigation, search
This article is about the calculator programming language. For the TI 99/4A home computer programming language, see TI BASIC (TI 99/4A).

TI-BASIC is the unofficial name of a BASIC-like language built into Texas Instruments (TI)'s graphing calculators, including the TI-83 series, TI-84 Plus series, TI-89 series, TI-92 series (including Voyage 200), TI-73, and TI-Nspire. TI rarely refers to the language by name, but the name TI-BASIC has been used in some developer documentation.[1][2]

For many applications, it is the most convenient way to program any TI calculator, since the capability to write programs in TI-BASIC is built-in. Assembly language (often referred to as "asm") can also be used, and C compilers exist for translation into assembly: TIGCC for Motorola 68000 (68k) based calculators, and SDCC for Zilog Z80 based calculators. However, both of them are cross-compilers, not allowing on-calculator programming. TI-BASIC is considerably slower than the assembly language (because it has to be interpreted), making it better suited to writing programs to quickly solve math problems or perform repetitive tasks, rather than programming games or graphics-intensive applications. Some math instruction books even provide programs in TI-BASIC (usually for the widespread variant used by the TI-82/83/84 series).

Although it is somewhat minimalist compared to programming languages used on computers, TI-BASIC is nonetheless an important factor in the programming community. Because TI graphing calculators are required for advanced mathematics classes in many high schools and universities, TI-BASIC often provides the first glimpse many students have into the world of programming.

Wikibooks

Wikibooks has textbooks on TI-BASIC programming:

Syntax[edit]

The syntax of all versions of TI-BASIC are somewhat different from typical BASIC implementations. The language itself has some basic structured programming capabilities, but makes limited to no use of or allowance for white space or indentation. It is also dependent on a somewhat non-standard character set, with specific characters for assignment (the right "STO" arrow, not readily available in most character sets), square and cube roots, and other mathematical symbols, as well as tokenized entry and storage for keywords. All statements begin with a colon, which also functions as a statement separator within lines. On the TI-83/84 models, closing parentheses, brackets, braces, and quotes can be omitted at the end of a line or before the STO token, although sometimes they are left on.

Expressions use infix notation, with standard operator precedence. Many statements include their arguments in parentheses, similar to the syntax used for mathematical functions. The assignment syntax is unusual; rather than using a let statement or an equals sign, TI-BASIC uses a right-arrow "STO" operator with the syntax: source → destination.

Control flow[edit]

Control flow statements include if-then-else blocks, for loops, while loops, and repeat loops, though no switch statements. Unusual for a high level language, TI-BASIC implementations include IS> (Increment and Skip if Greater Than) and DS< (Decrement and Skip if Less Than) statements, constructs generally associated with assembly languages. Sections of programs can be labeled; however, particularly on the Z80 models, the labels function primarily as destinations for GOTO statements rather than as program or block labels.

Availability of functions and subroutines depends on the implementation; the versions available on the TI-82-descended calculators do not even support a GOSUB-like function, though it is possible to call programs from within each other and share variables between programs. TI-89/92-based designs can have access to shared functions, essentially programs capable of returning a value.

Data types[edit]

TI-BASIC is a strongly and dynamically-typed language. Available data types differ considerably between the 68k and Z80 versions. It is not possible to create user-defined data types without using a library written in assembly. Lists are often used as a replacement for structs.

68k[edit]

  • Integers, which can store a large amount of data. The 68k calculators can store very large numbers, as high as 10^{600}, with perfect accuracy.
  • Real numbers, using decimal floating point. These store up to 14 significant digits depending on the calculator model.
  • Complex numbers, implemented as pairs of reals.
  • Strings
  • Lists, which are one-dimensional linked lists which support elementwise operations. On the 68k calculators, elements can be integers, reals, complex numbers, strings or expressions.
  • Matrices, with elements subject to the same restrictions in lists
  • Symbolic expressions, unique to the 68k series.

Data types that cannot be directly manipulated (typing only their name on a line would result in an error) include:

  • Pictures
  • Data
  • Programs
  • Functions

TI-83/84 (Z80)[edit]

  • Numerical variables, including A - Z and Θ (theta). These allow real numbers or complex numbers (implemented as pairs of reals) to be stored in floating point format. Values may range from 1E-99 to 1E99 with up to ten digits of accuracy. The limit of 27 variables, however, may be expanded through the use of lists or matrices, as a programmer can define each element in a list or matrix as a unique real variable and reference it accordingly.
  • Strings, including Str0 - Str9. These may store any number of characters or even function names, as long as there is available memory. They can be evaluated as an expression with the expr() function, which is found in the catalog.
  • Lists, including L1 - L6, with the ability to create additional ones. These are essentially one-dimensional arrays used to store a real or complex number into each of their elements. The dimension of a list, its number of elements, may range from 1 to 999, although available memory may be a limiting factor. When a list's dimension is set lower than it previously was, elements at the end are cut off. When set higher, extra elements at the end are filled with zeros. Dimensions are set by storing a valid number into the dim( of the list name. The default lists are named L1–L6, but custom lists can be created. This is done by setting dimension of a list referenced with the L token (accessible by pressing [2nd]+[LIST(STAT)], under the OPS menu as item B:), followed by a name ranging from 1 to 5 characters that begins with an uppercase letter (A–Z) or Θ. Individual elements of lists can be accessed by placing the element number in parentheses after the list name.
  • Matrices, including [A] - [J]. Their elements are subject to the same restrictions as lists. Their dimensions may be defined up to 99x99 elements, although, again, available memory will limit this. It is not possible to create user-defined matrices, so only the ten built in ones may be utilized.
  • Equation variables, including Y0 - Y9, r1 - r6, and u, v, w. These are essentially strings which store equations. They are evaluated to return a value when used in an expression or program. Specific values can be plugged in for the independent variable by following the equation name by the value in parentheses. (Y1(4) would return the value of Y1 at X=4)

Data types that cannot be directly manipulated (typing only their name on a line would result in an error) include:

  • Pictures (Pic0 - Pic9)
  • Programs (Prgm)
  • Graph databases (GDB0 - GDB9)

Examples:

:16→A        "Store the value of 16 into variable A."
:B+1→B       "Increment the relative value of B by 1."
:−1.3+4.9i→Θ "Store the value of negative 1.3 plus 4.9i into variable theta (Θ)."
:25→dim(L1)  "Set the dimension of list L1 to 25, so it has 25 elements."
:999→dim(L6) "Set the dimension of list L6 to its maximum of 999."
:0→dim(L2)   "Will return an error because zero is not a valid list dimension."
:Disp L1(4)     "Yields the value of the fourth element of List 1."
:15→dim(LMYLST) "Creates a new list named MYLST with 15 default elements (filled with zeros)."
:9→LMYLST(3)    "Stores the value of nine into the third element of list MYLST."
:Disp LMYLST(3) "Displays '9'."

Variables[edit]

Flexibility in the use of variables varies widely by the calculator model. The 68k calculators allow all variable names to have up to eight alphanumeric (including Greek) characters. Furthermore, variables can be grouped into "folders", or made local to a program (by declaring them with the Local statement). In contrast, on the TI-83, the programmer can create lists whose names are up to five characters. All other data types are limited, such as the 27 real or complex variables (A-Z and Θ), and a number of predefined variable names of other types (e.g., matrices have to be one of the ten variables [A]-[J]). Also, all variables are global.

Comments[edit]

The 68k calculators allow programs to include single-line comments, using © as a comment symbol. If a comment appears as the first line after the "Prgm" statement, it is displayed in the status bar when the program is selected in the catalog; such comments are often used to document the names or types of parameters.

Z80 programmers often start lines with " (double quotation mark) to denote a comment. Lines starting with " are actually executed, assigning the contents of the comment to the Ans variable, but this doesn't affect anything other than performance unless the Ans variable is read right afterwards.

Functions[edit]

The 68k version of TI-BASIC allows creating user-defined functions. Functions have the same syntax as programs except that they use the Func...EndFunc keywords instead of Prgm...EndPrgm, and that they are not allowed to use instructions that perform I/O, modify non-local variables, nor call programs. However, functions can still be non-pure because they can call built-in functions such as getTime(), getKey(), or rand().

All functions have a return value, which in the absence of an explicit Return statement is the last expression evaluated.

Third-party language extensions[edit]

Third-party applications, in chronological order Omnicalc,[3] xLIB,[4] Celtic, and Doors CS,[5] have overloaded TI-BASIC functions on the Z80 calculators to provide additional language functionality. The third-party libraries overload the sum(), real(), det() and identity() functions, which are handled and interpreted by their respective applications. Among the extra functions are fast shape-drawing routines, sprite and tilemap tools, program and VAT modification and access abilities, GUI construction features, and much more, most of which are ordinarily restricted to use by assembly programmers. All of the functions require that an application like Doors CS 7.0 be present on the user's calculator, sometimes considered a detraction to the use of the libraries.

Examples[edit]

These examples are slightly TI-83-series biased. For example, "ClrHome" would be "ClLCD" on the TI-85 and TI-86.

An entirely different command is required to clear the screen in the TI-89. Since output is generally displayed on the ProgramIO screen, the "ClrIO" command is required to clear the output screen. There exists a "ClrHome" command on the TI-89, and it performs its function - namely, clearing the Home screen. For programming purposes, however, this command is essentially useless.

Hello world[edit]

The following program, when executed, will display the phrase "HELLO, WORLD!":

Z80 Series

PROGRAM:HELLOWLD
:ClrHome
:Disp "HELLO, WORLD!"

68k Series

hellowld()
:Prgm
:  ClrIO
:  Disp "HELLO, WORLD!"
:EndPrgm

TI-Nspire

text"HELLO, WORLD!"

TI-Nspire CX

text "HELLO, WORLD!"

or

Disp "HELLO, WORLD"

Lists and loops[edit]

Lists[edit]

TI-82 Series Z80

PROGRAM:LISTS
:Input "NUMBER OF VARS? ",A
:If A<1 or fPart(A)
:Stop
:For(N,1,A)
:Input "VAR ",B
:B→L1(N)
:End
:L1

TI-85/86 Z80

PROGRAM:Lists
:Input "Number of variables? ",A
:If A<1 or fPart A
:Stop
:For(N,1,A)
:Input "Var ",B
:B→L1(N)
:End
:L1

68k Series

lists()
:Prgm
:  Local n,i,c
:  {}→list
:  Input "Number of variables?",n
:  For i,1,n
:    Input "Variable #"&string(i),c
:    c→list[i]
:  EndFor
:EndPrgm

In addition, lists are important for saving save game data and highscore data in programs. This is better than storing highscore or save game information as a variable, as variables are commonly changed during calculations performed by the user. Lists on the TI-82 cannot have custom names (L1 through L6 are preprogrammed). The TI-85 and TI-86 do not have the ability to handle a variable name with subscripts. The TI-81 is completely unable to handle lists.

Elaborations[edit]

Essentially, this program can be used to compile a list containing an arbitrary number of elements, which could then be implemented in a larger program that would break up the list and put each element to use. For instance, if we were to create a polynomial equation solver, we would use the technique noted above to compile all the coefficients into a list. Under the guidelines of the Rational Root Theorem, we would implement the first and last elements into a program to be factored and paired (and put into another list). To finish, we would create another While loop which would take the list with the factored elements, raise them to the appropriate power (this can be done by finding the "dim(L1)", subtracting one from it, and implementing another While loop which would subtract from the "dim(L1)" with intervals of 1), multiplying the result to the elements of the original list, adding the new results, and checking to see if the sum (of the new result) is equal to 0.

The "If A<1 or fPart(A)" statement checks to see if the variable A is a negative number, is zero or a non-integer. If so, the program is stopped by the Stop statement. If the condition is not true, then the program continues on to the rest of the code. The first variable is used to define L1. In the For loop, N is first set to 1, then the For loop will continue while N is less than or equal to A. After every iteration of the For loop, N will increase by 1. Every time the For loop is executed, an input is asked for, and the element is added to the end of the list, and saved. By this time, the list L1 should have a dim(L1) = A (the length of the list is A) and be ready for manipulation.

Counting[edit]

Z80 Series

PROGRAM:COUNTING
:ClrHome
:10^10→B
:For(A,1,B)
:Output(1,1,A)
:End

68k Series

counting()
:Prgm
:  Local a,b
:  10^10→b
:  For a,1,b
:    Disp a
:  EndFor
:EndPrgm

Explanation of Z80 counting code[edit]

This program defines the variable B, as (10^10). In the Z80 version, a "For loop" is established, which will store each successive number from 1 through B (10^10) in variable A.

The program then outputs the data currently stored in Variable A, to character space 1,1 in the screen (Top left corner).

A Clrhome refresh is not required, because each successive "A" variable which is outputted to character space (1,1) will replace the previous value on the screen which was there.

This loop will continue until the value of variable "A" is equivalent to the value of Variable "B", at which point the program will end.

Recursion[edit]

Recursion is possible. A program can be called from within itself or from within another program.

Z80 Series

PROGRAM:FACTUI
:"A user interface for FACT"
:Prompt X
:1→A:"An accumulator of sorts. The answer will be stored here."
:prgmFACT
PROGRAM:FACT
:If X=0
:Then
:Disp A
:Stop
:End
:XA→A
:X-1→X
:prgmFACT

68k Series

factui()
:Prgm
:  Local fact, n
:  Define fact(x)=Func
:    If x=0
:      Return 1
:    Return x*fact(x-1)
:  EndFunc
:  Input "Input a number", n
:  Disp "n! = "
:  Disp fact(n)
:EndPrgm

Functions[edit]

The 68k series makes a distinction between programs and functions. Functions are just like programs except that they do not allow statements with side effects (like Input or Disp, nor modifying non-local variables), and they return a value, which in the absence of an explicit Return statement is the last expression evaluated.

fact(x)
:Func
:  If x=0
:    Return 1
:  If x<0
:    Return undef
:  x*fact(x-1)
:End Func

Editors and Tools[edit]

The growth of the hobbyist graphing calculator community in the 1990s brought with it sharing and collaboration, including the need to share TI-BASIC code on mailing lists and discussion forums. At first, this was done by typing out the TI-BASIC code from a calculator screen into a computer by hand, or conversely, entering programs manually into calculators. TI-BASIC programs are stored in a tokenized format, they cannot be edited using standard computer text editors, so as the calculator programming community matured, a need for an automated converter arose. The format for computer-stored TI-BASIC programs generated by Texas Instruments' TI-GraphLink application was eventually decoded, and third-party tools were created to manipulate these files. TI created a BASIC editor that they included in certain releases of the TI-GraphLink linking program, but it has not gained widespread usage. In particular, it used a custom character set that did not display properly when copied and pasted to fora. In 2005, United-TI member Joe "Brazucs" Penna (now known as MysteryGuitarMan) created a translator in PHP that would convert text from the TI-GraphLink editor into standard Unicode, a project that soon expanded to include a regex-based TI-BASIC optimizer.[6]

Independently, Christopher "Kerm Martian" Mitchell of Cemetech began creating an online converter to extract plain-text (and later HTML and BBCode-formatted) contents from tokenized TI-BASIC programs,[7] which expanded to include an online program editor, exporter, and TI-83 Plus emulator.[8] The SourceCoder project absorbed OptiBASIC at the end of 2005. The only other major TI-BASIC editor currently in use is TokenIDE (or "Tokens"), created by Shaun "Merthsoft" McFall. An offline editor, Tokens can import, edit, and export TI-BASIC programs, includes tools to track program size and correctness, and offers ancillary features such as a sprite/image editor. Built around token definitions stored in XML files, it is intended to be extensible to work with any user-specified token mapping.[9]

See also[edit]

References[edit]

  1. ^ http://education.ti.com/educationportal/sites/US/productDetail/us_ti89ti.html?bid=5 TI-89 at education.ti.com
  2. ^ http://education.ti.com/nspire/scripting. Retrieved 2011-06-05.
  3. ^ ticalc.org. Omnicalc v1.00 Released
  4. ^ Evans, Travis. xLIB by Patrick Prendergast
  5. ^ Sutherland, Nikky. Find an Oasis in Doors CS 7.0
  6. ^ "Cemetech – United-TI Archives – Project Ideas/Start New Projects – opTI-BASIC [Topic]". Cemetech archive of UnitedTI. Retrieved 2012-08-05. 
  7. ^ "Michael Vincent". "Martian Develops SourceCoder; Conquers BASIClings". ticalc.org. Retrieved 2012-08-05. 
  8. ^ "Travis Evans". "Cemetech's SourceCoder Updated". ticalc.org. Retrieved 2012-08-05. 
  9. ^ "Ryan Boyd". "TokenIDE / Shaun McFall Interview". ticalc.org. Retrieved 2013-01-18. 

External links[edit]