TScript

Tech Talk

Homepage
TScript
Tech Talk
 

If you assign a string to the TScript's "Text" property and then call the "Compile" method the following things will happen:

Tokenizer (Scanner)

The string containing the script program code is broken up into tokens. A script program like "a := 2;" would result in this tokenlist:

[ttName: "A"] [ttSpace] [ttAssign] [ttSpace] [ttInteger: 2] [ttSemi]

"ttName", "ttSpace", ... are constants used in the source code of TScript indicating the token type.

Parser

The parser transforms the token list into a tree of TExpr objects. Every node in this tree is a function represented by a TFuncExpr object. The children of a node are the arguments of the function. Control structures like "while do end", "for do end" and "if then else" are mapped to functions. E. g. "if then else" is beeing mapped to a function "IF" that has three arguments. The first argument returns a boolean value. If it's true the second argument is evaluated otherwise the third.

// Script Code
if a = 0 then b := 2 + 1;

// Is interpreted as...
SCRIPT (IF (= (a, 0), SET (b, +(2, 1) ) );

// ... and parsed into an TExpr tree:
func: SCRIPT
  func: IF
    func: =
      var: a
      const: 0
    func: SET
      var: b
      func: +
        const: 2
        const: 1

The tree generated by the parser consists of four subclasses of TExpr:

TFuncExpr (func)
- Encapsulates a function.
- Method "Eval" evaluates the function using its arguments and returns the result value if there is one.

TVarExpr (var)
- Encapsulates a variable used in the script
- Method "Eval" returns the value of the variable
- Method "SetValue" sets a new value

TArrayExpr
- Encapsulates an element of an array
- Method "Eval" returns the value of the array-element
- Method "SetValue" sets a new value for the array-element

TConstExpr (const)
- Represents a constant value or a constant declared in the "const" section
- Method "Eval" returns the constant value

Execution

If you call the "Execute" method of the TScript component, the "Eval" method of the base TFuncExpr object is called. This object evaluates its arguments exactly once. If an argument is a function, this function evaluates its arguments and so on...

Variables

For every variable found in the script code the parser creates a TVariable object. After compilation all TVariable objects contain the value varClear (= void in TScript, see "Variants" in the online help of Delphi). The first assignment fixes the type of the TVariable object. Every other assignment has to be of the same type.

a := 'Hallo';

This statement fixes the variable "a" to the type "string". An further assignment like the following would result in a run-time-error:

a := 2;

You can create and set variables before compilation using TScript.SetGlobal, TScript.SetGlobalConst and during the execution of the script using TScript.SetVar and TScript.GetVar methods.

Arrays

Arrays are stored in variant arrays. They are created using the command VarArrayCreate (see Delphi help). To create an array in code use these lines:

var
   v : Variant;
begin
   v := VarArrayCreate ([0, 2], varOleStr);
   Script1.SetGlobal ('x', v);
end;

Constants

Constants are read-only variables set by the compiler.