Tests for Parser

This commit is contained in:
Michael Schimmel
2025-11-25 14:40:04 +01:00
parent 29c36c7ae0
commit d84509c034
6 changed files with 172 additions and 73 deletions
+5 -2
View File
@@ -16,6 +16,9 @@ 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;
@@ -236,7 +239,7 @@ 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 Exception.CreateFmt('Undefined identifier: "%s"', [Node.Name]); raise EBinderException.CreateFmt('Undefined identifier: "%s"', [Node.Name]);
if physAddr.ScopeDepth > 0 then if physAddr.ScopeDepth > 0 then
begin begin
@@ -262,7 +265,7 @@ var
begin begin
var identifier := Node.Target.AsIdentifier; var identifier := Node.Target.AsIdentifier;
if not IsValidIdentifier(identifier.Name) then if not IsValidIdentifier(identifier.Name) then
raise Exception.CreateFmt('Invalid identifier name: "%s".', [identifier.Name]); raise EBinderException.CreateFmt('Invalid identifier name: "%s".', [identifier.Name]);
slot := FCurrentBuilder.Define(identifier.Name); slot := FCurrentBuilder.Define(identifier.Name);
addr := TResolvedAddress.Create(akLocalOrParent, 0, slot); addr := TResolvedAddress.Create(akLocalOrParent, 0, slot);
+32 -18
View File
@@ -15,6 +15,9 @@ 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;
@@ -110,7 +113,8 @@ 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 Exception.CreateFmt('Scope depth mismatch during type lookup. Requested Depth: %d.', [Address.ScopeDepth]); raise ETypeCheckException
.CreateFmt('Scope depth mismatch during type lookup. Requested Depth: %d.', [Address.ScopeDepth]);
ctx := ctx.FParent; ctx := ctx.FParent;
end; end;
@@ -318,10 +322,10 @@ begin
if (targetType.Kind = stUnknown) then if (targetType.Kind = stUnknown) then
begin begin
if not TTypeRules.CanAssign(sourceType, targetType) then if not TTypeRules.CanAssign(sourceType, targetType) then
raise ETypeException.CreateFmt('Cannot assign type %s to %s', [sourceType.ToString, targetType.ToString]); raise ETypeCheckException.CreateFmt('Cannot assign type %s to %s', [sourceType.ToString, targetType.ToString]);
end end
else else
raise ETypeException.CreateFmt('Cannot assign type %s to %s', [sourceType.ToString, targetType.ToString]); raise ETypeCheckException.CreateFmt('Cannot assign type %s to %s', [sourceType.ToString, targetType.ToString]);
end; end;
if ((targetType.Kind = stUnknown) or Assigned(placeholderType)) and (sourceType.Kind <> stUnknown) then if ((targetType.Kind = stUnknown) or Assigned(placeholderType)) and (sourceType.Kind <> stUnknown) then
@@ -466,13 +470,13 @@ begin
var argsStr: string := ''; var argsStr: string := '';
for i := 0 to High(argTypes) do for i := 0 to High(argTypes) do
argsStr := argsStr + argTypes[i].ToString + ' '; argsStr := argsStr + argTypes[i].ToString + ' ';
raise ETypeException raise ETypeCheckException
.CreateFmt('No matching signature for call with args (%s) found on method %s', [argsStr, calleeType.ToString]); .CreateFmt('No matching signature for call with args (%s) found on method %s', [argsStr, calleeType.ToString]);
end; end;
end; end;
end end
else if calleeType.Kind <> TStaticTypeKind.stUnknown then else if calleeType.Kind <> TStaticTypeKind.stUnknown then
raise ETypeException.CreateFmt('Cannot invoke type %s as a function.', [calleeType.ToString]); raise ETypeCheckException.CreateFmt('Cannot invoke type %s as a function.', [calleeType.ToString]);
Result := TAst.FunctionCall(newCallee, newArgs, retType, Node.IsTailCall); Result := TAst.FunctionCall(newCallee, newArgs, retType, Node.IsTailCall);
end; end;
@@ -509,14 +513,19 @@ 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 ETypeException.CreateFmt('If condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]); raise ETypeCheckException.CreateFmt('If condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]);
thenType := newThen.AsTypedNode.StaticType; thenType := newThen.AsTypedNode.StaticType;
elseType := elseType :=
if newElse <> nil then newElse.AsTypedNode.StaticType if newElse <> nil then newElse.AsTypedNode.StaticType
else TTypes.Void; else TTypes.Void;
resultType := TTypeRules.Promote(thenType, elseType); try
resultType := TTypeRules.Promote(thenType, elseType);
except
on E: Exception do // Catch base exception (ETypeException from Myc.Ast.Types)
raise ETypeCheckException.Create(E.Message);
end;
Result := TAst.IfExpr(newCond, newThen, newElse, resultType); Result := TAst.IfExpr(newCond, newThen, newElse, resultType);
end; end;
@@ -534,12 +543,17 @@ 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 ETypeException.CreateFmt('Ternary condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]); raise ETypeCheckException.CreateFmt('Ternary condition must be Boolean or Ordinal, but got %s', [conditionType.ToString]);
thenType := newThen.AsTypedNode.StaticType; thenType := newThen.AsTypedNode.StaticType;
elseType := newElse.AsTypedNode.StaticType; elseType := newElse.AsTypedNode.StaticType;
resultType := TTypeRules.Promote(thenType, elseType); try
resultType := TTypeRules.Promote(thenType, elseType);
except
on E: Exception do
raise ETypeCheckException.Create(E.Message);
end;
Result := TAst.TernaryExpr(newCond, newThen, newElse, resultType); Result := TAst.TernaryExpr(newCond, newThen, newElse, resultType);
end; end;
@@ -562,7 +576,7 @@ 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 ETypeException.CreateFmt('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]); raise ETypeCheckException.CreateFmt('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]);
var fieldType := TTypes.FromScalarKind(baseType.Definition.Fields[fieldIndex].Value); var fieldType := TTypes.FromScalarKind(baseType.Definition.Fields[fieldIndex].Value);
@@ -576,11 +590,11 @@ 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 ETypeException.CreateFmt('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]); raise ETypeCheckException.CreateFmt('Member "%s" not found in type %s', [Node.Member.Value.Name, baseType.ToString]);
elemType := genDef.Fields[fieldIndex].Value; elemType := genDef.Fields[fieldIndex].Value;
end end
else else
raise ETypeException.CreateFmt('Member access requires a record type, but got %s', [baseType.ToString]); raise ETypeCheckException.CreateFmt('Member access requires a record type, but got %s', [baseType.ToString]);
end; end;
Result := TAst.MemberAccess(newBase, newMember.AsKeyword, elemType); Result := TAst.MemberAccess(newBase, newMember.AsKeyword, elemType);
@@ -601,10 +615,10 @@ 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 ETypeException.CreateFmt('Indexer `[]` can only be applied to series types, but got %s', [baseType.ToString]); raise ETypeCheckException.CreateFmt('Indexer `[]` can only be applied to series types, but got %s', [baseType.ToString]);
if (indexType.Kind <> stUnknown) and not TTypeRules.CanAssign(TTypes.Ordinal, indexType) then if (indexType.Kind <> stUnknown) and not TTypeRules.CanAssign(TTypes.Ordinal, indexType) then
raise ETypeException.CreateFmt('Indexer `[]` requires an Ordinal index, but got %s', [indexType.ToString]); raise ETypeCheckException.CreateFmt('Indexer `[]` requires an Ordinal index, but got %s', [indexType.ToString]);
if baseType.Kind = TStaticTypeKind.stSeries then if baseType.Kind = TStaticTypeKind.stSeries then
elemType := baseType.ElementType elemType := baseType.ElementType
@@ -708,10 +722,10 @@ 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 ETypeException.CreateFmt('"add" requires a series as its first argument, but got %s', [seriesType.ToString]); raise ETypeCheckException.CreateFmt('"add" requires a series as its first argument, but got %s', [seriesType.ToString]);
if not TTypeRules.CanAssign(seriesType.ElementType, valueType) then if not TTypeRules.CanAssign(seriesType.ElementType, valueType) then
raise ETypeException raise ETypeCheckException
.CreateFmt('Cannot add item of type %s to series of type %s', [valueType.ToString, seriesType.ElementType.ToString]); .CreateFmt('Cannot add item of type %s to series of type %s', [valueType.ToString, seriesType.ElementType.ToString]);
end; end;
@@ -719,7 +733,7 @@ begin
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 ETypeException.Create('Lookback parameter for "add" must be an ordinal value.'); raise ETypeCheckException.Create('Lookback parameter for "add" must be an ordinal value.');
end; end;
Result := TAst.AddSeriesItem(newSeries.AsIdentifier, newValue, newLookback, TTypes.Void); Result := TAst.AddSeriesItem(newSeries.AsIdentifier, newValue, newLookback, TTypes.Void);
@@ -741,7 +755,7 @@ 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 ETypeException.CreateFmt('"length" requires a series, but got %s', [seriesType.ToString]); raise ETypeCheckException.CreateFmt('"length" requires a series, but got %s', [seriesType.ToString]);
Result := TAst.SeriesLength(newSeries.AsIdentifier, TTypes.Ordinal); Result := TAst.SeriesLength(newSeries.AsIdentifier, TTypes.Ordinal);
end; end;
+24 -19
View File
@@ -12,6 +12,9 @@ uses
Myc.Ast.Scope; Myc.Ast.Scope;
type type
// Exception for runtime errors during script evaluation
EEvaluatorException = class(EAstException);
// The standard AST evaluator for production use. // The standard AST evaluator for production use.
// This class inherits directly from TInterfacedObject as it is an // This class inherits directly from TInterfacedObject as it is an
// Interpreter (AST -> TDataValue), not a Transformer (AST -> IAstNode). // Interpreter (AST -> TDataValue), not a Transformer (AST -> IAstNode).
@@ -222,7 +225,7 @@ begin
adr: TResolvedAddress; adr: TResolvedAddress;
begin begin
if (Length(ArgValues) <> Length(params)) then if (Length(ArgValues) <> Length(params)) then
raise EArgumentException.CreateFmt('Argument count mismatch: expected %d, got %d', [Length(params), Length(ArgValues)]); raise EEvaluatorException.CreateFmt('Argument count mismatch: expected %d, got %d', [Length(params), Length(ArgValues)]);
// Create the new execution scope for this function call. // Create the new execution scope for this function call.
lambdaScope := TScope.CreateScope(closureScope, descriptor, capturedCells); lambdaScope := TScope.CreateScope(closureScope, descriptor, capturedCells);
@@ -265,17 +268,17 @@ end;
function TEvaluatorVisitor.VisitQuasiquote(const Node: IQuasiquoteNode): TDataValue; function TEvaluatorVisitor.VisitQuasiquote(const Node: IQuasiquoteNode): TDataValue;
begin begin
raise Exception.Create('Quasiquote nodes are a compile-time construct and cannot be evaluated at runtime.'); raise EEvaluatorException.Create('Quasiquote nodes are a compile-time construct and cannot be evaluated at runtime.');
end; end;
function TEvaluatorVisitor.VisitUnquote(const Node: IUnquoteNode): TDataValue; function TEvaluatorVisitor.VisitUnquote(const Node: IUnquoteNode): TDataValue;
begin begin
raise Exception.Create('Unquote nodes are a compile-time construct and cannot be evaluated at runtime.'); raise EEvaluatorException.Create('Unquote nodes are a compile-time construct and cannot be evaluated at runtime.');
end; end;
function TEvaluatorVisitor.VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TDataValue; function TEvaluatorVisitor.VisitUnquoteSplicing(const Node: IUnquoteSplicingNode): TDataValue;
begin begin
raise Exception.Create('Unquote-splicing nodes are a compile-time construct and cannot be evaluated at runtime.'); raise EEvaluatorException.Create('Unquote-splicing nodes are a compile-time construct and cannot be evaluated at runtime.');
end; end;
function TEvaluatorVisitor.VisitFunctionCall(const Node: IFunctionCallNode): TDataValue; function TEvaluatorVisitor.VisitFunctionCall(const Node: IFunctionCallNode): TDataValue;
@@ -308,7 +311,7 @@ begin
// --- Dynamic Path (Default) --- // --- Dynamic Path (Default) ---
calleeValue := Node.Callee.Accept(Self); calleeValue := Node.Callee.Accept(Self);
if calleeValue.Kind <> vkMethod then if calleeValue.Kind <> vkMethod then
raise EArgumentException.Create('Expression is not invokable in this context.'); raise EEvaluatorException.Create('Expression is not invokable in this context.');
argNodes := Node.Arguments; argNodes := Node.Arguments;
SetLength(argValues, Length(argNodes)); SetLength(argValues, Length(argNodes));
@@ -371,7 +374,7 @@ begin
begin begin
lookbackValue := Node.Lookback.Accept(Self); lookbackValue := Node.Lookback.Accept(Self);
if (lookbackValue.Kind <> vkScalar) or (lookbackValue.AsScalar.Kind <> TScalar.TKind.Ordinal) then if (lookbackValue.Kind <> vkScalar) or (lookbackValue.AsScalar.Kind <> TScalar.TKind.Ordinal) then
raise EArgumentException.Create('Lookback parameter must be an integer.'); raise EEvaluatorException.Create('Lookback parameter must be an integer.');
lookback := lookbackValue.AsScalar.Value.AsInt64; lookback := lookbackValue.AsScalar.Value.AsInt64;
end; end;
@@ -379,7 +382,7 @@ begin
vkRecordSeries: vkRecordSeries:
begin begin
if (itemValue.Kind <> vkRecord) then if (itemValue.Kind <> vkRecord) then
raise EArgumentException.Create('Can only add record values to a TScalarRecordSeries.'); raise EEvaluatorException.Create('Can only add record values to a TScalarRecordSeries.');
with seriesVar.AsRecordSeries do with seriesVar.AsRecordSeries do
begin begin
@@ -387,7 +390,7 @@ begin
end; end;
end; end;
else else
raise EArgumentException.Create('"add" operation is only supported for series types.'); raise EEvaluatorException.Create('"add" operation is only supported for series types.');
end; end;
Result := TDataValue.Void; Result := TDataValue.Void;
end; end;
@@ -395,7 +398,7 @@ end;
function TEvaluatorVisitor.VisitAssignment(const Node: IAssignmentNode): TDataValue; function TEvaluatorVisitor.VisitAssignment(const Node: IAssignmentNode): TDataValue;
begin begin
if Node.Target.Kind <> akIdentifier then if Node.Target.Kind <> akIdentifier then
raise ETypeException.Create('Runtime Error: Assignment target must be an identifier.'); raise EEvaluatorException.Create('Runtime Error: Assignment target must be an identifier.');
// Evaluate value // Evaluate value
Result := Node.Value.Accept(Self); Result := Node.Value.Accept(Self);
@@ -423,7 +426,7 @@ begin
begin begin
var recordDef := TRttiAstHelper.JsonToRecordDefinition(def); var recordDef := TRttiAstHelper.JsonToRecordDefinition(def);
if Length(recordDef.Fields) = 0 then if Length(recordDef.Fields) = 0 then
raise EArgumentException.Create('Failed to parse record definition from JSON array.'); raise EEvaluatorException.Create('Failed to parse record definition from JSON array.');
var recordSeries := TScalarRecordSeries.Create(recordDef); var recordSeries := TScalarRecordSeries.Create(recordDef);
Result := TDataValue.FromRecordSeries(recordSeries); Result := TDataValue.FromRecordSeries(recordSeries);
end end
@@ -457,7 +460,7 @@ begin
indexValue := Node.Index.Accept(Self); indexValue := Node.Index.Accept(Self);
if (indexValue.Kind <> vkScalar) or (indexValue.AsScalar.Kind <> TScalar.TKind.Ordinal) then if (indexValue.Kind <> vkScalar) or (indexValue.AsScalar.Kind <> TScalar.TKind.Ordinal) then
raise EArgumentException.Create('Indexer `[]` requires an integer argument.'); raise EEvaluatorException.Create('Indexer `[]` requires an integer argument.');
index := indexValue.AsScalar.Value.AsInt64; index := indexValue.AsScalar.Value.AsInt64;
@@ -466,14 +469,15 @@ begin
begin begin
series := baseValue.AsSeries; series := baseValue.AsSeries;
if (index < 0) or (index >= series.TotalCount) then if (index < 0) or (index >= series.TotalCount) then
raise EArgumentException.CreateFmt('Index %d is out of bounds for series with %d elements.', [index, series.TotalCount]); raise EEvaluatorException.CreateFmt('Index %d is out of bounds for series with %d elements.', [index, series.TotalCount]);
Result := TDataValue(series.Items[Integer(index)]); // Explicit cast Result := TDataValue(series.Items[Integer(index)]); // Explicit cast
end; end;
vkRecordSeries: vkRecordSeries:
begin begin
recSeries := baseValue.AsRecordSeries; recSeries := baseValue.AsRecordSeries;
if (index < 0) or (index >= recSeries.TotalCount) then if (index < 0) or (index >= recSeries.TotalCount) then
raise EArgumentException.CreateFmt('Index %d is out of bounds for series with %d elements.', [index, recSeries.TotalCount]); raise EEvaluatorException
.CreateFmt('Index %d is out of bounds for series with %d elements.', [index, recSeries.TotalCount]);
// Materialize the TScalarRecord by accessing each member series by index // Materialize the TScalarRecord by accessing each member series by index
fieldCount := Length(recSeries.Def.Fields); fieldCount := Length(recSeries.Def.Fields);
@@ -491,7 +495,7 @@ begin
Result := TDataValue.FromRecord(rec); Result := TDataValue.FromRecord(rec);
end; end;
else else
raise EArgumentException.Create('Indexer `[]` is not supported for this value type.'); raise EEvaluatorException.Create('Indexer `[]` is not supported for this value type.');
end; end;
end; end;
@@ -511,12 +515,12 @@ begin
var rec := baseValue.AsGenericRecord; var rec := baseValue.AsGenericRecord;
var fieldIndex := rec.IndexOf(Node.Member.Value); var fieldIndex := rec.IndexOf(Node.Member.Value);
if fieldIndex < 0 then if fieldIndex < 0 then
raise EArgumentException.CreateFmt('Member ":%s" not found in record.', [Node.Member.Value.Name]); raise EEvaluatorException.CreateFmt('Member ":%s" not found in record.', [Node.Member.Value.Name]);
Result := rec.Fields[fieldIndex].Value; Result := rec.Fields[fieldIndex].Value;
end; end;
else else
raise EArgumentException.Create('Member access operator `.` is not supported for this value type.'); raise EEvaluatorException.Create('Member access operator `.` is not supported for this value type.');
end; end;
end; end;
@@ -559,7 +563,7 @@ begin
Result := TDataValue.FromRecord(rec); Result := TDataValue.FromRecord(rec);
end end
else else
raise EInvalidOpException.Create('RecordLiteral has no definition (Binder/TypeChecker failure).'); raise EEvaluatorException.Create('RecordLiteral has no definition (Binder/TypeChecker failure).');
end; end;
function TEvaluatorVisitor.VisitVariableDeclaration(const Node: IVariableDeclarationNode): TDataValue; function TEvaluatorVisitor.VisitVariableDeclaration(const Node: IVariableDeclarationNode): TDataValue;
@@ -567,7 +571,7 @@ var
address: TResolvedAddress; address: TResolvedAddress;
begin begin
if Node.Target.Kind <> akIdentifier then if Node.Target.Kind <> akIdentifier then
raise ETypeException.Create('Runtime Error: Variable declaration target must be an identifier.'); raise EEvaluatorException.Create('Runtime Error: Variable declaration target must be an identifier.');
// 1. Evaluate Initializer // 1. Evaluate Initializer
if Assigned(Node.Initializer) then if Assigned(Node.Initializer) then
@@ -635,7 +639,8 @@ begin
vkSeries: len := seriesValue.AsSeries.Count; vkSeries: len := seriesValue.AsSeries.Count;
vkRecordSeries: len := seriesValue.AsRecordSeries.Count; vkRecordSeries: len := seriesValue.AsRecordSeries.Count;
else else
raise EArgumentException.CreateFmt('Cannot get length of type %s.', [GetEnumName(TypeInfo(TDataValueKind), Ord(seriesValue.Kind))]); raise EEvaluatorException
.CreateFmt('Cannot get length of type %s.', [GetEnumName(TypeInfo(TDataValueKind), Ord(seriesValue.Kind))]);
end; end;
Result := TDataValue(TScalar.FromInt64(len)); // Explicit cast Result := TDataValue(TScalar.FromInt64(len)); // Explicit cast
+4
View File
@@ -401,6 +401,10 @@ 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
+102 -29
View File
@@ -7,10 +7,21 @@ uses
Myc.Ast.Nodes, Myc.Ast.Nodes,
Myc.Ast.Visitor; Myc.Ast.Visitor;
type
// Exception for parsing errors containing location information
EParserException = class(EAstException)
strict private
FLine: Integer;
FCol: Integer;
public
constructor Create(const AMessage: string; ALine, ACol: Integer);
property Line: Integer read FLine;
property Col: Integer read FCol;
end;
var var
// This BNF should always be kept up to date and valid. It is used to comunicate with LLMs. // This BNF should always be kept up to date and valid. It is used to comunicate with LLMs.
// (Nop isn't part of the language syntax.) // (Nop isn't part of the language syntax.)
BNF: String = BNF: String =
''' '''
(* ---------------------------------------------------------------------- *) (* ---------------------------------------------------------------------- *)
@@ -46,7 +57,7 @@ var
list ::= "(" list_content ")" list ::= "(" list_content ")"
(* The parser distinguishes between special forms (like 'if') and (* The parser distinguishes between special forms (like 'if') and
general function calls based on the first element. *) general function calls based on the first element. *)
list_content ::= special_form | function_call list_content ::= special_form | function_call
special_form ::= "if" expression expression expression? special_form ::= "if" expression expression expression?
@@ -61,7 +72,7 @@ var
| dot_identifier expression | dot_identifier expression
(* A function call is any list not recognized as a special form. (* A function call is any list not recognized as a special form.
The first 'expression' is the callee, the rest are the arguments. *) The first 'expression' is the callee, the rest are the arguments. *)
function_call ::= expression expression* function_call ::= expression expression*
(* ---------------------------------------------------------------------- *) (* ---------------------------------------------------------------------- *)
@@ -86,7 +97,7 @@ var
dot_identifier ::= "." identifier dot_identifier ::= "." identifier
(* An identifier is a sequence of characters that are not delimiters. (* An identifier is a sequence of characters that are not delimiters.
The lexer is very permissive. *) The lexer is very permissive. *)
identifier ::= ? any sequence of chars not containing whitespace, '()[]{}'`~:; ? identifier ::= ? any sequence of chars not containing whitespace, '()[]{}'`~:; ?
digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
@@ -142,20 +153,29 @@ type
TToken = record TToken = record
Kind: TTokenKind; Kind: TTokenKind;
Text: string; Text: string;
Line: Integer;
Col: Integer;
end; end;
TLexer = class TLexer = class
private private
FSource: string; FSource: string;
FCurrentPos: Integer; FCurrentPos: Integer;
FLine: Integer;
FLineStart: Integer;
function Peek: Char; function Peek: Char;
procedure Advance; procedure Advance;
function ReadNumber: string; function ReadNumber: string;
function ReadString: string; function ReadString: string;
function ReadIdentifier: string; function ReadIdentifier: string;
function GetCurrentLine: Integer;
function GetCurrentCol: Integer;
procedure Error(const Msg: string);
public public
constructor Create(const ASource: string); constructor Create(const ASource: string);
function GetNextToken: TToken; function GetNextToken: TToken;
property CurrentLine: Integer read GetCurrentLine;
property CurrentCol: Integer read GetCurrentCol;
end; end;
TExpr = record TExpr = record
@@ -168,6 +188,8 @@ type
private private
FLexer: TLexer; FLexer: TLexer;
FCurrentToken: TToken; FCurrentToken: TToken;
procedure Error(const Msg: string);
procedure ErrorFmt(const Msg: string; const Args: array of const);
procedure Consume(AExpectedKind: TTokenKind); procedure Consume(AExpectedKind: TTokenKind);
procedure NextToken; procedure NextToken;
function ParseList: IAstNode; function ParseList: IAstNode;
@@ -223,6 +245,15 @@ type
procedure VisitNop(const Node: INopNode); override; procedure VisitNop(const Node: INopNode); override;
end; end;
{ EParserException }
constructor EParserException.Create(const AMessage: string; ALine, ACol: Integer);
begin
inherited CreateFmt('[Line %d, Col %d] %s', [ALine, ACol, AMessage]);
FLine := ALine;
FCol := ACol;
end;
{ TLexer } { TLexer }
constructor TLexer.Create(const ASource: string); constructor TLexer.Create(const ASource: string);
@@ -230,6 +261,23 @@ begin
inherited Create; inherited Create;
FSource := ASource; FSource := ASource;
FCurrentPos := 1; FCurrentPos := 1;
FLine := 1;
FLineStart := 1;
end;
function TLexer.GetCurrentLine: Integer;
begin
Result := FLine;
end;
function TLexer.GetCurrentCol: Integer;
begin
Result := FCurrentPos - FLineStart + 1;
end;
procedure TLexer.Error(const Msg: string);
begin
raise EParserException.Create(Msg, FLine, GetCurrentCol);
end; end;
function TLexer.Peek: Char; function TLexer.Peek: Char;
@@ -241,8 +289,19 @@ begin
end; end;
procedure TLexer.Advance; procedure TLexer.Advance;
var
c: Char;
begin begin
inc(FCurrentPos); if FCurrentPos <= Length(FSource) then
begin
c := FSource[FCurrentPos];
if c = #10 then
begin
Inc(FLine);
FLineStart := FCurrentPos + 1;
end;
end;
Inc(FCurrentPos);
end; end;
function TLexer.ReadIdentifier: string; function TLexer.ReadIdentifier: string;
@@ -293,7 +352,7 @@ begin
begin begin
Advance; // Consume '\' Advance; // Consume '\'
if FCurrentPos > Length(FSource) then if FCurrentPos > Length(FSource) then
raise Exception.Create('Syntax Error: String literal ends with an escape character.'); Error('Syntax Error: String literal ends with an escape character.');
c := Peek; // Get escaped char c := Peek; // Get escaped char
case c of case c of
@@ -319,7 +378,7 @@ begin
end; end;
// If we fall out of the loop, we hit EOF without a closing quote. // If we fall out of the loop, we hit EOF without a closing quote.
raise Exception.Create('Syntax Error: Unterminated string literal.'); Error('Syntax Error: Unterminated string literal.');
finally finally
builder.Free; builder.Free;
@@ -349,6 +408,10 @@ begin
break; break;
end; end;
// Capture location for the start of the token
Result.Line := CurrentLine;
Result.Col := CurrentCol;
if FCurrentPos > Length(FSource) then if FCurrentPos > Length(FSource) then
begin begin
Result.Kind := tkEOF; Result.Kind := tkEOF;
@@ -449,6 +512,16 @@ begin
inherited; inherited;
end; end;
procedure TParser.Error(const Msg: string);
begin
raise EParserException.Create(Msg, FCurrentToken.Line, FCurrentToken.Col);
end;
procedure TParser.ErrorFmt(const Msg: string; const Args: array of const);
begin
raise EParserException.Create(Format(Msg, Args), FCurrentToken.Line, FCurrentToken.Col);
end;
procedure TParser.NextToken; procedure TParser.NextToken;
begin begin
FCurrentToken := FLexer.GetNextToken; FCurrentToken := FLexer.GetNextToken;
@@ -457,7 +530,7 @@ end;
procedure TParser.Consume(AExpectedKind: TTokenKind); procedure TParser.Consume(AExpectedKind: TTokenKind);
begin begin
if FCurrentToken.Kind <> AExpectedKind then if FCurrentToken.Kind <> AExpectedKind then
raise Exception.CreateFmt('Syntax Error: Expected token %s, but found %s', [AExpectedKind.ToString, FCurrentToken.Kind.ToString]); ErrorFmt('Syntax Error: Expected token %s, but found %s', [AExpectedKind.ToString, FCurrentToken.Kind.ToString]);
NextToken; NextToken;
end; end;
@@ -471,9 +544,9 @@ begin
while FCurrentToken.Kind <> tkRightBracket do while FCurrentToken.Kind <> tkRightBracket do
begin begin
if FCurrentToken.Kind = tkEOF then if FCurrentToken.Kind = tkEOF then
raise Exception.Create('Syntax Error: Unexpected end of file.'); Error('Syntax Error: Unexpected end of file.');
if FCurrentToken.Kind <> tkIdentifier then if FCurrentToken.Kind <> tkIdentifier then
raise Exception.Create('Syntax Error: Expected identifier in parameter list.'); Error('Syntax Error: Expected identifier in parameter list.');
params.Add(TAst.Identifier(FCurrentToken.Text)); params.Add(TAst.Identifier(FCurrentToken.Text));
NextToken; NextToken;
end; end;
@@ -496,17 +569,17 @@ begin
while FCurrentToken.Kind <> tkRightBrace do while FCurrentToken.Kind <> tkRightBrace do
begin begin
if FCurrentToken.Kind = tkEOF then if FCurrentToken.Kind = tkEOF then
raise Exception.Create('Syntax Error: Unexpected end of file in record literal.'); Error('Syntax Error: Unexpected end of file in record literal.');
// Key must be a keyword // Key must be a keyword
if FCurrentToken.Kind <> tkKeyword then if FCurrentToken.Kind <> tkKeyword then
raise Exception.Create('Syntax Error: Expected keyword (e.g., :key) as field name in record literal.'); Error('Syntax Error: Expected keyword (e.g., :key) as field name in record literal.');
fieldName := FCurrentToken.Text; fieldName := FCurrentToken.Text;
NextToken; NextToken;
// Value can be any expression // Value can be any expression
if FCurrentToken.Kind = tkRightBrace then if FCurrentToken.Kind = tkRightBrace then
raise Exception.Create('Syntax Error: Missing value for key ' + fieldName + ' in record literal.'); Error('Syntax Error: Missing value for key ' + fieldName + ' in record literal.');
fieldValue := ParseExpression.Node; fieldValue := ParseExpression.Node;
fields.Add(TRecordFieldLiteral.Create(TAst.Keyword(fieldName), fieldValue)); fields.Add(TRecordFieldLiteral.Create(TAst.Keyword(fieldName), fieldValue));
@@ -520,7 +593,6 @@ begin
end; end;
function TParser.ParseList: IAstNode; function TParser.ParseList: IAstNode;
var var
elements: TList<TExpr>; elements: TList<TExpr>;
head: TExpr; head: TExpr;
@@ -530,14 +602,14 @@ var
begin begin
Consume(tkLeftParen); Consume(tkLeftParen);
if FCurrentToken.Kind = tkRightParen then if FCurrentToken.Kind = tkRightParen then
raise Exception.Create('Syntax Error: Empty list () is not a valid expression.'); Error('Syntax Error: Empty list () is not a valid expression.');
elements := TList<TExpr>.Create; elements := TList<TExpr>.Create;
try try
while FCurrentToken.Kind <> tkRightParen do while FCurrentToken.Kind <> tkRightParen do
begin begin
if FCurrentToken.Kind = tkEOF then if FCurrentToken.Kind = tkEOF then
raise Exception.Create('Syntax Error: Unexpected end of file.'); Error('Syntax Error: Unexpected end of file.');
elements.Add(ParseExpression); elements.Add(ParseExpression);
end; end;
@@ -557,7 +629,7 @@ begin
if SameText(head.Token.Text, 'if') then if SameText(head.Token.Text, 'if') then
begin begin
if not (Length(tailNodes) in [2, 3]) then if not (Length(tailNodes) in [2, 3]) then
raise Exception.Create('Syntax Error in if statement.'); Error('Syntax Error in if statement.');
var elseBranch: IAstNode := nil; var elseBranch: IAstNode := nil;
if Length(tailNodes) = 3 then if Length(tailNodes) = 3 then
@@ -568,7 +640,7 @@ begin
else if SameText(head.Token.Text, '?') then else if SameText(head.Token.Text, '?') then
begin begin
if Length(tailNodes) <> 3 then if Length(tailNodes) <> 3 then
raise Exception.Create('Syntax Error in ternary statement.'); Error('Syntax Error in ternary statement.');
Result := TAst.TernaryExpr(tailNodes[0], tailNodes[1], tailNodes[2]); Result := TAst.TernaryExpr(tailNodes[0], tailNodes[1], tailNodes[2]);
end end
@@ -576,7 +648,7 @@ begin
begin begin
// Identifier check removed to support macros (e.g. def ~x 1) // Identifier check removed to support macros (e.g. def ~x 1)
if not (Length(tailNodes) in [1, 2]) then if not (Length(tailNodes) in [1, 2]) then
raise Exception.CreateFmt('Syntax Error: ''def'' requires a target and an optional initializer.', []); Error('Syntax Error: ''def'' requires a target and an optional initializer.');
initializer := nil; initializer := nil;
if Length(tailNodes) = 2 then if Length(tailNodes) = 2 then
@@ -587,13 +659,13 @@ begin
else if SameText(head.Token.Text, 'defmacro') then else if SameText(head.Token.Text, 'defmacro') then
begin begin
if (Length(tailNodes) <> 3) or (tailTokens[0].Kind <> tkIdentifier) then if (Length(tailNodes) <> 3) or (tailTokens[0].Kind <> tkIdentifier) then
raise Exception.Create('Syntax Error: ''defmacro'' requires a name, a parameter list, and a body.'); Error('Syntax Error: ''defmacro'' requires a name, a parameter list, and a body.');
var macroName := tailNodes[0].AsIdentifier; var macroName := tailNodes[0].AsIdentifier;
var macroParams := elements[2].Params; var macroParams := elements[2].Params;
if tailTokens[2].Kind <> tkBacktick then if tailTokens[2].Kind <> tkBacktick then
raise Exception.Create('Syntax Error: Expected a quasiquote as macro body.'); Error('Syntax Error: Expected a quasiquote as macro body.');
var macroBody := IQuasiQuoteNode(tailNodes[2]); var macroBody := IQuasiQuoteNode(tailNodes[2]);
@@ -603,14 +675,14 @@ begin
begin begin
// Identifier check removed // Identifier check removed
if Length(tailNodes) <> 2 then if Length(tailNodes) <> 2 then
raise Exception.Create('Syntax Error: ''assign'' requires exactly 2 arguments (target and value).'); Error('Syntax Error: ''assign'' requires exactly 2 arguments (target and value).');
Result := TAst.Assign(tailNodes[0], tailNodes[1]); Result := TAst.Assign(tailNodes[0], tailNodes[1]);
end end
else if SameText(head.Token.Text, 'fn') then else if SameText(head.Token.Text, 'fn') then
begin begin
if Length(tailNodes) <> 2 then if Length(tailNodes) <> 2 then
raise Exception.Create('Syntax Error: ''fn'' requires a parameter list and a body.'); Error('Syntax Error: ''fn'' requires a parameter list and a body.');
Result := TAst.LambdaExpr(elements[1].Params, tailNodes[1]); Result := TAst.LambdaExpr(elements[1].Params, tailNodes[1]);
end end
else if SameText(head.Token.Text, 'do') then else if SameText(head.Token.Text, 'do') then
@@ -620,15 +692,16 @@ begin
else if SameText(head.Token.Text, 'get') then else if SameText(head.Token.Text, 'get') then
begin begin
if Length(tailNodes) <> 2 then if Length(tailNodes) <> 2 then
raise Exception.Create('Syntax Error: ''get'' requires exactly 2 arguments (base and index).'); Error('Syntax Error: ''get'' requires exactly 2 arguments (base and index).');
Result := TAst.Indexer(tailNodes[0], tailNodes[1]) Result := TAst.Indexer(tailNodes[0], tailNodes[1])
end end
else if (Length(head.Token.Text) > 1) and (head.Token.Text.StartsWith('.')) then else if (Length(head.Token.Text) > 1) and (head.Token.Text.StartsWith('.')) then
begin begin
if Length(tailNodes) <> 1 then if Length(tailNodes) <> 1 then
raise Exception.CreateFmt( ErrorFmt(
'Syntax Error: Member access (e.g., ''%s'') requires exactly 1 argument (the base object).', 'Syntax Error: Member access (e.g., ''%s'') requires exactly 1 argument (the base object).',
[head.Token.Text]); [head.Token.Text]
);
Result := TAst.MemberAccess(tailNodes[0], TAst.Keyword(head.Token.Text.Substring(1))); Result := TAst.MemberAccess(tailNodes[0], TAst.Keyword(head.Token.Text.Substring(1)));
end; end;
end; end;
@@ -696,7 +769,7 @@ begin
else if TryStrToFloat(FCurrentToken.Text, dbl, TFormatSettings.Invariant) then else if TryStrToFloat(FCurrentToken.Text, dbl, TFormatSettings.Invariant) then
Result.Node := TAst.Constant(dbl) Result.Node := TAst.Constant(dbl)
else else
raise Exception.CreateFmt('Syntax Error: Invalid number format "%s"', [FCurrentToken.Text]); ErrorFmt('Syntax Error: Invalid number format "%s"', [FCurrentToken.Text]);
NextToken; NextToken;
end; end;
tkString: tkString:
@@ -736,7 +809,7 @@ begin
tkLeftBrace: Result.Node := ParseRecordLiteral; tkLeftBrace: Result.Node := ParseRecordLiteral;
tkEOF: {nop}; tkEOF: {nop};
else else
raise Exception.CreateFmt('Syntax Error: Unexpected token %s', [FCurrentToken.Kind.ToString]); ErrorFmt('Syntax Error: Unexpected token %s', [FCurrentToken.Kind.ToString]);
end; end;
end; end;
@@ -744,7 +817,7 @@ function TParser.Parse: IAstNode;
begin begin
var expr := ParseExpression; var expr := ParseExpression;
if FCurrentToken.Kind <> tkEOF then if FCurrentToken.Kind <> tkEOF then
raise Exception.Create('Syntax Error: Unexpected characters after end of expression.'); Error('Syntax Error: Unexpected characters after end of expression.');
Result := expr.Node; Result := expr.Node;
end; end;
+5 -5
View File
@@ -356,27 +356,27 @@ end;
procedure TTestMycAstScript.Parser_Error_UnbalancedParens_MissingRight; procedure TTestMycAstScript.Parser_Error_UnbalancedParens_MissingRight;
begin begin
// FIX 2: Pass explicit Exception type to WillRaise // FIX 2: Pass explicit Exception type to WillRaise
Assert.WillRaise(procedure begin Parse('(a b'); end, Exception); Assert.WillRaise(procedure begin Parse('(a b'); end, EParserException);
end; end;
procedure TTestMycAstScript.Parser_Error_UnbalancedParens_ExtraRight; procedure TTestMycAstScript.Parser_Error_UnbalancedParens_ExtraRight;
begin begin
Assert.WillRaise(procedure begin Parse('a )'); end, Exception); Assert.WillRaise(procedure begin Parse('a )'); end, EParserException);
end; end;
procedure TTestMycAstScript.Parser_Error_UnterminatedString; procedure TTestMycAstScript.Parser_Error_UnterminatedString;
begin begin
Assert.WillRaise(procedure begin Parse('"hello'); end, Exception); Assert.WillRaise(procedure begin Parse('"hello'); end, EParserException);
end; end;
procedure TTestMycAstScript.Parser_Error_EmptyList; procedure TTestMycAstScript.Parser_Error_EmptyList;
begin begin
Assert.WillRaise(procedure begin Parse('()'); end, Exception); Assert.WillRaise(procedure begin Parse('()'); end, EParserException);
end; end;
procedure TTestMycAstScript.Parser_Error_Record_MissingValue; procedure TTestMycAstScript.Parser_Error_Record_MissingValue;
begin begin
Assert.WillRaise(procedure begin Parse('{:a 1 :b}'); end, Exception); Assert.WillRaise(procedure begin Parse('{:a 1 :b}'); end, EParserException);
end; end;
end. end.