3. Language Definition
Here we define the basic elements of the language.
3.1 Name
A name is a letter, followed by a series of letters and numbers.
Two names are the same if they consist of the same sequence of letters and numbers.
This comparison is not case sensitive.
Certain names are reserved for built in procedures, functions and operations.
For instance, a, hello, h2o are valid names. 2a, a_b are not.
a is equivalent to A.
3.2 Constants
There are three types of constants, integer, floating point and string.
Integer constants are simple numbers, like 2, 0, -73.
Floating point constants are similar, but are expressed with a decimal point e.g. 2.5, -27.3 etc.
They can also be written in exponential format e.g. 2.5e12 or -1e-10.
String constants are enclosed in either single or double quotes.
They may contain any characters between the quotes except, of course, the matching quote.
If you wish to put a " in a string constant, enclose the constant in single quotes '' and vice versa.
If you need to use both, you will have to split the constant in two, and add them together.
3.3 Variables
There are three types of variables, floating point, integer and string.
A floating point variable is expressed by a name.
An integer variable is expressed by a name followed imediately by a % sign.
A string variable is expressed by a name followed immediately by a $ sign.
Note that a, a% and a$ can all be used in the same program, and are distinct.
If a variable has not been defined when it is referenced, it will have the value 0 for an integer or floating variable, and "" for a string variable.
3.4 Arrays
Palm Basic supports floating point, integer and string arrays of arbitrary dimension.
An element of an array is expressed by a suitable variable name followed by a '(', the expressions giving the dimension offsets separated by commas then a ')'.
Dimension offsets are based from 1.
So a(1) is the first element of the one dimensional array of floating point values named a.
b$(7,4+4) is the (7,8)th element of the two dimensional array of strings names b.
Note that the array a is distinct from the variable a, and both can coexist in the same program.
3.5 Functions
Palm Basic contains a number of built in functions, like sin, cos, mid$ etc.
The syntax for a function is identical to that for an array.
For example, sin(1) will return the sine of 1, cos(a) will return the cosine of the value in variable a.
3.6 Expressions
Expressions consist of constants, variables, array elements or functions separated by operators.
Operators are evaluated according to the standard rules of precedence.
Round brackets '(' and ')' can be used to group subexpressions to override the standard evaluation order.
Examples of expressions are:
2+2, 2+a, 2+3*sin(x), (2+3)*sin(x), a$ + "fred"
3.7 Statements
There are four types of statement.
Label.
A name, by itself, is a label.
This can be used in a goto or a gosub to redirect program flow.
Assignment.
An assignment statement consists of a variable or array element followed by an equals sign and an expression.
For instance, a = 1 is an assignment, as is h$ = "hello", or x% = 73/12.
Procedure.
A procedurual statement is the name of a built in procedure, like print, goto or rem, optionally followed by the procedures arguments.
If there is more than one argument, they are typically separated by commas, except for the print and input statements, where there is a more sophisticated punctuation system.
Examples of procedural statements are print "hello" or drawline 10, 10, 100, 100 or cls.
Structural statements. There are three structural statements in Palm Basic. These are if...else...then, do...loop and for...to...step...next. We will cover these individually in the section called Structural Statements later in this chapter.
3.7 Lines
A line is zero or more statements separated by colons.
The following are valid lines:
a=3:print a
dim a$(30): a$(12) = "This is Palm Basic" : a$(30) = "This is the last string"
3.8 Programs
A program is a collection of lines entered into the editor.
Each statement on each line is executed in order, unless a goto, gosub or control statement is encountered.
These have the potential to move execution to another part of the program according to the rules laid down later.
A simple program might be:
for a = 1 to 10
print a, a*a
next a
3.9 Structural statements
As mentioned above, there are three basic structural statements in Palm Basic, though each of these can consist of more than one statement.
3.9.1 For...To...Step...Next loops
The for...next loop consists of two statements.
The first must be of the format:
for variable = expression to expression (step expression)
Note that the variable must be an integer or a floating point variable.
The second expression must be of the format:
next variable
The named variable is initialised to the first expression.
At each matching next statement, the variable is incremented by the amount of the step expression.
It is then compared to the to expression.
If the step expression is positive, and the resulting variable is greater than the to expression, the for loop is terminated and control proceeds to the statement after the next.
Note that the final value of the variable is not altered.
If the resulting variable is less than or equal to the to expression, execution resumes just after the for expression.
If the step expression is negative, the behavior is the same, but the comparisons are opposite.
Note that the step and to expressions are evaluated once at the start of the for loop, and are not reevaluated each time.
Also note that the loop will be executed at least once.
3.9.2 If...Else...Then statements
These statements always start with
if expression
There are two forms of the if...else...then statement.
Single line.
Here, the if expression is followed on the same line by optionally the keyword then, followed by one or more statements separated by colons.
These in turn may be followed by the keyword else, followed by one or more expressions separated by colons.
If the expression evaluates to a non-zero value, the first set of statements are executed.
If the expression evaluates to a zero value, the second set of expressions (if they exist) are executed.
Multi line.
If there is either nothing following the if statement, or only the keywork then, this statement is the start of a multi-line if statement.
If the expression evaluated to a non-zero value, control is passed to the next statement, and continues until an else is encountered.
Control is then suspended until the terminating endif statement is encountered.
If the expression evaluated to zero, statement execution is suspended until the matching else or endif is encountered.
3.9.3 Do...Loop statments
A loop statement passes control back to the matching do statement.
Note that either do or loop may be followed by while expression or until expression.
Control passes to the statement beyond the loop if the expression after a while evaluates to zero, or the expression after an until evaluates to non-zero.
For instance:
do
input "Enter a string, or nothing to stop "a$
print a$
loop while a$ <> ""
Note that control statements can be safely nested.
It is also possible to jump (with goto) out of control statements, either forward or backward.
It is not possible to jump into a control statement - this will cause an error when the ending control is encountered.