Pascal Syntax
ArtSong’s Pascal Script Editor and Algorithm components execute scripts written in Pascal syntax. Current Pascal syntax supports:
begin .. end code block delimiter
procedure and function declarations
if .. then .. else decision making
for .. to .. do .. step loop constructor
while .. do loop constructor
repeat .. until loop constructor
try .. except and try .. finally exception handling blocks
case statements for decision making
array constructors (x:=[ 1, 2, 3 ];)
* , / , and , + , - , or , not, <> , >= , <= , = , > , < , div , mod , xor , shl , shr operators
access to object properties and methods ( ObjectName.SubObject.Property )
Script structure
Script structure consists of two different code sections: 1) a FUNCTIONS section containing procedure and function declarations and 2) a MAIN section. Both sections are optional, but at least one should be present in script. If both sections are present the ‘procedure and function’ section should precede the ‘main’ section. The ‘main’ section does not need to be inside a begin..end code block and it may just consist of a single statement.
Script execution begins in the main section.
Examples
SCRIPT 1:
// Start of ‘procedure’ section procedure DoSomething; begin CallSomething; end;
// Start of ‘main’ section begin CallSomethingElse; end;
SCRIPT 2:
// start of ‘main’ section // no ‘procedures’ section begin CallSomethingElse; end;
SCRIPT 3:
// start of procedure section having // no ‘main’ section function MyFunction begin result:='Ok!'; end;
SCRIPT 4:
// start of ‘main’ section // no ‘procedure’ section CallSomethingElse;
Like standard Pascal, statements should be terminated by the ";" character and begin..end blocks are used to group statements into code blocks.
Identifiers
‘Identifier’ refers to the names that we use in scripts to represent variables, functions, procedures, etc. The rules for naming follow the common naming practices for standard Pascal: names must begin with an alphabetic character (a..z or A..Z) or underscore (_), and can be followed by alphanumeric chars (a..Z and 0..9) or underscore '_' char. Names cannot contain any other characters or spaces.
Valid identifiers:
VarName _Some
Example Invalid identifiers:
2Var // ERROR - starts with a number
My Name // ERROR - contains a space
Some-more // ERROR - contains an ‘other’ // character (hyphen)
This,is,not,valid // ERROR - contains commas
Variables
Variables are named ‘data’ storage areas used to hold values during script execution. ArtSong requires variables to be declared prior to use; if you do not declare variables you will receive a compile error.
Declare variables using the var keyword. Multiple variables can be declared on the same line. It is important to note that all variables have local scope; they can only be ‘seen’ within the code block they are defined in.
Example Variable Declarations
var myVariable; // declares one variable
// named ‘myVariable’
var myA, myB, myC; // declares three variables
Variables having global scope, (can be seen from any code block), must be defined before the FUNCTIONS section.
vars myA, myB, myC;
FUNCTIONS Code Block
MAIN Code Block
Implementation Note: Variables are actually implemented as Variants by the scripting engine and can be used to store integer or floating point numbers, text strings, or object references.
Examples
SCRIPT 1:
procedure Msg; var S; // declares a local variable ‘S’ which can // only be used in this procedure< begin S:='Hello world!'; ShowMessage(S); end;
SCRIPT 2:
var A; // variable ‘A’ declared in ‘main’ // second is local only to ‘main’ section begin A:=0; A:=A+1; end;
SCRIPT 3:
var S; // another ‘Main’ section variable
// declaration S:='Hello World!'; ShowMessage(S);
SCRIPT 4:
var A; // declaration of global variable ‘A’ // which can be accessed from // any code section
// ‘procedures’ section procedure DoSomething begin A := 32; end;
// ‘main’ section
ShowMessage(A); DoSomething(); // assigns a value to A ShowMessage(A);
Assignment statements
Assignment statements are used to assign a value or expression result to a variable or object property. Assign statements are built using the assignment operator “:=".
Examples:
myVar := 2; myOtherVar := 2.345; Button.Caption := 'This ' + 'is ok.';
Character strings
Character (or text) strings are declared using the single quote (') character.
A := 'This is a text string';
Concatenate strings using the ‘+’ operator.
B := 'Text ' + 'concat';
You can use #nn to declare a literal character inside a string. There is no need to use '+' operator to add a character to a string.
C := 'String with CR and LF char at the end'#13#10;
D := 'String with '#33#34' characters in the middle';
String characters 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[I + 1] returns the character immediately after the one indexed by I.
Examples:
MyChar := MyStr[2];
MyStr[1] := 'A';
»
|