This commit is contained in:
nub31
2025-10-31 15:18:18 +01:00
parent 7c7624b1bc
commit 40d500fddd
8 changed files with 231 additions and 188 deletions

View File

@@ -1,4 +1,6 @@
using NubLang.Ast;
using NubLang.Diagnostics;
using NubLang.Modules;
using NubLang.Syntax;
using OmniSharp.Extensions.LanguageServer.Protocol;
@@ -8,70 +10,92 @@ public class WorkspaceManager(DiagnosticsPublisher diagnosticsPublisher)
{
private readonly Dictionary<string, SyntaxTree> _syntaxTrees = new();
private readonly Dictionary<string, List<TopLevelNode>> _compilationUnits = new();
// private readonly Dictionary<string, TypedModule> _modules = new();
private ModuleRepository? _repository;
public void Init(string rootPath)
{
// var files = Directory.GetFiles(rootPath, "*.nub", SearchOption.AllDirectories);
// foreach (var path in files)
// {
// var text = File.ReadAllText(path);
// var tokenizer = new Tokenizer(path, text);
//
// tokenizer.Tokenize();
// diagnosticsPublisher.Publish(path, tokenizer.Diagnostics);
//
// var parser = new Parser();
// var parseResult = parser.Parse(tokenizer.Tokens);
// diagnosticsPublisher.Publish(path, parser.Diagnostics);
//
// _syntaxTrees[path] = parseResult;
// }
//
// 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;
// }
var files = Directory.GetFiles(rootPath, "*.nub", SearchOption.AllDirectories);
foreach (var path in files)
{
var text = File.ReadAllText(path);
var tokenizer = new Tokenizer();
tokenizer.Tokenize(path, text);
diagnosticsPublisher.Publish(path, tokenizer.Diagnostics);
var parser = new Parser();
var parseResult = parser.Parse(tokenizer.Tokens);
diagnosticsPublisher.Publish(path, parser.Diagnostics);
_syntaxTrees[path] = parseResult;
}
ModuleRepository repository;
foreach (var (fsPath, syntaxTree) in _syntaxTrees)
{
try
{
repository = ModuleRepository.Create(_syntaxTrees.Select(x => x.Value).ToList());
}
catch (CompileException e)
{
return;
}
var typeChecker = new TypeChecker();
var result = typeChecker.Check(syntaxTree, repository);
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 syntaxTree = parser.Parse(tokenizer.Tokens);
// diagnosticsPublisher.Publish(path, parser.Diagnostics);
// _syntaxTrees[fsPath] = syntaxTree;
//
// 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 RemoveFile(DocumentUri path)
{
var fsPath = path.GetFileSystemPath();
_syntaxTrees.Remove(fsPath);
_compilationUnits.Remove(fsPath);
var text = File.ReadAllText(fsPath);
var tokenizer = new Tokenizer();
tokenizer.Tokenize(fsPath, text);
diagnosticsPublisher.Publish(path, tokenizer.Diagnostics);
var parser = new Parser();
var syntaxTree = parser.Parse(tokenizer.Tokens);
diagnosticsPublisher.Publish(path, parser.Diagnostics);
_syntaxTrees[fsPath] = syntaxTree;
ModuleRepository repository;
try
{
repository = ModuleRepository.Create(_syntaxTrees.Select(x => x.Value).ToList());
}
catch (CompileException e)
{
diagnosticsPublisher.Publish(path, [e.Diagnostic]);
return;
}
var typeChecker = new TypeChecker();
var result = typeChecker.Check(syntaxTree, repository);
diagnosticsPublisher.Publish(fsPath, typeChecker.Diagnostics);
_compilationUnits[fsPath] = result;
}
public List<TopLevelNode>? GetCompilationUnit(DocumentUri path)
{
return _compilationUnits.GetValueOrDefault(path.GetFileSystemPath());
}
public ModuleRepository? GetModuleRepository()
{
try
{
return ModuleRepository.Create(_syntaxTrees.Select(x => x.Value).ToList());
}
catch (CompileException e)
{
return null;
}
}
}