Running SMALL Programs
Like all scripting languages (awk, PERL, JavaScript, TCL, etc) SMALL is
interpreted. The interpreter can run interactively reading statements from
the user. The SMALL interpretter can also read and interpret a file with
extension or suffix ".sma". A SMALL program called p is always stored in a
file p.sma.
SMALL interpreters recognise the names of programs inside programs. When the name of a program is met they start to interpret the program's file. This is named a "call". The program name "calls" the program file. The call is in the "calling" program's file. When a called file is finished the interpreter returns to the calling program at the next statement after the call. One program can have any number of calls of any number of files.
The name of the interpreter should be "small". A command "small p" should
interpret file "p.sma". The command "small" with no argument executes the
interpreter in interactive mode. In a graphic environment it should be
possible to drag and drop a "p.sma" file into the icon for small interpreter
to get the same effect as "small p.sma". A double-click on a "p.sma" file
would also run the interpreter.
Programs
A SMALL program is a sequence of statements, commands, comments, and
separators.
Each statement and command in a program is interpreted in the order that they occur. Comments and separators are ignored.
A program or part of a program can either terminate or not terminate. If it terminates it either succeeds or fails:
Statements and commands can either succeed, fail, or not terminate. A program succeeds if all the statements and commands executed in the program succeed. A program fails if any statement or command fails. A program terminates when all executed statements and commands in the program have terminated. A program will not terminate if any executed statement or command in it does not terminate.
// This is a commentComments separate statements and commmands. They are ignored by the interpreter.
edit example
exitCommands communicate with the operating system in which the SMALL interpreter is embedded. An "exit" terminates the interpreter. An "edit p" command saves the current version of the program in the program file and invokes the users favorite editor -- stored in the environment variable EDIT -- to edit the file. SMALL takes over control when the editor exits. In essence SMALL calls the editor as a subprogram.
Statements
Statements do things they are either structured or an expression
followed by other things:
Structured statement
[ a < 0? -a -> b || a -> b ]Expresion statement
-a -> b
Structured statements allow a program to select alternatives and to repeat pieces of code. Expression statements allow the computation and disposition of values.
An expression statement combines testing, assignment and output into one simple construct.
Examples
i+1 -> i;
i > 0?
i - 1 -> i ?
"i =" i !
h/2.0 -> h !
1/i+s ->s
x*x^
2*3
1*3+3
3+3*1
1+2*2
1+2+3The interpretter evaluates expressions to give a value. Whitespace (ws) has no effect on the computation. Values can be strings, integers, or floating point numbers. Expressions are evaluated strictly from left to right. All the above examples give the value 6.
Here is a more precise definition of the semantics of an expression using two functions E and V.
For example
"abbbc" : "ab*c"is true(non-zero) but
"axbc" : "ab*c"is false (0).
The default operator indicates string concatenation:
"Time = " h ":" m "."!
Numbers are converted to decimal strings before taking part in string operations. Strings are converted to numbers before taking part in numerical operations.
123
a
$
abc
$->a;Read next line into variable a as a string.
$+$!Read two lines, convert to number, add them and print the result.
"Answer Y or N"! $="Y"?Prompt for a Yes or No response and test it.
"Answer y or n:"! $:"[yY].*"?A less stringent test for words like "yes", "Yes", "Yo", "Ya", "y", ...
"n=?"! $->nPrompt for a value for n.
abs
sqrt
quadraticA program call p fails if the file p.sma can not be found, or if the program inside the file fails. As an aid to debugging a program call outputs a warning: "p.sma not found" if the the file does not exist.
A call can fail, succeed, or not terminate according to what its
program does. A program call that succeeds returns a value. The file should
contain an expression whose value is disposed of by returning it ("^"). This
command also returns control. For example a program sqx might consist of a
single statement.
(sqx):
x*x^
V(+ s)= if V(s) is a number n then n else convert string to number.
The set of variables are part of the interpreter not a particular program. They are can be used and changed by any program that is executed. A called program gets its variables from the calling program. Any variables that are changed by the called program are also changed in the calling program.
For example suppose we wish to read a number and output its square. We can use sqx above:
$->x; sqx!Or, perhaps, if we have a program sqxy (in file sqxy.sma)
x*x -> y.then
$->x; sqxy; y!
ACapital and lower case letters are different variables. There are therefore 52 variables.
"This is a string"
"A"
"123"
123
123.0Note that 12. is not a valid float.
Examples of iterations and alternatives:
(Log):
0->l; {a>1.0? 1+l->l; a/2.0->a;}
[ a>b? a! || a<=b? b! ]
[ p? 0^ || 1^]
[ p? q? 1^ || ~p? ~q? 1^ || 0^]
[ p? [ q? 1^ || ~q? 0^] || 0^]
{i>1? [ i%2=0? i/2->i; || 3*i+1->i; ] }
{h+l->x; x/2->x; sqx->y; y-0.5<t? x->l; || y+0.5>t? x->h; }These structures have a syntax like guarded commands. The semantics are simpler.
Each alternative is tried, in turn, until one succeds or all have failed. The first alternative is tried first. If it completes without failing then the set of alternatives also complete successfully and the later one ones are ignored. If an alternative fails then control is passed to the start of next alternative. If all fail then the set of alternatives as a whole has failed. If any succeeds (even after several failures) the whole succeeds and the later ones are ignored.
At the failure or success of a set of alternatives, control passes to the end of the structure. If the structure is an iteration and if the alternativess succeeded then the whole structure is repeated. When all alternatives in an iteration fail then the iteration terminates successfully. Notice that an iteration can not fail. However an iteration can avoid terminating for ever. In a selection, the structure fails if all the alternatives fail and succeeds if any alternative succeeds.
Semantics
The object-oriented operational semantics expressed using the UML is TBD.
// This would be put in a file abs.sma
// It is given a number in variable e
// It returns the absolute value of e
[ e < 0 ? -e^
|| e^
]
// This would be put in file sqrt.sma
// Given a positive number in d
// sqrt leaves an approximation to its sqrt in the same variable
// Uses variables x,y, e, and f
// calls abs above
d/2 -> x
{ x*x-d -> e; abs>0.00001?
2*d ->f; x/2.0 ->y; d/f+y ->x;
vv }
x ->d
//This would be in a file quadratic.sma
//'small quadratic' then reads three lines containing a number each
//and solves a quadratic equation ax*x+bx+c=0
// Uses variables a,b,c,d,e,f,s,t,x,y
// Calls sqrt and indirectly abs
//read a,b, and c
$->a; $->b; $->c;
// calculate the denominator t
2*a->t
[ t=0? "This is a linear equation"!
|| t<>0?// a<>0.
// find the discriminant d
2*t*c->s; b*b-s->d;
// select the correct case depending on the discriminant d
[ d>0? sqrt; "Real roots: " -b+d/t " and " -b-d/t !
||d=0? "Equal roots: " -b/t!
||d<0? -d ->d; sqrt; "Complex roots: " -b/t "+/- i*" d/t!
]
]
. . . . . . . . . ( end of section Examples) <<Contents | End>>
Notations
. . . . . . . . . ( end of section Small) <<Contents | End>>