From 8c76f75f7c1365f0aa444dc49d60ab014e9b2db9 Mon Sep 17 00:00:00 2001 From: nub31 Date: Thu, 11 Sep 2025 22:03:30 +0200 Subject: [PATCH] ... --- .../NubLang/Generation/QBE/QBEGenerator.cs | 2 +- compiler/NubLang/Parsing/Parser.cs | 11 ++++-- compiler/NubLang/TypeChecking/Module.cs | 36 +++++++++++++------ compiler/NubLang/TypeChecking/TypeChecker.cs | 2 +- compiler/NubLang/TypeResolver.cs | 10 +++--- example/makefile | 2 +- 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/compiler/NubLang/Generation/QBE/QBEGenerator.cs b/compiler/NubLang/Generation/QBE/QBEGenerator.cs index e4d3eac..643495f 100644 --- a/compiler/NubLang/Generation/QBE/QBEGenerator.cs +++ b/compiler/NubLang/Generation/QBE/QBEGenerator.cs @@ -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(); diff --git a/compiler/NubLang/Parsing/Parser.cs b/compiler/NubLang/Parsing/Parser.cs index f4f40cc..8e6012e 100644 --- a/compiler/NubLang/Parsing/Parser.cs +++ b/compiler/NubLang/Parsing/Parser.cs @@ -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; + } } } diff --git a/compiler/NubLang/TypeChecking/Module.cs b/compiler/NubLang/TypeChecking/Module.cs index 365394b..cb4bc50 100644 --- a/compiler/NubLang/TypeChecking/Module.cs +++ b/compiler/NubLang/TypeChecking/Module.cs @@ -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(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(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(type, def.Exported)); break; } default: @@ -97,11 +95,27 @@ public class ModuleSignature return modules; } - private readonly List _structs = []; - private readonly List _interfaces = []; - private readonly Dictionary _functions = []; + private class ModuleMember + { + public ModuleMember(T value, bool exported) + { + Value = value; + Exported = exported; + } - public IReadOnlyList StructTypes => _structs; - public IReadOnlyList InterfaceTypes => _interfaces; - public IReadOnlyDictionary Functions => _functions; + public T Value { get; set; } + public bool Exported { get; set; } + } + + private readonly List> _structs = []; + private readonly List> _interfaces = []; + private readonly Dictionary> _functions = []; + + public IReadOnlyList ExportedStructTypes => _structs.Where(x => x.Exported).Select(x => x.Value).ToList(); + public IReadOnlyList ExportedInterfaceTypes => _interfaces.Where(x => x.Exported).Select(x => x.Value).ToList(); + public IReadOnlyDictionary ExportedFunctions => _functions.Where(x => x.Value.Exported).ToDictionary(x => x.Key, x => x.Value.Value); + + public IReadOnlyList AllStructTypes => _structs.Select(x => x.Value).ToList(); + public IReadOnlyList AllInterfaceTypes => _interfaces.Select(x => x.Value).ToList(); + public IReadOnlyDictionary AllFunctions => _functions.ToDictionary(x => x.Key, x => x.Value.Value); } \ No newline at end of file diff --git a/compiler/NubLang/TypeChecking/TypeChecker.cs b/compiler/NubLang/TypeChecking/TypeChecker.cs index 114ba37..8681652 100644 --- a/compiler/NubLang/TypeChecking/TypeChecker.cs +++ b/compiler/NubLang/TypeChecking/TypeChecker.cs @@ -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); } diff --git a/compiler/NubLang/TypeResolver.cs b/compiler/NubLang/TypeResolver.cs index 6bbdec2..d457da4 100644 --- a/compiler/NubLang/TypeResolver.cs +++ b/compiler/NubLang/TypeResolver.cs @@ -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; } diff --git a/example/makefile b/example/makefile index d2eebc9..c159d03 100644 --- a/example/makefile +++ b/example/makefile @@ -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):