95 lines
3.5 KiB
C#
95 lines
3.5 KiB
C#
using NubLang.Parsing.Syntax;
|
|
using NubLang.TypeChecking;
|
|
using NubLang.TypeChecking.Node;
|
|
|
|
namespace NubLang;
|
|
|
|
public static class TypeResolver
|
|
{
|
|
public static TypeNode ResolveType(TypeSyntax type, IReadOnlyDictionary<string, ModuleSignature> modules)
|
|
{
|
|
return type switch
|
|
{
|
|
BoolTypeSyntax => new BoolTypeNode(),
|
|
CStringTypeSyntax => new CStringTypeNode(),
|
|
IntTypeSyntax i => new IntTypeNode(i.Signed, i.Width),
|
|
CustomTypeSyntax c => ResolveCustomType(c.Module, c.Name, modules),
|
|
FloatTypeSyntax f => new FloatTypeNode(f.Width),
|
|
FuncTypeSyntax func => new FuncTypeNode(func.Parameters.Select(x => ResolveType(x, modules)).ToList(), ResolveType(func.ReturnType, modules)),
|
|
ArrayTypeSyntax arr => new ArrayTypeNode(ResolveType(arr.BaseType, modules)),
|
|
PointerTypeSyntax ptr => new PointerTypeNode(ResolveType(ptr.BaseType, modules)),
|
|
StringTypeSyntax => new StringTypeNode(),
|
|
VoidTypeSyntax => new VoidTypeNode(),
|
|
_ => throw new NotSupportedException($"Unknown type syntax: {type}")
|
|
};
|
|
}
|
|
|
|
public static TypeNode ResolveCustomType(string moduleName, string typeName, IReadOnlyDictionary<string, ModuleSignature> modules)
|
|
{
|
|
if (!modules.TryGetValue(moduleName, out var module))
|
|
{
|
|
throw new Exception("Module not found: " + moduleName);
|
|
}
|
|
|
|
var structType = module.StructTypes.FirstOrDefault(x => x.Name == typeName);
|
|
if (structType != null)
|
|
{
|
|
return structType;
|
|
}
|
|
|
|
var interfaceType = module.InterfaceTypes.FirstOrDefault(x => x.Name == typeName);
|
|
if (interfaceType != null)
|
|
{
|
|
return interfaceType;
|
|
}
|
|
|
|
throw new Exception($"Type {typeName} not found in module {moduleName}");
|
|
}
|
|
|
|
public static StructTypeNode ResolveStructType(string moduleName, string structName, IReadOnlyDictionary<string, ModuleSignature> modules)
|
|
{
|
|
if (!modules.TryGetValue(moduleName, out var module))
|
|
{
|
|
throw new Exception("Module not found: " + moduleName);
|
|
}
|
|
|
|
var structType = module.StructTypes.FirstOrDefault(x => x.Name == structName);
|
|
if (structType != null)
|
|
{
|
|
return structType;
|
|
}
|
|
|
|
throw new Exception($"Struct type {structName} not found in module {moduleName}");
|
|
}
|
|
|
|
public static InterfaceTypeNode ResolveInterfaceType(string moduleName, string interfaceName, IReadOnlyDictionary<string, ModuleSignature> modules)
|
|
{
|
|
if (!modules.TryGetValue(moduleName, out var module))
|
|
{
|
|
throw new Exception("Module not found: " + moduleName);
|
|
}
|
|
|
|
var structType = module.InterfaceTypes.FirstOrDefault(x => x.Name == interfaceName);
|
|
if (structType != null)
|
|
{
|
|
return structType;
|
|
}
|
|
|
|
throw new Exception($"Interface type {interfaceName} not found in module {moduleName}");
|
|
}
|
|
|
|
public static FuncTypeNode ResolveFunctionType(string moduleName, string funcName, IReadOnlyDictionary<string, ModuleSignature> modules)
|
|
{
|
|
if (!modules.TryGetValue(moduleName, out var module))
|
|
{
|
|
throw new Exception("Module not found: " + moduleName);
|
|
}
|
|
|
|
if (module.Functions.TryGetValue(funcName, out var funcType))
|
|
{
|
|
return funcType;
|
|
}
|
|
|
|
throw new Exception($"Function type {funcName} not found in module {moduleName}");
|
|
}
|
|
} |