This commit is contained in:
nub31
2025-09-11 22:03:30 +02:00
parent 2af3782e32
commit 9c2975d45f
6 changed files with 42 additions and 21 deletions

View File

@@ -44,7 +44,7 @@ public class QBEGenerator
foreach (var (module, signature) in _moduleSignatures)
{
foreach (var structType in signature.StructTypes)
foreach (var structType in signature.ExportedStructTypes)
{
EmitStructType(module, structType);
_writer.NewLine();

View File

@@ -44,7 +44,7 @@ public sealed class Parser
while (TryExpectSymbol(Symbol.Import))
{
imports.Add(ExpectIdentifier().Value);
imports.Add(ExpectLiteral(LiteralKind.String).Value);
}
}
catch (ParseException e)
@@ -629,7 +629,14 @@ public sealed class Parser
catch (ParseException ex)
{
_diagnostics.Add(ex.Diagnostic);
Next();
if (HasToken)
{
Next();
}
else
{
break;
}
}
}

View File

@@ -21,8 +21,6 @@ public class ModuleSignature
foreach (var def in syntaxTree.Definitions)
{
// if (!def.Exported) continue;
switch (def)
{
case FuncSyntax funcDef:
@@ -31,7 +29,7 @@ public class ModuleSignature
var returnType = TypeResolver.ResolveType(funcDef.Signature.ReturnType, modules);
var type = new FuncTypeNode(parameters, returnType, funcDef.ExternSymbol);
module._functions.Add(funcDef.Name, type);
module._functions.Add(funcDef.Name, new ModuleMember<FuncTypeNode>(type, def.Exported));
break;
}
case InterfaceSyntax interfaceDef:
@@ -46,7 +44,7 @@ public class ModuleSignature
}
var type = new InterfaceTypeNode(moduleName, interfaceDef.Name, functions);
module._interfaces.Add(type);
module._interfaces.Add(new ModuleMember<InterfaceTypeNode>(type, def.Exported));
break;
}
case StructSyntax structDef:
@@ -83,7 +81,7 @@ public class ModuleSignature
}
var type = new StructTypeNode(moduleName, structDef.Name, fields, functions, interfaceImplementations);
module._structs.Add(type);
module._structs.Add(new ModuleMember<StructTypeNode>(type, def.Exported));
break;
}
default:
@@ -97,11 +95,27 @@ public class ModuleSignature
return modules;
}
private readonly List<StructTypeNode> _structs = [];
private readonly List<InterfaceTypeNode> _interfaces = [];
private readonly Dictionary<string, FuncTypeNode> _functions = [];
private class ModuleMember<T>
{
public ModuleMember(T value, bool exported)
{
Value = value;
Exported = exported;
}
public IReadOnlyList<StructTypeNode> StructTypes => _structs;
public IReadOnlyList<InterfaceTypeNode> InterfaceTypes => _interfaces;
public IReadOnlyDictionary<string, FuncTypeNode> Functions => _functions;
public T Value { get; set; }
public bool Exported { get; set; }
}
private readonly List<ModuleMember<StructTypeNode>> _structs = [];
private readonly List<ModuleMember<InterfaceTypeNode>> _interfaces = [];
private readonly Dictionary<string, ModuleMember<FuncTypeNode>> _functions = [];
public IReadOnlyList<StructTypeNode> ExportedStructTypes => _structs.Where(x => x.Exported).Select(x => x.Value).ToList();
public IReadOnlyList<InterfaceTypeNode> ExportedInterfaceTypes => _interfaces.Where(x => x.Exported).Select(x => x.Value).ToList();
public IReadOnlyDictionary<string, FuncTypeNode> ExportedFunctions => _functions.Where(x => x.Value.Exported).ToDictionary(x => x.Key, x => x.Value.Value);
public IReadOnlyList<StructTypeNode> AllStructTypes => _structs.Select(x => x.Value).ToList();
public IReadOnlyList<InterfaceTypeNode> AllInterfaceTypes => _interfaces.Select(x => x.Value).ToList();
public IReadOnlyDictionary<string, FuncTypeNode> AllFunctions => _functions.ToDictionary(x => x.Key, x => x.Value.Value);
}

View File

@@ -347,7 +347,7 @@ public sealed class TypeChecker
// Second, look in the current module for a function matching the identifier
var module = _moduleSignatures[_syntaxTree.Metadata.ModuleName];
if (module.Functions.TryGetValue(expression.Name, out var function))
if (module.AllFunctions.TryGetValue(expression.Name, out var function))
{
return new FuncIdentifierNode(function, _syntaxTree.Metadata.ModuleName, expression.Name);
}

View File

@@ -31,13 +31,13 @@ public static class TypeResolver
throw new Exception("Module not found: " + moduleName);
}
var structType = module.StructTypes.FirstOrDefault(x => x.Name == typeName);
var structType = module.AllStructTypes.FirstOrDefault(x => x.Name == typeName);
if (structType != null)
{
return structType;
}
var interfaceType = module.InterfaceTypes.FirstOrDefault(x => x.Name == typeName);
var interfaceType = module.AllInterfaceTypes.FirstOrDefault(x => x.Name == typeName);
if (interfaceType != null)
{
return interfaceType;
@@ -53,7 +53,7 @@ public static class TypeResolver
throw new Exception("Module not found: " + moduleName);
}
var structType = module.StructTypes.FirstOrDefault(x => x.Name == structName);
var structType = module.AllStructTypes.FirstOrDefault(x => x.Name == structName);
if (structType != null)
{
return structType;
@@ -69,7 +69,7 @@ public static class TypeResolver
throw new Exception("Module not found: " + moduleName);
}
var structType = module.InterfaceTypes.FirstOrDefault(x => x.Name == interfaceName);
var structType = module.AllInterfaceTypes.FirstOrDefault(x => x.Name == interfaceName);
if (structType != null)
{
return structType;
@@ -85,7 +85,7 @@ public static class TypeResolver
throw new Exception("Module not found: " + moduleName);
}
if (module.Functions.TryGetValue(funcName, out var funcType))
if (module.AllFunctions.TryGetValue(funcName, out var funcType))
{
return funcType;
}

View File

@@ -4,7 +4,7 @@ out: .build/out.o
gcc -nostartfiles -o out x86_64.s .build/out.o
.build/out.o: $(NUBC) main.nub
$(NUBC) main.nub
$(NUBC) main.nub module.nub
.PHONY: $(NUBC)
$(NUBC):