![]() |
![]() |
![]() |
DelphiWebScriptTech Talk |
|
![]() |
Homepage DWS ![]() |
If
you assign a string to the property "Text" of
TDelphiWebScript and 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 these tokens: [ttName: "A"] [ttAssign] [ttInteger: 2] [ttSemi] "ttName", ... are constants used in the source code of DWS indicating the token type (see dwsTokenizer.pas). ParserThe parser transforms the token stream into a tree of TExpr objects. Every node in this tree is a function. The children of a node are the arguments of the function. Control structures like "while do", "for to do" and "if then else" are mapped to functions. E. g. "if then else" is mapped to a function "IFELSE" that takes 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 may contain four subclasses of TExpr: TFuncExpr (func) TVarExpr (var) TArrayExpr TConstExpr (const) ExecutionIf 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... VariablesFor 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". A 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. ArraysArrays are stored in variant arrays. They are created using the command VarArrayCreate (see Delphi help). To create a DWS array in Delphi use this code: var v : Variant; begin v := VarArrayCreate ([0, 2], varOleStr); Script1.SetGlobal ('x', v); end; ConstantsConstants are read-only variables set by the compiler. |