Refactor main
This commit is contained in:
@@ -3,50 +3,58 @@ using Nub.Lang.Frontend.Lexing;
|
|||||||
using Nub.Lang.Frontend.Parsing;
|
using Nub.Lang.Frontend.Parsing;
|
||||||
using Nub.Lang.Frontend.Typing;
|
using Nub.Lang.Frontend.Typing;
|
||||||
|
|
||||||
List<ModuleNode> modules = [];
|
namespace Nub.Lang;
|
||||||
|
|
||||||
var lexer = new Lexer();
|
internal static class Program
|
||||||
var parser = new Parser();
|
|
||||||
|
|
||||||
Parse(args[0]);
|
|
||||||
|
|
||||||
void Parse(string path)
|
|
||||||
{
|
{
|
||||||
var files = Directory.EnumerateFiles(path, "*.nub", SearchOption.TopDirectoryOnly);
|
private static readonly Lexer Lexer = new();
|
||||||
|
private static readonly Parser Parser = new();
|
||||||
List<Token> tokens = [];
|
|
||||||
foreach (var file in files)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var src = File.ReadAllText(file);
|
var modules = RunFrontend(args[0]);
|
||||||
tokens.AddRange(lexer.Lex(src));
|
|
||||||
|
var definitions = modules.SelectMany(f => f.Definitions).ToArray();
|
||||||
|
|
||||||
|
var typer = new ExpressionTyper(definitions);
|
||||||
|
typer.Populate();
|
||||||
|
|
||||||
|
var generator = new Generator(definitions);
|
||||||
|
var asm = generator.Generate();
|
||||||
|
|
||||||
|
Console.WriteLine(asm);
|
||||||
|
|
||||||
|
File.WriteAllText(args[1], asm);
|
||||||
}
|
}
|
||||||
|
|
||||||
var module = parser.ParseModule(tokens, path);
|
private static IEnumerable<ModuleNode> RunFrontend(string path)
|
||||||
modules.Add(module);
|
|
||||||
|
|
||||||
foreach (var import in module.Imports)
|
|
||||||
{
|
{
|
||||||
var importPath = Path.GetFullPath(import, module.Path);
|
List<ModuleNode> modules = [];
|
||||||
if (modules.All(m => m.Path != importPath))
|
RunFrontend(path, modules);
|
||||||
|
return modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void RunFrontend(string path, List<ModuleNode> modules)
|
||||||
|
{
|
||||||
|
var files = Directory.EnumerateFiles(path, "*.nub", SearchOption.TopDirectoryOnly);
|
||||||
|
|
||||||
|
List<Token> tokens = [];
|
||||||
|
foreach (var file in files)
|
||||||
{
|
{
|
||||||
Parse(importPath);
|
var src = File.ReadAllText(file);
|
||||||
|
tokens.AddRange(Lexer.Lex(src));
|
||||||
|
}
|
||||||
|
|
||||||
|
var module = Parser.ParseModule(tokens, path);
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var moduleNode in modules)
|
|
||||||
{
|
|
||||||
Console.WriteLine(moduleNode.Path);
|
|
||||||
}
|
|
||||||
|
|
||||||
var definitions = modules.SelectMany(f => f.Definitions).ToArray();
|
|
||||||
|
|
||||||
var typer = new ExpressionTyper(definitions);
|
|
||||||
typer.Populate();
|
|
||||||
|
|
||||||
var generator = new Generator(definitions);
|
|
||||||
var asm = generator.Generate();
|
|
||||||
|
|
||||||
Console.WriteLine(asm);
|
|
||||||
|
|
||||||
File.WriteAllText(args[1], asm);
|
|
||||||
Reference in New Issue
Block a user