Pascal Scripting Language

<< Click to Display Table of Contents >>

Navigation:  FindOnClick > FindOnClick For Advanced Users > Scripting >

Pascal Scripting Language

 

The Pascal syntax is similar to Delphi and supports:

 

begin .. end constructor

procedure and function declarations

if .. then .. else constructor

for .. to .. do .. step constructor

while .. do constructor

repeat .. until constructor

try .. except and try .. finally blocks

case statements

array constructors (x:=[ 1, 2, 3 ];)

^ , * , / , and , + , - , or , <> , >=, <= , = , > , < , div , mod , xor , shl , shr operators

access to object properties and methods ( ObjectName.SubObject.Property )

 

Like in Pascal, statements should be terminated by the ";" character. Begin..end blocks are allowed to group statements.

 

 

Identifiers

 

Identifier names in scripts (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 '_' char. They cannot contain any other character or spaces.

 

For example:

 

Valid: VarName, _Some, V1A2, _____Some____

Invalid: 2Var, My Name, Some-more, This,is,not,valid

 

 

Assign Statements

 

Just like in Pascal, assign statements (assign a value or expression result to a variable or object property) are built using ":=". Examples:

 

MyVar := 2;
Button.Caption := 'This ' + 'is ok.';

 

 

Character Strings

 

Strings (sequence of characters) are declared in Pascal using single quote (') character. Double quotes (") are not used. You can also use #nn to declare a character inside a string. There is no need to use '+' operator to add a character to a string. For example:

 

A := 'This is a text';
Str := 'Text '+'concat';
B := 'String with CR and LF char at the end'#13#10;
C := 'String with '#33#34' characters in the middle';

 

 

Comments

 

Comments can be inserted inside scripts. You can use // chars or (* *) or { } blocks. Using // the comment will finish at the end of line. For example:

 

// This is a comment before ShowMessage
ShowMessage('Ok');
 
(* This is another comment *)
ShowMessage('More ok!');
 
{ And this is a comment
with two lines }
ShowMessage('End of okays');

 

 

Variables

 

When using Pascal you must declare variables. However, there is no need to declare variable types. To declare variables use the var directive and the variable name. For example:

 

procedure Msg;
var S;
begin
  S:='Hello world!';
  ShowMessage(S);
end;

 

 

Indexes

 

Strings, arrays and array properties can be indexed using "[" and "]" chars. For example, if Str is a string variable, the expression Str[3] returns the third character in the string denoted by Str, while Str[X + 1] returns the character immediately after the one indexed by X. For example:

 

MyChar:=MyStr[2];
MyStr[1]:='A';
MyArray[1,2]:=1530;
Lines.Strings[2]:='Some text'; 

 

 

Arrays

 

Array constructors and variant arrays are supported. To construct an an array, use "[" and "]" chars. You can construct multi-index array nesting array constructors. You can then access arrays using indexes. If the array is multi-index, separate indexes using ",".

 

If a variable is a variant array, then indexing in that variable is supported. A variable is a variant array if it was assigned using an array constructor, if it is a direct reference to a Delphi variable which is a variant array or if it was created using the VarArrayCreate procedure.

 

Arrays are 0-based index. For example:

 

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 an else part and the expression is false, then the statement (or block) after else is executed. For example:

 

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 control condition is evaluated before the statement. Hence, if the control condition 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, execution continues. For example:

 

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;
 
while not Eof(InputFile) do begin
  Readln(InputFile, Line);
  Process(Line);
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. For example:

 

repeat
  K := I mod J;
  I := J;
  J := K;
until J = 0;
 
repeat
  Write('Enter a value (0..9): ');
  Readln(I);
until (I >= 0) and (I <= 9);

 

 

For statements

 

The for statement has the following syntax: for counter := initialValue to finalValue do statement

 

The for statement sets the counter to initialValue, repeats execution of the statement (or block) and increments the value of counter until counter reaches finalValue. For example:

 

for c:=1 to 10 do
  a:=a+c;
 
for i:=a to b do begin
  j:=i^2;
  sum:=sum+j;
end;

 

 

Case statements

 

Case statements have the following syntax:

 

case selectorExpression of

caseexpr1: statement1;

...

caseexprn: statementn;

else

  elsestatement;

end;

 

if selectorExpression matches the result of one of the caseexprn expressions, the respective statement (or block) will be executed. Otherwise, the elsestatement will be executed. The Else part of a case statement is optional. It is different from Delphi because the case statement in doesn't need to use only ordinal values. You can use expressions of any type in both selector expression and case expression. For example:

 

case uppercase(Fruit) of
  'lime': ShowMessage('green');
  'orange': ShowMessage('orange');
  'apple': ShowMessage('red');
else
  ShowMessage('black');
end;

 

 

Function and procedure declarations

 

The declaration of functions and procedures is similar to Object Pascal in Delphi, with the difference you don't specify variable types. Just like OP, to return function values, use the implicitly declared result variable. Parameters by reference can also be used, with the restriction that there is no need to specify variable types. For example:

 

procedure HelloWorld;
begin
  ShowMessage('Hello world!');
end;
 
procedure UpcaseMessage(Msg);
begin
  ShowMessage(Uppercase(Msg));
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;

 

 

<%SBCOPYRIGHT%>