TScript

TFunc Body

Homepage
TScript
TFunc Body
 

OnEval Event

Assumed you define a TFunc named fncSqrt with declaration:

sqrt (float) : float;

Now you have to implement the OnEval event of fncSQRT:

procedure TfrmScriptDemo.fncSqrtEval(Sender: TObject;
   args: TArgList; var Result: Variant);
begin
   result := math.sqrt (args[0].Eval);
end;

The argument "args" is a list of all arguments for your function "sqrt". Due to the declaration of "sqrt" args.Count will always be 1 and args[0] is only argument of type float. Due to the declaration the return value has to be a floating point value (Double, Extended, Single or compatible). If your event method returns a value of another type a runtime error will occurr.

Type of Arguments

If the declaration of your function doesn't say something else all elements of the list "args" are of type TConstExpr. If you use the reserved word "var" in a function declaration there are only TVarExpr and TArrayExpr (an element of an array):

assign(var void, all);

procedure TfrmScriptDemo.fncAssignEval(Sender: TObject;
   args: TArgList; var Result: Variant);
begin
   result := TVarExpr (args[0].Eval).SetValue (args[1].Eval);
end;

If you use the reserved word "func" in a function declaration there are TFuncExpr's, too. "func" is only usefull if the argument is a function with side effects. Example:

sideeffect (func integer) : integer;

procedure TfrmScriptDemo.fncSideEffectEval(Sender: TObject;
   args: TArgList; var Result: Variant);
begin
   args[0].Eval;  
   args[0].Eval;
   result := args[0].Eval
end;
i := 1;
res := sideeffect (inc (i)) // res -> 4

For the declaration "sideeffect (integer) : integer;" res would have the value 2.