Pipes and test scripts
This commit is contained in:
@@ -13,7 +13,7 @@ uses
|
||||
Myc.Ast.Scope,
|
||||
Myc.Ast.Types,
|
||||
Myc.Ast.Compiler.Binder.Upvalues,
|
||||
Myc.Ast.Identities, // Wichtig für AsNamed
|
||||
Myc.Ast.Identities,
|
||||
Myc.Ast;
|
||||
|
||||
type
|
||||
@@ -40,7 +40,6 @@ type
|
||||
FBoxedDeclarations: THashSet<IVariableDeclarationNode>;
|
||||
FLog: ICompilerLog;
|
||||
|
||||
// Counts lambdas encountered to determine if a lambda has nested children
|
||||
FLambdaCounter: Integer;
|
||||
|
||||
function IsValidIdentifier(const Name: string): Boolean;
|
||||
@@ -234,7 +233,7 @@ var
|
||||
upvalueIndex: Integer;
|
||||
identity: INamedIdentity;
|
||||
begin
|
||||
identity := Node.Identity.AsNamed; // Type-safe extraction of identity
|
||||
identity := Node.Identity.AsNamed;
|
||||
|
||||
if Node.Address.Kind = akUnresolved then
|
||||
begin
|
||||
@@ -249,7 +248,6 @@ begin
|
||||
if physAddr.ScopeDepth > 0 then
|
||||
begin
|
||||
upvalueIndex := CaptureUpvalue(physAddr);
|
||||
// Recreate identifier using the SAME identity but new address
|
||||
Result := TAst.Identifier(identity, TResolvedAddress.Create(akUpvalue, 0, upvalueIndex), TTypes.Unknown);
|
||||
end
|
||||
else
|
||||
@@ -298,12 +296,8 @@ begin
|
||||
else
|
||||
newInit := nil;
|
||||
|
||||
// Recreate identifier with bound address using original identity
|
||||
newIdent := TAst.Identifier(identIdentity, addr, TTypes.Unknown);
|
||||
|
||||
isBoxed := (FBoxedDeclarations <> nil) and FBoxedDeclarations.Contains(Node);
|
||||
|
||||
// Recreate variable decl using original identity
|
||||
Result := TAst.VarDecl(Node.Identity, newIdent, newInit, TTypes.Unknown, isBoxed);
|
||||
|
||||
if Assigned(FFunctionRegistry) and (newInit <> nil) and (newInit.Kind = akLambdaExpression) then
|
||||
@@ -356,7 +350,6 @@ begin
|
||||
try
|
||||
FCurrentBuilder.Define('<self>');
|
||||
|
||||
// Create Array to hold transformed parameters
|
||||
SetLength(newParams, Node.Parameters.Count);
|
||||
|
||||
for i := 0 to Node.Parameters.Count - 1 do
|
||||
@@ -383,7 +376,6 @@ begin
|
||||
if (parentBuilder.Parent = nil) and (FArgTypes <> nil) and (i < Length(FArgTypes)) then
|
||||
paramType := FArgTypes[i];
|
||||
|
||||
// Rebind parameter identifier using original identity
|
||||
newParams[i] := TAst.Identifier(paramIdentity, addr, paramType);
|
||||
end;
|
||||
|
||||
@@ -417,40 +409,22 @@ begin
|
||||
|
||||
hasNested := FLambdaCounter > (startCount + 1);
|
||||
|
||||
// Wrap the array in a List wrapper to satisfy the factory interface
|
||||
// We reuse the identity of the old parameter list
|
||||
paramList := TParameterList.Create(newParams, Node.Parameters.Identity);
|
||||
|
||||
// Rebuild Lambda using original identity
|
||||
Result :=
|
||||
TAst.LambdaExpr(
|
||||
Node.Identity,
|
||||
paramList,
|
||||
newBody,
|
||||
finalLayout,
|
||||
nil, // Descriptor is created in TypeChecker
|
||||
upvaluesList,
|
||||
hasNested,
|
||||
Node.IsPure
|
||||
);
|
||||
Result := TAst.LambdaExpr(Node.Identity, paramList, newBody, finalLayout, nil, upvaluesList, hasNested, Node.IsPure);
|
||||
end;
|
||||
|
||||
function TAstBinder.VisitFunctionCall(const Node: IFunctionCallNode): IAstNode;
|
||||
begin
|
||||
if Node.Callee.Kind = akKeyword then
|
||||
begin
|
||||
// Transform keyword call (:key obj) into member access (.key obj)
|
||||
// Check argument count
|
||||
if Node.Arguments.Count <> 1 then
|
||||
begin
|
||||
if Assigned(FLog) then
|
||||
FLog.AddError(Format('Keyword access :%s requires exactly one argument.', [Node.Callee.AsKeyword.Value.Name]), Node);
|
||||
// Fallthrough to standard visit/recreation to avoid crash
|
||||
end
|
||||
else
|
||||
begin
|
||||
// Note: The new MemberAccess node gets the identity of the original FunctionCall
|
||||
// The member keyword retains its own identity.
|
||||
var memberAccess := TAst.MemberAccess(Node.Identity, Accept(Node.Arguments[0]), Node.Callee.AsKeyword);
|
||||
Result := memberAccess;
|
||||
exit;
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -48,6 +48,12 @@ type
|
||||
function VisitAddSeriesItem(const Node: IAddSeriesItemNode): TDataValue; override;
|
||||
function VisitSeriesLength(const Node: ISeriesLengthNode): TDataValue; override;
|
||||
function VisitNop(const Node: INopNode): TDataValue; override;
|
||||
|
||||
// Pipe Support
|
||||
function VisitPipeInput(const Node: IPipeInputNode): TDataValue; override;
|
||||
function VisitPipeSelectorList(const Node: IPipeSelectorList): TDataValue; override;
|
||||
function VisitPipeInputList(const Node: IPipeInputList): TDataValue; override;
|
||||
function VisitPipe(const Node: IPipeNode): TDataValue; override;
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -309,4 +315,54 @@ begin
|
||||
Result := TDataValue.Void;
|
||||
end;
|
||||
|
||||
// --- Pipe Support ---
|
||||
|
||||
function TDebugEvaluatorVisitor.VisitPipe(const Node: IPipeNode): TDataValue;
|
||||
begin
|
||||
AppendLine('Pipe {');
|
||||
Indent;
|
||||
try
|
||||
Result := inherited VisitPipe(Node);
|
||||
finally
|
||||
Unindent;
|
||||
end;
|
||||
AppendLine(Format('} -> %s', [Result.ToString]));
|
||||
end;
|
||||
|
||||
function TDebugEvaluatorVisitor.VisitPipeInput(const Node: IPipeInputNode): TDataValue;
|
||||
begin
|
||||
AppendLine(Format('PipeInput (Source: %s) {', [Node.StreamSource.Name]));
|
||||
Indent;
|
||||
try
|
||||
Result := inherited VisitPipeInput(Node);
|
||||
finally
|
||||
Unindent;
|
||||
end;
|
||||
AppendLine('}');
|
||||
end;
|
||||
|
||||
function TDebugEvaluatorVisitor.VisitPipeSelectorList(const Node: IPipeSelectorList): TDataValue;
|
||||
begin
|
||||
AppendLine('Selectors [');
|
||||
Indent;
|
||||
try
|
||||
Result := inherited VisitPipeSelectorList(Node);
|
||||
finally
|
||||
Unindent;
|
||||
end;
|
||||
AppendLine(']');
|
||||
end;
|
||||
|
||||
function TDebugEvaluatorVisitor.VisitPipeInputList(const Node: IPipeInputList): TDataValue;
|
||||
begin
|
||||
AppendLine('Inputs [');
|
||||
Indent;
|
||||
try
|
||||
Result := inherited VisitPipeInputList(Node);
|
||||
finally
|
||||
Unindent;
|
||||
end;
|
||||
AppendLine(']');
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
@@ -40,7 +40,6 @@ type
|
||||
|
||||
function ExpandMacros(const Node: IAstNode): IAstNode;
|
||||
|
||||
// Updated Bind signature to accept Log
|
||||
function Bind(
|
||||
const Node: IAstNode;
|
||||
out Layout: IScopeLayout;
|
||||
|
||||
+112
-14
@@ -83,6 +83,8 @@ uses
|
||||
Myc.Ast,
|
||||
Myc.Data.Decimal,
|
||||
Myc.Data.Series,
|
||||
Myc.Data.Stream,
|
||||
Myc.Data.Stream.Pipes,
|
||||
Myc.Data.Scalar.JSON,
|
||||
Myc.Ast.Types;
|
||||
|
||||
@@ -633,20 +635,6 @@ begin
|
||||
Result := TDataValue.Void;
|
||||
end;
|
||||
|
||||
// --- Pipe Visitors Stubs ---
|
||||
|
||||
function TEvaluatorVisitor.VisitPipe(const Node: IPipeNode): TDataValue;
|
||||
begin
|
||||
// Placeholder for Phase 4 (Runtime Integration)
|
||||
// Here we will:
|
||||
// 1. Resolve Input Streams from Scope
|
||||
// 2. Build TPipeConfig
|
||||
// 3. Compile Lambda to TDataValue.TFunc
|
||||
// 4. Create Adapter
|
||||
// 5. Create TPipeStream
|
||||
raise EEvaluatorException.Create('Pipe execution is not yet implemented.');
|
||||
end;
|
||||
|
||||
function TEvaluatorVisitor.VisitPipeInput(const Node: IPipeInputNode): TDataValue;
|
||||
begin
|
||||
Result := TDataValue.Void;
|
||||
@@ -662,4 +650,114 @@ begin
|
||||
Result := TDataValue.Void;
|
||||
end;
|
||||
|
||||
// --- Pipe Implementation ---
|
||||
|
||||
function TEvaluatorVisitor.VisitPipe(const Node: IPipeNode): TDataValue;
|
||||
var
|
||||
i: Integer;
|
||||
inputNode: IPipeInputNode;
|
||||
sources: TArray<IStream>;
|
||||
config: TPipeConfig;
|
||||
inputVal: TDataValue;
|
||||
sourceSeries: IScalarRecordSeries;
|
||||
outputDef: IScalarRecordDefinition;
|
||||
lambdaFunc: TDataValue.TFunc;
|
||||
pipeAdapter: TPipeStream.TPipeLambda;
|
||||
begin
|
||||
SetLength(sources, Node.Inputs.Count);
|
||||
SetLength(config, Node.Inputs.Count);
|
||||
|
||||
// 1. Resolve Inputs & Build Config
|
||||
for i := 0 to Node.Inputs.Count - 1 do
|
||||
begin
|
||||
inputNode := Node.Inputs[i];
|
||||
inputVal := FScope[inputNode.StreamSource.Address];
|
||||
|
||||
// STRICT CHECK: Only vkStream is allowed.
|
||||
if inputVal.Kind = vkStream then
|
||||
begin
|
||||
sources[i] := inputVal.AsStream;
|
||||
end
|
||||
else
|
||||
begin
|
||||
raise EEvaluatorException
|
||||
.CreateFmt('Variable "%s" must be a Stream. Found type "%s".', [inputNode.StreamSource.Name, inputVal.Kind.ToString]);
|
||||
end;
|
||||
|
||||
sourceSeries := sources[i].Series;
|
||||
var srcDef := sourceSeries.Def;
|
||||
var selectors := inputNode.Selectors;
|
||||
|
||||
SetLength(config[i], selectors.Count);
|
||||
for var k := 0 to selectors.Count - 1 do
|
||||
begin
|
||||
var key := selectors[k].Value;
|
||||
var idx := srcDef.IndexOf(key);
|
||||
// Runtime safety check (TypeChecker usually handles this)
|
||||
if idx < 0 then
|
||||
raise EEvaluatorException.CreateFmt('Field ":%s" missing in stream "%s".', [key.Name, inputNode.StreamSource.Name]);
|
||||
|
||||
var fieldKind := srcDef.Items[idx].Value;
|
||||
config[i][k] := TScalarRecordField.Create(key, fieldKind);
|
||||
end;
|
||||
end;
|
||||
|
||||
// 2. Get Output Definition
|
||||
if (Node.StaticType = nil) or (Node.StaticType.Kind <> stRecordSeries) then
|
||||
raise EEvaluatorException.Create(
|
||||
'Internal Error: Pipe requires a RecordSeries static type definition. Lambda must return a Record.');
|
||||
|
||||
outputDef := Node.StaticType.Definition;
|
||||
|
||||
// 3. Compile Lambda
|
||||
lambdaFunc := Node.Transformation.Accept(Self).AsMethod();
|
||||
|
||||
// 4. Create Adapter
|
||||
pipeAdapter :=
|
||||
function(const SourceSeries: array of ISeries; out Results: array of TScalar.TValue): Boolean
|
||||
var
|
||||
scriptArgs: TArray<TDataValue>;
|
||||
scriptResult: TDataValue;
|
||||
k: Integer;
|
||||
resRec: IScalarRecord;
|
||||
begin
|
||||
SetLength(scriptArgs, Length(SourceSeries));
|
||||
// Pass Scalars (Current Items)
|
||||
for k := 0 to High(SourceSeries) do
|
||||
scriptArgs[k] := TDataValue(SourceSeries[k].Items[0]);
|
||||
|
||||
scriptResult := lambdaFunc(scriptArgs);
|
||||
|
||||
// Void -> Filtered out (no emit)
|
||||
if scriptResult.IsVoid then
|
||||
exit(False);
|
||||
|
||||
// STRICT CHECK: Only ScalarRecord allowed
|
||||
if scriptResult.Kind = vkScalarRecord then
|
||||
begin
|
||||
resRec := scriptResult.AsScalarRecord;
|
||||
|
||||
// Integrity check: Runtime Record must match Static Definition
|
||||
if resRec.Def.Count <> Length(Results) then
|
||||
raise EEvaluatorException.Create('Pipe lambda returned record with incorrect field count/layout.');
|
||||
|
||||
for k := 0 to resRec.Def.Count - 1 do
|
||||
Results[k] := resRec.Items[k].Value.Value;
|
||||
end
|
||||
else
|
||||
begin
|
||||
// Scalars are forbidden
|
||||
raise EEvaluatorException
|
||||
.CreateFmt('Pipe lambda returned invalid type "%s". Must return a Record or Void.', [scriptResult.Kind.ToString]);
|
||||
end;
|
||||
|
||||
Result := True;
|
||||
end;
|
||||
|
||||
var pipe := TPipeStream.Create(config, outputDef, sources, pipeAdapter);
|
||||
|
||||
// Result is a Stream
|
||||
Result := TDataValue.FromStream(pipe);
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
@@ -3,7 +3,7 @@ unit Myc.Ast.RTL.Core;
|
||||
interface
|
||||
|
||||
uses
|
||||
system.sysutils,
|
||||
System.SysUtils,
|
||||
Myc.Utils,
|
||||
Myc.Data.Scalar,
|
||||
Myc.Data.Value,
|
||||
@@ -16,233 +16,233 @@ type
|
||||
// (* --- Dynamic Fallbacks (Interpreter) --- *)
|
||||
|
||||
// Arithmetic
|
||||
[TRtlExport('+')]
|
||||
[TRtlExport('+', Pure)]
|
||||
class function Add(const Args: TArray<TDataValue>): TDataValue; overload; static;
|
||||
[TRtlExport('-')]
|
||||
[TRtlExport('-', Pure)]
|
||||
class function Subtract(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('*')]
|
||||
[TRtlExport('*', Pure)]
|
||||
class function Multiply(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('/')]
|
||||
[TRtlExport('/', Pure)]
|
||||
class function Divide(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
|
||||
[TRtlExport('div')]
|
||||
[TRtlExport('div', Pure)]
|
||||
class function IntDivide(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('mod')]
|
||||
[TRtlExport('mod', Pure)]
|
||||
class function Modulus(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
|
||||
// Comparison
|
||||
[TRtlExport('=')]
|
||||
[TRtlExport('=', Impure)]
|
||||
class function Equal(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('<>')]
|
||||
[TRtlExport('<>', Impure)]
|
||||
class function NotEqual(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('<')]
|
||||
[TRtlExport('<', Impure)]
|
||||
class function LessThan(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('<=')]
|
||||
[TRtlExport('<=', Impure)]
|
||||
class function LessThanOrEqual(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('>')]
|
||||
[TRtlExport('>', Impure)]
|
||||
class function GreaterThan(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('>=')]
|
||||
[TRtlExport('>=', Impure)]
|
||||
class function GreaterThanOrEqual(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
|
||||
// Logic / Bitwise
|
||||
[TRtlExport('not')]
|
||||
[TRtlExport('not', Impure)]
|
||||
class function LogicalNot(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('and')]
|
||||
[TRtlExport('and', Impure)]
|
||||
class function BitwiseAnd(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('or')]
|
||||
[TRtlExport('or', Impure)]
|
||||
class function BitwiseOr(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('xor')]
|
||||
[TRtlExport('xor', Impure)]
|
||||
class function BitwiseXor(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('shl')]
|
||||
[TRtlExport('shl', Impure)]
|
||||
class function LeftShift(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('shr')]
|
||||
[TRtlExport('shr', Impure)]
|
||||
class function RightShift(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
|
||||
// (* Dynamic fallbacks for TScalar input *)
|
||||
[TRtlExport('Abs')]
|
||||
[TRtlExport('Abs', Impure)]
|
||||
class function Abs(const Arg: TScalar): TScalar; static;
|
||||
[TRtlExport('Round')]
|
||||
[TRtlExport('Round', Impure)]
|
||||
class function Round(const Arg: TScalar): TScalar; static;
|
||||
[TRtlExport('Trunc')]
|
||||
[TRtlExport('Trunc', Impure)]
|
||||
class function Trunc(const Arg: TScalar): TScalar; static;
|
||||
[TRtlExport('Ceil')]
|
||||
[TRtlExport('Ceil', Impure)]
|
||||
class function Ceil(const Arg: TScalar): TScalar; static;
|
||||
[TRtlExport('Floor')]
|
||||
[TRtlExport('Floor', Impure)]
|
||||
class function Floor(const Arg: TScalar): TScalar; static;
|
||||
[TRtlExport('Sign')]
|
||||
[TRtlExport('Sign', Impure)]
|
||||
class function Sign(const Arg: TScalar): TScalar; static;
|
||||
|
||||
// (* DateTime Constructors *)
|
||||
[TRtlExport('Now', False)]
|
||||
[TRtlExport('Now', Impure)]
|
||||
class function Now(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('Date')]
|
||||
[TRtlExport('Date', Impure)]
|
||||
class function Date(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
|
||||
// (* Other dynamic functions *)
|
||||
[TRtlExport('Memoize')]
|
||||
[TRtlExport('Memoize', Impure)]
|
||||
class function Memoize(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('Map')]
|
||||
[TRtlExport('Map', Impure)]
|
||||
class function Map(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('Reduce')]
|
||||
[TRtlExport('Reduce', Impure)]
|
||||
class function Reduce(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('Where')]
|
||||
[TRtlExport('Where', Impure)]
|
||||
class function Where(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
[TRtlExport('Any')]
|
||||
[TRtlExport('Any', Impure)]
|
||||
class function Any(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
|
||||
// (* --- Static Specializations (Monomorphization Targets) --- *)
|
||||
|
||||
// Add
|
||||
[TRtlExport('+', True)]
|
||||
[TRtlExport('+', Pure)]
|
||||
class function Add_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('+', True)]
|
||||
[TRtlExport('+', Pure)]
|
||||
class function Add_Float_Float_Float(A, B: Double): Double; static;
|
||||
[TRtlExport('+', True)]
|
||||
[TRtlExport('+', Pure)]
|
||||
class function Add_Ordinal_Float_Float(A: Int64; B: Double): Double; static;
|
||||
[TRtlExport('+', True)]
|
||||
[TRtlExport('+', Pure)]
|
||||
class function Add_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
|
||||
[TRtlExport('+', True)]
|
||||
[TRtlExport('+', Pure)]
|
||||
class function Add_Text_Text_Text(const A: String; const B: String): String; static;
|
||||
|
||||
// Subtract
|
||||
[TRtlExport('-', True)]
|
||||
[TRtlExport('-', Pure)]
|
||||
class function Subtract_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('-', True)]
|
||||
[TRtlExport('-', Pure)]
|
||||
class function Subtract_Float_Float_Float(A, B: Double): Double; static;
|
||||
[TRtlExport('-', True)]
|
||||
[TRtlExport('-', Pure)]
|
||||
class function Subtract_Ordinal_Float_Float(A: Int64; B: Double): Double; static;
|
||||
[TRtlExport('-', True)]
|
||||
[TRtlExport('-', Pure)]
|
||||
class function Subtract_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
|
||||
|
||||
// Multiply
|
||||
[TRtlExport('*', True)]
|
||||
[TRtlExport('*', Pure)]
|
||||
class function Multiply_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('*', True)]
|
||||
[TRtlExport('*', Pure)]
|
||||
class function Multiply_Float_Float_Float(A, B: Double): Double; static;
|
||||
[TRtlExport('*', True)]
|
||||
[TRtlExport('*', Pure)]
|
||||
class function Multiply_Ordinal_Float_Float(A: Int64; B: Double): Double; static;
|
||||
[TRtlExport('*', True)]
|
||||
[TRtlExport('*', Pure)]
|
||||
class function Multiply_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
|
||||
|
||||
// Divide (Impure due to EDivByZero potential)
|
||||
[TRtlExport('/')]
|
||||
[TRtlExport('/', Impure)]
|
||||
class function Divide_Ordinal_Ordinal_Float(A, B: Int64): Double; static;
|
||||
[TRtlExport('/')]
|
||||
[TRtlExport('/', Impure)]
|
||||
class function Divide_Float_Float_Float(A, B: Double): Double; static;
|
||||
[TRtlExport('/')]
|
||||
[TRtlExport('/', Impure)]
|
||||
class function Divide_Ordinal_Float_Float(A: Int64; B: Double): Double; static;
|
||||
[TRtlExport('/')]
|
||||
[TRtlExport('/', Impure)]
|
||||
class function Divide_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
|
||||
|
||||
// Integer Math (Impure due to EDivByZero)
|
||||
[TRtlExport('div')]
|
||||
[TRtlExport('div', Impure)]
|
||||
class function Div_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('mod')]
|
||||
[TRtlExport('mod', Impure)]
|
||||
class function Mod_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
|
||||
// Comparisons
|
||||
[TRtlExport('=', True)]
|
||||
[TRtlExport('=', Pure)]
|
||||
class function Equal_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('=', True)]
|
||||
[TRtlExport('=', Pure)]
|
||||
class function Equal_Float_Float_Ordinal(A, B: Double): Int64; static;
|
||||
[TRtlExport('=', True)]
|
||||
[TRtlExport('=', Pure)]
|
||||
class function Equal_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
|
||||
[TRtlExport('=', True)]
|
||||
[TRtlExport('=', Pure)]
|
||||
class function Equal_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
|
||||
[TRtlExport('=', True)]
|
||||
[TRtlExport('=', Pure)]
|
||||
class function Equal_Keyword_Keyword_Ordinal(A, B: Int64): Int64; static;
|
||||
|
||||
[TRtlExport('<>', True)]
|
||||
[TRtlExport('<>', Pure)]
|
||||
class function NotEqual_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('<>', True)]
|
||||
[TRtlExport('<>', Pure)]
|
||||
class function NotEqual_Float_Float_Ordinal(A, B: Double): Int64; static;
|
||||
[TRtlExport('<>', True)]
|
||||
[TRtlExport('<>', Pure)]
|
||||
class function NotEqual_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
|
||||
[TRtlExport('<>', True)]
|
||||
[TRtlExport('<>', Pure)]
|
||||
class function NotEqual_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
|
||||
[TRtlExport('<>', True)]
|
||||
[TRtlExport('<>', Pure)]
|
||||
class function NotEqual_Keyword_Keyword_Ordinal(A, B: Int64): Int64; static;
|
||||
|
||||
[TRtlExport('<', True)]
|
||||
[TRtlExport('<', Pure)]
|
||||
class function Less_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('<', True)]
|
||||
[TRtlExport('<', Pure)]
|
||||
class function Less_Float_Float_Ordinal(A, B: Double): Int64; static;
|
||||
[TRtlExport('<', True)]
|
||||
[TRtlExport('<', Pure)]
|
||||
class function Less_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
|
||||
[TRtlExport('<', True)]
|
||||
[TRtlExport('<', Pure)]
|
||||
class function Less_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
|
||||
|
||||
[TRtlExport('<=', True)]
|
||||
[TRtlExport('<=', Pure)]
|
||||
class function LessOrEqual_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('<=', True)]
|
||||
[TRtlExport('<=', Pure)]
|
||||
class function LessOrEqual_Float_Float_Ordinal(A, B: Double): Int64; static;
|
||||
[TRtlExport('<=', True)]
|
||||
[TRtlExport('<=', Pure)]
|
||||
class function LessOrEqual_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
|
||||
[TRtlExport('<=', True)]
|
||||
[TRtlExport('<=', Pure)]
|
||||
class function LessOrEqual_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
|
||||
|
||||
[TRtlExport('>', True)]
|
||||
[TRtlExport('>', Pure)]
|
||||
class function Greater_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('>', True)]
|
||||
[TRtlExport('>', Pure)]
|
||||
class function Greater_Float_Float_Ordinal(A, B: Double): Int64; static;
|
||||
[TRtlExport('>', True)]
|
||||
[TRtlExport('>', Pure)]
|
||||
class function Greater_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
|
||||
[TRtlExport('>', True)]
|
||||
[TRtlExport('>', Pure)]
|
||||
class function Greater_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
|
||||
|
||||
[TRtlExport('>=', True)]
|
||||
[TRtlExport('>=', Pure)]
|
||||
class function GreaterOrEqual_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('>=', True)]
|
||||
[TRtlExport('>=', Pure)]
|
||||
class function GreaterOrEqual_Float_Float_Ordinal(A, B: Double): Int64; static;
|
||||
[TRtlExport('>=', True)]
|
||||
[TRtlExport('>=', Pure)]
|
||||
class function GreaterOrEqual_Ordinal_Float_Ordinal(A: Int64; B: Double): Int64; static;
|
||||
[TRtlExport('>=', True)]
|
||||
[TRtlExport('>=', Pure)]
|
||||
class function GreaterOrEqual_Float_Ordinal_Ordinal(A: Double; B: Int64): Int64; static;
|
||||
|
||||
// Bitwise Static
|
||||
[TRtlExport('and', True)]
|
||||
[TRtlExport('and', Pure)]
|
||||
class function And_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('or', True)]
|
||||
[TRtlExport('or', Pure)]
|
||||
class function Or_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('xor', True)]
|
||||
[TRtlExport('xor', Pure)]
|
||||
class function Xor_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('shl', True)]
|
||||
[TRtlExport('shl', Pure)]
|
||||
class function Shl_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
[TRtlExport('shr', True)]
|
||||
[TRtlExport('shr', Pure)]
|
||||
class function Shr_Ordinal_Ordinal_Ordinal(A, B: Int64): Int64; static;
|
||||
|
||||
// Unary
|
||||
[TRtlExport('-', True)]
|
||||
[TRtlExport('-', Pure)]
|
||||
class function Negate_Ordinal_Ordinal(A: Int64): Int64; static;
|
||||
[TRtlExport('-', True)]
|
||||
[TRtlExport('-', Pure)]
|
||||
class function Negate_Float_Float(A: Double): Double; static;
|
||||
|
||||
[TRtlExport('not', True)]
|
||||
[TRtlExport('not', Pure)]
|
||||
class function Not_Ordinal_Ordinal(A: Int64): Int64; static;
|
||||
|
||||
// Standard functions
|
||||
[TRtlExport('Abs', True)]
|
||||
[TRtlExport('Abs', Pure)]
|
||||
class function Abs_Ordinal_Ordinal(A: Int64): Int64; static;
|
||||
[TRtlExport('Abs', True)]
|
||||
[TRtlExport('Abs', Pure)]
|
||||
class function Abs_Float_Float(A: Double): Double; static;
|
||||
|
||||
[TRtlExport('Round', True)]
|
||||
[TRtlExport('Round', Pure)]
|
||||
class function Round_Ordinal_Ordinal(A: Int64): Int64; static;
|
||||
[TRtlExport('Round', True)]
|
||||
[TRtlExport('Round', Pure)]
|
||||
class function Round_Float_Ordinal(A: Double): Int64; static;
|
||||
|
||||
[TRtlExport('Trunc', True)]
|
||||
[TRtlExport('Trunc', Pure)]
|
||||
class function Trunc_Ordinal_Ordinal(A: Int64): Int64; static;
|
||||
[TRtlExport('Trunc', True)]
|
||||
[TRtlExport('Trunc', Pure)]
|
||||
class function Trunc_Float_Ordinal(A: Double): Int64; static;
|
||||
end;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
system.generics.collections,
|
||||
system.math,
|
||||
system.dateutils,
|
||||
System.Generics.Collections,
|
||||
System.Math,
|
||||
System.Dateutils,
|
||||
Myc.Data.Decimal;
|
||||
|
||||
{ TRtlFunctions - Operator Implementations }
|
||||
|
||||
@@ -20,11 +20,13 @@ uses
|
||||
type
|
||||
// Defines the export parameters for a native RTL function.
|
||||
TRtlExportAttribute = class(TCustomAttribute)
|
||||
type
|
||||
TPurity = (Pure, Impure);
|
||||
public
|
||||
Name: string;
|
||||
IsPure: Boolean;
|
||||
constructor Create(const AName: string); overload;
|
||||
constructor Create(const AName: string; AIsPure: Boolean); overload;
|
||||
constructor Create(const AName: string; APurity: TPurity); overload;
|
||||
end;
|
||||
|
||||
// Defines the key for the static bootstrap cache.
|
||||
@@ -160,11 +162,11 @@ begin
|
||||
Self.IsPure := False; // Default to impure (safe)
|
||||
end;
|
||||
|
||||
constructor TRtlExportAttribute.Create(const AName: string; AIsPure: Boolean);
|
||||
constructor TRtlExportAttribute.Create(const AName: string; APurity: TPurity);
|
||||
begin
|
||||
inherited Create;
|
||||
Self.Name := AName;
|
||||
Self.IsPure := AIsPure;
|
||||
Self.IsPure := APurity = Pure;
|
||||
end;
|
||||
|
||||
{ TStaticSignatureKey }
|
||||
|
||||
@@ -545,18 +545,22 @@ function TAstTransformer.VisitConstant(const Node: IConstantNode): IAstNode;
|
||||
begin
|
||||
Result := Node;
|
||||
end;
|
||||
|
||||
function TAstTransformer.VisitIdentifier(const Node: IIdentifierNode): IAstNode;
|
||||
begin
|
||||
Result := Node;
|
||||
end;
|
||||
|
||||
function TAstTransformer.VisitKeyword(const Node: IKeywordNode): IAstNode;
|
||||
begin
|
||||
Result := Node;
|
||||
end;
|
||||
|
||||
function TAstTransformer.VisitNop(const Node: INopNode): IAstNode;
|
||||
begin
|
||||
Result := Node;
|
||||
end;
|
||||
|
||||
function TAstTransformer.VisitCreateSeries(const Node: ICreateSeriesNode): IAstNode;
|
||||
begin
|
||||
Result := Node;
|
||||
|
||||
@@ -26,6 +26,8 @@ type
|
||||
// The 8-byte storage for the scalar value
|
||||
PValue = ^TValue;
|
||||
TValue = record
|
||||
class operator Implicit(A: Int64): TValue; overload;
|
||||
class operator Implicit(A: Double): TValue; overload;
|
||||
case TKind of
|
||||
TKind.Ordinal, TKind.Keyword, TKind.Boolean: (AsInt64: Int64);
|
||||
TKind.Float, TKind.DateTime: (AsDouble: Double);
|
||||
@@ -190,19 +192,19 @@ type
|
||||
IScalarRecordSeries = interface(IKeywordMapping<ISeries>)
|
||||
{$region 'private'}
|
||||
function GetDef: IScalarRecordDefinition;
|
||||
function GetTotalCount: Int64;
|
||||
{$endregion}
|
||||
property Def: IScalarRecordDefinition read GetDef;
|
||||
property TotalCount: Int64 read GetTotalCount;
|
||||
end;
|
||||
|
||||
IWriteableScalarRecordSeries = interface(IScalarRecordSeries)
|
||||
{$region 'private'}
|
||||
function GetRecordCount: Int64;
|
||||
function GetTotalCount: Int64;
|
||||
{$endregion}
|
||||
procedure Add(const Item: IKeywordMapping<TScalar>; Lookback: Int64 = -1); overload;
|
||||
procedure Add(const Items: array of TScalar.TValue; Lookback: Int64 = -1); overload;
|
||||
property RecordCount: Int64 read GetRecordCount;
|
||||
property TotalCount: Int64 read GetTotalCount;
|
||||
end;
|
||||
|
||||
// A series of scalar records, optimized for memory and access speed.
|
||||
@@ -1071,6 +1073,16 @@ begin
|
||||
Result := FDef.IndexOf(Key);
|
||||
end;
|
||||
|
||||
class operator TScalar.TValue.Implicit(A: Int64): TValue;
|
||||
begin
|
||||
Result.AsInt64 := A;
|
||||
end;
|
||||
|
||||
class operator TScalar.TValue.Implicit(A: Double): TValue;
|
||||
begin
|
||||
Result.AsDouble := A;
|
||||
end;
|
||||
|
||||
initialization
|
||||
Assert(sizeof(TScalar.TValue) = 8);
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ uses
|
||||
System.SyncObjs,
|
||||
Myc.Data.Scalar,
|
||||
Myc.Data.Keyword,
|
||||
Myc.Data.Value,
|
||||
Myc.Core.Notifier;
|
||||
|
||||
type
|
||||
|
||||
@@ -9,6 +9,7 @@ uses
|
||||
Myc.Utils,
|
||||
Myc.Data.Scalar,
|
||||
Myc.Data.Series,
|
||||
Myc.Data.Stream,
|
||||
Myc.Data.Keyword;
|
||||
|
||||
type
|
||||
@@ -24,6 +25,7 @@ type
|
||||
vkMethod,
|
||||
vkInterface,
|
||||
vkPointer,
|
||||
vkStream,
|
||||
vkGeneric
|
||||
);
|
||||
|
||||
@@ -86,6 +88,9 @@ type
|
||||
class function FromGenericRecord(const AValue: IKeywordMapping<TDataValue>): TDataValue; static; inline;
|
||||
class function FromSeries(const AValue: ISeries): TDataValue; static; inline;
|
||||
|
||||
// New Factory for Streams
|
||||
class function FromStream(const AStream: IStream): TDataValue; static; inline;
|
||||
|
||||
// --- Existing Accessors ---
|
||||
function AsScalar: TScalar; inline;
|
||||
function AsMethod: TFunc; inline;
|
||||
@@ -96,6 +101,9 @@ type
|
||||
function AsSeries: ISeries; inline;
|
||||
function AsObject: TObject; overload; inline;
|
||||
|
||||
// New Accessor for Streams
|
||||
function AsStream: IStream; inline;
|
||||
|
||||
function ToString: String;
|
||||
property IsVoid: Boolean read GetIsVoid;
|
||||
property Kind: TDataValueKind read FKind;
|
||||
@@ -141,6 +149,7 @@ begin
|
||||
vkInterface: Result := 'Interface';
|
||||
vkPointer: Result := 'Pointer';
|
||||
vkGeneric: Result := 'Generic';
|
||||
vkStream: Result := 'Stream';
|
||||
else
|
||||
Result := 'Unknown';
|
||||
end;
|
||||
@@ -273,6 +282,12 @@ begin
|
||||
Result.FInterface := AValue;
|
||||
end;
|
||||
|
||||
class function TDataValue.FromStream(const AStream: IStream): TDataValue;
|
||||
begin
|
||||
Result.FKind := vkStream;
|
||||
Result.FInterface := AStream;
|
||||
end;
|
||||
|
||||
class function TDataValue.Map(const SourceSeries: ISeries; const MapperFunc: TFunc): ISeries;
|
||||
begin
|
||||
Result := TMapSeries.Create(SourceSeries, MapperFunc);
|
||||
@@ -285,6 +300,13 @@ begin
|
||||
Result := ISeries(FInterface);
|
||||
end;
|
||||
|
||||
function TDataValue.AsStream: IStream;
|
||||
begin
|
||||
if (FKind <> vkStream) then
|
||||
raise EInvalidCast.Create('Cannot read value as Stream.');
|
||||
Result := IStream(FInterface);
|
||||
end;
|
||||
|
||||
function TDataValue.AsMethod: TFunc;
|
||||
begin
|
||||
if (FKind <> vkMethod) then
|
||||
@@ -443,6 +465,7 @@ begin
|
||||
sb.Free;
|
||||
end;
|
||||
end;
|
||||
vkStream: Result := '<stream>';
|
||||
vkVoid: Result := '<void>';
|
||||
vkMethod: Result := '<method>';
|
||||
vkGeneric: Result := '<generic>';
|
||||
|
||||
Reference in New Issue
Block a user