Macro expander integrated in binder
This commit is contained in:
@@ -345,17 +345,29 @@ begin
|
|||||||
if not (pair.JsonValue is TJSONObject) then
|
if not (pair.JsonValue is TJSONObject) then
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
// First, deserialize the AST node from JSON.
|
||||||
funcAst := converter.Deserialize(pair.JsonValue as TJSONObject);
|
funcAst := converter.Deserialize(pair.JsonValue as TJSONObject);
|
||||||
// Execute the lambda to get a callable function value
|
|
||||||
var adr := scopeDescr.FindSymbol(pair.JsonString.Value);
|
var adr := scopeDescr.FindSymbol(pair.JsonString.Value);
|
||||||
if adr.Kind = akUnresolved then
|
if adr.Kind = akUnresolved then
|
||||||
begin
|
begin
|
||||||
funcValue := ExecuteAst(funcAst, FGScope);
|
// Distinguish between loading a macro and loading a function.
|
||||||
FGScope.Define(pair.JsonString.Value, funcValue);
|
if funcAst is TMacroDefinitionNode then
|
||||||
Memo1.Lines.Add(Format('Defined function "%s"', [pair.JsonString.Value]));
|
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
|
end
|
||||||
else
|
else
|
||||||
Memo1.Lines.Add(Format('Function "%s" already defined', [pair.JsonString.Value]));
|
Memo1.Lines.Add(Format('Symbol "%s" already defined', [pair.JsonString.Value]));
|
||||||
end;
|
end;
|
||||||
finally
|
finally
|
||||||
jsonObj.Free;
|
jsonObj.Free;
|
||||||
|
|||||||
+127
-167
@@ -6,6 +6,7 @@ uses
|
|||||||
System.SysUtils,
|
System.SysUtils,
|
||||||
System.Classes,
|
System.Classes,
|
||||||
System.Generics.Collections,
|
System.Generics.Collections,
|
||||||
|
Myc.Data.Scalar,
|
||||||
Myc.Data.Value,
|
Myc.Data.Value,
|
||||||
Myc.Ast.Nodes,
|
Myc.Ast.Nodes,
|
||||||
Myc.Ast.Visitor,
|
Myc.Ast.Visitor,
|
||||||
@@ -21,15 +22,16 @@ type
|
|||||||
TAstBinder = class; // Forward declaration
|
TAstBinder = class; // Forward declaration
|
||||||
|
|
||||||
// This visitor handles the expansion of a single macro body (` `...`).
|
// This visitor handles the expansion of a single macro body (` `...`).
|
||||||
// It correctly distinguishes between syntactic unquoting and value unquoting.
|
|
||||||
TExpansionVisitor = class(TAstTransformer)
|
TExpansionVisitor = class(TAstTransformer)
|
||||||
private
|
private
|
||||||
FBinder: TAstBinder;
|
FBinder: TAstBinder;
|
||||||
FMacroScope: IExecutionScope;
|
FMacroScope: IExecutionScope;
|
||||||
|
function TransformAndSpliceNodes(const ANodes: TArray<IAstNode>): TArray<IAstNode>;
|
||||||
protected
|
protected
|
||||||
function VisitUnquote(const Node: IUnquoteNode): TDataValue; override;
|
function VisitUnquote(const Node: IUnquoteNode): TDataValue; override;
|
||||||
function VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TDataValue; override;
|
function VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TDataValue; override;
|
||||||
function VisitFunctionCall(const Node: IFunctionCallNode): TDataValue; override;
|
function VisitFunctionCall(const Node: IFunctionCallNode): TDataValue; override;
|
||||||
|
function VisitBlockExpression(const Node: IBlockExpressionNode): TDataValue; override;
|
||||||
public
|
public
|
||||||
constructor Create(const ABinder: TAstBinder; const AMacroScope: IExecutionScope);
|
constructor Create(const ABinder: TAstBinder; const AMacroScope: IExecutionScope);
|
||||||
end;
|
end;
|
||||||
@@ -53,7 +55,9 @@ type
|
|||||||
FNextIsTail: Boolean;
|
FNextIsTail: Boolean;
|
||||||
FBoxedDeclarations: THashSet<IVariableDeclarationNode>;
|
FBoxedDeclarations: THashSet<IVariableDeclarationNode>;
|
||||||
FEvaluatorFactory: TEvaluatorFactory;
|
FEvaluatorFactory: TEvaluatorFactory;
|
||||||
FMacros: TDictionary<string, IMacroDefinitionNode>;
|
// Operator folding maps
|
||||||
|
FBinaryOperators: TDictionary<string, TScalar.TBinaryOp>;
|
||||||
|
FUnaryOperators: TDictionary<string, TScalar.TUnaryOp>;
|
||||||
|
|
||||||
procedure EnterScope;
|
procedure EnterScope;
|
||||||
procedure ExitScope;
|
procedure ExitScope;
|
||||||
@@ -152,6 +156,44 @@ begin
|
|||||||
FMacroScope := AMacroScope;
|
FMacroScope := AMacroScope;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TExpansionVisitor.TransformAndSpliceNodes(const ANodes: TArray<IAstNode>): TArray<IAstNode>;
|
||||||
|
var
|
||||||
|
newList: TList<IAstNode>;
|
||||||
|
nodeToSplice: IAstNode;
|
||||||
|
begin
|
||||||
|
newList := TList<IAstNode>.Create;
|
||||||
|
try
|
||||||
|
for var node in ANodes do
|
||||||
|
begin
|
||||||
|
if (node is TUnquoteSplicingNode) then
|
||||||
|
begin
|
||||||
|
var spliceExpr := (node as TUnquoteSplicingNode).Expression;
|
||||||
|
var evaluatedSpliceValue := VisitUnquote(TAst.Unquote(spliceExpr));
|
||||||
|
|
||||||
|
if (not evaluatedSpliceValue.IsVoid) and (evaluatedSpliceValue.Kind = vkInterface) then
|
||||||
|
begin
|
||||||
|
nodeToSplice := evaluatedSpliceValue.AsIntf<IAstNode>;
|
||||||
|
if (nodeToSplice is TBlockExpressionNode) then
|
||||||
|
newList.AddRange((nodeToSplice as TBlockExpressionNode).Expressions)
|
||||||
|
else
|
||||||
|
raise Exception.Create('Expression inside unquote-splicing (`~@`) must be a list of nodes (a block).');
|
||||||
|
end
|
||||||
|
else
|
||||||
|
raise Exception.Create('Expression inside unquote-splicing (`~@`) must evaluate to a list of nodes (a block).');
|
||||||
|
end
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
var transformedValue := node.Accept(self);
|
||||||
|
if not transformedValue.IsVoid then
|
||||||
|
newList.Add(transformedValue.AsIntf<IAstNode>);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
Result := newList.ToArray;
|
||||||
|
finally
|
||||||
|
newList.Free;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
function TExpansionVisitor.VisitUnquote(const Node: IUnquoteNode): TDataValue;
|
function TExpansionVisitor.VisitUnquote(const Node: IUnquoteNode): TDataValue;
|
||||||
var
|
var
|
||||||
value: TDataValue;
|
value: TDataValue;
|
||||||
@@ -160,82 +202,49 @@ var
|
|||||||
begin
|
begin
|
||||||
expr := Node.Expression;
|
expr := Node.Expression;
|
||||||
|
|
||||||
// Check if the expression is a simple identifier that refers to a macro parameter.
|
|
||||||
if (expr is TIdentifierNode) then
|
if (expr is TIdentifierNode) then
|
||||||
begin
|
begin
|
||||||
addr := FMacroScope.CreateDescriptor.FindSymbol((expr as TIdentifierNode).Name);
|
addr := FMacroScope.CreateDescriptor.FindSymbol((expr as TIdentifierNode).Name);
|
||||||
if (addr.Kind = akLocalOrParent) and (addr.ScopeDepth = 0) then
|
if (addr.Kind = akLocalOrParent) and (addr.ScopeDepth = 0) then
|
||||||
begin
|
begin
|
||||||
// It's a macro parameter. Get its value, which is the AST passed as an argument.
|
|
||||||
var argValue := FMacroScope.Values[addr];
|
var argValue := FMacroScope.Values[addr];
|
||||||
if argValue.Kind = vkInterface then
|
if argValue.Kind = vkInterface then
|
||||||
begin
|
begin
|
||||||
// This is syntactic unquoting. Return the AST directly.
|
|
||||||
Result := argValue;
|
Result := argValue;
|
||||||
exit;
|
exit;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// If it's not a parameter or the parameter doesn't hold an AST, it's value unquoting.
|
|
||||||
// Evaluate the expression at compile time using the binder's context.
|
|
||||||
value := FBinder.EvaluateAtCompileTime(expr);
|
value := FBinder.EvaluateAtCompileTime(expr);
|
||||||
|
|
||||||
// Convert the resulting value back into an AST node to splice it into the tree.
|
|
||||||
if value.Kind in [vkScalar, vkText, vkVoid] then
|
if value.Kind in [vkScalar, vkText, vkVoid] then
|
||||||
Result := TDataValue.FromIntf<IAstNode>(TAst.Constant(value))
|
Result := TDataValue.FromIntf<IAstNode>(TAst.Constant(value))
|
||||||
else
|
else
|
||||||
// For now, other complex types are not supported for value unquoting.
|
|
||||||
raise Exception.CreateFmt('Cannot unquote complex value of type %s at compile time.', [value.Kind.ToString]);
|
raise Exception.CreateFmt('Cannot unquote complex value of type %s at compile time.', [value.Kind.ToString]);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TExpansionVisitor.VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TDataValue;
|
function TExpansionVisitor.VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TDataValue;
|
||||||
begin
|
begin
|
||||||
// Similar to VisitUnquote, but we expect the result to be a list of nodes.
|
raise Exception.Create('Unquote-splicing (`~@`) can only appear inside a list form (e.g., a function call or a `do` block).');
|
||||||
// We evaluate the inner expression. The result is a TDataValue.
|
|
||||||
// We don't do anything with it here; we just return it.
|
|
||||||
// The VisitFunctionCall override will check for this and perform the "splicing".
|
|
||||||
var value := VisitUnquote(TAst.Unquote(Node.Expression)).AsIntf<IAstNode>;
|
|
||||||
Result := TDataValue.FromIntf<IUnquoteSplicingNode>(TAst.UnquoteSplicing(value));
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TExpansionVisitor.VisitFunctionCall(const Node: IFunctionCallNode): TDataValue;
|
function TExpansionVisitor.VisitFunctionCall(const Node: IFunctionCallNode): TDataValue;
|
||||||
var
|
var
|
||||||
newArgs: TList<IAstNode>;
|
newArgs: TArray<IAstNode>;
|
||||||
transformedArg: IAstNode;
|
transformedCallee: IAstNode;
|
||||||
splicingNode: IUnquoteSplicingNode;
|
|
||||||
begin
|
begin
|
||||||
// This override handles splicing arguments (~@).
|
transformedCallee := Self.Accept(Node.Callee).AsIntf<IAstNode>;
|
||||||
newArgs := TList<IAstNode>.Create;
|
newArgs := TransformAndSpliceNodes(Node.Arguments);
|
||||||
try
|
Result := TDataValue.FromIntf<IFunctionCallNode>(TAst.FunctionCall(transformedCallee, newArgs));
|
||||||
for var arg in Node.Arguments do
|
end;
|
||||||
begin
|
|
||||||
var transformedArgValue := Self.Accept(arg); // This might return an UnquoteSplicing node.
|
|
||||||
if transformedArgValue.IsVoid then
|
|
||||||
continue;
|
|
||||||
|
|
||||||
transformedArg := transformedArgValue.AsIntf<IAstNode>;
|
function TExpansionVisitor.VisitBlockExpression(const Node: IBlockExpressionNode): TDataValue;
|
||||||
|
var
|
||||||
if (transformedArg is TUnquoteSplicingNode) then
|
newExprs: TArray<IAstNode>;
|
||||||
begin
|
begin
|
||||||
splicingNode := transformedArg as TUnquoteSplicingNode;
|
newExprs := TransformAndSpliceNodes(Node.Expressions);
|
||||||
// The inner expression should have been evaluated to an AST block
|
Result := TDataValue.FromIntf<IBlockExpressionNode>(TAst.Block(newExprs));
|
||||||
if (splicingNode.Expression is TBlockExpressionNode) then
|
|
||||||
newArgs.AddRange((splicingNode.Expression as TBlockExpressionNode).Expressions)
|
|
||||||
else
|
|
||||||
raise Exception.Create('Expression inside unquote-splicing (`~@`) must evaluate to a list of nodes (a block).');
|
|
||||||
end
|
|
||||||
else
|
|
||||||
begin
|
|
||||||
newArgs.Add(transformedArg);
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
|
|
||||||
var transformedCallee := Self.Accept(Node.Callee).AsIntf<IAstNode>;
|
|
||||||
Result := TDataValue.FromIntf<IFunctionCallNode>(TAst.FunctionCall(transformedCallee, newArgs.ToArray));
|
|
||||||
finally
|
|
||||||
newArgs.Free;
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ TBoundIdentifierNode }
|
{ TBoundIdentifierNode }
|
||||||
@@ -312,6 +321,8 @@ end;
|
|||||||
{ TAstBinder }
|
{ TAstBinder }
|
||||||
|
|
||||||
constructor TAstBinder.Create(const AInitialScope: IExecutionScope; const AEvaluatorFactory: TEvaluatorFactory);
|
constructor TAstBinder.Create(const AInitialScope: IExecutionScope; const AEvaluatorFactory: TEvaluatorFactory);
|
||||||
|
var
|
||||||
|
op: TScalar.TBinaryOp;
|
||||||
begin
|
begin
|
||||||
inherited Create;
|
inherited Create;
|
||||||
Assert(Assigned(AInitialScope));
|
Assert(Assigned(AInitialScope));
|
||||||
@@ -325,15 +336,24 @@ begin
|
|||||||
FIsTailStack := TStack<Boolean>.Create;
|
FIsTailStack := TStack<Boolean>.Create;
|
||||||
FNextIsTail := True;
|
FNextIsTail := True;
|
||||||
FBoxedDeclarations := nil;
|
FBoxedDeclarations := nil;
|
||||||
FMacros := TDictionary<string, IMacroDefinitionNode>.Create;
|
|
||||||
|
// Initialize operator folding maps
|
||||||
|
FBinaryOperators := TDictionary<string, TScalar.TBinaryOp>.Create;
|
||||||
|
for op := Low(TScalar.TBinaryOp) to High(TScalar.TBinaryOp) do
|
||||||
|
FBinaryOperators.Add(op.ToString, op);
|
||||||
|
|
||||||
|
FUnaryOperators := TDictionary<string, TScalar.TUnaryOp>.Create;
|
||||||
|
FUnaryOperators.Add('not', TScalar.TUnaryOp.Not);
|
||||||
|
// Note: '-' is handled as a special case in VisitFunctionCall
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TAstBinder.Destroy;
|
destructor TAstBinder.Destroy;
|
||||||
begin
|
begin
|
||||||
|
FUnaryOperators.Free;
|
||||||
|
FBinaryOperators.Free;
|
||||||
FIsTailStack.Free;
|
FIsTailStack.Free;
|
||||||
FUpvalueStack.Free;
|
FUpvalueStack.Free;
|
||||||
FBoxedDeclarations.Free;
|
FBoxedDeclarations.Free;
|
||||||
FMacros.Free;
|
|
||||||
inherited;
|
inherited;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -364,20 +384,11 @@ var
|
|||||||
evaluator: IEvaluatorVisitor;
|
evaluator: IEvaluatorVisitor;
|
||||||
tempInitScope: IExecutionScope;
|
tempInitScope: IExecutionScope;
|
||||||
begin
|
begin
|
||||||
// Create a temporary scope that represents the binder's current lexical context.
|
|
||||||
tempInitScope := TScope.CreateScope(FInitialScope.Parent, FCurrentDescriptor, nil);
|
tempInitScope := TScope.CreateScope(FInitialScope.Parent, FCurrentDescriptor, nil);
|
||||||
|
|
||||||
// 1. Bind the sub-tree in a new binder, using the current scope descriptor.
|
|
||||||
subBinder := TAstBinder.Create(tempInitScope, FEvaluatorFactory);
|
subBinder := TAstBinder.Create(tempInitScope, FEvaluatorFactory);
|
||||||
boundSubAst := subBinder.Execute(ANode, subDescriptor);
|
boundSubAst := subBinder.Execute(ANode, subDescriptor);
|
||||||
|
|
||||||
// 2. Create the execution scope for this specific evaluation.
|
|
||||||
evalScope := subDescriptor.CreateScope(tempInitScope);
|
evalScope := subDescriptor.CreateScope(tempInitScope);
|
||||||
|
|
||||||
// 3. Create the correct evaluator (Debug/Production) using the injected factory.
|
|
||||||
evaluator := FEvaluatorFactory(evalScope);
|
evaluator := FEvaluatorFactory(evalScope);
|
||||||
|
|
||||||
// 4. Execute and return the resulting value.
|
|
||||||
Result := evaluator.Execute(boundSubAst);
|
Result := evaluator.Execute(boundSubAst);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -388,15 +399,10 @@ begin
|
|||||||
EnterScope;
|
EnterScope;
|
||||||
try
|
try
|
||||||
var transformedValue := Accept(RootNode);
|
var transformedValue := Accept(RootNode);
|
||||||
|
|
||||||
// Handle the case where the entire script was just declarations.
|
|
||||||
if transformedValue.IsVoid then
|
if transformedValue.IsVoid then
|
||||||
// Return a valid, empty AST.
|
|
||||||
Result := TAst.Block([])
|
Result := TAst.Block([])
|
||||||
else
|
else
|
||||||
// The script produced a valid, executable AST.
|
|
||||||
Result := transformedValue.AsIntf<IAstNode>;
|
Result := transformedValue.AsIntf<IAstNode>;
|
||||||
|
|
||||||
Descriptor := FCurrentDescriptor;
|
Descriptor := FCurrentDescriptor;
|
||||||
finally
|
finally
|
||||||
ExitScope;
|
ExitScope;
|
||||||
@@ -408,101 +414,89 @@ end;
|
|||||||
|
|
||||||
function TAstBinder.VisitMacroDefinition(const Node: IMacroDefinitionNode): TDataValue;
|
function TAstBinder.VisitMacroDefinition(const Node: IMacroDefinitionNode): TDataValue;
|
||||||
begin
|
begin
|
||||||
// Register the macro for the current binder instance.
|
FCurrentDescriptor.DefineMacro(Node.Name.Name, Node);
|
||||||
FMacros.AddOrSetValue(Node.Name.Name, Node);
|
|
||||||
|
|
||||||
// Return an empty block node. This is a valid "no-op" node
|
|
||||||
// that is simply ignored by the evaluator and safely handled by all visitors.
|
|
||||||
Result := TDataValue.Void;
|
Result := TDataValue.Void;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TAstBinder.VisitFunctionCall(const Node: IFunctionCallNode): TDataValue;
|
function TAstBinder.VisitFunctionCall(const Node: IFunctionCallNode): TDataValue;
|
||||||
var
|
var
|
||||||
macroDef: IMacroDefinitionNode;
|
|
||||||
calleeIdentifier: TIdentifierNode;
|
calleeIdentifier: TIdentifierNode;
|
||||||
i: Integer;
|
binaryOp: TScalar.TBinaryOp;
|
||||||
expansionScope: IExecutionScope;
|
unaryOp: TScalar.TUnaryOp;
|
||||||
expander: TExpansionVisitor;
|
macroDef: IMacroDefinitionNode;
|
||||||
expandedBody: IAstNode;
|
|
||||||
isTailCall: Boolean;
|
|
||||||
callee: IAstNode;
|
|
||||||
args: TArray<IAstNode>;
|
|
||||||
boundCall: IFunctionCallNode;
|
|
||||||
begin
|
begin
|
||||||
// First, check if this is a macro call.
|
// --- Optimization: Operator Folding ---
|
||||||
if (Node.Callee is TIdentifierNode) then
|
if (Node.Callee is TIdentifierNode) then
|
||||||
begin
|
begin
|
||||||
calleeIdentifier := Node.Callee as TIdentifierNode;
|
calleeIdentifier := Node.Callee as TIdentifierNode;
|
||||||
if FMacros.TryGetValue(calleeIdentifier.Name, macroDef) then
|
|
||||||
|
// Try to fold binary operators
|
||||||
|
if FBinaryOperators.TryGetValue(calleeIdentifier.Name, binaryOp) then
|
||||||
begin
|
begin
|
||||||
// It's a macro. Expand it now.
|
if Length(Node.Arguments) = 2 then
|
||||||
expansionScope := TAst.CreateScope(nil);
|
|
||||||
|
|
||||||
// Check for variadic macro parameter
|
|
||||||
var params := macroDef.Parameters;
|
|
||||||
var lastParamName := '';
|
|
||||||
if Length(params) > 0 then
|
|
||||||
lastParamName := params[High(params)].Name;
|
|
||||||
|
|
||||||
if (Length(params) > 1) and (lastParamName.StartsWith('&')) then
|
|
||||||
begin
|
begin
|
||||||
var requiredArgs := Length(params) - 1;
|
FNextIsTail := False;
|
||||||
if Length(Node.Arguments) < requiredArgs then
|
var left := Accept(Node.Arguments[0]).AsIntf<IAstNode>;
|
||||||
raise Exception.CreateFmt(
|
var right := Accept(Node.Arguments[1]).AsIntf<IAstNode>;
|
||||||
'Macro %s expects at least %d arguments, but got %d',
|
Result := TDataValue.FromIntf<IAstNode>(TAst.BinaryExpr(left, binaryOp, right));
|
||||||
[calleeIdentifier.Name, requiredArgs, Length(Node.Arguments)]);
|
exit;
|
||||||
|
|
||||||
// Bind fixed arguments
|
|
||||||
for i := 0 to requiredArgs - 1 do
|
|
||||||
expansionScope.Define(params[i].Name, TDataValue.FromIntf<IAstNode>(Node.Arguments[i]));
|
|
||||||
|
|
||||||
// Bind rest arguments as a list (AST block)
|
|
||||||
var restArgs: TArray<IAstNode>;
|
|
||||||
SetLength(restArgs, Length(Node.Arguments) - requiredArgs);
|
|
||||||
for i := 0 to High(restArgs) do
|
|
||||||
restArgs[i] := Node.Arguments[requiredArgs + i];
|
|
||||||
expansionScope.Define(lastParamName.Substring(1), TDataValue.FromIntf<IAstNode>(TAst.Block(restArgs)));
|
|
||||||
end
|
|
||||||
else
|
|
||||||
begin
|
|
||||||
if Length(Node.Arguments) <> Length(params) then
|
|
||||||
raise Exception.CreateFmt(
|
|
||||||
'Macro %s expects %d arguments, but got %d',
|
|
||||||
[calleeIdentifier.Name, Length(params), Length(Node.Arguments)]);
|
|
||||||
|
|
||||||
for i := 0 to High(params) do
|
|
||||||
expansionScope.Define(params[i].Name, TDataValue.FromIntf<IAstNode>(Node.Arguments[i]));
|
|
||||||
end;
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Try to fold unary operators
|
||||||
|
if FUnaryOperators.TryGetValue(calleeIdentifier.Name, unaryOp) then
|
||||||
|
begin
|
||||||
|
if Length(Node.Arguments) = 1 then
|
||||||
|
begin
|
||||||
|
FNextIsTail := False;
|
||||||
|
var right := Accept(Node.Arguments[0]).AsIntf<IAstNode>;
|
||||||
|
Result := TDataValue.FromIntf<IAstNode>(TAst.UnaryExpr(unaryOp, right));
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Special case for negation '-'
|
||||||
|
if (calleeIdentifier.Name = '-') and (Length(Node.Arguments) = 1) then
|
||||||
|
begin
|
||||||
|
FNextIsTail := False;
|
||||||
|
var right := Accept(Node.Arguments[0]).AsIntf<IAstNode>;
|
||||||
|
Result := TDataValue.FromIntf<IAstNode>(TAst.UnaryExpr(TScalar.TUnaryOp.Negate, right));
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// --- Macro Expansion ---
|
||||||
|
macroDef := FCurrentDescriptor.FindMacro(calleeIdentifier.Name);
|
||||||
|
if macroDef <> nil then
|
||||||
|
begin
|
||||||
|
var expansionScope := TAst.CreateScope(nil);
|
||||||
|
var params := macroDef.Parameters;
|
||||||
|
if Length(Node.Arguments) <> Length(params) then
|
||||||
|
raise Exception.CreateFmt(
|
||||||
|
'Macro %s expects %d arguments, but got %d',
|
||||||
|
[calleeIdentifier.Name, Length(params), Length(Node.Arguments)]);
|
||||||
|
|
||||||
|
for var i := 0 to High(params) do
|
||||||
|
expansionScope.Define(params[i].Name, TDataValue.FromIntf<IAstNode>(Node.Arguments[i]));
|
||||||
|
|
||||||
// A macro body MUST be a quasiquote.
|
|
||||||
if not (macroDef.Body is TQuasiquoteNode) then
|
if not (macroDef.Body is TQuasiquoteNode) then
|
||||||
raise Exception.CreateFmt('Macro body for "%s" must be a quasiquoted expression.', [calleeIdentifier.Name]);
|
raise Exception.CreateFmt('Macro body for "%s" must be a quasiquoted expression.', [calleeIdentifier.Name]);
|
||||||
|
|
||||||
// Use the dedicated expansion visitor to process the CONTENT of the macro body.
|
|
||||||
var quasiquoteBody := macroDef.Body as TQuasiquoteNode;
|
var quasiquoteBody := macroDef.Body as TQuasiquoteNode;
|
||||||
expander := TExpansionVisitor.Create(Self, expansionScope);
|
var expander := TExpansionVisitor.Create(Self, expansionScope);
|
||||||
expandedBody := expander.Execute(quasiquoteBody.Expression);
|
var expandedBody := expander.Execute(quasiquoteBody.Expression);
|
||||||
|
|
||||||
// Bind the newly generated AST fragment.
|
|
||||||
var boundExpandedBody := Self.Accept(expandedBody).AsIntf<IAstNode>;
|
var boundExpandedBody := Self.Accept(expandedBody).AsIntf<IAstNode>;
|
||||||
|
|
||||||
// Wrap the result in a TMacroExpansionNode.
|
|
||||||
// The original 'Node' is passed to preserve the call site information for tools.
|
|
||||||
var macroNode := TMacroExpansionNode.Create(Node, boundExpandedBody);
|
var macroNode := TMacroExpansionNode.Create(Node, boundExpandedBody);
|
||||||
|
|
||||||
Result := TDataValue.FromIntf<IMacroExpansionNode>(macroNode);
|
Result := TDataValue.FromIntf<IMacroExpansionNode>(macroNode);
|
||||||
exit;
|
exit;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// It's a regular function call, proceed with normal binding.
|
// --- Default: Bind as a standard function call ---
|
||||||
isTailCall := FIsTailStack.Peek;
|
var isTailCall := FIsTailStack.Peek;
|
||||||
|
|
||||||
FNextIsTail := False;
|
FNextIsTail := False;
|
||||||
callee := Accept(Node.Callee).AsIntf<IAstNode>;
|
var callee := Accept(Node.Callee).AsIntf<IAstNode>;
|
||||||
args := TransformNodes<IAstNode>(Node.Arguments);
|
var args := TransformNodes<IAstNode>(Node.Arguments);
|
||||||
|
var boundCall := TBoundFunctionCallNode.Create(Node, callee, args, isTailCall);
|
||||||
boundCall := TBoundFunctionCallNode.Create(Node, callee, args, isTailCall);
|
|
||||||
Result := TDataValue.FromIntf<IFunctionCallNode>(boundCall);
|
Result := TDataValue.FromIntf<IFunctionCallNode>(boundCall);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -513,22 +507,11 @@ var
|
|||||||
boundExpandedBody: IAstNode;
|
boundExpandedBody: IAstNode;
|
||||||
boundOriginalCall: IFunctionCallNode;
|
boundOriginalCall: IFunctionCallNode;
|
||||||
begin
|
begin
|
||||||
// This handles the case where a macro expansion node is injected into the
|
|
||||||
// tree before binding. The binder must recursively bind its contents.
|
|
||||||
|
|
||||||
// 1. Bind the components of the original call site.
|
|
||||||
boundCallee := Accept(Node.Callee).AsIntf<IAstNode>;
|
boundCallee := Accept(Node.Callee).AsIntf<IAstNode>;
|
||||||
boundArgs := TransformNodes<IAstNode>(Node.Arguments);
|
boundArgs := TransformNodes<IAstNode>(Node.Arguments);
|
||||||
|
|
||||||
// 2. Bind the expanded body.
|
|
||||||
boundExpandedBody := Accept(Node.ExpandedBody).AsIntf<IAstNode>;
|
boundExpandedBody := Accept(Node.ExpandedBody).AsIntf<IAstNode>;
|
||||||
|
|
||||||
// 3. Re-create the IFunctionCallNode part of the original call with bound children.
|
|
||||||
boundOriginalCall := TAst.FunctionCall(boundCallee, boundArgs);
|
boundOriginalCall := TAst.FunctionCall(boundCallee, boundArgs);
|
||||||
|
|
||||||
// 4. Create a new TMacroExpansionNode that wraps the now-bound components.
|
|
||||||
var newMacroNode := TMacroExpansionNode.Create(boundOriginalCall, boundExpandedBody);
|
var newMacroNode := TMacroExpansionNode.Create(boundOriginalCall, boundExpandedBody);
|
||||||
|
|
||||||
Result := TDataValue.FromIntf<IMacroExpansionNode>(newMacroNode);
|
Result := TDataValue.FromIntf<IMacroExpansionNode>(newMacroNode);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -543,11 +526,9 @@ var
|
|||||||
begin
|
begin
|
||||||
if Name.IsEmpty then
|
if Name.IsEmpty then
|
||||||
exit(False);
|
exit(False);
|
||||||
|
|
||||||
c := Name[1];
|
c := Name[1];
|
||||||
if not (c.IsLetter or (c = '_')) then
|
if not (c.IsLetter or (c = '_')) then
|
||||||
exit(False);
|
exit(False);
|
||||||
|
|
||||||
for c in Name do
|
for c in Name do
|
||||||
begin
|
begin
|
||||||
if not (c.IsLetterOrDigit or (c = '_') or (c = '-')) then
|
if not (c.IsLetterOrDigit or (c = '_') or (c = '-')) then
|
||||||
@@ -583,7 +564,6 @@ begin
|
|||||||
begin
|
begin
|
||||||
FNextIsTail := isContextTail and (i = High(Node.Expressions));
|
FNextIsTail := isContextTail and (i = High(Node.Expressions));
|
||||||
transformedValue := Accept(Node.Expressions[i]);
|
transformedValue := Accept(Node.Expressions[i]);
|
||||||
// If a sub-expression (like a macro definition) returns a void value, skip it.
|
|
||||||
if not transformedValue.IsVoid then
|
if not transformedValue.IsVoid then
|
||||||
exprList.Add(transformedValue.AsIntf<IAstNode>);
|
exprList.Add(transformedValue.AsIntf<IAstNode>);
|
||||||
end;
|
end;
|
||||||
@@ -592,7 +572,6 @@ begin
|
|||||||
exprList.Free;
|
exprList.Free;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Avoid creating a new node if nothing changed.
|
|
||||||
if (Length(exprs) = Length(Node.Expressions)) then
|
if (Length(exprs) = Length(Node.Expressions)) then
|
||||||
begin
|
begin
|
||||||
var same := True;
|
var same := True;
|
||||||
@@ -618,10 +597,8 @@ var
|
|||||||
condition, thenBranch, elseBranch: IAstNode;
|
condition, thenBranch, elseBranch: IAstNode;
|
||||||
begin
|
begin
|
||||||
isContextTail := FIsTailStack.Peek;
|
isContextTail := FIsTailStack.Peek;
|
||||||
|
|
||||||
FNextIsTail := False;
|
FNextIsTail := False;
|
||||||
condition := Accept(Node.Condition).AsIntf<IAstNode>;
|
condition := Accept(Node.Condition).AsIntf<IAstNode>;
|
||||||
|
|
||||||
FNextIsTail := isContextTail;
|
FNextIsTail := isContextTail;
|
||||||
thenBranch := Accept(Node.ThenBranch).AsIntf<IAstNode>;
|
thenBranch := Accept(Node.ThenBranch).AsIntf<IAstNode>;
|
||||||
elseBranch := Accept(Node.ElseBranch).AsIntf<IAstNode>;
|
elseBranch := Accept(Node.ElseBranch).AsIntf<IAstNode>;
|
||||||
@@ -648,7 +625,6 @@ begin
|
|||||||
EnterScope;
|
EnterScope;
|
||||||
try
|
try
|
||||||
FCurrentDescriptor.Define('<self>');
|
FCurrentDescriptor.Define('<self>');
|
||||||
|
|
||||||
SetLength(boundParams, Length(Node.Parameters));
|
SetLength(boundParams, Length(Node.Parameters));
|
||||||
for i := 0 to High(Node.Parameters) do
|
for i := 0 to High(Node.Parameters) do
|
||||||
begin
|
begin
|
||||||
@@ -659,10 +635,8 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
lastNestedLambdaCount := FNestedLambdaCount;
|
lastNestedLambdaCount := FNestedLambdaCount;
|
||||||
|
|
||||||
FNextIsTail := True;
|
FNextIsTail := True;
|
||||||
boundBody := Accept(Node.Body).AsIntf<IAstNode>;
|
boundBody := Accept(Node.Body).AsIntf<IAstNode>;
|
||||||
|
|
||||||
hasNestedLambdas := FNestedLambdaCount > lastNestedLambdaCount;
|
hasNestedLambdas := FNestedLambdaCount > lastNestedLambdaCount;
|
||||||
lambdaScope := FCurrentDescriptor;
|
lambdaScope := FCurrentDescriptor;
|
||||||
finally
|
finally
|
||||||
@@ -677,11 +651,9 @@ begin
|
|||||||
function(const Left, Right: TPair<TResolvedAddress, Integer>): Integer begin Result := Left.Value - Right.Value; end
|
function(const Left, Right: TPair<TResolvedAddress, Integer>): Integer begin Result := Left.Value - Right.Value; end
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
SetLength(upvalues, Length(sortedPairs));
|
SetLength(upvalues, Length(sortedPairs));
|
||||||
for i := 0 to High(sortedPairs) do
|
for i := 0 to High(sortedPairs) do
|
||||||
upvalues[i] := sortedPairs[i].Key;
|
upvalues[i] := sortedPairs[i].Key;
|
||||||
|
|
||||||
finally
|
finally
|
||||||
FUpvalueStack.Pop;
|
FUpvalueStack.Pop;
|
||||||
end;
|
end;
|
||||||
@@ -695,7 +667,6 @@ function TAstBinder.VisitRecurNode(const Node: IRecurNode): TDataValue;
|
|||||||
begin
|
begin
|
||||||
if not FIsTailStack.Peek then
|
if not FIsTailStack.Peek then
|
||||||
raise Exception.Create('''recur'' can only be used in a tail position.');
|
raise Exception.Create('''recur'' can only be used in a tail position.');
|
||||||
|
|
||||||
FNextIsTail := False;
|
FNextIsTail := False;
|
||||||
Result := inherited VisitRecurNode(Node);
|
Result := inherited VisitRecurNode(Node);
|
||||||
end;
|
end;
|
||||||
@@ -706,10 +677,8 @@ var
|
|||||||
condition, thenBranch, elseBranch: IAstNode;
|
condition, thenBranch, elseBranch: IAstNode;
|
||||||
begin
|
begin
|
||||||
isContextTail := FIsTailStack.Peek;
|
isContextTail := FIsTailStack.Peek;
|
||||||
|
|
||||||
FNextIsTail := False;
|
FNextIsTail := False;
|
||||||
condition := Accept(Node.Condition).AsIntf<IAstNode>;
|
condition := Accept(Node.Condition).AsIntf<IAstNode>;
|
||||||
|
|
||||||
FNextIsTail := isContextTail;
|
FNextIsTail := isContextTail;
|
||||||
thenBranch := Accept(Node.ThenBranch).AsIntf<IAstNode>;
|
thenBranch := Accept(Node.ThenBranch).AsIntf<IAstNode>;
|
||||||
elseBranch := Accept(Node.ElseBranch).AsIntf<IAstNode>;
|
elseBranch := Accept(Node.ElseBranch).AsIntf<IAstNode>;
|
||||||
@@ -737,17 +706,13 @@ begin
|
|||||||
if (adr.ScopeDepth > 0) and (FUpvalueStack.Count > 0) then
|
if (adr.ScopeDepth > 0) and (FUpvalueStack.Count > 0) then
|
||||||
begin
|
begin
|
||||||
var upvalue := FUpvalueStack.Peek;
|
var upvalue := FUpvalueStack.Peek;
|
||||||
|
|
||||||
// up to outer scope
|
|
||||||
dec(adr.ScopeDepth);
|
dec(adr.ScopeDepth);
|
||||||
|
|
||||||
var upvalueIndex: Integer;
|
var upvalueIndex: Integer;
|
||||||
if not upvalue.Map.TryGetValue(adr, upvalueIndex) then
|
if not upvalue.Map.TryGetValue(adr, upvalueIndex) then
|
||||||
begin
|
begin
|
||||||
upvalueIndex := upvalue.Map.Count;
|
upvalueIndex := upvalue.Map.Count;
|
||||||
upvalue.Map.Add(adr, upvalueIndex);
|
upvalue.Map.Add(adr, upvalueIndex);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
boundNode := TBoundIdentifierNode.Create(Node, TResolvedAddress.Create(akUpvalue, 0, upvalueIndex));
|
boundNode := TBoundIdentifierNode.Create(Node, TResolvedAddress.Create(akUpvalue, 0, upvalueIndex));
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@@ -777,13 +742,8 @@ begin
|
|||||||
|
|
||||||
slotIndex := FCurrentDescriptor.Define(Node.Identifier.Name);
|
slotIndex := FCurrentDescriptor.Define(Node.Identifier.Name);
|
||||||
address := TResolvedAddress.Create(akLocalOrParent, 0, slotIndex);
|
address := TResolvedAddress.Create(akLocalOrParent, 0, slotIndex);
|
||||||
|
|
||||||
boundIdentifier := TBoundIdentifierNode.Create(Node.Identifier, address);
|
boundIdentifier := TBoundIdentifierNode.Create(Node.Identifier, address);
|
||||||
|
|
||||||
// Check if the analysis pass marked this declaration as being captured by a closure.
|
|
||||||
isBoxed := (FBoxedDeclarations <> nil) and FBoxedDeclarations.Contains(Node);
|
isBoxed := (FBoxedDeclarations <> nil) and FBoxedDeclarations.Contains(Node);
|
||||||
|
|
||||||
// Always create a bound declaration node, passing the IsBoxed flag.
|
|
||||||
boundDecl := TBoundVariableDeclarationNode.Create(boundIdentifier, initializer, isBoxed);
|
boundDecl := TBoundVariableDeclarationNode.Create(boundIdentifier, initializer, isBoxed);
|
||||||
Result := TDataValue.FromIntf<IVariableDeclarationNode>(boundDecl);
|
Result := TDataValue.FromIntf<IVariableDeclarationNode>(boundDecl);
|
||||||
end;
|
end;
|
||||||
|
|||||||
@@ -10,12 +10,41 @@ uses
|
|||||||
|
|
||||||
type
|
type
|
||||||
// Contains the "pure" native implementations.
|
// Contains the "pure" native implementations.
|
||||||
// These functions work with Delphi-native types (like TScalar) instead of TDataValue,
|
|
||||||
// making them type-safe and easier to test.
|
|
||||||
// The registration mechanism below will automatically create the required high-performance
|
|
||||||
// wrappers to make them available to the script interpreter.
|
|
||||||
TRtlFunctions = record
|
TRtlFunctions = record
|
||||||
public
|
public
|
||||||
|
[TRtlFunction('+')]
|
||||||
|
class function Add(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
|
[TRtlFunction('-')]
|
||||||
|
class function Subtract(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
|
[TRtlFunction('*')]
|
||||||
|
class function Multiply(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
|
[TRtlFunction('/')]
|
||||||
|
class function Divide(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
|
[TRtlFunction('=')]
|
||||||
|
class function Equal(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
|
[TRtlFunction('<>')]
|
||||||
|
class function NotEqual(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
|
[TRtlFunction('<')]
|
||||||
|
class function LessThan(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
|
[TRtlFunction('<=')]
|
||||||
|
class function LessThanOrEqual(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
|
[TRtlFunction('>')]
|
||||||
|
class function GreaterThan(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
|
[TRtlFunction('>=')]
|
||||||
|
class function GreaterThanOrEqual(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
|
[TRtlFunction('not')]
|
||||||
|
class function LogicalNot(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
[TRtlFunction('Abs')]
|
[TRtlFunction('Abs')]
|
||||||
class function Abs(const Arg: TScalar): TScalar; static;
|
class function Abs(const Arg: TScalar): TScalar; static;
|
||||||
|
|
||||||
@@ -31,8 +60,6 @@ type
|
|||||||
[TRtlFunction('Sign')]
|
[TRtlFunction('Sign')]
|
||||||
class function Sign(const Arg: TScalar): TScalar; static;
|
class function Sign(const Arg: TScalar): TScalar; static;
|
||||||
|
|
||||||
// NOTE: Higher-order and complex functions can keep the TDataValue signature for now.
|
|
||||||
// The registry is smart enough to handle both native types and the TDataValue signature directly.
|
|
||||||
[TRtlFunction('Memoize')]
|
[TRtlFunction('Memoize')]
|
||||||
class function Memoize(const Args: TArray<TDataValue>): TDataValue; static;
|
class function Memoize(const Args: TArray<TDataValue>): TDataValue; static;
|
||||||
|
|
||||||
@@ -57,6 +84,139 @@ uses
|
|||||||
System.Math,
|
System.Math,
|
||||||
Myc.Data.Decimal;
|
Myc.Data.Decimal;
|
||||||
|
|
||||||
|
{ TRtlFunctions - Operator Implementations }
|
||||||
|
|
||||||
|
class function TRtlFunctions.Add(const Args: TArray<TDataValue>): TDataValue;
|
||||||
|
var
|
||||||
|
res: TScalar;
|
||||||
|
begin
|
||||||
|
if Length(Args) <> 2 then
|
||||||
|
raise EArgumentException.Create('Operator + requires 2 arguments.');
|
||||||
|
if not TScalar.TryBinaryOperation(TScalar.TBinaryOp.Add, Args[0], Args[1], res) then
|
||||||
|
raise EArgumentException.Create('Invalid arguments for operator +.');
|
||||||
|
Result := res;
|
||||||
|
end;
|
||||||
|
|
||||||
|
class function TRtlFunctions.Subtract(const Args: TArray<TDataValue>): TDataValue;
|
||||||
|
var
|
||||||
|
res: TScalar;
|
||||||
|
begin
|
||||||
|
if Length(Args) = 1 then // Unary negation
|
||||||
|
begin
|
||||||
|
if not TScalar.TryUnaryOperation(TScalar.TUnaryOp.Negate, Args[0], res) then
|
||||||
|
raise EArgumentException.Create('Invalid argument for unary operator -.');
|
||||||
|
end
|
||||||
|
else if Length(Args) = 2 then // Binary subtraction
|
||||||
|
begin
|
||||||
|
if not TScalar.TryBinaryOperation(TScalar.TBinaryOp.Subtract, Args[0], Args[1], res) then
|
||||||
|
raise EArgumentException.Create('Invalid arguments for binary operator -.');
|
||||||
|
end
|
||||||
|
else
|
||||||
|
raise EArgumentException.Create('Operator - requires 1 or 2 arguments.');
|
||||||
|
Result := res;
|
||||||
|
end;
|
||||||
|
|
||||||
|
class function TRtlFunctions.Multiply(const Args: TArray<TDataValue>): TDataValue;
|
||||||
|
var
|
||||||
|
res: TScalar;
|
||||||
|
begin
|
||||||
|
if Length(Args) <> 2 then
|
||||||
|
raise EArgumentException.Create('Operator * requires 2 arguments.');
|
||||||
|
if not TScalar.TryBinaryOperation(TScalar.TBinaryOp.Multiply, Args[0], Args[1], res) then
|
||||||
|
raise EArgumentException.Create('Invalid arguments for operator *.');
|
||||||
|
Result := res;
|
||||||
|
end;
|
||||||
|
|
||||||
|
class function TRtlFunctions.Divide(const Args: TArray<TDataValue>): TDataValue;
|
||||||
|
var
|
||||||
|
res: TScalar;
|
||||||
|
begin
|
||||||
|
if Length(Args) <> 2 then
|
||||||
|
raise EArgumentException.Create('Operator / requires 2 arguments.');
|
||||||
|
if not TScalar.TryBinaryOperation(TScalar.TBinaryOp.Divide, Args[0], Args[1], res) then
|
||||||
|
raise EArgumentException.Create('Invalid arguments for operator /.');
|
||||||
|
Result := res;
|
||||||
|
end;
|
||||||
|
|
||||||
|
class function TRtlFunctions.Equal(const Args: TArray<TDataValue>): TDataValue;
|
||||||
|
var
|
||||||
|
res: TScalar;
|
||||||
|
begin
|
||||||
|
if Length(Args) <> 2 then
|
||||||
|
raise EArgumentException.Create('Operator = requires 2 arguments.');
|
||||||
|
if not TScalar.TryBinaryOperation(TScalar.TBinaryOp.Equal, Args[0], Args[1], res) then
|
||||||
|
raise EArgumentException.Create('Invalid arguments for operator =.');
|
||||||
|
Result := res;
|
||||||
|
end;
|
||||||
|
|
||||||
|
class function TRtlFunctions.NotEqual(const Args: TArray<TDataValue>): TDataValue;
|
||||||
|
var
|
||||||
|
res: TScalar;
|
||||||
|
begin
|
||||||
|
if Length(Args) <> 2 then
|
||||||
|
raise EArgumentException.Create('Operator <> requires 2 arguments.');
|
||||||
|
if not TScalar.TryBinaryOperation(TScalar.TBinaryOp.NotEqual, Args[0], Args[1], res) then
|
||||||
|
raise EArgumentException.Create('Invalid arguments for operator <>.');
|
||||||
|
Result := res;
|
||||||
|
end;
|
||||||
|
|
||||||
|
class function TRtlFunctions.LessThan(const Args: TArray<TDataValue>): TDataValue;
|
||||||
|
var
|
||||||
|
res: TScalar;
|
||||||
|
begin
|
||||||
|
if Length(Args) <> 2 then
|
||||||
|
raise EArgumentException.Create('Operator < requires 2 arguments.');
|
||||||
|
if not TScalar.TryBinaryOperation(TScalar.TBinaryOp.Less, Args[0], Args[1], res) then
|
||||||
|
raise EArgumentException.Create('Invalid arguments for operator <.');
|
||||||
|
Result := res;
|
||||||
|
end;
|
||||||
|
|
||||||
|
class function TRtlFunctions.LessThanOrEqual(const Args: TArray<TDataValue>): TDataValue;
|
||||||
|
var
|
||||||
|
res: TScalar;
|
||||||
|
begin
|
||||||
|
if Length(Args) <> 2 then
|
||||||
|
raise EArgumentException.Create('Operator <= requires 2 arguments.');
|
||||||
|
if not TScalar.TryBinaryOperation(TScalar.TBinaryOp.LessOrEqual, Args[0], Args[1], res) then
|
||||||
|
raise EArgumentException.Create('Invalid arguments for operator <=.');
|
||||||
|
Result := res;
|
||||||
|
end;
|
||||||
|
|
||||||
|
class function TRtlFunctions.GreaterThan(const Args: TArray<TDataValue>): TDataValue;
|
||||||
|
var
|
||||||
|
res: TScalar;
|
||||||
|
begin
|
||||||
|
if Length(Args) <> 2 then
|
||||||
|
raise EArgumentException.Create('Operator > requires 2 arguments.');
|
||||||
|
if not TScalar.TryBinaryOperation(TScalar.TBinaryOp.Greater, Args[0], Args[1], res) then
|
||||||
|
raise EArgumentException.Create('Invalid arguments for operator >.');
|
||||||
|
Result := res;
|
||||||
|
end;
|
||||||
|
|
||||||
|
class function TRtlFunctions.GreaterThanOrEqual(const Args: TArray<TDataValue>): TDataValue;
|
||||||
|
var
|
||||||
|
res: TScalar;
|
||||||
|
begin
|
||||||
|
if Length(Args) <> 2 then
|
||||||
|
raise EArgumentException.Create('Operator >= requires 2 arguments.');
|
||||||
|
if not TScalar.TryBinaryOperation(TScalar.TBinaryOp.GreaterOrEqual, Args[0], Args[1], res) then
|
||||||
|
raise EArgumentException.Create('Invalid arguments for operator >=.');
|
||||||
|
Result := res;
|
||||||
|
end;
|
||||||
|
|
||||||
|
class function TRtlFunctions.LogicalNot(const Args: TArray<TDataValue>): TDataValue;
|
||||||
|
var
|
||||||
|
res: TScalar;
|
||||||
|
begin
|
||||||
|
if Length(Args) <> 1 then
|
||||||
|
raise EArgumentException.Create('Operator not requires 1 argument.');
|
||||||
|
if not TScalar.TryUnaryOperation(TScalar.TUnaryOp.Not, Args[0], res) then
|
||||||
|
raise EArgumentException.Create('Invalid argument for operator not.');
|
||||||
|
Result := res;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TRtlFunctions - Standard Functions }
|
||||||
|
|
||||||
class function TRtlFunctions.Abs(const Arg: TScalar): TScalar;
|
class function TRtlFunctions.Abs(const Arg: TScalar): TScalar;
|
||||||
begin
|
begin
|
||||||
case Arg.Kind of
|
case Arg.Kind of
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ type
|
|||||||
property Parent: IExecutionScope read GetParent;
|
property Parent: IExecutionScope read GetParent;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// Describes the layout of a scope: variable names and their slot indices.
|
// Describes the layout of a scope: variable names, their slot indices and macros.
|
||||||
// This is generated by the binder and used to create TExecutionScope instances at runtime.
|
// This is generated by the binder and used to create TExecutionScope instances at runtime.
|
||||||
IScopeDescriptor = interface
|
IScopeDescriptor = interface
|
||||||
{$region 'private'}
|
{$region 'private'}
|
||||||
@@ -55,7 +55,9 @@ type
|
|||||||
{$endregion}
|
{$endregion}
|
||||||
function CreateScope(const AParent: IExecutionScope): IExecutionScope;
|
function CreateScope(const AParent: IExecutionScope): IExecutionScope;
|
||||||
function Define(const Name: string): Integer;
|
function Define(const Name: string): Integer;
|
||||||
|
procedure DefineMacro(const Name: string; const Node: IMacroDefinitionNode);
|
||||||
function FindSymbol(const Name: string): TResolvedAddress;
|
function FindSymbol(const Name: string): TResolvedAddress;
|
||||||
|
function FindMacro(const Name: string): IMacroDefinitionNode;
|
||||||
property Parent: IScopeDescriptor read GetParent;
|
property Parent: IScopeDescriptor read GetParent;
|
||||||
property SlotCount: Integer read GetSlotCount;
|
property SlotCount: Integer read GetSlotCount;
|
||||||
property Symbols: TDictionary<string, Integer> read GetSymbols;
|
property Symbols: TDictionary<string, Integer> read GetSymbols;
|
||||||
@@ -77,7 +79,8 @@ implementation
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
System.Generics.Defaults,
|
System.Generics.Defaults,
|
||||||
System.SyncObjs;
|
System.SyncObjs,
|
||||||
|
Myc.Ast;
|
||||||
|
|
||||||
type
|
type
|
||||||
TExecutionScope = class(TInterfacedObject, IExecutionScope)
|
TExecutionScope = class(TInterfacedObject, IExecutionScope)
|
||||||
@@ -132,6 +135,7 @@ type
|
|||||||
private
|
private
|
||||||
FParent: IScopeDescriptor;
|
FParent: IScopeDescriptor;
|
||||||
FSymbols: TDictionary<string, Integer>;
|
FSymbols: TDictionary<string, Integer>;
|
||||||
|
FMacros: TDictionary<string, IMacroDefinitionNode>;
|
||||||
function GetParent: IScopeDescriptor;
|
function GetParent: IScopeDescriptor;
|
||||||
function GetSlotCount: Integer;
|
function GetSlotCount: Integer;
|
||||||
function GetSymbols: TDictionary<string, Integer>;
|
function GetSymbols: TDictionary<string, Integer>;
|
||||||
@@ -140,7 +144,9 @@ type
|
|||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
class function CreateDescriptor(const Scope: IExecutionScope): IScopeDescriptor; static;
|
class function CreateDescriptor(const Scope: IExecutionScope): IScopeDescriptor; static;
|
||||||
function Define(const Name: string): Integer;
|
function Define(const Name: string): Integer;
|
||||||
|
procedure DefineMacro(const Name: string; const Node: IMacroDefinitionNode);
|
||||||
function FindSymbol(const Name: string): TResolvedAddress;
|
function FindSymbol(const Name: string): TResolvedAddress;
|
||||||
|
function FindMacro(const Name: string): IMacroDefinitionNode;
|
||||||
function CreateScope(const Parent: IExecutionScope): IExecutionScope;
|
function CreateScope(const Parent: IExecutionScope): IExecutionScope;
|
||||||
procedure PopulateFromScope(Scope: TExecutionScope);
|
procedure PopulateFromScope(Scope: TExecutionScope);
|
||||||
property Symbols: TDictionary<string, Integer> read FSymbols;
|
property Symbols: TDictionary<string, Integer> read FSymbols;
|
||||||
@@ -447,16 +453,18 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
{ TScopeDescriptor }
|
{ TScopeDescriptor }
|
||||||
// ... (Implementation unchanged and correct) ...
|
|
||||||
constructor TScopeDescriptor.Create(const AParent: IScopeDescriptor);
|
constructor TScopeDescriptor.Create(const AParent: IScopeDescriptor);
|
||||||
begin
|
begin
|
||||||
inherited Create;
|
inherited Create;
|
||||||
FParent := AParent;
|
FParent := AParent;
|
||||||
FSymbols := TDictionary<string, Integer>.Create;
|
FSymbols := TDictionary<string, Integer>.Create;
|
||||||
|
FMacros := TDictionary<string, IMacroDefinitionNode>.Create;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
destructor TScopeDescriptor.Destroy;
|
destructor TScopeDescriptor.Destroy;
|
||||||
begin
|
begin
|
||||||
|
FMacros.Free;
|
||||||
FSymbols.Free;
|
FSymbols.Free;
|
||||||
inherited;
|
inherited;
|
||||||
end;
|
end;
|
||||||
@@ -484,22 +492,42 @@ begin
|
|||||||
FSymbols.Add(Name, Result);
|
FSymbols.Add(Name, Result);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TScopeDescriptor.FindSymbol(const Name: string): TResolvedAddress;
|
procedure TScopeDescriptor.DefineMacro(const Name: string; const Node: IMacroDefinitionNode);
|
||||||
|
begin
|
||||||
|
FMacros.AddOrSetValue(Name, Node);
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TScopeDescriptor.FindMacro(const Name: string): IMacroDefinitionNode;
|
||||||
var
|
var
|
||||||
currentDescriptor: TScopeDescriptor;
|
currentDescriptor: TScopeDescriptor;
|
||||||
|
begin
|
||||||
|
// Find a macro by searching up the parent scope chain.
|
||||||
|
Result := nil;
|
||||||
|
currentDescriptor := Self;
|
||||||
|
while Assigned(currentDescriptor) do
|
||||||
|
begin
|
||||||
|
if currentDescriptor.FMacros.TryGetValue(Name, Result) then
|
||||||
|
exit;
|
||||||
|
currentDescriptor := currentDescriptor.FParent as TScopeDescriptor;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TScopeDescriptor.FindSymbol(const Name: string): TResolvedAddress;
|
||||||
|
var
|
||||||
|
currentDescriptor: IScopeDescriptor;
|
||||||
begin
|
begin
|
||||||
Result.Kind := akUnresolved;
|
Result.Kind := akUnresolved;
|
||||||
Result.ScopeDepth := 0;
|
Result.ScopeDepth := 0;
|
||||||
currentDescriptor := Self;
|
currentDescriptor := Self;
|
||||||
while currentDescriptor <> nil do
|
while Assigned(currentDescriptor) do
|
||||||
begin
|
begin
|
||||||
if currentDescriptor.FSymbols.TryGetValue(Name, Result.SlotIndex) then
|
if currentDescriptor.Symbols.TryGetValue(Name, Result.SlotIndex) then
|
||||||
begin
|
begin
|
||||||
Result.Kind := akLocalOrParent;
|
Result.Kind := akLocalOrParent;
|
||||||
exit;
|
exit;
|
||||||
end;
|
end;
|
||||||
inc(Result.ScopeDepth);
|
inc(Result.ScopeDepth);
|
||||||
currentDescriptor := currentDescriptor.FParent as TScopeDescriptor;
|
currentDescriptor := currentDescriptor.Parent;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -519,15 +547,31 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TScopeDescriptor.PopulateFromScope(Scope: TExecutionScope);
|
procedure TScopeDescriptor.PopulateFromScope(Scope: TExecutionScope);
|
||||||
|
var
|
||||||
|
val: TDataValue;
|
||||||
begin
|
begin
|
||||||
|
// Recreate descriptor by iterating all defined symbols in the runtime scope.
|
||||||
for var pair in Scope.NameToIndex do
|
for var pair in Scope.NameToIndex do
|
||||||
begin
|
begin
|
||||||
var name := Scope.NameStrings[pair.Key];
|
var name := Scope.NameStrings[pair.Key];
|
||||||
|
var slotIndex := pair.Value;
|
||||||
|
|
||||||
|
// Add to variable symbol table.
|
||||||
if not FSymbols.ContainsKey(name) then
|
if not FSymbols.ContainsKey(name) then
|
||||||
FSymbols.Add(name, pair.Value);
|
FSymbols.Add(name, slotIndex);
|
||||||
|
|
||||||
|
// Check if the symbol is also a macro and add it to the macro table.
|
||||||
|
val := Scope.FValues[slotIndex].Value;
|
||||||
|
if (val.Kind = vkInterface) and (val.AsIntf<IAstNode> is TMacroDefinitionNode) then
|
||||||
|
begin
|
||||||
|
if not FMacros.ContainsKey(name) then
|
||||||
|
FMacros.Add(name, val.AsIntf<IMacroDefinitionNode>);
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
{ TScope }
|
||||||
|
|
||||||
class function TScope.CreateDescriptor(const Parent: IScopeDescriptor): IScopeDescriptor;
|
class function TScope.CreateDescriptor(const Parent: IScopeDescriptor): IScopeDescriptor;
|
||||||
begin
|
begin
|
||||||
Result := TScopeDescriptor.Create(Parent);
|
Result := TScopeDescriptor.Create(Parent);
|
||||||
|
|||||||
+10
-32
@@ -38,6 +38,7 @@ type
|
|||||||
tkQuote, // '
|
tkQuote, // '
|
||||||
tkBacktick, // `
|
tkBacktick, // `
|
||||||
tkTilde, // ~
|
tkTilde, // ~
|
||||||
|
tkAt, // @
|
||||||
tkIdentifier,
|
tkIdentifier,
|
||||||
tkNumber,
|
tkNumber,
|
||||||
tkString,
|
tkString,
|
||||||
@@ -251,6 +252,11 @@ begin
|
|||||||
Result.Kind := tkTilde;
|
Result.Kind := tkTilde;
|
||||||
Advance;
|
Advance;
|
||||||
end;
|
end;
|
||||||
|
'@':
|
||||||
|
begin
|
||||||
|
Result.Kind := tkAt;
|
||||||
|
Advance;
|
||||||
|
end;
|
||||||
'"':
|
'"':
|
||||||
begin
|
begin
|
||||||
Result.Kind := tkString;
|
Result.Kind := tkString;
|
||||||
@@ -351,7 +357,6 @@ begin
|
|||||||
|
|
||||||
head := elements[0];
|
head := elements[0];
|
||||||
|
|
||||||
// Convert the rest of the list to an array for the factory functions
|
|
||||||
SetLength(tailTokens, elements.Count - 1);
|
SetLength(tailTokens, elements.Count - 1);
|
||||||
SetLength(tailNodes, elements.Count - 1);
|
SetLength(tailNodes, elements.Count - 1);
|
||||||
for var i := 0 to High(tailNodes) do
|
for var i := 0 to High(tailNodes) do
|
||||||
@@ -391,7 +396,6 @@ begin
|
|||||||
end
|
end
|
||||||
else if SameText(head.Token.Text, 'defmacro') then
|
else if SameText(head.Token.Text, 'defmacro') then
|
||||||
begin
|
begin
|
||||||
// (defmacro name [params] body)
|
|
||||||
if (Length(tailNodes) <> 3) or (tailTokens[0].Kind <> tkIdentifier) then
|
if (Length(tailNodes) <> 3) or (tailTokens[0].Kind <> tkIdentifier) then
|
||||||
raise Exception.Create('Syntax Error: ''defmacro'' requires a name, a parameter list, and a body.');
|
raise Exception.Create('Syntax Error: ''defmacro'' requires a name, a parameter list, and a body.');
|
||||||
if elements[2].Params = nil then
|
if elements[2].Params = nil then
|
||||||
@@ -424,34 +428,10 @@ begin
|
|||||||
else if SameText(head.Token.Text, 'get') then
|
else if SameText(head.Token.Text, 'get') then
|
||||||
Result := TAst.Indexer(tailNodes[0], tailNodes[1])
|
Result := TAst.Indexer(tailNodes[0], tailNodes[1])
|
||||||
else if (Length(head.Token.Text) > 1) and (head.Token.Text.StartsWith('.')) then
|
else if (Length(head.Token.Text) > 1) and (head.Token.Text.StartsWith('.')) then
|
||||||
Result := TAst.MemberAccess(tailNodes[0], TAst.Identifier(head.Token.Text.Substring(1)))
|
Result := TAst.MemberAccess(tailNodes[0], TAst.Identifier(head.Token.Text.Substring(1)));
|
||||||
else
|
|
||||||
begin
|
|
||||||
if Length(tailNodes) = 1 then
|
|
||||||
begin
|
|
||||||
for var op := Low(TScalar.TUnaryOp) to High(TScalar.TUnaryOp) do
|
|
||||||
begin
|
|
||||||
if head.Token.Text = op.ToString then
|
|
||||||
begin
|
|
||||||
Result := TAst.UnaryExpr(op, tailNodes[0]);
|
|
||||||
break
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
end
|
|
||||||
else if Length(tailNodes) = 2 then
|
|
||||||
begin
|
|
||||||
for var op := Low(TScalar.TBinaryOp) to High(TScalar.TBinaryOp) do
|
|
||||||
begin
|
|
||||||
if head.Token.Text = op.ToString then
|
|
||||||
begin
|
|
||||||
Result := TAst.BinaryExpr(tailNodes[0], op, tailNodes[1]);
|
|
||||||
break
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
// If no special form matched, treat it as a generic function call.
|
||||||
if Result = nil then
|
if Result = nil then
|
||||||
Result := TAst.FunctionCall(head.Node, tailNodes);
|
Result := TAst.FunctionCall(head.Node, tailNodes);
|
||||||
|
|
||||||
@@ -480,14 +460,13 @@ begin
|
|||||||
tkTilde:
|
tkTilde:
|
||||||
begin
|
begin
|
||||||
NextToken;
|
NextToken;
|
||||||
// Check for unquote-splicing ('~@')
|
if FCurrentToken.Kind = tkAt then
|
||||||
if (FCurrentToken.Kind = tkIdentifier) and (FCurrentToken.Text = '@') then
|
|
||||||
begin
|
begin
|
||||||
NextToken;
|
NextToken;
|
||||||
expr := ParseExpression;
|
expr := ParseExpression;
|
||||||
Result.Node := TAst.UnquoteSplicing(expr.Node);
|
Result.Node := TAst.UnquoteSplicing(expr.Node);
|
||||||
end
|
end
|
||||||
else // It's a regular unquote ('~')
|
else
|
||||||
begin
|
begin
|
||||||
expr := ParseExpression;
|
expr := ParseExpression;
|
||||||
Result.Node := TAst.Unquote(expr.Node);
|
Result.Node := TAst.Unquote(expr.Node);
|
||||||
@@ -498,7 +477,6 @@ begin
|
|||||||
begin
|
begin
|
||||||
NextToken;
|
NextToken;
|
||||||
expr := ParseExpression;
|
expr := ParseExpression;
|
||||||
// A regular quote '(...) can be desugared into (quote (...))
|
|
||||||
Result.Node := TAst.FunctionCall(TAst.Identifier('quote'), [expr.Node]);
|
Result.Node := TAst.FunctionCall(TAst.Identifier('quote'), [expr.Node]);
|
||||||
exit;
|
exit;
|
||||||
end;
|
end;
|
||||||
|
|||||||
@@ -194,6 +194,7 @@ type
|
|||||||
public
|
public
|
||||||
constructor Create(const AExpression: IAstNode);
|
constructor Create(const AExpression: IAstNode);
|
||||||
function Accept(const Visitor: IAstVisitor): TDataValue; override;
|
function Accept(const Visitor: IAstVisitor): TDataValue; override;
|
||||||
|
property Expression: IAstNode read GetExpression;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
TFunctionCallNode = class(TAstNode, IFunctionCallNode)
|
TFunctionCallNode = class(TAstNode, IFunctionCallNode)
|
||||||
|
|||||||
Reference in New Issue
Block a user