...
This commit is contained in:
29
compiler/NubLang.CLI/Archive.cs
Normal file
29
compiler/NubLang.CLI/Archive.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace NubLang.CLI;
|
||||
|
||||
public static class Archive
|
||||
{
|
||||
public static async Task<bool> Invoke(string fileName, params IEnumerable<string> objectFiles)
|
||||
{
|
||||
using var process = new Process();
|
||||
process.StartInfo = new ProcessStartInfo("ar", ["rcs", fileName, ..objectFiles])
|
||||
{
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
|
||||
process.Start();
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
var errors = await process.StandardError.ReadToEndAsync();
|
||||
if (!string.IsNullOrWhiteSpace(errors))
|
||||
{
|
||||
await Console.Error.WriteLineAsync("ar error when archiving:\n" + errors);
|
||||
}
|
||||
|
||||
return process.ExitCode == 0;
|
||||
}
|
||||
}
|
||||
30
compiler/NubLang.CLI/GCC.cs
Normal file
30
compiler/NubLang.CLI/GCC.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace NubLang.CLI;
|
||||
|
||||
public static class GCC
|
||||
{
|
||||
public static async Task<bool> Assemble(string asmPath, string outPath)
|
||||
{
|
||||
using var process = new Process();
|
||||
process.StartInfo = new ProcessStartInfo("x86_64-elf-as", ["-nostartfiles", "-o", outPath, asmPath])
|
||||
{
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
|
||||
process.Start();
|
||||
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
var errors = await process.StandardError.ReadToEndAsync();
|
||||
if (!string.IsNullOrWhiteSpace(errors))
|
||||
{
|
||||
await Console.Error.WriteLineAsync("gcc error when assembling:\n" + errors);
|
||||
}
|
||||
|
||||
return process.ExitCode == 0;
|
||||
}
|
||||
}
|
||||
16
compiler/NubLang.CLI/NubLang.CLI.csproj
Normal file
16
compiler/NubLang.CLI/NubLang.CLI.csproj
Normal file
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<AssemblyName>nubc</AssemblyName>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<PublishAot>true</PublishAot>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\NubLang\NubLang.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
9
compiler/NubLang.CLI/Options.cs
Normal file
9
compiler/NubLang.CLI/Options.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using NubLang.Code;
|
||||
|
||||
namespace NubLang.CLI;
|
||||
|
||||
public class Options
|
||||
{
|
||||
public string? OutputPath { get; set; }
|
||||
public List<SourceFile> Files { get; } = [];
|
||||
}
|
||||
138
compiler/NubLang.CLI/Program.cs
Normal file
138
compiler/NubLang.CLI/Program.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System.Reflection;
|
||||
using NubLang.CLI;
|
||||
using NubLang.Code;
|
||||
using NubLang.Diagnostics;
|
||||
using NubLang.Generation.QBE;
|
||||
using NubLang.Parsing;
|
||||
using NubLang.Parsing.Syntax;
|
||||
using NubLang.Tokenization;
|
||||
using NubLang.TypeChecking;
|
||||
using Module = NubLang.TypeChecking.Module;
|
||||
|
||||
var options = new Options();
|
||||
|
||||
for (var i = 0; i < args.Length; i++)
|
||||
{
|
||||
var arg = args[i];
|
||||
switch (arg)
|
||||
{
|
||||
case "-o":
|
||||
{
|
||||
++i;
|
||||
if (i >= args.Length)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
options.OutputPath = args[i];
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
options.Files.Add(new SourceFile(arg));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var file in options.Files)
|
||||
{
|
||||
if (!File.Exists(file.Path))
|
||||
{
|
||||
Console.Error.WriteLine($"File '{file}' does not exist");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
var diagnostics = new List<Diagnostic>();
|
||||
|
||||
var syntaxTrees = new List<SyntaxTree>();
|
||||
foreach (var file in options.Files)
|
||||
{
|
||||
var tokenizer = new Tokenizer(file);
|
||||
var tokens = tokenizer.Tokenize().ToList();
|
||||
diagnostics.AddRange(tokenizer.GetDiagnostics());
|
||||
|
||||
var parser = new Parser();
|
||||
var syntaxTree = parser.Parse(tokens);
|
||||
|
||||
diagnostics.AddRange(parser.GetDiagnostics());
|
||||
|
||||
syntaxTrees.Add(syntaxTree);
|
||||
}
|
||||
|
||||
var moduleSignatures = ModuleSignature.CollectFromSyntaxTrees(syntaxTrees);
|
||||
var modules = Module.CollectFromSyntaxTrees(syntaxTrees);
|
||||
|
||||
var typedModules = new List<TypedModule>();
|
||||
|
||||
foreach (var module in modules)
|
||||
{
|
||||
var typeChecker = new TypeChecker(module, moduleSignatures);
|
||||
var typedModule = typeChecker.CheckModule();
|
||||
diagnostics.AddRange(typeChecker.GetDiagnostics());
|
||||
typedModules.Add(typedModule);
|
||||
}
|
||||
|
||||
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>();
|
||||
|
||||
for (var i = 0; i < typedModules.Count; i++)
|
||||
{
|
||||
var typedModule = typedModules[i];
|
||||
var outFileName = Path.Combine(".build", Path.ChangeExtension(options.Files[i].Path, null));
|
||||
|
||||
var outFileDir = Path.GetDirectoryName(outFileName);
|
||||
if (!string.IsNullOrEmpty(outFileDir))
|
||||
{
|
||||
Directory.CreateDirectory(outFileDir);
|
||||
}
|
||||
|
||||
var generator = new QBEGenerator(typedModule, moduleSignatures);
|
||||
var ssa = generator.Emit();
|
||||
|
||||
var ssaFilePath = Path.ChangeExtension(outFileName, "ssa");
|
||||
File.WriteAllText(ssaFilePath, ssa);
|
||||
|
||||
var asmFilePath = Path.ChangeExtension(outFileName, "s");
|
||||
var qbeSuccess = await QBE.Invoke(ssaFilePath, asmFilePath);
|
||||
|
||||
if (!qbeSuccess)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
var objFilePath = Path.ChangeExtension(outFileName, "o");
|
||||
var asmSuccess = await GCC.Assemble(asmFilePath, objFilePath);
|
||||
|
||||
if (!asmSuccess)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
objectFiles.Add(objFilePath);
|
||||
}
|
||||
|
||||
var outPath = options.OutputPath ?? Path.Combine("out.a");
|
||||
var outDir = Path.GetDirectoryName(outPath);
|
||||
if (!string.IsNullOrEmpty(outDir))
|
||||
{
|
||||
Directory.CreateDirectory(outDir);
|
||||
}
|
||||
|
||||
var archiveResult = await Archive.Invoke(outPath, objectFiles);
|
||||
if (!archiveResult)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
30
compiler/NubLang.CLI/QBE.cs
Normal file
30
compiler/NubLang.CLI/QBE.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace NubLang.CLI;
|
||||
|
||||
public static class QBE
|
||||
{
|
||||
public static async Task<bool> Invoke(string ssaPath, string outPath)
|
||||
{
|
||||
using var process = new Process();
|
||||
process.StartInfo = new ProcessStartInfo("qbe", ["-o", outPath, ssaPath])
|
||||
{
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
|
||||
process.Start();
|
||||
|
||||
await process.WaitForExitAsync();
|
||||
|
||||
var errors = await process.StandardError.ReadToEndAsync();
|
||||
if (!string.IsNullOrWhiteSpace(errors))
|
||||
{
|
||||
await Console.Error.WriteLineAsync("qbe error:\n" + errors);
|
||||
}
|
||||
|
||||
return process.ExitCode == 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user