Appearance
Pascal Syntax
Overview
begin .. endconstructorprocedureandfunctiondeclarationsif .. then .. elseconstructorfor .. to .. do .. stepconstructorwhile .. doconstructorrepeat .. untilconstructortry .. exceptandtry .. finallyblockscasestatements[1,2,3]array constructors^,*,/,and,+,-,or,<>,>=,<=,=,>,<,div,mod,xor,shl,shroperatorsexitstatementObjectName.Propertyaccess to object properties and methods
Script structure
The script structure consists of two major blocks:
- Procedure and function declarations
- Main Block
Both are optional, but at least one should be present in script. There is no need for the main block to be inside begin .. end. It can be a single statement.
Example 1:
pascal
procedure DoSomething;
begin
CallSomething;
end;
begin
CallSomethingElse;
endExample 2:
pascal
begin
CallSomethingElse;
endExample 3:
pascal
function MyFunction;
begin
Result := 'OK';
end;Identifiers
Identifier names (variable names, function and procedure names, etc.) follow the most common rules in Pascal. They should begin with a character (a..z or A..Z), or _, and can be followed by alphanumeric chars or underscore (_). They cannot contain any other character or whitespace.
Valid identifiers:
pascal
VarName
_Some
V1A2
_____Some____Invalid identifiers:
pascal
2Var
My Name
Some-more
This,is,not,validAssign statements
Just like in Pascal, assign statements (assign a value or expression result to a variable or object property) are built using :=.
Example:
pascal
MyVar := 2;
MyNumber := 5.678
MyText := 'This' + ' is a text.';Floats
Float values use a point (.) as decimal separator and no thousand separator.
Example:
pascal
A := 1234.56;Strings
Strings (sequence of characters) are declared in Pascal using single quotes ('). Double quotes (") are not used. In addition, #nn can be used to declare a character via its ascii code nn inside a string. In that case, there is no need to use the + operator to add the character to a string.
Example:
pascal
A := 'This is a text';
B := 'Text' + ' concat';
C := 'String with CR and LF char at the end'#13#10;
D := 'String with '#33#34' characters in the middle';Comments
Comments can be inserted inside a script via // chars for single line comments or (* *) or { } for block comments.
Example:
pascal
// This is a comment
A := 10;
(* This is another comment *)
A := 20;
{ And this is a comment
with two lines }
A := 30;Variables
There is no need to declare variable types in a script. Thus, variables can be declared by just using the var directive and its name. There is no need to declare variables. In this case, variables are implicitly declared.
Example 1:
pascal
procedure SetText;
var
S;
begin
S := 'Hello world!';
end;Example 2:
pascal
var
A;
begin
A := 0;
A := A + 1;
end;Example 3:
pascal
var
S: string;
begin
S := 'Hello World!';
end;Array Type
Even though a variable type is not required and is ignored in most cases, there are some special types that have a meaning. For example, when declaring a variable as an array then the variable will be automatically initialized as a variant array of that type, instead of null.
Example:
pascal
var
Arr: array[0..10] of string;
begin
Arr[0] := 'First';
Arr[1] := 'Second';
endThe type of array items and the lower index are optional. The following declarations are all valid and result in the same array type:
pascal
var
Arr1: array[0..10] of string;
Arr2: array[10] of string;
Arr3: array[0..10];
Arr4: array[10];Script arrays are always 0 based and indicating a different number for the lower bound will cause a compilation error.
pascal
var
Arr: array[1..10] of string; // Invalid declarationIndices
Strings, arrays and array properties can be indexed using square brackets. For example, if Str is a string variable, the expression Str[3] returns the third character in the string denoted by Str, while Str[I + 1] returns the character immediately after the one indexed by I.
Example:
pascal
MyChar := MyStr[2];
MyStr[1] := 'A';
MyArray[1,2] := 1234;
Strings1.Line[2] := 'Line in Strings1 object';Arrays
To construct an array, square brackets can be used. and multi-index arrays can be constructed by nesting array constructors. The multi-index array can then be accessed by separating indexes using a comma (,). If a variable is a variant array, the script automatically supports indexing in that variable. Array indexes are 0 based.
Example:
pascal
NewArray := [2,4,6,8];
Num := NewArray[1]; // Num receives "4"
MultiArray := [['green','red','blue'],['apple','orange','lemon']];
Str := MultiArray[0,2]; // Str receives 'blue'
MultiArray[1,1] := 'new orange';If Statements
There are two forms of if statement. if .. then and the if .. then .. else. Like normal Pascal, if the if expression is true, the statement (or block) is executed. If there is else part and expression is false, statement (or block) after else is executed.
Example:
pascal
if J <> 0 then Result := I / J;
if J = 0 then Exit else Result := I / J;
if J <> 0 then
begin
Result := I / J;
Count := Count + 1;
end
else
Done := True;While Statements
A while statement is used to repeat a statement or a block, while a control condition (expression) is evaluated as true. The expression is evaluated before the statement. Hence, if the expression is false at first iteration, the statement sequence is never executed. The while statement executes its constituent statement (or block) repeatedly, testing the expression before each iteration. As long as the expression returns True, the execution continues.
Example:
pascal
while Data[I] <> X do I := I + 1;
while I > 0 do
begin
if Odd(I) then Z := Z * X;
I := I div 2;
X := Sqr(X);
end;Repeat Statements
The syntax of a repeat statement is repeat statement1; ...; statementn; until expression where expression returns a Boolean value. The repeat statement executes its sequence of constituent statements continually, testing the expression after each iteration. When the expression returns true, the repeat statement terminates. The sequence is always executed at least once because the expression is not evaluated until after the first iteration.
Example:
pascal
repeat
K := I mod J;
I := J;
J := K;
until J = 0;For Statements
A script supports for statements with the following syntax:
pascal
for Counter := InitialValue to FinalValue do statementThe for statement sets a counter to the initialValue, repeats the execution of statement (or block) and increments the value of the counter until it reaches the finalValue.
Example 1:
pascal
for i := 1 to 10 do
a := a + i;Example 2:
pascal
for i := a to b do
begin
j := i^2;
sum := sum + j;
endCase Statements
A script supports Case statements with the following syntax.
pascal
case SelectorExpression of
CaseExpr1: Statement1;
...
CaseExprN: StatementN;
else
ElseStatement;
end;If the SelectorExpression matches the result of one of the CaseExpr expressions, the respective statement (or block) will be executed. Otherwise, the ElseStatement will be executed. The else part of the case statement is optional. Expressions of any type can be used in the selector expression and the case expressions, not only ordinal values.
Example:
pascal
case Fruit of
'Lime': Color := 'Green';
'Orange': Color := 'Orange';
'Apple': Color := 'Red';
else
Color := 'Black';
end;Function and Procedure Declaration
Declaration of functions and procedures are similar to Pascal, with the difference that variable types do not need to be specified. To return function values, the implicitly declared Result variable is used. Parameters by reference can also be used by using var.
Example:
pascal
procedure HelloWorld;
begin
A := 'Hello world!';
end;
procedure SetUppercase(Text);
begin
A := UpperCase(Text));
end;
function TodayAsString;
begin
Result := DateToStr(Date);
end;
function Max(A, B);
begin
if A > B then
Result := A
else
Result := B;
end;
procedure SwapValues(var A, B);
var
Temp;
begin
Temp := A;
A := B;
B := Temp;
end;