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 FSpecialImplementorList: TSpecialImplementorList;
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;
Private FPrototype: string;
Private FNotifier: TNotifierComponent;
Protected ShouldEvaluate: boolean;
Protected Data: array of TVariables;

Methods

Private constructor Create(const VariableName: string; const DataType: TRbwDataType; const CanConvertToConstant: boolean; SpecialImplementorList: TSpecialImplementorList); overload;
Private constructor Create(const FunctionRecord: TFunctionRecord; SpecialImplementorList: TSpecialImplementorList); overload;
Private constructor Create(const VariableName: string; const DataType: TRbwDataType; SpecialImplementorList: TSpecialImplementorList); 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); inline;
Private function DecompileByType(DecompileType: TDecompileType): string;
Private procedure FillVariables;
Private procedure SetResultFromFunction;
Protected function GetVariablesUsed: TStringList; virtual;
Protected procedure MakeDiagram(List: TStringList; Level: integer); override;
Public constructor Create(const FunctionClass: TFunctionClass; SpecialImplementorList: TSpecialImplementorList); overload; virtual;
Public function Decompile: string; override;
Public function DecompileDisplay: string; override;
Public destructor Destroy; override;
Public procedure Evaluate; virtual;
Public class function New(const FunctionClass: TFunctionClass; SpecialImplementorList: TSpecialImplementorList): TExpression; virtual;
Public function UsesVariable(const Variable: TCustomVariable): boolean; virtual;
Public procedure Diagram(List: TStringList);
Public function UsesFunction(FunctionName: string): boolean;

Properties

Private property AllowConversionToConstant: boolean read FAllowConversionToConstant write SetAllowConversionToConstant;
Private property Variables[constIndex:integer]: TConstant read GetVariables write SetVariables;
Public property Tag: integer read FTag write FTag;
Public property VariablesUsed: TStringList read GetVariablesUsed;
Public property Notifier: TNotifierComponent read FNotifier;

Description

Fields

Private FSpecialImplementorList: TSpecialImplementorList;
 
Private FOptionalArguments: integer;

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

Private FunctionAddr: Pointer;

FunctionAddr is the address of the function used to evaluate the TExpression.

Private VariablesForFunction: array of Pointer;

VariablesForFunction is the argument passed to the function used to evaluate the TExpression.

Private FAllowConversionToConstant: boolean;

See AllowConversionToConstant.

Private FTag: integer;

See Tag

Private StringVariableIndicies: array of integer;

StringVariableIndicies indicates which members of VariablesForFunction refer to strings.

Private StringVariableCount: integer;

StringVariableCount is the number of strings in VariablesForFunction.

Private FVariablesUsed: TStringList;

FVariablesUsed is used to hold the result of VariablesUsed.

Private FTopLevel: boolean;

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

Private FPrototype: string;

FPrototype is the prototype of the base TExpression without any hints about where it belongs in a hierarchy. FPrototype is used in MakeDiagram.

See also
Prototype
Prototype should give the names and arguments for a function as they should be shown on a GUI interface.
Private FNotifier: TNotifierComponent;

FNotifier is used to provide notification to components that used an instance of FNotifier when the instance is destroyed.

Protected 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; SpecialImplementorList: TSpecialImplementorList); overload;

Create a TExpression.

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

Create a TExpression.

Private constructor Create(const VariableName: string; const DataType: TRbwDataType; SpecialImplementorList: TSpecialImplementorList); 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); inline;

See AllowConversionToConstant.

Private function DecompileByType(DecompileType: TDecompileType): string;
 
Private procedure FillVariables;

FillVariables initializes VariablesForFunction and StringVariableIndicies

Private procedure SetResultFromFunction;
 
Protected function GetVariablesUsed: TStringList; virtual;

See VariablesUsed.

Protected procedure MakeDiagram(List: TStringList; Level: integer); override;

MakeDiagram fills List with a diagram of itself. First, it adds a new line to List. The new line begins with Level number of tab characters. This is followed by its own name followed by another tab character and then Decompile. Finally it calls MakeDiagram for each of its children passing on List and Level+1.

Public constructor Create(const FunctionClass: TFunctionClass; SpecialImplementorList: TSpecialImplementorList); 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 function DecompileDisplay: string; override;
 
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; SpecialImplementorList: TSpecialImplementorList): 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 TRbwParser.SpecialImplementorList. One such descendant is TSelectExpression which overrides Evaluate.

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

UsesVariable returns True if Variable is used by the TExpression.

Public procedure Diagram(List: TStringList);

Diagram fills List with a diagram of itself. Each line begins with N tab characters. The number of tab characters indicates the position of that line in a hierarchy of child TCustomValues. Next the line has the name of the TCustomValue, another tab character and then a Decompiled version of the TCustomValue.

Public function UsesFunction(FunctionName: string): boolean;
 

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[constIndex: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. The Objects properties holds the variables themselves. The TStringList is owned by the TExpression.

VariablesUsed is sorted, ignores duplicates, and is not case-sensitive;

Public property Notifier: TNotifierComponent read FNotifier;

Notifier is used to provide notification to components that used an instance of Notifier when the instance is destroyed.


Generated by PasDoc 0.12.1 on 2013-05-13 15:41:59