Pascal basics

The script language is based on the Pascal programming language, but it’s quite simple to understand.

In this language, as in many others, there are basic "building blocks" you can combine to achieve what you want to do. Here weʼll review some basic notions. You can find on the internet many tutorials about Pascal and Delphi, that could help you learn more advanced concepts.

Notation conventions

  • Like other computer languages, the scripting language is made with a combination of keywords (words specific to the language like : "for", "to", "begin", "end", "case", etc), operators (either mathematical: +, -, /, *, etc… or logical: >, <, =, <>, OR, AND, not), and variables (you choose their names).
  • The scripting language is NOT case sensitive, so "Var" and "var", "GetValue" and "getvalue",
    "For" and "for", or "NbOfMIDI" and "nbOfMIDI" mean exactly the same things.
  • A script is made of a series of instructions. Each instruction is separated by
    a semicolon ";". Don’t forget these, it’s a common source of errors. You can write several instructions on the same line like:
  Var x : integer ; x:=2 ; x:=x*2 ;

But usually it’s more clear to put each instruction on a separate line:

  Var x : integer ;
  x:=2 ;
  x:=x*2 ;

Everything that is situated after two slashes // is considered as a comment (it’s not processed by the script compiler, and is just here to give comments or explanations about the code).

For example:

  //initialisation procedure to create parameters.

