Compiler errors
This commit is contained in:
@@ -245,7 +245,7 @@ begin
|
|||||||
begin
|
begin
|
||||||
var layout: IScopeLayout;
|
var layout: IScopeLayout;
|
||||||
// Bind now returns the bound AST and provides the layout
|
// Bind now returns the bound AST and provides the layout
|
||||||
Result := FEnvironment.Environment.Bind(Result, layout);
|
Result := FEnvironment.Environment.Bind(Result, layout, [], nil);
|
||||||
|
|
||||||
if CompilerStageBox.ItemIndex > 2 then
|
if CompilerStageBox.ItemIndex > 2 then
|
||||||
// Specialize no longer needs descriptor
|
// Specialize no longer needs descriptor
|
||||||
@@ -286,6 +286,25 @@ begin
|
|||||||
Result := FCurrExec.Func([]);
|
Result := FCurrExec.Func([]);
|
||||||
|
|
||||||
except
|
except
|
||||||
|
on E: ECompilationFailed do
|
||||||
|
begin
|
||||||
|
Memo1.Lines.BeginUpdate;
|
||||||
|
try
|
||||||
|
Memo1.Lines.Clear;
|
||||||
|
// Die Hauptnachricht der Exception (z.B. "Compilation failed with 5 error(s)...")
|
||||||
|
Memo1.Lines.Add(E.Message);
|
||||||
|
Memo1.Lines.Add(''); // Leerzeile zur optischen Trennung
|
||||||
|
|
||||||
|
// Iteration durch alle gesammelten Fehler im Log
|
||||||
|
for var err in E.Errors do
|
||||||
|
begin
|
||||||
|
// err.ToString formatiert automatisch als "[Error] Nachricht" etc.
|
||||||
|
Memo1.Lines.Add(err.ToString);
|
||||||
|
end;
|
||||||
|
finally
|
||||||
|
Memo1.Lines.EndUpdate;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
on E: Exception do
|
on E: Exception do
|
||||||
begin
|
begin
|
||||||
FCurrExec.Func := nil;
|
FCurrExec.Func := nil;
|
||||||
|
|||||||
@@ -16,9 +16,6 @@ uses
|
|||||||
Myc.Ast;
|
Myc.Ast;
|
||||||
|
|
||||||
type
|
type
|
||||||
// Exception specific to the binding phase (e.g. undefined symbols, invalid identifiers)
|
|
||||||
EBinderException = class(EAstException);
|
|
||||||
|
|
||||||
IAstBinder = interface(IAstVisitor)
|
IAstBinder = interface(IAstVisitor)
|
||||||
function Execute(const RootNode: IAstNode; out Layout: IScopeLayout): IAstNode;
|
function Execute(const RootNode: IAstNode; out Layout: IScopeLayout): IAstNode;
|
||||||
end;
|
end;
|
||||||
@@ -40,6 +37,7 @@ type
|
|||||||
FArgTypes: TArray<IStaticType>;
|
FArgTypes: TArray<IStaticType>;
|
||||||
FFunctionRegistry: IFunctionDefinitionRegistry;
|
FFunctionRegistry: IFunctionDefinitionRegistry;
|
||||||
FBoxedDeclarations: THashSet<IVariableDeclarationNode>;
|
FBoxedDeclarations: THashSet<IVariableDeclarationNode>;
|
||||||
|
FLog: ICompilerLog;
|
||||||
|
|
||||||
// Counts lambdas encountered to determine if a lambda has nested children
|
// Counts lambdas encountered to determine if a lambda has nested children
|
||||||
FLambdaCounter: Integer;
|
FLambdaCounter: Integer;
|
||||||
@@ -63,7 +61,8 @@ type
|
|||||||
constructor Create(
|
constructor Create(
|
||||||
const AParentLayout: IScopeLayout;
|
const AParentLayout: IScopeLayout;
|
||||||
const AFunctionRegistry: IFunctionDefinitionRegistry;
|
const AFunctionRegistry: IFunctionDefinitionRegistry;
|
||||||
const AArgTypes: TArray<IStaticType>
|
const AArgTypes: TArray<IStaticType>;
|
||||||
|
const ALog: ICompilerLog
|
||||||
);
|
);
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
|
|
||||||
@@ -73,6 +72,7 @@ type
|
|||||||
const ParentLayout: IScopeLayout;
|
const ParentLayout: IScopeLayout;
|
||||||
const RootNode: IAstNode;
|
const RootNode: IAstNode;
|
||||||
out Layout: IScopeLayout;
|
out Layout: IScopeLayout;
|
||||||
|
const ALog: ICompilerLog;
|
||||||
const AFunctionRegistry: IFunctionDefinitionRegistry = nil;
|
const AFunctionRegistry: IFunctionDefinitionRegistry = nil;
|
||||||
const AArgTypes: TArray<IStaticType> = nil
|
const AArgTypes: TArray<IStaticType> = nil
|
||||||
): IAstNode; static;
|
): IAstNode; static;
|
||||||
@@ -110,7 +110,8 @@ end;
|
|||||||
constructor TAstBinder.Create(
|
constructor TAstBinder.Create(
|
||||||
const AParentLayout: IScopeLayout;
|
const AParentLayout: IScopeLayout;
|
||||||
const AFunctionRegistry: IFunctionDefinitionRegistry;
|
const AFunctionRegistry: IFunctionDefinitionRegistry;
|
||||||
const AArgTypes: TArray<IStaticType>
|
const AArgTypes: TArray<IStaticType>;
|
||||||
|
const ALog: ICompilerLog
|
||||||
);
|
);
|
||||||
begin
|
begin
|
||||||
inherited Create;
|
inherited Create;
|
||||||
@@ -120,6 +121,7 @@ begin
|
|||||||
|
|
||||||
FFunctionRegistry := AFunctionRegistry;
|
FFunctionRegistry := AFunctionRegistry;
|
||||||
FArgTypes := AArgTypes;
|
FArgTypes := AArgTypes;
|
||||||
|
FLog := ALog;
|
||||||
FBoxedDeclarations := nil;
|
FBoxedDeclarations := nil;
|
||||||
FLambdaCounter := 0;
|
FLambdaCounter := 0;
|
||||||
end;
|
end;
|
||||||
@@ -135,11 +137,12 @@ class function TAstBinder.Bind(
|
|||||||
const ParentLayout: IScopeLayout;
|
const ParentLayout: IScopeLayout;
|
||||||
const RootNode: IAstNode;
|
const RootNode: IAstNode;
|
||||||
out Layout: IScopeLayout;
|
out Layout: IScopeLayout;
|
||||||
|
const ALog: ICompilerLog;
|
||||||
const AFunctionRegistry: IFunctionDefinitionRegistry = nil;
|
const AFunctionRegistry: IFunctionDefinitionRegistry = nil;
|
||||||
const AArgTypes: TArray<IStaticType> = nil
|
const AArgTypes: TArray<IStaticType> = nil
|
||||||
): IAstNode;
|
): IAstNode;
|
||||||
begin
|
begin
|
||||||
var binder := TAstBinder.Create(ParentLayout, AFunctionRegistry, AArgTypes);
|
var binder := TAstBinder.Create(ParentLayout, AFunctionRegistry, AArgTypes, ALog);
|
||||||
try
|
try
|
||||||
Result := binder.Execute(RootNode, Layout);
|
Result := binder.Execute(RootNode, Layout);
|
||||||
finally
|
finally
|
||||||
@@ -239,7 +242,13 @@ begin
|
|||||||
if Node.Address.Kind = akUnresolved then
|
if Node.Address.Kind = akUnresolved then
|
||||||
begin
|
begin
|
||||||
if not ResolveSymbol(Node.Name, physAddr) then
|
if not ResolveSymbol(Node.Name, physAddr) then
|
||||||
raise EBinderException.CreateFmt('Undefined identifier: "%s"', [Node.Name]);
|
begin
|
||||||
|
// Log error but don't crash. Return unresolved node (Poison Pill).
|
||||||
|
if Assigned(FLog) then
|
||||||
|
FLog.AddError(Format('Undefined identifier: "%s"', [Node.Name]), Node);
|
||||||
|
Result := Node;
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
if physAddr.ScopeDepth > 0 then
|
if physAddr.ScopeDepth > 0 then
|
||||||
begin
|
begin
|
||||||
@@ -264,12 +273,28 @@ var
|
|||||||
isBoxed: Boolean;
|
isBoxed: Boolean;
|
||||||
begin
|
begin
|
||||||
var identifier := Node.Target.AsIdentifier;
|
var identifier := Node.Target.AsIdentifier;
|
||||||
if not IsValidIdentifier(identifier.Name) then
|
|
||||||
raise EBinderException.CreateFmt('Invalid identifier name: "%s".', [identifier.Name]);
|
|
||||||
|
|
||||||
// Check if the identifier is already defined in the current scope builder to avoid Scope Asserts.
|
if not IsValidIdentifier(identifier.Name) then
|
||||||
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
|
FLog.AddError(Format('Invalid identifier name: "%s".', [identifier.Name]), Node);
|
||||||
|
// Continue to try processing the initializer
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Check for duplicates before defining
|
||||||
if FCurrentBuilder.FindSlot(identifier.Name) >= 0 then
|
if FCurrentBuilder.FindSlot(identifier.Name) >= 0 then
|
||||||
raise EBinderException.CreateFmt('Variable "%s" is already defined in this scope.', [identifier.Name]);
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
|
FLog.AddError(Format('Variable "%s" is already defined in this scope.', [identifier.Name]), Node);
|
||||||
|
|
||||||
|
// Do NOT define the slot to avoid scope corruption.
|
||||||
|
// Visit initializer to catch errors there, then return original node or dummy.
|
||||||
|
if Assigned(Node.Initializer) then
|
||||||
|
Accept(Node.Initializer);
|
||||||
|
|
||||||
|
Result := Node;
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
slot := FCurrentBuilder.Define(identifier.Name);
|
slot := FCurrentBuilder.Define(identifier.Name);
|
||||||
addr := TResolvedAddress.Create(akLocalOrParent, 0, slot);
|
addr := TResolvedAddress.Create(akLocalOrParent, 0, slot);
|
||||||
@@ -300,6 +325,7 @@ begin
|
|||||||
|
|
||||||
if Assigned(FFunctionRegistry) and (newValue <> nil) and (newValue.Kind = akLambdaExpression) then
|
if Assigned(FFunctionRegistry) and (newValue <> nil) and (newValue.Kind = akLambdaExpression) then
|
||||||
begin
|
begin
|
||||||
|
// Only register if it resolved correctly
|
||||||
if newIdent.AsIdentifier.Address.Kind = akLocalOrParent then
|
if newIdent.AsIdentifier.Address.Kind = akLocalOrParent then
|
||||||
FFunctionRegistry.Register(newIdent.AsIdentifier.Address, newValue.AsLambdaExpression);
|
FFunctionRegistry.Register(newIdent.AsIdentifier.Address, newValue.AsLambdaExpression);
|
||||||
end;
|
end;
|
||||||
@@ -338,18 +364,33 @@ begin
|
|||||||
SetLength(newParams, Length(Node.Parameters));
|
SetLength(newParams, Length(Node.Parameters));
|
||||||
for i := 0 to High(Node.Parameters) do
|
for i := 0 to High(Node.Parameters) do
|
||||||
begin
|
begin
|
||||||
// Parameter names must also be checked for uniqueness, though usually guaranteed by Parser/AST construction
|
var paramName := Node.Parameters[i].Name;
|
||||||
if FCurrentBuilder.FindSlot(Node.Parameters[i].Name) >= 0 then
|
|
||||||
raise EBinderException.CreateFmt('Duplicate parameter name "%s".', [Node.Parameters[i].Name]);
|
// Check duplicate parameters
|
||||||
|
if FCurrentBuilder.FindSlot(paramName) >= 0 then
|
||||||
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
|
FLog.AddError(Format('Duplicate parameter name "%s".', [paramName]), Node);
|
||||||
|
// Skip defining this parameter to avoid scope crash, but create a dummy binding
|
||||||
|
// so index alignment isn't completely broken if we were to continue harder.
|
||||||
|
// Here we just skip definition.
|
||||||
|
end
|
||||||
|
else
|
||||||
|
FCurrentBuilder.Define(paramName);
|
||||||
|
|
||||||
|
// Note: We still create a valid (or attempted) identifier node mapping
|
||||||
|
// even if definition failed, to keep AST shape consistent.
|
||||||
|
slot := FCurrentBuilder.FindSlot(paramName); // might return <0 or previous slot if duplicate
|
||||||
|
if slot < 0 then
|
||||||
|
slot := 0; // Fallback for duplicates
|
||||||
|
|
||||||
slot := FCurrentBuilder.Define(Node.Parameters[i].Name);
|
|
||||||
addr := TResolvedAddress.Create(akLocalOrParent, 0, slot);
|
addr := TResolvedAddress.Create(akLocalOrParent, 0, slot);
|
||||||
|
|
||||||
paramType := TTypes.Unknown;
|
paramType := TTypes.Unknown;
|
||||||
if (parentBuilder.Parent = nil) and (FArgTypes <> nil) and (i < Length(FArgTypes)) then
|
if (parentBuilder.Parent = nil) and (FArgTypes <> nil) and (i < Length(FArgTypes)) then
|
||||||
paramType := FArgTypes[i];
|
paramType := FArgTypes[i];
|
||||||
|
|
||||||
newParams[i] := TAst.Identifier(Node.Parameters[i].Name, addr, paramType);
|
newParams[i] := TAst.Identifier(paramName, addr, paramType);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
newBody := Accept(Node.Body);
|
newBody := Accept(Node.Body);
|
||||||
|
|||||||
@@ -15,9 +15,6 @@ uses
|
|||||||
Myc.Ast;
|
Myc.Ast;
|
||||||
|
|
||||||
type
|
type
|
||||||
// Exception for type mismatches during compilation
|
|
||||||
ETypeCheckException = class(EAstException);
|
|
||||||
|
|
||||||
IAstTypeChecker = interface(IAstVisitor)
|
IAstTypeChecker = interface(IAstVisitor)
|
||||||
function Execute(const RootNode: IAstNode; const Layout: IScopeLayout): IAstNode;
|
function Execute(const RootNode: IAstNode; const Layout: IScopeLayout): IAstNode;
|
||||||
end;
|
end;
|
||||||
@@ -40,6 +37,8 @@ type
|
|||||||
|
|
||||||
private
|
private
|
||||||
FCurrentContext: TTypeContext;
|
FCurrentContext: TTypeContext;
|
||||||
|
FLog: ICompilerLog;
|
||||||
|
|
||||||
function CreateContextChain(L: IScopeLayout): TTypeContext;
|
function CreateContextChain(L: IScopeLayout): TTypeContext;
|
||||||
|
|
||||||
protected
|
protected
|
||||||
@@ -64,12 +63,12 @@ type
|
|||||||
function VisitKeyword(const Node: IKeywordNode): IAstNode; override;
|
function VisitKeyword(const Node: IKeywordNode): IAstNode; override;
|
||||||
|
|
||||||
public
|
public
|
||||||
constructor Create(const RootLayout: IScopeLayout);
|
constructor Create(const RootLayout: IScopeLayout; const ALog: ICompilerLog);
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
|
|
||||||
function Execute(const RootNode: IAstNode; const Layout: IScopeLayout): IAstNode;
|
function Execute(const RootNode: IAstNode; const Layout: IScopeLayout): IAstNode;
|
||||||
|
|
||||||
class function CheckTypes(const RootNode: IAstNode; const Layout: IScopeLayout): IAstNode; static;
|
class function CheckTypes(const RootNode: IAstNode; const Layout: IScopeLayout; const ALog: ICompilerLog): IAstNode; static;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
@@ -107,8 +106,11 @@ begin
|
|||||||
for i := 1 to Address.ScopeDepth do
|
for i := 1 to Address.ScopeDepth do
|
||||||
begin
|
begin
|
||||||
if not Assigned(ctx.FParent) then
|
if not Assigned(ctx.FParent) then
|
||||||
raise ETypeCheckException
|
begin
|
||||||
.CreateFmt('Scope depth mismatch during type lookup. Requested Depth: %d.', [Address.ScopeDepth]);
|
// Should technically not happen if Binder did its job, but safe guard it.
|
||||||
|
Result := TTypes.Unknown;
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
ctx := ctx.FParent;
|
ctx := ctx.FParent;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -148,9 +150,10 @@ begin
|
|||||||
Result := TTypeContext.Create(p, L, []);
|
Result := TTypeContext.Create(p, L, []);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
constructor TTypeChecker.Create(const RootLayout: IScopeLayout);
|
constructor TTypeChecker.Create(const RootLayout: IScopeLayout; const ALog: ICompilerLog);
|
||||||
begin
|
begin
|
||||||
inherited Create;
|
inherited Create;
|
||||||
|
FLog := ALog;
|
||||||
FCurrentContext := CreateContextChain(RootLayout);
|
FCurrentContext := CreateContextChain(RootLayout);
|
||||||
if FCurrentContext = nil then
|
if FCurrentContext = nil then
|
||||||
FCurrentContext := TTypeContext.Create(nil, nil, []);
|
FCurrentContext := TTypeContext.Create(nil, nil, []);
|
||||||
@@ -167,9 +170,9 @@ begin
|
|||||||
inherited;
|
inherited;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
class function TTypeChecker.CheckTypes(const RootNode: IAstNode; const Layout: IScopeLayout): IAstNode;
|
class function TTypeChecker.CheckTypes(const RootNode: IAstNode; const Layout: IScopeLayout; const ALog: ICompilerLog): IAstNode;
|
||||||
begin
|
begin
|
||||||
var checker := TTypeChecker.Create(Layout) as IAstTypeChecker;
|
var checker := TTypeChecker.Create(Layout, ALog) as IAstTypeChecker;
|
||||||
Result := checker.Execute(RootNode, Layout);
|
Result := checker.Execute(RootNode, Layout);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -182,13 +185,11 @@ end;
|
|||||||
|
|
||||||
function TTypeChecker.VisitConstant(const Node: IConstantNode): IAstNode;
|
function TTypeChecker.VisitConstant(const Node: IConstantNode): IAstNode;
|
||||||
begin
|
begin
|
||||||
Assert(Node.StaticType.Kind <> stUnknown);
|
|
||||||
Result := Node;
|
Result := Node;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TTypeChecker.VisitKeyword(const Node: IKeywordNode): IAstNode;
|
function TTypeChecker.VisitKeyword(const Node: IKeywordNode): IAstNode;
|
||||||
begin
|
begin
|
||||||
Assert(Node.StaticType.Kind = stKeyword);
|
|
||||||
Result := Node;
|
Result := Node;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -198,6 +199,14 @@ var
|
|||||||
adr: TResolvedAddress;
|
adr: TResolvedAddress;
|
||||||
begin
|
begin
|
||||||
adr := Node.Address;
|
adr := Node.Address;
|
||||||
|
|
||||||
|
// If binder failed to resolve, we propagate Unknown type to prevent cascading errors.
|
||||||
|
if adr.Kind = akUnresolved then
|
||||||
|
begin
|
||||||
|
Result := TAst.Identifier(Node.Name, adr, TTypes.Unknown);
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
typ := FCurrentContext.LookupType(adr);
|
typ := FCurrentContext.LookupType(adr);
|
||||||
Result := TAst.Identifier(Node.Name, adr, typ);
|
Result := TAst.Identifier(Node.Name, adr, typ);
|
||||||
end;
|
end;
|
||||||
@@ -226,6 +235,15 @@ begin
|
|||||||
adr := Node.Target.AsIdentifier.Address;
|
adr := Node.Target.AsIdentifier.Address;
|
||||||
initType := TTypes.Unknown;
|
initType := TTypes.Unknown;
|
||||||
|
|
||||||
|
// If address is unresolved (binder error), we just process the initializer and return.
|
||||||
|
if adr.Kind = akUnresolved then
|
||||||
|
begin
|
||||||
|
if Assigned(Node.Initializer) then
|
||||||
|
Accept(Node.Initializer);
|
||||||
|
Result := Node;
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
placeholderType := nil;
|
placeholderType := nil;
|
||||||
if (Node.Initializer <> nil) and (Node.Initializer.Kind = akLambdaExpression) then
|
if (Node.Initializer <> nil) and (Node.Initializer.Kind = akLambdaExpression) then
|
||||||
begin
|
begin
|
||||||
@@ -270,6 +288,14 @@ begin
|
|||||||
targetType := newIdent.AsTypedNode.StaticType;
|
targetType := newIdent.AsTypedNode.StaticType;
|
||||||
adr := newIdent.AsIdentifier.Address;
|
adr := newIdent.AsIdentifier.Address;
|
||||||
|
|
||||||
|
// If binder failed, address is unresolved. We still check the value but skip assignment logic.
|
||||||
|
if adr.Kind = akUnresolved then
|
||||||
|
begin
|
||||||
|
Accept(Node.Value);
|
||||||
|
Result := Node;
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
placeholderType := nil;
|
placeholderType := nil;
|
||||||
if (Node.Value <> nil) and (Node.Value.Kind = akLambdaExpression) then
|
if (Node.Value <> nil) and (Node.Value.Kind = akLambdaExpression) then
|
||||||
begin
|
begin
|
||||||
@@ -290,17 +316,18 @@ begin
|
|||||||
newValue := Accept(Node.Value);
|
newValue := Accept(Node.Value);
|
||||||
sourceType := newValue.AsTypedNode.StaticType;
|
sourceType := newValue.AsTypedNode.StaticType;
|
||||||
|
|
||||||
if not TTypeRules.CanAssign(targetType, sourceType) then
|
// Only check assignment if both types are known to avoid cascading error reports
|
||||||
|
if (targetType.Kind <> stUnknown) and (sourceType.Kind <> stUnknown) then
|
||||||
begin
|
begin
|
||||||
if (targetType.Kind = stUnknown) then
|
if not TTypeRules.CanAssign(targetType, sourceType) then
|
||||||
begin
|
begin
|
||||||
if not TTypeRules.CanAssign(sourceType, targetType) then
|
if Assigned(FLog) then
|
||||||
raise ETypeCheckException.CreateFmt('Cannot assign type %s to %s', [sourceType.ToString, targetType.ToString]);
|
FLog.AddError(Format('Cannot assign type %s to %s', [sourceType.ToString, targetType.ToString]), Node);
|
||||||
end
|
// We continue, effectively ignoring the assignment type-wise (variable keeps old type).
|
||||||
else
|
end;
|
||||||
raise ETypeCheckException.CreateFmt('Cannot assign type %s to %s', [sourceType.ToString, targetType.ToString]);
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
// Type Inference for 'Unknown' targets
|
||||||
if ((targetType.Kind = stUnknown) or Assigned(placeholderType)) and (sourceType.Kind <> stUnknown) then
|
if ((targetType.Kind = stUnknown) or Assigned(placeholderType)) and (sourceType.Kind <> stUnknown) then
|
||||||
begin
|
begin
|
||||||
FCurrentContext.SetType(adr.SlotIndex, sourceType);
|
FCurrentContext.SetType(adr.SlotIndex, sourceType);
|
||||||
@@ -337,14 +364,20 @@ begin
|
|||||||
var paramIdent := Node.Parameters[i];
|
var paramIdent := Node.Parameters[i];
|
||||||
var paramAdr := paramIdent.Address;
|
var paramAdr := paramIdent.Address;
|
||||||
paramTypes[i] := TTypes.Unknown;
|
paramTypes[i] := TTypes.Unknown;
|
||||||
FCurrentContext.SetType(paramAdr.SlotIndex, paramTypes[i]);
|
|
||||||
|
if paramAdr.Kind <> akUnresolved then
|
||||||
|
FCurrentContext.SetType(paramAdr.SlotIndex, paramTypes[i]);
|
||||||
|
|
||||||
newParams[i] := TAst.Identifier(paramIdent.Name, paramAdr, paramTypes[i]);
|
newParams[i] := TAst.Identifier(paramIdent.Name, paramAdr, paramTypes[i]);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
newBody := Accept(Node.Body);
|
newBody := Accept(Node.Body);
|
||||||
bodyType := newBody.AsTypedNode.StaticType;
|
bodyType := newBody.AsTypedNode.StaticType;
|
||||||
methodType := TTypes.CreateMethod(paramTypes, bodyType);
|
methodType := TTypes.CreateMethod(paramTypes, bodyType);
|
||||||
|
|
||||||
|
// Set self-reference type (slot 0 is <self>)
|
||||||
FCurrentContext.SetType(0, methodType);
|
FCurrentContext.SetType(0, methodType);
|
||||||
|
|
||||||
finalDescriptor := TScope.CreateDescriptor(Node.Layout, FCurrentContext.Types);
|
finalDescriptor := TScope.CreateDescriptor(Node.Layout, FCurrentContext.Types);
|
||||||
finally
|
finally
|
||||||
var temp := FCurrentContext;
|
var temp := FCurrentContext;
|
||||||
@@ -367,6 +400,7 @@ var
|
|||||||
bestSig: IMethodSignature;
|
bestSig: IMethodSignature;
|
||||||
sig: IMethodSignature;
|
sig: IMethodSignature;
|
||||||
match: Boolean;
|
match: Boolean;
|
||||||
|
argsStr: string;
|
||||||
begin
|
begin
|
||||||
newCallee := Accept(Node.Callee);
|
newCallee := Accept(Node.Callee);
|
||||||
SetLength(newArgs, Length(Node.Arguments));
|
SetLength(newArgs, Length(Node.Arguments));
|
||||||
@@ -415,16 +449,34 @@ begin
|
|||||||
retType := bestSig.ReturnType
|
retType := bestSig.ReturnType
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
var argsStr: string := '';
|
// Log error, but don't crash
|
||||||
for i := 0 to High(argTypes) do
|
if Assigned(FLog) then
|
||||||
argsStr := argsStr + argTypes[i].ToString + ' ';
|
begin
|
||||||
raise ETypeCheckException
|
argsStr := '';
|
||||||
.CreateFmt('No matching signature for call with args (%s) found on method %s', [argsStr, calleeType.ToString]);
|
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; // Poison result
|
||||||
end;
|
end;
|
||||||
|
end
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
// We have unknown arguments (probably due to upstream errors).
|
||||||
|
// We cannot resolve the signature, so the result is Unknown.
|
||||||
|
// We do NOT log an error here to avoid spam.
|
||||||
|
retType := TTypes.Unknown;
|
||||||
end;
|
end;
|
||||||
end
|
end
|
||||||
else if calleeType.Kind <> TStaticTypeKind.stUnknown then
|
else if calleeType.Kind <> TStaticTypeKind.stUnknown then
|
||||||
raise ETypeCheckException.CreateFmt('Cannot invoke type %s as a function.', [calleeType.ToString]);
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
|
FLog.AddError(Format('Cannot invoke type %s as a function.', [calleeType.ToString]), Node);
|
||||||
|
retType := TTypes.Unknown;
|
||||||
|
end;
|
||||||
|
|
||||||
Result := TAst.FunctionCall(newCallee, newArgs, retType, Node.IsTailCall);
|
Result := TAst.FunctionCall(newCallee, newArgs, retType, Node.IsTailCall);
|
||||||
end;
|
end;
|
||||||
@@ -457,10 +509,14 @@ begin
|
|||||||
newElse := Accept(Node.ElseBranch);
|
newElse := Accept(Node.ElseBranch);
|
||||||
|
|
||||||
conditionType := newCond.AsTypedNode.StaticType;
|
conditionType := newCond.AsTypedNode.StaticType;
|
||||||
|
|
||||||
if (conditionType.Kind <> stUnknown)
|
if (conditionType.Kind <> stUnknown)
|
||||||
and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType)
|
and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType)
|
||||||
and not TTypeRules.CanAssign(TTypes.Boolean, conditionType) then
|
and not TTypeRules.CanAssign(TTypes.Boolean, conditionType) then
|
||||||
raise ETypeCheckException.CreateFmt('If condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]);
|
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;
|
thenType := newThen.AsTypedNode.StaticType;
|
||||||
elseType :=
|
elseType :=
|
||||||
@@ -469,7 +525,11 @@ begin
|
|||||||
|
|
||||||
resultType := TTypeRules.Promote(thenType, elseType);
|
resultType := TTypeRules.Promote(thenType, elseType);
|
||||||
if resultType = nil then
|
if resultType = nil then
|
||||||
raise ETypeCheckException.CreateFmt('Cannot promote types %s and %s in If-Expression', [thenType.ToString, elseType.ToString]);
|
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(newCond, newThen, newElse, resultType);
|
Result := TAst.IfExpr(newCond, newThen, newElse, resultType);
|
||||||
end;
|
end;
|
||||||
@@ -487,14 +547,21 @@ begin
|
|||||||
if (conditionType.Kind <> stUnknown)
|
if (conditionType.Kind <> stUnknown)
|
||||||
and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType)
|
and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType)
|
||||||
and not TTypeRules.CanAssign(TTypes.Boolean, conditionType) then
|
and not TTypeRules.CanAssign(TTypes.Boolean, conditionType) then
|
||||||
raise ETypeCheckException.CreateFmt('Ternary condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]);
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
|
FLog.AddError(Format('Ternary condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]), Node);
|
||||||
|
end;
|
||||||
|
|
||||||
thenType := newThen.AsTypedNode.StaticType;
|
thenType := newThen.AsTypedNode.StaticType;
|
||||||
elseType := newElse.AsTypedNode.StaticType;
|
elseType := newElse.AsTypedNode.StaticType;
|
||||||
|
|
||||||
resultType := TTypeRules.Promote(thenType, elseType);
|
resultType := TTypeRules.Promote(thenType, elseType);
|
||||||
if resultType = nil then
|
if resultType = nil then
|
||||||
raise ETypeCheckException.CreateFmt('Cannot promote types %s and %s in Ternary-Expression', [thenType.ToString, elseType.ToString]);
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
|
FLog.AddError(Format('Cannot promote types %s and %s in Ternary-Expression', [thenType.ToString, elseType.ToString]), Node);
|
||||||
|
resultType := TTypes.Unknown;
|
||||||
|
end;
|
||||||
|
|
||||||
Result := TAst.TernaryExpr(newCond, newThen, newElse, resultType);
|
Result := TAst.TernaryExpr(newCond, newThen, newElse, resultType);
|
||||||
end;
|
end;
|
||||||
@@ -517,25 +584,36 @@ begin
|
|||||||
begin
|
begin
|
||||||
fieldIndex := baseType.Definition.IndexOf(Node.Member.Value);
|
fieldIndex := baseType.Definition.IndexOf(Node.Member.Value);
|
||||||
if fieldIndex < 0 then
|
if fieldIndex < 0 then
|
||||||
raise ETypeCheckException.CreateFmt('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]);
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
var fieldType := TTypes.FromScalarKind(baseType.Definition.Fields[fieldIndex].Value);
|
FLog.AddError(Format('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]), Node);
|
||||||
|
end
|
||||||
if baseType.Kind = TStaticTypeKind.stRecord then
|
|
||||||
elemType := fieldType
|
|
||||||
else
|
else
|
||||||
elemType := TTypes.CreateSeries(fieldType);
|
begin
|
||||||
|
var fieldType := TTypes.FromScalarKind(baseType.Definition.Fields[fieldIndex].Value);
|
||||||
|
if baseType.Kind = TStaticTypeKind.stRecord then
|
||||||
|
elemType := fieldType
|
||||||
|
else
|
||||||
|
elemType := TTypes.CreateSeries(fieldType);
|
||||||
|
end;
|
||||||
end
|
end
|
||||||
else if (baseType.Kind = TStaticTypeKind.stGenericRecord) then
|
else if (baseType.Kind = TStaticTypeKind.stGenericRecord) then
|
||||||
begin
|
begin
|
||||||
var genDef := baseType.GenericDefinition;
|
var genDef := baseType.GenericDefinition;
|
||||||
fieldIndex := genDef.IndexOf(Node.Member.Value);
|
fieldIndex := genDef.IndexOf(Node.Member.Value);
|
||||||
if fieldIndex < 0 then
|
if fieldIndex < 0 then
|
||||||
raise ETypeCheckException.CreateFmt('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]);
|
begin
|
||||||
elemType := genDef.Fields[fieldIndex].Value;
|
if Assigned(FLog) then
|
||||||
|
FLog.AddError(Format('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]), Node);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
elemType := genDef.Fields[fieldIndex].Value;
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise ETypeCheckException.CreateFmt('Member access requires a record type, but got %s', [baseType.ToString]);
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
|
FLog.AddError(Format('Member access requires a record type, but got %s', [baseType.ToString]), Node);
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Result := TAst.MemberAccess(newBase, newMember.AsKeyword, elemType);
|
Result := TAst.MemberAccess(newBase, newMember.AsKeyword, elemType);
|
||||||
@@ -556,15 +634,23 @@ begin
|
|||||||
if (baseType.Kind <> TStaticTypeKind.stUnknown) then
|
if (baseType.Kind <> TStaticTypeKind.stUnknown) then
|
||||||
begin
|
begin
|
||||||
if (baseType.Kind <> TStaticTypeKind.stSeries) and (baseType.Kind <> TStaticTypeKind.stRecordSeries) then
|
if (baseType.Kind <> TStaticTypeKind.stSeries) and (baseType.Kind <> TStaticTypeKind.stRecordSeries) then
|
||||||
raise ETypeCheckException.CreateFmt('Indexer `[]` can only be applied to series types, but got %s', [baseType.ToString]);
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
if (indexType.Kind <> stUnknown) and not TTypeRules.CanAssign(TTypes.Ordinal, indexType) then
|
FLog.AddError(Format('Indexer `[]` can only be applied to series types, but got %s', [baseType.ToString]), Node);
|
||||||
raise ETypeCheckException.CreateFmt('Indexer `[]` requires an Ordinal index, but got %s', [indexType.ToString]);
|
end
|
||||||
|
|
||||||
if baseType.Kind = TStaticTypeKind.stSeries then
|
|
||||||
elemType := baseType.ElementType
|
|
||||||
else
|
else
|
||||||
elemType := TTypes.CreateRecord(baseType.Definition);
|
begin
|
||||||
|
if baseType.Kind = TStaticTypeKind.stSeries then
|
||||||
|
elemType := baseType.ElementType
|
||||||
|
else
|
||||||
|
elemType := TTypes.CreateRecord(baseType.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;
|
end;
|
||||||
|
|
||||||
Result := TAst.Indexer(newBase, newIndex, elemType);
|
Result := TAst.Indexer(newBase, newIndex, elemType);
|
||||||
@@ -639,12 +725,14 @@ var
|
|||||||
elemType: IStaticType;
|
elemType: IStaticType;
|
||||||
begin
|
begin
|
||||||
try
|
try
|
||||||
// TScalar.StringToKind might throw EConvertError or generic Exception if string is invalid
|
|
||||||
elemType := TTypes.FromScalarKind(TScalar.StringToKind(Node.Definition));
|
elemType := TTypes.FromScalarKind(TScalar.StringToKind(Node.Definition));
|
||||||
except
|
except
|
||||||
on E: Exception do
|
on E: Exception do
|
||||||
// Instead of silently using Unknown, we now raise a proper compiler error
|
begin
|
||||||
raise ETypeCheckException.CreateFmt('Invalid type definition "%s" in new-series.', [Node.Definition]);
|
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;
|
end;
|
||||||
|
|
||||||
Result := TAst.CreateSeries(Node.Definition, TTypes.CreateSeries(elemType));
|
Result := TAst.CreateSeries(Node.Definition, TTypes.CreateSeries(elemType));
|
||||||
@@ -665,18 +753,32 @@ begin
|
|||||||
if (seriesType.Kind <> stUnknown) then
|
if (seriesType.Kind <> stUnknown) then
|
||||||
begin
|
begin
|
||||||
if (seriesType.Kind <> TStaticTypeKind.stSeries) then
|
if (seriesType.Kind <> TStaticTypeKind.stSeries) then
|
||||||
raise ETypeCheckException.CreateFmt('"add" requires a series as its first argument, but got %s', [seriesType.ToString]);
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
if not TTypeRules.CanAssign(seriesType.ElementType, valueType) then
|
FLog.AddError(Format('"add" requires a series as its first argument, but got %s', [seriesType.ToString]), Node);
|
||||||
raise ETypeCheckException
|
end
|
||||||
.CreateFmt('Cannot add item of type %s to series of type %s', [valueType.ToString, seriesType.ElementType.ToString]);
|
else
|
||||||
|
begin
|
||||||
|
// Only check assignment compatibility if value type is known
|
||||||
|
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;
|
end;
|
||||||
|
|
||||||
if (newLookback <> nil) then
|
if (newLookback <> nil) then
|
||||||
begin
|
begin
|
||||||
var lookbackType := newLookback.AsTypedNode.StaticType;
|
var lookbackType := newLookback.AsTypedNode.StaticType;
|
||||||
if (lookbackType.Kind <> stUnknown) and not (lookbackType.Kind = TStaticTypeKind.stOrdinal) then
|
if (lookbackType.Kind <> stUnknown) and not (lookbackType.Kind = TStaticTypeKind.stOrdinal) then
|
||||||
raise ETypeCheckException.Create('Lookback parameter for "add" must be an ordinal value.');
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
|
FLog.AddError('Lookback parameter for "add" must be an ordinal value.', Node);
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Result := TAst.AddSeriesItem(newSeries.AsIdentifier, newValue, newLookback, TTypes.Void);
|
Result := TAst.AddSeriesItem(newSeries.AsIdentifier, newValue, newLookback, TTypes.Void);
|
||||||
@@ -698,7 +800,10 @@ begin
|
|||||||
if (seriesType.Kind <> stUnknown)
|
if (seriesType.Kind <> stUnknown)
|
||||||
and (seriesType.Kind <> TStaticTypeKind.stSeries)
|
and (seriesType.Kind <> TStaticTypeKind.stSeries)
|
||||||
and (seriesType.Kind <> TStaticTypeKind.stRecordSeries) then
|
and (seriesType.Kind <> TStaticTypeKind.stRecordSeries) then
|
||||||
raise ETypeCheckException.CreateFmt('"length" requires a series, but got %s', [seriesType.ToString]);
|
begin
|
||||||
|
if Assigned(FLog) then
|
||||||
|
FLog.AddError(Format('"length" requires a series, but got %s', [seriesType.ToString]), Node);
|
||||||
|
end;
|
||||||
|
|
||||||
Result := TAst.SeriesLength(newSeries.AsIdentifier, TTypes.Ordinal);
|
Result := TAst.SeriesLength(newSeries.AsIdentifier, TTypes.Ordinal);
|
||||||
end;
|
end;
|
||||||
|
|||||||
@@ -39,7 +39,15 @@ type
|
|||||||
procedure SetExecutionStrategy(const AStrategy: IExecutionStrategy);
|
procedure SetExecutionStrategy(const AStrategy: IExecutionStrategy);
|
||||||
|
|
||||||
function ExpandMacros(const Node: IAstNode): IAstNode;
|
function ExpandMacros(const Node: IAstNode): IAstNode;
|
||||||
function Bind(const Node: IAstNode; out Layout: IScopeLayout; const AArgTypes: TArray<IStaticType> = []): IAstNode;
|
|
||||||
|
// Updated Bind signature to accept Log
|
||||||
|
function Bind(
|
||||||
|
const Node: IAstNode;
|
||||||
|
out Layout: IScopeLayout;
|
||||||
|
const AArgTypes: TArray<IStaticType>;
|
||||||
|
const Log: ICompilerLog
|
||||||
|
): IAstNode;
|
||||||
|
|
||||||
function Specialize(const Node: IAstNode): IAstNode;
|
function Specialize(const Node: IAstNode): IAstNode;
|
||||||
|
|
||||||
function Compile(const Node: IFunctionDefinition; const ArgTypes: TArray<IStaticType> = []): TCompiledFunction;
|
function Compile(const Node: IFunctionDefinition; const ArgTypes: TArray<IStaticType> = []): TCompiledFunction;
|
||||||
@@ -162,7 +170,14 @@ type
|
|||||||
procedure SetExecutionStrategy(const AStrategy: IExecutionStrategy);
|
procedure SetExecutionStrategy(const AStrategy: IExecutionStrategy);
|
||||||
|
|
||||||
function ExpandMacros(const Node: IAstNode): IAstNode;
|
function ExpandMacros(const Node: IAstNode): IAstNode;
|
||||||
function Bind(const Node: IAstNode; out Layout: IScopeLayout; const ArgTypes: TArray<IStaticType>): IAstNode;
|
|
||||||
|
function Bind(
|
||||||
|
const Node: IAstNode;
|
||||||
|
out Layout: IScopeLayout;
|
||||||
|
const ArgTypes: TArray<IStaticType>;
|
||||||
|
const Log: ICompilerLog
|
||||||
|
): IAstNode;
|
||||||
|
|
||||||
function Specialize(const Node: IAstNode): IAstNode;
|
function Specialize(const Node: IAstNode): IAstNode;
|
||||||
|
|
||||||
function Compile(const Node: IFunctionDefinition; const ArgTypes: TArray<IStaticType>): TCompiledFunction; overload;
|
function Compile(const Node: IFunctionDefinition; const ArgTypes: TArray<IStaticType>): TCompiledFunction; overload;
|
||||||
@@ -227,8 +242,6 @@ end;
|
|||||||
procedure TAstEnvironment.Define(const Name: String; const AScript: IAstNode);
|
procedure TAstEnvironment.Define(const Name: String; const AScript: IAstNode);
|
||||||
begin
|
begin
|
||||||
var compiled := Compile(AScript);
|
var compiled := Compile(AScript);
|
||||||
// Note: Define now potentially handles Pure flags internally via the Node metadata,
|
|
||||||
// but the runtime value is just the TDataValue.
|
|
||||||
RootScope.Define(Name, compiled.Func([]), compiled.StaticType);
|
RootScope.Define(Name, compiled.Func([]), compiled.StaticType);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@@ -318,13 +331,18 @@ begin
|
|||||||
Result := FMonomorphCache;
|
Result := FMonomorphCache;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TEnvironment.Bind(const Node: IAstNode; out Layout: IScopeLayout; const ArgTypes: TArray<IStaticType>): IAstNode;
|
function TEnvironment.Bind(
|
||||||
|
const Node: IAstNode;
|
||||||
|
out Layout: IScopeLayout;
|
||||||
|
const ArgTypes: TArray<IStaticType>;
|
||||||
|
const Log: ICompilerLog
|
||||||
|
): IAstNode;
|
||||||
begin
|
begin
|
||||||
// 1. Bind (produces Layout and Bound AST with Addresses)
|
// 1. Bind (produces Layout and Bound AST with Addresses)
|
||||||
var boundAst := TAstBinder.Bind(FRootScope.Descriptor.Layout, Node, Layout, FFunctionRegistry, ArgTypes);
|
var boundAst := TAstBinder.Bind(FRootScope.Descriptor.Layout, Node, Layout, Log, FFunctionRegistry, ArgTypes);
|
||||||
|
|
||||||
// 2. Check Types (produces Typed AST with Descriptors baked into nodes)
|
// 2. Check Types (produces Typed AST with Descriptors baked into nodes)
|
||||||
Result := TTypeChecker.CheckTypes(boundAst, Layout);
|
Result := TTypeChecker.CheckTypes(boundAst, Layout, Log);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TEnvironment.GetMacroRegistry: IMacroRegistry;
|
function TEnvironment.GetMacroRegistry: IMacroRegistry;
|
||||||
@@ -356,21 +374,38 @@ var
|
|||||||
|
|
||||||
typedNode, specialized, tcoOptimized: IAstNode;
|
typedNode, specialized, tcoOptimized: IAstNode;
|
||||||
isPure: Boolean;
|
isPure: Boolean;
|
||||||
begin
|
|
||||||
// Same steps as above, but Node is already a definition (Lambda)
|
|
||||||
var expanded := ExpandMacros(Node);
|
|
||||||
typedNode := Bind(expanded, layout, ArgTypes);
|
|
||||||
|
|
||||||
|
log: ICompilerLog;
|
||||||
|
begin
|
||||||
|
log := TCompilerLog.Create;
|
||||||
|
|
||||||
|
// 1. Expand Macros
|
||||||
|
var expanded := ExpandMacros(Node);
|
||||||
|
|
||||||
|
// 2. Bind & TypeCheck (accumulating errors)
|
||||||
|
typedNode := Bind(expanded, layout, ArgTypes, log);
|
||||||
|
|
||||||
|
// 3. Check for compilation errors
|
||||||
|
if log.HasErrors then
|
||||||
|
raise ECompilationFailed.Create(log.GetEntries);
|
||||||
|
|
||||||
|
// Note: If types are Unknown but no errors were logged (edge case), we proceed.
|
||||||
|
// But Binder/TypeChecker logic ensures errors are logged for Unknowns that matter.
|
||||||
|
|
||||||
|
// 4. Specialization & Optimization
|
||||||
descriptor := typedNode.AsLambdaExpression.Descriptor;
|
descriptor := typedNode.AsLambdaExpression.Descriptor;
|
||||||
|
// Descriptor might be nil if Bind failed badly, but HasErrors check above covers that.
|
||||||
Assert(Assigned(descriptor));
|
Assert(Assigned(descriptor));
|
||||||
|
|
||||||
funcType := typedNode.AsTypedNode.StaticType;
|
funcType := typedNode.AsTypedNode.StaticType;
|
||||||
|
|
||||||
specialized := Specialize(typedNode);
|
specialized := Specialize(typedNode);
|
||||||
tcoOptimized := TAstTCO.Optimize(specialized);
|
tcoOptimized := TAstTCO.Optimize(specialized);
|
||||||
|
|
||||||
// Purity Inference
|
// 5. Purity Inference
|
||||||
isPure := TPurityAnalyzer.IsPure(tcoOptimized.AsLambdaExpression.Body);
|
isPure := TPurityAnalyzer.IsPure(tcoOptimized.AsLambdaExpression.Body);
|
||||||
|
|
||||||
|
// 6. Generate Visitor/Closure
|
||||||
var visitor := FExecutionStrategy.CreateVisitor(descriptor.CreateScope(FRootScope));
|
var visitor := FExecutionStrategy.CreateVisitor(descriptor.CreateScope(FRootScope));
|
||||||
var closure := tcoOptimized.Accept(visitor);
|
var closure := tcoOptimized.Accept(visitor);
|
||||||
|
|
||||||
@@ -397,10 +432,18 @@ begin
|
|||||||
evaluator: IEvaluatorVisitor;
|
evaluator: IEvaluatorVisitor;
|
||||||
boundSubAst: IAstNode;
|
boundSubAst: IAstNode;
|
||||||
scratchScope: IExecutionScope;
|
scratchScope: IExecutionScope;
|
||||||
|
tempLog: ICompilerLog;
|
||||||
begin
|
begin
|
||||||
|
// Create temporary log for the macro context
|
||||||
|
tempLog := TCompilerLog.Create;
|
||||||
|
|
||||||
// 1. Binding
|
// 1. Binding
|
||||||
// Note: Scope is dynamic (from TMacroExpander), so Descriptor.Layout describes current variables.
|
// Note: Scope is dynamic (from TMacroExpander), so Descriptor.Layout describes current variables.
|
||||||
boundSubAst := TAstBinder.Bind(Scope.Descriptor.Layout, ANode, tmpLayout);
|
boundSubAst := TAstBinder.Bind(Scope.Descriptor.Layout, ANode, tmpLayout, tempLog);
|
||||||
|
|
||||||
|
// Macro execution is strictly fail-fast. If we can't bind arguments, we can't run the macro.
|
||||||
|
if tempLog.HasErrors then
|
||||||
|
raise EMacroException.Create('Macro Argument Error: ' + tempLog.GetEntries[0].Message);
|
||||||
|
|
||||||
// 2. Scope Matching
|
// 2. Scope Matching
|
||||||
// Create a child scope to ensure correct depth resolution (Binder sees Layout at depth 0 relative to itself)
|
// Create a child scope to ensure correct depth resolution (Binder sees Layout at depth 0 relative to itself)
|
||||||
@@ -411,13 +454,7 @@ begin
|
|||||||
Result := evaluator.Execute(boundSubAst);
|
Result := evaluator.Execute(boundSubAst);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
Result :=
|
Result := TMacroExpander.ExpandMacros(FMacroRegistry, FRootScope, Node, macroEvaluator);
|
||||||
TMacroExpander.ExpandMacros(
|
|
||||||
FMacroRegistry,
|
|
||||||
FRootScope,
|
|
||||||
Node,
|
|
||||||
macroEvaluator // Injected single dependency
|
|
||||||
);
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TEnvironment.GetFunctionRegistry: IFunctionDefinitionRegistry;
|
function TEnvironment.GetFunctionRegistry: IFunctionDefinitionRegistry;
|
||||||
|
|||||||
+167
-8
@@ -12,9 +12,70 @@ uses
|
|||||||
Myc.Ast.Types;
|
Myc.Ast.Types;
|
||||||
|
|
||||||
type
|
type
|
||||||
// --- Forward Declarations to break cycles ---
|
// --- Forward Declarations ---
|
||||||
IAstVisitor = interface;
|
|
||||||
IAstNode = interface;
|
IAstNode = interface;
|
||||||
|
|
||||||
|
// --- Error Handling Infrastructure ---
|
||||||
|
|
||||||
|
TCompilerErrorLevel = (elHint, elWarning, elError);
|
||||||
|
|
||||||
|
TCompilerError = record
|
||||||
|
Level: TCompilerErrorLevel;
|
||||||
|
Message: string;
|
||||||
|
Node: IAstNode; // Context node where the error occurred
|
||||||
|
constructor Create(ALevel: TCompilerErrorLevel; const AMessage: string; const ANode: IAstNode = nil);
|
||||||
|
function ToString: string;
|
||||||
|
end;
|
||||||
|
|
||||||
|
ICompilerLog = interface
|
||||||
|
procedure Add(ALevel: TCompilerErrorLevel; const AMessage: string; const ANode: IAstNode = nil);
|
||||||
|
procedure AddError(const AMessage: string; const ANode: IAstNode = nil);
|
||||||
|
procedure AddWarning(const AMessage: string; const ANode: IAstNode = nil);
|
||||||
|
|
||||||
|
function HasErrors: Boolean;
|
||||||
|
function GetEntryCount: Integer;
|
||||||
|
function GetEntries: TArray<TCompilerError>;
|
||||||
|
|
||||||
|
property Entries: TArray<TCompilerError> read GetEntries;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// Standard implementation of the log
|
||||||
|
TCompilerLog = class(TInterfacedObject, ICompilerLog)
|
||||||
|
private
|
||||||
|
FEntries: TList<TCompilerError>;
|
||||||
|
public
|
||||||
|
constructor Create;
|
||||||
|
destructor Destroy; override;
|
||||||
|
procedure Add(ALevel: TCompilerErrorLevel; const AMessage: string; const ANode: IAstNode = nil);
|
||||||
|
procedure AddError(const AMessage: string; const ANode: IAstNode = nil);
|
||||||
|
procedure AddWarning(const AMessage: string; const ANode: IAstNode = nil);
|
||||||
|
function HasErrors: Boolean;
|
||||||
|
function GetEntryCount: Integer;
|
||||||
|
function GetEntries: TArray<TCompilerError>;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// 1. The abstract base class for all Compiler related exceptions.
|
||||||
|
// Catching this will catch compilation failures AND internal logic errors.
|
||||||
|
EAstException = class(Exception);
|
||||||
|
|
||||||
|
// 2. The concrete exception thrown at the END of a compilation pass if errors occurred.
|
||||||
|
// It wraps the accumulated log.
|
||||||
|
ECompilationFailed = class(EAstException)
|
||||||
|
private
|
||||||
|
FErrors: TArray<TCompilerError>;
|
||||||
|
public
|
||||||
|
constructor Create(const AErrors: TArray<TCompilerError>);
|
||||||
|
function ToString: string; override;
|
||||||
|
property Errors: TArray<TCompilerError> read FErrors;
|
||||||
|
end;
|
||||||
|
|
||||||
|
// 3. (Optional) Exception for critical internal bugs (e.g. implementation defects).
|
||||||
|
// This should ideally never happen in production.
|
||||||
|
EInternalCompilerError = class(EAstException);
|
||||||
|
|
||||||
|
// --- AST Interfaces ---
|
||||||
|
|
||||||
|
IAstVisitor = interface;
|
||||||
IFunctionDefinition = interface;
|
IFunctionDefinition = interface;
|
||||||
IIdentifierNode = interface;
|
IIdentifierNode = interface;
|
||||||
IConstantNode = interface;
|
IConstantNode = interface;
|
||||||
@@ -158,7 +219,7 @@ type
|
|||||||
{$region 'private'}
|
{$region 'private'}
|
||||||
function GetBody: IAstNode;
|
function GetBody: IAstNode;
|
||||||
function GetParameters: TArray<IIdentifierNode>;
|
function GetParameters: TArray<IIdentifierNode>;
|
||||||
function GetIsPure: Boolean; // (* ADDED *)
|
function GetIsPure: Boolean;
|
||||||
{$endregion}
|
{$endregion}
|
||||||
property Body: IAstNode read GetBody;
|
property Body: IAstNode read GetBody;
|
||||||
property Parameters: TArray<IIdentifierNode> read GetParameters;
|
property Parameters: TArray<IIdentifierNode> read GetParameters;
|
||||||
@@ -249,7 +310,7 @@ type
|
|||||||
function GetArguments: TArray<IAstNode>;
|
function GetArguments: TArray<IAstNode>;
|
||||||
function GetIsTailCall: Boolean;
|
function GetIsTailCall: Boolean;
|
||||||
function GetStaticTarget: TDataValue.TFunc;
|
function GetStaticTarget: TDataValue.TFunc;
|
||||||
function GetIsTargetPure: Boolean; // (* ADDED *)
|
function GetIsTargetPure: Boolean;
|
||||||
{$endregion}
|
{$endregion}
|
||||||
property Callee: IAstNode read GetCallee;
|
property Callee: IAstNode read GetCallee;
|
||||||
property Arguments: TArray<IAstNode> read GetArguments;
|
property Arguments: TArray<IAstNode> read GetArguments;
|
||||||
@@ -401,10 +462,6 @@ type
|
|||||||
// A factory for creating visitors
|
// A factory for creating visitors
|
||||||
TEvaluatorFactory = reference to function(const Scope: IExecutionScope): IEvaluatorVisitor;
|
TEvaluatorFactory = reference to function(const Scope: IExecutionScope): IEvaluatorVisitor;
|
||||||
|
|
||||||
// Base class for ALL compiler exceptions.
|
|
||||||
// Renamed from IAstException to EAstException to follow standard Delphi naming convention.
|
|
||||||
EAstException = class(Exception);
|
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
uses
|
uses
|
||||||
@@ -429,4 +486,106 @@ begin
|
|||||||
Value := AValue;
|
Value := AValue;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
{ TCompilerError }
|
||||||
|
|
||||||
|
constructor TCompilerError.Create(ALevel: TCompilerErrorLevel; const AMessage: string; const ANode: IAstNode);
|
||||||
|
begin
|
||||||
|
Level := ALevel;
|
||||||
|
Message := AMessage;
|
||||||
|
Node := ANode;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TCompilerError.ToString: string;
|
||||||
|
begin
|
||||||
|
// Simplified string representation for quick debugging
|
||||||
|
case Level of
|
||||||
|
elHint: Result := '[Hint] ';
|
||||||
|
elWarning: Result := '[Warning] ';
|
||||||
|
elError: Result := '[Error] ';
|
||||||
|
end;
|
||||||
|
Result := Result + Message;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TCompilerLog }
|
||||||
|
|
||||||
|
constructor TCompilerLog.Create;
|
||||||
|
begin
|
||||||
|
inherited Create;
|
||||||
|
FEntries := TList<TCompilerError>.Create;
|
||||||
|
end;
|
||||||
|
|
||||||
|
destructor TCompilerLog.Destroy;
|
||||||
|
begin
|
||||||
|
FEntries.Free;
|
||||||
|
inherited;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TCompilerLog.Add(ALevel: TCompilerErrorLevel; const AMessage: string; const ANode: IAstNode);
|
||||||
|
begin
|
||||||
|
FEntries.Add(TCompilerError.Create(ALevel, AMessage, ANode));
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TCompilerLog.AddError(const AMessage: string; const ANode: IAstNode);
|
||||||
|
begin
|
||||||
|
Add(elError, AMessage, ANode);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TCompilerLog.AddWarning(const AMessage: string; const ANode: IAstNode);
|
||||||
|
begin
|
||||||
|
Add(elWarning, AMessage, ANode);
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TCompilerLog.HasErrors: Boolean;
|
||||||
|
var
|
||||||
|
entry: TCompilerError;
|
||||||
|
begin
|
||||||
|
Result := False;
|
||||||
|
for entry in FEntries do
|
||||||
|
if entry.Level = elError then
|
||||||
|
Exit(True);
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TCompilerLog.GetEntryCount: Integer;
|
||||||
|
begin
|
||||||
|
Result := FEntries.Count;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TCompilerLog.GetEntries: TArray<TCompilerError>;
|
||||||
|
begin
|
||||||
|
Result := FEntries.ToArray;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ ECompilationFailed }
|
||||||
|
|
||||||
|
constructor ECompilationFailed.Create(const AErrors: TArray<TCompilerError>);
|
||||||
|
var
|
||||||
|
msg: string;
|
||||||
|
begin
|
||||||
|
if Length(AErrors) > 0 then
|
||||||
|
msg := Format('Compilation failed with %d error(s). First: %s', [Length(AErrors), AErrors[0].Message])
|
||||||
|
else
|
||||||
|
msg := 'Compilation failed with unspecified errors.';
|
||||||
|
inherited Create(msg);
|
||||||
|
FErrors := AErrors;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function ECompilationFailed.ToString: string;
|
||||||
|
var
|
||||||
|
sb: TStringBuilder;
|
||||||
|
err: TCompilerError;
|
||||||
|
begin
|
||||||
|
sb := TStringBuilder.Create;
|
||||||
|
try
|
||||||
|
sb.AppendLine(Message);
|
||||||
|
for err in FErrors do
|
||||||
|
begin
|
||||||
|
sb.Append(' - ');
|
||||||
|
sb.AppendLine(err.ToString);
|
||||||
|
end;
|
||||||
|
Result := sb.ToString;
|
||||||
|
finally
|
||||||
|
sb.Free;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|||||||
Reference in New Issue
Block a user