This commit is contained in:
nub31
2025-10-23 18:32:03 +02:00
parent db5d444cf2
commit bfe8b7b18e
4 changed files with 4217 additions and 177 deletions

View File

@@ -1,12 +1,10 @@
using NubLang.Ast;
using NubLang.Generation;
using NubLang.Syntax;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
namespace NubLang.LSP;
public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher, ILanguageServerFacade server)
public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher)
{
private readonly Dictionary<string, SyntaxTree> _syntaxTrees = new();
private readonly Dictionary<string, CompilationUnit> _compilationUnits = new();
@@ -29,47 +27,37 @@ public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher, ILangua
_syntaxTrees[path] = parseResult;
}
Generate();
foreach (var (fsPath, syntaxTree) in _syntaxTrees)
{
var modules = Module.Collect(_syntaxTrees.Select(x => x.Value).ToList());
var typeChecker = new TypeChecker(syntaxTree, modules);
var result = typeChecker.Check();
diagnosticsPublisher.Publish(fsPath, typeChecker.Diagnostics);
_compilationUnits[fsPath] = result;
}
}
public void UpdateFile(DocumentUri path)
{
var fsPath = path.GetFileSystemPath();
var text = File.ReadAllText(fsPath);
var tokenizer = new Tokenizer(fsPath, text);
tokenizer.Tokenize();
diagnosticsPublisher.Publish(path, tokenizer.Diagnostics);
var parser = new Parser();
var parseResult = parser.Parse(tokenizer.Tokens);
var syntaxTree = parser.Parse(tokenizer.Tokens);
diagnosticsPublisher.Publish(path, parser.Diagnostics);
_syntaxTrees[fsPath] = syntaxTree;
_syntaxTrees[fsPath] = parseResult;
Generate();
}
private void Generate()
{
var modules = Module.Collect(_syntaxTrees.Select(x => x.Value).ToList());
foreach (var (fsPath, syntaxTree) in _syntaxTrees)
{
var typeChecker = new TypeChecker(syntaxTree, modules);
var result = typeChecker.Check();
diagnosticsPublisher.Publish(fsPath, typeChecker.Diagnostics);
_compilationUnits[fsPath] = result;
var generator = new Generator(result);
var c = generator.Emit();
server.SendNotification("nub/output", new
{
content = c,
path = fsPath
});
}
var typeChecker = new TypeChecker(syntaxTree, modules);
var result = typeChecker.Check();
diagnosticsPublisher.Publish(fsPath, typeChecker.Diagnostics);
_compilationUnits[fsPath] = result;
}
public void RemoveFile(DocumentUri path)
@@ -79,11 +67,6 @@ public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher, ILangua
_compilationUnits.Remove(fsPath);
}
public Dictionary<string, CompilationUnit> GetCompilationUnits()
{
return _compilationUnits;
}
public CompilationUnit? GetCompilationUnit(DocumentUri path)
{
return _compilationUnits.GetValueOrDefault(path.GetFileSystemPath());