99 lines
2.2 KiB
C#
99 lines
2.2 KiB
C#
using CLI;
|
|
using Generation.QBE;
|
|
using Syntax;
|
|
using Syntax.Diagnostics;
|
|
using Syntax.Parsing;
|
|
using Syntax.Tokenization;
|
|
using Syntax.Typing;
|
|
|
|
const string OUT_DIR = "bin-int";
|
|
|
|
Directory.CreateDirectory(OUT_DIR);
|
|
|
|
var files = new List<string>();
|
|
|
|
foreach (var arg in args)
|
|
{
|
|
files.Add(arg);
|
|
}
|
|
|
|
var diagnostics = new List<Diagnostic>();
|
|
var syntaxTrees = new Dictionary<string, SyntaxTree>();
|
|
|
|
foreach (var file in files)
|
|
{
|
|
if (!File.Exists(file))
|
|
{
|
|
Console.Error.WriteLine($"File '{file}' does not exist");
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
foreach (var file in files)
|
|
{
|
|
var content = File.ReadAllText(file);
|
|
var sourceText = new SourceText(file, content);
|
|
|
|
var tokenizeResult = Tokenizer.Tokenize(sourceText, out var tokenizerDiagnostics);
|
|
diagnostics.AddRange(tokenizerDiagnostics);
|
|
|
|
var syntaxTree = Parser.ParseFile(tokenizeResult, out var parseDiagnostics);
|
|
diagnostics.AddRange(parseDiagnostics);
|
|
|
|
if (syntaxTree != null)
|
|
{
|
|
syntaxTrees[file] = syntaxTree;
|
|
}
|
|
}
|
|
|
|
var definitionTable = new DefinitionTable(syntaxTrees.Values);
|
|
|
|
var boundSyntaxTrees = new Dictionary<string, BoundSyntaxTree>();
|
|
|
|
foreach (var (file, syntaxTree) in syntaxTrees)
|
|
{
|
|
var boundSyntaxTree = Binder.Bind(syntaxTree, definitionTable, out var binderDiagnostics);
|
|
diagnostics.AddRange(binderDiagnostics);
|
|
boundSyntaxTrees[file] = boundSyntaxTree;
|
|
}
|
|
|
|
var boundDefinitionTable = new BoundDefinitionTable(boundSyntaxTrees.Values);
|
|
|
|
foreach (var diagnostic in diagnostics)
|
|
{
|
|
Console.Error.WriteLine(diagnostic.FormatANSI());
|
|
}
|
|
|
|
if (diagnostics.Any(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error))
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
var objectFiles = new List<string>();
|
|
|
|
foreach (var file in files)
|
|
{
|
|
var ssa = QBEGenerator.Emit(boundSyntaxTrees[file], boundDefinitionTable);
|
|
var asm = await QBE.Invoke(ssa);
|
|
if (asm == null)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
var fileName = Path.GetTempFileName();
|
|
var asmSuccess = await GCC.Assemble(asm, fileName);
|
|
if (!asmSuccess)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
objectFiles.Add(fileName);
|
|
}
|
|
|
|
var archiveResult = await Archive.Invoke(Path.Join(OUT_DIR, "out.a"), objectFiles);
|
|
if (!archiveResult)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
return 0; |