This commit is contained in:
nub31
2025-11-03 17:10:15 +01:00
parent 7d49bf43b7
commit 47fef6bc9f
7 changed files with 175 additions and 210 deletions

View File

@@ -122,7 +122,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Type {value.Type} is not assignable to {field.Type} for field {field.NameToken.Value}")
.At(field)
.At(field, _syntaxTree.Tokens)
.Build());
}
}
@@ -144,7 +144,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Cannot assign {value.Type} to {target.Type}")
.At(statement.Value)
.At(statement.Value, _syntaxTree.Tokens)
.Build());
}
@@ -184,7 +184,7 @@ public sealed class TypeChecker
return expression switch
{
FuncCallNode funcCall => new StatementFuncCallNode(statement.Tokens, funcCall),
_ => throw new CompileException(Diagnostic.Error("Expressions statements can only be function calls").At(statement).Build())
_ => throw new CompileException(Diagnostic.Error("Expressions statements can only be function calls").At(statement, _syntaxTree.Tokens).Build())
};
}
@@ -210,7 +210,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Cannot assign {assignmentNode.Type} to variable of type {type}")
.At(statement.Assignment)
.At(statement.Assignment, _syntaxTree.Tokens)
.Build());
}
}
@@ -219,7 +219,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Cannot infer type of variable {statement.NameToken.Value}")
.At(statement)
.At(statement, _syntaxTree.Tokens)
.Build());
}
@@ -274,7 +274,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Cannot iterate over type {target.Type} which does not have size information")
.At(forSyntax.Target)
.At(forSyntax.Target, _syntaxTree.Tokens)
.Build());
}
}
@@ -337,7 +337,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Unable to infer target type of cast")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.WithHelp("Specify target type where value is used")
.Build());
}
@@ -348,7 +348,7 @@ public sealed class TypeChecker
{
Diagnostics.Add(Diagnostic
.Warning("Target type of cast is same as the value. Cast is unnecessary")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.Build());
return value;
@@ -452,7 +452,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Array indexer must be of type int")
.At(expression.Index)
.At(expression.Index, _syntaxTree.Tokens)
.Build());
}
@@ -463,7 +463,7 @@ public sealed class TypeChecker
NubArrayType arrayType => new ArrayIndexAccessNode(expression.Tokens, arrayType.ElementType, target, index),
NubConstArrayType constArrayType => new ConstArrayIndexAccessNode(expression.Tokens, constArrayType.ElementType, target, index),
NubSliceType sliceType => new SliceIndexAccessNode(expression.Tokens, sliceType.ElementType, target, index),
_ => throw new CompileException(Diagnostic.Error($"Cannot use array indexer on type {target.Type}").At(expression).Build())
_ => throw new CompileException(Diagnostic.Error($"Cannot use array indexer on type {target.Type}").At(expression, _syntaxTree.Tokens).Build())
};
}
@@ -490,7 +490,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Unable to infer type of array initializer")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.WithHelp("Provide a type for a variable assignment")
.Build());
}
@@ -503,7 +503,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Value in array initializer is not the same as the array type")
.At(valueExpression)
.At(valueExpression, _syntaxTree.Tokens)
.Build());
}
@@ -548,7 +548,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Equal and not equal operators must must be used with int, float or bool types")
.At(expression.Left)
.At(expression.Left, _syntaxTree.Tokens)
.Build());
}
@@ -557,7 +557,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Expected type {left.Type} from left side of binary expression, but got {right.Type}")
.At(expression.Right)
.At(expression.Right, _syntaxTree.Tokens)
.Build());
}
@@ -573,7 +573,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Greater than and less than operators must must be used with int or float types")
.At(expression.Left)
.At(expression.Left, _syntaxTree.Tokens)
.Build());
}
@@ -582,7 +582,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Expected type {left.Type} from left side of binary expression, but got {right.Type}")
.At(expression.Right)
.At(expression.Right, _syntaxTree.Tokens)
.Build());
}
@@ -596,7 +596,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Logical and/or must must be used with bool types")
.At(expression.Left)
.At(expression.Left, _syntaxTree.Tokens)
.Build());
}
@@ -605,7 +605,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Expected type {left.Type} from left side of binary expression, but got {right.Type}")
.At(expression.Right)
.At(expression.Right, _syntaxTree.Tokens)
.Build());
}
@@ -618,7 +618,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("The plus operator must only be used with int and float types")
.At(expression.Left)
.At(expression.Left, _syntaxTree.Tokens)
.Build());
}
@@ -643,7 +643,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Math operators must be used with int or float types")
.At(expression.Left)
.At(expression.Left, _syntaxTree.Tokens)
.Build());
}
@@ -652,7 +652,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Expected type {left.Type} from left side of binary expression, but got {right.Type}")
.At(expression.Right)
.At(expression.Right, _syntaxTree.Tokens)
.Build());
}
@@ -666,7 +666,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Bitwise operators must be used with int types")
.At(expression.Left)
.At(expression.Left, _syntaxTree.Tokens)
.Build());
}
@@ -675,7 +675,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Bitwise operators must be used with int types")
.At(expression.Right)
.At(expression.Right, _syntaxTree.Tokens)
.Build());
}
@@ -690,7 +690,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Bitwise operators must be used with int types")
.At(expression.Left)
.At(expression.Left, _syntaxTree.Tokens)
.Build());
}
@@ -699,7 +699,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Expected type {left.Type} from left side of binary expression, but got {right.Type}")
.At(expression.Right)
.At(expression.Right, _syntaxTree.Tokens)
.Build());
}
@@ -723,7 +723,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Negation operator must be used with signed integer or float types")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.Build());
}
@@ -736,7 +736,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error("Invert operator must be used with booleans")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.Build());
}
@@ -756,7 +756,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Cannot dereference non-pointer type {target.Type}")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.Build());
}
@@ -768,14 +768,14 @@ public sealed class TypeChecker
var accessor = CheckExpression(expression.Expression);
if (accessor.Type is not NubFuncType funcType)
{
throw new CompileException(Diagnostic.Error($"Cannot call non-function type {accessor.Type}").At(expression.Expression).Build());
throw new CompileException(Diagnostic.Error($"Cannot call non-function type {accessor.Type}").At(expression.Expression, _syntaxTree.Tokens).Build());
}
if (expression.Parameters.Count != funcType.Parameters.Count)
{
throw new CompileException(Diagnostic
.Error($"Function {funcType} expects {funcType.Parameters.Count} parameters but got {expression.Parameters.Count}")
.At(expression.Parameters.LastOrDefault(expression))
.At(expression.Parameters.LastOrDefault(expression), _syntaxTree.Tokens)
.Build());
}
@@ -790,7 +790,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Parameter {i + 1} does not match the type {expectedParameterType} for function {funcType}")
.At(parameter)
.At(parameter, _syntaxTree.Tokens)
.Build());
}
@@ -817,7 +817,7 @@ public sealed class TypeChecker
throw new CompileException(Diagnostic
.Error($"There is no identifier named {expression.NameToken.Value}")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.Build());
}
@@ -834,7 +834,7 @@ public sealed class TypeChecker
throw new CompileException(Diagnostic
.Error($"Module {expression.ModuleToken.Value} does not export a member named {expression.NameToken.Value}")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.Build());
}
}
@@ -909,7 +909,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Struct {target.Type} does not have a field with the name {expression.MemberToken.Value}")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.Build());
}
@@ -919,7 +919,7 @@ public sealed class TypeChecker
{
throw new CompileException(Diagnostic
.Error($"Cannot access struct member {expression.MemberToken.Value} on type {target.Type}")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.Build());
}
}
@@ -950,7 +950,7 @@ public sealed class TypeChecker
throw new CompileException(Diagnostic
.Error("Cannot get implicit type of struct")
.WithHelp("Specify struct type with struct {type_name} syntax")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.Build());
}
@@ -963,7 +963,7 @@ public sealed class TypeChecker
{
Diagnostics.AddRange(Diagnostic
.Error($"Struct {structType.Name} does not have a field named {initializer.Key}")
.At(initializer.Value)
.At(initializer.Value, _syntaxTree.Tokens)
.Build());
continue;
@@ -981,7 +981,7 @@ public sealed class TypeChecker
{
Diagnostics.Add(Diagnostic
.Warning($"Fields {string.Join(", ", missingFields)} are not initialized")
.At(expression)
.At(expression, _syntaxTree.Tokens)
.Build());
}
@@ -1065,7 +1065,7 @@ public sealed class TypeChecker
throw new CompileException(Diagnostic
.Error($"Type {customType.NameToken.Value} not found in module {module.Name}")
.At(customType)
.At(customType, _syntaxTree.Tokens)
.Build());
}