Tests for Parser
This commit is contained in:
@@ -15,6 +15,9 @@ uses
|
||||
Myc.Ast;
|
||||
|
||||
type
|
||||
// Exception for type mismatches during compilation
|
||||
ETypeCheckException = class(EAstException);
|
||||
|
||||
IAstTypeChecker = interface(IAstVisitor)
|
||||
function Execute(const RootNode: IAstNode; const Layout: IScopeLayout): IAstNode;
|
||||
end;
|
||||
@@ -110,7 +113,8 @@ begin
|
||||
for i := 1 to Address.ScopeDepth do
|
||||
begin
|
||||
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;
|
||||
end;
|
||||
|
||||
@@ -318,10 +322,10 @@ begin
|
||||
if (targetType.Kind = stUnknown) then
|
||||
begin
|
||||
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
|
||||
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;
|
||||
|
||||
if ((targetType.Kind = stUnknown) or Assigned(placeholderType)) and (sourceType.Kind <> stUnknown) then
|
||||
@@ -466,13 +470,13 @@ begin
|
||||
var argsStr: string := '';
|
||||
for i := 0 to High(argTypes) do
|
||||
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]);
|
||||
end;
|
||||
end;
|
||||
end
|
||||
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);
|
||||
end;
|
||||
@@ -509,14 +513,19 @@ begin
|
||||
if (conditionType.Kind <> stUnknown)
|
||||
and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType)
|
||||
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;
|
||||
elseType :=
|
||||
if newElse <> nil then newElse.AsTypedNode.StaticType
|
||||
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);
|
||||
end;
|
||||
@@ -534,12 +543,17 @@ begin
|
||||
if (conditionType.Kind <> stUnknown)
|
||||
and not TTypeRules.CanAssign(TTypes.Ordinal, conditionType)
|
||||
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;
|
||||
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);
|
||||
end;
|
||||
@@ -562,7 +576,7 @@ begin
|
||||
begin
|
||||
fieldIndex := baseType.Definition.IndexOf(Node.Member.Value);
|
||||
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);
|
||||
|
||||
@@ -576,11 +590,11 @@ begin
|
||||
var genDef := baseType.GenericDefinition;
|
||||
fieldIndex := genDef.IndexOf(Node.Member.Value);
|
||||
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;
|
||||
end
|
||||
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;
|
||||
|
||||
Result := TAst.MemberAccess(newBase, newMember.AsKeyword, elemType);
|
||||
@@ -601,10 +615,10 @@ begin
|
||||
if (baseType.Kind <> TStaticTypeKind.stUnknown) then
|
||||
begin
|
||||
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
|
||||
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
|
||||
elemType := baseType.ElementType
|
||||
@@ -708,10 +722,10 @@ begin
|
||||
if (seriesType.Kind <> stUnknown) then
|
||||
begin
|
||||
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
|
||||
raise ETypeException
|
||||
raise ETypeCheckException
|
||||
.CreateFmt('Cannot add item of type %s to series of type %s', [valueType.ToString, seriesType.ElementType.ToString]);
|
||||
end;
|
||||
|
||||
@@ -719,7 +733,7 @@ begin
|
||||
begin
|
||||
var lookbackType := newLookback.AsTypedNode.StaticType;
|
||||
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;
|
||||
|
||||
Result := TAst.AddSeriesItem(newSeries.AsIdentifier, newValue, newLookback, TTypes.Void);
|
||||
@@ -741,7 +755,7 @@ begin
|
||||
if (seriesType.Kind <> stUnknown)
|
||||
and (seriesType.Kind <> TStaticTypeKind.stSeries)
|
||||
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);
|
||||
end;
|
||||
|
||||
Reference in New Issue
Block a user