...
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Nub.Lang.Backend;
|
||||
using Nub.Lang.Frontend;
|
||||
using Nub.Lang.Backend;
|
||||
using Nub.Lang.Frontend.Diagnostics;
|
||||
using Nub.Lang.Frontend.Lexing;
|
||||
using Nub.Lang.Frontend.Parsing;
|
||||
@@ -10,94 +8,57 @@ namespace Nub.Lang;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
private static readonly Lexer Lexer = new();
|
||||
private static readonly Parser Parser = new();
|
||||
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
if (args.Length != 2)
|
||||
{
|
||||
Console.WriteLine("Usage: nub <input-dir> <output-file>");
|
||||
Console.WriteLine("Example: nub src out.asm");
|
||||
Console.Error.WriteLine("Usage: nub <input-dir>");
|
||||
Console.Error.WriteLine("Example: nub src");
|
||||
return 1;
|
||||
}
|
||||
|
||||
var input = Path.GetFullPath(args[0]);
|
||||
var output = Path.GetFullPath(args[1]);
|
||||
var srcDir = Path.GetFullPath(args[0]);
|
||||
|
||||
if (!Directory.Exists(input))
|
||||
if (!Directory.Exists(srcDir))
|
||||
{
|
||||
Console.WriteLine($"Error: Input directory '{input}' does not exist.");
|
||||
Console.Error.WriteLine($"Error: Input directory '{srcDir}' does not exist.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
var outputDir = Path.GetDirectoryName(output);
|
||||
if (outputDir == null || !Directory.Exists(outputDir))
|
||||
{
|
||||
Console.WriteLine($"Error: Output directory '{outputDir}' does not exist.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Path.GetFileName(output)))
|
||||
{
|
||||
Console.WriteLine("Error: Output path must specify a file, not a directory.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (TryRunFrontend(input, out var definitions))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
var generator = new Generator(definitions);
|
||||
var result = generator.Generate();
|
||||
|
||||
File.WriteAllText(output, result);
|
||||
return 0;
|
||||
return Compile(srcDir);
|
||||
}
|
||||
|
||||
private static bool TryRunFrontend(string rootFilePath, out List<DefinitionNode> definitions)
|
||||
private static int Compile(string srcDir)
|
||||
{
|
||||
List<ModuleNode> modules = [];
|
||||
var error = TryRunFrontend(rootFilePath, modules);
|
||||
definitions = modules.SelectMany(f => f.Definitions).ToList();
|
||||
|
||||
var typeChecker = new TypeChecker(definitions);
|
||||
var typeCheckResult = typeChecker.TypeCheck();
|
||||
typeCheckResult.PrintAllDiagnostics();
|
||||
error = error || typeCheckResult.HasErrors;
|
||||
return error;
|
||||
}
|
||||
var lexer = new Lexer();
|
||||
var parser = new Parser();
|
||||
var typeChecker = new TypeChecker();
|
||||
|
||||
private static bool TryRunFrontend(string rootFilePath, List<ModuleNode> modules)
|
||||
{
|
||||
var error = false;
|
||||
var filePaths = Directory.EnumerateFiles(rootFilePath, "*.nub", SearchOption.TopDirectoryOnly);
|
||||
|
||||
List<Token> tokens = [];
|
||||
foreach (var filePath in filePaths)
|
||||
List<SourceFile> files = [];
|
||||
foreach (var file in Directory.EnumerateFiles(srcDir, "*.nub", SearchOption.AllDirectories))
|
||||
{
|
||||
var src = File.ReadAllText(filePath);
|
||||
tokens.AddRange(Lexer.Lex(src, new SourceFile(filePath, src)));
|
||||
}
|
||||
var content = File.ReadAllText(file);
|
||||
|
||||
var parseResult = Parser.ParseModule(tokens, rootFilePath);
|
||||
parseResult.PrintAllDiagnostics();
|
||||
var tokenizeResult = lexer.Tokenize(new SourceText(file, content));
|
||||
tokenizeResult.PrintAllDiagnostics();
|
||||
|
||||
modules.Add(parseResult.Value);
|
||||
var parseResult = parser.ParseModule(tokenizeResult.Value);
|
||||
parseResult.PrintAllDiagnostics();
|
||||
|
||||
foreach (var import in parseResult.Value.Imports)
|
||||
{
|
||||
var importPath = Path.GetFullPath(import, parseResult.Value.Path);
|
||||
if (modules.All(m => m.Path != importPath))
|
||||
if (parseResult.Value != null)
|
||||
{
|
||||
if (!TryRunFrontend(importPath, modules))
|
||||
{
|
||||
error = true;
|
||||
}
|
||||
files.Add(parseResult.Value);
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
var typeCheckResult = typeChecker.TypeCheck(files);
|
||||
typeCheckResult.PrintAllDiagnostics();
|
||||
|
||||
var generator = new Generator(typeCheckResult.Value);
|
||||
var result = generator.Generate();
|
||||
|
||||
Console.Out.Write(result);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user