AxoCalculator implements several different programming languages. This document describes its implementation of the Pascal language. It is a simplified subset of true Pascal. Some of the strict requirements of Pascal have been relaxed to make programming AxoCalculator easier. Pascal requires that all variables be declared and that all statements end with a semicolon. It also defines the assignment operator as ' := '. These requirements make it laborious to perform simple calculations. For example, to set two variables, a and b, to 10 and 20 respectively, add them together, then output the result you would need to type the following using real Pascal :
var
a, b : Real;
begin
a := 10;
b := 20;
writeLn (a+b);
end;
In contrast, AxoCalculator's Pascal language does not require variables to be declared, ignores semicolons, accepts both ' = ' and ' := ' as assignment operators, and outputs the result of any expression which lacks an assignment operator. This means you only have to type :
a = 10
b = 20
a + b
AxoCalculator can execute both of the above programs. First choose "Settings…" under the "Calculator" menu, and make sure the programming language is set to Pascal. To execute one or more lines of code, select (highlight) them using the mouse, then press the "enter" key.
Strings
Character strings are enclosed in single quotes (e.g. 'This is a string'). To include a quote, tab or return in a character string, use
\' for a quote,
\t for a tab
\r or \n for a return.
String variables can be created in three ways. The simplest is to assign a string to a previously unused variable name. For example,
aString = "Hello world"
As an alternative, use the "NewString" procedure.
NewString (bString)
bString="abcde"
The third approach is to declare a string variable (Str255) at the start of a procedure or program (see "Variable Declaration" below).
Strings can be appended to one another using the "Concat" function.
NewString (cString)
cString = concat (aString, " ", bString)
Individual characters in a string can be accessed and manipulated. A string variable behaves like a 255 element array. Each element is one character. Accessing an element returns the ASCII code of that character. The length of the string is stored in the zero indexed element.
AxoCalculator programs can interact with the user via standard dialogs. This is done using two built in procedure calls, "Alert" and "PoseDialog". These procedures are described in the "Built in Procedures" document. "Alert" is used for simple messages or for returning results. "PoseDialog" is used for requesting one or more numerical values. An example program follows :
PoseDialog ('\r Calculate the volume and surface area of a sphere',
& 'Radius of sphere ',radius)
theSA = 4 * pi * radius ^ 2
theVolume = (4 / 3) * pi * radius ^ 3
Alert ('The volume and surface area of a \r sphere with radius ', radius,
& ' are : \r Volume = ',theVolume,
& '\r Surface area = ',theSA)
Conditional Branch
AxoCalculator supports the standard "if then else" statement for conditional branching. A program demonstrating this statement follows :
{ This program finds the square root of a number entered by the user }
PoseDialog ('\r Find the square root of a number', 'Enter the number', a)
if a < 0 then
Alert ('Can\'t calculate the square root of a negative number : ',a)
else
begin
b = sqrt (a)
Alert ('\r The square root of ',a,'is ',b)
end
Note : because the "else" condition required two lines to be executed,
these lines are bracketed between a "begin" and an "end" statement.
AxoCalculator's Pascal language relaxes the use of semi-colons after every statement. This makes writing simple programs easier, but can lead to ambiguities with "if then else" statements. Do not use the following styles,
A) "if then else" all on the same line
if (condition1) then action1 else action2
Instead use,
if (condition1) then action1
else action2
or,
if (condition1) then
action1
else
action2
B) "else if" extending over more than one line
if (condition1) then
action1
else if (condition2) then
action2
else
action3
Instead use,
if (condition1) then
action1
else
begin
if (condition2) then
action2
else
action3
end
Loop Commands
AxoCalculator supports several commands for executing a group of statements multiple times. These loop commands are "For", "While" and "Repeat". Executable programs demonstrating each of these commands follow.
Note : To interrupt a running program (for example to escape from
an infinite loop) press the "esc" key, or the Cmd-period
key combination.
• The "For" command
For i = 1 to 5 do
begin
a = i * 5
b = sin (a)
WriteLn ('sin ( ',a,') = ',b)
end
• The "While" command
a = 6
While a > 0 do
begin
a = a - 1
b = exp (a)
WriteLn ('exp ( ',a,') = ',b)
end
• The "Repeat Until" command
a = 0
b = 0
Repeat
a = a + 1
b = b + 2
c = b ^ a
WriteLn (b,'^ ',a,' = ',c)
Until a > 5
These loop commands can also be used to execute a single line expression multiple times. For example :
For i = 1 to 20 do write (i)
Variable Declaration
Variables created without being declared (as in the above examples) are always "Real" (i.e. floating point). To work with Integer, Boolean or Array variables, they must be declared in standard Pascal fashion. Variable declaration begins with the reserved word, "Var". For example :
Var
i : Integer
b : Boolean
r : Real
anArr : Array[1..50]
aStr: Str255
bStr: String
Each variable name is followed by a colon, then the type of the variable. For array variables, the size of the array is also specified. AxoCalculator only supports an array low bound of 1, and accepts array declarations without a low bound (e.g. Array[50] ). Array elements are always Real. Arrays can be created anywhere in a program using the "NewArray" procedure,
NewArray (anArray, arraySize)
Variables may be declared as either global (available to all programs and procedures) or local (available only to the currently active procedure). Global variables are preserved until they are explicitly unloaded. Local variables, which are declared within a procedure, are automatically unloaded when the procedure finishes executing. Local variables are declared immediately after a "Procedure", "Function" or "Program" declaration line (see below), and before the first "begin" statement. Global variables are declared outside (usually before) any "Procedure", "Function" or "Program" declarations. Examples of both local and global variable declaration can be found in the "Example Programs" document.
Procedures, Functions and Programs
AxoCalculator's programming language can be extended by loading user defined Procedures, Functions and Programs. These all have the same basic form :
• a declaration line which includes a name and lists any parameters
• an optional local variable declaration section
• a program section bracketed between "begin" and "end" statements.
Parameters are always passed by value.
Variable parameters are not supported.
Programs can not have any parameters.
A Procedure, Function or Program can be loaded by selecting it's text, then pressing "enter" or choosing "Load" under the "Calculator" menu. The only difference is that "Load" will not attempt to execute any lines of code outside the procedure declaration, and may therefore produce a clearer error message.
To run a Procedure, Function or Program, type in its name followed by any parameters. A Function may be executed as part of a numerical expression. Procedures and Functions may call other Procedures and Functions.
Declaring a Procedure
Here is an example of how to declare a simple Procedure with a single parameter. This procedure has no local variable declaration section. To load it, select the following 5 lines, and press "enter". To run it, type "CountTo(10)" then "enter".
procedure CountTo (Number)
begin
For j = 1 to Number do Write (j)
WriteLn
end
Declaring a Function
Here is an example of how do declare a simple Function with a single parameter. A function returns a numerical result, and the type of the result (Real, Integer or Boolean) is specified following the function's parameter list. Load this example function as above.
function Factorial (n) : Real
Var
f : Real
j : Integer
begin
f = 1
For j = 1 to n do f = f * j
Factorial = f
end
Here are two examples of how to use the "Factorial" function.
One practical use for functions is for unit conversion. Here is an example function which converts inches to centimeters.
function inchToCm (inch) : Real
begin
inchToCm = inch * 2.54
end
Note: A general unit conversion function is provided in the document
"Unit Conversions" in the "AxoCalculator AutoLoad" folder.
Adding a Program to the Calculator Menu
Here is an example of how do declare a simple program. Note that a program has no parameter list, and its name is followed by an optional character string. If present, this string is appended to the calculator menu when the program is loaded. If the second last character of the string is a slash ( / ) then the last character becomes a command key equivalent for running the program. Load the program as above. To run it, type "CountDown" then "enter", or select "Count Down to Lift Off" from the "Calculator" menu.
program CountDown 'Count Down to Lift Off/9'
begin
WriteLn
WriteLn ('Prepare for count down.')
FlushOutput
{ Pause }
For k = 1 to 50 do a = exp(2.0)
{ Start the count down }
j = 10
While j >= 0 do
begin
if (j = 3) then
WriteLn ('Ignition')
if (j <> 0) then
WriteLn (j)
else
WriteLn ('Lift Off !!')
FlushOutput
Beep
j = j - 1
{ Slow down the count }
For k = 1 to 20 do a = exp(2.0)
end
end
Auto-loading a Program
A useful program can be automatically loaded every time AxoCalculator is started up. To auto-load one or more programs, simply place them in the folder "AxoCalculator AutoLoad". This folder must be located in the same folder as the AxoCalculator program.