using NubLang.Ast;
using NubLang.Diagnostics;
using NubLang.Syntax;

var diagnostics = new List<Diagnostic>();
var syntaxTrees = new List<SyntaxTree>();

foreach (var file in args)
{
    var tokenizer = new Tokenizer(file, File.ReadAllText(file));
    tokenizer.Tokenize();
    diagnostics.AddRange(tokenizer.Diagnostics);

    var parser = new Parser();
    var syntaxTree = parser.Parse(tokenizer.Tokens);
    diagnostics.AddRange(parser.Diagnostics);

    syntaxTrees.Add(syntaxTree);
}

var modules = Module.Collect(syntaxTrees);
var compilationUnits = new List<CompilationUnit>();

for (var i = 0; i < args.Length; i++)
{
    var typeChecker = new TypeChecker(syntaxTrees[i], modules);
    var compilationUnit = typeChecker.Check();

    compilationUnits.Add(compilationUnit);
    diagnostics.AddRange(typeChecker.Diagnostics);
}

foreach (var diagnostic in diagnostics)
{
    Console.Error.WriteLine(diagnostic.FormatANSI());
}

if (diagnostics.Any(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error))
{
    return 1;
}

return 0;