Fix infinite recursion
This commit is contained in:
@@ -177,19 +177,19 @@ public class StringTypeNode : ComplexTypeNode
|
||||
public class StructTypeNode(string name, IReadOnlyList<TypeNode> fields, IReadOnlyList<FuncTypeNode> functions, IReadOnlyList<InterfaceTypeNode> interfaceImplementations) : ComplexTypeNode
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public IReadOnlyList<TypeNode> Fields { get; } = fields;
|
||||
public IReadOnlyList<FuncTypeNode> Functions { get; } = functions;
|
||||
public IReadOnlyList<InterfaceTypeNode> InterfaceImplementations { get; } = interfaceImplementations;
|
||||
public IReadOnlyList<TypeNode> Fields { get; set; } = fields;
|
||||
public IReadOnlyList<FuncTypeNode> Functions { get; set; } = functions;
|
||||
public IReadOnlyList<InterfaceTypeNode> InterfaceImplementations { get; set; } = interfaceImplementations;
|
||||
|
||||
public override string ToString() => Name;
|
||||
public override bool Equals(TypeNode? other) => other is StructTypeNode custom && Name == custom.Name;
|
||||
public override int GetHashCode() => HashCode.Combine(typeof(StructTypeNode), Name);
|
||||
}
|
||||
|
||||
public class InterfaceTypeNode(string name, IReadOnlyList<FuncTypeNode> funcs) : ComplexTypeNode
|
||||
public class InterfaceTypeNode(string name, IReadOnlyList<FuncTypeNode> functions) : ComplexTypeNode
|
||||
{
|
||||
public string Name { get; } = name;
|
||||
public IReadOnlyList<FuncTypeNode> Funcs { get; } = funcs;
|
||||
public IReadOnlyList<FuncTypeNode> Functions { get; set; } = functions;
|
||||
|
||||
public override string ToString() => Name;
|
||||
public override bool Equals(TypeNode? other) => other is InterfaceTypeNode custom && Name == custom.Name;
|
||||
|
||||
@@ -646,8 +646,15 @@ public sealed class TypeChecker
|
||||
};
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, TypeNode> _typeCache = new();
|
||||
|
||||
private TypeNode CheckCustomType(CustomTypeSyntax type)
|
||||
{
|
||||
if (_typeCache.TryGetValue(type.Name, out var cachedType))
|
||||
{
|
||||
return cachedType;
|
||||
}
|
||||
|
||||
var structs = _definitionTable.LookupStruct(type.Name).ToArray();
|
||||
if (structs.Length > 0)
|
||||
{
|
||||
@@ -658,6 +665,9 @@ public sealed class TypeChecker
|
||||
|
||||
var @struct = structs[0];
|
||||
|
||||
var result = new StructTypeNode(type.Name, [], [], []);
|
||||
_typeCache.Add(type.Name, result);
|
||||
|
||||
var fields = @struct.Fields.Select(x => CheckType(x.Type)).ToList();
|
||||
|
||||
var funcs = @struct.Functions
|
||||
@@ -677,7 +687,10 @@ public sealed class TypeChecker
|
||||
interfaceImplementations.Add(interfaceType);
|
||||
}
|
||||
|
||||
return new StructTypeNode(type.Name, fields, funcs, interfaceImplementations);
|
||||
result.Fields = fields;
|
||||
result.Functions = funcs;
|
||||
result.InterfaceImplementations = interfaceImplementations;
|
||||
return result;
|
||||
}
|
||||
|
||||
var interfaces = _definitionTable.LookupInterface(type.Name).ToArray();
|
||||
@@ -689,11 +702,16 @@ public sealed class TypeChecker
|
||||
}
|
||||
|
||||
var @interface = interfaces[0];
|
||||
|
||||
var result = new InterfaceTypeNode(type.Name, []);
|
||||
_typeCache.Add(type.Name, result);
|
||||
|
||||
var functions = @interface.Functions
|
||||
.Select(x => new FuncTypeNode(x.Signature.Parameters.Select(y => CheckType(y.Type)).ToList(), CheckType(x.Signature.ReturnType)))
|
||||
.ToList();
|
||||
|
||||
return new InterfaceTypeNode(type.Name, functions);
|
||||
result.Functions = functions;
|
||||
return result;
|
||||
}
|
||||
|
||||
throw new TypeCheckerException(Diagnostic.Error($"Type {type.Name} is not defined").Build());
|
||||
|
||||
Reference in New Issue
Block a user