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 (module, signature) in _moduleSignatures)
{ {
foreach (var structType in signature.StructTypes) foreach (var structType in signature.ExportedStructTypes)
{ {
EmitStructType(module, structType); EmitStructType(module, structType);
_writer.NewLine(); _writer.NewLine();

View File

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

View File

@@ -21,8 +21,6 @@ public class ModuleSignature
foreach (var def in syntaxTree.Definitions) foreach (var def in syntaxTree.Definitions)
{ {
// if (!def.Exported) continue;
switch (def) switch (def)
{ {
case FuncSyntax funcDef: case FuncSyntax funcDef:
@@ -31,7 +29,7 @@ public class ModuleSignature
var returnType = TypeResolver.ResolveType(funcDef.Signature.ReturnType, modules); var returnType = TypeResolver.ResolveType(funcDef.Signature.ReturnType, modules);
var type = new FuncTypeNode(parameters, returnType, funcDef.ExternSymbol); 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; break;
} }
case InterfaceSyntax interfaceDef: case InterfaceSyntax interfaceDef:
@@ -46,7 +44,7 @@ public class ModuleSignature
} }
var type = new InterfaceTypeNode(moduleName, interfaceDef.Name, functions); var type = new InterfaceTypeNode(moduleName, interfaceDef.Name, functions);
module._interfaces.Add(type); module._interfaces.Add(new ModuleMember<InterfaceTypeNode>(type, def.Exported));
break; break;
} }
case StructSyntax structDef: case StructSyntax structDef:
@@ -83,7 +81,7 @@ public class ModuleSignature
} }
var type = new StructTypeNode(moduleName, structDef.Name, fields, functions, interfaceImplementations); var type = new StructTypeNode(moduleName, structDef.Name, fields, functions, interfaceImplementations);
module._structs.Add(type); module._structs.Add(new ModuleMember<StructTypeNode>(type, def.Exported));
break; break;
} }
default: default:
@@ -97,11 +95,27 @@ public class ModuleSignature
return modules; return modules;
} }
private readonly List<StructTypeNode> _structs = []; private class ModuleMember<T>
private readonly List<InterfaceTypeNode> _interfaces = []; {
private readonly Dictionary<string, FuncTypeNode> _functions = []; public ModuleMember(T value, bool exported)
{
Value = value;
Exported = exported;
}
public IReadOnlyList<StructTypeNode> StructTypes => _structs; public T Value { get; set; }
public IReadOnlyList<InterfaceTypeNode> InterfaceTypes => _interfaces; public bool Exported { get; set; }
public IReadOnlyDictionary<string, FuncTypeNode> Functions => _functions; }
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 // Second, look in the current module for a function matching the identifier
var module = _moduleSignatures[_syntaxTree.Metadata.ModuleName]; 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); 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); 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) if (structType != null)
{ {
return structType; return structType;
} }
var interfaceType = module.InterfaceTypes.FirstOrDefault(x => x.Name == typeName); var interfaceType = module.AllInterfaceTypes.FirstOrDefault(x => x.Name == typeName);
if (interfaceType != null) if (interfaceType != null)
{ {
return interfaceType; return interfaceType;
@@ -53,7 +53,7 @@ public static class TypeResolver
throw new Exception("Module not found: " + moduleName); 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) if (structType != null)
{ {
return structType; return structType;
@@ -69,7 +69,7 @@ public static class TypeResolver
throw new Exception("Module not found: " + moduleName); 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) if (structType != null)
{ {
return structType; return structType;
@@ -85,7 +85,7 @@ public static class TypeResolver
throw new Exception("Module not found: " + moduleName); 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; return funcType;
} }

View File

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