Ast Playground 1. visualization
This commit is contained in:
@@ -7,7 +7,9 @@ uses
|
|||||||
Myc.Ast.Evaluator in '..\Src\AST\Myc.Ast.Evaluator.pas',
|
Myc.Ast.Evaluator in '..\Src\AST\Myc.Ast.Evaluator.pas',
|
||||||
Myc.Ast.Printer in '..\Src\AST\Myc.Ast.Printer.pas',
|
Myc.Ast.Printer in '..\Src\AST\Myc.Ast.Printer.pas',
|
||||||
Myc.Ast.Nodes in '..\Src\AST\Myc.Ast.Nodes.pas',
|
Myc.Ast.Nodes in '..\Src\AST\Myc.Ast.Nodes.pas',
|
||||||
Myc.Ast.Scope in '..\Src\AST\Myc.Ast.Scope.pas';
|
Myc.Ast.Scope in '..\Src\AST\Myc.Ast.Scope.pas',
|
||||||
|
DraggablePanel in 'DraggablePanel.pas',
|
||||||
|
Myc.Ast.Visualizer in 'Myc.Ast.Visualizer.pas';
|
||||||
|
|
||||||
{$R *.res}
|
{$R *.res}
|
||||||
|
|
||||||
|
|||||||
@@ -139,6 +139,8 @@
|
|||||||
<DCCReference Include="..\Src\AST\Myc.Ast.Printer.pas"/>
|
<DCCReference Include="..\Src\AST\Myc.Ast.Printer.pas"/>
|
||||||
<DCCReference Include="..\Src\AST\Myc.Ast.Nodes.pas"/>
|
<DCCReference Include="..\Src\AST\Myc.Ast.Nodes.pas"/>
|
||||||
<DCCReference Include="..\Src\AST\Myc.Ast.Scope.pas"/>
|
<DCCReference Include="..\Src\AST\Myc.Ast.Scope.pas"/>
|
||||||
|
<DCCReference Include="DraggablePanel.pas"/>
|
||||||
|
<DCCReference Include="Myc.Ast.Visualizer.pas"/>
|
||||||
<BuildConfiguration Include="Base">
|
<BuildConfiguration Include="Base">
|
||||||
<Key>Base</Key>
|
<Key>Base</Key>
|
||||||
</BuildConfiguration>
|
</BuildConfiguration>
|
||||||
|
|||||||
@@ -0,0 +1,264 @@
|
|||||||
|
unit DraggablePanel;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
uses
|
||||||
|
System.SysUtils,
|
||||||
|
System.Classes,
|
||||||
|
System.UITypes,
|
||||||
|
System.Types,
|
||||||
|
FMX.Types,
|
||||||
|
FMX.Controls,
|
||||||
|
FMX.StdCtrls,
|
||||||
|
FMX.Graphics,
|
||||||
|
FMX.Objects;
|
||||||
|
|
||||||
|
type
|
||||||
|
/// <summary>
|
||||||
|
/// A panel that can contain other controls and can be moved by dragging its background.
|
||||||
|
/// It custom-paints its own border and title.
|
||||||
|
/// </summary>
|
||||||
|
TAuraNode = class(TStyledControl)
|
||||||
|
private
|
||||||
|
// Flag to indicate if the control is currently being dragged.
|
||||||
|
FIsDragging: Boolean;
|
||||||
|
// Stores the mouse coordinates at the beginning of the drag operation.
|
||||||
|
FDownPos: TPointF;
|
||||||
|
// Properties for the custom-drawn border.
|
||||||
|
FBorderColor: TAlphaColor;
|
||||||
|
FBorderWidth: Single;
|
||||||
|
FBorderRadius: Single;
|
||||||
|
// Properties for the title
|
||||||
|
FTitle: string;
|
||||||
|
FTitleFont: TFont;
|
||||||
|
FTitleFontColor: TAlphaColor;
|
||||||
|
|
||||||
|
procedure SetBorderColor(const Value: TAlphaColor);
|
||||||
|
procedure SetBorderWidth(const Value: Single);
|
||||||
|
procedure SetBorderRadius(const Value: Single);
|
||||||
|
procedure SetTitle(const Value: string);
|
||||||
|
procedure SetTitleFont(const Value: TFont);
|
||||||
|
procedure SetTitleFontColor(const Value: TAlphaColor);
|
||||||
|
procedure TitleFontChanged(Sender: TObject);
|
||||||
|
protected
|
||||||
|
procedure Paint; override;
|
||||||
|
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
|
||||||
|
procedure MouseMove(Shift: TShiftState; X, Y: Single); override;
|
||||||
|
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
|
||||||
|
public
|
||||||
|
constructor Create(AOwner: TComponent); override;
|
||||||
|
destructor Destroy; override;
|
||||||
|
published
|
||||||
|
// Custom border properties
|
||||||
|
property BorderColor: TAlphaColor read FBorderColor write SetBorderColor;
|
||||||
|
property BorderWidth: Single read FBorderWidth write SetBorderWidth;
|
||||||
|
property BorderRadius: Single read FBorderRadius write SetBorderRadius;
|
||||||
|
|
||||||
|
// Title properties
|
||||||
|
property Title: string read FTitle write SetTitle;
|
||||||
|
property TitleFont: TFont read FTitleFont write SetTitleFont;
|
||||||
|
property TitleFontColor: TAlphaColor read FTitleFontColor write SetTitleFontColor;
|
||||||
|
|
||||||
|
// Standard control properties
|
||||||
|
property Align;
|
||||||
|
property Anchors;
|
||||||
|
property ClipChildren default True;
|
||||||
|
property Cursor default crDefault;
|
||||||
|
property DragMode default TDragMode.dmManual;
|
||||||
|
property Enabled;
|
||||||
|
property Height;
|
||||||
|
property HelpContext;
|
||||||
|
property HelpKeyword;
|
||||||
|
property HelpType;
|
||||||
|
property Hint;
|
||||||
|
property HitTest default True;
|
||||||
|
property Locked;
|
||||||
|
property Margins;
|
||||||
|
property Opacity;
|
||||||
|
property Padding;
|
||||||
|
property PopupMenu;
|
||||||
|
property Position;
|
||||||
|
property RotationAngle;
|
||||||
|
property RotationCenter;
|
||||||
|
property Scale;
|
||||||
|
property Size;
|
||||||
|
property StyleLookup;
|
||||||
|
property Visible;
|
||||||
|
property Width;
|
||||||
|
property OnClick;
|
||||||
|
property OnDblClick;
|
||||||
|
property OnMouseDown;
|
||||||
|
property OnMouseMove;
|
||||||
|
property OnMouseUp;
|
||||||
|
property OnMouseWheel;
|
||||||
|
property OnMouseEnter;
|
||||||
|
property OnMouseLeave;
|
||||||
|
end;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
{ TAuraNode }
|
||||||
|
|
||||||
|
constructor TAuraNode.Create(AOwner: TComponent);
|
||||||
|
begin
|
||||||
|
inherited Create(AOwner);
|
||||||
|
FIsDragging := False;
|
||||||
|
|
||||||
|
// Default border settings
|
||||||
|
FBorderColor := TAlphaColors.Gray;
|
||||||
|
FBorderWidth := 1;
|
||||||
|
FBorderRadius := 0; // Sharp corners by default
|
||||||
|
|
||||||
|
// Default title settings
|
||||||
|
FTitle := 'Node';
|
||||||
|
FTitleFont := TFont.Create;
|
||||||
|
FTitleFont.Family := 'Segoe UI';
|
||||||
|
FTitleFont.Size := 11;
|
||||||
|
FTitleFont.Style := [TFontStyle.fsBold];
|
||||||
|
FTitleFont.OnChanged := TitleFontChanged;
|
||||||
|
FTitleFontColor := TAlphaColors.Black;
|
||||||
|
|
||||||
|
Width := 80;
|
||||||
|
Height := 45;
|
||||||
|
|
||||||
|
// The panel must be able to receive mouse events.
|
||||||
|
HitTest := True;
|
||||||
|
// Clip children to the panel's bounds.
|
||||||
|
ClipChildren := True;
|
||||||
|
end;
|
||||||
|
|
||||||
|
destructor TAuraNode.Destroy;
|
||||||
|
begin
|
||||||
|
FTitleFont.Free;
|
||||||
|
inherited;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraNode.SetBorderColor(const Value: TAlphaColor);
|
||||||
|
begin
|
||||||
|
if FBorderColor <> Value then
|
||||||
|
begin
|
||||||
|
FBorderColor := Value;
|
||||||
|
Repaint;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraNode.SetBorderWidth(const Value: Single);
|
||||||
|
begin
|
||||||
|
if FBorderWidth <> Value then
|
||||||
|
begin
|
||||||
|
FBorderWidth := Value;
|
||||||
|
Repaint;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraNode.SetBorderRadius(const Value: Single);
|
||||||
|
begin
|
||||||
|
if FBorderRadius <> Value then
|
||||||
|
begin
|
||||||
|
FBorderRadius := Value;
|
||||||
|
Repaint;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraNode.SetTitle(const Value: string);
|
||||||
|
begin
|
||||||
|
if FTitle <> Value then
|
||||||
|
begin
|
||||||
|
FTitle := Value;
|
||||||
|
Repaint;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraNode.SetTitleFont(const Value: TFont);
|
||||||
|
begin
|
||||||
|
FTitleFont.Assign(Value);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraNode.SetTitleFontColor(const Value: TAlphaColor);
|
||||||
|
begin
|
||||||
|
if FTitleFontColor <> Value then
|
||||||
|
begin
|
||||||
|
FTitleFontColor := Value;
|
||||||
|
Repaint;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraNode.TitleFontChanged(Sender: TObject);
|
||||||
|
begin
|
||||||
|
Repaint;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraNode.Paint;
|
||||||
|
var
|
||||||
|
LRect, LTitleRect: TRectF;
|
||||||
|
begin
|
||||||
|
inherited; // Allow styled painting to occur first (if any)
|
||||||
|
|
||||||
|
// Custom painting for the border
|
||||||
|
if (FBorderWidth > 0) and (FBorderColor <> TAlphaColors.Null) then
|
||||||
|
begin
|
||||||
|
LRect := TRectF.Create(0, 0, Width, Height);
|
||||||
|
// Inflate inwards so the border is fully visible within the control's bounds
|
||||||
|
LRect.Inflate(-FBorderWidth / 2, -FBorderWidth / 2);
|
||||||
|
|
||||||
|
Canvas.Stroke.Kind := TBrushKind.Solid;
|
||||||
|
Canvas.Stroke.Color := FBorderColor;
|
||||||
|
Canvas.Stroke.Thickness := FBorderWidth;
|
||||||
|
|
||||||
|
// Use DrawRoundRect to support both sharp and rounded corners
|
||||||
|
Canvas.DrawRect(LRect, FBorderRadius, FBorderRadius, AllCorners, 1.0, TCornerType.Round);
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Draw the title
|
||||||
|
if not FTitle.IsEmpty then
|
||||||
|
begin
|
||||||
|
// Define the rectangle for the title at the top of the control
|
||||||
|
LTitleRect := TRectF.Create(0, FBorderWidth, Width, FBorderWidth + FTitleFont.Size * 1.8);
|
||||||
|
|
||||||
|
Canvas.Font.Assign(FTitleFont);
|
||||||
|
Canvas.Fill.Color := FTitleFontColor;
|
||||||
|
|
||||||
|
// Draw the text centered horizontally and vertically within the title rectangle
|
||||||
|
Canvas.FillText(LTitleRect, FTitle, False, 1.0, [], TTextAlign.Center, TTextAlign.Center);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraNode.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single);
|
||||||
|
begin
|
||||||
|
inherited;
|
||||||
|
if (Button = TMouseButton.mbLeft) then
|
||||||
|
begin
|
||||||
|
if (ObjectAtPoint(LocalToScreen(TPointF.Create(X, Y))) = Self as IControl) then
|
||||||
|
begin
|
||||||
|
FIsDragging := True;
|
||||||
|
FDownPos := TPointF.Create(X, Y);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraNode.MouseMove(Shift: TShiftState; X, Y: Single);
|
||||||
|
begin
|
||||||
|
inherited;
|
||||||
|
if FIsDragging then
|
||||||
|
begin
|
||||||
|
var deltaX := X - FDownPos.X;
|
||||||
|
var deltaY := Y - FDownPos.Y;
|
||||||
|
|
||||||
|
Position.X := Position.X + deltaX;
|
||||||
|
Position.Y := Position.Y + deltaY;
|
||||||
|
|
||||||
|
if ParentControl <> nil then
|
||||||
|
ParentControl.Repaint;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraNode.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Single);
|
||||||
|
begin
|
||||||
|
inherited;
|
||||||
|
if (Button = TMouseButton.mbLeft) and (FIsDragging) then
|
||||||
|
begin
|
||||||
|
FIsDragging := False;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
+30
-10
@@ -2,7 +2,7 @@ object Form1: TForm1
|
|||||||
Left = 0
|
Left = 0
|
||||||
Top = 0
|
Top = 0
|
||||||
Caption = 'Form1'
|
Caption = 'Form1'
|
||||||
ClientHeight = 638
|
ClientHeight = 883
|
||||||
ClientWidth = 925
|
ClientWidth = 925
|
||||||
FormFactor.Width = 320
|
FormFactor.Width = 320
|
||||||
FormFactor.Height = 480
|
FormFactor.Height = 480
|
||||||
@@ -10,9 +10,9 @@ object Form1: TForm1
|
|||||||
OnCreate = FormCreate
|
OnCreate = FormCreate
|
||||||
DesignerMasterStyle = 0
|
DesignerMasterStyle = 0
|
||||||
object Panel1: TPanel
|
object Panel1: TPanel
|
||||||
Align = Left
|
Align = MostLeft
|
||||||
Size.Width = 137.000000000000000000
|
Size.Width = 129.000000000000000000
|
||||||
Size.Height = 638.000000000000000000
|
Size.Height = 883.000000000000000000
|
||||||
Size.PlatformDefault = False
|
Size.PlatformDefault = False
|
||||||
TabOrder = 1
|
TabOrder = 1
|
||||||
object Test1Button: TButton
|
object Test1Button: TButton
|
||||||
@@ -89,24 +89,44 @@ object Form1: TForm1
|
|||||||
OnClick = DoTriggerButtonClick
|
OnClick = DoTriggerButtonClick
|
||||||
object DoTrigger2Button: TButton
|
object DoTrigger2Button: TButton
|
||||||
Position.Y = 30.000000000000000000
|
Position.Y = 30.000000000000000000
|
||||||
TabOrder = 8
|
TabOrder = 5
|
||||||
Text = 'Trigger2'
|
Text = 'Trigger2'
|
||||||
TextSettings.Trimming = None
|
TextSettings.Trimming = None
|
||||||
OnClick = DoTrigger2ButtonClick
|
OnClick = DoTrigger2ButtonClick
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
object ClearButton: TButton
|
||||||
|
Position.X = 24.000000000000000000
|
||||||
|
Position.Y = 432.000000000000000000
|
||||||
|
Size.Width = 80.000000000000000000
|
||||||
|
Size.Height = 22.000000000000000000
|
||||||
|
Size.PlatformDefault = False
|
||||||
|
TabOrder = 11
|
||||||
|
Text = 'Clear'
|
||||||
|
TextSettings.Trimming = None
|
||||||
|
OnClick = ClearButtonClick
|
||||||
|
end
|
||||||
|
end
|
||||||
|
object Panel2: TPanel
|
||||||
|
Align = Client
|
||||||
|
Size.Width = 796.000000000000000000
|
||||||
|
Size.Height = 616.000000000000000000
|
||||||
|
Size.PlatformDefault = False
|
||||||
|
TabOrder = 3
|
||||||
end
|
end
|
||||||
object Memo1: TMemo
|
object Memo1: TMemo
|
||||||
Touch.InteractiveGestures = [Pan, LongTap, DoubleTap]
|
Touch.InteractiveGestures = [Pan, LongTap, DoubleTap]
|
||||||
DataDetectorTypes = []
|
DataDetectorTypes = []
|
||||||
StyledSettings = [Size, Style, FontColor]
|
StyledSettings = [Size, Style, FontColor]
|
||||||
TextSettings.Font.Family = 'Consolas'
|
TextSettings.Font.Family = 'Consolas'
|
||||||
Align = Client
|
Align = Bottom
|
||||||
Size.Width = 788.000000000000000000
|
Position.X = 129.000000000000000000
|
||||||
Size.Height = 638.000000000000000000
|
Position.Y = 616.000000000000000000
|
||||||
|
Size.Width = 796.000000000000000000
|
||||||
|
Size.Height = 267.000000000000000000
|
||||||
Size.PlatformDefault = False
|
Size.PlatformDefault = False
|
||||||
TabOrder = 2
|
TabOrder = 2
|
||||||
Viewport.Width = 784.000000000000000000
|
Viewport.Width = 792.000000000000000000
|
||||||
Viewport.Height = 634.000000000000000000
|
Viewport.Height = 263.000000000000000000
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
+50
-13
@@ -8,6 +8,7 @@ uses
|
|||||||
System.UITypes,
|
System.UITypes,
|
||||||
System.Classes,
|
System.Classes,
|
||||||
System.Variants,
|
System.Variants,
|
||||||
|
System.Generics.Collections,
|
||||||
FMX.Types,
|
FMX.Types,
|
||||||
FMX.Controls,
|
FMX.Controls,
|
||||||
FMX.Forms,
|
FMX.Forms,
|
||||||
@@ -18,11 +19,15 @@ uses
|
|||||||
FMX.ScrollBox,
|
FMX.ScrollBox,
|
||||||
FMX.Memo,
|
FMX.Memo,
|
||||||
FMX.Controls.Presentation,
|
FMX.Controls.Presentation,
|
||||||
|
DraggablePanel,
|
||||||
|
Myc.Ast.Visualizer,
|
||||||
Myc.Data.POD,
|
Myc.Data.POD,
|
||||||
Myc.Ast.Nodes,
|
Myc.Ast.Nodes,
|
||||||
Myc.Ast,
|
Myc.Ast,
|
||||||
Myc.Ast.Evaluator,
|
Myc.Ast.Evaluator,
|
||||||
Myc.Ast.Printer; // Added for TExecutionScope
|
Myc.Ast.Printer,
|
||||||
|
FMX.Layouts,
|
||||||
|
FMX.Objects; // Added for TExecutionScope
|
||||||
|
|
||||||
type
|
type
|
||||||
TForm1 = class(TForm)
|
TForm1 = class(TForm)
|
||||||
@@ -38,6 +43,9 @@ type
|
|||||||
CrerateTriggerExampleButton: TButton;
|
CrerateTriggerExampleButton: TButton;
|
||||||
DoTriggerButton: TButton;
|
DoTriggerButton: TButton;
|
||||||
DoTrigger2Button: TButton;
|
DoTrigger2Button: TButton;
|
||||||
|
Panel2: TPanel;
|
||||||
|
ClearButton: TButton;
|
||||||
|
procedure ClearButtonClick(Sender: TObject);
|
||||||
procedure FormCreate(Sender: TObject);
|
procedure FormCreate(Sender: TObject);
|
||||||
procedure CrerateTriggerExampleButtonClick(Sender: TObject);
|
procedure CrerateTriggerExampleButtonClick(Sender: TObject);
|
||||||
procedure DebugButtonClick(Sender: TObject);
|
procedure DebugButtonClick(Sender: TObject);
|
||||||
@@ -52,6 +60,8 @@ type
|
|||||||
// Stores the last AST generated by Test1 or Test2 button clicks.
|
// Stores the last AST generated by Test1 or Test2 button clicks.
|
||||||
FLastAst: IAstNode;
|
FLastAst: IAstNode;
|
||||||
FGScope: IExecutionScope;
|
FGScope: IExecutionScope;
|
||||||
|
FWorkspace: TAuraWorkspace;
|
||||||
|
procedure WorkspaceMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
|
||||||
public
|
public
|
||||||
{ Public declarations }
|
{ Public declarations }
|
||||||
end;
|
end;
|
||||||
@@ -67,8 +77,20 @@ uses
|
|||||||
|
|
||||||
{$R *.fmx}
|
{$R *.fmx}
|
||||||
|
|
||||||
|
procedure TForm1.ClearButtonClick(Sender: TObject);
|
||||||
|
begin
|
||||||
|
FWorkspace.DeleteChildren;
|
||||||
|
FWorkspace.Repaint;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TForm1.FormCreate(Sender: TObject);
|
procedure TForm1.FormCreate(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
|
FWorkspace := TAuraWorkspace.Create(Panel2);
|
||||||
|
FWorkspace.Parent := Panel2;
|
||||||
|
FWorkspace.Align := TAlignLayout.Client;
|
||||||
|
|
||||||
|
FWorkspace.OnMouseDown := WorkspaceMouseDown;
|
||||||
|
|
||||||
FGScope := T_ExecutionScope.Create(nil);
|
FGScope := T_ExecutionScope.Create(nil);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -371,7 +393,6 @@ end;
|
|||||||
|
|
||||||
procedure TForm1.CrerateTriggerExampleButtonClick(Sender: TObject);
|
procedure TForm1.CrerateTriggerExampleButtonClick(Sender: TObject);
|
||||||
var
|
var
|
||||||
lambdaAst: ILambdaExpressionNode;
|
|
||||||
visitor: IAstVisitor;
|
visitor: IAstVisitor;
|
||||||
closureValue: TAstValue;
|
closureValue: TAstValue;
|
||||||
begin
|
begin
|
||||||
@@ -384,26 +405,33 @@ begin
|
|||||||
Memo1.Lines.Clear;
|
Memo1.Lines.Clear;
|
||||||
Memo1.Lines.Add('--- Creating Trigger Blueprint ---');
|
Memo1.Lines.Add('--- Creating Trigger Blueprint ---');
|
||||||
|
|
||||||
// 1. Initialize the variable 'X' to 0 directly in the scope.
|
var blk :=
|
||||||
FGScope.SetValue('X', TAstValue.FromScalar(TScalar.FromInt64(0)));
|
TAst.Block(
|
||||||
|
[
|
||||||
// 2. Create a lambda that takes the summand as an argument.
|
TAst.VarDecl(TAst.Identifier('X'), TAst.Constant(TScalar.FromInt64(0))),
|
||||||
// AST for: (summand) => { X = X + summand; }
|
TAst.VarDecl(
|
||||||
// Using the new Assignment node to modify 'X' in its parent scope.
|
TAst.Identifier('tickHandler'),
|
||||||
lambdaAst :=
|
|
||||||
TAst.LambdaExpr(
|
TAst.LambdaExpr(
|
||||||
[TAst.Identifier('summand')],
|
[TAst.Identifier('summand')],
|
||||||
TAst.Assign(TAst.Identifier('X'), TAst.BinaryExpr(TAst.Identifier('X'), boAdd, TAst.Identifier('summand')))
|
TAst.Assign(TAst.Identifier('X'), TAst.BinaryExpr(TAst.Identifier('X'), boAdd, TAst.Identifier('summand')))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
// 3. Evaluate the lambda to create a closure and store it in the scope.
|
// lambdaAst :=
|
||||||
|
// TAst.LambdaExpr(
|
||||||
|
// [TAst.Identifier('summand')],
|
||||||
|
// TAst.Assign(TAst.Identifier('X'), TAst.BinaryExpr(TAst.Identifier('X'), boAdd, TAst.Identifier('summand')))
|
||||||
|
// );
|
||||||
|
|
||||||
|
// Evaluate the lambda to create a closure and store it in the scope.
|
||||||
// The closure captures the scope where 'X' is defined.
|
// The closure captures the scope where 'X' is defined.
|
||||||
visitor := TEvaluatorVisitor.Create(FGScope);
|
visitor := TEvaluatorVisitor.Create(FGScope);
|
||||||
closureValue := lambdaAst.Accept(visitor);
|
blk.Accept(visitor);
|
||||||
FGScope.SetValue('tickHandler', closureValue);
|
|
||||||
|
|
||||||
// FLastAst is not used for this parameterized example.
|
// FLastAst is not used for this parameterized example.
|
||||||
FLastAst := lambdaAst;
|
FLastAst := blk;
|
||||||
|
|
||||||
Memo1.Lines.Add('Variable "X" initialized to 0 in persistent scope.');
|
Memo1.Lines.Add('Variable "X" initialized to 0 in persistent scope.');
|
||||||
Memo1.Lines.Add('Blueprint function "tickHandler(summand)" created.');
|
Memo1.Lines.Add('Blueprint function "tickHandler(summand)" created.');
|
||||||
@@ -470,4 +498,13 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TForm1.WorkspaceMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
|
||||||
|
begin
|
||||||
|
if Button <> TMouseButton.mbMiddle then
|
||||||
|
exit;
|
||||||
|
|
||||||
|
if FLastAst <> nil then
|
||||||
|
FWorkspace.BuildTree(FLastAst, TPointF.Create(X, Y));
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|||||||
@@ -0,0 +1,711 @@
|
|||||||
|
unit Myc.Ast.Visualizer;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
uses
|
||||||
|
System.SysUtils,
|
||||||
|
System.Classes,
|
||||||
|
System.UITypes,
|
||||||
|
System.Types,
|
||||||
|
System.Generics.Collections,
|
||||||
|
FMX.Types,
|
||||||
|
FMX.Controls,
|
||||||
|
FMX.Objects,
|
||||||
|
FMX.Graphics,
|
||||||
|
Myc.Ast.Nodes,
|
||||||
|
DraggablePanel;
|
||||||
|
|
||||||
|
type
|
||||||
|
// Record to store a connection between two pins
|
||||||
|
TPinConnection = record
|
||||||
|
OutputPin: TControl;
|
||||||
|
InputPin: TControl;
|
||||||
|
constructor Create(AOutputPin, AInputPin: TControl);
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Defines the shape of a pin.
|
||||||
|
TPinShape = (psCircle, psTriangle);
|
||||||
|
// Defines the horizontal alignment of a pin on a node.
|
||||||
|
TPinAlignment = (paLeft, paRight);
|
||||||
|
|
||||||
|
TAuraWorkspace = class;
|
||||||
|
|
||||||
|
TAstToAuraNodeVisitor = class(TInterfacedObject, IAstVisitor)
|
||||||
|
private
|
||||||
|
FWorkspace: TAuraWorkspace;
|
||||||
|
FParentControl: TControl;
|
||||||
|
FCurrentPos: TPointF;
|
||||||
|
FSpacing: TPointF;
|
||||||
|
FLastNode: TAuraNode;
|
||||||
|
FConnections: TList<TPinConnection>;
|
||||||
|
function CreateNodeControl(const ATitle, ADetails: string): TAuraNode;
|
||||||
|
function CreatePin(
|
||||||
|
AParentNode: TAuraNode;
|
||||||
|
AShape: TPinShape;
|
||||||
|
AAlignment: TPinAlignment;
|
||||||
|
AColor: TAlphaColor;
|
||||||
|
const ATag: string;
|
||||||
|
AOffsetY: Single = 0
|
||||||
|
): TControl;
|
||||||
|
function FindPinByTag(AParentNode: TAuraNode; const ATag: string): TControl;
|
||||||
|
function ConnectPins(InputPin: TControl; OutputNode: TAuraNode; const Tag: string): TControl;
|
||||||
|
public
|
||||||
|
constructor Create(
|
||||||
|
AWorkspace: TAuraWorkspace;
|
||||||
|
AParentControl: TControl;
|
||||||
|
const AStartPosition: TPointF;
|
||||||
|
AConnections: TList<TPinConnection>
|
||||||
|
);
|
||||||
|
// Public access to the collected connections
|
||||||
|
property Connections: TList<TPinConnection> read FConnections;
|
||||||
|
|
||||||
|
{ IAstVisitor }
|
||||||
|
function VisitConstant(const Node: IConstantNode): TAstValue;
|
||||||
|
function VisitIdentifier(const Node: IIdentifierNode): TAstValue;
|
||||||
|
function VisitBinaryExpression(const Node: IBinaryExpressionNode): TAstValue;
|
||||||
|
function VisitUnaryExpression(const Node: IUnaryExpressionNode): TAstValue;
|
||||||
|
function VisitIfExpression(const Node: IIfExpressionNode): TAstValue;
|
||||||
|
function VisitLambdaExpression(const Node: ILambdaExpressionNode): TAstValue;
|
||||||
|
function VisitFunctionCall(const Node: IFunctionCallNode): TAstValue;
|
||||||
|
function VisitBlockExpression(const Node: IBlockExpressionNode): TAstValue;
|
||||||
|
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstValue;
|
||||||
|
function VisitAssignment(const Node: IAssignmentNode): TAstValue;
|
||||||
|
end;
|
||||||
|
|
||||||
|
TAuraWorkspace = class(TStyledControl)
|
||||||
|
private
|
||||||
|
FConnections: TArray<TPinConnection>;
|
||||||
|
protected
|
||||||
|
procedure Paint; override;
|
||||||
|
procedure DoDeleteChildren; override;
|
||||||
|
public
|
||||||
|
procedure BuildTree(const Root: IAstNode; const Position: TPointF);
|
||||||
|
|
||||||
|
published
|
||||||
|
// Standard control properties
|
||||||
|
property Align;
|
||||||
|
property Anchors;
|
||||||
|
property ClipChildren default True;
|
||||||
|
property Cursor default crDefault;
|
||||||
|
property DragMode default TDragMode.dmManual;
|
||||||
|
property Enabled;
|
||||||
|
property Height;
|
||||||
|
property HelpContext;
|
||||||
|
property HelpKeyword;
|
||||||
|
property HelpType;
|
||||||
|
property Hint;
|
||||||
|
property HitTest default True;
|
||||||
|
property Locked;
|
||||||
|
property Margins;
|
||||||
|
property Opacity;
|
||||||
|
property Padding;
|
||||||
|
property PopupMenu;
|
||||||
|
property Position;
|
||||||
|
property RotationAngle;
|
||||||
|
property RotationCenter;
|
||||||
|
property Scale;
|
||||||
|
property Size;
|
||||||
|
property StyleLookup;
|
||||||
|
property Visible;
|
||||||
|
property Width;
|
||||||
|
property OnClick;
|
||||||
|
property OnDblClick;
|
||||||
|
property OnMouseDown;
|
||||||
|
property OnMouseMove;
|
||||||
|
property OnMouseUp;
|
||||||
|
property OnMouseWheel;
|
||||||
|
property OnMouseEnter;
|
||||||
|
property OnMouseLeave;
|
||||||
|
end;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
System.Math;
|
||||||
|
|
||||||
|
{ TPinConnection }
|
||||||
|
|
||||||
|
constructor TPinConnection.Create(AOutputPin, AInputPin: TControl);
|
||||||
|
begin
|
||||||
|
OutputPin := AOutputPin;
|
||||||
|
InputPin := AInputPin;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TAstToAuraNodeVisitor }
|
||||||
|
|
||||||
|
constructor TAstToAuraNodeVisitor.Create(
|
||||||
|
AWorkspace: TAuraWorkspace;
|
||||||
|
AParentControl: TControl;
|
||||||
|
const AStartPosition: TPointF;
|
||||||
|
AConnections: TList<TPinConnection>
|
||||||
|
);
|
||||||
|
begin
|
||||||
|
inherited Create;
|
||||||
|
FWorkspace := AWorkspace;
|
||||||
|
FParentControl := AParentControl;
|
||||||
|
FCurrentPos := AStartPosition;
|
||||||
|
FSpacing := TPointF.Create(20, 10); // Horizontal indent, Vertical spacing
|
||||||
|
FConnections := AConnections;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.ConnectPins(InputPin: TControl; OutputNode: TAuraNode; const Tag: string): TControl;
|
||||||
|
begin
|
||||||
|
Result := FindPinByTag(OutputNode, Tag);
|
||||||
|
if Assigned(Result) then
|
||||||
|
FConnections.Add(TPinConnection.Create(Result, InputPin));
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.CreateNodeControl(const ATitle, ADetails: string): TAuraNode;
|
||||||
|
const
|
||||||
|
// Horizontal padding on each side of the title to ensure text and pins are fully visible.
|
||||||
|
cHorizontalPadding = 25;
|
||||||
|
var
|
||||||
|
LFullTitle: string;
|
||||||
|
LTextWidth: Single;
|
||||||
|
LNewWidth: Single;
|
||||||
|
LMeasureCanvas: TCanvas;
|
||||||
|
begin
|
||||||
|
Result := TAuraNode.Create(FParentControl);
|
||||||
|
Result.Parent := FParentControl;
|
||||||
|
|
||||||
|
// Build the full title string first
|
||||||
|
LFullTitle := ATitle;
|
||||||
|
if not ADetails.IsEmpty then
|
||||||
|
LFullTitle := LFullTitle + ': ' + ADetails;
|
||||||
|
Result.Title := LFullTitle;
|
||||||
|
|
||||||
|
Result.Position.Point := FCurrentPos;
|
||||||
|
|
||||||
|
// Adjust the width so that the text and pins are completely visible.
|
||||||
|
LMeasureCanvas := TCanvasManager.MeasureCanvas;
|
||||||
|
if Assigned(LMeasureCanvas) then
|
||||||
|
begin
|
||||||
|
LMeasureCanvas.Font.Assign(Result.TitleFont);
|
||||||
|
LTextWidth := LMeasureCanvas.TextWidth(Result.Title);
|
||||||
|
LNewWidth := LTextWidth + (2 * cHorizontalPadding);
|
||||||
|
// Ensure the new width is not smaller than the default width of the node.
|
||||||
|
Result.Width := Max(Result.Width, LNewWidth);
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Advance vertical position for the next node
|
||||||
|
FCurrentPos.Y := FCurrentPos.Y + Result.Height + FSpacing.Y;
|
||||||
|
|
||||||
|
FLastNode := Result;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.CreatePin(
|
||||||
|
AParentNode: TAuraNode;
|
||||||
|
AShape: TPinShape;
|
||||||
|
AAlignment: TPinAlignment;
|
||||||
|
AColor: TAlphaColor;
|
||||||
|
const ATag: string;
|
||||||
|
AOffsetY: Single
|
||||||
|
): TControl;
|
||||||
|
const
|
||||||
|
cPinSize = 10;
|
||||||
|
var
|
||||||
|
LPosX, LPosY: Single;
|
||||||
|
begin
|
||||||
|
// Calculate vertical position (centered + offset)
|
||||||
|
LPosY := (AParentNode.Height / 2) - (cPinSize / 2) + AOffsetY;
|
||||||
|
|
||||||
|
// Calculate horizontal position
|
||||||
|
if (AAlignment = paLeft) then
|
||||||
|
LPosX := 0
|
||||||
|
else // paRight
|
||||||
|
LPosX := AParentNode.Width - cPinSize;
|
||||||
|
|
||||||
|
// Create the shape
|
||||||
|
case AShape of
|
||||||
|
psCircle:
|
||||||
|
begin
|
||||||
|
var LCircle := TEllipse.Create(AParentNode);
|
||||||
|
LCircle.Fill.Color := AColor;
|
||||||
|
LCircle.Stroke.Kind := TBrushKind.None;
|
||||||
|
Result := LCircle;
|
||||||
|
end;
|
||||||
|
psTriangle:
|
||||||
|
begin
|
||||||
|
var LTriangle := TPath.Create(AParentNode);
|
||||||
|
LTriangle.Data.Data := Format('M 0 0 L %d %d L 0 %d Z', [cPinSize, cPinSize div 2, cPinSize]);
|
||||||
|
LTriangle.Fill.Color := AColor;
|
||||||
|
LTriangle.Stroke.Kind := TBrushKind.None;
|
||||||
|
Result := LTriangle;
|
||||||
|
end;
|
||||||
|
else
|
||||||
|
Result := nil;
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Common properties
|
||||||
|
Result.Parent := AParentNode;
|
||||||
|
Result.SetBounds(LPosX, LPosY, cPinSize, cPinSize);
|
||||||
|
Result.HitTest := False;
|
||||||
|
|
||||||
|
// Use TagString to identify the control as a pin and describe its function.
|
||||||
|
Result.TagString := ATag;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.FindPinByTag(AParentNode: TAuraNode; const ATag: string): TControl;
|
||||||
|
var
|
||||||
|
LControl: TControl;
|
||||||
|
begin
|
||||||
|
Result := nil;
|
||||||
|
if not Assigned(AParentNode) then
|
||||||
|
Exit;
|
||||||
|
|
||||||
|
for LControl in AParentNode.Controls do
|
||||||
|
begin
|
||||||
|
if (LControl.TagString = ATag) then
|
||||||
|
begin
|
||||||
|
Result := LControl;
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.VisitAssignment(const Node: IAssignmentNode): TAstValue;
|
||||||
|
const
|
||||||
|
cExecPinColor = TAlphaColors.Lightgreen;
|
||||||
|
cDataPinColor = TAlphaColors.Dodgerblue;
|
||||||
|
var
|
||||||
|
LAssignmentNode, LValueNode: TAuraNode;
|
||||||
|
LInputPin: TControl;
|
||||||
|
LStartPosition: TPointF;
|
||||||
|
begin
|
||||||
|
LStartPosition := FCurrentPos;
|
||||||
|
|
||||||
|
// Visit the value expression first
|
||||||
|
Node.Value.Accept(Self);
|
||||||
|
LValueNode := FLastNode;
|
||||||
|
|
||||||
|
// Reposition for the assignment node
|
||||||
|
FCurrentPos.X := LValueNode.Position.X + LValueNode.Width + FSpacing.X;
|
||||||
|
FCurrentPos.Y := LValueNode.Position.Y;
|
||||||
|
|
||||||
|
// Create the assignment node and its pins
|
||||||
|
LAssignmentNode := CreateNodeControl('Assignment', Node.Identifier.Name);
|
||||||
|
CreatePin(LAssignmentNode, psTriangle, paLeft, cExecPinColor, 'Pin.Exec.In', -8);
|
||||||
|
CreatePin(LAssignmentNode, psTriangle, paRight, cExecPinColor, 'Pin.Exec.Out');
|
||||||
|
LInputPin := CreatePin(LAssignmentNode, psCircle, paLeft, cDataPinColor, 'Pin.Data.In', 8);
|
||||||
|
|
||||||
|
// Connect the value to the assignment
|
||||||
|
ConnectPins(LInputPin, LValueNode, 'Pin.Data.Out');
|
||||||
|
|
||||||
|
// Finalize visitor state
|
||||||
|
FCurrentPos.X := LStartPosition.X;
|
||||||
|
FCurrentPos.Y := Max(LValueNode.Position.Y + LValueNode.Height, LAssignmentNode.Position.Y + LAssignmentNode.Height) + FSpacing.Y;
|
||||||
|
FLastNode := LAssignmentNode;
|
||||||
|
|
||||||
|
Result := TAstValue.Void;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.VisitBinaryExpression(const Node: IBinaryExpressionNode): TAstValue;
|
||||||
|
const
|
||||||
|
cDataPinColor = TAlphaColors.Dodgerblue;
|
||||||
|
cDefaultHeight = 45;
|
||||||
|
var
|
||||||
|
LBinaryExprNode, LLeftNode, LRightNode: TAuraNode;
|
||||||
|
LInputPin1, LInputPin2: TControl;
|
||||||
|
LStartPosition: TPointF;
|
||||||
|
LRightEndY, LMaxChildWidth: Single;
|
||||||
|
begin
|
||||||
|
LStartPosition := FCurrentPos;
|
||||||
|
|
||||||
|
// First, visit the child nodes to create their visual representation
|
||||||
|
// Left branch
|
||||||
|
Node.Left.Accept(Self);
|
||||||
|
LLeftNode := FLastNode;
|
||||||
|
|
||||||
|
// Right branch starts below the left branch
|
||||||
|
Node.Right.Accept(Self);
|
||||||
|
LRightNode := FLastNode;
|
||||||
|
LRightEndY := FCurrentPos.Y;
|
||||||
|
|
||||||
|
// Now, create the node for the binary operation itself
|
||||||
|
LMaxChildWidth := Max(LLeftNode.Position.X + LLeftNode.Width, LRightNode.Position.X + LRightNode.Width);
|
||||||
|
FCurrentPos.X := LMaxChildWidth + FSpacing.X;
|
||||||
|
|
||||||
|
// Vertically center the binary expression node between its children
|
||||||
|
var LTotalChildHeight := LRightEndY - LStartPosition.Y;
|
||||||
|
var LNodeCenterY := LStartPosition.Y + (LTotalChildHeight / 2);
|
||||||
|
FCurrentPos.Y := LNodeCenterY - (cDefaultHeight / 2);
|
||||||
|
|
||||||
|
LBinaryExprNode := CreateNodeControl(Node.Operator.ToString, '');
|
||||||
|
|
||||||
|
// Create pins for the binary expression node
|
||||||
|
LInputPin1 := CreatePin(LBinaryExprNode, psCircle, paLeft, cDataPinColor, 'Pin.Data.In.1', -8);
|
||||||
|
LInputPin2 := CreatePin(LBinaryExprNode, psCircle, paLeft, cDataPinColor, 'Pin.Data.In.2', 8);
|
||||||
|
CreatePin(LBinaryExprNode, psCircle, paRight, cDataPinColor, 'Pin.Data.Out', 0);
|
||||||
|
|
||||||
|
// Connect the output of the left child to the first input of the binary node
|
||||||
|
ConnectPins(LInputPin1, LLeftNode, 'Pin.Data.Out');
|
||||||
|
|
||||||
|
// Connect the output of the right child to the second input of the binary node
|
||||||
|
ConnectPins(LInputPin2, LRightNode, 'Pin.Data.Out');
|
||||||
|
|
||||||
|
// Finalize visitor state for the next node at this level
|
||||||
|
FCurrentPos.X := LStartPosition.X;
|
||||||
|
FCurrentPos.Y := Max(LRightEndY, LBinaryExprNode.Position.Y + LBinaryExprNode.Height + FSpacing.Y);
|
||||||
|
FLastNode := LBinaryExprNode;
|
||||||
|
|
||||||
|
Result := TAstValue.Void;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.VisitBlockExpression(const Node: IBlockExpressionNode): TAstValue;
|
||||||
|
var
|
||||||
|
Expression: IExpressionNode;
|
||||||
|
LBlockNode: TAuraNode;
|
||||||
|
LChildVisitor: IAstVisitor;
|
||||||
|
LChildStartPos: TPointF;
|
||||||
|
LMaxRight: Single;
|
||||||
|
LMaxBottom: Single;
|
||||||
|
LControl: TControl;
|
||||||
|
begin
|
||||||
|
// A block node visually encloses all its child nodes.
|
||||||
|
// Create the container node for the block. Its size will be adjusted later.
|
||||||
|
LBlockNode := TAuraNode.Create(FParentControl);
|
||||||
|
LBlockNode.Parent := FParentControl;
|
||||||
|
LBlockNode.Title := 'Block';
|
||||||
|
LBlockNode.Position.Point := FCurrentPos;
|
||||||
|
|
||||||
|
// Create a new visitor for the child nodes. The children will be parented
|
||||||
|
// to LBlockNode and positioned relative to it, starting below the title.
|
||||||
|
LChildStartPos := TPointF.Create(FSpacing.X, FSpacing.Y + LBlockNode.TitleFont.Size * 1.8);
|
||||||
|
LChildVisitor := TAstToAuraNodeVisitor.Create(FWorkspace, LBlockNode, LChildStartPos, FConnections);
|
||||||
|
|
||||||
|
// Visit all expressions within the block using the new visitor.
|
||||||
|
for Expression in Node.Expressions do
|
||||||
|
begin
|
||||||
|
Expression.Accept(LChildVisitor);
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Adjust the size of the block node to encompass all its children.
|
||||||
|
LMaxRight := 0;
|
||||||
|
LMaxBottom := 0;
|
||||||
|
for LControl in LBlockNode.Controls do
|
||||||
|
begin
|
||||||
|
if (LControl is TAuraNode) then
|
||||||
|
begin
|
||||||
|
LMaxRight := Max(LMaxRight, LControl.Position.X + LControl.Width);
|
||||||
|
LMaxBottom := Max(LMaxBottom, LControl.Position.Y + LControl.Height);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Set the final size with some internal padding.
|
||||||
|
LBlockNode.Width := Max(LBlockNode.Width, LMaxRight + FSpacing.X);
|
||||||
|
LBlockNode.Height := Max(LBlockNode.Height, LMaxBottom + FSpacing.Y);
|
||||||
|
|
||||||
|
// Update the current Y position of the main visitor to be below the now correctly sized block node.
|
||||||
|
FCurrentPos.Y := LBlockNode.Position.Y + LBlockNode.Height + FSpacing.Y;
|
||||||
|
FLastNode := LBlockNode;
|
||||||
|
|
||||||
|
Result := TAstValue.Void;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.VisitConstant(const Node: IConstantNode): TAstValue;
|
||||||
|
const
|
||||||
|
cDataPinColor = TAlphaColors.Dodgerblue;
|
||||||
|
var
|
||||||
|
LConstantNode: TAuraNode;
|
||||||
|
begin
|
||||||
|
// A constant has a data output pin.
|
||||||
|
// Data outputs are round and on the right side of the box.
|
||||||
|
LConstantNode := CreateNodeControl('Constant', Node.Value.ToString);
|
||||||
|
CreatePin(LConstantNode, psCircle, paRight, cDataPinColor, 'Pin.Data.Out', 0);
|
||||||
|
Result := TAstValue.Void;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.VisitFunctionCall(const Node: IFunctionCallNode): TAstValue;
|
||||||
|
var
|
||||||
|
Arg: IExpressionNode;
|
||||||
|
begin
|
||||||
|
CreateNodeControl('FunctionCall', '');
|
||||||
|
FCurrentPos.X := FCurrentPos.X + FSpacing.X;
|
||||||
|
try
|
||||||
|
// Callee
|
||||||
|
CreateNodeControl('Callee', '');
|
||||||
|
FCurrentPos.X := FCurrentPos.X + FSpacing.X;
|
||||||
|
try
|
||||||
|
Node.Callee.Accept(Self);
|
||||||
|
finally
|
||||||
|
FCurrentPos.X := FCurrentPos.X - FSpacing.X;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Arguments
|
||||||
|
if Node.Arguments.Count > 0 then
|
||||||
|
begin
|
||||||
|
CreateNodeControl('Arguments', '');
|
||||||
|
FCurrentPos.X := FCurrentPos.X + FSpacing.X;
|
||||||
|
try
|
||||||
|
for Arg in Node.Arguments do
|
||||||
|
Arg.Accept(Self);
|
||||||
|
finally
|
||||||
|
FCurrentPos.X := FCurrentPos.X - FSpacing.X;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
finally
|
||||||
|
FCurrentPos.X := FCurrentPos.X - FSpacing.X;
|
||||||
|
end;
|
||||||
|
Result := TAstValue.Void;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.VisitIdentifier(const Node: IIdentifierNode): TAstValue;
|
||||||
|
const
|
||||||
|
cDataPinColor = TAlphaColors.Dodgerblue;
|
||||||
|
begin
|
||||||
|
CreateNodeControl('Identifier', Node.Name);
|
||||||
|
// Add a data output pin on the right side.
|
||||||
|
CreatePin(FLastNode, psCircle, paRight, cDataPinColor, 'Pin.Data.Out');
|
||||||
|
Result := TAstValue.Void;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.VisitIfExpression(const Node: IIfExpressionNode): TAstValue;
|
||||||
|
const
|
||||||
|
cExecPinColor = TAlphaColors.Lightgreen;
|
||||||
|
cDataPinColor = TAlphaColors.Dodgerblue;
|
||||||
|
cDefaultHeight = 60; // Taller to accommodate more pins
|
||||||
|
var
|
||||||
|
LIfNode, LConditionNode, LThenNode, LElseNode: TAuraNode;
|
||||||
|
LConditionInputPin, LThenOutputPin, LElseOutputPin: TControl;
|
||||||
|
LStartPosition: TPointF;
|
||||||
|
LConditionEndY, LThenEndY, LElseEndY, LMaxChildWidth: Single;
|
||||||
|
begin
|
||||||
|
LStartPosition := FCurrentPos;
|
||||||
|
|
||||||
|
// Place the input node for the condition to the left of the 'If' node.
|
||||||
|
Node.Condition.Accept(Self);
|
||||||
|
LConditionNode := FLastNode;
|
||||||
|
LConditionEndY := FCurrentPos.Y;
|
||||||
|
|
||||||
|
// Position and create the 'If' node to the right of the condition's subtree.
|
||||||
|
LMaxChildWidth := LConditionNode.Position.X + LConditionNode.Width;
|
||||||
|
FCurrentPos.X := LMaxChildWidth + FSpacing.X;
|
||||||
|
FCurrentPos.Y := LStartPosition.Y + (LConditionNode.Position.Y + LConditionNode.Height / 2) - LStartPosition.Y - (cDefaultHeight / 2);
|
||||||
|
|
||||||
|
LIfNode := CreateNodeControl('If', '');
|
||||||
|
LIfNode.Height := cDefaultHeight;
|
||||||
|
CreatePin(LIfNode, psTriangle, paLeft, cExecPinColor, 'Pin.Exec.In', -12);
|
||||||
|
LConditionInputPin := CreatePin(LIfNode, psCircle, paLeft, cDataPinColor, 'Pin.Data.In.Condition', 12);
|
||||||
|
LThenOutputPin := CreatePin(LIfNode, psTriangle, paRight, cExecPinColor, 'Pin.Exec.Out.Then', -12);
|
||||||
|
if Assigned(Node.ElseBranch) then
|
||||||
|
LElseOutputPin := CreatePin(LIfNode, psTriangle, paRight, cExecPinColor, 'Pin.Exec.Out.Else', 12)
|
||||||
|
else
|
||||||
|
LElseOutputPin := nil;
|
||||||
|
|
||||||
|
// Connect the condition's output to the 'If' node's data input.
|
||||||
|
ConnectPins(LConditionInputPin, LConditionNode, 'Pin.Data.Out');
|
||||||
|
|
||||||
|
// Visit the 'Then' and 'Else' execution branches, placing them to the right of the 'If' node.
|
||||||
|
var LBranchStartX := LIfNode.Position.X + LIfNode.Width + FSpacing.X;
|
||||||
|
|
||||||
|
// Then branch
|
||||||
|
FCurrentPos.X := LBranchStartX;
|
||||||
|
FCurrentPos.Y := LStartPosition.Y;
|
||||||
|
Node.ThenBranch.Accept(Self);
|
||||||
|
LThenNode := FLastNode;
|
||||||
|
LThenEndY := FCurrentPos.Y;
|
||||||
|
// ConnectPins(FindPinByTag(LThenNode, 'Pin.Exec.In'), LIfNode, 'Pin.Exec.Out.Then');
|
||||||
|
|
||||||
|
// Else branch (if it exists)
|
||||||
|
LElseEndY := LThenEndY;
|
||||||
|
if Assigned(Node.ElseBranch) then
|
||||||
|
begin
|
||||||
|
FCurrentPos.X := LBranchStartX;
|
||||||
|
FCurrentPos.Y := LThenEndY;
|
||||||
|
Node.ElseBranch.Accept(Self);
|
||||||
|
LElseNode := FLastNode;
|
||||||
|
LElseEndY := FCurrentPos.Y;
|
||||||
|
// ConnectPins(FindPinByTag(LElseNode, 'Pin.Exec.In'), LIfNode, 'Pin.Exec.Out.Else');
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Finalize visitor state.
|
||||||
|
FCurrentPos.X := LStartPosition.X;
|
||||||
|
FCurrentPos.Y := Max(LConditionEndY, LElseEndY);
|
||||||
|
FLastNode := LIfNode;
|
||||||
|
|
||||||
|
Result := TAstValue.Void;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.VisitLambdaExpression(const Node: ILambdaExpressionNode): TAstValue;
|
||||||
|
const
|
||||||
|
cExecPinColor = TAlphaColors.Lightgreen;
|
||||||
|
cDataPinColor = TAlphaColors.Dodgerblue;
|
||||||
|
var
|
||||||
|
ParamStr: String;
|
||||||
|
LLambdaNode: TAuraNode;
|
||||||
|
LChildVisitor: IAstVisitor;
|
||||||
|
LChildStartPos: TPointF;
|
||||||
|
LMaxRight, LMaxBottom: Single;
|
||||||
|
LControl: TControl;
|
||||||
|
i: Integer;
|
||||||
|
begin
|
||||||
|
// Prepare the parameter string for the title
|
||||||
|
ParamStr := '(';
|
||||||
|
if Length(Node.Parameters) > 0 then
|
||||||
|
begin
|
||||||
|
ParamStr := ParamStr + Node.Parameters[0].Name;
|
||||||
|
for i := 1 to High(Node.Parameters) do
|
||||||
|
ParamStr := ParamStr + ', ' + Node.Parameters[i].Name;
|
||||||
|
end;
|
||||||
|
ParamStr := ParamStr + ')';
|
||||||
|
|
||||||
|
// Create the container node for the lambda. Its size will be adjusted later.
|
||||||
|
LLambdaNode := CreateNodeControl('Lambda', ParamStr);
|
||||||
|
|
||||||
|
// Create a new visitor for the child nodes (the lambda body).
|
||||||
|
// The children will be parented to LLambdaNode and positioned relative to it.
|
||||||
|
LChildStartPos := TPointF.Create(FSpacing.X, FSpacing.Y + LLambdaNode.TitleFont.Size * 1.8);
|
||||||
|
LChildVisitor := TAstToAuraNodeVisitor.Create(FWorkspace, LLambdaNode, LChildStartPos, FConnections);
|
||||||
|
|
||||||
|
// Visit the body expression using the new visitor.
|
||||||
|
Node.Body.Accept(LChildVisitor);
|
||||||
|
|
||||||
|
// Adjust the size of the lambda node to encompass its body.
|
||||||
|
LMaxRight := 0;
|
||||||
|
LMaxBottom := 0;
|
||||||
|
for LControl in LLambdaNode.Controls do
|
||||||
|
begin
|
||||||
|
if (LControl is TAuraNode) then
|
||||||
|
begin
|
||||||
|
LMaxRight := Max(LMaxRight, LControl.Position.X + LControl.Width);
|
||||||
|
LMaxBottom := Max(LMaxBottom, LControl.Position.Y + LControl.Height);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Set the final size with some internal padding.
|
||||||
|
LLambdaNode.Width := Max(LLambdaNode.Width, LMaxRight + FSpacing.X);
|
||||||
|
LLambdaNode.Height := Max(LLambdaNode.Height, LMaxBottom + FSpacing.Y);
|
||||||
|
|
||||||
|
// Add a data output pin for the resulting closure.
|
||||||
|
CreatePin(LLambdaNode, psCircle, paRight, cDataPinColor, 'Pin.Data.Out');
|
||||||
|
|
||||||
|
// Update the current Y position of the main visitor to be below the now correctly sized lambda node.
|
||||||
|
FCurrentPos.Y := LLambdaNode.Position.Y + LLambdaNode.Height + FSpacing.Y;
|
||||||
|
|
||||||
|
Result := TAstValue.Void;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.VisitUnaryExpression(const Node: IUnaryExpressionNode): TAstValue;
|
||||||
|
begin
|
||||||
|
CreateNodeControl('UnaryExpr', Node.Operator.ToString);
|
||||||
|
FCurrentPos.X := FCurrentPos.X + FSpacing.X;
|
||||||
|
try
|
||||||
|
Node.Right.Accept(Self);
|
||||||
|
finally
|
||||||
|
FCurrentPos.X := FCurrentPos.X - FSpacing.X;
|
||||||
|
end;
|
||||||
|
Result := TAstValue.Void;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TAstToAuraNodeVisitor.VisitVariableDeclaration(const Node: IVariableDeclarationNode): TAstValue;
|
||||||
|
const
|
||||||
|
cExecPinColor = TAlphaColors.Lightgreen;
|
||||||
|
cDataPinColor = TAlphaColors.Dodgerblue;
|
||||||
|
cInitializerTreeWidth = 150;
|
||||||
|
var
|
||||||
|
LVarDeclNode, LInitializerNode: TAuraNode;
|
||||||
|
LStartPosition: TPointF;
|
||||||
|
LInitializerEndY, LVarDeclNodeEndY: Single;
|
||||||
|
LInputPin: TControl;
|
||||||
|
begin
|
||||||
|
LStartPosition := FCurrentPos;
|
||||||
|
LInitializerEndY := LStartPosition.Y;
|
||||||
|
LInitializerNode := nil;
|
||||||
|
|
||||||
|
if Assigned(Node.Initializer) then
|
||||||
|
begin
|
||||||
|
Node.Initializer.Accept(Self);
|
||||||
|
|
||||||
|
LInitializerNode := FLastNode;
|
||||||
|
LInitializerEndY := FCurrentPos.Y;
|
||||||
|
FCurrentPos.X := LStartPosition.X + cInitializerTreeWidth + FSpacing.X;
|
||||||
|
FCurrentPos.Y := LStartPosition.Y;
|
||||||
|
end;
|
||||||
|
|
||||||
|
LVarDeclNode := CreateNodeControl('VarDecl', Node.Identifier.Name);
|
||||||
|
LVarDeclNodeEndY := FCurrentPos.Y;
|
||||||
|
|
||||||
|
CreatePin(LVarDeclNode, psTriangle, paLeft, cExecPinColor, 'Pin.Exec.In', -8);
|
||||||
|
CreatePin(LVarDeclNode, psTriangle, paRight, cExecPinColor, 'Pin.Exec.Out', -8);
|
||||||
|
|
||||||
|
if Assigned(Node.Initializer) then
|
||||||
|
begin
|
||||||
|
LInputPin := CreatePin(LVarDeclNode, psCircle, paLeft, cDataPinColor, 'Pin.Data.In', 8);
|
||||||
|
ConnectPins(LInputPin, LInitializerNode, 'Pin.Data.Out');
|
||||||
|
|
||||||
|
FCurrentPos.Y := Max(LInitializerEndY, LVarDeclNodeEndY);
|
||||||
|
FCurrentPos.X := LStartPosition.X;
|
||||||
|
end;
|
||||||
|
FLastNode := LVarDeclNode;
|
||||||
|
|
||||||
|
Result := TAstValue.Void;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TAuraWorkspace }
|
||||||
|
|
||||||
|
procedure TAuraWorkspace.BuildTree(const Root: IAstNode; const Position: TPointF);
|
||||||
|
begin
|
||||||
|
var Connections := TList<TPinConnection>.Create;
|
||||||
|
try
|
||||||
|
Root.Accept(TAstToAuraNodeVisitor.Create(Self, Self, Position, Connections));
|
||||||
|
FConnections := FConnections + Connections.ToArray;
|
||||||
|
finally
|
||||||
|
Connections.Free;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraWorkspace.DoDeleteChildren;
|
||||||
|
begin
|
||||||
|
FConnections := nil;
|
||||||
|
inherited;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TAuraWorkspace.Paint;
|
||||||
|
var
|
||||||
|
LConnection: TPinConnection;
|
||||||
|
LStartPoint, LEndPoint, LPinCenter: TPointF;
|
||||||
|
LPath: TPathData;
|
||||||
|
LControlPoint1, LControlPoint2: TPointF;
|
||||||
|
LDeltaX, LControlOffset: Single;
|
||||||
|
begin
|
||||||
|
inherited;
|
||||||
|
|
||||||
|
Canvas.Stroke.Kind := TBrushKind.Solid;
|
||||||
|
Canvas.Stroke.Color := TAlphaColors.Dodgerblue;
|
||||||
|
Canvas.Stroke.Thickness := 2;
|
||||||
|
|
||||||
|
// Gehe durch alle gespeicherten Verbindungen und zeichne sie
|
||||||
|
for LConnection in FConnections do
|
||||||
|
begin
|
||||||
|
// Hole die absoluten Koordinaten der Pin-Mittelpunkte
|
||||||
|
LPinCenter := TPointF.Create(LConnection.OutputPin.Width / 2, LConnection.OutputPin.Height / 2);
|
||||||
|
var LAbsoluteStart := LConnection.OutputPin.LocalToAbsolute(LPinCenter);
|
||||||
|
|
||||||
|
LPinCenter := TPointF.Create(LConnection.InputPin.Width / 2, LConnection.InputPin.Height / 2);
|
||||||
|
var LAbsoluteEnd := LConnection.InputPin.LocalToAbsolute(LPinCenter);
|
||||||
|
|
||||||
|
// Rechne sie in die lokalen Koordinaten der PaintBox um
|
||||||
|
LStartPoint := AbsoluteToLocal(LAbsoluteStart);
|
||||||
|
LEndPoint := AbsoluteToLocal(LAbsoluteEnd);
|
||||||
|
|
||||||
|
// Draw a Bezier curve with horizontal tangents at start and end points.
|
||||||
|
LPath := TPathData.Create;
|
||||||
|
try
|
||||||
|
LDeltaX := LEndPoint.X - LStartPoint.X;
|
||||||
|
LControlOffset := Max(50, Abs(LDeltaX) / 2);
|
||||||
|
|
||||||
|
LControlPoint1 := TPointF.Create(LStartPoint.X + LControlOffset, LStartPoint.Y);
|
||||||
|
LControlPoint2 := TPointF.Create(LEndPoint.X - LControlOffset, LEndPoint.Y);
|
||||||
|
|
||||||
|
LPath.MoveTo(LStartPoint);
|
||||||
|
LPath.CurveTo(LControlPoint1, LControlPoint2, LEndPoint);
|
||||||
|
|
||||||
|
Canvas.DrawPath(LPath, 1.0);
|
||||||
|
finally
|
||||||
|
LPath.Free;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -201,14 +201,14 @@ uses
|
|||||||
System.Classes;
|
System.Classes;
|
||||||
|
|
||||||
type
|
type
|
||||||
// Private interface to encapsulate a string value for reference counting.
|
// Private interface to encapsulate a non scalar value for reference counting.
|
||||||
IAstTextValue = interface(IInterface)
|
IAstValue<T> = interface
|
||||||
function GetValue: string;
|
function GetValue: string;
|
||||||
property Value: string read GetValue;
|
property Value: string read GetValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Implementation of the text value interface.
|
// Implementation of the text value interface.
|
||||||
TAstTextValue = class(TInterfacedObject, IAstTextValue)
|
TAstValue<T> = class(TInterfacedObject, IAstValue<T>)
|
||||||
private
|
private
|
||||||
FValue: string;
|
FValue: string;
|
||||||
function GetValue: string;
|
function GetValue: string;
|
||||||
@@ -216,15 +216,15 @@ type
|
|||||||
constructor Create(const AValue: string);
|
constructor Create(const AValue: string);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TAstTextValue }
|
{ TAstValue }
|
||||||
|
|
||||||
constructor TAstTextValue.Create(const AValue: string);
|
constructor TAstValue<T>.Create(const AValue: string);
|
||||||
begin
|
begin
|
||||||
inherited Create;
|
inherited Create;
|
||||||
FValue := AValue;
|
FValue := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TAstTextValue.GetValue: string;
|
function TAstValue<T>.GetValue: string;
|
||||||
begin
|
begin
|
||||||
Result := FValue;
|
Result := FValue;
|
||||||
end;
|
end;
|
||||||
@@ -255,7 +255,7 @@ function TAstValue.AsText: String;
|
|||||||
begin
|
begin
|
||||||
if (FKind <> avkText) then
|
if (FKind <> avkText) then
|
||||||
raise EInvalidCast.Create('Cannot read value as Text.');
|
raise EInvalidCast.Create('Cannot read value as Text.');
|
||||||
Result := IAstTextValue(FInterface).Value;
|
Result := IAstValue<String>(FInterface).Value;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TAstValue.FromClosure(const AValue: IEvaluatorClosure): TAstValue;
|
class function TAstValue.FromClosure(const AValue: IEvaluatorClosure): TAstValue;
|
||||||
@@ -275,7 +275,7 @@ end;
|
|||||||
class function TAstValue.FromText(const AValue: String): TAstValue;
|
class function TAstValue.FromText(const AValue: String): TAstValue;
|
||||||
begin
|
begin
|
||||||
Result.FKind := avkText;
|
Result.FKind := avkText;
|
||||||
Result.FInterface := TAstTextValue.Create(AValue);
|
Result.FInterface := TAstValue<String>.Create(AValue);
|
||||||
Result.FScalar := Default(TScalar);
|
Result.FScalar := Default(TScalar);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|||||||
+147
-266
@@ -8,7 +8,7 @@ uses
|
|||||||
Myc.Data.Series;
|
Myc.Data.Series;
|
||||||
|
|
||||||
type
|
type
|
||||||
// POD: Plain old data (...that fits into 128 bits)
|
// POD: Plain old data (...that fits into 64 bits)
|
||||||
|
|
||||||
TScalarKind = (
|
TScalarKind = (
|
||||||
skInteger,
|
skInteger,
|
||||||
@@ -26,9 +26,9 @@ type
|
|||||||
skDecimal
|
skDecimal
|
||||||
);
|
);
|
||||||
|
|
||||||
TScalarBytes = array[0..15] of Byte;
|
TScalarBytes = array[0..7] of Byte;
|
||||||
TScalarPChar = array[0..7] of Char;
|
TScalarPChar = array[0..3] of Char;
|
||||||
TScalarString = String[15];
|
TScalarString = String[7];
|
||||||
|
|
||||||
TScalarValue = record
|
TScalarValue = record
|
||||||
public
|
public
|
||||||
@@ -49,54 +49,29 @@ type
|
|||||||
class function FromBytes(const AValue: TScalarBytes): TScalarValue; static; inline;
|
class function FromBytes(const AValue: TScalarBytes): TScalarValue; static; inline;
|
||||||
class function FromDecimal(AValue: TDecimal): TScalarValue; static; inline;
|
class function FromDecimal(AValue: TDecimal): TScalarValue; static; inline;
|
||||||
|
|
||||||
function GetAsInteger: Integer; inline;
|
// Direct field access for performance.
|
||||||
function GetAsInt64: Int64; inline;
|
|
||||||
function GetAsUInt64: UInt64; inline;
|
|
||||||
function GetAsSingle: Single; inline;
|
|
||||||
function GetAsDouble: Double; inline;
|
|
||||||
function GetAsDateTime: TDateTime; inline;
|
|
||||||
function GetAsTimestamp: TTimestamp; inline;
|
|
||||||
function GetAsBoolean: Boolean; inline;
|
|
||||||
function GetAsChar: Char; inline;
|
|
||||||
function GetAsPChar: PChar; inline;
|
|
||||||
function GetAsString: ShortString; inline;
|
|
||||||
function GetAsBytes: TScalarBytes; inline;
|
|
||||||
function GetAsDecimal: TDecimal; inline;
|
|
||||||
|
|
||||||
property AsInteger: Integer read GetAsInteger;
|
|
||||||
property AsInt64: Int64 read GetAsInt64;
|
|
||||||
property AsUInt64: UInt64 read GetAsUInt64;
|
|
||||||
property AsSingle: Single read GetAsSingle;
|
|
||||||
property AsDouble: Double read GetAsDouble;
|
|
||||||
property AsDateTime: TDateTime read GetAsDateTime;
|
|
||||||
property AsTimestamp: TTimestamp read GetAsTimestamp;
|
|
||||||
property AsBoolean: Boolean read GetAsBoolean;
|
|
||||||
property AsChar: Char read GetAsChar;
|
|
||||||
property AsPChar: PChar read GetAsPChar;
|
|
||||||
property AsString: ShortString read GetAsString;
|
|
||||||
property AsBytes: TScalarBytes read GetAsBytes;
|
|
||||||
property AsDecimal: TDecimal read GetAsDecimal;
|
|
||||||
|
|
||||||
strict private
|
|
||||||
case TScalarKind of
|
case TScalarKind of
|
||||||
skInteger: (FInteger: Integer);
|
skInteger: (AsInteger: Integer);
|
||||||
skInt64: (FInt64: Int64);
|
skInt64: (AsInt64: Int64);
|
||||||
skUInt64: (FUInt64: UInt64);
|
skUInt64: (AsUInt64: UInt64);
|
||||||
skSingle: (FSingle: Single);
|
skSingle: (AsSingle: Single);
|
||||||
skDouble: (FDouble: Double);
|
skDouble: (AsDouble: Double);
|
||||||
skDateTime: (FDateTime: TDateTime);
|
skDateTime: (AsDateTime: TDateTime);
|
||||||
skTimestamp: (FTimestamp: TTimestamp);
|
skTimestamp: (AsTimestamp: TTimestamp);
|
||||||
skBoolean: (FBoolean: Boolean);
|
skBoolean: (AsBoolean: Boolean);
|
||||||
skChar: (FChar: Char);
|
skChar: (AsChar: Char);
|
||||||
skPChar: (FPChar: TScalarPChar);
|
skPChar: (AsPChar: TScalarPChar);
|
||||||
skString: (FString: String[15]);
|
skString: (AsString: String[7]);
|
||||||
skBytes: (FBytes: TScalarBytes);
|
skBytes: (AsBytes: TScalarBytes);
|
||||||
skDecimal: (FDecimal: TDecimal);
|
skDecimal: (AsDecimal: TDecimal);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// A scalar value with a type identifier.
|
// A scalar value with a type identifier.
|
||||||
TScalar = record
|
TScalar = record
|
||||||
public
|
public
|
||||||
|
Kind: TScalarKind;
|
||||||
|
Value: TScalarValue;
|
||||||
|
|
||||||
// Creates a new scalar from a kind and a value.
|
// Creates a new scalar from a kind and a value.
|
||||||
constructor Create(AKind: TScalarKind; const AValue: TScalarValue);
|
constructor Create(AKind: TScalarKind; const AValue: TScalarValue);
|
||||||
|
|
||||||
@@ -113,19 +88,9 @@ type
|
|||||||
class function FromPChar(const AValue: TScalarPChar): TScalar; static; inline;
|
class function FromPChar(const AValue: TScalarPChar): TScalar; static; inline;
|
||||||
class function FromString(const AValue: String): TScalar; static; inline;
|
class function FromString(const AValue: String): TScalar; static; inline;
|
||||||
class function FromBytes(const AValue: TScalarBytes): TScalar; static; inline;
|
class function FromBytes(const AValue: TScalarBytes): TScalar; static; inline;
|
||||||
class function FromDecimal(AValue: TDecimal): TScalar; static; inline;
|
class function FromDecimal(const AValue: TDecimal): TScalar; static; inline;
|
||||||
|
|
||||||
function GetKind: TScalarKind; inline;
|
|
||||||
function GetValue: TScalarValue; inline;
|
|
||||||
|
|
||||||
function ToString: String;
|
function ToString: String;
|
||||||
|
|
||||||
property Kind: TScalarKind read GetKind;
|
|
||||||
property Value: TScalarValue read GetValue;
|
|
||||||
|
|
||||||
strict private
|
|
||||||
FKind: TScalarKind;
|
|
||||||
FValue: TScalarValue;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Basic data structures using the scalar type (these are not POD of course)
|
// Basic data structures using the scalar type (these are not POD of course)
|
||||||
@@ -133,51 +98,23 @@ type
|
|||||||
// An array of scalar values of the same kind.
|
// An array of scalar values of the same kind.
|
||||||
TScalarArray = record
|
TScalarArray = record
|
||||||
public
|
public
|
||||||
|
Kind: TScalarKind;
|
||||||
|
Items: TArray<TScalarValue>;
|
||||||
|
|
||||||
// Creates a new scalar array.
|
// Creates a new scalar array.
|
||||||
constructor Create(AKind: TScalarKind; const AItems: TArray<TScalarValue>);
|
constructor Create(AKind: TScalarKind; const AItems: TArray<TScalarValue>);
|
||||||
|
|
||||||
function GetKind: TScalarKind; inline;
|
|
||||||
function GetItems: TArray<TScalarValue>; inline;
|
|
||||||
|
|
||||||
property Kind: TScalarKind read GetKind;
|
|
||||||
property Items: TArray<TScalarValue> read GetItems;
|
|
||||||
|
|
||||||
strict private
|
|
||||||
FKind: TScalarKind;
|
|
||||||
FItems: TArray<TScalarValue>;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
TScalarTuple = TArray<TScalar>;
|
TScalarTuple = TArray<TScalar>;
|
||||||
|
|
||||||
// A field definition for a scalar record.
|
// A field definition for a scalar record.
|
||||||
TScalarRecordField = record
|
TScalarRecordField = record
|
||||||
public
|
Name: String;
|
||||||
// Creates a new record field definition.
|
Kind: TScalarKind;
|
||||||
constructor Create(const AName: String; AKind: TScalarKind);
|
constructor Create(const AName: String; AKind: TScalarKind);
|
||||||
|
|
||||||
function GetName: String; inline;
|
|
||||||
function GetKind: TScalarKind; inline;
|
|
||||||
|
|
||||||
property Name: String read GetName;
|
|
||||||
property Kind: TScalarKind read GetKind;
|
|
||||||
|
|
||||||
strict private
|
|
||||||
FName: String;
|
|
||||||
FKind: TScalarKind;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// A collection of field definitions that describe a scalar record.
|
TScalarRecordDefinition = TArray<TScalarRecordField>;
|
||||||
TScalarRecordDefinition = record
|
|
||||||
public
|
|
||||||
// Creates a new record definition.
|
|
||||||
constructor Create(const AFields: TArray<TScalarRecordField>);
|
|
||||||
|
|
||||||
function GetFields: TArray<TScalarRecordField>; inline;
|
|
||||||
property Fields: TArray<TScalarRecordField> read GetFields;
|
|
||||||
|
|
||||||
strict private
|
|
||||||
FFields: TArray<TScalarRecordField>;
|
|
||||||
end;
|
|
||||||
|
|
||||||
// A record of scalar values, based on a definition.
|
// A record of scalar values, based on a definition.
|
||||||
TScalarRecord = record
|
TScalarRecord = record
|
||||||
@@ -215,272 +152,213 @@ type
|
|||||||
FItems: TSeries<TScalarValue>;
|
FItems: TSeries<TScalarValue>;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
// A time series of scalar records, optimized for memory and access speed.
|
||||||
|
TScalarRecordSeries = record
|
||||||
|
private
|
||||||
|
FDef: TScalarRecordDefinition;
|
||||||
|
FArray: TChunkArray<TScalarValue>;
|
||||||
|
FTotalCount: Int64;
|
||||||
|
function GetDef: TScalarRecordDefinition; inline;
|
||||||
|
function GetItems(Idx: Integer): TScalarRecord;
|
||||||
|
public
|
||||||
|
constructor Create(const ADef: TScalarRecordDefinition);
|
||||||
|
procedure Add(const Item: TScalarRecord; Lookback: Int64 = -1);
|
||||||
|
property Def: TScalarRecordDefinition read GetDef;
|
||||||
|
property Items[Idx: Integer]: TScalarRecord read GetItems; default;
|
||||||
|
property TotalCount: Int64 read FTotalCount;
|
||||||
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
{ TScalarValue }
|
{ TScalarValue }
|
||||||
|
|
||||||
class function TScalarValue.FromBoolean(AValue: Boolean): TScalarValue;
|
class function TScalarValue.FromBoolean(AValue: Boolean): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FBoolean := AValue;
|
Result.AsBoolean := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromBytes(const AValue: TScalarBytes): TScalarValue;
|
class function TScalarValue.FromBytes(const AValue: TScalarBytes): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FBytes := AValue;
|
Result.AsBytes := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromChar(AValue: Char): TScalarValue;
|
class function TScalarValue.FromChar(AValue: Char): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FChar := AValue;
|
Result.AsChar := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromDateTime(AValue: TDateTime): TScalarValue;
|
class function TScalarValue.FromDateTime(AValue: TDateTime): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FDateTime := AValue;
|
Result.AsDateTime := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromDecimal(AValue: TDecimal): TScalarValue;
|
class function TScalarValue.FromDecimal(AValue: TDecimal): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FDecimal := AValue;
|
Result.AsDecimal := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromDouble(AValue: Double): TScalarValue;
|
class function TScalarValue.FromDouble(AValue: Double): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FDouble := AValue;
|
Result.AsDouble := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromInt64(AValue: Int64): TScalarValue;
|
class function TScalarValue.FromInt64(AValue: Int64): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FInt64 := AValue;
|
Result.AsInt64 := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromInteger(AValue: Integer): TScalarValue;
|
class function TScalarValue.FromInteger(AValue: Integer): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FInteger := AValue;
|
Result.AsInteger := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromPChar(const AValue: String): TScalarValue;
|
class function TScalarValue.FromPChar(const AValue: String): TScalarValue;
|
||||||
var
|
var
|
||||||
len, i: Integer;
|
len, i: Integer;
|
||||||
begin
|
begin
|
||||||
FillChar(Result.FPChar, SizeOf(Result.FPChar), #0);
|
FillChar(Result.AsPChar, SizeOf(Result.AsPChar), #0);
|
||||||
len := Length(AValue);
|
len := Length(AValue);
|
||||||
if (len > 0) then
|
if (len > 0) then
|
||||||
begin
|
begin
|
||||||
if (len > Length(Result.FPChar)) then
|
if (len > Length(Result.AsPChar)) then
|
||||||
len := Length(Result.FPChar);
|
len := Length(Result.AsPChar);
|
||||||
for i := 0 to len - 1 do
|
for i := 0 to len - 1 do
|
||||||
Result.FPChar[i] := AValue[i + 1];
|
Result.AsPChar[i] := AValue[i + 1];
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromPChar(const AValue: TScalarPChar): TScalarValue;
|
class function TScalarValue.FromPChar(const AValue: TScalarPChar): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FPChar := AValue;
|
Result.AsPChar := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromSingle(AValue: Single): TScalarValue;
|
class function TScalarValue.FromSingle(AValue: Single): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FSingle := AValue;
|
Result.AsSingle := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromString(const AValue: String): TScalarValue;
|
class function TScalarValue.FromString(const AValue: String): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FString := ShortString(AValue);
|
Result.AsString := ShortString(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromString(const AValue: TScalarString): TScalarValue;
|
class function TScalarValue.FromString(const AValue: TScalarString): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FString := AValue;
|
Result.AsString := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromTimestamp(AValue: TTimestamp): TScalarValue;
|
class function TScalarValue.FromTimestamp(AValue: TTimestamp): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FTimestamp := AValue;
|
Result.AsTimestamp := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalarValue.FromUInt64(AValue: UInt64): TScalarValue;
|
class function TScalarValue.FromUInt64(AValue: UInt64): TScalarValue;
|
||||||
begin
|
begin
|
||||||
Result.FUInt64 := AValue;
|
Result.AsUInt64 := AValue;
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsBoolean: Boolean;
|
|
||||||
begin
|
|
||||||
Result := FBoolean;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsBytes: TScalarBytes;
|
|
||||||
begin
|
|
||||||
Result := FBytes;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsChar: Char;
|
|
||||||
begin
|
|
||||||
Result := FChar;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsDateTime: TDateTime;
|
|
||||||
begin
|
|
||||||
Result := FDateTime;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsDecimal: TDecimal;
|
|
||||||
begin
|
|
||||||
Result := FDecimal;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsDouble: Double;
|
|
||||||
begin
|
|
||||||
Result := FDouble;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsInt64: Int64;
|
|
||||||
begin
|
|
||||||
Result := FInt64;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsInteger: Integer;
|
|
||||||
begin
|
|
||||||
Result := FInteger;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsPChar: PChar;
|
|
||||||
begin
|
|
||||||
Result := @FPChar;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsSingle: Single;
|
|
||||||
begin
|
|
||||||
Result := FSingle;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsString: ShortString;
|
|
||||||
begin
|
|
||||||
Result := FString;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsTimestamp: TTimestamp;
|
|
||||||
begin
|
|
||||||
Result := FTimestamp;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarValue.GetAsUInt64: UInt64;
|
|
||||||
begin
|
|
||||||
Result := FUInt64;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TScalar }
|
{ TScalar }
|
||||||
|
|
||||||
constructor TScalar.Create(AKind: TScalarKind; const AValue: TScalarValue);
|
constructor TScalar.Create(AKind: TScalarKind; const AValue: TScalarValue);
|
||||||
begin
|
begin
|
||||||
FKind := AKind;
|
Kind := AKind;
|
||||||
FValue := AValue;
|
Value := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromBoolean(AValue: Boolean): TScalar;
|
class function TScalar.FromBoolean(AValue: Boolean): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skBoolean;
|
Result.Kind := skBoolean;
|
||||||
Result.FValue := TScalarValue.FromBoolean(AValue);
|
Result.Value := TScalarValue.FromBoolean(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromBytes(const AValue: TScalarBytes): TScalar;
|
class function TScalar.FromBytes(const AValue: TScalarBytes): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skBytes;
|
Result.Kind := skBytes;
|
||||||
Result.FValue := TScalarValue.FromBytes(AValue);
|
Result.Value := TScalarValue.FromBytes(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromChar(AValue: Char): TScalar;
|
class function TScalar.FromChar(AValue: Char): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skChar;
|
Result.Kind := skChar;
|
||||||
Result.FValue := TScalarValue.FromChar(AValue);
|
Result.Value := TScalarValue.FromChar(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromDateTime(AValue: TDateTime): TScalar;
|
class function TScalar.FromDateTime(AValue: TDateTime): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skDateTime;
|
Result.Kind := skDateTime;
|
||||||
Result.FValue := TScalarValue.FromDateTime(AValue);
|
Result.Value := TScalarValue.FromDateTime(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromDecimal(AValue: TDecimal): TScalar;
|
class function TScalar.FromDecimal(const AValue: TDecimal): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skDecimal;
|
Result.Kind := skDecimal;
|
||||||
Result.FValue := TScalarValue.FromDecimal(AValue);
|
Result.Value := TScalarValue.FromDecimal(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromDouble(AValue: Double): TScalar;
|
class function TScalar.FromDouble(AValue: Double): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skDouble;
|
Result.Kind := skDouble;
|
||||||
Result.FValue := TScalarValue.FromDouble(AValue);
|
Result.Value := TScalarValue.FromDouble(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromInt64(AValue: Int64): TScalar;
|
class function TScalar.FromInt64(AValue: Int64): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skInt64;
|
Result.Kind := skInt64;
|
||||||
Result.FValue := TScalarValue.FromInt64(AValue);
|
Result.Value := TScalarValue.FromInt64(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromInteger(AValue: Integer): TScalar;
|
class function TScalar.FromInteger(AValue: Integer): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skInteger;
|
Result.Kind := skInteger;
|
||||||
Result.FValue := TScalarValue.FromInteger(AValue);
|
Result.Value := TScalarValue.FromInteger(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromPChar(const AValue: TScalarPChar): TScalar;
|
class function TScalar.FromPChar(const AValue: TScalarPChar): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skPChar;
|
Result.Kind := skPChar;
|
||||||
Result.FValue := TScalarValue.FromPChar(AValue);
|
Result.Value := TScalarValue.FromPChar(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromSingle(AValue: Single): TScalar;
|
class function TScalar.FromSingle(AValue: Single): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skSingle;
|
Result.Kind := skSingle;
|
||||||
Result.FValue := TScalarValue.FromSingle(AValue);
|
Result.Value := TScalarValue.FromSingle(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromString(const AValue: String): TScalar;
|
class function TScalar.FromString(const AValue: String): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skString;
|
Result.Kind := skString;
|
||||||
Result.FValue := TScalarValue.FromString(AValue);
|
Result.Value := TScalarValue.FromString(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromTimestamp(AValue: TTimestamp): TScalar;
|
class function TScalar.FromTimestamp(AValue: TTimestamp): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skTimestamp;
|
Result.Kind := skTimestamp;
|
||||||
Result.FValue := TScalarValue.FromTimestamp(AValue);
|
Result.Value := TScalarValue.FromTimestamp(AValue);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TScalar.FromUInt64(AValue: UInt64): TScalar;
|
class function TScalar.FromUInt64(AValue: UInt64): TScalar;
|
||||||
begin
|
begin
|
||||||
Result.FKind := skUInt64;
|
Result.Kind := skUInt64;
|
||||||
Result.FValue := TScalarValue.FromUInt64(AValue);
|
Result.Value := TScalarValue.FromUInt64(AValue);
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalar.GetKind: TScalarKind;
|
|
||||||
begin
|
|
||||||
Result := FKind;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalar.GetValue: TScalarValue;
|
|
||||||
begin
|
|
||||||
Result := FValue;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TScalar.ToString: String;
|
function TScalar.ToString: String;
|
||||||
begin
|
begin
|
||||||
case FKind of
|
case Kind of
|
||||||
skInteger: Result := IntToStr(FValue.AsInteger);
|
skInteger: Result := IntToStr(Value.AsInteger);
|
||||||
skInt64: Result := IntToStr(FValue.AsInt64);
|
skInt64: Result := IntToStr(Value.AsInt64);
|
||||||
skUInt64: Result := UIntToStr(FValue.AsUInt64);
|
skUInt64: Result := UIntToStr(Value.AsUInt64);
|
||||||
skSingle: Result := FloatToStr(FValue.AsSingle);
|
skSingle: Result := FloatToStr(Value.AsSingle);
|
||||||
skDouble: Result := FloatToStr(FValue.AsDouble);
|
skDouble: Result := FloatToStr(Value.AsDouble);
|
||||||
skDateTime: Result := DateTimeToStr(FValue.AsDateTime);
|
skDateTime: Result := DateTimeToStr(Value.AsDateTime);
|
||||||
skTimestamp: Result := FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', TimeStampToDateTime(FValue.AsTimestamp));
|
skTimestamp: Result := FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', TimeStampToDateTime(Value.AsTimestamp));
|
||||||
skBoolean: Result := BoolToStr(FValue.AsBoolean, True);
|
skBoolean: Result := BoolToStr(Value.AsBoolean, True);
|
||||||
skChar: Result := '''' + FValue.AsChar + '''';
|
skChar: Result := '''' + Value.AsChar + '''';
|
||||||
skPChar: Result := FValue.AsPChar; // Already null-terminated
|
skPChar: Result := PChar(@Value.AsPChar); // Needs explicit cast/pointer
|
||||||
skString: Result := '''' + string(FValue.AsString) + '''';
|
skString: Result := '''' + string(Value.AsString) + '''';
|
||||||
skBytes: Result := '(Bytes)';
|
skBytes: Result := '(Bytes)';
|
||||||
skDecimal: Result := FloatToStr(Double(FValue.AsDecimal));
|
skDecimal: Result := FloatToStr(Double(Value.AsDecimal));
|
||||||
else
|
else
|
||||||
Result := '[Unknown Scalar]';
|
Result := '[Unknown Scalar]';
|
||||||
end;
|
end;
|
||||||
@@ -490,55 +368,23 @@ end;
|
|||||||
|
|
||||||
constructor TScalarArray.Create(AKind: TScalarKind; const AItems: TArray<TScalarValue>);
|
constructor TScalarArray.Create(AKind: TScalarKind; const AItems: TArray<TScalarValue>);
|
||||||
begin
|
begin
|
||||||
FKind := AKind;
|
Kind := AKind;
|
||||||
FItems := AItems;
|
Items := AItems;
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarArray.GetKind: TScalarKind;
|
|
||||||
begin
|
|
||||||
Result := FKind;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarArray.GetItems: TArray<TScalarValue>;
|
|
||||||
begin
|
|
||||||
Result := FItems;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TScalarRecordField }
|
{ TScalarRecordField }
|
||||||
|
|
||||||
constructor TScalarRecordField.Create(const AName: String; AKind: TScalarKind);
|
constructor TScalarRecordField.Create(const AName: String; AKind: TScalarKind);
|
||||||
begin
|
begin
|
||||||
FName := AName;
|
Name := AName;
|
||||||
FKind := AKind;
|
Kind := AKind;
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarRecordField.GetName: String;
|
|
||||||
begin
|
|
||||||
Result := FName;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarRecordField.GetKind: TScalarKind;
|
|
||||||
begin
|
|
||||||
Result := FKind;
|
|
||||||
end;
|
|
||||||
|
|
||||||
{ TScalarRecordDefinition }
|
|
||||||
|
|
||||||
constructor TScalarRecordDefinition.Create(const AFields: TArray<TScalarRecordField>);
|
|
||||||
begin
|
|
||||||
FFields := AFields;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TScalarRecordDefinition.GetFields: TArray<TScalarRecordField>;
|
|
||||||
begin
|
|
||||||
Result := FFields;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TScalarRecord }
|
{ TScalarRecord }
|
||||||
|
|
||||||
constructor TScalarRecord.Create(const ADef: TScalarRecordDefinition; const AFields: TArray<TScalarValue>);
|
constructor TScalarRecord.Create(const ADef: TScalarRecordDefinition; const AFields: TArray<TScalarValue>);
|
||||||
begin
|
begin
|
||||||
Assert(Length(ADef.Fields) = Length(AFields), 'Field definition and value count must match.');
|
Assert(Length(ADef) = Length(AFields), 'Field definition and value count must match.');
|
||||||
FDef := ADef;
|
FDef := ADef;
|
||||||
FFields := AFields;
|
FFields := AFields;
|
||||||
end;
|
end;
|
||||||
@@ -559,9 +405,9 @@ var
|
|||||||
fieldDef: TScalarRecordField;
|
fieldDef: TScalarRecordField;
|
||||||
begin
|
begin
|
||||||
// Find the index of the field by name (case-insensitive)
|
// Find the index of the field by name (case-insensitive)
|
||||||
for i := 0 to Length(FDef.Fields) - 1 do
|
for i := 0 to Length(FDef) - 1 do
|
||||||
begin
|
begin
|
||||||
fieldDef := FDef.Fields[i];
|
fieldDef := FDef[i];
|
||||||
if (CompareText(fieldDef.Name, Name) = 0) then
|
if (CompareText(fieldDef.Name, Name) = 0) then
|
||||||
begin
|
begin
|
||||||
// Found it. Construct the TScalar result from the kind and value.
|
// Found it. Construct the TScalar result from the kind and value.
|
||||||
@@ -592,7 +438,42 @@ begin
|
|||||||
Result := FItems;
|
Result := FItems;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
{ TScalarRecordSeries }
|
||||||
|
|
||||||
|
// Implements a time series of scalar records using a chunk array for efficient storage.
|
||||||
|
// Each record's fields are stored sequentially in the TChunkArray.
|
||||||
|
|
||||||
|
constructor TScalarRecordSeries.Create(const ADef: TScalarRecordDefinition);
|
||||||
|
begin
|
||||||
|
Assert(Length(ADef) > 0);
|
||||||
|
FDef := ADef;
|
||||||
|
FArray := Default(TChunkArray<TScalarValue>);
|
||||||
|
FTotalCount := 0;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TScalarRecordSeries.Add(const Item: TScalarRecord; Lookback: Int64 = -1);
|
||||||
|
begin
|
||||||
|
FArray.Add(Item.Fields, Length(FDef) * Lookback);
|
||||||
|
inc(FTotalCount);
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TScalarRecordSeries.GetDef: TScalarRecordDefinition;
|
||||||
|
begin
|
||||||
|
Result := FDef;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TScalarRecordSeries.GetItems(Idx: Integer): TScalarRecord;
|
||||||
|
var
|
||||||
|
values: TArray<TScalarValue>;
|
||||||
|
fieldCount: Integer;
|
||||||
|
begin
|
||||||
|
fieldCount := Length(FDef);
|
||||||
|
SetLength(values, fieldCount);
|
||||||
|
Move(FArray.ItemRef[(FArray.Count - fieldCount) - (Idx * fieldCount)]^, values[0], sizeof(TScalarValue) * fieldCount);
|
||||||
|
Result.Create(FDef, values);
|
||||||
|
end;
|
||||||
|
|
||||||
initialization
|
initialization
|
||||||
Assert(sizeof(TScalarValue) = 16);
|
Assert(sizeof(TScalarValue) = 8);
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|||||||
+19
-10
@@ -4,16 +4,19 @@ interface
|
|||||||
|
|
||||||
type
|
type
|
||||||
TChunkArray<T> = record
|
TChunkArray<T> = record
|
||||||
|
type
|
||||||
|
PT = ^T;
|
||||||
private
|
private
|
||||||
const
|
const
|
||||||
ChunkSize = 1024;
|
ChunkSize = 1024;
|
||||||
type
|
type
|
||||||
TChunk = TArray<T>;
|
TChunk = array of T;
|
||||||
private
|
private
|
||||||
FChunks: TArray<TChunk>;
|
FChunks: TArray<TChunk>;
|
||||||
FOffset: Integer;
|
FOffset: Integer;
|
||||||
FCount: Integer;
|
FCount: Integer;
|
||||||
function GetItems(Idx: Integer): T; inline;
|
function GetItems(Idx: Integer): T; inline;
|
||||||
|
function GetItemRef(Idx: Integer): PT; inline;
|
||||||
|
|
||||||
public
|
public
|
||||||
constructor Create(const AChunks: TArray<TChunk>; ACount: Integer; AOffset: Integer);
|
constructor Create(const AChunks: TArray<TChunk>; ACount: Integer; AOffset: Integer);
|
||||||
@@ -32,6 +35,7 @@ type
|
|||||||
|
|
||||||
property Count: Integer read FCount;
|
property Count: Integer read FCount;
|
||||||
property Items[Idx: Integer]: T read GetItems; default;
|
property Items[Idx: Integer]: T read GetItems; default;
|
||||||
|
property ItemRef[Idx: Integer]: PT read GetItemRef;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// A series where the last added item has index 0.
|
// A series where the last added item has index 0.
|
||||||
@@ -274,9 +278,7 @@ end;
|
|||||||
|
|
||||||
function TChunkArray<T>.GetItems(Idx: Integer): T;
|
function TChunkArray<T>.GetItems(Idx: Integer): T;
|
||||||
begin
|
begin
|
||||||
// Convert logical index to physical index inside chunks
|
Result := GetItemRef(Idx)^;
|
||||||
var physicalIdx := Idx + FOffset;
|
|
||||||
Result := FChunks[physicalIdx div ChunkSize][physicalIdx mod ChunkSize];
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TChunkArray<T>.Copy(MaxCount: Integer): TChunkArray<T>;
|
function TChunkArray<T>.Copy(MaxCount: Integer): TChunkArray<T>;
|
||||||
@@ -341,6 +343,13 @@ begin
|
|||||||
Result.FCount := effectiveCount;
|
Result.FCount := effectiveCount;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TChunkArray<T>.GetItemRef(Idx: Integer): PT;
|
||||||
|
begin
|
||||||
|
Assert((Idx >= 0) and (Idx < FCount));
|
||||||
|
var physicalIdx := Idx + FOffset;
|
||||||
|
Result := @FChunks[physicalIdx div ChunkSize][physicalIdx mod ChunkSize];
|
||||||
|
end;
|
||||||
|
|
||||||
class operator TChunkArray<T>.Initialize(out Dest: TChunkArray<T>);
|
class operator TChunkArray<T>.Initialize(out Dest: TChunkArray<T>);
|
||||||
begin
|
begin
|
||||||
Dest.FOffset := 0;
|
Dest.FOffset := 0;
|
||||||
@@ -349,6 +358,12 @@ end;
|
|||||||
|
|
||||||
{ TSeries<T> }
|
{ TSeries<T> }
|
||||||
|
|
||||||
|
constructor TSeries<T>.Create(const AArray: TChunkArray<T>; ATotalCount: Int64);
|
||||||
|
begin
|
||||||
|
FArray := AArray;
|
||||||
|
FTotalCount := ATotalCount;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TSeries<T>.Add(const Data: T; Lookback: Int64);
|
procedure TSeries<T>.Add(const Data: T; Lookback: Int64);
|
||||||
begin
|
begin
|
||||||
FArray.Add(Data, Lookback);
|
FArray.Add(Data, Lookback);
|
||||||
@@ -361,12 +376,6 @@ begin
|
|||||||
inc(FTotalCount, Length(Data));
|
inc(FTotalCount, Length(Data));
|
||||||
end;
|
end;
|
||||||
|
|
||||||
constructor TSeries<T>.Create(const AArray: TChunkArray<T>; ATotalCount: Int64);
|
|
||||||
begin
|
|
||||||
FArray := AArray;
|
|
||||||
FTotalCount := ATotalCount;
|
|
||||||
end;
|
|
||||||
|
|
||||||
function TSeries<T>.Copy(Lookback: Integer): TSeries<T>;
|
function TSeries<T>.Copy(Lookback: Integer): TSeries<T>;
|
||||||
begin
|
begin
|
||||||
// Create a copy of the underlying chunk array, optionally truncating it.
|
// Create a copy of the underlying chunk array, optionally truncating it.
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
unit Myc.Trade.Indicators_V2;
|
unit Myc.Trade.Indicators_V2 deprecated;
|
||||||
|
|
||||||
interface
|
interface
|
||||||
|
|
||||||
@@ -377,7 +377,7 @@ begin
|
|||||||
begin
|
begin
|
||||||
shortName := rttiType.Name;
|
shortName := rttiType.Name;
|
||||||
end;
|
end;
|
||||||
|
{
|
||||||
// Create the main factory procedure. This is a double-nested anonymous method
|
// Create the main factory procedure. This is a double-nested anonymous method
|
||||||
// that wraps the template's specific factory and worker functions.
|
// that wraps the template's specific factory and worker functions.
|
||||||
factoryProc :=
|
factoryProc :=
|
||||||
@@ -419,7 +419,7 @@ begin
|
|||||||
Result := DataValueFromTValue(resultAsTValue);
|
Result := DataValueFromTValue(resultAsTValue);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
}
|
||||||
// Create the final factory instance with all type handles and extracted metadata.
|
// Create the final factory instance with all type handles and extracted metadata.
|
||||||
Result := TGenericIndicatorFactory.Create(factoryProc, shortName, name, hint, paramsDataType, argsDataType, resultDataType);
|
Result := TGenericIndicatorFactory.Create(factoryProc, shortName, name, hint, paramsDataType, argsDataType, resultDataType);
|
||||||
end;
|
end;
|
||||||
@@ -452,7 +452,7 @@ begin
|
|||||||
var paramsAsTValue := TValue.From<TParams>(Params);
|
var paramsAsTValue := TValue.From<TParams>(Params);
|
||||||
var paramsAsDataValue := DataValueFromTValue(paramsAsTValue);
|
var paramsAsDataValue := DataValueFromTValue(paramsAsTValue);
|
||||||
var cIndicator := FFactoryProc(paramsAsDataValue);
|
var cIndicator := FFactoryProc(paramsAsDataValue);
|
||||||
|
{
|
||||||
Result :=
|
Result :=
|
||||||
function(const Args: TArgs): TResult
|
function(const Args: TArgs): TResult
|
||||||
begin
|
begin
|
||||||
@@ -461,7 +461,7 @@ begin
|
|||||||
var resultAsDataValue := cIndicator(argsAsDataValue);
|
var resultAsDataValue := cIndicator(argsAsDataValue);
|
||||||
var resultAsTValue := resultAsDataValue.AsTValue;
|
var resultAsTValue := resultAsDataValue.AsTValue;
|
||||||
Result := resultAsTValue.AsType<TResult>;
|
Result := resultAsTValue.AsType<TResult>;
|
||||||
end;
|
end;}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TIndicatorRegistry.TItem }
|
{ TIndicatorRegistry.TItem }
|
||||||
@@ -481,7 +481,7 @@ begin
|
|||||||
var paramsAsTValue := TValue.From<TParams>(Params);
|
var paramsAsTValue := TValue.From<TParams>(Params);
|
||||||
var paramsAsDataValue := DataValueFromTValue(paramsAsTValue);
|
var paramsAsDataValue := DataValueFromTValue(paramsAsTValue);
|
||||||
var cIndicator := FFactory.CreateIndicator(paramsAsDataValue);
|
var cIndicator := FFactory.CreateIndicator(paramsAsDataValue);
|
||||||
|
{
|
||||||
// Return a wrapper that handles the conversion for args and result.
|
// Return a wrapper that handles the conversion for args and result.
|
||||||
Result :=
|
Result :=
|
||||||
function(const Args: TArgs): TResult
|
function(const Args: TArgs): TResult
|
||||||
@@ -491,7 +491,7 @@ begin
|
|||||||
var resultAsDataValue := cIndicator(argsAsDataValue);
|
var resultAsDataValue := cIndicator(argsAsDataValue);
|
||||||
var resultAsTValue := resultAsDataValue.AsTValue;
|
var resultAsTValue := resultAsDataValue.AsTValue;
|
||||||
Result := resultAsTValue.AsType<TResult>;
|
Result := resultAsTValue.AsType<TResult>;
|
||||||
end;
|
end;}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TIndicatorRegistry }
|
{ TIndicatorRegistry }
|
||||||
|
|||||||
+50
-8
@@ -50,6 +50,9 @@ type
|
|||||||
procedure TestScalarRecordAndDefinition;
|
procedure TestScalarRecordAndDefinition;
|
||||||
[Test]
|
[Test]
|
||||||
procedure TestScalarSeries;
|
procedure TestScalarSeries;
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
procedure TestScalarRecordSeries;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
@@ -117,7 +120,7 @@ begin
|
|||||||
skString:
|
skString:
|
||||||
begin
|
begin
|
||||||
scalar := TScalar.FromString(AValueStr);
|
scalar := TScalar.FromString(AValueStr);
|
||||||
s := Copy(AValueStr, 1, 15); // ShortString truncates to 15 chars
|
s := Copy(AValueStr, 1, 7); // ShortString truncates to 7 chars
|
||||||
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skString');
|
Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skString');
|
||||||
Assert.AreEqual(s, string(scalar.Value.AsString), 'Value mismatch for AsString');
|
Assert.AreEqual(s, string(scalar.Value.AsString), 'Value mismatch for AsString');
|
||||||
end;
|
end;
|
||||||
@@ -223,23 +226,21 @@ var
|
|||||||
begin
|
begin
|
||||||
// 1. Create a definition
|
// 1. Create a definition
|
||||||
recDef :=
|
recDef :=
|
||||||
TScalarRecordDefinition.Create(
|
|
||||||
[
|
[
|
||||||
TScalarRecordField.Create('ID', skInt64),
|
TScalarRecordField.Create('ID', skInt64),
|
||||||
TScalarRecordField.Create('Name', skString),
|
TScalarRecordField.Create('Name', skString),
|
||||||
TScalarRecordField.Create('Price', skDecimal)
|
TScalarRecordField.Create('Price', skDecimal)
|
||||||
]
|
];
|
||||||
);
|
Assert.AreEqual(Int64(3), Length(recDef), 'Record definition field count mismatch');
|
||||||
Assert.AreEqual(Int64(3), Length(recDef.Fields), 'Record definition field count mismatch');
|
Assert.AreEqual('Name', recDef[1].Name, 'Record definition field name mismatch');
|
||||||
Assert.AreEqual('Name', recDef.Fields[1].Name, 'Record definition field name mismatch');
|
|
||||||
|
|
||||||
// 2. Create a matching set of values
|
// 2. Create a matching set of values
|
||||||
values := [TScalar.FromInt64(99).Value, TScalar.FromString('ProductX').Value, TScalar.FromDecimal(TDecimal.Create(1995, 2)).Value];
|
values := [TScalar.FromInt64(99).Value, TScalar.FromString('Product').Value, TScalar.FromDecimal(TDecimal.Create(1995, 2)).Value];
|
||||||
|
|
||||||
// 3. Create the record
|
// 3. Create the record
|
||||||
rec := TScalarRecord.Create(recDef, values);
|
rec := TScalarRecord.Create(recDef, values);
|
||||||
Assert.AreEqual(Int64(3), Length(rec.Fields), 'Record field count mismatch');
|
Assert.AreEqual(Int64(3), Length(rec.Fields), 'Record field count mismatch');
|
||||||
Assert.AreEqual('ProductX', string(rec.Fields[1].AsString), 'Record field value mismatch');
|
Assert.AreEqual('Product', string(rec.Fields[1].AsString), 'Record field value mismatch');
|
||||||
Assert.AreEqual(TDecimal.Create(1995, 2), rec.Fields[2].AsDecimal, 'Record decimal field value mismatch');
|
Assert.AreEqual(TDecimal.Create(1995, 2), rec.Fields[2].AsDecimal, 'Record decimal field value mismatch');
|
||||||
|
|
||||||
// 4. Test assertion for mismatched field count
|
// 4. Test assertion for mismatched field count
|
||||||
@@ -277,6 +278,47 @@ begin
|
|||||||
Assert.AreEqual(101.5, scalarSeries.Items[3].AsDouble, 1E-12, 'Series item [3] mismatch');
|
Assert.AreEqual(101.5, scalarSeries.Items[3].AsDouble, 1E-12, 'Series item [3] mismatch');
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TTestPOD.TestScalarRecordSeries;
|
||||||
|
var
|
||||||
|
recDef: TScalarRecordDefinition;
|
||||||
|
series: TScalarRecordSeries;
|
||||||
|
rec1, rec2, rec3: TScalarRecord;
|
||||||
|
retrievedRec: TScalarRecord;
|
||||||
|
begin
|
||||||
|
// 1. Define the structure of the records in the series
|
||||||
|
recDef := [TScalarRecordField.Create('ID', skInt64), TScalarRecordField.Create('Price', skDecimal)];
|
||||||
|
|
||||||
|
// 2. Create the series with the definition
|
||||||
|
series := TScalarRecordSeries.Create(recDef);
|
||||||
|
Assert.AreEqual(Int64(0), series.TotalCount, 'Initial series should be empty');
|
||||||
|
|
||||||
|
// 3. Create some records
|
||||||
|
rec1 := TScalarRecord.Create(recDef, [TScalarValue.FromInt64(1), TScalarValue.FromDecimal(TDecimal.Create(100, 2))]);
|
||||||
|
rec2 := TScalarRecord.Create(recDef, [TScalarValue.FromInt64(2), TScalarValue.FromDecimal(TDecimal.Create(101, 2))]);
|
||||||
|
rec3 := TScalarRecord.Create(recDef, [TScalarValue.FromInt64(3), TScalarValue.FromDecimal(TDecimal.Create(102, 2))]);
|
||||||
|
|
||||||
|
// 4. Add records to the series
|
||||||
|
series.Add(rec1);
|
||||||
|
series.Add(rec2);
|
||||||
|
series.Add(rec3);
|
||||||
|
|
||||||
|
// 5. Verify the state of the series
|
||||||
|
Assert.AreEqual(Int64(3), series.TotalCount, 'Series total count mismatch after adding items');
|
||||||
|
|
||||||
|
// 6. Check items. Index 0 is the last added item.
|
||||||
|
retrievedRec := series.Items[0]; // Should be rec3
|
||||||
|
Assert.AreEqual(skInt64, retrievedRec['ID'].Kind, 'Item[0] ID kind mismatch');
|
||||||
|
Assert.AreEqual(Int64(3), retrievedRec['ID'].Value.AsInt64, 'Item[0] ID value mismatch');
|
||||||
|
Assert.AreEqual(TDecimal.Create(102, 2), retrievedRec['Price'].Value.AsDecimal, 'Item[0] Price value mismatch');
|
||||||
|
|
||||||
|
retrievedRec := series.Items[1]; // Should be rec2
|
||||||
|
Assert.AreEqual(Int64(2), retrievedRec.Items['ID'].Value.AsInt64, 'Item[1] ID value mismatch');
|
||||||
|
|
||||||
|
retrievedRec := series.Items[2]; // Should be rec1
|
||||||
|
Assert.AreEqual(Int64(1), retrievedRec.Items['ID'].Value.AsInt64, 'Item[2] ID value mismatch');
|
||||||
|
Assert.AreEqual(TDecimal.Create(100, 2), retrievedRec.Items['Price'].Value.AsDecimal, 'Item[2] Price value mismatch');
|
||||||
|
end;
|
||||||
|
|
||||||
initialization
|
initialization
|
||||||
FormatSettings.DecimalSeparator := '.';
|
FormatSettings.DecimalSeparator := '.';
|
||||||
TDUnitX.RegisterTestFixture(TTestPOD);
|
TDUnitX.RegisterTestFixture(TTestPOD);
|
||||||
|
|||||||
Reference in New Issue
Block a user