AST development
This commit is contained in:
@@ -106,6 +106,13 @@ object Form1: TForm1
|
||||
TextSettings.Trimming = None
|
||||
OnClick = ClearButtonClick
|
||||
end
|
||||
object FlowOnlyBox: TCheckBox
|
||||
Position.X = 24.000000000000000000
|
||||
Position.Y = 8.000000000000000000
|
||||
TabOrder = 12
|
||||
Text = 'Flow only'
|
||||
OnChange = ClearButtonClick
|
||||
end
|
||||
end
|
||||
object Panel2: TPanel
|
||||
Align = Client
|
||||
|
||||
+94
-20
@@ -45,6 +45,7 @@ type
|
||||
DoTrigger2Button: TButton;
|
||||
Panel2: TPanel;
|
||||
ClearButton: TButton;
|
||||
FlowOnlyBox: TCheckBox;
|
||||
procedure ClearButtonClick(Sender: TObject);
|
||||
procedure FormCreate(Sender: TObject);
|
||||
procedure CrerateTriggerExampleButtonClick(Sender: TObject);
|
||||
@@ -92,7 +93,7 @@ begin
|
||||
|
||||
FWorkspace.OnMouseDown := WorkspaceMouseDown;
|
||||
|
||||
FGScope := T_ExecutionScope.Create(nil);
|
||||
FGScope := TExecutionScope.Create(nil);
|
||||
end;
|
||||
|
||||
procedure TForm1.DebugButtonClick(Sender: TObject);
|
||||
@@ -109,7 +110,7 @@ begin
|
||||
exit;
|
||||
end;
|
||||
|
||||
scope := T_ExecutionScope.Create(FGScope);
|
||||
scope := TExecutionScope.Create(FGScope);
|
||||
|
||||
Memo1.Lines.Clear;
|
||||
Memo1.Lines.Add('--- Debug Evaluator Trace ---');
|
||||
@@ -193,8 +194,8 @@ begin
|
||||
[TAst.Identifier('n')],
|
||||
TAst.IfExpr(
|
||||
TAst.BinaryExpr(TAst.Identifier('n'), boLess, TAst.Constant(TScalar.FromInt64(2))),
|
||||
TAst.VarDecl(TAst.Identifier('Result'), TAst.Identifier('n')),
|
||||
TAst.VarDecl(
|
||||
TAst.Assign(TAst.Identifier('Result'), TAst.Identifier('n')),
|
||||
TAst.Assign(
|
||||
TAst.Identifier('Result'),
|
||||
TAst.BinaryExpr(
|
||||
TAst.FunctionCall(
|
||||
@@ -215,8 +216,39 @@ begin
|
||||
]
|
||||
);
|
||||
|
||||
root :=
|
||||
TAst.Block(
|
||||
[
|
||||
TAst.VarDecl(
|
||||
TAst.Identifier('fib'),
|
||||
TAst.LambdaExpr(
|
||||
[TAst.Identifier('n')],
|
||||
// The body is now a single ternary expression that returns a value.
|
||||
TAst.TernaryExpr(
|
||||
TAst.BinaryExpr(TAst.Identifier('n'), boLess, TAst.Constant(TScalar.FromInt64(2))),
|
||||
// The value if true.
|
||||
TAst.Identifier('n'),
|
||||
// The value if false.
|
||||
TAst.BinaryExpr(
|
||||
TAst.FunctionCall(
|
||||
TAst.Identifier('fib'),
|
||||
[TAst.BinaryExpr(TAst.Identifier('n'), boSubtract, TAst.Constant(TScalar.FromInt64(1)))]
|
||||
),
|
||||
boAdd,
|
||||
TAst.FunctionCall(
|
||||
TAst.Identifier('fib'),
|
||||
[TAst.BinaryExpr(TAst.Identifier('n'), boSubtract, TAst.Constant(TScalar.FromInt64(2)))]
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
),
|
||||
TAst.FunctionCall(TAst.Identifier('fib'), [TAst.Constant(TScalar.FromInt64(30))])
|
||||
]
|
||||
);
|
||||
|
||||
FLastAst := root;
|
||||
scope := T_ExecutionScope.Create(nil);
|
||||
scope := TExecutionScope.Create(nil);
|
||||
|
||||
visitor := TEvaluatorVisitor.Create(scope);
|
||||
result := root.Accept(visitor);
|
||||
@@ -270,6 +302,7 @@ begin
|
||||
Memo1.Lines.Add('--- Recursive factorial(20) ---');
|
||||
|
||||
sw.Start;
|
||||
{
|
||||
root :=
|
||||
TAst.Block(
|
||||
[
|
||||
@@ -279,7 +312,6 @@ begin
|
||||
[TAst.Identifier('n')],
|
||||
TAst.Block(
|
||||
[
|
||||
TAst.VarDecl(TAst.Identifier('Result'), TAst.Constant(TScalar.FromDouble(0))),
|
||||
TAst.IfExpr(
|
||||
TAst.BinaryExpr(TAst.Identifier('n'), boLess, TAst.Constant(TScalar.FromInt64(2))),
|
||||
TAst.Assign(TAst.Identifier('Result'), TAst.Constant(TScalar.FromInt64(1))),
|
||||
@@ -302,9 +334,41 @@ begin
|
||||
TAst.FunctionCall(TAst.Identifier('factorial'), [TAst.Constant(TScalar.FromInt64(20))])
|
||||
]
|
||||
);
|
||||
}
|
||||
root :=
|
||||
TAst.Block(
|
||||
[
|
||||
TAst.VarDecl(
|
||||
TAst.Identifier('factorial'),
|
||||
TAst.LambdaExpr(
|
||||
[TAst.Identifier('n')],
|
||||
TAst.Block(
|
||||
[
|
||||
TAst.Assign(
|
||||
TAst.Identifier('Result'),
|
||||
TAst.TernaryExpr(
|
||||
TAst.BinaryExpr(TAst.Identifier('n'), boLess, TAst.Constant(TScalar.FromInt64(2))),
|
||||
TAst.Constant(TScalar.FromInt64(1)),
|
||||
TAst.BinaryExpr(
|
||||
TAst.Identifier('n'),
|
||||
boMultiply,
|
||||
TAst.FunctionCall(
|
||||
TAst.Identifier('factorial'),
|
||||
[TAst.BinaryExpr(TAst.Identifier('n'), boSubtract, TAst.Constant(TScalar.FromInt64(1)))]
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
]
|
||||
)
|
||||
)
|
||||
),
|
||||
TAst.FunctionCall(TAst.Identifier('factorial'), [TAst.Constant(TScalar.FromInt64(20))])
|
||||
]
|
||||
);
|
||||
|
||||
FLastAst := root;
|
||||
scope := T_ExecutionScope.Create(nil);
|
||||
scope := TExecutionScope.Create(nil);
|
||||
|
||||
visitor := TEvaluatorVisitor.Create(scope);
|
||||
result := root.Accept(visitor);
|
||||
@@ -317,7 +381,6 @@ end;
|
||||
|
||||
procedure TForm1.Test1ButtonClick(Sender: TObject);
|
||||
var
|
||||
root: IAstNode;
|
||||
scope: IExecutionScope;
|
||||
visitor: IAstVisitor;
|
||||
result: TAstValue;
|
||||
@@ -330,20 +393,26 @@ begin
|
||||
Memo1.Lines.Add('--- Simple AST Execution ---');
|
||||
|
||||
sw.Start;
|
||||
root :=
|
||||
TAst.Block(
|
||||
[
|
||||
TAst.VarDecl(TAst.Identifier('a'), TAst.Constant(TScalar.FromInt64(10))),
|
||||
TAst.VarDecl(TAst.Identifier('b'), TAst.BinaryExpr(TAst.Identifier('a'), boMultiply, TAst.Constant(TScalar.FromInt64(2)))),
|
||||
TAst.BinaryExpr(TAst.Identifier('a'), boAdd, TAst.Identifier('b'))
|
||||
]
|
||||
var main :=
|
||||
TAst.LambdaExpr(
|
||||
[],
|
||||
TAst.Block(
|
||||
[
|
||||
TAst.VarDecl(TAst.Identifier('a'), TAst.Constant(TScalar.FromInt64(10))),
|
||||
TAst.VarDecl(
|
||||
TAst.Identifier('b'),
|
||||
TAst.BinaryExpr(TAst.Identifier('a'), boMultiply, TAst.Constant(TScalar.FromInt64(2)))
|
||||
),
|
||||
TAst.AssignResult(TAst.BinaryExpr(TAst.Identifier('a'), boAdd, TAst.Identifier('b')))
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
FLastAst := root;
|
||||
scope := T_ExecutionScope.Create(FGScope);
|
||||
FLastAst := main;
|
||||
scope := TExecutionScope.Create(FGScope);
|
||||
|
||||
visitor := TEvaluatorVisitor.Create(scope);
|
||||
result := root.Accept(visitor);
|
||||
result := TAst.FunctionCall(main, []).Accept(visitor);
|
||||
sw.Stop;
|
||||
|
||||
if not result.IsVoid then
|
||||
@@ -395,7 +464,7 @@ begin
|
||||
|
||||
FLastAst := root;
|
||||
|
||||
scope := T_ExecutionScope.Create(FGScope);
|
||||
scope := TExecutionScope.Create(FGScope);
|
||||
|
||||
visitor := TEvaluatorVisitor.Create(scope);
|
||||
result := root.Accept(visitor);
|
||||
@@ -520,7 +589,12 @@ begin
|
||||
exit;
|
||||
|
||||
if FLastAst <> nil then
|
||||
FWorkspace.BuildTree(FLastAst, TPointF.Create(X, Y));
|
||||
begin
|
||||
var visu := TVisualizationMode.vmDetailed;
|
||||
if FlowOnlyBox.IsChecked then
|
||||
visu := TVisualizationMode.vmControlFlow;
|
||||
FWorkspace.BuildTree(FLastAst, TPointF.Create(X, Y), visu);
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user