516 lines
18 KiB
ObjectPascal
516 lines
18 KiB
ObjectPascal
unit Myc.Ast.Binding;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils,
|
|
System.Classes,
|
|
System.Generics.Collections,
|
|
Myc.Data.Value,
|
|
Myc.Ast.Nodes,
|
|
Myc.Ast.Visitor,
|
|
Myc.Ast.Scope,
|
|
Myc.Ast.Analyzer,
|
|
Myc.Ast;
|
|
|
|
type
|
|
// The binder is a transformer that enriches the AST with semantic information
|
|
// like resolved addresses, scopes, and tail-call annotations.
|
|
IAstBinder = interface(IAstVisitor)
|
|
function Execute(const RootNode: IAstNode; out Descriptor: IScopeDescriptor): IAstNode;
|
|
end;
|
|
|
|
TAstBinder = class(TAstTransformer, IAstBinder)
|
|
private
|
|
type
|
|
// Helper class to track upvalues for a lambda expression.
|
|
TUpvalueMapping = class
|
|
public
|
|
Map: TDictionary<TResolvedAddress, Integer>;
|
|
Nodes: TList<IIdentifierNode>;
|
|
constructor Create;
|
|
destructor Destroy; override;
|
|
end;
|
|
private
|
|
FCurrentDescriptor: IScopeDescriptor;
|
|
FUpvalueStack: TStack<TUpvalueMapping>;
|
|
FNestedLambdaCount: Integer;
|
|
FIsTailStack: TStack<Boolean>;
|
|
FNextIsTail: Boolean;
|
|
// Contains all variable declarations that are captured by a closure.
|
|
// This is populated by a pre-pass before the transformation begins.
|
|
FBoxedDeclarations: THashSet<IVariableDeclarationNode>;
|
|
|
|
procedure EnterScope;
|
|
procedure ExitScope;
|
|
function IsValidIdentifier(const Name: string): Boolean;
|
|
|
|
protected
|
|
// Stack management for tail-call state is centralized here.
|
|
function Accept(const Node: IAstNode): TDataValue; override;
|
|
|
|
// The binder overrides specific visit methods to enrich the AST.
|
|
function VisitIdentifier(const Node: IIdentifierNode): TDataValue; override;
|
|
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): TDataValue; override;
|
|
function VisitAssignment(const Node: IAssignmentNode): TDataValue; override;
|
|
function VisitLambdaExpression(const Node: ILambdaExpressionNode): TDataValue; override;
|
|
function VisitMacroDefinition(const Node: IMacroDefinitionNode): TDataValue; override;
|
|
function VisitFunctionCall(const Node: IFunctionCallNode): TDataValue; override;
|
|
function VisitRecurNode(const Node: IRecurNode): TDataValue; override;
|
|
function VisitBlockExpression(const Node: IBlockExpressionNode): TDataValue; override;
|
|
function VisitIfExpression(const Node: IIfExpressionNode): TDataValue; override;
|
|
function VisitTernaryExpression(const Node: ITernaryExpressionNode): TDataValue; override;
|
|
function VisitBinaryExpression(const Node: IBinaryExpressionNode): TDataValue; override;
|
|
function VisitUnaryExpression(const Node: IUnaryExpressionNode): TDataValue; override;
|
|
public
|
|
constructor Create(const AInitialScope: IExecutionScope);
|
|
destructor Destroy; override;
|
|
|
|
function Execute(const RootNode: IAstNode; out Descriptor: IScopeDescriptor): IAstNode;
|
|
end;
|
|
|
|
TBoundIdentifierNode = class(TIdentifierNode)
|
|
private
|
|
FAddress: TResolvedAddress;
|
|
public
|
|
constructor Create(const AUnboundNode: IIdentifierNode; const AAddress: TResolvedAddress);
|
|
property Address: TResolvedAddress read FAddress;
|
|
end;
|
|
|
|
// A bound variable declaration node that includes whether the variable is captured (boxed).
|
|
TBoundVariableDeclarationNode = class(TVariableDeclarationNode)
|
|
private
|
|
FIsBoxed: Boolean;
|
|
public
|
|
constructor Create(const AIdentifier: IIdentifierNode; AInitializer: IAstNode; AIsBoxed: Boolean);
|
|
property IsBoxed: Boolean read FIsBoxed;
|
|
end;
|
|
|
|
TBoundLambdaExpressionNode = class(TLambdaExpressionNode)
|
|
private
|
|
FScopeDescriptor: IScopeDescriptor;
|
|
FUpvalues: TArray<TResolvedAddress>;
|
|
FHasNestedLambdas: Boolean;
|
|
public
|
|
constructor Create(
|
|
const AUnboundNode: ILambdaExpressionNode;
|
|
const ABody: IAstNode;
|
|
const AParameters: TArray<IIdentifierNode>;
|
|
const AScopeDescriptor: IScopeDescriptor;
|
|
const AUpvalues: TArray<TResolvedAddress>;
|
|
AHasNestedLambdas: Boolean
|
|
);
|
|
|
|
property ScopeDescriptor: IScopeDescriptor read FScopeDescriptor;
|
|
property Upvalues: TArray<TResolvedAddress> read FUpvalues;
|
|
property HasNestedLambdas: Boolean read FHasNestedLambdas;
|
|
end;
|
|
|
|
TBoundFunctionCallNode = class(TFunctionCallNode)
|
|
private
|
|
FIsTailCall: Boolean;
|
|
public
|
|
constructor Create(
|
|
const AUnboundNode: IFunctionCallNode;
|
|
const ACallee: IAstNode;
|
|
const AArguments: TArray<IAstNode>;
|
|
AIsTailCall: Boolean
|
|
);
|
|
property IsTailCall: Boolean read FIsTailCall;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.Generics.Defaults,
|
|
System.Character;
|
|
|
|
type
|
|
// A custom equality comparer for TResolvedAddress to ensure correct behavior in TDictionary.
|
|
TResolvedAddressComparer = class(TEqualityComparer<TResolvedAddress>)
|
|
public
|
|
function Equals(const Left, Right: TResolvedAddress): Boolean; override;
|
|
function GetHashCode(const Value: TResolvedAddress): Integer; override;
|
|
end;
|
|
|
|
{ TBoundIdentifierNode }
|
|
constructor TBoundIdentifierNode.Create(const AUnboundNode: IIdentifierNode; const AAddress: TResolvedAddress);
|
|
begin
|
|
inherited Create(AUnboundNode.Name);
|
|
FAddress := AAddress;
|
|
end;
|
|
|
|
{ TBoundVariableDeclarationNode }
|
|
constructor TBoundVariableDeclarationNode.Create(const AIdentifier: IIdentifierNode; AInitializer: IAstNode; AIsBoxed: Boolean);
|
|
begin
|
|
inherited Create(AIdentifier, AInitializer);
|
|
FIsBoxed := AIsBoxed;
|
|
end;
|
|
|
|
{ TBoundLambdaExpressionNode }
|
|
constructor TBoundLambdaExpressionNode.Create(
|
|
const AUnboundNode: ILambdaExpressionNode;
|
|
const ABody: IAstNode;
|
|
const AParameters: TArray<IIdentifierNode>;
|
|
const AScopeDescriptor: IScopeDescriptor;
|
|
const AUpvalues: TArray<TResolvedAddress>;
|
|
AHasNestedLambdas: Boolean
|
|
);
|
|
begin
|
|
inherited Create(AParameters, ABody);
|
|
FScopeDescriptor := AScopeDescriptor;
|
|
FUpvalues := AUpvalues;
|
|
FHasNestedLambdas := AHasNestedLambdas;
|
|
end;
|
|
|
|
{ TBoundFunctionCallNode }
|
|
constructor TBoundFunctionCallNode.Create(
|
|
const AUnboundNode: IFunctionCallNode;
|
|
const ACallee: IAstNode;
|
|
const AArguments: TArray<IAstNode>;
|
|
AIsTailCall: Boolean
|
|
);
|
|
begin
|
|
inherited Create(ACallee, AArguments);
|
|
FIsTailCall := AIsTailCall;
|
|
end;
|
|
|
|
{ TResolvedAddressComparer }
|
|
function TResolvedAddressComparer.Equals(const Left, Right: TResolvedAddress): Boolean;
|
|
begin
|
|
Result := (Left = Right);
|
|
end;
|
|
|
|
function TResolvedAddressComparer.GetHashCode(const Value: TResolvedAddress): Integer;
|
|
begin
|
|
Result := 17;
|
|
Result := Result * 23 + Ord(Value.Kind);
|
|
Result := Result * 23 + Value.ScopeDepth;
|
|
Result := Result * 23 + Value.SlotIndex;
|
|
end;
|
|
|
|
{ TAstBinder.TUpvalueMapping }
|
|
constructor TAstBinder.TUpvalueMapping.Create;
|
|
begin
|
|
inherited Create;
|
|
Map := TDictionary<TResolvedAddress, Integer>.Create(TResolvedAddressComparer.Create);
|
|
Nodes := TList<IIdentifierNode>.Create();
|
|
end;
|
|
|
|
destructor TAstBinder.TUpvalueMapping.Destroy;
|
|
begin
|
|
Nodes.Free;
|
|
Map.Free;
|
|
inherited Destroy;
|
|
end;
|
|
|
|
constructor TAstBinder.Create(const AInitialScope: IExecutionScope);
|
|
begin
|
|
inherited Create;
|
|
FCurrentDescriptor := AInitialScope.CreateDescriptor;
|
|
FUpvalueStack := TObjectStack<TUpvalueMapping>.Create(True);
|
|
FNestedLambdaCount := 0;
|
|
FIsTailStack := TStack<Boolean>.Create;
|
|
FNextIsTail := True;
|
|
FBoxedDeclarations := nil;
|
|
end;
|
|
|
|
destructor TAstBinder.Destroy;
|
|
begin
|
|
FIsTailStack.Free;
|
|
FUpvalueStack.Free;
|
|
FBoxedDeclarations.Free;
|
|
inherited;
|
|
end;
|
|
|
|
function TAstBinder.Accept(const Node: IAstNode): TDataValue;
|
|
begin
|
|
if (not Assigned(Node)) or Done then
|
|
exit;
|
|
|
|
FIsTailStack.Push(FNextIsTail);
|
|
try
|
|
Result := inherited Accept(Node);
|
|
finally
|
|
FNextIsTail := FIsTailStack.Pop;
|
|
end;
|
|
end;
|
|
|
|
procedure TAstBinder.EnterScope;
|
|
begin
|
|
FCurrentDescriptor := TScope.CreateDescriptor(FCurrentDescriptor);
|
|
end;
|
|
|
|
function TAstBinder.Execute(const RootNode: IAstNode; out Descriptor: IScopeDescriptor): IAstNode;
|
|
begin
|
|
// First pass: Analyze the entire AST to find all variable declarations
|
|
// that are captured by closures (upvalues).
|
|
FBoxedDeclarations := TUpvalueAnalyzer.Analyze(RootNode, FCurrentDescriptor.Parent);
|
|
try
|
|
// Second pass: Transform the tree, using the information from the analysis pass.
|
|
EnterScope;
|
|
try
|
|
Result := Accept(RootNode).AsIntf<IAstNode>;
|
|
Descriptor := FCurrentDescriptor;
|
|
finally
|
|
ExitScope;
|
|
end;
|
|
finally
|
|
// The binder now owns the hash set, which will be freed in the destructor.
|
|
end;
|
|
end;
|
|
|
|
procedure TAstBinder.ExitScope;
|
|
begin
|
|
FCurrentDescriptor := FCurrentDescriptor.Parent;
|
|
end;
|
|
|
|
function TAstBinder.IsValidIdentifier(const Name: string): Boolean;
|
|
var
|
|
c: Char;
|
|
begin
|
|
if Name.IsEmpty then
|
|
exit(False);
|
|
|
|
c := Name[1];
|
|
if not (c.IsLetter or (c = '_')) then
|
|
exit(False);
|
|
|
|
for c in Name do
|
|
begin
|
|
if not (c.IsLetterOrDigit or (c = '_') or (c = '-')) then
|
|
exit(False);
|
|
end;
|
|
Result := True;
|
|
end;
|
|
|
|
function TAstBinder.VisitIdentifier(const Node: IIdentifierNode): TDataValue;
|
|
var
|
|
adr: TResolvedAddress;
|
|
boundNode: IIdentifierNode;
|
|
begin
|
|
adr := FCurrentDescriptor.FindSymbol(Node.Name);
|
|
if adr.Kind = akLocalOrParent then
|
|
begin
|
|
if (adr.ScopeDepth > 0) and (FUpvalueStack.Count > 0) then
|
|
begin
|
|
var upvalue := FUpvalueStack.Peek;
|
|
|
|
// up to outer scope
|
|
dec(adr.ScopeDepth);
|
|
|
|
var upvalueIndex: Integer;
|
|
if not upvalue.Map.TryGetValue(adr, upvalueIndex) then
|
|
begin
|
|
upvalueIndex := upvalue.Map.Count;
|
|
upvalue.Map.Add(adr, upvalueIndex);
|
|
end;
|
|
|
|
boundNode := TBoundIdentifierNode.Create(Node, TResolvedAddress.Create(akUpvalue, 0, upvalueIndex));
|
|
end
|
|
else
|
|
boundNode := TBoundIdentifierNode.Create(Node, adr);
|
|
Result := TDataValue.FromIntf<IIdentifierNode>(boundNode);
|
|
end
|
|
else
|
|
raise Exception.CreateFmt('Undefined identifier: "%s"', [Node.Name]);
|
|
end;
|
|
|
|
function TAstBinder.VisitVariableDeclaration(const Node: IVariableDeclarationNode): TDataValue;
|
|
var
|
|
initializer: IAstNode;
|
|
slotIndex: Integer;
|
|
address: TResolvedAddress;
|
|
boundIdentifier: IIdentifierNode;
|
|
isBoxed: Boolean;
|
|
boundDecl: IVariableDeclarationNode;
|
|
begin
|
|
if not IsValidIdentifier(Node.Identifier.Name) then
|
|
raise Exception.CreateFmt('Invalid identifier name: "%s".', [Node.Identifier.Name]);
|
|
|
|
FNextIsTail := False;
|
|
initializer := nil;
|
|
if Node.Initializer <> nil then
|
|
initializer := Accept(Node.Initializer).AsIntf<IAstNode>;
|
|
|
|
slotIndex := FCurrentDescriptor.Define(Node.Identifier.Name);
|
|
address := TResolvedAddress.Create(akLocalOrParent, 0, slotIndex);
|
|
|
|
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);
|
|
|
|
// Always create a bound declaration node, passing the IsBoxed flag.
|
|
boundDecl := TBoundVariableDeclarationNode.Create(boundIdentifier, initializer, isBoxed);
|
|
Result := TDataValue.FromIntf<IVariableDeclarationNode>(boundDecl);
|
|
end;
|
|
|
|
function TAstBinder.VisitAssignment(const Node: IAssignmentNode): TDataValue;
|
|
begin
|
|
FNextIsTail := False;
|
|
Result := inherited VisitAssignment(Node);
|
|
end;
|
|
|
|
function TAstBinder.VisitBinaryExpression(const Node: IBinaryExpressionNode): TDataValue;
|
|
begin
|
|
FNextIsTail := False;
|
|
Result := inherited VisitBinaryExpression(Node);
|
|
end;
|
|
|
|
function TAstBinder.VisitUnaryExpression(const Node: IUnaryExpressionNode): TDataValue;
|
|
begin
|
|
FNextIsTail := False;
|
|
Result := inherited VisitUnaryExpression(Node);
|
|
end;
|
|
|
|
function TAstBinder.VisitLambdaExpression(const Node: ILambdaExpressionNode): TDataValue;
|
|
var
|
|
i: integer;
|
|
boundParams: TArray<IIdentifierNode>;
|
|
boundBody: IAstNode;
|
|
lambdaScope: IScopeDescriptor;
|
|
upvalues: TArray<TResolvedAddress>;
|
|
hasNestedLambdas: Boolean;
|
|
lastNestedLambdaCount: Integer;
|
|
boundLambda: ILambdaExpressionNode;
|
|
begin
|
|
FUpvalueStack.Push(TUpvalueMapping.Create);
|
|
try
|
|
EnterScope;
|
|
try
|
|
FCurrentDescriptor.Define('<self>');
|
|
|
|
SetLength(boundParams, Length(Node.Parameters));
|
|
for i := 0 to High(Node.Parameters) do
|
|
begin
|
|
var paramNode := Node.Parameters[i];
|
|
var slotIndex := FCurrentDescriptor.Define(paramNode.Name);
|
|
var address := TResolvedAddress.Create(akLocalOrParent, 0, slotIndex);
|
|
boundParams[i] := TBoundIdentifierNode.Create(paramNode, address);
|
|
end;
|
|
|
|
lastNestedLambdaCount := FNestedLambdaCount;
|
|
|
|
FNextIsTail := True;
|
|
boundBody := Accept(Node.Body).AsIntf<IAstNode>;
|
|
|
|
hasNestedLambdas := FNestedLambdaCount > lastNestedLambdaCount;
|
|
lambdaScope := FCurrentDescriptor;
|
|
finally
|
|
ExitScope;
|
|
end;
|
|
|
|
var upvalueMapping := FUpvalueStack.Peek;
|
|
var sortedPairs := upvalueMapping.Map.ToArray;
|
|
TArray.Sort<TPair<TResolvedAddress, Integer>>(
|
|
sortedPairs,
|
|
TComparer<TPair<TResolvedAddress, Integer>>.Construct(
|
|
function(const Left, Right: TPair<TResolvedAddress, Integer>): Integer begin Result := Left.Value - Right.Value; end
|
|
)
|
|
);
|
|
|
|
SetLength(upvalues, Length(sortedPairs));
|
|
for i := 0 to High(sortedPairs) do
|
|
upvalues[i] := sortedPairs[i].Key;
|
|
|
|
finally
|
|
FUpvalueStack.Pop;
|
|
end;
|
|
|
|
inc(FNestedLambdaCount);
|
|
boundLambda := TBoundLambdaExpressionNode.Create(Node, boundBody, boundParams, lambdaScope, upvalues, hasNestedLambdas);
|
|
Result := TDataValue.FromIntf<ILambdaExpressionNode>(boundLambda);
|
|
end;
|
|
|
|
function TAstBinder.VisitFunctionCall(const Node: IFunctionCallNode): TDataValue;
|
|
var
|
|
isTailCall: Boolean;
|
|
callee: IAstNode;
|
|
args: TArray<IAstNode>;
|
|
boundCall: IFunctionCallNode;
|
|
begin
|
|
isTailCall := FIsTailStack.Peek;
|
|
|
|
FNextIsTail := False;
|
|
callee := Accept(Node.Callee).AsIntf<IAstNode>;
|
|
args := TransformNodes<IAstNode>(Node.Arguments);
|
|
|
|
boundCall := TBoundFunctionCallNode.Create(Node, callee, args, isTailCall);
|
|
Result := TDataValue.FromIntf<IFunctionCallNode>(boundCall);
|
|
end;
|
|
|
|
function TAstBinder.VisitRecurNode(const Node: IRecurNode): TDataValue;
|
|
begin
|
|
if not FIsTailStack.Peek then
|
|
raise Exception.Create('''recur'' can only be used in a tail position.');
|
|
|
|
FNextIsTail := False;
|
|
Result := inherited VisitRecurNode(Node);
|
|
end;
|
|
|
|
function TAstBinder.VisitBlockExpression(const Node: IBlockExpressionNode): TDataValue;
|
|
var
|
|
exprs: TArray<IAstNode>;
|
|
i: Integer;
|
|
isContextTail: Boolean;
|
|
begin
|
|
isContextTail := FIsTailStack.Peek;
|
|
|
|
SetLength(exprs, Length(Node.Expressions));
|
|
for i := 0 to High(exprs) do
|
|
begin
|
|
FNextIsTail := isContextTail and (i = High(exprs));
|
|
exprs[i] := Accept(Node.Expressions[i]).AsIntf<IAstNode>;
|
|
end;
|
|
Result := TDataValue.FromIntf<IBlockExpressionNode>(TAst.Block(exprs));
|
|
end;
|
|
|
|
function TAstBinder.VisitIfExpression(const Node: IIfExpressionNode): TDataValue;
|
|
var
|
|
isContextTail: Boolean;
|
|
condition, thenBranch, elseBranch: IAstNode;
|
|
begin
|
|
isContextTail := FIsTailStack.Peek;
|
|
|
|
FNextIsTail := False;
|
|
condition := Accept(Node.Condition).AsIntf<IAstNode>;
|
|
|
|
FNextIsTail := isContextTail;
|
|
thenBranch := Accept(Node.ThenBranch).AsIntf<IAstNode>;
|
|
elseBranch := Accept(Node.ElseBranch).AsIntf<IAstNode>;
|
|
|
|
if (condition <> Node.Condition) or (thenBranch <> Node.ThenBranch) or (elseBranch <> Node.ElseBranch) then
|
|
Result := TDataValue.FromIntf<IIfExpressionNode>(TAst.IfExpr(condition, thenBranch, elseBranch))
|
|
else
|
|
Result := TDataValue.FromIntf<IIfExpressionNode>(Node);
|
|
end;
|
|
|
|
function TAstBinder.VisitMacroDefinition(const Node: IMacroDefinitionNode): TDataValue;
|
|
begin
|
|
// The binder runs after the macro expander. It should never see a macro definition.
|
|
raise Exception.Create('IMacroDefinitionNode found in AST after macro expansion phase.');
|
|
end;
|
|
|
|
function TAstBinder.VisitTernaryExpression(const Node: ITernaryExpressionNode): TDataValue;
|
|
var
|
|
isContextTail: Boolean;
|
|
condition, thenBranch, elseBranch: IAstNode;
|
|
begin
|
|
isContextTail := FIsTailStack.Peek;
|
|
|
|
FNextIsTail := False;
|
|
condition := Accept(Node.Condition).AsIntf<IAstNode>;
|
|
|
|
FNextIsTail := isContextTail;
|
|
thenBranch := Accept(Node.ThenBranch).AsIntf<IAstNode>;
|
|
elseBranch := Accept(Node.ElseBranch).AsIntf<IAstNode>;
|
|
|
|
if (condition <> Node.Condition) or (thenBranch <> Node.ThenBranch) or (elseBranch <> Node.ElseBranch) then
|
|
Result := TDataValue.FromIntf<ITernaryExpressionNode>(TAst.TernaryExpr(condition, thenBranch, elseBranch))
|
|
else
|
|
Result := TDataValue.FromIntf<ITernaryExpressionNode>(Node);
|
|
end;
|
|
|
|
end.
|