TDAStdExpressionEvaluator

Overview

The TDAStdExpressionEvaluator class is the specialization of the TDAExpressionEvaluator class. It implements the following commonly used functions:

  • Convert(val, typeName)
  • Len(val)
  • IsNull(val, resultIfValIsNull)
  • IIF(switcher, ifTrueResult, ifFalseResult)
  • Trim(val)
  • Substring(val, index, count)

Use case

program TDAStdExpressionEvaluator_Demo;

{$APPTYPE CONSOLE}

uses
  SysUtils, Variants, uDAExpressionEvaluator;

type
  TEvaluationContext = class
  public
    procedure ValueSupplier(Sender: TDAExpressionEvaluator; const aIdentifier: string; out aValue: Variant);
  end;

{ TEvaluationContext }

procedure TEvaluationContext.ValueSupplier(Sender: TDAExpressionEvaluator;
  const aIdentifier: string; out aValue: Variant);
begin
  if aIdentifier = 'null' then aValue := null
  else aValue := Unassigned;
end;

procedure doDemo ( const expression : string);
var
  evaluator   : TDAExpressionEvaluator;
  context     : TEvaluationContext;
begin
  evaluator := TDAStdExpressionEvaluator.Create;
  context := TEvaluationContext.Create;
  evaluator.OnGetValue := context.ValueSupplier;
  Write (QuotedStr (expression), ' -> ');
  WriteLn (QuotedStr (evaluator.Evaluate (expression)));
  evaluator.Free;
end;

begin
  try
    doDemo ('Len(`12345`)');  //'Len(`12345`)' -> '5'
    doDemo ('IsNull(null, `First arg is null`)'); //'IsNull(null, `First arg is null`)' -> 'First arg is null'
    doDemo ('IsNull(TRUE, `First arg is not null`)'); //'IsNull(TRUE, `First arg is not null`)' -> 'True'
    doDemo ('IIF(2 + 2 = 4, `is true`, `is false`)'); //'IIF(2 + 2 = 4, `is true`, `is false`)' -> 'is true'
    doDemo ('IIF(4 <> 2 * 2, `is true`, `is false`)');  //'IIF(4 <> 2 * 2, `is true`, `is false`)' -> 'is false'
    doDemo ('Trim(`  value  `)'); //'Trim(`  value  `)' -> 'value'
    doDemo ('Substring(`one two three`, 6, 3)'); //'Substring(`one two three`, 6, 3)' -> 'two'
  except on E : Exception do
    WriteLn ('Exception: ', E.Message);
  end;
end.

Location

 

constructor Create

Creates a new instance.

constructor Create

Evaluate    (declared in TDAExpressionEvaluator)

Performs Pascal Script from aString (see the Use Case).

function Evaluate(const aString: string): Variant

Parameters:

  • aString: Script text

FunctionList    (declared in TDAExpressionEvaluator)

Holds the set of known functions (see the Use Case).

property FunctionList: TDAEEFunctionList read

OnGetValue    (declared in TDAExpressionEvaluator)

Fires when the script engine encounters an unkown variable (see the Use Case).

property OnGetValue: TDAEEGetValue read write
delegate: procedure OnGetValue(Sender: TDAExpressionEvaluator; const aIdentifier: string; out aValue: Variant)

OnUnknownFunctionCall    (declared in TDAExpressionEvaluator)

Fires when the script engine encounters an unkown a call of an unknown function (not present in FunctionList)(see the Use Case).

property OnUnknownFunctionCall: TDAEEFunctionCall read write
delegate: procedure OnUnknownFunctionCall(Sender: TDAExpressionEvaluator; const aIdentifier: string; const Args: array of Variant; out aValue: Variant)

StringCaseInsensitive    (declared in TDAExpressionEvaluator)

Controls if Pascal rules for identifier name distinguishing is used (True) or not (False, default).

property StringCaseInsensitive: Boolean read write

UseTrueFalseinVariableName    (declared in TDAExpressionEvaluator)

Controls if the name of a variable (True and false) presents a Boolean value (False, default) or a variable (True).

property UseTrueFalseinVariableName: Boolean read write

UseWildcardsInEqual    (declared in TDAExpressionEvaluator)

Controls if a Like style (symbol "*") is used in string comparison (True) or not (False, default).

property UseWildcardsInEqual: Boolean read write

 

FunctionList    (declared in TDAExpressionEvaluator)

Holds the set of known functions (see the Use Case).

property FunctionList: TDAEEFunctionList read

StringCaseInsensitive    (declared in TDAExpressionEvaluator)

Controls if Pascal rules for identifier name distinguishing is used (True) or not (False, default).

property StringCaseInsensitive: Boolean read write

UseTrueFalseinVariableName    (declared in TDAExpressionEvaluator)

Controls if the name of a variable (True and false) presents a Boolean value (False, default) or a variable (True).

property UseTrueFalseinVariableName: Boolean read write

UseWildcardsInEqual    (declared in TDAExpressionEvaluator)

Controls if a Like style (symbol "*") is used in string comparison (True) or not (False, default).

property UseWildcardsInEqual: Boolean read write

 

constructor Create

Creates a new instance.

constructor Create

Evaluate    (declared in TDAExpressionEvaluator)

Performs Pascal Script from aString (see the Use Case).

function Evaluate(const aString: string): Variant

Parameters:

  • aString: Script text

 

OnGetValue    (declared in TDAExpressionEvaluator)

Fires when the script engine encounters an unkown variable (see the Use Case).

property OnGetValue: TDAEEGetValue read write
delegate: procedure OnGetValue(Sender: TDAExpressionEvaluator; const aIdentifier: string; out aValue: Variant)

OnUnknownFunctionCall    (declared in TDAExpressionEvaluator)

Fires when the script engine encounters an unkown a call of an unknown function (not present in FunctionList)(see the Use Case).

property OnUnknownFunctionCall: TDAEEFunctionCall read write
delegate: procedure OnUnknownFunctionCall(Sender: TDAExpressionEvaluator; const aIdentifier: string; const Args: array of Variant; out aValue: Variant)