Compiler exceptions

This commit is contained in:
Michael Schimmel
2025-11-25 15:27:57 +01:00
parent d84509c034
commit 4e508d90a5
8 changed files with 62 additions and 28 deletions
+17 -4
View File
@@ -8,13 +8,16 @@ uses
System.Generics.Collections,
Myc.Data.Scalar,
Myc.Data.Value,
Myc.Ast.Nodes,
Myc.Ast.Nodes, // Provides EAstException
Myc.Ast.Visitor,
Myc.Ast.Scope,
Myc.Ast.Types,
Myc.Ast;
type
// Exception specific to AST lowering/rewriting errors
ELoweringException = class(EAstException);
IAstLowerer = interface(IAstVisitor)
function Execute(const RootNode: IAstNode): IAstNode;
end;
@@ -27,6 +30,8 @@ type
FBinaryOperators: TDictionary<string, TScalar.TBinaryOp>;
FUnaryOperators: TDictionary<string, TScalar.TUnaryOp>;
protected
// Implement Visit methods here when specific lowering logic is added
// (e.g. converting operator function calls to specific node types if added in future)
public
constructor Create;
destructor Destroy; override;
@@ -47,7 +52,7 @@ constructor TAstLowerer.Create;
begin
inherited Create;
// Operator folding maps
// Operator folding maps (Reserved for future static optimization)
FBinaryOperators := TDictionary<string, TScalar.TBinaryOp>.Create;
FBinaryOperators.Add('+', TScalar.TBinaryOp.Add);
@@ -76,12 +81,20 @@ end;
class function TAstLowerer.Lower(const RootNode: IAstNode): IAstNode;
begin
var lowerer := TAstLowerer.Create as IAstLowerer;
Result := lowerer.Execute(RootNode);
try
Result := lowerer.Execute(RootNode);
except
on E: Exception do
raise ELoweringException.Create('Internal Error during AST Lowering: ' + E.Message);
end;
end;
function TAstLowerer.Execute(const RootNode: IAstNode): IAstNode;
begin
Result := Accept(RootNode); // Use IAstNode-returning Accept
if not Assigned(RootNode) then
exit(nil);
Result := Accept(RootNode);
if not Assigned(Result) then
Result := TAst.Block([]);
end;