...
This commit is contained in:
@@ -284,7 +284,12 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
|
||||
|
||||
private TypedNodeExpressionModuleIdent CheckExpressionModuleIdent(NodeExpressionModuleIdent expression)
|
||||
{
|
||||
var identifierType = moduleGraph.ResolveIdentifier(expression.Module.Ident, expression.Value.Ident);
|
||||
if (!moduleGraph.TryResolveModule(expression.Module.Ident, out var module))
|
||||
throw new CompileException(Diagnostic.Error($"Module '{expression.Module.Ident}' not found").At(fileName, expression.Module).Build());
|
||||
|
||||
if (!module.TryResolveIdentifierType(expression.Value.Ident, out var identifierType))
|
||||
throw new CompileException(Diagnostic.Error($"Identifier '{expression.Module.Ident}::{expression.Value.Ident}' not found").At(fileName, expression.Value).Build());
|
||||
|
||||
return new TypedNodeExpressionModuleIdent(expression.Tokens, identifierType, expression.Module, expression.Value);
|
||||
}
|
||||
|
||||
@@ -301,7 +306,7 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
|
||||
|
||||
var field = structType.Fields.FirstOrDefault(x => x.Name == expression.Name.Ident);
|
||||
if (field == null)
|
||||
throw new CompileException(Diagnostic.Error($"Struct {target.Type} does not have a field matching the name '{expression.Name.Ident}'").At(fileName, target).Build());
|
||||
throw new CompileException(Diagnostic.Error($"Struct '{target.Type}' does not have a field matching the name '{expression.Name.Ident}'").At(fileName, target).Build());
|
||||
|
||||
return new TypedNodeExpressionMemberAccess(expression.Tokens, field.Type, target, expression.Name);
|
||||
}
|
||||
@@ -313,14 +318,16 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
|
||||
|
||||
private TypedNodeExpressionStructLiteral CheckExpressionStructLiteral(NodeExpressionStructLiteral expression)
|
||||
{
|
||||
var type = moduleGraph.ResolveStruct(expression.Module.Ident, expression.Name.Ident);
|
||||
if (type == null)
|
||||
throw new CompileException(Diagnostic.Error($"Undeclared struct '{expression.Module.Ident}::{expression.Name.Ident}'").At(fileName, expression.Name).Build());
|
||||
if (!moduleGraph.TryResolveModule(expression.Module.Ident, out var module))
|
||||
throw new CompileException(Diagnostic.Error($"Module '{expression.Module.Ident}' not found").At(fileName, expression.Module).Build());
|
||||
|
||||
if (!module.TryResolveStructType(expression.Name.Ident, out var structType))
|
||||
throw new CompileException(Diagnostic.Error($"Struct '{expression.Module.Ident}::{expression.Name.Ident}' not found").At(fileName, expression.Name).Build());
|
||||
|
||||
var initializers = new List<TypedNodeExpressionStructLiteral.Initializer>();
|
||||
foreach (var initializer in expression.Initializers)
|
||||
{
|
||||
var field = type.Fields.FirstOrDefault(x => x.Name == initializer.Name.Ident);
|
||||
var field = structType.Fields.FirstOrDefault(x => x.Name == initializer.Name.Ident);
|
||||
if (field == null)
|
||||
throw new CompileException(Diagnostic.Error($"Field '{initializer.Name.Ident}' does not exist on struct '{expression.Name.Ident}'").At(fileName, initializer.Name).Build());
|
||||
|
||||
@@ -331,25 +338,36 @@ public sealed class TypeChecker(string fileName, NodeDefinitionFunc function, Mo
|
||||
initializers.Add(new TypedNodeExpressionStructLiteral.Initializer(initializer.Tokens, initializer.Name, value));
|
||||
}
|
||||
|
||||
return new TypedNodeExpressionStructLiteral(expression.Tokens, type, initializers);
|
||||
return new TypedNodeExpressionStructLiteral(expression.Tokens, structType, initializers);
|
||||
}
|
||||
|
||||
private NubType ResolveType(NodeType node)
|
||||
{
|
||||
return node switch
|
||||
{
|
||||
NodeTypeBool type => new NubTypeBool(),
|
||||
NodeTypeCustom type => moduleGraph.ResolveStruct(type.Module.Ident, type.Name.Ident),
|
||||
NodeTypeBool => new NubTypeBool(),
|
||||
NodeTypeCustom type => ResolveCustomType(type),
|
||||
NodeTypeFunc type => new NubTypeFunc(type.Parameters.Select(ResolveType).ToList(), ResolveType(type.ReturnType)),
|
||||
NodeTypePointer type => new NubTypePointer(ResolveType(type.To)),
|
||||
NodeTypeSInt type => new NubTypeSInt(type.Width),
|
||||
NodeTypeUInt type => new NubTypeUInt(type.Width),
|
||||
NodeTypeString type => new NubTypeString(),
|
||||
NodeTypeVoid type => new NubTypeVoid(),
|
||||
NodeTypeString => new NubTypeString(),
|
||||
NodeTypeVoid => new NubTypeVoid(),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
|
||||
private NubTypeStruct ResolveCustomType(NodeTypeCustom type)
|
||||
{
|
||||
if (!moduleGraph.TryResolveModule(type.Module.Ident, out var module))
|
||||
throw new CompileException(Diagnostic.Error($"Module '{type.Module.Ident}' not found").At(fileName, type.Module).Build());
|
||||
|
||||
if (!module.TryResolveStructType(type.Module.Ident, out var structType))
|
||||
throw new CompileException(Diagnostic.Error($"Custom type '{type.Module.Ident}::{type.Name.Ident}' not found").At(fileName, type.Name).Build());
|
||||
|
||||
return structType;
|
||||
}
|
||||
|
||||
private class Scope(Scope? parent)
|
||||
{
|
||||
private readonly Dictionary<string, NubType> identifiers = new();
|
||||
|
||||
Reference in New Issue
Block a user