Class TExpression

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type TExpression = class(TCustomValue)

Description

TExpression is the compiled version of an Expression generated by the TRbwParser.Compile method.

It can be evaluated by using the Evaluate method and the result can then be read using the BooleanResult method, DoubleResult method, IntegerResult method, or StringResult method. The correct one to read can be determined from the ResultType property.

Every instance of TExpression is owned by the instance of TRbwParser that compiled it.

Hierarchy

Overview

Fields

Private FOptionalArguments: integer;
Private FunctionAddr: Pointer;
Private VariablesForFunction: array of Pointer;
Private FAllowConversionToConstant: boolean;
Private FTag: integer;
Private StringVariableIndicies: array of integer;
Private StringVariableCount: integer;
Private FVariablesUsed: TStringList;
Private FTopLevel: boolean;
Protected ShouldEvaluate: boolean;
Protected Data: array of TVariables;

Methods

Private constructor Create(const VariableName: string; const DataType: TRbwDataType; const CanConvertToConstant: boolean); overload;
Private constructor Create(const FunctionRecord: TFunctionRecord); overload;
Private constructor Create(const VariableName: string; const DataType: TRbwDataType); overload;
Private procedure Initalize(const FunctionAddress: Pointer; const DataTypes: array of TRbwDataType; const OptionalArguments: integer);
Private function GetVariables(const Index: integer): TConstant;
Private procedure SetVariables(const Index: integer; const Value: TConstant);
Private function ConvertToConstant: TConstant;
Private procedure ResetDataLength(const Count: integer);
Private procedure SetAllowConversionToConstant(const Value: boolean);
Private procedure FillVariables;
Private function GetVariablesUsed: TStringList;
Public constructor Create(const FunctionClass: TFunctionClass); overload; virtual;
Public function Decompile: string; override;
Public destructor Destroy; override;
Public procedure Evaluate; virtual;
Public class function New(const FunctionClass: TFunctionClass): TExpression; virtual;
Public function UsesVariable(const Variable: TCustomVariable): boolean;

Properties

Private property AllowConversionToConstant: boolean read FAllowConversionToConstant write SetAllowConversionToConstant;
Private property Variables[const Index: integer]: TConstant read GetVariables write SetVariables;
Public property Tag: integer read FTag write FTag;
Public property VariablesUsed: TStringList read GetVariablesUsed;

Description

Fields

Private FOptionalArguments: integer;

FOptionalArguments: integer; FOptionalArguments is the number of optional arguments in the function that will be evaluated.

Private FunctionAddr: Pointer;

FunctionAddr: Pointer; FunctionAddr is the address of the function used to evaluate the TExpression.

Private VariablesForFunction: array of Pointer;

VariablesForFunction: array of Pointer; VariablesForFunction is the argument passed to the function used to evaluate the TExpression.

Private FAllowConversionToConstant: boolean;

FAllowConversionToConstant: boolean; See AllowConversionToConstant.

Private FTag: integer;

FTag: integer; See Tag

Private StringVariableIndicies: array of integer;

StringVariableIndicies: array of integer; StringVariableIndicies indicates which members of VariablesForFunction refer to strings.

Private StringVariableCount: integer;

StringVariableCount: integer; StringVariableCount is the number of strings in VariablesForFunction.

Private FVariablesUsed: TStringList;

FVariablesUsed: TStringList; FVariablesUsed is used to hold the result of VariablesUsed.

Private FTopLevel: boolean;

FTopLevel: boolean; FTopLevel is used in Decompile to determine whether or not to include parenthesis around the outermost item.

Protected ShouldEvaluate: boolean;

ShouldEvaluate: boolean;

ShouldEvaluate is set to false if a TExpression is equivalent to a TConstant. In such cases, the TExpression is evaluated when it is created and does not need to be reevaluated later. An example would be if the expression used to create the TExpression was "1 + 1". This would be converted to "2".

Protected Data: array of TVariables;

Data: array of TVariables; Data holds the arguments used to evaluate the TExpression.

Methods

Private constructor Create(const VariableName: string; const DataType: TRbwDataType; const CanConvertToConstant: boolean); overload;

Create a TExpression.

Private constructor Create(const FunctionRecord: TFunctionRecord); overload;

Create a TExpression.

