{$ifdef fpc}{$mode delphi}{$endif}
uses classes;
type
TExpressionNodeType = (entBinary, entUnary, entLiteral, entVariable);
TBinaryOperator = (boEqual, boNotEqual, boAnd, boOr, boLess, boLessEqual, boGreater, boGreaterEqual);
TUnaryOperator = (uoNot);
TLiteralValue = type ShortString;
TVariableName = type ShortString;
IExpressionNode= Interface
['{F0DE9818-97BF-4A57-8838-67F72F402707}']
// Add methodss you need;
// Interface handles memory management
// The expressions can become quite complex
end;
TExpressionNode = class(TInterfacedObject, IExpressionNode)
NodeType:record
case TExpressionNodeType of
entBinary: (Op: TBinaryOperator; Left, Right: TExpressionNode);
entUnary: (UnOp: TUnaryOperator; Operand: TExpressionNode);
entLiteral: (LiteralValue: ShortString);
entVariable: (VariableName: ShortString);
end;
public
constructor Create(const operand:TBinaryOperator); overload;
constructor Create(const operand:TUnaryOperator); overload;
constructor Create(const operand:TLiteralValue);overload;
constructor Create(const operand:TVariableName);overload;
constructor Create(const operand:TBinaryOperator;const left,right:IExpressionNode); overload;
constructor Create(const operand:TUnaryOperator;const node:IExpressionNode); overload;
end;
{ TExpressionNode }
constructor TExpressionNode.Create(const operand: TBinaryoperator);
begin
Inherited create;
NodeType.OP := operand;
end;
constructor TExpressionNode.Create(const operand: TUnaryoperator);
begin
Inherited create;
NodeType.UnOP := operand;
end;
constructor TExpressionNode.Create(const operand: TLiteralValue);
begin
Inherited create;
Nodetype.LiteralValue := operand;
end;
constructor TExpressionNode.Create(const operand: TVariableName);
begin
Inherited create;
Nodetype.VariableName := operand;
end;
constructor TExpressionNode.Create(const operand: TUnaryOperator;
const node: IExpressionNode);
begin
create(operand);
// do something with node
end;
constructor TExpressionNode.Create(const operand: TBinaryOperator; const left,
right: IExpressionNode);
begin
create(operand);
// do something with left, right
end;
var
EN:IExpressionNode; // The interface, not the class!
begin
EN:= TExpressionNode.Create(boEqual);
end.