diff options
author | Alfie Eagleton <67986414+TheAlfanator@users.noreply.github.com> | 2021-11-08 17:28:30 +0000 |
---|---|---|
committer | Alfie Eagleton <67986414+TheAlfanator@users.noreply.github.com> | 2021-11-08 17:28:30 +0000 |
commit | 03380520e95699ca41c74439f6216685ab8da6b7 (patch) | |
tree | 89a46bc6e1e85b2647d5b0e4bed529e2c0414798 /src/Compiler/ExecuteC.java | |
parent | 8157d9bee1c0d6ce0e59ac45bcad0d011cbe10a3 (diff) | |
parent | a12094123dcacee41a7472031db6fe6027b083a7 (diff) | |
download | esotericFORTRAN-03380520e95699ca41c74439f6216685ab8da6b7.tar.gz esotericFORTRAN-03380520e95699ca41c74439f6216685ab8da6b7.zip |
Merge branch 'main' of https://github.com/AlfieEagleton/EsotericProject
Diffstat (limited to 'src/Compiler/ExecuteC.java')
-rw-r--r-- | src/Compiler/ExecuteC.java | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/src/Compiler/ExecuteC.java b/src/Compiler/ExecuteC.java index 5e32eea..6240d04 100644 --- a/src/Compiler/ExecuteC.java +++ b/src/Compiler/ExecuteC.java @@ -7,13 +7,14 @@ import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; - +import java.nio.file.Paths; +import java.nio.file.Files; public class ExecuteC { - public void compileAndExecuteC(List<String> code){ - writeProgram(code); - if (!compileC()){ - String output = runProgram(); + public void compileAndExecuteC(List<String> code, String filename) { + writeProgram(code, filename); + if (!compileC(filename)){ + String output = runProgram(filename); System.out.println(output); } else{ @@ -21,10 +22,20 @@ public class ExecuteC { } } - public void writeProgram(List<String> codeLines){ + public void compileAndExecuteC(List<String> code){ + compileAndExecuteC(code, "main"); + } + + public void writeProgram(List<String> codeLines, String filename){ + try { + Files.createDirectories(Paths.get("build")); + } catch (IOException e) { + e.printStackTrace(); + } + BufferedWriter output = null; try { - File file = new File("main.c"); + File file = Paths.get("build", String.format("%s.c", filename)).toFile(); output = new BufferedWriter(new FileWriter(file)); for(String line:codeLines){ output.write(line+"\n"); @@ -36,10 +47,19 @@ public class ExecuteC { } - public Boolean compileC(){ + public Boolean compileC(String filename){ try{ String s= null; - Process p = Runtime.getRuntime().exec("cmd /C gcc main.c -o main.exe"); + Process p; + if (System.getProperty("os.name").equals("Linux")) { + p = Runtime.getRuntime().exec(String.format("gcc build/%s.c -o build/%s", filename, filename)); + } else { + p = Runtime.getRuntime().exec(String.format( + "cmd /C gcc %s -o %s", + Paths.get("build", String.format("%s.c", filename)).toString(), + Paths.get("build", String.format("%s.exe", filename)).toString() + )); + } BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); boolean error=false; @@ -53,11 +73,17 @@ public class ExecuteC { return false; } - public String runProgram(){ + public String runProgram(String filename){ try{ - String[] command = {"cmd", "/C", "main.exe"}; - ProcessBuilder probuilder = new ProcessBuilder(command); + ProcessBuilder probuilder; + if (System.getProperty("os.name").equals("Linux")) { + String[] command = {"sh", "-c", String.format("./build/%s", filename)}; + probuilder = new ProcessBuilder(command); + } else { + String[] command = {"cmd", "/C", Paths.get("build", String.format("%s.exe", filename)).toString()}; + probuilder = new ProcessBuilder(command); + } Process p = probuilder.start(); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); @@ -71,6 +97,7 @@ public class ExecuteC { } catch (IOException e){ e.printStackTrace(); } + System.out.println(); return null; } } |