Private constructor Create(const VariableName: string; const DataType: TRbwDataType); overload;

Create a TExpression.

Private procedure Initalize(const FunctionAddress: Pointer; const DataTypes: array of TRbwDataType; const OptionalArguments: integer);

Initializes certain variables. Called by Create.

Private function GetVariables(const Index: integer): TConstant;

See Variables.

Private procedure SetVariables(const Index: integer; const Value: TConstant);

See Variables.

Private function ConvertToConstant: TConstant;

if the expression can be represented as a constant value, ConvertToConstant returns a TConstant that represents that value. Otherwise, it returns nil.

Private procedure ResetDataLength(const Count: integer);

If optional arguments are used, ResetDataLength is used to resets the length of arrays to the correct length.

Private procedure SetAllowConversionToConstant(const Value: boolean);

See AllowConversionToConstant.

Private procedure FillVariables;

FillVariables initializes VariablesForFunction and StringVariableIndicies

Private function GetVariablesUsed: TStringList;

See VariablesUsed.

Public constructor Create(const FunctionClass: TFunctionClass); overload; virtual;

Create creates a TExpression based on a TFunctionClass. The values of any variables used by the TExpression must also be set before calling Evaluate.

Public function Decompile: string; override;

Decompile converts the value stored in the TExpression to a string that can be compiled into an equivalent TExpression together with its arguments.

Public destructor Destroy; override;

Destroy destroys the TExpression. Do not call Destroy directly. Call Free instead.

Public procedure Evaluate; virtual;

Evaluate evaluates the expression and sets the result which may then be read using BooleanResult, DoubleResult, IntegerResult, or StringResult depending on the ResultType.

Example:

      procedure TForm1.Button1Click(Sender: TObject);
      begin
        RbwParser1.Compile(Edit1.Text);
        RbwParser1.CurrentExpression.Evaluate;
        case RbwParser1.CurrentExpression.ResultType of
          rdtDouble:
            begin
              Label1.Caption := 'result: '
                + FloatToStr(RbwParser1.CurrentExpression.DoubleResult);
            end;
          rdtInteger:
            begin
              Label1.Caption := 'result: '
                + IntToStr(RbwParser1.CurrentExpression.IntegerResult);
            end;
          rdtBoolean:
            begin
              if RbwParser1.CurrentExpression.BooleanResult then
              begin
                Label1.Caption := 'result: ' + 'True';
              end
              else
              begin
                Label1.Caption := 'result: ' + 'False';
              end;
            end;
          rdtString:
            begin
              Label1.Caption := 'result: '
                + RbwParser1.CurrentExpression.StringResult;
            end;
        else Assert(False);
        end;
      end;
     

Public class function New(const FunctionClass: TFunctionClass): TExpression; virtual;

New usually calls Create to create a TExpression based on FunctionClass. However, in some cases, it creates a descendant of TExpression. Many of the descendents are declared in the implementation section of RBW_Parser.pas. Others can be generated via SpecialImplementorList. One such descendant is TSelectExpression which overrides Evaluate.

Public function UsesVariable(const Variable: TCustomVariable): boolean;

UsesVariable returns True if Variable is used by the TExpression.

Properties

Private property AllowConversionToConstant: boolean read FAllowConversionToConstant write SetAllowConversionToConstant;

AllowConversionToConstant defines whether the result of a function may be considered a constant value if all of the values passed to the function in the values array are constants.

Normally AllowConversionToConstant should be set to True but if the function makes reference to global variables that may change between one evaluation of the expression and the next, AllowConversionToConstant should be set to False.

Pi is an example of a function for which AllowConversionToConstant should be true.

AllowConversionToConstant is used when optimizing compiled expressions.

Private property Variables[const Index: integer]: TConstant read GetVariables write SetVariables;

Variables define the data passed to the function when Evaluate is called. Any descendant of TConstant may be assigned to Variables.

Public property Tag: integer read FTag write FTag;

Tag has no predefined meaning. The Tag property is provided for the convenience of developers. It can be used for storing an additional integer value or it can be typecast to any 32-bit value such as a component reference or a pointer.

Public property VariablesUsed: TStringList read GetVariablesUsed;

VariablesUsed is a list of all the variables used by the TExpression.


Generated by PasDoc 0.10.0 on 2006-10-31 09:56:44