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
+8 -5
View File
@@ -15,6 +15,9 @@ uses
Myc.Ast;
type
// Exception specific to macro expansion errors
EMacroException = class(EAstException);
IAstMacroExpander = interface(IAstVisitor)
function Execute(const RootNode: IAstNode): IAstNode;
end;
@@ -306,13 +309,13 @@ begin
if value.Kind in [vkScalar, vkText, vkVoid] then
Result := TAst.Constant(value)
else
raise Exception.CreateFmt('Cannot unquote complex runtime value of type %s at compile time.', [value.Kind.ToString]);
raise EMacroException.CreateFmt('Cannot unquote complex runtime value of type %s at compile time.', [value.Kind.ToString]);
end;
function TExpansionVisitor.VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): IAstNode;
begin
// ~@ can only be handled within a list context (TransformAndSpliceNodes)
raise Exception.Create('Unquote-splicing (`~@`) can only appear inside a list form.');
raise EMacroException.Create('Unquote-splicing (`~@`) can only appear inside a list form.');
end;
function TExpansionVisitor.VisitFunctionCall(const Node: IFunctionCallNode): IAstNode;
@@ -443,7 +446,7 @@ begin
var params := macroDef.Parameters;
if Length(Node.Arguments) <> Length(params) then
raise Exception.CreateFmt('Macro %s expects %d arguments.', [calleeIdentifier.Name, Length(params)]);
raise EMacroException.CreateFmt('Macro %s expects %d arguments.', [calleeIdentifier.Name, Length(params)]);
// 2. Populate scope with UN-EVALUATED AST nodes (Code as Data)
for i := 0 to High(params) do
@@ -470,12 +473,12 @@ end;
function TMacroExpander.VisitUnquote(const Node: IUnquoteNode): IAstNode;
begin
// Unquotes outside of a macro definition/expansion phase are invalid syntax
raise Exception.Create('Unquote (`~`) can only be used inside a quasiquote.');
raise EMacroException.Create('Unquote (`~`) can only be used inside a quasiquote.');
end;
function TMacroExpander.VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): IAstNode;
begin
raise Exception.Create('Unquote-splicing (`~@`) can only be used inside a quasiquote.');
raise EMacroException.Create('Unquote-splicing (`~@`) can only be used inside a quasiquote.');
end;
{ TMacroRegistry }