diff options
Diffstat (limited to 'src/Compiler/ExecuteC.java')
-rw-r--r-- | src/Compiler/ExecuteC.java | 71 |
1 files changed, 47 insertions, 24 deletions
diff --git a/src/Compiler/ExecuteC.java b/src/Compiler/ExecuteC.java index 035813a..dd26e3c 100644 --- a/src/Compiler/ExecuteC.java +++ b/src/Compiler/ExecuteC.java @@ -8,34 +8,54 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import java.nio.file.Paths; +import java.nio.file.Path; import java.nio.file.Files; public class ExecuteC { - public void compileAndExecuteC(List<String> code, String filename) { - writeProgram(code, filename); - if (!compileC(filename)){ - String output = runProgram(filename); - System.out.println(output); + + Path buildToDir; + String filename; + + public void compileAndExecuteC(List<String> code, String outPathStr, boolean executeAfter, boolean leaveCFile) { + + Path outPath = Path.of(outPathStr); + int numElements = outPath.getNameCount(); + filename = outPath.getName(numElements - 1).toString(); + if (numElements == 1) { + buildToDir = Paths.get("build"); + } else { + buildToDir = outPath.subpath(0, numElements - 1); + } + + try { + Files.createDirectories(buildToDir); + } catch (IOException e) { + e.printStackTrace(); } - else{ + + File cProgram = writeProgram(code); + if (!compileC()) { + if (executeAfter) { + String output = runProgram(); + System.out.println(output); + } + } else { Language.displayError("Runtime Error"); } + if (!leaveCFile) { + cProgram.delete(); + } } - public void compileAndExecuteC(List<String> code){ - compileAndExecuteC(code, "main"); + public void compileAndExecuteC(List<String> code) { + compileAndExecuteC(code, "main", true, true); } - public void writeProgram(List<String> codeLines, String filename){ - try { - Files.createDirectories(Paths.get("build")); - } catch (IOException e) { - e.printStackTrace(); - } - + public File writeProgram(List<String> codeLines){ BufferedWriter output = null; + File file = null; try { - File file = Paths.get("build", String.format("%s.c", filename)).toFile(); + file = Paths.get(buildToDir.toString(), String.format("%s.c", filename)).toFile(); output = new BufferedWriter(new FileWriter(file)); for(String line:codeLines){ output.write(line+"\n"); @@ -44,20 +64,23 @@ public class ExecuteC { } catch ( IOException e ) { e.printStackTrace(); } - + return file; } - public Boolean compileC(String filename){ + public Boolean compileC(){ try{ String s= null; Process p; if (System.getProperty("os.name").equals("Linux")) { - p = Runtime.getRuntime().exec(String.format("gcc build/%s.c -o build/%s", filename, filename)); + p = Runtime.getRuntime().exec(String.format( + "gcc %s/%s.c -o %s/%s", + buildToDir.toString(), filename, buildToDir.toString(), 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() + Paths.get(buildToDir.toString(), String.format("%s.c", filename)).toString(), + Paths.get(buildToDir.toString(), String.format("%s.exe", filename)).toString() )); } BufferedReader stdError = new BufferedReader(new @@ -73,15 +96,15 @@ public class ExecuteC { return false; } - public String runProgram(String filename){ + public String runProgram(){ try{ ProcessBuilder probuilder; if (System.getProperty("os.name").equals("Linux")) { - String[] command = {"sh", "-c", String.format("./build/%s", filename)}; + String[] command = {"sh", "-c", Paths.get(buildToDir.toString(), filename).toString()}; probuilder = new ProcessBuilder(command); } else { - String[] command = {"cmd", "/C", Paths.get("build", String.format("%s.exe", filename)).toString()}; + String[] command = {"cmd", "/C", Paths.get(buildToDir.toString(), String.format("%s.exe", filename)).toString()}; probuilder = new ProcessBuilder(command); } Process p = probuilder.start(); |