411 lines
14 KiB
ObjectPascal
411 lines
14 KiB
ObjectPascal
unit Myc.Ast.Binding;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils,
|
|
System.Classes,
|
|
System.Generics.Collections,
|
|
Myc.Data.Scalar,
|
|
Myc.Data.Value,
|
|
Myc.Ast.Nodes,
|
|
Myc.Ast.Visitor,
|
|
Myc.Ast.Scope,
|
|
Myc.Ast.Analyzer,
|
|
Myc.Ast.Types,
|
|
Myc.Ast;
|
|
|
|
type
|
|
IAstBinder = interface(IAstVisitor)
|
|
function Execute(const RootNode: IAstNode; out Descriptor: IScopeDescriptor): IAstNode;
|
|
end;
|
|
|
|
TAstBinder = class; // Forward declaration
|
|
|
|
TAstBinder = class(TAstTransformer, IAstBinder)
|
|
private
|
|
type
|
|
TUpvalueMapping = TDictionary<TResolvedAddress, Integer>;
|
|
private
|
|
FInitialScope: IExecutionScope;
|
|
FCurrentDescriptor: IScopeDescriptor;
|
|
FUpvalueStack: TStack<TUpvalueMapping>;
|
|
FNestedLambdaCount: Integer;
|
|
FBoxedDeclarations: THashSet<IVariableDeclarationNode>;
|
|
|
|
procedure EnterScope;
|
|
procedure ExitScope;
|
|
function IsValidIdentifier(const Name: string): Boolean;
|
|
|
|
protected
|
|
// --- Core Binding Logic (Mutators) ---
|
|
function VisitIdentifier(const Node: IIdentifierNode): IAstNode; override;
|
|
function VisitVariableDeclaration(const Node: IVariableDeclarationNode): IAstNode; override;
|
|
function VisitLambdaExpression(const Node: ILambdaExpressionNode): IAstNode; override;
|
|
// --- Transformation Logic ---
|
|
function VisitFunctionCall(const Node: IFunctionCallNode): IAstNode; override;
|
|
|
|
// --- Standard Traversal (Use inherited) ---
|
|
function VisitKeyword(const Node: IKeywordNode): IAstNode; override;
|
|
function VisitMacroDefinition(const Node: IMacroDefinitionNode): IAstNode; override;
|
|
function VisitMacroExpansionNode(const Node: IMacroExpansionNode): IAstNode; override;
|
|
function VisitRecurNode(const Node: IRecurNode): IAstNode; override;
|
|
function VisitConstant(const Node: IConstantNode): IAstNode; override;
|
|
function VisitCreateSeries(const Node: ICreateSeriesNode): IAstNode; override;
|
|
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): IAstNode; override;
|
|
function VisitSeriesLength(const Node: ISeriesLengthNode): IAstNode; override;
|
|
|
|
public
|
|
constructor Create(const AInitialScope: IExecutionScope);
|
|
destructor Destroy; override;
|
|
function Execute(const RootNode: IAstNode; out Descriptor: IScopeDescriptor): IAstNode;
|
|
|
|
class function Bind(
|
|
const InitialScope: IExecutionScope;
|
|
const RootNode: IAstNode;
|
|
out Descriptor: IScopeDescriptor
|
|
): IAstNode; static;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.Generics.Defaults,
|
|
System.Character,
|
|
Myc.Data.Keyword;
|
|
|
|
type
|
|
TResolvedAddressComparer = class(TEqualityComparer<TResolvedAddress>)
|
|
public
|
|
function Equals(const Left, Right: TResolvedAddress): Boolean; override;
|
|
function GetHashCode(const Value: TResolvedAddress): Integer; override;
|
|
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 }
|
|
|
|
constructor TAstBinder.Create(const AInitialScope: IExecutionScope);
|
|
begin
|
|
inherited Create;
|
|
Assert(Assigned(AInitialScope));
|
|
|
|
FInitialScope := AInitialScope;
|
|
FCurrentDescriptor := AInitialScope.CreateDescriptor;
|
|
FUpvalueStack := TObjectStack<TUpvalueMapping>.Create(True); // Use Comparer
|
|
FNestedLambdaCount := 0;
|
|
FBoxedDeclarations := nil;
|
|
end;
|
|
|
|
destructor TAstBinder.Destroy;
|
|
begin
|
|
FUpvalueStack.Free;
|
|
FBoxedDeclarations.Free;
|
|
inherited;
|
|
end;
|
|
|
|
class function TAstBinder.Bind(const InitialScope: IExecutionScope; const RootNode: IAstNode; out Descriptor: IScopeDescriptor): IAstNode;
|
|
begin
|
|
var binder := TAstBinder.Create(InitialScope) as IAstBinder;
|
|
Result := binder.Execute(RootNode, Descriptor);
|
|
end;
|
|
|
|
procedure TAstBinder.EnterScope;
|
|
begin
|
|
FCurrentDescriptor := TScope.CreateDescriptor(FCurrentDescriptor);
|
|
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.Execute(const RootNode: IAstNode; out Descriptor: IScopeDescriptor): IAstNode;
|
|
begin
|
|
// Pre-pass: Find all variables that need boxing
|
|
FBoxedDeclarations := TUpvalueAnalyzer.Analyze(RootNode, FCurrentDescriptor.Parent);
|
|
try
|
|
EnterScope;
|
|
try
|
|
// Main pass: Run the mutator
|
|
Result := Accept(RootNode); // Accept returns IAstNode
|
|
if not Assigned(Result) then
|
|
Result := TAst.Block([]);
|
|
|
|
// Set the type of the root expression (e.g., the final 'do' block)
|
|
(Result as TAstNode).StaticType := TTypes.Unknown;
|
|
Descriptor := FCurrentDescriptor;
|
|
finally
|
|
ExitScope;
|
|
end;
|
|
finally
|
|
FBoxedDeclarations.Free; // Free the set
|
|
FBoxedDeclarations := nil;
|
|
end;
|
|
end;
|
|
|
|
function TAstBinder.VisitMacroDefinition(const Node: IMacroDefinitionNode): IAstNode;
|
|
begin
|
|
// Macros are compile-time only. The Binder (Phase 2) should not see them.
|
|
raise Exception.Create('TMyAstBinder: MacroDefinition node encountered.');
|
|
end;
|
|
|
|
function TAstBinder.VisitMacroExpansionNode(const Node: IMacroExpansionNode): IAstNode;
|
|
begin
|
|
// The MacroExpander (Phase 1) should have unwrapped this.
|
|
// We only visit the *expanded* body.
|
|
Result := Accept(Node.ExpandedBody);
|
|
end;
|
|
|
|
function TAstBinder.VisitFunctionCall(const Node: IFunctionCallNode): IAstNode;
|
|
begin
|
|
// --- Transformation: Keyword-as-Function ---
|
|
if (Node.Callee is TKeywordNode) then
|
|
begin
|
|
var keywordNode := (Node.Callee as TKeywordNode);
|
|
if Length(Node.Arguments) <> 1 then
|
|
raise ETypeException.CreateFmt(
|
|
'Keyword :%s expects exactly one argument (the record/map), but got %d',
|
|
[keywordNode.Value.Name, Length(Node.Arguments)]);
|
|
|
|
// Manually visit the argument, as we are replacing this node
|
|
var baseNode := Accept(Node.Arguments[0]);
|
|
var memberAccessNode := TAst.MemberAccess(baseNode, keywordNode);
|
|
|
|
// Visit the *new* node to bind it
|
|
Result := Accept(memberAccessNode);
|
|
exit;
|
|
end;
|
|
|
|
// --- Default: Bind as a standard function call ---
|
|
// Use the inherited implementation to visit children (Callee, Arguments)
|
|
// and mutate their properties in place.
|
|
Result := inherited VisitFunctionCall(Node);
|
|
|
|
// Set metadata for *this* node
|
|
(Node as TFunctionCallNode).IsTailCall := False; // Default, TCO (Phase 5) will set this
|
|
end;
|
|
|
|
function TAstBinder.VisitConstant(const Node: IConstantNode): IAstNode;
|
|
begin
|
|
// Set type (Phase 3)
|
|
case Node.Value.Kind of
|
|
TDataValueKind.vkScalar: (Node as TAstNode).StaticType := TTypes.FromScalarKind(Node.Value.AsScalar.Kind);
|
|
TDataValueKind.vkText: (Node as TAstNode).StaticType := TTypes.Text;
|
|
TDataValueKind.vkVoid: (Node as TAstNode).StaticType := TTypes.Void;
|
|
else
|
|
(Node as TAstNode).StaticType := TTypes.Unknown;
|
|
end;
|
|
Result := Node;
|
|
end;
|
|
|
|
function TAstBinder.VisitKeyword(const Node: IKeywordNode): IAstNode;
|
|
begin
|
|
(Node as TAstNode).StaticType := TTypes.Keyword;
|
|
Result := Node;
|
|
end;
|
|
|
|
function TAstBinder.VisitCreateSeries(const Node: ICreateSeriesNode): IAstNode;
|
|
var
|
|
elemType: IStaticType;
|
|
begin
|
|
try
|
|
elemType := TTypes.FromScalarKind(TScalar.StringToKind(Node.Definition));
|
|
except
|
|
on E: Exception do
|
|
elemType := TTypes.Unknown;
|
|
end;
|
|
(Node as TAstNode).StaticType := TTypes.CreateSeries(elemType);
|
|
Result := Node;
|
|
end;
|
|
|
|
function TAstBinder.VisitAddSeriesItem(const Node: IAddSeriesItemNode): IAstNode;
|
|
begin
|
|
// Visit children
|
|
Result := inherited VisitAddSeriesItem(Node);
|
|
(Node as TAstNode).StaticType := TTypes.Void;
|
|
end;
|
|
|
|
function TAstBinder.VisitSeriesLength(const Node: ISeriesLengthNode): IAstNode;
|
|
begin
|
|
// Visit children
|
|
Result := inherited VisitSeriesLength(Node);
|
|
(Node as TAstNode).StaticType := TTypes.Ordinal;
|
|
end;
|
|
|
|
function TAstBinder.VisitLambdaExpression(const Node: ILambdaExpressionNode): IAstNode;
|
|
var
|
|
i: integer;
|
|
N: TLambdaExpressionNode;
|
|
adr: TResolvedAddress;
|
|
begin
|
|
N := (Node as TLambdaExpressionNode);
|
|
|
|
// We do *not* call inherited, as we must manage the scope manually.
|
|
|
|
FUpvalueStack.Push(TUpvalueMapping.Create(TResolvedAddressComparer.Create));
|
|
try
|
|
EnterScope;
|
|
try
|
|
// Define <self> (slot 0)
|
|
FCurrentDescriptor.Define('<self>', TTypes.Unknown);
|
|
|
|
// Define parameters
|
|
for i := 0 to High(N.Parameters) do
|
|
begin
|
|
var paramNode := N.Parameters[i];
|
|
var slotIndex := FCurrentDescriptor.Define(paramNode.Name, TTypes.Unknown);
|
|
adr := TResolvedAddress.Create(akLocalOrParent, 0, slotIndex);
|
|
|
|
// Mutate the parameter node with its address
|
|
(paramNode as TIdentifierNode).Address := adr;
|
|
(paramNode as TAstNode).StaticType := TTypes.Unknown;
|
|
end;
|
|
|
|
// Visit the body *within the new scope*
|
|
var lastNestedLambdaCount := FNestedLambdaCount;
|
|
N.Body := Accept(N.Body);
|
|
N.HasNestedLambdas := FNestedLambdaCount > lastNestedLambdaCount;
|
|
|
|
// Save the descriptor for the evaluator
|
|
N.ScopeDescriptor := FCurrentDescriptor;
|
|
finally
|
|
ExitScope;
|
|
end;
|
|
|
|
// --- Extract Upvalues ---
|
|
var upvalueMapping := FUpvalueStack.Peek;
|
|
var sortedPairs := upvalueMapping.ToArray;
|
|
// Sort by index (Value)
|
|
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
|
|
)
|
|
);
|
|
|
|
var uvArr: TArray<TResolvedAddress>;
|
|
SetLength(uvArr, Length(sortedPairs));
|
|
for i := 0 to High(uvArr) do
|
|
uvArr[i] := sortedPairs[i].Key;
|
|
N.Upvalues := uvArr;
|
|
|
|
finally
|
|
FUpvalueStack.Pop;
|
|
end;
|
|
|
|
inc(FNestedLambdaCount);
|
|
// Type will be set by TypeChecker
|
|
Result := Node;
|
|
end;
|
|
|
|
function TAstBinder.VisitRecurNode(const Node: IRecurNode): IAstNode;
|
|
begin
|
|
// Visit children
|
|
Result := inherited VisitRecurNode(Node);
|
|
(Node as TAstNode).StaticType := TTypes.Void; // Recur never returns a value
|
|
end;
|
|
|
|
function TAstBinder.VisitIdentifier(const Node: IIdentifierNode): IAstNode;
|
|
var
|
|
symbol: TResolvedSymbol;
|
|
adr: TResolvedAddress;
|
|
begin
|
|
symbol := FCurrentDescriptor.FindSymbol(Node.Name);
|
|
adr := symbol.Address;
|
|
|
|
if adr.Kind = akLocalOrParent then
|
|
begin
|
|
if (adr.ScopeDepth > 0) and (FUpvalueStack.Count > 0) then
|
|
begin
|
|
// --- Handle Upvalue ---
|
|
var upvalueMap := FUpvalueStack.Peek;
|
|
// Adjust address to be relative to the captured scope
|
|
dec(adr.ScopeDepth);
|
|
|
|
var upvalueIndex: Integer;
|
|
if not upvalueMap.TryGetValue(adr, upvalueIndex) then
|
|
begin
|
|
// This is a new upvalue for this lambda
|
|
upvalueIndex := upvalueMap.Count;
|
|
upvalueMap.Add(adr, upvalueIndex);
|
|
end;
|
|
|
|
// Mutate the node to point to the Upvalue slot
|
|
(Node as TIdentifierNode).Address := TResolvedAddress.Create(akUpvalue, 0, upvalueIndex);
|
|
end
|
|
else
|
|
begin
|
|
// --- Handle LocalOrParent ---
|
|
// Mutate the node to point to the Local/Parent slot
|
|
(Node as TIdentifierNode).Address := adr;
|
|
end;
|
|
|
|
(Node as TAstNode).StaticType := symbol.StaticType; // Set type from scope
|
|
end
|
|
else
|
|
raise Exception.CreateFmt('Undefined identifier: "%s"', [Node.Name]);
|
|
|
|
Result := Node;
|
|
end;
|
|
|
|
function TAstBinder.VisitVariableDeclaration(const Node: IVariableDeclarationNode): IAstNode;
|
|
var
|
|
slotIndex: Integer;
|
|
address: TResolvedAddress;
|
|
N: TVariableDeclarationNode;
|
|
begin
|
|
N := (Node as TVariableDeclarationNode);
|
|
|
|
if not IsValidIdentifier(N.Identifier.Name) then
|
|
raise Exception.CreateFmt('Invalid identifier name: "%s".', [N.Identifier.Name]);
|
|
|
|
// 1. Visit initializer *first*
|
|
if Assigned(N.Initializer) then
|
|
N.Initializer := Accept(N.Initializer);
|
|
|
|
// 2. Define variable in *current* scope
|
|
slotIndex := FCurrentDescriptor.Define(N.Identifier.Name, TTypes.Unknown);
|
|
address := TResolvedAddress.Create(akLocalOrParent, 0, slotIndex);
|
|
|
|
// 3. Mutate the Identifier node (which is NOT visited by inherited call)
|
|
(N.Identifier as TIdentifierNode).Address := address;
|
|
(N.Identifier as TAstNode).StaticType := TTypes.Unknown; // TypeChecker will set this
|
|
|
|
// 4. Mutate this declaration node
|
|
N.IsBoxed := (FBoxedDeclarations <> nil) and FBoxedDeclarations.Contains(Node);
|
|
|
|
Result := Node;
|
|
end;
|
|
|
|
end.
|