Documentation
Statements
output [expression]
input [variable]
input [variable] prompt [string]
set [variable] to [expression]
if [boolean_expression] then [statement_1] . . . [statement_n] endif
if [boolean_expression] then [statement_A1] . . . [statement_An] else [statement_B1] . . . [statement_Bn] endif
if [boolean_expression_A] then [statement_A1] . . . [statement_An] elseif [boolean_expression_B] then [statement_B1] . . . [statement_Bn] . . . else [statement_N1] . . . [statement_Nn] endif
while [boolean_expression] do [statement_1] . . . [statement_n] endwhile
Variables
A variable is a named area of memory that can hold one value. A variable name can be any sequence of letters {a-z, A-Z}, digits {0-9}, and the underscore {'_'}, subject to two restrictions:
- It cannot begin with a digit; and
- It cannot be a SPL keyword (a word reserved by SPL itself). The following eighteen keywords are not allowed:
and do else elseif endif endwhile false if input not or output prompt set then to true while
Expressions
An expression can be- a string, such as "Hello World!" or "3 + 4 = 7" or "The word 'and' is reserved"; or
- a number, such as 2, 14.9, -0.5, 4.1e8; or
- a boolean value - one of the words true or false
- a variable; or
- one or more of the above modified by an operator
Examples of Expressions (using output statement)
Statement(s) | What is Displayed |
---|---|
output "Hello World!" | Hello World! |
output 3 | 3 |
output 3 + 4 | 7 |
output "3 + 4" | 3 + 4 |
output "3 + 4 = " & 3 + 4 | 3 + 4 = 7 |
set x to 7
output x * 9 |
63 |
output 2 + 3 < 4 | false |
output 2 < 5 and 5 < 8 | true |
Operators
The SPL operators are shown below, listed from highest precedence to lowest. (All operators on the same row have equal precedence.)
Operator | Associativity | Name/Function |
---|---|---|
( ) | inner-to-outer | Parentheses |
^ | right-to-left | Exponentiation |
- | right | Unary Minus |
*, /, //, % | left-to-right | Multiplication, Division, Integer Division, Remainder |
+, - | left-to-right | Addition, Subtraction |
=, !=, <, <=, >, >= | none | Equal, Not Equal, Less Than, Less Than or Equal, Greater Than, Greater Than or Equal |
not | right | Negation |
and | left-to-right | Conjunction |
or | left-to-right | Disjunction |
& | left-to-right | String Concatenation |
Associativity indicates the execution order when an expression contains multiple operators with equal precedence. For example:
- Addition and subtraction have equal precedence. Since their associativity is left-to-right, the expression 5 - 2 + 3 means (5 - 2) + 3.
- Since the associativity of exponentiation is right-to-left, the expression 5 ^ 2 ^ 3 means 5 ^ (2 ^ 3).
Note on Integer Division (the "//" Operator)
When two numbers are divided using integer division, the result is the whole number part of the answer - i.e., the part to the left of the decimal point.
In other words, the result is truncated, not rounded.
Examples
Division | Result |
---|---|
6 // 3 | 2 |
5 // 3 | 1 |
4 // 3 | 1 |
3 // 3 | 1 |
2 // 3 | 0 |
Useful Tip: if a < b, then a // b = 0.
Comments
The hash sign '#' indicates a comment in SPL. Anything appearing after '#' until the end of the line will be ignored.
White Space
Ordinary spaces are required to separate SPL keywords from other tokens. All other white space, while usually helpful, is optional. So, spaces are not required to separate operators from their operands, line breaks are not required to separate statements, and no indentation is required to determine scope.
For example, the following two programs are equivalent (they both display the values of 2n - 1, for n from 1 to 12):
set n to 1 while n <= 12 do output 2 ^ n - 1 set n to n + 1 endwhile |
set n to 1 while n<=12 do output 2^n-1 set n to n+1 endwhile |