diff options
Diffstat (limited to 'src/Compiler')
-rw-r--r-- | src/Compiler/ExecuteC.java | 51 | ||||
-rw-r--r-- | src/Compiler/Language.java | 10 |
2 files changed, 43 insertions, 18 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; } } diff --git a/src/Compiler/Language.java b/src/Compiler/Language.java index 44168db..bb7e235 100644 --- a/src/Compiler/Language.java +++ b/src/Compiler/Language.java @@ -10,7 +10,6 @@ import java.util.Scanner; public class Language { static boolean hadError = false; public static void main(String[] args){ - //Allow users to input a single line of code //Still needs some work to re-ask for input after each line if (args.length < 1){ @@ -19,7 +18,7 @@ public class Language { while (sourceCode!=""){ System.out.print("Code: "); sourceCode = input.nextLine(); - runInterpreter(sourceCode); + runInterpreter(sourceCode, "out"); hadError=false; } input.close(); @@ -28,7 +27,7 @@ public class Language { } else if (args.length==1){ try { String sourcecode = Files.readString(Paths.get(args[0])); //Maybe should set charset here - runInterpreter(sourcecode); + runInterpreter(sourcecode, args[0].split("\\.(?=[^\\.]+$)")[0]); } catch (IOException exception){ System.out.println("File not found"); } @@ -40,8 +39,7 @@ public class Language { } //Function to take source code and output the result - private static void runInterpreter(String sourceCode){ - + private static void runInterpreter(String sourceCode, String outName){ //Extract tokens from the source code TokenScanner scanner = new TokenScanner(); List<Token> tokens = scanner.extractTokens(sourceCode); @@ -62,7 +60,7 @@ public class Language { //Execute created C code ExecuteC cExecutor = new ExecuteC(); - cExecutor.compileAndExecuteC(code); + cExecutor.compileAndExecuteC(code, outName); } |