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