Pipes and test scripts
This commit is contained in:
@@ -60,19 +60,21 @@ type
|
||||
function VisitFunctionCall(const Node: IFunctionCallNode): IAstNode; override;
|
||||
function VisitBlockExpression(const Node: IBlockExpressionNode): IAstNode; override;
|
||||
function VisitIfExpression(const Node: IIfExpressionNode): IAstNode; override;
|
||||
function VisitCondExpression(const Node: ICondExpressionNode): IAstNode; override;
|
||||
function VisitMemberAccess(const Node: IMemberAccessNode): IAstNode; override;
|
||||
function VisitIndexer(const Node: IIndexerNode): IAstNode; override;
|
||||
function VisitRecordLiteral(const Node: IRecordLiteralNode): IAstNode; override;
|
||||
function VisitCreateSeries(const Node: ICreateSeriesNode): IAstNode; override;
|
||||
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): IAstNode; override;
|
||||
function VisitSeriesLength(const Node: ISeriesLengthNode): IAstNode; override;
|
||||
function VisitRecurNode(const Node: IRecurNode): IAstNode; override;
|
||||
function VisitNop(const Node: INopNode): IAstNode; override;
|
||||
function VisitRecordLiteral(const Node: IRecordLiteralNode): IAstNode; override;
|
||||
|
||||
function VisitConstant(const Node: IConstantNode): IAstNode; override;
|
||||
function VisitKeyword(const Node: IKeywordNode): IAstNode; override;
|
||||
|
||||
// Pipe Support
|
||||
function VisitPipeInput(const Node: IPipeInputNode): IAstNode; override;
|
||||
function VisitPipe(const Node: IPipeNode): IAstNode; override;
|
||||
|
||||
public
|
||||
constructor Create(const RootLayout: IScopeLayout; const RootScope: IExecutionScope; const ALog: ICompilerLog);
|
||||
destructor Destroy; override;
|
||||
@@ -183,8 +185,6 @@ begin
|
||||
p := CreateContextChain(L.Parent);
|
||||
|
||||
desc := nil;
|
||||
// Check if this layout is the root layout (has no parent).
|
||||
// If so, attach the RootScope's descriptor to pull in RTL types (like +).
|
||||
if (L.Parent = nil) and Assigned(FRootScope) then
|
||||
begin
|
||||
desc := FRootScope.Descriptor;
|
||||
@@ -224,10 +224,6 @@ class function TTypeChecker.CheckTypes(
|
||||
var
|
||||
startLayout: IScopeLayout;
|
||||
begin
|
||||
// Determine the starting scope for the TypeChecker.
|
||||
// The 'Layout' parameter represents the *inner* scope of the script (the "Room").
|
||||
// To correctly simulate entering this scope (via VisitLambdaExpression), the TypeChecker
|
||||
// must start in the *surrounding* scope (the "Hallway"), which is Layout.Parent.
|
||||
if Assigned(Layout) then
|
||||
startLayout := Layout.Parent
|
||||
else
|
||||
@@ -312,14 +308,9 @@ var
|
||||
initType: IStaticType;
|
||||
newInitializer, newIdent: IAstNode;
|
||||
adr: TResolvedAddress;
|
||||
lambdaNode: ILambdaExpressionNode;
|
||||
placeholderType: IStaticType;
|
||||
i: Integer;
|
||||
identNode: IIdentifierNode;
|
||||
identIdentity: INamedIdentity;
|
||||
begin
|
||||
identNode := Node.Target.AsIdentifier;
|
||||
identIdentity := identNode.Identity.AsNamed;
|
||||
adr := identNode.Address;
|
||||
initType := TTypes.Unknown;
|
||||
|
||||
@@ -331,35 +322,18 @@ begin
|
||||
Exit;
|
||||
end;
|
||||
|
||||
placeholderType := nil;
|
||||
if (Node.Initializer <> nil) and (Node.Initializer.Kind = akLambdaExpression) then
|
||||
begin
|
||||
lambdaNode := Node.Initializer.AsLambdaExpression;
|
||||
var paramTypes: TArray<IStaticType>;
|
||||
SetLength(paramTypes, lambdaNode.Parameters.Count);
|
||||
for i := 0 to High(paramTypes) do
|
||||
paramTypes[i] := TTypes.Unknown;
|
||||
|
||||
placeholderType := TTypes.CreateMethod(paramTypes, TTypes.Unknown);
|
||||
FCurrentContext.SetType(adr.SlotIndex, placeholderType);
|
||||
initType := placeholderType;
|
||||
end;
|
||||
|
||||
if Assigned(Node.Initializer) then
|
||||
newInitializer := Accept(Node.Initializer)
|
||||
else
|
||||
newInitializer := nil;
|
||||
|
||||
if Assigned(newInitializer) then
|
||||
initType := newInitializer.AsTypedNode.StaticType
|
||||
else if not Assigned(placeholderType) then
|
||||
initType := TTypes.Unknown;
|
||||
initType := newInitializer.AsTypedNode.StaticType;
|
||||
|
||||
if initType.Kind <> stUnknown then
|
||||
FCurrentContext.SetType(adr.SlotIndex, initType);
|
||||
|
||||
newIdent := TAst.Identifier(identIdentity, adr, initType);
|
||||
|
||||
newIdent := TAst.Identifier(identNode.Identity.AsNamed, adr, initType);
|
||||
Result := TAst.VarDecl(Node.Identity, newIdent, newInitializer, initType, Node.IsBoxed);
|
||||
end;
|
||||
|
||||
@@ -368,15 +342,9 @@ var
|
||||
targetType, sourceType: IStaticType;
|
||||
newIdent, newValue: IAstNode;
|
||||
adr: TResolvedAddress;
|
||||
lambdaNode: ILambdaExpressionNode;
|
||||
placeholderType: IStaticType;
|
||||
i: Integer;
|
||||
identNode: IIdentifierNode;
|
||||
identIdentity: INamedIdentity;
|
||||
begin
|
||||
identNode := Node.Target.AsIdentifier;
|
||||
identIdentity := identNode.Identity.AsNamed;
|
||||
|
||||
newIdent := Accept(Node.Target);
|
||||
targetType := newIdent.AsTypedNode.StaticType;
|
||||
adr := identNode.Address;
|
||||
@@ -388,23 +356,6 @@ begin
|
||||
Exit;
|
||||
end;
|
||||
|
||||
placeholderType := nil;
|
||||
if (Node.Value <> nil) and (Node.Value.Kind = akLambdaExpression) then
|
||||
begin
|
||||
lambdaNode := Node.Value.AsLambdaExpression;
|
||||
if (targetType.Kind <> stMethod) then
|
||||
begin
|
||||
var paramTypes: TArray<IStaticType>;
|
||||
SetLength(paramTypes, lambdaNode.Parameters.Count);
|
||||
for i := 0 to High(paramTypes) do
|
||||
paramTypes[i] := TTypes.Unknown;
|
||||
|
||||
placeholderType := TTypes.CreateMethod(paramTypes, TTypes.Unknown);
|
||||
FCurrentContext.SetType(adr.SlotIndex, placeholderType);
|
||||
targetType := placeholderType;
|
||||
end;
|
||||
end;
|
||||
|
||||
newValue := Accept(Node.Value);
|
||||
sourceType := newValue.AsTypedNode.StaticType;
|
||||
|
||||
@@ -417,16 +368,35 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
if ((targetType.Kind = stUnknown) or Assigned(placeholderType)) and (sourceType.Kind <> stUnknown) then
|
||||
if (targetType.Kind = stUnknown) and (sourceType.Kind <> stUnknown) then
|
||||
begin
|
||||
FCurrentContext.SetType(adr.SlotIndex, sourceType);
|
||||
newIdent := TAst.Identifier(identIdentity, adr, sourceType);
|
||||
newIdent := TAst.Identifier(identNode.Identity.AsNamed, adr, sourceType);
|
||||
targetType := sourceType;
|
||||
end;
|
||||
|
||||
Result := TAst.Assign(Node.Identity, newIdent, newValue, targetType);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitBlockExpression(const Node: IBlockExpressionNode): IAstNode;
|
||||
var
|
||||
blockType: IStaticType;
|
||||
newExprs: TArray<IAstNode>;
|
||||
i: Integer;
|
||||
begin
|
||||
SetLength(newExprs, Node.Expressions.Count);
|
||||
for i := 0 to Node.Expressions.Count - 1 do
|
||||
newExprs[i] := Accept(Node.Expressions[i]);
|
||||
|
||||
if Length(newExprs) > 0 then
|
||||
blockType := newExprs[High(newExprs)].AsTypedNode.StaticType
|
||||
else
|
||||
blockType := TTypes.Void;
|
||||
|
||||
var exprList := TExpressionList.Create(newExprs, Node.Expressions.Identity);
|
||||
Result := TAst.Block(Node.Identity, exprList, blockType);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitLambdaExpression(const Node: ILambdaExpressionNode): IAstNode;
|
||||
var
|
||||
newParams: TArray<IIdentifierNode>;
|
||||
@@ -434,72 +404,57 @@ var
|
||||
bodyType, methodType: IStaticType;
|
||||
paramTypes: TArray<IStaticType>;
|
||||
upvalueTypes: TArray<IStaticType>;
|
||||
upvalueAddrs: TArray<TResolvedAddress>;
|
||||
i: Integer;
|
||||
finalDescriptor: IScopeDescriptor;
|
||||
paramIdent: IIdentifierNode;
|
||||
paramIdentity: INamedIdentity;
|
||||
lookupAddr: TResolvedAddress;
|
||||
injectedType: IStaticType;
|
||||
begin
|
||||
// 1. Resolve Upvalue Types
|
||||
upvalueAddrs := Node.Upvalues;
|
||||
var upvalueAddrs := Node.Upvalues;
|
||||
SetLength(upvalueTypes, Length(upvalueAddrs));
|
||||
|
||||
for i := 0 to High(upvalueAddrs) do
|
||||
begin
|
||||
lookupAddr := upvalueAddrs[i];
|
||||
|
||||
// CRITICAL: Scope Depth Adjustment
|
||||
// The Binder calculated addresses relative to the *body* of this lambda (Internal View).
|
||||
// FCurrentContext currently points to the *defining* scope (External View/Parent).
|
||||
// Therefore, we must decrease the depth by 1 to look up the type in the current context.
|
||||
var lookupAddr := upvalueAddrs[i];
|
||||
if lookupAddr.Kind = akLocalOrParent then
|
||||
begin
|
||||
// Check > 0 to prevent crash if Binder somehow produced 0 (though logic says it shouldn't).
|
||||
if lookupAddr.ScopeDepth > 0 then
|
||||
Dec(lookupAddr.ScopeDepth)
|
||||
else
|
||||
begin
|
||||
// Guard for Binder quirks or edge cases
|
||||
if Assigned(FLog) then
|
||||
FLog.AddWarning('Compiler integrity check warning: Upvalue Depth 0 encountered during type check.', Node);
|
||||
end;
|
||||
Dec(lookupAddr.ScopeDepth);
|
||||
end;
|
||||
|
||||
upvalueTypes[i] := FCurrentContext.LookupType(lookupAddr);
|
||||
end;
|
||||
|
||||
// 2. Enter New Scope (The "Room")
|
||||
// 2. Enter New Scope
|
||||
FCurrentContext := TTypeContext.Create(FCurrentContext, Node.Layout, upvalueTypes, nil);
|
||||
try
|
||||
// 3. Register Parameters
|
||||
SetLength(newParams, Node.Parameters.Count);
|
||||
SetLength(paramTypes, Node.Parameters.Count);
|
||||
|
||||
for i := 0 to Node.Parameters.Count - 1 do
|
||||
begin
|
||||
paramIdent := Node.Parameters[i];
|
||||
paramIdentity := paramIdent.Identity.AsNamed;
|
||||
var paramAdr := paramIdent.Address;
|
||||
|
||||
paramTypes[i] := TTypes.Unknown;
|
||||
// Check if there is already a type assigned (e.g. injected by Pipe Visitor)
|
||||
injectedType := paramIdent.AsTypedNode.StaticType;
|
||||
|
||||
if paramAdr.Kind <> akUnresolved then
|
||||
FCurrentContext.SetType(paramAdr.SlotIndex, paramTypes[i]);
|
||||
if injectedType.Kind = stUnknown then
|
||||
paramTypes[i] := TTypes.Unknown
|
||||
else
|
||||
paramTypes[i] := injectedType;
|
||||
|
||||
newParams[i] := TAst.Identifier(paramIdentity, paramAdr, paramTypes[i]);
|
||||
if paramIdent.Address.Kind <> akUnresolved then
|
||||
FCurrentContext.SetType(paramIdent.Address.SlotIndex, paramTypes[i]);
|
||||
|
||||
newParams[i] := TAst.Identifier(paramIdent.Identity.AsNamed, paramIdent.Address, paramTypes[i]);
|
||||
end;
|
||||
|
||||
// 4. Visit Body
|
||||
newBody := Accept(Node.Body);
|
||||
|
||||
// 5. Determine Function Signature
|
||||
bodyType := newBody.AsTypedNode.StaticType;
|
||||
methodType := TTypes.CreateMethod(paramTypes, bodyType);
|
||||
|
||||
finalDescriptor := TScope.CreateDescriptor(Node.Layout, FCurrentContext.Types);
|
||||
finally
|
||||
// 6. Leave Scope
|
||||
var temp := FCurrentContext;
|
||||
FCurrentContext := FCurrentContext.FParent;
|
||||
temp.Free;
|
||||
@@ -529,15 +484,13 @@ var
|
||||
argTypes: TArray<IStaticType>;
|
||||
hasUnknownArgs: Boolean;
|
||||
bestSig: IMethodSignature;
|
||||
sig: IMethodSignature;
|
||||
match: Boolean;
|
||||
argsStr: string;
|
||||
begin
|
||||
newCallee := Accept(Node.Callee);
|
||||
SetLength(newArgs, Node.Arguments.Count);
|
||||
SetLength(argTypes, Node.Arguments.Count);
|
||||
|
||||
hasUnknownArgs := False;
|
||||
|
||||
for i := 0 to Node.Arguments.Count - 1 do
|
||||
begin
|
||||
newArgs[i] := Accept(Node.Arguments[i]);
|
||||
@@ -549,26 +502,22 @@ begin
|
||||
calleeType := newCallee.AsTypedNode.StaticType;
|
||||
retType := TTypes.Unknown;
|
||||
|
||||
if calleeType.Kind = TStaticTypeKind.stMethod then
|
||||
if calleeType.Kind = stMethod then
|
||||
begin
|
||||
if not hasUnknownArgs then
|
||||
begin
|
||||
bestSig := nil;
|
||||
for sig in calleeType.Signatures do
|
||||
for var sig in calleeType.Signatures do
|
||||
begin
|
||||
if Length(sig.ParamTypes) <> Length(argTypes) then
|
||||
continue;
|
||||
|
||||
match := True;
|
||||
for j := 0 to High(argTypes) do
|
||||
begin
|
||||
if not TTypeRules.CanAssign(sig.ParamTypes[j], argTypes[j]) then
|
||||
begin
|
||||
match := False;
|
||||
break;
|
||||
end;
|
||||
end;
|
||||
|
||||
if match then
|
||||
begin
|
||||
bestSig := sig;
|
||||
@@ -578,389 +527,179 @@ begin
|
||||
|
||||
if Assigned(bestSig) then
|
||||
retType := bestSig.ReturnType
|
||||
else
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
begin
|
||||
argsStr := '';
|
||||
for i := 0 to High(argTypes) do
|
||||
argsStr := argsStr + argTypes[i].ToString + ' ';
|
||||
FLog.AddError(
|
||||
Format('No matching signature for call with args (%s) found on method %s', [argsStr, calleeType.ToString]),
|
||||
Node
|
||||
);
|
||||
end;
|
||||
retType := TTypes.Unknown;
|
||||
end;
|
||||
else if Assigned(FLog) then
|
||||
FLog.AddError(Format('No matching signature found for method call on %s', [calleeType.ToString]), Node);
|
||||
end;
|
||||
end
|
||||
else if calleeType.Kind <> TStaticTypeKind.stUnknown then
|
||||
begin
|
||||
else if calleeType.Kind <> stUnknown then
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Cannot invoke type %s as a function.', [calleeType.ToString]), Node);
|
||||
retType := TTypes.Unknown;
|
||||
end;
|
||||
|
||||
var argList := TArgumentList.Create(newArgs, Node.Arguments.Identity);
|
||||
Result := TAst.FunctionCall(Node.Identity, newCallee, argList, retType, Node.IsTailCall, nil, False);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitBlockExpression(const Node: IBlockExpressionNode): IAstNode;
|
||||
var
|
||||
blockType: IStaticType;
|
||||
newExprs: TArray<IAstNode>;
|
||||
i: Integer;
|
||||
begin
|
||||
SetLength(newExprs, Node.Expressions.Count);
|
||||
for i := 0 to Node.Expressions.Count - 1 do
|
||||
newExprs[i] := Accept(Node.Expressions[i]);
|
||||
|
||||
if Length(newExprs) > 0 then
|
||||
blockType := newExprs[High(newExprs)].AsTypedNode.StaticType
|
||||
else
|
||||
blockType := TTypes.Void;
|
||||
|
||||
var exprList := TExpressionList.Create(newExprs, Node.Expressions.Identity);
|
||||
Result := TAst.Block(Node.Identity, exprList, blockType);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitIfExpression(const Node: IIfExpressionNode): IAstNode;
|
||||
var
|
||||
conditionType, thenType, elseType, resultType: IStaticType;
|
||||
newCond, newThen, newElse: IAstNode;
|
||||
resType: IStaticType;
|
||||
begin
|
||||
newCond := Accept(Node.Condition);
|
||||
newThen := Accept(Node.ThenBranch);
|
||||
newElse := Accept(Node.ElseBranch);
|
||||
|
||||
conditionType := newCond.AsTypedNode.StaticType;
|
||||
if (conditionType.Kind <> stUnknown)
|
||||
and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType)
|
||||
and not TTypeRules.CanAssign(TTypes.Boolean, conditionType) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('If condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]), Node);
|
||||
end;
|
||||
|
||||
thenType := newThen.AsTypedNode.StaticType;
|
||||
elseType :=
|
||||
if newElse <> nil then newElse.AsTypedNode.StaticType
|
||||
else TTypes.Void;
|
||||
|
||||
resultType := TTypeRules.Promote(thenType, elseType);
|
||||
if resultType = nil then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Cannot promote types %s and %s in If-Expression', [thenType.ToString, elseType.ToString]), Node);
|
||||
resultType := TTypes.Unknown;
|
||||
end;
|
||||
|
||||
Result := TAst.IfExpr(Node.Identity, newCond, newThen, newElse, resultType);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitCondExpression(const Node: ICondExpressionNode): IAstNode;
|
||||
var
|
||||
i: Integer;
|
||||
newPairs: TArray<TCondPair>;
|
||||
newElse: IAstNode;
|
||||
condType, branchType, resultType: IStaticType;
|
||||
begin
|
||||
SetLength(newPairs, Length(Node.Pairs));
|
||||
resultType := nil;
|
||||
|
||||
for i := 0 to High(Node.Pairs) do
|
||||
begin
|
||||
var newCond := Accept(Node.Pairs[i].Condition);
|
||||
condType := newCond.AsTypedNode.StaticType;
|
||||
|
||||
if (condType.Kind <> stUnknown)
|
||||
and not TTypeRules.CanAssign(TTypes.Ordinal, condType)
|
||||
and not TTypeRules.CanAssign(TTypes.Boolean, condType) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Cond condition must be Boolean or Ordinal, but got %s', [condType.ToString]), Node);
|
||||
end;
|
||||
|
||||
var newBranch := Accept(Node.Pairs[i].Branch);
|
||||
branchType := newBranch.AsTypedNode.StaticType;
|
||||
|
||||
newPairs[i] := TCondPair.Create(newCond, newBranch);
|
||||
|
||||
if resultType = nil then
|
||||
resultType := branchType
|
||||
else
|
||||
begin
|
||||
var promoted := TTypeRules.Promote(resultType, branchType);
|
||||
if promoted = nil then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(
|
||||
Format('Incompatible types in Cond branches: %s and %s', [resultType.ToString, branchType.ToString]),
|
||||
Node
|
||||
);
|
||||
resultType := TTypes.Unknown;
|
||||
end
|
||||
else
|
||||
resultType := promoted;
|
||||
end;
|
||||
end;
|
||||
|
||||
newElse := Accept(Node.ElseBranch);
|
||||
var elseType := newElse.AsTypedNode.StaticType;
|
||||
|
||||
if resultType = nil then
|
||||
resultType := elseType
|
||||
// Simple promotion logic
|
||||
if (newElse <> nil) then
|
||||
resType := TTypeRules.Promote(newThen.AsTypedNode.StaticType, newElse.AsTypedNode.StaticType)
|
||||
else
|
||||
begin
|
||||
var promoted := TTypeRules.Promote(resultType, elseType);
|
||||
if promoted = nil then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(
|
||||
Format('Incompatible types in Cond branches (Else): %s and %s', [resultType.ToString, elseType.ToString]),
|
||||
Node
|
||||
);
|
||||
resultType := TTypes.Unknown;
|
||||
end
|
||||
else
|
||||
resultType := promoted;
|
||||
end;
|
||||
resType := TTypes.MakeOptional(newThen.AsTypedNode.StaticType);
|
||||
|
||||
Result := TAst.CondExpr(Node.Identity, newPairs, newElse, resultType);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitMemberAccess(const Node: IMemberAccessNode): IAstNode;
|
||||
var
|
||||
unwrappedBaseType, elemType: IStaticType;
|
||||
fieldIndex: Integer;
|
||||
newBase, newMember: IAstNode;
|
||||
isOptionalAccess: Boolean;
|
||||
begin
|
||||
newBase := Accept(Node.Base);
|
||||
newMember := Accept(Node.Member);
|
||||
|
||||
unwrappedBaseType := PrepareBaseType(newBase, isOptionalAccess);
|
||||
elemType := TTypes.Unknown;
|
||||
|
||||
if (unwrappedBaseType.Kind <> TStaticTypeKind.stUnknown) then
|
||||
begin
|
||||
if (unwrappedBaseType.Kind = TStaticTypeKind.stRecord) or (unwrappedBaseType.Kind = TStaticTypeKind.stRecordSeries) then
|
||||
begin
|
||||
fieldIndex := unwrappedBaseType.Definition.IndexOf(Node.Member.Value);
|
||||
if fieldIndex < 0 then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Member "%s" not found in type %s', [Node.Member.Value.Name, unwrappedBaseType.ToString]), Node);
|
||||
end
|
||||
else
|
||||
begin
|
||||
var fieldType := TTypes.FromScalarKind(unwrappedBaseType.Definition[fieldIndex].Value);
|
||||
if unwrappedBaseType.Kind = TStaticTypeKind.stRecord then
|
||||
elemType := fieldType
|
||||
else
|
||||
elemType := TTypes.CreateSeries(fieldType);
|
||||
end;
|
||||
end
|
||||
else if (unwrappedBaseType.Kind = TStaticTypeKind.stGenericRecord) then
|
||||
begin
|
||||
var genDef := unwrappedBaseType.GenericDefinition;
|
||||
fieldIndex := genDef.IndexOf(Node.Member.Value);
|
||||
if fieldIndex < 0 then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Member "%s" not found in type %s', [Node.Member.Value.Name, unwrappedBaseType.ToString]), Node);
|
||||
end
|
||||
else
|
||||
elemType := genDef[fieldIndex].Value;
|
||||
end
|
||||
else
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Member access requires a record type, but got %s', [unwrappedBaseType.ToString]), Node);
|
||||
end;
|
||||
end;
|
||||
|
||||
elemType := ApplyOptionality(elemType, isOptionalAccess);
|
||||
|
||||
Result := TAst.MemberAccess(Node.Identity, newBase, newMember.AsKeyword, elemType);
|
||||
Result := TAst.IfExpr(Node.Identity, newCond, newThen, newElse, resType);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitIndexer(const Node: IIndexerNode): IAstNode;
|
||||
var
|
||||
unwrappedBaseType, indexType, elemType: IStaticType;
|
||||
newBase, newIndex: IAstNode;
|
||||
isOptionalAccess: Boolean;
|
||||
baseType, elemType: IStaticType;
|
||||
isOpt: Boolean;
|
||||
begin
|
||||
newBase := Accept(Node.Base);
|
||||
newIndex := Accept(Node.Index);
|
||||
baseType := PrepareBaseType(newBase, isOpt);
|
||||
|
||||
unwrappedBaseType := PrepareBaseType(newBase, isOptionalAccess);
|
||||
|
||||
indexType := newIndex.AsTypedNode.StaticType;
|
||||
elemType := TTypes.Unknown;
|
||||
if baseType.Kind = stSeries then
|
||||
elemType := baseType.ElementType
|
||||
else if baseType.Kind = stRecordSeries then
|
||||
elemType := TTypes.CreateRecord(baseType.Definition);
|
||||
|
||||
if (unwrappedBaseType.Kind <> TStaticTypeKind.stUnknown) then
|
||||
begin
|
||||
if (unwrappedBaseType.Kind <> TStaticTypeKind.stSeries) and (unwrappedBaseType.Kind <> TStaticTypeKind.stRecordSeries) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Indexer `[]` can only be applied to series types, but got %s', [unwrappedBaseType.ToString]), Node);
|
||||
end
|
||||
else
|
||||
begin
|
||||
if unwrappedBaseType.Kind = TStaticTypeKind.stSeries then
|
||||
elemType := unwrappedBaseType.ElementType
|
||||
else
|
||||
elemType := TTypes.CreateRecord(unwrappedBaseType.Definition);
|
||||
end;
|
||||
end;
|
||||
|
||||
if (indexType.Kind <> stUnknown) and not TTypeRules.CanAssign(TTypes.Ordinal, indexType) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Indexer `[]` requires an Ordinal index, but got %s', [indexType.ToString]), Node);
|
||||
end;
|
||||
|
||||
elemType := ApplyOptionality(elemType, isOptionalAccess);
|
||||
|
||||
elemType := ApplyOptionality(elemType, isOpt);
|
||||
Result := TAst.Indexer(Node.Identity, newBase, newIndex, elemType);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitMemberAccess(const Node: IMemberAccessNode): IAstNode;
|
||||
var
|
||||
newBase: IAstNode;
|
||||
baseType, resType: IStaticType;
|
||||
idx: Integer;
|
||||
isOpt: Boolean;
|
||||
begin
|
||||
newBase := Accept(Node.Base);
|
||||
baseType := PrepareBaseType(newBase, isOpt);
|
||||
resType := TTypes.Unknown;
|
||||
|
||||
if (baseType.Kind = stRecord) or (baseType.Kind = stRecordSeries) then
|
||||
begin
|
||||
idx := baseType.Definition.IndexOf(Node.Member.Value);
|
||||
if idx >= 0 then
|
||||
begin
|
||||
var fieldType := TTypes.FromScalarKind(baseType.Definition.Items[idx].Value);
|
||||
if baseType.Kind = stRecordSeries then
|
||||
resType := TTypes.CreateSeries(fieldType)
|
||||
else
|
||||
resType := fieldType;
|
||||
end
|
||||
else if Assigned(FLog) then
|
||||
FLog.AddError('Member not found', Node);
|
||||
end;
|
||||
|
||||
resType := ApplyOptionality(resType, isOpt);
|
||||
Result := TAst.MemberAccess(Node.Identity, newBase, Node.Member, resType);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitRecordLiteral(const Node: IRecordLiteralNode): IAstNode;
|
||||
var
|
||||
i: Integer;
|
||||
scalarDefFields: TArray<TScalarRecordField>;
|
||||
def: IScalarRecordDefinition;
|
||||
staticType: IStaticType;
|
||||
valType: IStaticType;
|
||||
scalarKind: TScalar.TKind;
|
||||
allScalar: Boolean;
|
||||
newFields: TArray<IRecordFieldNode>;
|
||||
fieldTypes: TArray<TPair<IKeyword, IStaticType>>;
|
||||
scalarFieldTypes: TArray<TPair<IKeyword, TScalar.TKind>>;
|
||||
isScalar: Boolean;
|
||||
valType: IStaticType;
|
||||
key: IKeyword;
|
||||
visitedValue: IAstNode;
|
||||
newFieldList: IRecordFieldList;
|
||||
begin
|
||||
SetLength(newFields, Node.Fields.Count);
|
||||
SetLength(fieldTypes, Node.Fields.Count);
|
||||
SetLength(scalarFieldTypes, Node.Fields.Count);
|
||||
isScalar := True;
|
||||
|
||||
for i := 0 to Node.Fields.Count - 1 do
|
||||
begin
|
||||
var field := Node.Fields[i];
|
||||
var newKey := Accept(field.Key).AsKeyword;
|
||||
var newValue := Accept(field.Value);
|
||||
var oldField := Node.Fields[i];
|
||||
visitedValue := Accept(oldField.Value);
|
||||
|
||||
if (newKey = field.Key) and (newValue = field.Value) then
|
||||
newFields[i] := field
|
||||
else
|
||||
newFields[i] := TAst.RecordField(field.Identity, newKey, newValue);
|
||||
end;
|
||||
// Keys are guaranteed to be keywords by parser
|
||||
key := oldField.Key.Value;
|
||||
|
||||
SetLength(scalarDefFields, Length(newFields));
|
||||
allScalar := True;
|
||||
// Recreate the field node with the typed value
|
||||
newFields[i] := TAst.RecordField(oldField.Identity, oldField.Key, visitedValue);
|
||||
|
||||
for i := 0 to High(newFields) do
|
||||
begin
|
||||
valType := newFields[i].Value.AsTypedNode.StaticType;
|
||||
// Analyze Type
|
||||
valType := visitedValue.AsTypedNode.StaticType;
|
||||
|
||||
if (valType.Kind = stOrdinal) then
|
||||
scalarKind := TScalar.TKind.Ordinal
|
||||
else if (valType.Kind = stFloat) then
|
||||
scalarKind := TScalar.TKind.Float
|
||||
else if (valType.Kind = stKeyword) then
|
||||
scalarKind := TScalar.TKind.Keyword
|
||||
else if (valType.Kind = stBoolean) then
|
||||
scalarKind := TScalar.TKind.Boolean
|
||||
else if (valType.Kind = stDateTime) then
|
||||
scalarKind := TScalar.TKind.DateTime
|
||||
// Check if strict scalar (no optionals allowed in packed ScalarRecord)
|
||||
if (valType.Kind in [stOrdinal, stFloat, stBoolean, stDateTime, stKeyword]) and (not valType.IsOptional) then
|
||||
begin
|
||||
var kind: TScalar.TKind;
|
||||
// Map StaticType Kind to Scalar Kind
|
||||
case valType.Kind of
|
||||
stOrdinal: kind := TScalar.TKind.Ordinal;
|
||||
stFloat: kind := TScalar.TKind.Float;
|
||||
stBoolean: kind := TScalar.TKind.Boolean;
|
||||
stDateTime: kind := TScalar.TKind.DateTime;
|
||||
stKeyword: kind := TScalar.TKind.Keyword;
|
||||
else
|
||||
kind := TScalar.TKind.Ordinal; // Should not happen given check above
|
||||
end;
|
||||
scalarFieldTypes[i] := TPair<IKeyword, TScalar.TKind>.Create(key, kind);
|
||||
end
|
||||
else
|
||||
begin
|
||||
allScalar := False;
|
||||
scalarKind := TScalar.TKind.Ordinal;
|
||||
isScalar := False;
|
||||
end;
|
||||
|
||||
if allScalar then
|
||||
scalarDefFields[i] := TScalarRecordField.Create(newFields[i].Key.Value, scalarKind);
|
||||
fieldTypes[i] := TPair<IKeyword, IStaticType>.Create(key, valType);
|
||||
end;
|
||||
|
||||
var fieldList := TRecordFieldList.Create(newFields, Node.Fields.Identity);
|
||||
// Build Definition
|
||||
var scalarDef: IScalarRecordDefinition := nil;
|
||||
var genericDef: IGenericRecordDefinition := nil;
|
||||
var resultType: IStaticType;
|
||||
|
||||
if allScalar then
|
||||
if isScalar and (Length(newFields) > 0) then
|
||||
begin
|
||||
def := TScalarRecord.TRegistry.Intern(scalarDefFields);
|
||||
staticType := TTypes.CreateRecord(def);
|
||||
Result := TAst.RecordLiteral(Node.Identity, fieldList, def, nil, staticType);
|
||||
scalarDef := TKeywordMappingRegistry<TScalar.TKind>.Intern(scalarFieldTypes);
|
||||
resultType := TTypes.CreateRecord(scalarDef);
|
||||
end
|
||||
else
|
||||
begin
|
||||
var genDefFields: TArray<TPair<IKeyword, IStaticType>>;
|
||||
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);
|
||||
Result := TAst.RecordLiteral(Node.Identity, fieldList, nil, genDef, staticType);
|
||||
genericDef := TGenericRecordRegistry.Intern(fieldTypes);
|
||||
resultType := TTypes.CreateGenericRecord(genericDef);
|
||||
end;
|
||||
|
||||
newFieldList := TRecordFieldList.Create(newFields, Node.Fields.Identity);
|
||||
Result := TAst.RecordLiteral(Node.Identity, newFieldList, scalarDef, genericDef, resultType);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitCreateSeries(const Node: ICreateSeriesNode): IAstNode;
|
||||
var
|
||||
elemType: IStaticType;
|
||||
defIdentity: IDefinitionIdentity;
|
||||
def: string;
|
||||
begin
|
||||
try
|
||||
defIdentity := Node.Identity.AsDefinition;
|
||||
elemType := TTypes.FromScalarKind(TScalar.StringToKind(defIdentity.Definition));
|
||||
except
|
||||
on E: Exception do
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Invalid type definition "%s" in new-series: %s', [Node.Definition, E.Message]), Node);
|
||||
elemType := TTypes.Unknown;
|
||||
end;
|
||||
end;
|
||||
def := Node.Definition;
|
||||
// Simple heuristic for type
|
||||
if def.StartsWith('[') then
|
||||
elemType := TTypes.CreateRecord(nil) // Placeholder, normally parses JSON
|
||||
else
|
||||
elemType := TTypes.FromScalarKind(TScalar.StringToKind(def));
|
||||
|
||||
Result := TAst.CreateSeries(defIdentity, TTypes.CreateSeries(elemType));
|
||||
Result := TAst.CreateSeries(Node.Identity.AsDefinition, TTypes.CreateSeries(elemType));
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitAddSeriesItem(const Node: IAddSeriesItemNode): IAstNode;
|
||||
var
|
||||
seriesType, valueType: IStaticType;
|
||||
newSeries, newValue, newLookback: IAstNode;
|
||||
function TTypeChecker.VisitSeriesLength(const Node: ISeriesLengthNode): IAstNode;
|
||||
begin
|
||||
newSeries := Accept(Node.Series);
|
||||
newValue := Accept(Node.Value);
|
||||
newLookback := Accept(Node.Lookback);
|
||||
|
||||
seriesType := newSeries.AsTypedNode.StaticType;
|
||||
valueType := newValue.AsTypedNode.StaticType;
|
||||
|
||||
if (seriesType.Kind <> stUnknown) then
|
||||
begin
|
||||
if (seriesType.Kind <> TStaticTypeKind.stSeries) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('"add" requires a series as its first argument, but got %s', [seriesType.ToString]), Node);
|
||||
end
|
||||
else
|
||||
begin
|
||||
if (valueType.Kind <> stUnknown) and not TTypeRules.CanAssign(seriesType.ElementType, valueType) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(
|
||||
Format('Cannot add item of type %s to series of type %s', [valueType.ToString, seriesType.ElementType.ToString]),
|
||||
Node
|
||||
);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
if (newLookback <> nil) then
|
||||
begin
|
||||
var lookbackType := newLookback.AsTypedNode.StaticType;
|
||||
if (lookbackType.Kind <> stUnknown) and not (lookbackType.Kind = TStaticTypeKind.stOrdinal) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError('Lookback parameter for "add" must be an ordinal value.', Node);
|
||||
end;
|
||||
end;
|
||||
|
||||
Result := TAst.AddSeriesItem(Node.Identity, newSeries.AsIdentifier, newValue, newLookback, TTypes.Void);
|
||||
Result := TAst.SeriesLength(Node.Identity, Accept(Node.Series).AsIdentifier, TTypes.Ordinal);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitNop(const Node: INopNode): IAstNode;
|
||||
@@ -968,23 +707,174 @@ begin
|
||||
Result := TAst.Nop(Node.Identity, TTypes.Void);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitSeriesLength(const Node: ISeriesLengthNode): IAstNode;
|
||||
var
|
||||
seriesType: IStaticType;
|
||||
newSeries: IAstNode;
|
||||
begin
|
||||
newSeries := Accept(Node.Series);
|
||||
seriesType := newSeries.AsTypedNode.StaticType;
|
||||
// =============================================================================
|
||||
// PIPE IMPLEMENTATION (Type Checking)
|
||||
// =============================================================================
|
||||
|
||||
if (seriesType.Kind <> stUnknown)
|
||||
and (seriesType.Kind <> TStaticTypeKind.stSeries)
|
||||
and (seriesType.Kind <> TStaticTypeKind.stRecordSeries) then
|
||||
function TTypeChecker.VisitPipeInput(const Node: IPipeInputNode): IAstNode;
|
||||
var
|
||||
newSource: IIdentifierNode;
|
||||
sourceType: IStaticType;
|
||||
newSelectors: TArray<IKeywordNode>;
|
||||
i: Integer;
|
||||
begin
|
||||
newSource := Accept(Node.StreamSource).AsIdentifier;
|
||||
sourceType := newSource.AsTypedNode.StaticType;
|
||||
|
||||
// 1. Verify Source is a Series-compatible type
|
||||
if (sourceType.Kind <> stUnknown) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('"length" requires a series, but got %s', [seriesType.ToString]), Node);
|
||||
if not ((sourceType.Kind = stSeries) or (sourceType.Kind = stRecordSeries)) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(
|
||||
Format('Pipe input "%s" must be a Series or RecordSeries, but got %s.', [newSource.Name, sourceType.ToString]),
|
||||
Node
|
||||
);
|
||||
end
|
||||
else if (sourceType.Kind = stRecordSeries) then
|
||||
begin
|
||||
// 2. Verify Selectors exist in Record Definition
|
||||
var def := sourceType.Definition;
|
||||
for var sel in Node.Selectors do
|
||||
begin
|
||||
if def.IndexOf(sel.Value) < 0 then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Field ":%s" not found in stream "%s".', [sel.Value.Name, newSource.Name]), sel);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
Result := TAst.SeriesLength(Node.Identity, newSeries.AsIdentifier, TTypes.Ordinal);
|
||||
// Reconstruct list simply to pass through
|
||||
SetLength(newSelectors, Node.Selectors.Count);
|
||||
for i := 0 to Node.Selectors.Count - 1 do
|
||||
newSelectors[i] := Node.Selectors[i];
|
||||
|
||||
Result := TAst.PipeInput(newSource, TAst.PipeSelectorList(newSelectors, Node.Selectors.Identity.Location), Node.Identity.Location);
|
||||
end;
|
||||
|
||||
function TTypeChecker.VisitPipe(const Node: IPipeNode): IAstNode;
|
||||
var
|
||||
i, k: Integer;
|
||||
inputNode: IPipeInputNode;
|
||||
newInputs: TArray<IPipeInputNode>;
|
||||
streamType: IStaticType;
|
||||
paramTypes: TList<IStaticType>;
|
||||
lambda: ILambdaExpressionNode;
|
||||
newParams: TArray<IIdentifierNode>;
|
||||
inferredType: IStaticType;
|
||||
begin
|
||||
SetLength(newInputs, Node.Inputs.Count);
|
||||
paramTypes := TList<IStaticType>.Create;
|
||||
try
|
||||
// 1. Visit Inputs and collect types for Lambda parameters
|
||||
for i := 0 to Node.Inputs.Count - 1 do
|
||||
begin
|
||||
inputNode := Accept(Node.Inputs[i]).AsPipeInput;
|
||||
newInputs[i] := inputNode;
|
||||
|
||||
streamType := inputNode.StreamSource.AsTypedNode.StaticType;
|
||||
|
||||
// Flatten logic: One lambda param per selector
|
||||
for var sel in inputNode.Selectors do
|
||||
begin
|
||||
inferredType := TTypes.Unknown;
|
||||
if streamType.Kind = stRecordSeries then
|
||||
begin
|
||||
// Extract field type from definition
|
||||
var def := streamType.Definition;
|
||||
var idx := def.IndexOf(sel.Value);
|
||||
if idx >= 0 then
|
||||
inferredType := TTypes.FromScalarKind(def.Items[idx].Value);
|
||||
end
|
||||
else if streamType.Kind = stSeries then
|
||||
begin
|
||||
// If simple series, use its element type
|
||||
if Assigned(streamType.ElementType) then
|
||||
inferredType := streamType.ElementType
|
||||
else
|
||||
inferredType := TTypes.Ordinal; // Fallback default
|
||||
end;
|
||||
|
||||
paramTypes.Add(inferredType);
|
||||
end;
|
||||
end;
|
||||
|
||||
// 2. Prepare Lambda with Inferred Types
|
||||
lambda := Node.Transformation;
|
||||
|
||||
if lambda.Parameters.Count <> paramTypes.Count then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(
|
||||
Format(
|
||||
'Pipe lambda expects %d parameters (one per selector), but got %d.',
|
||||
[paramTypes.Count, lambda.Parameters.Count]
|
||||
),
|
||||
lambda
|
||||
);
|
||||
end;
|
||||
|
||||
SetLength(newParams, lambda.Parameters.Count);
|
||||
for k := 0 to lambda.Parameters.Count - 1 do
|
||||
begin
|
||||
var oldP := lambda.Parameters[k];
|
||||
var typeToInject :=
|
||||
if k < paramTypes.Count then paramTypes[k]
|
||||
else TTypes.Unknown;
|
||||
|
||||
// Create new identifier with the inferred type!
|
||||
newParams[k] := TAst.Identifier(oldP.Identity.AsNamed, oldP.Address, typeToInject);
|
||||
end;
|
||||
|
||||
// Recreate Lambda NODE with Typed Parameters
|
||||
var preTypedLambda :=
|
||||
TAst.LambdaExpr(
|
||||
lambda.Identity,
|
||||
TParameterList.Create(newParams, lambda.Parameters.Identity),
|
||||
lambda.Body,
|
||||
lambda.Layout,
|
||||
lambda.Descriptor,
|
||||
lambda.Upvalues,
|
||||
lambda.HasNestedLambdas,
|
||||
lambda.IsPure,
|
||||
TTypes.Unknown // Will be recalculated in Accept
|
||||
);
|
||||
|
||||
// 3. Visit the Lambda (This will now type-check the Body using the types we just injected)
|
||||
var typedLambda := Accept(preTypedLambda).AsLambdaExpression;
|
||||
|
||||
// 4. Infer Pipe Return Type & Validate Strictness
|
||||
var lambdaRetType := typedLambda.AsTypedNode.StaticType.Signatures[0].ReturnType;
|
||||
var pipeType: IStaticType;
|
||||
|
||||
if lambdaRetType.Kind = stRecord then
|
||||
begin
|
||||
// Valid: Record -> RecordSeries
|
||||
pipeType := TTypes.CreateRecordSeries(lambdaRetType.Definition);
|
||||
end
|
||||
else
|
||||
begin
|
||||
// STRICT CHECK: Scalars are NOT allowed.
|
||||
if (lambdaRetType.Kind <> stUnknown) and (lambdaRetType.Kind <> stVoid) then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(
|
||||
Format('Pipe function must return a Record (e.g. {:res ...}). Type "%s" is invalid.', [lambdaRetType.ToString]),
|
||||
lambda
|
||||
);
|
||||
end;
|
||||
|
||||
// If Void or Unknown, or Error case:
|
||||
pipeType := TTypes.Unknown;
|
||||
end;
|
||||
|
||||
Result := TAst.Pipe(Node.Identity, TPipeInputList.Create(newInputs, Node.Inputs.Identity), typedLambda, pipeType);
|
||||
finally
|
||||
paramTypes.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
Reference in New Issue
Block a user