66 lines
1.8 KiB
C#
66 lines
1.8 KiB
C#
using System.Diagnostics;
|
|
using Compiler;
|
|
|
|
var moduleGraphBuilder = ModuleGraph.Create();
|
|
var asts = new List<Ast>();
|
|
|
|
foreach (var fileName in args)
|
|
{
|
|
var file = File.ReadAllText(fileName);
|
|
|
|
var tokens = Tokenizer.Tokenize(fileName, file, out var tokenizerDiagnostics);
|
|
|
|
foreach (var diagnostic in tokenizerDiagnostics)
|
|
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
|
|
|
if (tokenizerDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
|
return 1;
|
|
|
|
var ast = Parser.Parse(fileName, tokens, out var parserDiagnostics);
|
|
|
|
foreach (var diagnostic in parserDiagnostics)
|
|
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
|
|
|
if (parserDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
|
return 1;
|
|
|
|
moduleGraphBuilder.AddAst(ast);
|
|
asts.Add(ast);
|
|
}
|
|
|
|
var moduleGraph = moduleGraphBuilder.Build(out var moduleGraphDiagnostics);
|
|
|
|
foreach (var diagnostic in moduleGraphDiagnostics)
|
|
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
|
|
|
if (moduleGraphDiagnostics.Any(x => x.Severity == DiagnosticSeverity.Error))
|
|
return 1;
|
|
|
|
var functions = new List<TypedNodeDefinitionFunc>();
|
|
|
|
foreach (var ast in asts)
|
|
{
|
|
foreach (var func in ast.Definitions.OfType<NodeDefinitionFunc>())
|
|
{
|
|
var typedFunction = TypeChecker.CheckFunction(ast.FileName, func, moduleGraph, out var typeCheckerDiagnostics);
|
|
|
|
foreach (var diagnostic in typeCheckerDiagnostics)
|
|
DiagnosticFormatter.Print(diagnostic, Console.Error);
|
|
|
|
if (typedFunction == null)
|
|
return 1;
|
|
|
|
functions.Add(typedFunction);
|
|
}
|
|
}
|
|
|
|
var output = Generator.Emit(functions, moduleGraph);
|
|
|
|
Directory.Delete(".build", recursive: true);
|
|
Directory.CreateDirectory(".build");
|
|
|
|
File.WriteAllText(".build/out.c", output);
|
|
|
|
Process.Start("gcc", ["-Og", "-g", "-o", ".build/out", ".build/out.c"]);
|
|
|
|
return 0; |