diff options
author | AidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com> | 2021-11-06 01:44:14 +0000 |
---|---|---|
committer | AidenRushbrooke <72034940+AidenRushbrooke@users.noreply.github.com> | 2021-11-06 01:44:14 +0000 |
commit | 0c54d7f8cb4b17d80ed21f7a9916ad27a13e34ed (patch) | |
tree | d64267b7da1691bad8797f81188798fb9628a212 /src/Compiler/ExecuteC.java | |
parent | d3c80f8bd236b1b4ed571ed6b347095efdaa99ed (diff) | |
download | esotericFORTRAN-0c54d7f8cb4b17d80ed21f7a9916ad27a13e34ed.tar.gz esotericFORTRAN-0c54d7f8cb4b17d80ed21f7a9916ad27a13e34ed.zip |
Re-arranged files and added C compilation
Diffstat (limited to 'src/Compiler/ExecuteC.java')
-rw-r--r-- | src/Compiler/ExecuteC.java | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/Compiler/ExecuteC.java b/src/Compiler/ExecuteC.java new file mode 100644 index 0000000..5e32eea --- /dev/null +++ b/src/Compiler/ExecuteC.java @@ -0,0 +1,76 @@ +package Compiler; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.List; + + +public class ExecuteC { + public void compileAndExecuteC(List<String> code){ + writeProgram(code); + if (!compileC()){ + String output = runProgram(); + System.out.println(output); + } + else{ + Language.displayError("Runtime Error"); + } + } + + public void writeProgram(List<String> codeLines){ + BufferedWriter output = null; + try { + File file = new File("main.c"); + output = new BufferedWriter(new FileWriter(file)); + for(String line:codeLines){ + output.write(line+"\n"); + } + output.close(); + } catch ( IOException e ) { + e.printStackTrace(); + } + + } + + public Boolean compileC(){ + try{ + String s= null; + Process p = Runtime.getRuntime().exec("cmd /C gcc main.c -o main.exe"); + BufferedReader stdError = new BufferedReader(new + InputStreamReader(p.getErrorStream())); + boolean error=false; + while ((s = stdError.readLine()) != null) { + error=true; + } + return error; + } catch (IOException e){ + e.printStackTrace(); + } + return false; + } + + public String runProgram(){ + + try{ + String[] command = {"cmd", "/C", "main.exe"}; + ProcessBuilder probuilder = new ProcessBuilder(command); + Process p = probuilder.start(); + BufferedReader stdInput = new BufferedReader(new + InputStreamReader(p.getInputStream())); + + String s = null; + String output=""; + while ((s = stdInput.readLine()) != null) { + output+=s; + } + return output; + } catch (IOException e){ + e.printStackTrace(); + } + return null; + } +} |