From f88c1e0455189ca2bc7371de7af3529a377757b7 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Sun, 4 May 2025 13:53:19 +0100 Subject: [PATCH] Made the outputted .exe be run on built Outputs the exit code of the process as that is currently the only way to output information with the language. --- LX-Build/CommandProcess.cs | 72 +++++++++++++++++++++++++------------- LX-Build/Main.cs | 19 ++++++---- example/main.lx | 2 +- 3 files changed, 61 insertions(+), 32 deletions(-) diff --git a/LX-Build/CommandProcess.cs b/LX-Build/CommandProcess.cs index 6be0671..6299986 100644 --- a/LX-Build/CommandProcess.cs +++ b/LX-Build/CommandProcess.cs @@ -1,37 +1,15 @@ using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace LX_Build { internal class CommandProcess { - private readonly string m_ErrorMessage; - private readonly int m_ExitCode; + private string m_ErrorMessage = string.Empty; + private int m_ExitCode = 0; - public int ExitCode() => m_ExitCode; - - public string Error() => m_ErrorMessage; - - public CommandProcess(string command) + private void HandleProcess(Process process) { - // Creates a process to run the command // - ProcessStartInfo info = new() - { - FileName = "cmd.exe", - Arguments = $"/c {command}", - RedirectStandardError = true, - RedirectStandardOutput = true, - UseShellExecute = false, - CreateNoWindow = true, - }; - - // Starts the process // - using Process process = Process.Start(info); - // Reads the streams of the output // string output = process.StandardOutput.ReadToEnd(); string error = process.StandardError.ReadToEnd(); @@ -43,5 +21,49 @@ namespace LX_Build m_ExitCode = process.ExitCode; m_ErrorMessage = error; } + + public CommandProcess(string command) + { + // Creates a process to run the command // + ProcessStartInfo info = new() + { + FileName = command, + Arguments = "", + RedirectStandardError = true, + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + // Starts the process and checks it is running // + using Process? process = Process.Start(info) + ?? throw new Exception($"Process failed to start: [{command}]"); + + HandleProcess(process); + } + + public CommandProcess(string command, string arguments) + { + // Creates a process to run the command // + ProcessStartInfo info = new() + { + FileName = command, + Arguments = arguments, + RedirectStandardError = true, + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true, + }; + + // Starts the process and checks it is running // + using Process? process = Process.Start(info) + ?? throw new Exception($"Process failed to start: [{command} {arguments}]"); + + HandleProcess(process); + } + + public int ExitCode() => m_ExitCode; + + public string Error() => m_ErrorMessage; } } diff --git a/LX-Build/Main.cs b/LX-Build/Main.cs index 59331a0..19ea274 100644 --- a/LX-Build/Main.cs +++ b/LX-Build/Main.cs @@ -7,11 +7,11 @@ namespace LX_Build { static void CompileToObj(string inPath, string outPath) { - // The command to compile the IR to .obj // - string command = $"llc -filetype=obj -o \"{outPath}\" \"{inPath}\""; + // The arguments to compiler LLVM IR to object files // + string arguments = $"-filetype=obj -o \"{outPath}\" \"{inPath}\""; // Runs the command // - CommandProcess process = new(command); + CommandProcess process = new("llc", arguments); if (process.ExitCode() == 0) { @@ -24,11 +24,11 @@ namespace LX_Build static void LinkToExe(string objectFile) { - // The command to link the object file to an .exe // - string command = $"lld-link /OUT:example/Main.exe {objectFile} /ENTRY:main"; + // The arguments to turn object files into an .exe // + string arguments = $"/OUT:example/Main.exe {objectFile} /ENTRY:main"; // Runs the command // - CommandProcess process = new(command); + CommandProcess process = new("lld-link", arguments); if (process.ExitCode() == 0) { @@ -52,6 +52,13 @@ namespace LX_Build // Links the object file to an .exe // LinkToExe("example/main.obj"); + + // Runs the outputted .exe // + string command = "example/Main.exe"; + CommandProcess exe = new(command); + + // Outputs that the program ended with {x} exit code // + Console.WriteLine("\nProcess {Main.exe} finished with exit code: " + exe.ExitCode()); } } } diff --git a/example/main.lx b/example/main.lx index d8bfbd4..a505872 100644 --- a/example/main.lx +++ b/example/main.lx @@ -1,7 +1,7 @@ func main() { int a - a = 783412 + a = 7834 int b b = 6