Immutable infered types

This commit is contained in:
Michael Schimmel
2025-11-04 22:10:11 +01:00
parent f73c0c67b8
commit 980919525b
6 changed files with 395 additions and 271 deletions
+7 -11
View File
@@ -12,7 +12,6 @@ uses
Myc.Ast.Visitor, Myc.Ast.Visitor,
Myc.Ast.Scope, Myc.Ast.Scope,
Myc.Ast.Analyzer, Myc.Ast.Analyzer,
Myc.Ast.Types,
Myc.Ast; Myc.Ast;
type type
@@ -162,7 +161,6 @@ begin
Result := TAst.Block([]); Result := TAst.Block([]);
// Set the type of the root expression (e.g., the final 'do' block) // Set the type of the root expression (e.g., the final 'do' block)
(Result as TAstNode).StaticType := TTypes.Unknown;
Descriptor := FCurrentDescriptor; Descriptor := FCurrentDescriptor;
finally finally
ExitScope; ExitScope;
@@ -195,7 +193,7 @@ begin
begin begin
var keywordNode := (Node.Callee as TKeywordNode); var keywordNode := (Node.Callee as TKeywordNode);
if Length(Node.Arguments) <> 1 then if Length(Node.Arguments) <> 1 then
raise ETypeException.CreateFmt( raise EArgumentException.CreateFmt(
'Keyword :%s expects exactly one argument (the record/map), but got %d', 'Keyword :%s expects exactly one argument (the record/map), but got %d',
[keywordNode.Value.Name, Length(Node.Arguments)]); [keywordNode.Value.Name, Length(Node.Arguments)]);
@@ -264,18 +262,18 @@ begin
EnterScope; EnterScope;
try try
// Define <self> (slot 0) // Define <self> (slot 0)
FCurrentDescriptor.Define('<self>', TTypes.Unknown); FCurrentDescriptor.Define('<self>');
// Define parameters // Define parameters
for i := 0 to High(N.Parameters) do for i := 0 to High(N.Parameters) do
begin begin
var paramNode := N.Parameters[i]; // This is a TIdentifierNode var paramNode := N.Parameters[i]; // This is a TIdentifierNode
var slotIndex := FCurrentDescriptor.Define(paramNode.Name, TTypes.Unknown); var slotIndex := FCurrentDescriptor.Define(paramNode.Name);
adr := TResolvedAddress.Create(akLocalOrParent, 0, slotIndex); adr := TResolvedAddress.Create(akLocalOrParent, 0, slotIndex);
// --- Replace Node --- // --- Replace Node ---
// Create a new TBoundIdentifierNode (implementation is in Myc.Ast) // Create a new TBoundIdentifierNode (implementation is in Myc.Ast)
var boundParamNode := TBoundIdentifierNode.Create(paramNode.Name, adr); var boundParamNode := TAst.BoundIdentifier(paramNode.Name, adr);
// Replace it in the parameter array // Replace it in the parameter array
N.Parameters[i] := boundParamNode; N.Parameters[i] := boundParamNode;
end; end;
@@ -364,8 +362,7 @@ begin
// --- Replace Node --- // --- Replace Node ---
// Create the new TBoundIdentifierNode (implementation is in Myc.Ast) // Create the new TBoundIdentifierNode (implementation is in Myc.Ast)
Result := TBoundIdentifierNode.Create(Node.Name, adr); Result := TAst.BoundIdentifier(Node.Name, adr);
// StaticType remains TTypes.Unknown (default)
end end
else else
begin begin
@@ -390,13 +387,12 @@ begin
N.Initializer := Accept(N.Initializer); N.Initializer := Accept(N.Initializer);
// 2. Define variable in *current* scope // 2. Define variable in *current* scope
slotIndex := FCurrentDescriptor.Define(N.Identifier.Name, TTypes.Unknown); slotIndex := FCurrentDescriptor.Define(N.Identifier.Name);
address := TResolvedAddress.Create(akLocalOrParent, 0, slotIndex); address := TResolvedAddress.Create(akLocalOrParent, 0, slotIndex);
// 3. --- Replace Node --- // 3. --- Replace Node ---
// Replace the TIdentifierNode with a new TBoundIdentifierNode // Replace the TIdentifierNode with a new TBoundIdentifierNode
N.Identifier := TBoundIdentifierNode.Create(N.Identifier.Name, address); N.Identifier := TAst.BoundIdentifier(N.Identifier.Name, address);
// StaticType remains TTypes.Unknown (default)
// 4. Mutate this declaration node // 4. Mutate this declaration node
N.IsBoxed := (FBoxedDeclarations <> nil) and FBoxedDeclarations.Contains(Node); N.IsBoxed := (FBoxedDeclarations <> nil) and FBoxedDeclarations.Contains(Node);
+10 -20
View File
@@ -26,7 +26,6 @@ type
private private
FBinaryOperators: TDictionary<string, TScalar.TBinaryOp>; FBinaryOperators: TDictionary<string, TScalar.TBinaryOp>;
FUnaryOperators: TDictionary<string, TScalar.TUnaryOp>; FUnaryOperators: TDictionary<string, TScalar.TUnaryOp>;
function SetType(const Node: IAstNode; const AType: IStaticType): IAstNode;
protected protected
// Override to find nodes to lower // Override to find nodes to lower
function VisitFunctionCall(const Node: IFunctionCallNode): IAstNode; override; function VisitFunctionCall(const Node: IFunctionCallNode): IAstNode; override;
@@ -82,16 +81,6 @@ begin
Result := Accept(RootNode); // Use IAstNode-returning Accept Result := Accept(RootNode); // Use IAstNode-returning Accept
if not Assigned(Result) then if not Assigned(Result) then
Result := TAst.Block([]); Result := TAst.Block([]);
if Assigned(Result) then
(Result as TAstNode).StaticType := (Result as TAstNode).StaticType;
end;
function TAstLowerer.SetType(const Node: IAstNode; const AType: IStaticType): IAstNode;
begin
if Assigned(Node) then // Add nil check for safety
(Node as TAstNode).StaticType := AType;
Result := Node;
end; end;
function TAstLowerer.VisitFunctionCall(const Node: IFunctionCallNode): IAstNode; function TAstLowerer.VisitFunctionCall(const Node: IFunctionCallNode): IAstNode;
@@ -100,11 +89,14 @@ var
binaryOp: TScalar.TBinaryOp; binaryOp: TScalar.TBinaryOp;
unaryOp: TScalar.TUnaryOp; unaryOp: TScalar.TUnaryOp;
left, right: IAstNode; left, right: IAstNode;
nodeType: IStaticType;
begin begin
// --- Optimization: Operator Folding --- // --- 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;
// Get the type *before* replacing the node (it's an IAstTypedNode)
nodeType := Node.AsTypedNode.StaticType;
if (Length(Node.Arguments) = 2) then if (Length(Node.Arguments) = 2) then
begin begin
@@ -113,9 +105,9 @@ begin
// Note: We MUST visit children *before* creating the new node // Note: We MUST visit children *before* creating the new node
left := Accept(Node.Arguments[0]); left := Accept(Node.Arguments[0]);
right := Accept(Node.Arguments[1]); right := Accept(Node.Arguments[1]);
var binExpr := TAst.BinaryExpr(left, binaryOp, right);
// Copy metadata (StaticType) from old node to new node // Call constructor directly, passing the inferred type
Result := SetType(binExpr, (Node as TAstNode).StaticType); Result := TBinaryExpressionNode.Create(left, binaryOp, right, nodeType);
exit; exit;
end; end;
end; end;
@@ -125,18 +117,16 @@ begin
if FUnaryOperators.TryGetValue(calleeIdentifier.Name, unaryOp) then if FUnaryOperators.TryGetValue(calleeIdentifier.Name, unaryOp) then
begin begin
right := Accept(Node.Arguments[0]); right := Accept(Node.Arguments[0]);
var unExpr := TAst.UnaryExpr(unaryOp, right); // Call constructor directly, passing the inferred type
// Copy metadata Result := TUnaryExpressionNode.Create(unaryOp, right, nodeType);
Result := SetType(unExpr, (Node as TAstNode).StaticType);
exit; exit;
end; end;
if (calleeIdentifier.Name = '-') then if (calleeIdentifier.Name = '-') then
begin begin
right := Accept(Node.Arguments[0]); right := Accept(Node.Arguments[0]);
var unExpr := TAst.UnaryExpr(TScalar.TUnaryOp.Negate, right); // Call constructor directly, passing the inferred type
// Copy metadata Result := TUnaryExpressionNode.Create(TScalar.TUnaryOp.Negate, right, nodeType);
Result := SetType(unExpr, (Node as TAstNode).StaticType);
exit; exit;
end; end;
end; end;
+12 -2
View File
@@ -7,7 +7,8 @@ uses
System.Generics.Collections, System.Generics.Collections,
Myc.Data.Scalar, Myc.Data.Scalar,
Myc.Data.Value, Myc.Data.Value,
Myc.Data.Keyword; Myc.Data.Keyword,
Myc.Ast.Types;
type type
// --- Forward Declarations to break cycles --- // --- Forward Declarations to break cycles ---
@@ -39,6 +40,7 @@ type
IRecurNode = interface; IRecurNode = interface;
INopNode = interface; INopNode = interface;
IBoundIdentifierNode = interface; IBoundIdentifierNode = interface;
IAstTypedNode = interface;
// Defines the concrete kinds of AST nodes // Defines the concrete kinds of AST nodes
TAstNodeKind = ( TAstNodeKind = (
@@ -157,13 +159,21 @@ type
function AsAddSeriesItem: IAddSeriesItemNode; function AsAddSeriesItem: IAddSeriesItemNode;
function AsSeriesLength: ISeriesLengthNode; function AsSeriesLength: ISeriesLengthNode;
function AsRecur: IRecurNode; function AsRecur: IRecurNode;
function AsNop: INopNode; // Added Nop function AsNop: INopNode;
function AsBoundIdentifierNode: IBoundIdentifierNode; function AsBoundIdentifierNode: IBoundIdentifierNode;
function AsTypedNode: IAstTypedNode;
property Kind: TAstNodeKind read GetKind; property Kind: TAstNodeKind read GetKind;
end; end;
IAstTypedNode = interface(IAstNode)
{$region 'private'}
function GetStaticType: IStaticType;
{$endregion}
property StaticType: IStaticType read GetStaticType;
end;
INopNode = interface(IAstNode) INopNode = interface(IAstNode)
// A placeholder node for the UI (e.g., drag target). // A placeholder node for the UI (e.g., drag target).
// This node is illegal during compilation. // This node is illegal during compilation.
+9 -5
View File
@@ -65,7 +65,7 @@ type
{$endregion} {$endregion}
function CreateScope(const AParent: IExecutionScope): IExecutionScope; function CreateScope(const AParent: IExecutionScope): IExecutionScope;
// Defines a symbol, returns its slot index. // Defines a symbol, returns its slot index.
function Define(const Name: string; const AType: IStaticType): Integer; function Define(const Name: string; const AType: IStaticType = nil): Integer;
// Updates the type of an already defined symbol (e.g., for lambda self-reference). // Updates the type of an already defined symbol (e.g., for lambda self-reference).
procedure UpdateType(SlotIndex: Integer; const AType: IStaticType); procedure UpdateType(SlotIndex: Integer; const AType: IStaticType);
procedure DefineMacro(const Name: string; const Node: IMacroDefinitionNode); procedure DefineMacro(const Name: string; const Node: IMacroDefinitionNode);
@@ -161,7 +161,7 @@ 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; const AType: IStaticType): Integer; function Define(const Name: string; const AType: IStaticType): Integer;
procedure UpdateType(SlotIndex: Integer; const AType: IStaticType); procedure UpdateType(SlotIndex: Integer; const AType: IStaticType = nil);
procedure DefineMacro(const Name: string; const Node: IMacroDefinitionNode); procedure DefineMacro(const Name: string; const Node: IMacroDefinitionNode);
function FindSymbol(const Name: string): TResolvedSymbol; function FindSymbol(const Name: string): TResolvedSymbol;
function FindMacro(const Name: string): IMacroDefinitionNode; function FindMacro(const Name: string): IMacroDefinitionNode;
@@ -525,14 +525,18 @@ begin
Result := FSymbols.Count; Result := FSymbols.Count;
FSymbols.Add(Name, Result); FSymbols.Add(Name, Result);
SetLength(FSlotTypes, Result + 1); SetLength(FSlotTypes, Result + 1);
FSlotTypes[Result] := AType; FSlotTypes[Result] :=
if AType <> nil then AType
else TTypes.Unknown;
end; end;
procedure TScopeDescriptor.UpdateType(SlotIndex: Integer; const AType: IStaticType); procedure TScopeDescriptor.UpdateType(SlotIndex: Integer; const AType: IStaticType = nil);
begin begin
if (SlotIndex < 0) or (SlotIndex >= Length(FSlotTypes)) then if (SlotIndex < 0) or (SlotIndex >= Length(FSlotTypes)) then
raise EArgumentOutOfRangeException.Create('Invalid SlotIndex in UpdateType.'); raise EArgumentOutOfRangeException.Create('Invalid SlotIndex in UpdateType.');
FSlotTypes[SlotIndex] := AType; FSlotTypes[SlotIndex] :=
If AType <> nil then AType
else TTypes.Unknown;
end; end;
procedure TScopeDescriptor.DefineMacro(const Name: string; const Node: IMacroDefinitionNode); procedure TScopeDescriptor.DefineMacro(const Name: string; const Node: IMacroDefinitionNode);
+194 -121
View File
@@ -22,11 +22,10 @@ type
// This transformer runs *after* the TAstBinder. // This transformer runs *after* the TAstBinder.
// It takes the "Bound AST" (which has addresses but mostly TTypes.Unknown) // It takes the "Bound AST" (which has addresses but mostly TTypes.Unknown)
// and traverses it bottom-up to infer and check all static types. // and traverses it bottom-up to infer and check all static types.
// It modifies the node.StaticType property and updates the IScopeDescriptor. // It *replaces* all IAstTypedNodes with new nodes containing the correct type.
TTypeChecker = class(TAstTransformer, IAstTypeChecker) TTypeChecker = class(TAstTransformer, IAstTypeChecker)
private private
FCurrentDescriptor: IScopeDescriptor; FCurrentDescriptor: IScopeDescriptor;
function SetType(const Node: IAstNode; const AType: IStaticType): IAstNode;
protected protected
// Override all visit methods to perform type checking // Override all visit methods to perform type checking
function VisitIdentifier(const Node: IIdentifierNode): IAstNode; override; function VisitIdentifier(const Node: IIdentifierNode): IAstNode; override;
@@ -88,24 +87,13 @@ begin
Result := Accept(RootNode); // Use IAstNode-returning Accept Result := Accept(RootNode); // Use IAstNode-returning Accept
if not Assigned(Result) then if not Assigned(Result) then
Result := TAst.Block([]); Result := TAst.Block([]);
// (Result as TAstNode).StaticType is set by the last Visit call
end;
function TTypeChecker.SetType(const Node: IAstNode; const AType: IStaticType): IAstNode;
begin
if Assigned(Node) then
(Node as TAstNode).StaticType := AType;
Result := Node;
end; end;
function TTypeChecker.VisitConstant(const Node: IConstantNode): IAstNode; function TTypeChecker.VisitConstant(const Node: IConstantNode): IAstNode;
var var
constType: IStaticType; constType: IStaticType;
begin begin
// This is a leaf node, call inherited (does nothing) // This is a leaf node.
Result := inherited VisitConstant(Node);
// Assign the type based on the literal value // Assign the type based on the literal value
case Node.Value.Kind of case Node.Value.Kind of
TDataValueKind.vkScalar: constType := TTypes.FromScalarKind(Node.Value.AsScalar.Kind); TDataValueKind.vkScalar: constType := TTypes.FromScalarKind(Node.Value.AsScalar.Kind);
@@ -115,34 +103,44 @@ begin
constType := TTypes.Unknown; constType := TTypes.Unknown;
end; end;
Result := SetType(Result, constType); // Create a new node with the correct type
Result := TConstantNode.Create(Node.Value, constType);
end; end;
function TTypeChecker.VisitKeyword(const Node: IKeywordNode): IAstNode; function TTypeChecker.VisitKeyword(const Node: IKeywordNode): IAstNode;
begin begin
// This is a leaf node // This is a leaf node.
Result := inherited VisitKeyword(Node); // The TKeywordNode constructor *forces* the type to be TTypes.Keyword.
Result := SetType(Result, TTypes.Keyword); Result := TKeywordNode.Create(Node.Value);
end; end;
function TTypeChecker.VisitIdentifier(const Node: IIdentifierNode): IAstNode; function TTypeChecker.VisitIdentifier(const Node: IIdentifierNode): IAstNode;
var var
symbol: TResolvedSymbol; symbol: TResolvedSymbol;
adr: TResolvedAddress;
begin begin
// This is a leaf node (guaranteed to be IBoundIdentifierNode) // This is a leaf node (guaranteed to be IBoundIdentifierNode by Binder)
Result := inherited VisitIdentifier(Node);
// Get the type from the descriptor (which was populated by Binder/RTL) // Get the type from the descriptor (which was populated by Binder/RTL)
symbol := FCurrentDescriptor.FindSymbol(Node.Name); symbol := FCurrentDescriptor.FindSymbol(Node.Name);
Result := SetType(Result, symbol.StaticType); adr := Node.AsBoundIdentifierNode.Address;
// Create a new node, copying the address and assigning the inferred type
Result := TBoundIdentifierNode.Create(Node.Name, adr, symbol.StaticType);
end; end;
function TTypeChecker.VisitRecurNode(const Node: IRecurNode): IAstNode; function TTypeChecker.VisitRecurNode(const Node: IRecurNode): IAstNode;
var
newArgs: TArray<IAstNode>;
i: Integer;
begin begin
// Visit children first // 1. Visit children
Result := inherited VisitRecurNode(Node); SetLength(newArgs, Length(Node.Arguments));
// Recur always results in Void (it never returns) for i := 0 to High(Node.Arguments) do
Result := SetType(Result, TTypes.Void); newArgs[i] := Accept(Node.Arguments[i]);
// 2. Create new node with inferred type
Result := TRecurNode.Create(newArgs, TTypes.Void);
end; end;
function TTypeChecker.VisitMacroDefinition(const Node: IMacroDefinitionNode): IAstNode; function TTypeChecker.VisitMacroDefinition(const Node: IMacroDefinitionNode): IAstNode;
@@ -159,43 +157,55 @@ end;
function TTypeChecker.VisitVariableDeclaration(const Node: IVariableDeclarationNode): IAstNode; function TTypeChecker.VisitVariableDeclaration(const Node: IVariableDeclarationNode): IAstNode;
var var
initType: IStaticType; initType: IStaticType;
newInitializer, newIdent: IAstNode;
boundIdent: IIdentifierNode; boundIdent: IIdentifierNode;
adr: TResolvedAddress; adr: TResolvedAddress;
N: TVariableDeclarationNode;
begin begin
// 1. Visit children first (Identifier is leaf, Initializer is traversed) // 1. Visit Initializer first (if it exists)
inherited; if Assigned(Node.Initializer) then
newInitializer := Accept(Node.Initializer)
else
newInitializer := nil;
// 2. Get initializer type // 2. Get initializer type
if Assigned(Node.Initializer) then if Assigned(newInitializer) then
initType := (Node.Initializer as TAstNode).StaticType initType := newInitializer.AsTypedNode.StaticType
else else
initType := TTypes.Unknown; // <-- This was TTypes.Void initType := TTypes.Unknown; // (def fib)
// 3. Get the address from the bound identifier (SAFE CAST) // 3. Get the address from the bound identifier (SAFE CAST)
boundIdent := Node.Identifier; boundIdent := Node.Identifier;
adr := boundIdent.AsBoundIdentifierNode.Address; adr := boundIdent.AsBoundIdentifierNode.Address;
// 4. Update the type in the scope descriptor (which was set to Unknown by the binder). // 4. Update the type in the scope descriptor (which was set to Unknown by the binder).
// Only update if we have a concrete type (not the default Unknown).
if initType.Kind <> stUnknown then if initType.Kind <> stUnknown then
FCurrentDescriptor.UpdateType(adr.SlotIndex, initType); FCurrentDescriptor.UpdateType(adr.SlotIndex, initType);
// 5. Update the static types of the nodes themselves. // 5. Create the new (typed) identifier node
(boundIdent as TAstNode).StaticType := initType; newIdent := TBoundIdentifierNode.Create(boundIdent.Name, adr, initType);
Result := SetType(Node, initType);
// 6. Create the new VariableDeclaration node
Result := TVariableDeclarationNode.Create(newIdent.AsIdentifier, newInitializer, initType);
// 7. Copy runtime flags (IsBoxed)
N := (Result as TVariableDeclarationNode);
N.IsBoxed := (Node as TVariableDeclarationNode).IsBoxed;
end; end;
function TTypeChecker.VisitAssignment(const Node: IAssignmentNode): IAstNode; function TTypeChecker.VisitAssignment(const Node: IAssignmentNode): IAstNode;
var var
targetType, sourceType: IStaticType; targetType, sourceType: IStaticType;
newIdent, newValue: IAstNode;
adr: TResolvedAddress; adr: TResolvedAddress;
begin begin
// 1. Visit children first (Identifier, Value) // 1. Visit children first (Identifier, Value)
inherited; newValue := Accept(Node.Value);
newIdent := Accept(Node.Identifier);
// 2. Get types // 2. Get types
targetType := (Node.Identifier as TAstNode).StaticType; targetType := newIdent.AsTypedNode.StaticType;
sourceType := (Node.Value as TAstNode).StaticType; sourceType := newValue.AsTypedNode.StaticType;
// 3. Check assignment // 3. Check assignment
if not TTypeRules.CanAssign(targetType, sourceType) then if not TTypeRules.CanAssign(targetType, sourceType) then
@@ -205,19 +215,23 @@ begin
// with the new, inferred type. This enables recursion. // with the new, inferred type. This enables recursion.
if (targetType.Kind = stUnknown) and (sourceType.Kind <> stUnknown) then if (targetType.Kind = stUnknown) and (sourceType.Kind <> stUnknown) then
begin begin
adr := Node.Identifier.AsBoundIdentifierNode.Address; adr := newIdent.AsBoundIdentifierNode.Address;
FCurrentDescriptor.UpdateType(adr.SlotIndex, sourceType); FCurrentDescriptor.UpdateType(adr.SlotIndex, sourceType);
// Re-set the identifier's node type (it was visited *before* this)
(Node.Identifier as TAstNode).StaticType := sourceType; // Re-create the identifier node *with the new type*
newIdent := TBoundIdentifierNode.Create(newIdent.AsIdentifier.Name, adr, sourceType);
targetType := sourceType; targetType := sourceType;
end; end;
Result := SetType(Node, targetType); // 5. Create the new Assignment node
Result := TAssignmentNode.Create(newIdent.AsIdentifier, newValue, targetType);
end; end;
function TTypeChecker.VisitLambdaExpression(const Node: ILambdaExpressionNode): IAstNode; function TTypeChecker.VisitLambdaExpression(const Node: ILambdaExpressionNode): IAstNode;
var var
boundNode: TLambdaExpressionNode; boundNode: TLambdaExpressionNode;
newParams: TArray<IIdentifierNode>;
newBody: IAstNode;
bodyType, methodType: IStaticType; bodyType, methodType: IStaticType;
paramTypes: TArray<IStaticType>; paramTypes: TArray<IStaticType>;
i: Integer; i: Integer;
@@ -229,58 +243,77 @@ begin
savedDescriptor := FCurrentDescriptor; savedDescriptor := FCurrentDescriptor;
FCurrentDescriptor := boundNode.ScopeDescriptor; FCurrentDescriptor := boundNode.ScopeDescriptor;
try try
// 2. Parameters are leaves, so we don't try to evaluate them // 2. Visit parameters (they are already bound, just need typing)
SetLength(newParams, Length(boundNode.Parameters));
// 3. Get parameter types (currently Unknown, but required for signature)
SetLength(paramTypes, Length(boundNode.Parameters)); SetLength(paramTypes, Length(boundNode.Parameters));
for i := 0 to High(boundNode.Parameters) do for i := 0 to High(boundNode.Parameters) do
paramTypes[i] := (boundNode.Parameters[i] as TAstNode).StaticType; // Propagates Unknown begin
// Parameters are leaves, but we must *replace* them with typed versions
// (even if they are just TTypes.Unknown for now, for type inference placeholders)
var paramIdent := boundNode.Parameters[i];
var paramAdr := paramIdent.AsBoundIdentifierNode.Address;
var newParam := TBoundIdentifierNode.Create(paramIdent.Name, paramAdr, TTypes.Unknown);
// 4. Visit the body to infer its return type newParams[i] := newParam;
Accept(boundNode.Body); paramTypes[i] := TTypes.Unknown;
bodyType := (boundNode.Body as TAstNode).StaticType; end;
// 5. Create the final method type // 3. Visit the body to infer its return type
newBody := Accept(boundNode.Body);
bodyType := newBody.AsTypedNode.StaticType;
// 4. Create the final method type
methodType := TTypes.CreateMethod(paramTypes, bodyType); methodType := TTypes.CreateMethod(paramTypes, bodyType);
// 6. Update the type for <self> (Slot 0) in the descriptor // 5. Update the type for <self> (Slot 0) in the descriptor
FCurrentDescriptor.UpdateType(0, methodType); FCurrentDescriptor.UpdateType(0, methodType);
finally finally
// 7. Restore parent descriptor // 6. Restore parent descriptor
FCurrentDescriptor := savedDescriptor; FCurrentDescriptor := savedDescriptor;
end; end;
// 8. Set the type of the lambda node itself // 7. Create the new (typed) lambda node
Result := SetType(boundNode, methodType); Result := TLambdaExpressionNode.Create(newParams, newBody, methodType);
// 8. Copy runtime properties
var newLambda := (Result as TLambdaExpressionNode);
newLambda.ScopeDescriptor := boundNode.ScopeDescriptor;
newLambda.Upvalues := boundNode.Upvalues;
newLambda.HasNestedLambdas := boundNode.HasNestedLambdas;
end; end;
function TTypeChecker.VisitFunctionCall(const Node: IFunctionCallNode): IAstNode; function TTypeChecker.VisitFunctionCall(const Node: IFunctionCallNode): IAstNode;
var var
calleeType, retType: IStaticType; calleeType, retType: IStaticType;
i: Integer; i: Integer;
newCallee: IAstNode;
newArgs: TArray<IAstNode>;
begin begin
// 1. Visit children first (bottom-up) // 1. Visit children first (bottom-up)
inherited; newCallee := Accept(Node.Callee);
SetLength(newArgs, Length(Node.Arguments));
for i := 0 to High(Node.Arguments) do
newArgs[i] := Accept(Node.Arguments[i]);
// 2. Get callee type (now inferred) // 2. Get callee type (now inferred)
calleeType := (Node.Callee as TAstNode).StaticType; calleeType := newCallee.AsTypedNode.StaticType;
retType := TTypes.Unknown; // Default if not a method retType := TTypes.Unknown; // Default if not a method
// 3. Perform type checking // 3. Perform type checking
if calleeType.Kind = TStaticTypeKind.stMethod then if calleeType.Kind = TStaticTypeKind.stMethod then
begin begin
var signature := calleeType.Signature; var signature := calleeType.Signature;
if Length(Node.Arguments) <> Length(signature.ParamTypes) then if Length(newArgs) <> Length(signature.ParamTypes) then
raise ETypeException raise ETypeException.CreateFmt('Function expects %d arguments, but got %d', [Length(signature.ParamTypes), Length(newArgs)]);
.CreateFmt('Function expects %d arguments, but got %d', [Length(signature.ParamTypes), Length(Node.Arguments)]);
retType := signature.ReturnType; retType := signature.ReturnType;
// Check argument types // Check argument types
for i := 0 to High(Node.Arguments) do for i := 0 to High(newArgs) do
begin begin
var argType := (Node.Arguments[i] as TAstNode).StaticType; var argType := newArgs[i].AsTypedNode.StaticType;
var paramType := signature.ParamTypes[i]; var paramType := signature.ParamTypes[i];
if not TTypeRules.CanAssign(paramType, argType) then if not TTypeRules.CanAssign(paramType, argType) then
raise ETypeException raise ETypeException
@@ -290,109 +323,136 @@ begin
else if calleeType.Kind <> TStaticTypeKind.stUnknown then else if calleeType.Kind <> TStaticTypeKind.stUnknown then
raise ETypeException.CreateFmt('Cannot invoke type %s as a function.', [calleeType.ToString]); raise ETypeException.CreateFmt('Cannot invoke type %s as a function.', [calleeType.ToString]);
// 4. Set the type for this call node // 4. Create the new (typed) call node
Result := SetType(Node, retType); Result := TFunctionCallNode.Create(newCallee, newArgs, retType);
// 5. Copy runtime properties
(Result as TFunctionCallNode).IsTailCall := (Node as TFunctionCallNode).IsTailCall;
end; end;
function TTypeChecker.VisitBlockExpression(const Node: IBlockExpressionNode): IAstNode; function TTypeChecker.VisitBlockExpression(const Node: IBlockExpressionNode): IAstNode;
var var
blockType: IStaticType; blockType: IStaticType;
newExprs: TArray<IAstNode>;
i: Integer;
begin begin
// 1. Visit children // 1. Visit children
inherited; SetLength(newExprs, Length(Node.Expressions));
for i := 0 to High(Node.Expressions) do
newExprs[i] := Accept(Node.Expressions[i]);
// 2. Type is type of last expression // 2. Type is type of last expression
if Length(Node.Expressions) > 0 then if Length(newExprs) > 0 then
blockType := (Node.Expressions[High(Node.Expressions)] as TAstNode).StaticType blockType := newExprs[High(newExprs)].AsTypedNode.StaticType
else else
blockType := TTypes.Void; blockType := TTypes.Void;
Result := SetType(Node, blockType); // 3. Create new node
Result := TBlockExpressionNode.Create(newExprs, blockType);
end; end;
function TTypeChecker.VisitIfExpression(const Node: IIfExpressionNode): IAstNode; function TTypeChecker.VisitIfExpression(const Node: IIfExpressionNode): IAstNode;
var var
conditionType, thenType, elseType, resultType: IStaticType; conditionType, thenType, elseType, resultType: IStaticType;
newCond, newThen, newElse: IAstNode;
begin begin
// 1. Visit children // 1. Visit children
inherited; newCond := Accept(Node.Condition);
newThen := Accept(Node.ThenBranch);
newElse := Accept(Node.ElseBranch); // Accept handles nil
// 2. Check condition // 2. Check condition
conditionType := (Node.Condition as TAstNode).StaticType; conditionType := newCond.AsTypedNode.StaticType;
if (conditionType.Kind <> stUnknown) and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType) then if (conditionType.Kind <> stUnknown) and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType) then
raise ETypeException.CreateFmt('If condition must be Ordinal, but got %s', [conditionType.ToString]); raise ETypeException.CreateFmt('If condition must be Ordinal, but got %s', [conditionType.ToString]);
// 3. Promote branch types // 3. Promote branch types
thenType := (Node.ThenBranch as TAstNode).StaticType; thenType := newThen.AsTypedNode.StaticType;
elseType := elseType :=
if Node.ElseBranch <> nil then (Node.ElseBranch as TAstNode).StaticType if newElse <> nil then newElse.AsTypedNode.StaticType
else TTypes.Void; else TTypes.Void;
resultType := TTypeRules.Promote(thenType, elseType); resultType := TTypeRules.Promote(thenType, elseType);
Result := SetType(Node, resultType);
// 4. Create new node
Result := TIfExpressionNode.Create(newCond, newThen, newElse, resultType);
end; end;
function TTypeChecker.VisitTernaryExpression(const Node: ITernaryExpressionNode): IAstNode; function TTypeChecker.VisitTernaryExpression(const Node: ITernaryExpressionNode): IAstNode;
var var
conditionType, thenType, elseType, resultType: IStaticType; conditionType, thenType, elseType, resultType: IStaticType;
newCond, newThen, newElse: IAstNode;
begin begin
// 1. Visit children // 1. Visit children
inherited; newCond := Accept(Node.Condition);
newThen := Accept(Node.ThenBranch);
newElse := Accept(Node.ElseBranch);
// 2. Check condition // 2. Check condition
conditionType := (Node.Condition as TAstNode).StaticType; conditionType := newCond.AsTypedNode.StaticType;
if (conditionType.Kind <> stUnknown) and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType) then if (conditionType.Kind <> stUnknown) and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType) then
raise ETypeException.CreateFmt('Ternary condition must be Ordinal, but got %s', [conditionType.ToString]); raise ETypeException.CreateFmt('Ternary condition must be Ordinal, but got %s', [conditionType.ToString]);
// 3. Promote branch types // 3. Promote branch types
thenType := (Node.ThenBranch as TAstNode).StaticType; thenType := newThen.AsTypedNode.StaticType;
elseType := (Node.ElseBranch as TAstNode).StaticType; elseType := newElse.AsTypedNode.StaticType;
resultType := TTypeRules.Promote(thenType, elseType); resultType := TTypeRules.Promote(thenType, elseType);
Result := SetType(Node, resultType);
// 4. Create new node
Result := TTernaryExpressionNode.Create(newCond, newThen, newElse, resultType);
end; end;
function TTypeChecker.VisitBinaryExpression(const Node: IBinaryExpressionNode): IAstNode; function TTypeChecker.VisitBinaryExpression(const Node: IBinaryExpressionNode): IAstNode;
var var
leftType, rightType, resultType: IStaticType; leftType, rightType, resultType: IStaticType;
newLeft, newRight: IAstNode;
begin begin
// 1. Visit children // 1. Visit children
inherited; newLeft := Accept(Node.Left);
newRight := Accept(Node.Right);
// 2. Get types // 2. Get types
leftType := (Node.Left as TAstNode).StaticType; leftType := newLeft.AsTypedNode.StaticType;
rightType := (Node.Right as TAstNode).StaticType; rightType := newRight.AsTypedNode.StaticType;
// 3. Resolve // 3. Resolve
resultType := TTypeRules.ResolveBinaryOp(Node.Operator, leftType, rightType); resultType := TTypeRules.ResolveBinaryOp(Node.Operator, leftType, rightType);
Result := SetType(Node, resultType);
// 4. Create new node
Result := TBinaryExpressionNode.Create(newLeft, Node.Operator, newRight, resultType);
end; end;
function TTypeChecker.VisitUnaryExpression(const Node: IUnaryExpressionNode): IAstNode; function TTypeChecker.VisitUnaryExpression(const Node: IUnaryExpressionNode): IAstNode;
var var
rightType, resultType: IStaticType; rightType, resultType: IStaticType;
newRight: IAstNode;
begin begin
// 1. Visit children // 1. Visit children
inherited; newRight := Accept(Node.Right);
// 2. Get types // 2. Get types
rightType := (Node.Right as TAstNode).StaticType; rightType := newRight.AsTypedNode.StaticType;
// 3. Resolve // 3. Resolve
resultType := TTypeRules.ResolveUnaryOp(Node.Operator, rightType); resultType := TTypeRules.ResolveUnaryOp(Node.Operator, rightType);
Result := SetType(Node, resultType);
// 4. Create new node
Result := TUnaryExpressionNode.Create(Node.Operator, newRight, resultType);
end; end;
function TTypeChecker.VisitMemberAccess(const Node: IMemberAccessNode): IAstNode; function TTypeChecker.VisitMemberAccess(const Node: IMemberAccessNode): IAstNode;
var var
baseType, elemType: IStaticType; baseType, elemType: IStaticType;
fieldIndex: Integer; fieldIndex: Integer;
newBase, newMember: IAstNode;
begin begin
// 1. Visit children // 1. Visit children
inherited; newBase := Accept(Node.Base);
newMember := Accept(Node.Member); // Visits the TKeywordNode
// 2. Get types // 2. Get types
baseType := (Node.Base as TAstNode).StaticType; baseType := newBase.AsTypedNode.StaticType;
elemType := TTypes.Unknown; elemType := TTypes.Unknown;
// 3. Resolve // 3. Resolve
@@ -425,19 +485,22 @@ begin
end; end;
end; end;
Result := SetType(Node, elemType); // 4. Create new node
Result := TMemberAccessNode.Create(newBase, newMember.AsKeyword, elemType);
end; end;
function TTypeChecker.VisitIndexer(const Node: IIndexerNode): IAstNode; function TTypeChecker.VisitIndexer(const Node: IIndexerNode): IAstNode;
var var
baseType, indexType, elemType: IStaticType; baseType, indexType, elemType: IStaticType;
newBase, newIndex: IAstNode;
begin begin
// 1. Visit children // 1. Visit children
inherited; newBase := Accept(Node.Base);
newIndex := Accept(Node.Index);
// 2. Get types // 2. Get types
baseType := (Node.Base as TAstNode).StaticType; baseType := newBase.AsTypedNode.StaticType;
indexType := (Node.Index as TAstNode).StaticType; indexType := newIndex.AsTypedNode.StaticType;
elemType := TTypes.Unknown; elemType := TTypes.Unknown;
// 3. Resolve // 3. Resolve
@@ -455,7 +518,8 @@ begin
elemType := TTypes.CreateRecord(baseType.Definition); elemType := TTypes.CreateRecord(baseType.Definition);
end; end;
Result := SetType(Node, elemType); // 4. Create new node
Result := TIndexerNode.Create(newBase, newIndex, elemType);
end; end;
function TTypeChecker.VisitRecordLiteral(const Node: IRecordLiteralNode): IAstNode; function TTypeChecker.VisitRecordLiteral(const Node: IRecordLiteralNode): IAstNode;
@@ -464,25 +528,29 @@ var
scalarDefFields: TArray<TScalarRecordField>; scalarDefFields: TArray<TScalarRecordField>;
def: IScalarRecordDefinition; def: IScalarRecordDefinition;
staticType: IStaticType; staticType: IStaticType;
valNode: IAstNode;
valType: IStaticType; valType: IStaticType;
scalarKind: TScalar.TKind; scalarKind: TScalar.TKind;
allScalar: Boolean; allScalar: Boolean;
N: TGenericRecordLiteralNode; // Parser creates this type oldNode: TGenericRecordLiteralNode; // Parser creates this type
newFields: TArray<TRecordFieldLiteral>;
begin begin
oldNode := (Node as TGenericRecordLiteralNode);
// 1. Visit all child nodes first to infer their types // 1. Visit all child nodes first to infer their types
inherited; SetLength(newFields, Length(oldNode.Fields));
for i := 0 to High(oldNode.Fields) do
begin
newFields[i].Key := Accept(oldNode.Fields[i].Key).AsKeyword;
newFields[i].Value := Accept(oldNode.Fields[i].Value);
end;
N := (Node as TGenericRecordLiteralNode); SetLength(scalarDefFields, Length(newFields));
SetLength(scalarDefFields, Length(N.Fields));
allScalar := True; allScalar := True;
// 2. Check if this record literal can be a TScalarRecord // 2. Check if this record literal can be a TScalarRecord
for i := 0 to High(N.Fields) do for i := 0 to High(newFields) do
begin begin
valNode := N.Fields[i].Value; valType := newFields[i].Value.AsTypedNode.StaticType;
valType := (valNode as TAstNode).StaticType;
if (valType.Kind = stOrdinal) then if (valType.Kind = stOrdinal) then
scalarKind := TScalar.TKind.Ordinal scalarKind := TScalar.TKind.Ordinal
@@ -497,31 +565,29 @@ begin
end; end;
if allScalar then if allScalar then
scalarDefFields[i] := TScalarRecordField.Create(N.Fields[i].Key.Value, scalarKind); scalarDefFields[i] := TScalarRecordField.Create(newFields[i].Key.Value, scalarKind);
end; end;
// 3. Create the appropriate record type (Scalar or Generic) and mutate the node // 3. Create the new node and set its type/definitions
if allScalar then if allScalar then
begin begin
def := TScalarRecordRegistry.Intern(scalarDefFields); def := TScalarRecordRegistry.Intern(scalarDefFields);
staticType := TTypes.CreateRecord(def); staticType := TTypes.CreateRecord(def);
N.Definition := def; // Mutate Result := TRecordLiteralNode.Create(newFields, staticType);
N.GenericDefinition := nil; // Mutate (Result as TRecordLiteralNode).Definition := def;
end end
else else
begin begin
var genDefFields: TArray<TPair<IKeyword, IStaticType>>; var genDefFields: TArray<TPair<IKeyword, IStaticType>>;
SetLength(genDefFields, Length(N.Fields)); SetLength(genDefFields, Length(newFields));
for i := 0 to High(N.Fields) do for i := 0 to High(newFields) do
genDefFields[i] := TPair<IKeyword, IStaticType>.Create(N.Fields[i].Key.Value, (N.Fields[i].Value as TAstNode).StaticType); genDefFields[i] := TPair<IKeyword, IStaticType>.Create(newFields[i].Key.Value, newFields[i].Value.AsTypedNode.StaticType);
var genDef := TGenericRecordRegistry.Intern(genDefFields); var genDef := TGenericRecordRegistry.Intern(genDefFields);
staticType := TTypes.CreateGenericRecord(genDef); staticType := TTypes.CreateGenericRecord(genDef);
N.GenericDefinition := genDef; // Mutate Result := TGenericRecordLiteralNode.Create(newFields, staticType);
N.Definition := nil; // Mutate (Result as TGenericRecordLiteralNode).GenericDefinition := genDef;
end; end;
Result := SetType(N, staticType);
end; end;
function TTypeChecker.VisitCreateSeries(const Node: ICreateSeriesNode): IAstNode; function TTypeChecker.VisitCreateSeries(const Node: ICreateSeriesNode): IAstNode;
@@ -529,7 +595,6 @@ var
elemType: IStaticType; elemType: IStaticType;
begin begin
// This is a leaf node // This is a leaf node
Result := inherited VisitCreateSeries(Node);
// Assign the type // Assign the type
try try
@@ -538,19 +603,24 @@ begin
on E: Exception do on E: Exception do
elemType := TTypes.Unknown; elemType := TTypes.Unknown;
end; end;
Result := SetType(Result, TTypes.CreateSeries(elemType));
// Create new node
Result := TCreateSeriesNode.Create(Node.Definition, TTypes.CreateSeries(elemType));
end; end;
function TTypeChecker.VisitAddSeriesItem(const Node: IAddSeriesItemNode): IAstNode; function TTypeChecker.VisitAddSeriesItem(const Node: IAddSeriesItemNode): IAstNode;
var var
seriesType, valueType: IStaticType; seriesType, valueType: IStaticType;
newSeries, newValue, newLookback: IAstNode;
begin begin
// 1. Visit children // 1. Visit children
inherited; newSeries := Accept(Node.Series);
newValue := Accept(Node.Value);
newLookback := Accept(Node.Lookback); // Handles nil
// 2. Get types // 2. Get types
seriesType := (Node.Series as TAstNode).StaticType; seriesType := newSeries.AsTypedNode.StaticType;
valueType := (Node.Value as TAstNode).StaticType; valueType := newValue.AsTypedNode.StaticType;
// 3. Check types // 3. Check types
if (seriesType.Kind <> stUnknown) then if (seriesType.Kind <> stUnknown) then
@@ -563,25 +633,27 @@ begin
.CreateFmt('Cannot add item of type %s to series of type %s', [valueType.ToString, seriesType.ElementType.ToString]); .CreateFmt('Cannot add item of type %s to series of type %s', [valueType.ToString, seriesType.ElementType.ToString]);
end; end;
if (Node.Lookback <> nil) then if (newLookback <> nil) then
begin begin
var lookbackType := (Node.Lookback as TAstNode).StaticType; var lookbackType := newLookback.AsTypedNode.StaticType;
if (lookbackType.Kind <> stUnknown) and not (lookbackType.Kind = TStaticTypeKind.stOrdinal) then if (lookbackType.Kind <> stUnknown) and not (lookbackType.Kind = TStaticTypeKind.stOrdinal) then
raise ETypeException.Create('Lookback parameter for "add" must be an ordinal value.'); raise ETypeException.Create('Lookback parameter for "add" must be an ordinal value.');
end; end;
Result := SetType(Node, TTypes.Void); // 4. Create new node
Result := TAddSeriesItemNode.Create(newSeries.AsIdentifier, newValue, newLookback, TTypes.Void);
end; end;
function TTypeChecker.VisitSeriesLength(const Node: ISeriesLengthNode): IAstNode; function TTypeChecker.VisitSeriesLength(const Node: ISeriesLengthNode): IAstNode;
var var
seriesType: IStaticType; seriesType: IStaticType;
newSeries: IAstNode;
begin begin
// 1. Visit children // 1. Visit children
inherited; newSeries := Accept(Node.Series);
// 2. Get type // 2. Get type
seriesType := (Node.Series as TAstNode).StaticType; seriesType := newSeries.AsTypedNode.StaticType;
// 3. Check type // 3. Check type
if (seriesType.Kind <> stUnknown) if (seriesType.Kind <> stUnknown)
@@ -589,7 +661,8 @@ begin
and (seriesType.Kind <> TStaticTypeKind.stRecordSeries) then and (seriesType.Kind <> TStaticTypeKind.stRecordSeries) then
raise ETypeException.CreateFmt('"length" requires a series, but got %s', [seriesType.ToString]); raise ETypeException.CreateFmt('"length" requires a series, but got %s', [seriesType.ToString]);
Result := SetType(Node, TTypes.Ordinal); // 4. Create new node
Result := TSeriesLengthNode.Create(newSeries.AsIdentifier, TTypes.Ordinal);
end; end;
end. end.
+163 -112
View File
@@ -70,13 +70,13 @@ type
const ALookback: IAstNode = nil const ALookback: IAstNode = nil
): IAddSeriesItemNode; static; ): IAddSeriesItemNode; static;
class function SeriesLength(const ASeries: IIdentifierNode): ISeriesLengthNode; static; class function SeriesLength(const ASeries: IIdentifierNode): ISeriesLengthNode; static;
class function BoundIdentifier(AName: string; const Address: TResolvedAddress): IBoundIdentifierNode; static;
end; end;
// Common base class for AST nodes to reduce boilerplate. // Common base class for AST nodes to reduce boilerplate.
TAstNode = class(TInterfacedObject, IAstNode) TAstNode = class(TInterfacedObject, IAstNode)
private private
FStaticType: IStaticType;
procedure SetStaticType(const AValue: IStaticType);
function GetKind: TAstNodeKind; virtual; abstract; function GetKind: TAstNodeKind; virtual; abstract;
public public
constructor Create; constructor Create;
@@ -110,30 +110,40 @@ type
function AsNop: INopNode; virtual; // Added Nop function AsNop: INopNode; virtual; // Added Nop
function AsBoundIdentifierNode: IBoundIdentifierNode; virtual; function AsBoundIdentifierNode: IBoundIdentifierNode; virtual;
function AsTypedNode: IAstTypedNode; virtual;
property StaticType: IStaticType read FStaticType write SetStaticType;
property Kind: TAstNodeKind read GetKind; property Kind: TAstNodeKind read GetKind;
end; end;
TConstantNode = class(TAstNode, IConstantNode) TAstTypedNode = class(TAstNode, IAstTypedNode)
private
FStaticType: IStaticType;
function GetStaticType: IStaticType;
public
constructor Create(const AStaticType: IStaticType);
function AsTypedNode: IAstTypedNode; override;
property StaticType: IStaticType read GetStaticType;
end;
TConstantNode = class(TAstTypedNode, IConstantNode)
private private
FValue: TDataValue; FValue: TDataValue;
function GetValue: TDataValue; function GetValue: TDataValue;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const AValue: TDataValue); constructor Create(const AValue: TDataValue; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsConstant: IConstantNode; override; function AsConstant: IConstantNode; override;
property Value: TDataValue read GetValue; // Value ist immutable property Value: TDataValue read GetValue; // Value ist immutable
end; end;
TIdentifierNode = class(TAstNode, IIdentifierNode) TIdentifierNode = class(TAstTypedNode, IIdentifierNode)
private private
FName: string; FName: string;
function GetName: string; function GetName: string;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const AName: string); constructor Create(const AName: string; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsIdentifier: IIdentifierNode; override; function AsIdentifier: IIdentifierNode; override;
property Name: string read FName; // Name ist immutable property Name: string read FName; // Name ist immutable
@@ -145,12 +155,12 @@ type
function GetAddress: TResolvedAddress; function GetAddress: TResolvedAddress;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const AName: string; const AAddress: TResolvedAddress); constructor Create(const AName: string; const AAddress: TResolvedAddress; const AStaticType: IStaticType);
function AsBoundIdentifierNode: IBoundIdentifierNode; override; function AsBoundIdentifierNode: IBoundIdentifierNode; override;
property Address: TResolvedAddress read GetAddress; property Address: TResolvedAddress read GetAddress;
end; end;
TKeywordNode = class(TAstNode, IKeywordNode) TKeywordNode = class(TAstTypedNode, IKeywordNode)
private private
FValue: IKeyword; FValue: IKeyword;
function GetValue: IKeyword; function GetValue: IKeyword;
@@ -162,7 +172,7 @@ type
property Value: IKeyword read FValue; // Value ist immutable property Value: IKeyword read FValue; // Value ist immutable
end; end;
TBinaryExpressionNode = class(TAstNode, IBinaryExpressionNode) TBinaryExpressionNode = class(TAstTypedNode, IBinaryExpressionNode)
private private
FLeft: IAstNode; FLeft: IAstNode;
FOperator: TScalar.TBinaryOp; FOperator: TScalar.TBinaryOp;
@@ -172,7 +182,7 @@ type
function GetRight: IAstNode; function GetRight: IAstNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const ALeft: IAstNode; AOperator: TScalar.TBinaryOp; const ARight: IAstNode); constructor Create(const ALeft: IAstNode; AOperator: TScalar.TBinaryOp; const ARight: IAstNode; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsBinaryExpression: IBinaryExpressionNode; override; function AsBinaryExpression: IBinaryExpressionNode; override;
property Left: IAstNode read FLeft write FLeft; // Writeable property Left: IAstNode read FLeft write FLeft; // Writeable
@@ -180,7 +190,7 @@ type
property Right: IAstNode read FRight write FRight; // Writeable property Right: IAstNode read FRight write FRight; // Writeable
end; end;
TUnaryExpressionNode = class(TAstNode, IUnaryExpressionNode) TUnaryExpressionNode = class(TAstTypedNode, IUnaryExpressionNode)
private private
FOperator: TScalar.TUnaryOp; FOperator: TScalar.TUnaryOp;
FRight: IAstNode; FRight: IAstNode;
@@ -188,14 +198,14 @@ type
function GetRight: IAstNode; function GetRight: IAstNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const AOperator: TScalar.TUnaryOp; const ARight: IAstNode); constructor Create(const AOperator: TScalar.TUnaryOp; const ARight: IAstNode; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsUnaryExpression: IUnaryExpressionNode; override; function AsUnaryExpression: IUnaryExpressionNode; override;
property Operator: TScalar.TUnaryOp read FOperator write FOperator; // Writeable property Operator: TScalar.TUnaryOp read FOperator write FOperator; // Writeable
property Right: IAstNode read FRight write FRight; // Writeable property Right: IAstNode read FRight write FRight; // Writeable
end; end;
TIfExpressionNode = class(TAstNode, IIfExpressionNode) TIfExpressionNode = class(TAstTypedNode, IIfExpressionNode)
private private
FCondition: IAstNode; FCondition: IAstNode;
FThenBranch: IAstNode; FThenBranch: IAstNode;
@@ -205,7 +215,7 @@ type
function GetElseBranch: IAstNode; function GetElseBranch: IAstNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const ACondition, AThenBranch, AElseBranch: IAstNode); constructor Create(const ACondition, AThenBranch, AElseBranch: IAstNode; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsIfExpression: IIfExpressionNode; override; function AsIfExpression: IIfExpressionNode; override;
property Condition: IAstNode read FCondition write FCondition; // Writeable property Condition: IAstNode read FCondition write FCondition; // Writeable
@@ -213,7 +223,7 @@ type
property ElseBranch: IAstNode read FElseBranch write FElseBranch; // Writeable property ElseBranch: IAstNode read FElseBranch write FElseBranch; // Writeable
end; end;
TTernaryExpressionNode = class(TAstNode, ITernaryExpressionNode) TTernaryExpressionNode = class(TAstTypedNode, ITernaryExpressionNode)
private private
FCondition: IAstNode; FCondition: IAstNode;
FThenBranch: IAstNode; FThenBranch: IAstNode;
@@ -223,7 +233,7 @@ type
function GetElseBranch: IAstNode; function GetElseBranch: IAstNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const ACondition, AThenBranch, AElseBranch: IAstNode); constructor Create(const ACondition, AThenBranch, AElseBranch: IAstNode; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsTernaryExpression: ITernaryExpressionNode; override; function AsTernaryExpression: ITernaryExpressionNode; override;
property Condition: IAstNode read FCondition write FCondition; // Writeable property Condition: IAstNode read FCondition write FCondition; // Writeable
@@ -231,7 +241,7 @@ type
property ElseBranch: IAstNode read FElseBranch write FElseBranch; // Writeable property ElseBranch: IAstNode read FElseBranch write FElseBranch; // Writeable
end; end;
TLambdaExpressionNode = class(TAstNode, ILambdaExpressionNode) TLambdaExpressionNode = class(TAstTypedNode, ILambdaExpressionNode)
private private
FParameters: TArray<IIdentifierNode>; FParameters: TArray<IIdentifierNode>;
FBody: IAstNode; FBody: IAstNode;
@@ -242,7 +252,7 @@ type
function GetBody: IAstNode; function GetBody: IAstNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const AParameters: TArray<IIdentifierNode>; const ABody: IAstNode); constructor Create(const AParameters: TArray<IIdentifierNode>; const ABody: IAstNode; const AStaticType: IStaticType);
destructor Destroy; override; destructor Destroy; override;
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsLambdaExpression: ILambdaExpressionNode; override; function AsLambdaExpression: ILambdaExpressionNode; override;
@@ -307,7 +317,7 @@ type
property Expression: IQuasiquoteNode read GetExpression write FExpression; property Expression: IQuasiquoteNode read GetExpression write FExpression;
end; end;
TFunctionCallNode = class(TAstNode, IFunctionCallNode) TFunctionCallNode = class(TAstTypedNode, IFunctionCallNode)
private private
FCallee: IAstNode; FCallee: IAstNode;
FArguments: TArray<IAstNode>; FArguments: TArray<IAstNode>;
@@ -316,7 +326,7 @@ type
function GetArguments: TArray<IAstNode>; function GetArguments: TArray<IAstNode>;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const ACallee: IAstNode; const AArguments: TArray<IAstNode>); constructor Create(const ACallee: IAstNode; const AArguments: TArray<IAstNode>; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsFunctionCall: IFunctionCallNode; override; function AsFunctionCall: IFunctionCallNode; override;
property Callee: IAstNode read FCallee write FCallee; // Writeable property Callee: IAstNode read FCallee write FCallee; // Writeable
@@ -324,13 +334,13 @@ type
property IsTailCall: Boolean read FIsTailCall write FIsTailCall; property IsTailCall: Boolean read FIsTailCall write FIsTailCall;
end; end;
TRecurNode = class(TAstNode, IRecurNode) TRecurNode = class(TAstTypedNode, IRecurNode)
private private
FArguments: TArray<IAstNode>; FArguments: TArray<IAstNode>;
function GetArguments: TArray<IAstNode>; function GetArguments: TArray<IAstNode>;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const AArguments: TArray<IAstNode>); constructor Create(const AArguments: TArray<IAstNode>; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsRecur: IRecurNode; override; function AsRecur: IRecurNode; override;
property Arguments: TArray<IAstNode> read FArguments write FArguments; // Writeable property Arguments: TArray<IAstNode> read FArguments write FArguments; // Writeable
@@ -342,24 +352,25 @@ type
function GetExpandedBody: IAstNode; function GetExpandedBody: IAstNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const AOriginalCallNode: IFunctionCallNode; const AExpandedBody: IAstNode); constructor Create(const AOriginalCallNode: IFunctionCallNode; const AExpandedBody: IAstNode; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsMacroExpansion: IMacroExpansionNode; override; function AsMacroExpansion: IMacroExpansionNode; override;
property ExpandedBody: IAstNode read FExpandedBody write FExpandedBody; // Writeable property ExpandedBody: IAstNode read FExpandedBody write FExpandedBody; // Writeable
end; end;
TBlockExpressionNode = class(TAstNode, IBlockExpressionNode)
TBlockExpressionNode = class(TAstTypedNode, IBlockExpressionNode)
private private
FExpressions: TArray<IAstNode>; FExpressions: TArray<IAstNode>;
function GetExpressions: TArray<IAstNode>; function GetExpressions: TArray<IAstNode>;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const AExpressions: array of IAstNode); constructor Create(const AExpressions: array of IAstNode; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsBlockExpression: IBlockExpressionNode; override; function AsBlockExpression: IBlockExpressionNode; override;
property Expressions: TArray<IAstNode> read FExpressions write FExpressions; // Writeable property Expressions: TArray<IAstNode> read FExpressions write FExpressions; // Writeable
end; end;
TVariableDeclarationNode = class(TAstNode, IVariableDeclarationNode) TVariableDeclarationNode = class(TAstTypedNode, IVariableDeclarationNode)
private private
FIdentifier: IIdentifierNode; FIdentifier: IIdentifierNode;
FInitializer: IAstNode; FInitializer: IAstNode;
@@ -368,7 +379,7 @@ type
function GetInitializer: IAstNode; function GetInitializer: IAstNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const AIdentifier: IIdentifierNode; AInitializer: IAstNode); constructor Create(const AIdentifier: IIdentifierNode; AInitializer: IAstNode; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsVariableDeclaration: IVariableDeclarationNode; override; function AsVariableDeclaration: IVariableDeclarationNode; override;
property Identifier: IIdentifierNode read FIdentifier write FIdentifier; // Writeable property Identifier: IIdentifierNode read FIdentifier write FIdentifier; // Writeable
@@ -376,7 +387,7 @@ type
property IsBoxed: Boolean read FIsBoxed write FIsBoxed; property IsBoxed: Boolean read FIsBoxed write FIsBoxed;
end; end;
TAssignmentNode = class(TAstNode, IAssignmentNode) TAssignmentNode = class(TAstTypedNode, IAssignmentNode)
private private
FIdentifier: IIdentifierNode; FIdentifier: IIdentifierNode;
FValue: IAstNode; FValue: IAstNode;
@@ -384,14 +395,14 @@ type
function GetValue: IAstNode; function GetValue: IAstNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const AIdentifier: IIdentifierNode; const AValue: IAstNode); constructor Create(const AIdentifier: IIdentifierNode; const AValue: IAstNode; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsAssignment: IAssignmentNode; override; function AsAssignment: IAssignmentNode; override;
property Identifier: IIdentifierNode read FIdentifier write FIdentifier; property Identifier: IIdentifierNode read FIdentifier write FIdentifier;
property Value: IAstNode read FValue write FValue; // Writeable property Value: IAstNode read FValue write FValue; // Writeable
end; end;
TIndexerNode = class(TAstNode, IIndexerNode) TIndexerNode = class(TAstTypedNode, IIndexerNode)
private private
FBase: IAstNode; FBase: IAstNode;
FIndex: IAstNode; FIndex: IAstNode;
@@ -399,14 +410,14 @@ type
function GetIndex: IAstNode; function GetIndex: IAstNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const ABase: IAstNode; const AIndex: IAstNode); constructor Create(const ABase: IAstNode; const AIndex: IAstNode; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsIndexer: IIndexerNode; override; function AsIndexer: IIndexerNode; override;
property Base: IAstNode read FBase write FBase; // Writeable property Base: IAstNode read FBase write FBase; // Writeable
property Index: IAstNode read FIndex write FIndex; // Writeable property Index: IAstNode read FIndex write FIndex; // Writeable
end; end;
TMemberAccessNode = class(TAstNode, IMemberAccessNode) TMemberAccessNode = class(TAstTypedNode, IMemberAccessNode)
private private
FBase: IAstNode; FBase: IAstNode;
FMember: IKeywordNode; FMember: IKeywordNode;
@@ -414,21 +425,21 @@ type
function GetMember: IKeywordNode; function GetMember: IKeywordNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const ABase: IAstNode; const AMember: IKeywordNode); constructor Create(const ABase: IAstNode; const AMember: IKeywordNode; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsMemberAccess: IMemberAccessNode; override; function AsMemberAccess: IMemberAccessNode; override;
property Base: IAstNode read FBase write FBase; // Writeable property Base: IAstNode read FBase write FBase; // Writeable
property Member: IKeywordNode read FMember write FMember; property Member: IKeywordNode read FMember write FMember;
end; end;
TRecordLiteralNode = class(TAstNode, IRecordLiteralNode) TRecordLiteralNode = class(TAstTypedNode, IRecordLiteralNode)
private private
FFields: TArray<TRecordFieldLiteral>; FFields: TArray<TRecordFieldLiteral>;
FDefinition: IScalarRecordDefinition; FDefinition: IScalarRecordDefinition;
function GetFields: TArray<TRecordFieldLiteral>; function GetFields: TArray<TRecordFieldLiteral>;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const AFields: TArray<TRecordFieldLiteral>); constructor Create(const AFields: TArray<TRecordFieldLiteral>; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsRecordLiteral: IRecordLiteralNode; override; function AsRecordLiteral: IRecordLiteralNode; override;
property Fields: TArray<TRecordFieldLiteral> read FFields write FFields; // Writeable property Fields: TArray<TRecordFieldLiteral> read FFields write FFields; // Writeable
@@ -439,22 +450,22 @@ type
private private
FGenericDefinition: IGenericRecordDefinition; FGenericDefinition: IGenericRecordDefinition;
public public
constructor Create(const AFields: TArray<TRecordFieldLiteral>); constructor Create(const AFields: TArray<TRecordFieldLiteral>; const AStaticType: IStaticType);
property GenericDefinition: IGenericRecordDefinition read FGenericDefinition write FGenericDefinition; property GenericDefinition: IGenericRecordDefinition read FGenericDefinition write FGenericDefinition;
end; end;
TCreateSeriesNode = class(TAstNode, ICreateSeriesNode) TCreateSeriesNode = class(TAstTypedNode, ICreateSeriesNode)
private private
FDefinition: String; FDefinition: String;
function GetDefinition: String; function GetDefinition: String;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const ADefinition: String); constructor Create(const ADefinition: String; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsCreateSeries: ICreateSeriesNode; override; function AsCreateSeries: ICreateSeriesNode; override;
end; end;
TAddSeriesItemNode = class(TAstNode, IAddSeriesItemNode) TAddSeriesItemNode = class(TAstTypedNode, IAddSeriesItemNode)
private private
FSeries: IIdentifierNode; FSeries: IIdentifierNode;
FValue: IAstNode; FValue: IAstNode;
@@ -464,7 +475,12 @@ type
function GetLookback: IAstNode; function GetLookback: IAstNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const ASeries: IIdentifierNode; const AValue: IAstNode; const ALookback: IAstNode); constructor Create(
const ASeries: IIdentifierNode;
const AValue: IAstNode;
const ALookback: IAstNode;
const AStaticType: IStaticType
);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsAddSeriesItem: IAddSeriesItemNode; override; function AsAddSeriesItem: IAddSeriesItemNode; override;
property Series: IIdentifierNode read FSeries write FSeries; property Series: IIdentifierNode read FSeries write FSeries;
@@ -472,13 +488,13 @@ type
property Lookback: IAstNode read FLookback write FLookback; // Writeable property Lookback: IAstNode read FLookback write FLookback; // Writeable
end; end;
TSeriesLengthNode = class(TAstNode, ISeriesLengthNode) TSeriesLengthNode = class(TAstTypedNode, ISeriesLengthNode)
private private
FSeries: IIdentifierNode; FSeries: IIdentifierNode;
function GetSeries: IIdentifierNode; function GetSeries: IIdentifierNode;
function GetKind: TAstNodeKind; override; function GetKind: TAstNodeKind; override;
public public
constructor Create(const ASeries: IIdentifierNode); constructor Create(const ASeries: IIdentifierNode; const AStaticType: IStaticType);
function Accept(const Visitor: IAstVisitor): TDataValue; override; function Accept(const Visitor: IAstVisitor): TDataValue; override;
function AsSeriesLength: ISeriesLengthNode; override; function AsSeriesLength: ISeriesLengthNode; override;
property Series: IIdentifierNode read FSeries write FSeries; property Series: IIdentifierNode read FSeries write FSeries;
@@ -516,7 +532,7 @@ end;
class function TAst.CreateSeries(const ADefinition: String): ICreateSeriesNode; class function TAst.CreateSeries(const ADefinition: String): ICreateSeriesNode;
begin begin
Result := TCreateSeriesNode.Create(ADefinition); Result := TCreateSeriesNode.Create(ADefinition, TTypes.Unknown);
end; end;
class function TAst.AddSeriesItem( class function TAst.AddSeriesItem(
@@ -525,44 +541,44 @@ class function TAst.AddSeriesItem(
const ALookback: IAstNode = nil const ALookback: IAstNode = nil
): IAddSeriesItemNode; ): IAddSeriesItemNode;
begin begin
Result := TAddSeriesItemNode.Create(ASeries, AValue, ALookback); Result := TAddSeriesItemNode.Create(ASeries, AValue, ALookback, TTypes.Unknown);
end; end;
class function TAst.SeriesLength(const ASeries: IIdentifierNode): ISeriesLengthNode; class function TAst.SeriesLength(const ASeries: IIdentifierNode): ISeriesLengthNode;
begin begin
Result := TSeriesLengthNode.Create(ASeries); Result := TSeriesLengthNode.Create(ASeries, TTypes.Unknown);
end; end;
class function TAst.Assign(const AIdentifier: IIdentifierNode; const AValue: IAstNode): IAssignmentNode; class function TAst.Assign(const AIdentifier: IIdentifierNode; const AValue: IAstNode): IAssignmentNode;
begin begin
Result := TAssignmentNode.Create(AIdentifier, AValue); Result := TAssignmentNode.Create(AIdentifier, AValue, TTypes.Unknown);
end; end;
class function TAst.AssignResult(const AValue: IAstNode): IAssignmentNode; class function TAst.AssignResult(const AValue: IAstNode): IAssignmentNode;
begin begin
// The parser ensures 'Result' is a valid identifier, so we create one. // The parser ensures 'Result' is a valid identifier, so we create one.
// The binder will resolve it. // The binder will resolve it.
Result := TAssignmentNode.Create(TAst.Identifier('Result'), AValue); Result := TAssignmentNode.Create(TAst.Identifier('Result'), AValue, TTypes.Unknown);
end; end;
class function TAst.BinaryExpr(ALeft: IAstNode; AOperator: TScalar.TBinaryOp; ARight: IAstNode): IBinaryExpressionNode; class function TAst.BinaryExpr(ALeft: IAstNode; AOperator: TScalar.TBinaryOp; ARight: IAstNode): IBinaryExpressionNode;
begin begin
Result := TBinaryExpressionNode.Create(ALeft, AOperator, ARight); Result := TBinaryExpressionNode.Create(ALeft, AOperator, ARight, TTypes.Unknown);
end; end;
class function TAst.Block(const AExpressions: array of IAstNode): IBlockExpressionNode; class function TAst.Block(const AExpressions: array of IAstNode): IBlockExpressionNode;
begin begin
Result := TBlockExpressionNode.Create(AExpressions); Result := TBlockExpressionNode.Create(AExpressions, TTypes.Unknown);
end; end;
class function TAst.Constant(const AValue: TDataValue): IConstantNode; class function TAst.Constant(const AValue: TDataValue): IConstantNode;
begin begin
Result := TConstantNode.Create(AValue); Result := TConstantNode.Create(AValue, TTypes.Unknown);
end; end;
class function TAst.Constant(const AValue: String): IConstantNode; class function TAst.Constant(const AValue: String): IConstantNode;
begin begin
Result := TConstantNode.Create(AValue); Result := TConstantNode.Create(AValue, TTypes.Unknown);
end; end;
class constructor TAst.Create; class constructor TAst.Create;
@@ -575,6 +591,11 @@ begin
FLibraries.Free; FLibraries.Free;
end; end;
class function TAst.BoundIdentifier(AName: string; const Address: TResolvedAddress): IBoundIdentifierNode;
begin
Result := TBoundIdentifierNode.Create(AName, Address, TTypes.Unknown);
end;
class procedure TAst.RegisterLibrary(const AProc: TRegisterLibraryProc); class procedure TAst.RegisterLibrary(const AProc: TRegisterLibraryProc);
begin begin
FLibraries.Add(AProc); FLibraries.Add(AProc);
@@ -582,22 +603,22 @@ end;
class function TAst.FunctionCall(const ACallee: IAstNode; const AArguments: TArray<IAstNode>): IFunctionCallNode; class function TAst.FunctionCall(const ACallee: IAstNode; const AArguments: TArray<IAstNode>): IFunctionCallNode;
begin begin
Result := TFunctionCallNode.Create(ACallee, AArguments); Result := TFunctionCallNode.Create(ACallee, AArguments, TTypes.Unknown);
end; end;
class function TAst.Identifier(AName: string): IIdentifierNode; class function TAst.Identifier(AName: string): IIdentifierNode;
begin begin
Result := TIdentifierNode.Create(AName); Result := TIdentifierNode.Create(AName, TTypes.Unknown);
end; end;
class function TAst.IfExpr(const ACondition: IAstNode; const AThenBranch, AElseBranch: IAstNode): IIfExpressionNode; class function TAst.IfExpr(const ACondition: IAstNode; const AThenBranch, AElseBranch: IAstNode): IIfExpressionNode;
begin begin
Result := TIfExpressionNode.Create(ACondition, AThenBranch, AElseBranch); Result := TIfExpressionNode.Create(ACondition, AThenBranch, AElseBranch, TTypes.Unknown);
end; end;
class function TAst.Indexer(const ABase: IAstNode; const AIndex: IAstNode): IIndexerNode; class function TAst.Indexer(const ABase: IAstNode; const AIndex: IAstNode): IIndexerNode;
begin begin
Result := TIndexerNode.Create(ABase, AIndex); Result := TIndexerNode.Create(ABase, AIndex, TTypes.Unknown);
end; end;
class function TAst.Keyword(const AName: string): IKeywordNode; class function TAst.Keyword(const AName: string): IKeywordNode;
@@ -607,7 +628,7 @@ end;
class function TAst.LambdaExpr(const AParameters: TArray<IIdentifierNode>; const ABody: IAstNode): ILambdaExpressionNode; class function TAst.LambdaExpr(const AParameters: TArray<IIdentifierNode>; const ABody: IAstNode): ILambdaExpressionNode;
begin begin
Result := TLambdaExpressionNode.Create(AParameters, ABody); Result := TLambdaExpressionNode.Create(AParameters, ABody, TTypes.Unknown);
end; end;
class function TAst.MacroDef( class function TAst.MacroDef(
@@ -621,12 +642,12 @@ end;
class function TAst.MacroExpansionNode(const AOriginalCallNode: IFunctionCallNode; const AExpandedBody: IAstNode): IMacroExpansionNode; class function TAst.MacroExpansionNode(const AOriginalCallNode: IFunctionCallNode; const AExpandedBody: IAstNode): IMacroExpansionNode;
begin begin
Result := TMacroExpansionNode.Create(AOriginalCallNode, AExpandedBody); Result := TMacroExpansionNode.Create(AOriginalCallNode, AExpandedBody, TTypes.Unknown);
end; end;
class function TAst.MemberAccess(const ABase: IAstNode; const AMember: IKeywordNode): IMemberAccessNode; class function TAst.MemberAccess(const ABase: IAstNode; const AMember: IKeywordNode): IMemberAccessNode;
begin begin
Result := TMemberAccessNode.Create(ABase, AMember); Result := TMemberAccessNode.Create(ABase, AMember, TTypes.Unknown);
end; end;
class function TAst.Nop: IAstNode; class function TAst.Nop: IAstNode;
@@ -646,7 +667,7 @@ begin
SetLength(args, Length(AArguments)); SetLength(args, Length(AArguments));
for var i := 0 to High(AArguments) do for var i := 0 to High(AArguments) do
args[i] := AArguments[i]; args[i] := AArguments[i];
Result := TRecurNode.Create(args); Result := TRecurNode.Create(args, TTypes.Unknown);
end; end;
class function TAst.RecordLiteral(const AFields: TArray<TRecordFieldLiteral>): IRecordLiteralNode; class function TAst.RecordLiteral(const AFields: TArray<TRecordFieldLiteral>): IRecordLiteralNode;
@@ -654,17 +675,17 @@ begin
// By default, the parser creates the generic (most flexible) node type. // By default, the parser creates the generic (most flexible) node type.
// The TypeChecker (Phase 3) is responsible for "lowering" this to a // The TypeChecker (Phase 3) is responsible for "lowering" this to a
// TRecordLiteralNode if it determines all fields are scalar. // TRecordLiteralNode if it determines all fields are scalar.
Result := TGenericRecordLiteralNode.Create(AFields); Result := TGenericRecordLiteralNode.Create(AFields, TTypes.Unknown);
end; end;
class function TAst.TernaryExpr(const ACondition: IAstNode; const AThenBranch, AElseBranch: IAstNode): ITernaryExpressionNode; class function TAst.TernaryExpr(const ACondition: IAstNode; const AThenBranch, AElseBranch: IAstNode): ITernaryExpressionNode;
begin begin
Result := TTernaryExpressionNode.Create(ACondition, AThenBranch, AElseBranch); Result := TTernaryExpressionNode.Create(ACondition, AThenBranch, AElseBranch, TTypes.Unknown);
end; end;
class function TAst.UnaryExpr(const AOperator: TScalar.TUnaryOp; const ARight: IAstNode): IUnaryExpressionNode; class function TAst.UnaryExpr(const AOperator: TScalar.TUnaryOp; const ARight: IAstNode): IUnaryExpressionNode;
begin begin
Result := TUnaryExpressionNode.Create(AOperator, ARight); Result := TUnaryExpressionNode.Create(AOperator, ARight, TTypes.Unknown);
end; end;
class function TAst.Unquote(const AExpression: IAstNode): IUnquoteNode; class function TAst.Unquote(const AExpression: IAstNode): IUnquoteNode;
@@ -679,7 +700,7 @@ end;
class function TAst.VarDecl(const AIdentifier: IIdentifierNode; AInitializer: IAstNode = nil): IVariableDeclarationNode; class function TAst.VarDecl(const AIdentifier: IIdentifierNode; AInitializer: IAstNode = nil): IVariableDeclarationNode;
begin begin
Result := TVariableDeclarationNode.Create(AIdentifier, AInitializer); Result := TVariableDeclarationNode.Create(AIdentifier, AInitializer, TTypes.Unknown);
end; end;
{ TNopNode } { TNopNode }
@@ -687,8 +708,6 @@ end;
constructor TNopNode.Create; constructor TNopNode.Create;
begin begin
inherited Create; inherited Create;
// Nop nodes are 'Void' by default, though TypeChecker will reject them anyway.
StaticType := TTypes.Void;
end; end;
function TNopNode.Accept(const Visitor: IAstVisitor): TDataValue; function TNopNode.Accept(const Visitor: IAstVisitor): TDataValue;
@@ -711,7 +730,6 @@ end;
constructor TAstNode.Create; constructor TAstNode.Create;
begin begin
inherited Create; inherited Create;
FStaticType := TTypes.Unknown; // Default
end; end;
function TAstNode.AsAddSeriesItem: IAddSeriesItemNode; function TAstNode.AsAddSeriesItem: IAddSeriesItemNode;
@@ -825,6 +843,11 @@ begin
raise ETypeException.Create('Node is not a TernaryExpression'); raise ETypeException.Create('Node is not a TernaryExpression');
end; end;
function TAstNode.AsTypedNode: IAstTypedNode;
begin
raise ETypeException.Create('Node has no type');
end;
function TAstNode.AsUnaryExpression: IUnaryExpressionNode; function TAstNode.AsUnaryExpression: IUnaryExpressionNode;
begin begin
raise ETypeException.Create('Node is not an UnaryExpression'); raise ETypeException.Create('Node is not an UnaryExpression');
@@ -845,16 +868,30 @@ begin
raise ETypeException.Create('Node is not a VariableDeclaration'); raise ETypeException.Create('Node is not a VariableDeclaration');
end; end;
procedure TAstNode.SetStaticType(const AValue: IStaticType); { TAstTypedNode }
constructor TAstTypedNode.Create(const AStaticType: IStaticType);
begin begin
FStaticType := AValue; inherited Create;
FStaticType := AStaticType;
Assert(FStaticType <> nil);
end;
function TAstTypedNode.AsTypedNode: IAstTypedNode;
begin
Result := Self;
end;
function TAstTypedNode.GetStaticType: IStaticType;
begin
Result := FStaticType;
end; end;
{ TConstantNode } { TConstantNode }
constructor TConstantNode.Create(const AValue: TDataValue); constructor TConstantNode.Create(const AValue: TDataValue; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FValue := AValue; FValue := AValue;
end; end;
@@ -880,9 +917,9 @@ end;
{ TIdentifierNode } { TIdentifierNode }
constructor TIdentifierNode.Create(const AName: string); constructor TIdentifierNode.Create(const AName: string; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FName := AName; FName := AName;
end; end;
@@ -910,7 +947,7 @@ end;
constructor TKeywordNode.Create(const AValue: IKeyword); constructor TKeywordNode.Create(const AValue: IKeyword);
begin begin
inherited Create; inherited Create(TTypes.Keyword);
FValue := AValue; FValue := AValue;
end; end;
@@ -936,9 +973,14 @@ end;
{ TBinaryExpressionNode } { TBinaryExpressionNode }
constructor TBinaryExpressionNode.Create(const ALeft: IAstNode; AOperator: TScalar.TBinaryOp; const ARight: IAstNode); constructor TBinaryExpressionNode.Create(
const ALeft: IAstNode;
AOperator: TScalar.TBinaryOp;
const ARight: IAstNode;
const AStaticType: IStaticType
);
begin begin
inherited Create; inherited Create(AStaticType);
FLeft := ALeft; FLeft := ALeft;
FOperator := AOperator; FOperator := AOperator;
FRight := ARight; FRight := ARight;
@@ -976,9 +1018,9 @@ end;
{ TUnaryExpressionNode } { TUnaryExpressionNode }
constructor TUnaryExpressionNode.Create(const AOperator: TScalar.TUnaryOp; const ARight: IAstNode); constructor TUnaryExpressionNode.Create(const AOperator: TScalar.TUnaryOp; const ARight: IAstNode; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FOperator := AOperator; FOperator := AOperator;
FRight := ARight; FRight := ARight;
end; end;
@@ -1010,9 +1052,9 @@ end;
{ TIfExpressionNode } { TIfExpressionNode }
constructor TIfExpressionNode.Create(const ACondition, AThenBranch, AElseBranch: IAstNode); constructor TIfExpressionNode.Create(const ACondition, AThenBranch, AElseBranch: IAstNode; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FCondition := ACondition; FCondition := ACondition;
FThenBranch := AThenBranch; FThenBranch := AThenBranch;
FElseBranch := AElseBranch; FElseBranch := AElseBranch;
@@ -1050,9 +1092,9 @@ end;
{ TTernaryExpressionNode } { TTernaryExpressionNode }
constructor TTernaryExpressionNode.Create(const ACondition, AThenBranch, AElseBranch: IAstNode); constructor TTernaryExpressionNode.Create(const ACondition, AThenBranch, AElseBranch: IAstNode; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FCondition := ACondition; FCondition := ACondition;
FThenBranch := AThenBranch; FThenBranch := AThenBranch;
FElseBranch := AElseBranch; FElseBranch := AElseBranch;
@@ -1090,9 +1132,9 @@ end;
{ TLambdaExpressionNode } { TLambdaExpressionNode }
constructor TLambdaExpressionNode.Create(const AParameters: TArray<IIdentifierNode>; const ABody: IAstNode); constructor TLambdaExpressionNode.Create(const AParameters: TArray<IIdentifierNode>; const ABody: IAstNode; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FParameters := AParameters; FParameters := AParameters;
FBody := ABody; FBody := ABody;
end; end;
@@ -1254,9 +1296,9 @@ end;
{ TFunctionCallNode } { TFunctionCallNode }
constructor TFunctionCallNode.Create(const ACallee: IAstNode; const AArguments: TArray<IAstNode>); constructor TFunctionCallNode.Create(const ACallee: IAstNode; const AArguments: TArray<IAstNode>; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FCallee := ACallee; FCallee := ACallee;
FArguments := AArguments; FArguments := AArguments;
FIsTailCall := False; FIsTailCall := False;
@@ -1289,10 +1331,14 @@ end;
{ TMacroExpansionNode } { TMacroExpansionNode }
constructor TMacroExpansionNode.Create(const AOriginalCallNode: IFunctionCallNode; const AExpandedBody: IAstNode); constructor TMacroExpansionNode.Create(
const AOriginalCallNode: IFunctionCallNode;
const AExpandedBody: IAstNode;
const AStaticType: IStaticType
);
begin begin
// Copy properties from the original call node // Copy properties from the original call node
inherited Create(AOriginalCallNode.Callee, AOriginalCallNode.Arguments); inherited Create(AOriginalCallNode.Callee, AOriginalCallNode.Arguments, AStaticType);
FExpandedBody := AExpandedBody; FExpandedBody := AExpandedBody;
end; end;
@@ -1318,9 +1364,9 @@ end;
{ TRecurNode } { TRecurNode }
constructor TRecurNode.Create(const AArguments: TArray<IAstNode>); constructor TRecurNode.Create(const AArguments: TArray<IAstNode>; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FArguments := AArguments; FArguments := AArguments;
end; end;
@@ -1346,11 +1392,11 @@ end;
{ TBlockExpressionNode } { TBlockExpressionNode }
constructor TBlockExpressionNode.Create(const AExpressions: array of IAstNode); constructor TBlockExpressionNode.Create(const AExpressions: array of IAstNode; const AStaticType: IStaticType);
var var
i: Integer; i: Integer;
begin begin
inherited Create; inherited Create(AStaticType);
SetLength(FExpressions, Length(AExpressions)); SetLength(FExpressions, Length(AExpressions));
for i := 0 to High(AExpressions) do for i := 0 to High(AExpressions) do
FExpressions[i] := AExpressions[i]; FExpressions[i] := AExpressions[i];
@@ -1378,9 +1424,9 @@ end;
{ TVariableDeclarationNode } { TVariableDeclarationNode }
constructor TVariableDeclarationNode.Create(const AIdentifier: IIdentifierNode; AInitializer: IAstNode); constructor TVariableDeclarationNode.Create(const AIdentifier: IIdentifierNode; AInitializer: IAstNode; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FIdentifier := AIdentifier; FIdentifier := AIdentifier;
FInitializer := AInitializer; FInitializer := AInitializer;
FIsBoxed := False; FIsBoxed := False;
@@ -1413,9 +1459,9 @@ end;
{ TAssignmentNode } { TAssignmentNode }
constructor TAssignmentNode.Create(const AIdentifier: IIdentifierNode; const AValue: IAstNode); constructor TAssignmentNode.Create(const AIdentifier: IIdentifierNode; const AValue: IAstNode; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FIdentifier := AIdentifier; FIdentifier := AIdentifier;
FValue := AValue; FValue := AValue;
end; end;
@@ -1447,9 +1493,9 @@ end;
{ TIndexerNode } { TIndexerNode }
constructor TIndexerNode.Create(const ABase: IAstNode; const AIndex: IAstNode); constructor TIndexerNode.Create(const ABase: IAstNode; const AIndex: IAstNode; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FBase := ABase; FBase := ABase;
FIndex := AIndex; FIndex := AIndex;
end; end;
@@ -1481,9 +1527,9 @@ end;
{ TMemberAccessNode } { TMemberAccessNode }
constructor TMemberAccessNode.Create(const ABase: IAstNode; const AMember: IKeywordNode); constructor TMemberAccessNode.Create(const ABase: IAstNode; const AMember: IKeywordNode; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FBase := ABase; FBase := ABase;
FMember := AMember; FMember := AMember;
end; end;
@@ -1515,9 +1561,9 @@ end;
{ TRecordLiteralNode } { TRecordLiteralNode }
constructor TRecordLiteralNode.Create(const AFields: TArray<TRecordFieldLiteral>); constructor TRecordLiteralNode.Create(const AFields: TArray<TRecordFieldLiteral>; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FFields := AFields; FFields := AFields;
end; end;
@@ -1543,16 +1589,16 @@ end;
{ TGenericRecordLiteralNode } { TGenericRecordLiteralNode }
constructor TGenericRecordLiteralNode.Create(const AFields: TArray<TRecordFieldLiteral>); constructor TGenericRecordLiteralNode.Create(const AFields: TArray<TRecordFieldLiteral>; const AStaticType: IStaticType);
begin begin
inherited Create(AFields); inherited Create(AFields, AStaticType);
end; end;
{ TCreateSeriesNode } { TCreateSeriesNode }
constructor TCreateSeriesNode.Create(const ADefinition: String); constructor TCreateSeriesNode.Create(const ADefinition: String; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FDefinition := ADefinition; FDefinition := ADefinition;
end; end;
@@ -1578,9 +1624,14 @@ end;
{ TAddSeriesItemNode } { TAddSeriesItemNode }
constructor TAddSeriesItemNode.Create(const ASeries: IIdentifierNode; const AValue: IAstNode; const ALookback: IAstNode); constructor TAddSeriesItemNode.Create(
const ASeries: IIdentifierNode;
const AValue: IAstNode;
const ALookback: IAstNode;
const AStaticType: IStaticType
);
begin begin
inherited Create; inherited Create(AStaticType);
FSeries := ASeries; FSeries := ASeries;
FValue := AValue; FValue := AValue;
FLookback := ALookback; FLookback := ALookback;
@@ -1618,9 +1669,9 @@ end;
{ TSeriesLengthNode } { TSeriesLengthNode }
constructor TSeriesLengthNode.Create(const ASeries: IIdentifierNode); constructor TSeriesLengthNode.Create(const ASeries: IIdentifierNode; const AStaticType: IStaticType);
begin begin
inherited Create; inherited Create(AStaticType);
FSeries := ASeries; FSeries := ASeries;
end; end;
@@ -1646,9 +1697,9 @@ end;
{ TBoundIdentifierNode } { TBoundIdentifierNode }
constructor TBoundIdentifierNode.Create(const AName: string; const AAddress: TResolvedAddress); constructor TBoundIdentifierNode.Create(const AName: string; const AAddress: TResolvedAddress; const AStaticType: IStaticType);
begin begin
inherited Create(AName); inherited Create(AName, AStaticType);
FAddress := AAddress; FAddress := AAddress;
end; end;