87 lines
2.6 KiB
C#
87 lines
2.6 KiB
C#
using Nub.Lang.Backend;
|
|
using Nub.Lang.Frontend.Lexing;
|
|
using Nub.Lang.Frontend.Parsing;
|
|
using Nub.Lang.Frontend.Typing;
|
|
|
|
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");
|
|
return 1;
|
|
}
|
|
|
|
var input = Path.GetFullPath(args[0]);
|
|
var output = Path.GetFullPath(args[1]);
|
|
|
|
if (!Directory.Exists(input))
|
|
{
|
|
Console.WriteLine($"Error: Input directory '{input}' 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;
|
|
}
|
|
|
|
var modules = RunFrontend(input);
|
|
var definitions = modules.SelectMany(f => f.Definitions).ToList();
|
|
|
|
var typeChecker = new TypeChecker(definitions);
|
|
typeChecker.TypeCheck();
|
|
|
|
var generator = new Generator(definitions);
|
|
var result = generator.Generate();
|
|
|
|
File.WriteAllText(output, result);
|
|
return 0;
|
|
}
|
|
|
|
private static List<ModuleNode> RunFrontend(string rootFilePath)
|
|
{
|
|
List<ModuleNode> modules = [];
|
|
RunFrontend(rootFilePath, modules);
|
|
return modules;
|
|
}
|
|
|
|
private static void RunFrontend(string rootFilePath, List<ModuleNode> modules)
|
|
{
|
|
var filePaths = Directory.EnumerateFiles(rootFilePath, "*.nub", SearchOption.TopDirectoryOnly);
|
|
|
|
List<Token> tokens = [];
|
|
foreach (var filePath in filePaths)
|
|
{
|
|
var src = File.ReadAllText(filePath);
|
|
tokens.AddRange(Lexer.Lex(src, new SourceFile(filePath, src)));
|
|
}
|
|
|
|
var module = Parser.ParseModule(tokens, rootFilePath);
|
|
modules.Add(module);
|
|
|
|
foreach (var import in module.Imports)
|
|
{
|
|
var importPath = Path.GetFullPath(import, module.Path);
|
|
if (modules.All(m => m.Path != importPath))
|
|
{
|
|
RunFrontend(importPath, modules);
|
|
}
|
|
}
|
|
}
|
|
} |