You can also use multi-line comments (especially when debugging it's convenient to comment several lines in one go).
You insert these comments between ( and ) or between {$ and $}.

For example:

  (* this is
  A multi-line comment *)

  {$ this is also a 
  multi-line comment $}

Scripting language building blocks

Variables

You give them a name (a, x, tick, MIDI-input, out2, or anything you like). Their name must not be a Pascal keyword, and must start with a
letter (a...z) or an underscore (-).

You use them to store data and make calculations, you can send them to functions and procedures to be processed.

Constants

These are variables that keep the same value throughout the script without being changed. Can be useful if the same value is used many times in your script, or, for instance if you deal with arrays of a fixed size and want to be able to easily change this size, etc.

Types

Every variable in Pascal must have a "type". (this tells basically to the computer how much memory space must be allocated for it). These are the common types used:

integer Whole numbers from -32768 to 32767

byte The integers from 0 to 255

real Floating point numbers from 1E-38 to 1E+38

boolean Can only have the value TRUE or FALSE

char Any character in the ASCII character set

shortint The integers from -128 to 127

word The integers from 0 to 65535

longint The integers from -2147483648 to 2147483647

single Real type with 7 significant digits

double Real type with 15 significant digits

extended Real type with 19 significant digits

comp The integers from about -10E18 to 10E18

In Usine, we’ll use mostly: integer, single, byte, boolean, char.

You can also use other types of data : array and string, we’ll explain them further.

Precisions about types

Along with the common Types of data described before,
you can build your own types.

To do this, just write the keyword: type followed by your custom type name, the keyword = and the type definition.

You can also use to declare a type a data structure called "record". For details about records read further.

Examples
Type MyInteger = integer; 

Type Apple = record
     Color : byte;
     Weight : single;
     Origin : string;
    End;

Usine’s script language has some useful types implemented you can use. They are:

type TParameter // this is a "Parameter", which means an inlet or an outlet for your module.

type TScale     = (lsLinear,lsLog,lsExp); // these are the different possible scale 
values for parameters.

type TParamType=
                     (
                     ptTextField, 
                     ptChooseColor,
                     ptMIDI,
                     ptGainFader,
                     ptAudio,
                     ptDataField,
                     ptDataFader,
                     ptButton,
                     ptListBox,
                     ptSwitch,
                     ptArray,
                     ptIpAddress,
                     ptSmpte,
                     ptMIDINoteFader,
                     ptPointerBitMap,
                     ptPointer,
                     ptLed,
                     ptOther
                     ); // these are the different available kind of parameters. 

type  TMIDI = Record
       Msg   : Byte; 
       Data1 : Byte; 
       Data2 : Byte; 
       Channel : byte; 
       end; // this is a MIDI code.

You’ll soon learn all what you need to fully understand parameters, and MIDI.

Variable declaration

Before using any variable in a script, you have to declare it.
You have to do it with the keyword var:

Var Variable-Name : Type
Examples
Var x : integer;  //this declares variable x to store integer values

Var  tab1  :  array  of  double;  //this  declares  a  table of floating point values called tab1

Var  Input1  :  tParameter;  //this  declares  an  inlet-outlet called input1

Var  MIDIIn  : tMIDI; //this  declares a variable  called MIDIIn that will store MIDI Code.

If you need to declare several variables of the same type you can do like this:

Var x, y, r : single;
Constants declarations
Const constant-name = Value;
Pascal specific keywords
Basic keywords

var : used to declare a variable := : used to assign a value to a variable (ex: x := 2; , stores the number 2 into variable x) ; : beware not to forget it at the end of each individual instruction begin end; : used to group instructions in "blocks" of instructions.

Example of a block
Begin
   X:=1;
   Y:=2;
End;  //  this  block  is  considered  as  one  single instruction.

Often you’ll use blocks inside blocks inside blocks, etc…, a bit like "Russian Dolls". It’s a good habit to indent each new begin and to align it with its related end; This helps for clarity, and to avoid mistakes.

Example:

Begin
// instructions

        Begin
         // instructions
                 Begin
                 // Instructions
                          Begin
                           // instructions
                           End;
                 End;
        End;
// instructions
End;
Important things about begin and end;
  • begin must always be "closed" by an end;
  • One instance of begin always relate to the closest following end;
  • One instance of end; always relate to the closest previous begin
  • You always have a semicolon (;) after end excepted in the following cases:

    In the if then else statement (see later in this chapter), a
    instance of end preceding else is //not// followed by a semicolon.

Loops

To execute a block of instructions several times (let’s say n times)
you have two basic options:

The for loop

   Var i, n : integer;
   For i:=1 to n do 
   Begin
    // instructions
   End;  //  the  block  will  be  executed  n  times, and at each iteration i will be incremented

The while loop

While  condition  do    //  condition  is  a  Boolean expression (like x<10, A AND b, y <> x, etc…)
Begin
// instructions
End;

don’t forget to have somewhere in the while block some code that will make the condition change after some time, else your loop will go forever, and you’ll probably crash Usine!

Conditional statements

Often, you’ll want a block of instructions to be processed only if a condition is
true, or if a variable has only certain values. These are two ways to achieve it:

If then else

If  condition  then  //  condition  is  a  Boolean  expression (like: x<10, A AND b, y <> x, etc…)
     Begin
     // instructions processed if the boolean is true
     End // notice the absence of ";" after end!
Else
     Begin
     //instructions processed if the boolean is false
     End;

When there is only one instruction to process, you can simply write for example:

Var x, y : single;
If x<>0 then y:=1/x;

Case

Let’s say we have a integer variable called n. (n can store for instance
the value of a combobox inlet).
If you want to execute different instructions according to the value of n, you can do like this:

case n of  // let’s say n can be 1, 2 or 3
1:
     begin
     // instructions processed if n = 1
     End;
2:
     begin
     // instructions processed if n = 2
     End;
3:
     begin
     // instructions processed if n = 3
     End;
End; //end of case

This is especially useful when you use listboxes.

Functions

They have a name optionally followed by argument(s) between parentheses. They return a result, according to the argument(s). Each argument has a specific type.

Example

round(x);  returns the rounded value of x. 

You can build you own functions and use them in you script,
or use Pascal’s or Usine’s built-in functions. The basic syntax of a function is:

function-name(param1, param2, ..., paramN); 

You can assign the result of a function to a variable:

Var x, y, r : single;
r := function-name(x,  y); // the result of the function with arguments x and y is stored to r

You can also use the function directly in an expression:

Var x, y, z, r : single;
r := x*function-name(y, z);

As said before, the type of the result, and the types of
the parameters must match with the function definition, else the script will not compile. Example from the script documentation:

function CreateParam(S1: string; s2: Longint) : TParameter; 
// This function "CreateParam" expects as arguments a String
// (the  display name of the parameter), and a LongInteger. 
// And  it  returns  a  Parameter  (type tParameter).

The script documentation is very useful, since it contains all
built-in functions definitions, and tells you exactly the number and
types of parameters expected by each function, and the type of its result.

You can build your own functions, this way:

function function-name ( param1 : type ; param2: type ;   ...;  
paramN : type): type-of-result;

Var  variable-name  :  variable-type;  //  here  you  declare the local variables used in your function
Begin
// Here’s the code of your function, assign the result of it to the variable Result
Result := ...;
End;

Procedures

Procedures also have a name optionally followed by argument(s)
between parentheses, but don't return any result like functions do.
If however an argument is declared with var before the variable name,
you can pass a variable as an argument and the procedure can change the value.

Example

SetArrayLength(table1,  16);  // sets  the  length  of  the  array named "table1" to 16. 

Beware that the types of the parameters must match with the procedure definition, else the script will not compile. Example from the script documentation:

procedure SetIsInput(Param: integer; val: boolean);
//  This  procedure  "SetIsInput",  expects  as  arguments  a Parameter 
(type tParameter), and a boolean value.

The script documentation is very useful, since it contains all
built-in procedure definitions, and tells you exactly the number and
types of parameters expected by each of them.

You can build your own procedure, this way:

procedure procedure-name ( param1 : type ; param2: type ;  … ;  
paramN : type);
Var  variable-name  :  variable-type;  //  here  you  declare the local variables used in your procedure
Begin
  //Here’s the code of your procedure, nothing to assign to result since 
  //a procedure doesn’t return any value
End;
Mathematical and logical operators

The scripting language contains all mathematical operators:

* +, -, , /**

You can also use the usual logical operators:

<, >, =, <=, >=, <> (not equal), and, or, not

All these logical operators return a boolean value (true or false).

You can also use built-in functions to perform more advanced maths:

  Function Timems :extended;
  Function Sin(e: Extended) : Extended; 
  Function Cos(e: Extended) : Extended; 
  Function ArcCos(e: Extended) : Extended; 
  Function ArcSin(e: Extended) : Extended; 
  Function Arctan(e: Extended) : Extended; 
  Function Sqrt (e: Extended) : Extended;    // Square root
  Function  Round(e: Extended)  : Longint;   // rounds to  the nearest integer
  Function Trunc(e: Extended) : Longint;     // removes decimal
  Function Int(e: Extended) : Longint; 
  Function Pi: Extended;
  Function Abs(e: Extended) : Extended; 
  Function Ln(e: Extended) : Extended; 
  Function Log(e: Extended) : Extended; 
  Function Exp(e: Extended) : Extended;
  Function power(base,e: Extended) : Extended; // example 2 power 4 will be written power(4,2);
  Function Random : single;

See also

version 3.0.150

Edit All Pages