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;
    }
}