Macros refactoring
This commit is contained in:
+41
-19
@@ -146,6 +146,7 @@ implementation
|
||||
uses
|
||||
Myc.Data.Scalar.JSON,
|
||||
System.Diagnostics, // For TStopwatch
|
||||
System.TimeSpan,
|
||||
Myc.Ast.Json, // For TAstJson serialization
|
||||
Myc.Ast.Types, // Needed for TTypeRules
|
||||
System.IOUtils; // For TFile
|
||||
@@ -332,26 +333,47 @@ begin
|
||||
|
||||
Scope.Define(
|
||||
'print',
|
||||
TDataValue(
|
||||
function(const Args: TArray<TDataValue>): TDataValue
|
||||
var
|
||||
str: TStringBuilder;
|
||||
begin
|
||||
str := TStringBuilder.Create;
|
||||
try
|
||||
for var i := 0 to High(Args) do
|
||||
begin
|
||||
if Args[i].Kind = vkText then
|
||||
str.Append(Args[i].AsText)
|
||||
else
|
||||
str.Append(Args[i].ToString);
|
||||
end;
|
||||
Memo1.Lines.Add(str.ToString);
|
||||
finally
|
||||
str.Free;
|
||||
function(const Args: TArray<TDataValue>): TDataValue
|
||||
var
|
||||
str: TStringBuilder;
|
||||
begin
|
||||
str := TStringBuilder.Create;
|
||||
try
|
||||
for var i := 0 to High(Args) do
|
||||
begin
|
||||
if Args[i].Kind = vkText then
|
||||
str.Append(Args[i].AsText)
|
||||
else
|
||||
str.Append(Args[i].ToString);
|
||||
end;
|
||||
end
|
||||
)
|
||||
Memo1.Lines.Add(str.ToString);
|
||||
finally
|
||||
str.Free;
|
||||
end;
|
||||
end
|
||||
);
|
||||
|
||||
Scope.Define(
|
||||
'timestamp',
|
||||
function(const Args: TArray<TDataValue>): TDataValue
|
||||
begin
|
||||
Result := TStopwatch.GetTimeStamp div TTimeSpan.TicksPerMillisecond;
|
||||
end
|
||||
);
|
||||
|
||||
TMacroExpander.GlobalMacroRegistry.Define(
|
||||
TAstScript
|
||||
.Parse(
|
||||
'''
|
||||
(defmacro stopwatch [body]
|
||||
`(do
|
||||
(def start-time (timestamp))
|
||||
(def result ~body)
|
||||
(print "Stopwatch: " (- (timestamp) start-time) "ms" )
|
||||
result
|
||||
))
|
||||
''')
|
||||
.AsMacroDefinition
|
||||
);
|
||||
end
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user