Remove interfaces
This commit is contained in:
@@ -12,8 +12,4 @@ public record StructFieldNode(string Name, TypeNode Type, Optional<ExpressionNod
|
||||
|
||||
public record StructFuncNode(string Name, FuncSignatureNode Signature, BlockNode Body) : Node;
|
||||
|
||||
public record StructNode(StructTypeNode StructType, string Module, string Name, List<StructFieldNode> Fields, List<StructFuncNode> Functions, List<InterfaceTypeNode> InterfaceImplementations) : DefinitionNode(Module, Name);
|
||||
|
||||
public record InterfaceFuncNode(string Name, FuncSignatureNode Signature) : Node;
|
||||
|
||||
public record InterfaceNode(InterfaceTypeNode InterfaceType, string Module, string Name, List<InterfaceFuncNode> Functions) : DefinitionNode(Module, Name);
|
||||
public record StructNode(StructTypeNode StructType, string Module, string Name, List<StructFieldNode> Fields, List<StructFuncNode> Functions) : DefinitionNode(Module, Name);
|
||||
@@ -33,6 +33,7 @@ public enum BinaryOperator
|
||||
public abstract record ExpressionNode(TypeNode Type) : Node;
|
||||
|
||||
public abstract record LValueExpressionNode(TypeNode Type) : RValueExpressionNode(Type);
|
||||
|
||||
public abstract record RValueExpressionNode(TypeNode Type) : ExpressionNode(Type);
|
||||
|
||||
public record BinaryExpressionNode(TypeNode Type, ExpressionNode Left, BinaryOperator Operator, ExpressionNode Right) : RValueExpressionNode(Type);
|
||||
@@ -43,8 +44,6 @@ public record FuncCallNode(TypeNode Type, ExpressionNode Expression, List<Expres
|
||||
|
||||
public record StructFuncCallNode(TypeNode Type, string Name, StructTypeNode StructType, ExpressionNode StructExpression, List<ExpressionNode> Parameters) : RValueExpressionNode(Type);
|
||||
|
||||
public record InterfaceFuncCallNode(TypeNode Type, string Name, InterfaceTypeNode InterfaceType, ExpressionNode InterfaceExpression, List<ExpressionNode> Parameters) : RValueExpressionNode(Type);
|
||||
|
||||
public record VariableIdentifierNode(TypeNode Type, string Name) : LValueExpressionNode(Type);
|
||||
|
||||
public record FuncParameterIdentifierNode(TypeNode Type, string Name) : RValueExpressionNode(Type);
|
||||
@@ -65,8 +64,6 @@ public record StructInitializerNode(StructTypeNode StructType, Dictionary<string
|
||||
|
||||
public record DereferenceNode(TypeNode Type, ExpressionNode Expression) : RValueExpressionNode(Type);
|
||||
|
||||
public record ConvertToInterfaceNode(TypeNode Type, InterfaceTypeNode InterfaceType, StructTypeNode StructType, ExpressionNode Implementation) : RValueExpressionNode(Type);
|
||||
|
||||
public record ConvertIntNode(TypeNode Type, ExpressionNode Value, IntTypeNode ValueType, IntTypeNode TargetType) : RValueExpressionNode(Type);
|
||||
|
||||
public record ConvertFloatNode(TypeNode Type, ExpressionNode Value, FloatTypeNode ValueType, FloatTypeNode TargetType) : RValueExpressionNode(Type);
|
||||
public record ConvertFloatNode(TypeNode Type, ExpressionNode Value, FloatTypeNode ValueType, FloatTypeNode TargetType) : RValueExpressionNode(Type);
|
||||
@@ -187,36 +187,18 @@ public class StructTypeFunc(string name, FuncTypeNode type)
|
||||
public FuncTypeNode Type { get; } = type;
|
||||
}
|
||||
|
||||
public class StructTypeNode(string module, string name, List<StructTypeField> fields, List<StructTypeFunc> functions, List<InterfaceTypeNode> interfaceImplementations) : ComplexTypeNode
|
||||
public class StructTypeNode(string module, string name, List<StructTypeField> fields, List<StructTypeFunc> functions) : ComplexTypeNode
|
||||
{
|
||||
public string Module { get; } = module;
|
||||
public string Name { get; } = name;
|
||||
public List<StructTypeField> Fields { get; set; } = fields;
|
||||
public List<StructTypeFunc> Functions { get; set; } = functions;
|
||||
public List<InterfaceTypeNode> InterfaceImplementations { get; set; } = interfaceImplementations;
|
||||
|
||||
public override string ToString() => Name;
|
||||
public override bool Equals(TypeNode? other) => other is StructTypeNode structType && Name == structType.Name && Module == structType.Module;
|
||||
public override int GetHashCode() => HashCode.Combine(typeof(StructTypeNode), Name);
|
||||
}
|
||||
|
||||
public class InterfaceTypeFunc(string name, FuncTypeNode type)
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public FuncTypeNode Type { get; } = type;
|
||||
}
|
||||
|
||||
public class InterfaceTypeNode(string module, string name, List<InterfaceTypeFunc> functions) : ComplexTypeNode
|
||||
{
|
||||
public string Module { get; } = module;
|
||||
public string Name { get; } = name;
|
||||
public List<InterfaceTypeFunc> Functions { get; set; } = functions;
|
||||
|
||||
public override string ToString() => Name;
|
||||
public override bool Equals(TypeNode? other) => other is InterfaceTypeNode interfaceType && Name == interfaceType.Name && Module == interfaceType.Module;
|
||||
public override int GetHashCode() => HashCode.Combine(typeof(InterfaceTypeNode), Name);
|
||||
}
|
||||
|
||||
public class ArrayTypeNode(TypeNode elementType) : ComplexTypeNode
|
||||
{
|
||||
public TypeNode ElementType { get; } = elementType;
|
||||
|
||||
@@ -16,7 +16,6 @@ public sealed class TypeChecker
|
||||
private readonly Stack<TypeNode> _funcReturnTypes = [];
|
||||
private readonly List<Diagnostic> _diagnostics = [];
|
||||
private readonly List<StructTypeNode> _referencedStructTypes = [];
|
||||
private readonly List<InterfaceTypeNode> _referencedInterfaceTypes = [];
|
||||
private readonly List<DefinitionNode> _definitions = [];
|
||||
|
||||
private Scope Scope => _scopes.Peek();
|
||||
@@ -33,7 +32,6 @@ public sealed class TypeChecker
|
||||
public List<DefinitionNode> Definitions => _definitions;
|
||||
public List<Diagnostic> Diagnostics => _diagnostics;
|
||||
public List<StructTypeNode> ReferencedStructTypes => _referencedStructTypes;
|
||||
public List<InterfaceTypeNode> ReferencedInterfaceTypes => _referencedInterfaceTypes;
|
||||
|
||||
public void Check()
|
||||
{
|
||||
@@ -41,7 +39,6 @@ public sealed class TypeChecker
|
||||
_funcReturnTypes.Clear();
|
||||
_diagnostics.Clear();
|
||||
_referencedStructTypes.Clear();
|
||||
_referencedInterfaceTypes.Clear();
|
||||
_definitions.Clear();
|
||||
|
||||
foreach (var definition in _syntaxTree.Definitions)
|
||||
@@ -61,32 +58,12 @@ public sealed class TypeChecker
|
||||
{
|
||||
return node switch
|
||||
{
|
||||
InterfaceSyntax definition => CheckInterfaceDefinition(definition),
|
||||
FuncSyntax definition => CheckFuncDefinition(definition),
|
||||
StructSyntax definition => CheckStructDefinition(definition),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(node))
|
||||
};
|
||||
}
|
||||
|
||||
private InterfaceNode CheckInterfaceDefinition(InterfaceSyntax node)
|
||||
{
|
||||
var functions = node.Functions
|
||||
.Select(function => new InterfaceFuncNode(function.Name, CheckFuncSignature(function.Signature)))
|
||||
.ToList();
|
||||
|
||||
var functionTypes = new List<InterfaceTypeFunc>();
|
||||
foreach (var function in node.Functions)
|
||||
{
|
||||
var parameters = function.Signature.Parameters.Select(x => ResolveType(x.Type)).ToList();
|
||||
var funcType = new FuncTypeNode(parameters, ResolveType(function.Signature.ReturnType));
|
||||
functionTypes.Add(new InterfaceTypeFunc(function.Name, funcType));
|
||||
}
|
||||
|
||||
var type = new InterfaceTypeNode(_syntaxTree.Metadata.ModuleName, node.Name, functionTypes);
|
||||
|
||||
return new InterfaceNode(type, _syntaxTree.Metadata.ModuleName, node.Name, functions);
|
||||
}
|
||||
|
||||
private StructNode CheckStructDefinition(StructSyntax node)
|
||||
{
|
||||
var fields = new List<StructFieldNode>();
|
||||
@@ -117,19 +94,6 @@ public sealed class TypeChecker
|
||||
functions.Add(new StructFuncNode(function.Name, CheckFuncSignature(function.Signature), body));
|
||||
}
|
||||
|
||||
var interfaceImplementations = new List<InterfaceTypeNode>();
|
||||
foreach (var interfaceImplementation in node.InterfaceImplementations)
|
||||
{
|
||||
var resolvedType = ResolveType(interfaceImplementation);
|
||||
if (resolvedType is not InterfaceTypeNode interfaceType)
|
||||
{
|
||||
_diagnostics.Add(Diagnostic.Error($"Struct {node.Name} cannot implement non-struct type {interfaceImplementation}").At(interfaceImplementation).Build());
|
||||
continue;
|
||||
}
|
||||
|
||||
interfaceImplementations.Add(interfaceType);
|
||||
}
|
||||
|
||||
var fieldTypes = node.Fields
|
||||
.Select(x => new StructTypeField(x.Name, ResolveType(x.Type), x.Value.HasValue))
|
||||
.ToList();
|
||||
@@ -142,22 +106,9 @@ public sealed class TypeChecker
|
||||
functionTypes.Add(new StructTypeFunc(function.Name, funcType));
|
||||
}
|
||||
|
||||
var interfaceImplementationTypes = new List<InterfaceTypeNode>();
|
||||
foreach (var interfaceImplementation in node.InterfaceImplementations)
|
||||
{
|
||||
var resolvedType = ResolveType(interfaceImplementation);
|
||||
if (resolvedType is not InterfaceTypeNode interfaceType)
|
||||
{
|
||||
_diagnostics.Add(Diagnostic.Error($"Struct {node.Name} cannot implement non-struct type {interfaceImplementation}").At(interfaceImplementation).Build());
|
||||
continue;
|
||||
}
|
||||
var type = new StructTypeNode(_syntaxTree.Metadata.ModuleName, node.Name, fieldTypes, functionTypes);
|
||||
|
||||
interfaceImplementationTypes.Add(interfaceType);
|
||||
}
|
||||
|
||||
var type = new StructTypeNode(_syntaxTree.Metadata.ModuleName, node.Name, fieldTypes, functionTypes, interfaceImplementationTypes);
|
||||
|
||||
return new StructNode(type, _syntaxTree.Metadata.ModuleName, node.Name, fields, functions, interfaceImplementations);
|
||||
return new StructNode(type, _syntaxTree.Metadata.ModuleName, node.Name, fields, functions);
|
||||
}
|
||||
|
||||
private FuncNode CheckFuncDefinition(FuncSyntax node)
|
||||
@@ -289,11 +240,6 @@ public sealed class TypeChecker
|
||||
return result;
|
||||
}
|
||||
|
||||
if (result.Type is StructTypeNode structType && expectedType is InterfaceTypeNode interfaceType)
|
||||
{
|
||||
return new ConvertToInterfaceNode(interfaceType, interfaceType, structType, result);
|
||||
}
|
||||
|
||||
if (result.Type is IntTypeNode sourceIntType && expectedType is IntTypeNode targetIntType)
|
||||
{
|
||||
if (sourceIntType.Signed == targetIntType.Signed && sourceIntType.Width < targetIntType.Width)
|
||||
@@ -586,7 +532,7 @@ public sealed class TypeChecker
|
||||
|
||||
if (!_resolvingTypes.Add(key))
|
||||
{
|
||||
var placeholder = new StructTypeNode(customType.Module, customType.Name, new List<StructTypeField>(), [], []);
|
||||
var placeholder = new StructTypeNode(customType.Module, customType.Name, [], []);
|
||||
_typeCache[key] = placeholder;
|
||||
return placeholder;
|
||||
}
|
||||
@@ -603,30 +549,18 @@ public sealed class TypeChecker
|
||||
var structType = module.StructTypes(includePrivate).FirstOrDefault(x => x.Name == customType.Name);
|
||||
if (structType != null)
|
||||
{
|
||||
var result = new StructTypeNode(customType.Module, structType.Name, [], [], []);
|
||||
var result = new StructTypeNode(customType.Module, structType.Name, [], []);
|
||||
_typeCache[key] = result;
|
||||
|
||||
var fields = structType.Fields.Select(x => new StructTypeField(x.Name, ResolveType(x.Type), x.HasDefaultValue)).ToList();
|
||||
result.Fields.AddRange(fields);
|
||||
|
||||
// todo(nub31): Functions and interface implementations
|
||||
// todo(nub31): Function implementations
|
||||
|
||||
_referencedStructTypes.Add(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
var interfaceType = module.InterfaceTypes(includePrivate).FirstOrDefault(x => x.Name == customType.Name);
|
||||
if (interfaceType != null)
|
||||
{
|
||||
var result = new InterfaceTypeNode(customType.Module, interfaceType.Name, []);
|
||||
_typeCache[key] = result;
|
||||
|
||||
// todo(nub31): Put func resolution code here
|
||||
|
||||
_referencedInterfaceTypes.AddRange(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
throw new TypeCheckerException(Diagnostic.Error($"Type {customType.Name} not found in module {customType.Module}").At(customType).Build());
|
||||
}
|
||||
finally
|
||||
|
||||
Reference in New Issue
Block a user