RTL custom interface types as records, added callbacks (marshalled using virtual interfaces)

This commit is contained in:
Michael Schimmel
2025-12-12 02:28:25 +01:00
parent 8c60949ec9
commit e84ecfa2d2
8 changed files with 192 additions and 44 deletions
File diff suppressed because one or more lines are too long
+2 -1
View File
@@ -37,7 +37,8 @@ uses
Myc.Fmx.AstEditor.Handlers.Primitives in '..\Src\AST\Myc.Fmx.AstEditor.Handlers.Primitives.pas',
Myc.Fmx.AstEditor.Handlers.Control in '..\Src\AST\Myc.Fmx.AstEditor.Handlers.Control.pas',
Myc.Fmx.AstEditor.Handlers.Data in '..\Src\AST\Myc.Fmx.AstEditor.Handlers.Data.pas',
Myc.Ast.RTL.TypeRegistry in '..\Src\AST\Myc.Ast.RTL.TypeRegistry.pas';
Myc.Ast.RTL.TypeRegistry in '..\Src\AST\Myc.Ast.RTL.TypeRegistry.pas',
Myc.Trade.Broker in '..\Src\Myc.Trade.Broker.pas';
{$R *.res}
+1
View File
@@ -168,6 +168,7 @@
<DCCReference Include="..\Src\AST\Myc.Fmx.AstEditor.Handlers.Control.pas"/>
<DCCReference Include="..\Src\AST\Myc.Fmx.AstEditor.Handlers.Data.pas"/>
<DCCReference Include="..\Src\AST\Myc.Ast.RTL.TypeRegistry.pas"/>
<DCCReference Include="..\Src\Myc.Trade.Broker.pas"/>
<BuildConfiguration Include="Base">
<Key>Base</Key>
</BuildConfiguration>
+8 -8
View File
@@ -245,15 +245,15 @@ object Form1: TForm1
Touch.InteractiveGestures = [Pan, LongTap, DoubleTap]
DataDetectorTypes = []
Lines.Strings = (
'(do '
' (defmacro test [body] `(fn [] ~body ))'
' (defmacro test2 [body] `(fn [] ~body ))'
' (test2 "hi")'
''
' (def debug-mode :true)'
'(do'
' ;; Broker mit 10.000 Startkapital erstellen'
' (def broker (create-broker 10000.0))'
' '
' (stopwatch '
' (if debug-mode (print "Debug an")))'
' ;; Aktien kaufen: 10 St'#252'ck von "AAPL" zu 150.0'
' ((.Buy broker) "AAPL" 10 150.0)'
' ((.Buy broker) "MSFT" 5 50.0)'
' '
' ((.ListPositions broker) (fn [t] (print t)))'
')')
StyledSettings = [Size, Style, FontColor]
TextSettings.Font.Family = 'Consolas'
+18 -9
View File
@@ -114,6 +114,7 @@ type
procedure RTLListViewChange(Sender: TObject);
private
FCurrUnboundAst: IAstNode;
FCurrCompiled: ILambdaExpressionNode;
FCurrExec: TCompiledFunction;
FEnvironment: TAstEnvironment;
FAstEditor: TAstEditor; // Die Komponente (Facade)
@@ -141,6 +142,7 @@ implementation
uses
Myc.Data.Scalar.JSON,
Myc.Trade.Broker,
System.Diagnostics, // For TStopwatch
System.TimeSpan,
Myc.Ast.Json, // For TAstJson serialization
@@ -306,7 +308,8 @@ begin
try
// Use Compile directly. Errors will be raised as ECompilationFailed.
// We catch them to print to log, but allow flow to continue so UI can be updated.
FCurrExec := FEnvironment.Compile(ANode);
FCurrCompiled := FEnvironment.Compile(ANode);
FCurrExec := FEnvironment.Link(FCurrCompiled);
if Assigned(FCurrExec.Func) then
begin
@@ -599,7 +602,8 @@ begin
Memo1.Lines.Add('--- Naive recursive fib with AST---');
var fibExec := TestEnv.Compile(TAst.FunctionCall(TAst.Identifier('fib'), [TAst.Constant(25)])).Func;
FCurrCompiled := TestEnv.Compile(TAst.FunctionCall(TAst.Identifier('fib'), [TAst.Constant(25)]));
var fibExec := TestEnv.Link(FCurrCompiled).Func;
sw := TStopwatch.StartNew;
result := fibExec([]);
@@ -614,7 +618,7 @@ begin
memoizeTest.Define('fib', TAstScript.Parse('(Memoize fib)'));
var memoizeExec := memoizeTest.Compile(TAstScript.Parse('(fib 25)')).Func;
var memoizeExec := memoizeTest.Link(memoizeTest.Compile(TAstScript.Parse('(fib 25)'))).Func;
sw := TStopwatch.StartNew;
result := memoizeExec([]);
@@ -757,7 +761,8 @@ begin
FCurrUnboundAst := converter.Deserialize(jsonObj);
// Run the full pipeline via environment
FCurrExec := FEnvironment.Compile(FCurrUnboundAst);
FCurrCompiled := FEnvironment.Compile(FCurrUnboundAst);
FCurrExec := FEnvironment.Link(FCurrCompiled);
Memo1.Lines.Add('AST deserialized and bound successfully from JSON.');
Memo1.Lines.Add('You can now visualize it (Middle Mouse Click) or pretty-print it.');
@@ -822,7 +827,7 @@ begin
var seriesAddress := env.RootScope.Define('current_series', TDataValue.Void);
var callExec := env.Compile(TAst.FunctionCall(TAst.Identifier('maCrossStrategy'), [TAst.Identifier('current_series')])).Func;
var callExec := env.Link(env.Compile(TAst.FunctionCall(TAst.Identifier('maCrossStrategy'), [TAst.Identifier('current_series')]))).Func;
// Simulation Loop
Memo1.Lines.Clear;
@@ -1169,7 +1174,8 @@ end;
procedure TForm1.Test1ButtonClick(Sender: TObject);
begin
FCurrUnboundAst := TAstScript.Parse('(do (print txt))');
var fn := FEnvironment.Compile(FCurrUnboundAst, [TAst.Identifier('txt')]).Func;
FCurrCompiled := FEnvironment.Compile(FCurrUnboundAst, [TAst.Identifier('txt')]);
var fn := FEnvironment.Link(FCurrCompiled).Func;
fn(['Hello World']);
FEnvironment.RootScope.Define('fn', fn);
UpdateScript;
@@ -1247,14 +1253,17 @@ begin
Memo1.Lines.Clear;
Memo1.Lines.Add('--- AST Dump ---');
if FCurrUnboundAst = nil then
if FCurrCompiled = nil then
begin
Memo1.Lines.Add('No AST available.');
Memo1.Lines.Add('No compiled AST available.');
exit;
end;
// Dump the raw unbound AST first
TAstDumper.Dump(FCurrUnboundAst, Memo1.Lines);
TAstDumper.Dump(FCurrCompiled, Memo1.Lines);
end;
initialization
TAst.RegisterLibrary(RegisterBroker);
end.