diff options
author | jwansek <eddie.atten.ea29@gmail.com> | 2021-11-08 14:21:59 +0000 |
---|---|---|
committer | jwansek <eddie.atten.ea29@gmail.com> | 2021-11-08 14:21:59 +0000 |
commit | eba31c969bb289bb79f844a23acc1604825d05ff (patch) | |
tree | e542882ed4ff718ddf02751d23b6ee03715a4140 /src/Compiler/ExecuteC.java | |
parent | 975fb6f000918085d1f5ba4ac6eb95c60411dae9 (diff) | |
download | esotericFORTRAN-eba31c969bb289bb79f844a23acc1604825d05ff.tar.gz esotericFORTRAN-eba31c969bb289bb79f844a23acc1604825d05ff.zip |
changed output name to same as source file, added build folder, linux compatability
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; } } |