Chapter Two
determine if a one-byte token is a variable. Because routines to
recognize Atari BASIC'so one-byte tokens take so much less
machine language, they execute relatively quickly.
The 256 possible tokens are divided into logical numerical
groups that also make them simpler to deal with in assembly
language. For example, any token whose value is 128 ($80) or
greater represents a variable name. The logical grouping of the
token values also means faster execution speeds, since, in
effect, the computer only has to check bit 7 to recognize a
variable.
The numerical grouping of the tokens is shown below:
Token Value (Hex) Description
00-0D Unused
0E Floating Point Numeric Constant.
The next six bytes will hold its value.
0F String Constant.
The next byte is the string length.
A string of that length follows.
10-3C Operators.
See table starting at $A7E3 for specific
operators and values.
39-54 Functions.
See table starting at $A820 for specific
functions and values.
55-7F Unused.
80-FF Variables.
In addition to the tokens listed above, there is another set
of single-byte tokens, the Statement Name Tokens. Every
statement in BASIC starts with a unique statement name, such
as LET, PRINT, and POKE. (An assignment statement such as
"A = B + C," without the word LET, is considered to begin with
an implied LET.) Each of these unique statement names is
represented by a unique Statement Name Token.
The Program Executor does not confuse Statement Name
Tokens with the other tokens because the Statement Name
Tokens are always located in the same place in every statement
- at the beginning. The Statement Name Token value is
derived from its entry number, starting with zero, in the
Statement Name Table at $A4AF.
Chapter Two
Tables
A table is a systematic arrangement of data or inforrnation.
Tables in Atari BASIC fall into two distinct types: tables that are
part of the Atari BASIC ROM and tables that Atari BASIC
builds in the user RAM area.
ROM Tables
The following is a brief description of the various tables in the
Atari BASIC ROM. The detailed use of these tables will be
explained in subsequent chapters.
Statement Name Table ($A4AF). The first two bytes in each
entry point to the information in the Statement Syntax Table
for this statement. The rest of the entry is the name of the
statement name in ATASCII. Since name lengths vary, the last
character of the statement name has the most signiflcant bit
turned on to indicate the end of the entry. The value of the
Statement Name Token is derived from the relative (from zero)
entry number of the statement name in this table.
Statement Execution Table ($AA00). Each entry in this table
is the two-byte address of the 6502 machine language code
which will simulate the execution of the statement. This table is
organized with the statements in the same order as the
statements in the Statement Name Table. Therefore, the
Statement Name Token can be used as an index to this table.
Operator Name Table ($A7E3). Each entry comprises the
ATASCII text of an Operator Symbol. The last character of each
entry has the most significant bit turned on to indicate the end
of the entry. The relative (from zero) entry number, plus 16
($10), is the value of the token for that entry. Each of the entries
is also given a label whose value is the value of the token for
that symbol. For example, the ";" symbol at $A7E8 is the fifth
(from zero) entry in the table. The label for the ";" token is
CSC, and the value of CSC is $15, or 21 decimal (1l6+5).
Operator Execution Table ($AA70). Each two-byte entry
points to the address, minus one, of the routine which
simulates the execution of an operator. The token value, minus
16, is used to access the entries in this table during execution
time: The entries in this table are in the same order as in the
Operator Name Table.
Operator Precedence Table ($AC3F). Each entry
represents the relative execution precedence of an individual
operator. The table entries are accessed by the operator tokens,
Chapter Two
minus 16. Entries correspond with the entries in the Operator
Name Table. (See Chapter 7.)
Statement Syntax Table ($A60D). Entries in this table are
used in the process of translating the source program to tokens.
The address pointer in the first part of each entry in the
Statement Name Table is used to access the specific syntax
information for that statement in this table. (See Chapter 5.)
RAM Tables
The tables that BASIC builds in the user RAM area will be
explained in detail in Chapter 3. The following is a brief
description of these tables:
Variable Name Table. Each entry contains the source
ATASCII text for the corresponding user variable symbol in the
program. The relative (from zero) entry number of each entry
in this table, plus 128, becomes the value of the token
representing the variable.
Variable Value Table. Each entry either contains or points
to the current value of a variable. The entries are accessed by
the token value, minus 128.
Statement Table. Each entry is one tokenized BASIC
program line. The tokenized lines are kept in this table in
ascending numerical order by line number.
Array/String Table. This table contains the current values
for all strings and numerical arrays. The location of the specific
values for each string and/or array variable is accessed from
information in the Variable Value Table.
Runtime Stack. This is the LIFO Runtime Stack, used to
control the execution of GOSUB/RETURN and similar
statements.
Pre-compiler
Atari BASIC translates the BASIC source lines from text to
tokens as soon as they are entered. To do this, Atari BASIC
must recognize the symbols of the BASIC Language. BASIC
also requires that its symbols be combined in certain specific
patterns. If the symbols don't follow the required patterns,
then Atari BASIC cannot translate the line. The process of
checking a source line for the required symbol patterns is called
syntax checking.
BASIC performs syntax checking as part of the tokenizing
process. When the Program Editor receives a completed line of
Chapter Two
input, the editor hands the line to the syntax routine, which
examines the first word of the line for a statement name. If a
valid statement name is not found, then the line is assumed to
be an implied LET statement.
The grammatical rules for each statement are contained in
the Statement Syntax Table. A special section of code examines
the symbols in the source line, under the dfrection of the
grammatical rules set forth in the Statement Syntax Table. If
the source line does not conform to the rules, then it is reported
back as an error. Otherwise, the line is translated to tokens.
The result of this process is returned to the Program Editor for
further processing.
Program Editor
When Atari BASIC is not executing statements, it is in the edit
mode. When the user enters a source line and hits return, the
editor accepts the line into a line buffer, where it is examined
by the pre-compiler. The pre-compiler returns either tokens or
an error text line.
If the line started with a line number, the editor inserts the
tokenized line into the Statement Table. If the Statement Table
already contains a line with the same line number, then the old
line is removed from the Statement Table. The new line is then
inserted just after the statement with the next lower line
number and just before the statement with the next higher line
number.
If the line has no line number, the editor inserts the line at
the end of the Statement Table. It then passes control to the
Program Executor, which will carry out the statement(s) in the
line at the end of the Statement Table.
Program Executor
The Program Executor has a pointer to the statement that it is to
execute. When control is passed to the executor, the pointer
points to the direct (command) line at the end of the statement
table. If that statement causes some other line to be executed
(RUN, GOTO, GOSUB, etc.), the pointer is changed to the
new line. Lines continue to be executed as long as nothing
stops that execution (END, STOP, error, etc.). When the
program execution is stopped, the Program Executor returns
control to the editor.
Chapter Two
When a statement is to be executed, the Statement Name
Token (the first code in the statement) directs the interpreter to
the specific code that executes that statement. For instance, if
that token represents the PRINT statement, the PRINT
execution code is called. The execution code for each statement
then examines the other tokens and simulates their operations.
Execute Expression
Arithmetic and logical expressions (A+B, C/D+E, F<G, etc.)
are simulated with the Execute Expression code. Expression
operators (+,-,*,etc.) have execution precedence - some
operators must be executed before some others. The
expression 1 + 3*4 has a value of 13 rather than 16
because * had a higher precedence than + . To properly
simulate expressions, BASIC rearranges the expression with
higher precedence first.
BASIC uses two temporary storage areas to hold parts of
the rearranged expression. One temporary storage area, the
Argument Stack, holds arguments - values consisting of
constants, variables, and temporary values resulting from
previous operator simulations. The other temporary storage
area, the Operator Stack, holds operators. Both temporary
storage areas are managed as Last-In/First-Out (LIFO) stacks.
LIFO Stacks
A LIFO (Last In/First Out) stack operates on the principle that
the last object placed in the stack storage area will be the first
object removed from it. If the letters A, B, C, and D, in that
order, were placed in a LIFO stack, then D would be the first
letter removed, followed by C, B, and A. The operations
required to rearrange the expression using these stacks will be
explained in Chapter 7
BASIC also uses another LIFO stack, the Runtime Stack, in
the simulation of statements such as GOSUB and FOR.
GOSUB requires that BASIC remember where in the statement
table the GOSUB was located so it will return to the right spot
when RETURN is executed. If more than one GOSUB is
executed before a RETURN, BASIC returns to the statement
after the most recent GOSUB.
<-Chapter 01Chapter 03->