功能
Parent page: DelphiScript
The common function statements used by the DelphiScript language are covered below. The range of DelphiScript functions are presented in sections for FileIO routines, Math routines, String routines, and Extension routines.
Calculating Expressions with the Evaluate Function
The built-in function Evaluate
interprets a string containing a valid mathematical expression during runtime and a result is returned. For example, you can write a script such as Evaluate(ProcNames[ProcIndex]);
and the procedure specified in ProcNames[ProcIndex]
will be called.
To calculate such an expression, you can use the Evaluate
method where expression is specified by the Expr
parameter. For example, you can calculate expressions such as the following:
Evaluate('2+5');
Evaluate('((10+15)-5)/2*5');
Evaluate('sin(3.1415926/2)*10');
Evaluate('2.5*log(3)');
Passing Parameters to Functions and Procedures
Both functions and procedures defined in a script can be declared to accept parameters. Additionally, functions are defined to return a value. Types of parameters in procedure/function declarations are ignored and can be skipped. For example, this code is correct:
Function sum(a, b) : integer;
Begin
Result := a + b;
End;
Exiting from a Procedure
DelphiScript provides Exit
and Break
statements to force a procedure to exit before it would terminate naturally. For example; if the value of a parameter is not suitable, you might want to issue a warning to the user and exit, as the example below shows.
Procedure DisplayName (s);
Begin
If s = '' Then
Begin
ShowMessage('Please enter a name');
Exit;
End;
ShowMessage(S + ' is shown');
End;
File IO Routines
DelphiScript has the following IO routines:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DelphiScript gives you the ability to write information to a text file, and since DelphiScript is an untyped language, all values must be converted to strings first.
Here, Read
and ReadLn
routines are equivalent — they read a line up to, but not including, the next line. A Writeln(String)
routine is equivalent to a Write(S)
and a Write(LineFeed + CarriageReturn)
routine.
To write out a text file, employ the AssignFile
, ReWrite
, Writeln
and CloseFile
procedures. To read in a text file, use the AssignFile
, Reset
, Readln
, and CloseFile
procedures. The below example writes to a text file and adds an end-of-line marker. Using the Try / Finally / End
block is recommended to make scripts secure in the event of an IO failure.
Example:
Var
InputFile : TextFile;
OutputFile : TextFile;
I : Integer;
Line : String;
Begin
AssignFile(OutputFile,eConvertedFile.Text);
Rewrite(OutputFile);
AssignFile(InputFIle,eOriginalFIle.Text);
Reset(InputFile);
Try
While not EOF(InputFile) do
Begin
Readln(InputFile,Line);
For I := 1 to Length(Line) Do
Line[I] := UpperCase(Line[I]);
Writeln(Outputfile, Line);
End;
Finally
CloseFile(InputFile);
CloseFile(OutputFile);
End;
End;
Math Routines
DelphiScript has the following math routines:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String Routines
DelphiScript has the following string routines, which can manipulate strings or characters. Only a subset of string routines (most of the string routines used by DelphiScript are imported from the Embarcadero Delphi's SysUtils unit) are shown in the table below:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String routines that are not supported by DelphiScript are outlined in the table below:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Extension Routines
Extension routines are used when dealing with server processes (a command is a packaged server process with parameters, if any) in scripts, especially if you need to extract or set strings for the parameters of processes. Some of the routines are listed below.
To execute parameters of processes in a script, the following functions may be needed:
|
|
|
|
|
|
|
|
|
Useful functions:
|
|
|
|
|
|
|
|
|
Useful dialogs:
|
|
|
|
|
|
Using Sets in DelphiScript
Embarcadero Delphi's Set
and In
keywords, and the set operators +, -, *, <=
and >=
are not supported in DelphiScript. The equivalent Set Operator keywords are tabulated below.
DelphiScript has no set types. To use sets in DelphiScript scripts, use the built-in functions; MkSet
, MkSetRange
, InSet
, SetUnion
, SetDifference
, SetIntersection
, and SubSet
functions that enable sets to be manipulated in a DelphiScript script. These are described in more detail below.
Set Operators
Embarcadero Delphi Set Operator keyword |
Equivalent DelphiScript Set Operator keyword |
---|---|
|
|
- |
|
* |
|
|
|
|
|
|
|
|
|
MkSet Function
The MkSet
function is a set constructor with a variable number of arguments. For example:
Font.Style = MkSet(fsBold,fsItalic);
The MkSet(fsBold,fsItalic)
here denotes two set elements only; fsBold
and fsItalic
.
MkSetRange Function
The MkSetRange
function is a set contructor with a range of arguments. For example:
LayerSet := MkSetRange(eTopLayer,eBottomLayer);
The MkSetRange(eTopLayer,eBottomLayer)
function denotes a range of layers from eTopLayer
to eBottomLayer
.
InSet Function
This InSet
function is used as a substitution of Delphi's In
operator. A in B
is equal to InSet(A, B)
.
If InSet(A,B) then
ShowMessage('A is in B set')
Else
ShowMessage('A not in B set');
SetUnion Function
The SetUnion
function is used as a substitution of Delphi's +
operator. A + B
is equal to SetUnion(A, B)
.
ASet := BSet + CSet;
// should be changed to:
ASet := SetUnion(BSet,CSet);
// in order to achieve the desired script result.
SetDifference Function
The SetDifference
function is used as a substitution of Delphi's -
operator. A - B
is equal to SetDifference(A, B)
.
SetIntersection Function
The SetIntersection
function is used as a substitution of Delphi's *
operator. A * B
is equal to SetIntersection(A, B)
.
SubSet Function
The SubSet
function is used as a substitution of Object Pascal's <=
operator. A <= B
is equal to SubSet(A, B)
.
Using Exception Handlers
The Try
keyword introduces a Try-Except statement or a Try-Finally statement. These two statements are related but serve different purposes:
Try-Finally
With Try-Finally, the statements in the Finally block are always executed regardless of whether an exception from the Try block, Exit or Break occurs. Use the Try-Finally block to free temporary objects and other resources and to perform clean-up activities.
Typically, only one Try-Finally statement is needed in a subroutine.
Example:
Reset(F);
Try
// process file F
Finally
CloseFile(F);
End;
Try-Except
Use Try-Except to handle exceptional cases. For example, to catch specific exceptions and do something useful with them, such as log them in an error log or create a friendly dialog box. Since the On
keyword is not supported in DelphiScript, the Raise
statement is used inside the Except
block.
Example:
Try
X := Y/Z;
Except
Raise('A divide by zero error!');
End;
Raise
The Raise
keyword is related to the Try
keyword. The Raise
keyword can be used without parameters to re-raise the last exception. It can also be used with a string parameter to raise an exception using a specific message.
Example:
Raise(Format('Invalid Value Entered : %d', [Height]));
Note that since the On
keyword is not supported, the Exception
objects (available in Embarcadro Delphi) cannot be used.