Macro expander integrated in binder
This commit is contained in:
@@ -345,17 +345,29 @@ begin
|
||||
if not (pair.JsonValue is TJSONObject) then
|
||||
continue;
|
||||
|
||||
// First, deserialize the AST node from JSON.
|
||||
funcAst := converter.Deserialize(pair.JsonValue as TJSONObject);
|
||||
// Execute the lambda to get a callable function value
|
||||
|
||||
var adr := scopeDescr.FindSymbol(pair.JsonString.Value);
|
||||
if adr.Kind = akUnresolved then
|
||||
begin
|
||||
funcValue := ExecuteAst(funcAst, FGScope);
|
||||
FGScope.Define(pair.JsonString.Value, funcValue);
|
||||
Memo1.Lines.Add(Format('Defined function "%s"', [pair.JsonString.Value]));
|
||||
// Distinguish between loading a macro and loading a function.
|
||||
if funcAst is TMacroDefinitionNode then
|
||||
begin
|
||||
// Macros are stored as raw AST nodes in the scope.
|
||||
FGScope.Define(pair.JsonString.Value, TDataValue.FromIntf<IAstNode>(funcAst));
|
||||
Memo1.Lines.Add(Format('Defined macro "%s"', [pair.JsonString.Value]));
|
||||
end
|
||||
else
|
||||
begin
|
||||
// Functions (lambdas) must be executed to create a callable closure.
|
||||
funcValue := ExecuteAst(funcAst, FGScope);
|
||||
FGScope.Define(pair.JsonString.Value, funcValue);
|
||||
Memo1.Lines.Add(Format('Defined function "%s"', [pair.JsonString.Value]));
|
||||
end;
|
||||
end
|
||||
else
|
||||
Memo1.Lines.Add(Format('Function "%s" already defined', [pair.JsonString.Value]));
|
||||
Memo1.Lines.Add(Format('Symbol "%s" already defined', [pair.JsonString.Value]));
|
||||
end;
|
||||
finally
|
||||
jsonObj.Free;
|
||||
|
||||
Reference in New Issue
Block a user