diff options
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | src/Compiler/ExecuteC.java | 51 | ||||
| -rw-r--r-- | src/Compiler/Language.java | 10 | ||||
| -rw-r--r-- | src/Makefile | 7 | ||||
| -rw-r--r-- | src/anotherexample.txt | 6 | ||||
| -rw-r--r-- | src/main.c | 10 | ||||
| -rw-r--r-- | src/main.exe | bin | 297288 -> 0 bytes | 
7 files changed, 60 insertions, 28 deletions
| @@ -1,3 +1,7 @@ +*.exe +src/.vscode/ +src/build/ +  *.class  code/simpleSableCCCalulator/sableCCCalculator/analysis/  code/simpleSableCCCalulator/sableCCCalculator/lexer/ 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);      } diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..3c2bfda --- /dev/null +++ b/src/Makefile @@ -0,0 +1,7 @@ +all: +	javac Compiler/*.java + +clean: +	rm -vf Compiler/*.class +	rm -vf *.c +	rm -vfr build/
\ No newline at end of file diff --git a/src/anotherexample.txt b/src/anotherexample.txt new file mode 100644 index 0000000..21100b2 --- /dev/null +++ b/src/anotherexample.txt @@ -0,0 +1,6 @@ +character (len=10)::hello +hello="hello" +if 4==5 then +hello="goodbye " +endif +print *,hello,6," world" endprint
\ No newline at end of file diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 1e6ed2b..0000000 --- a/src/main.c +++ /dev/null @@ -1,10 +0,0 @@ -#include <stdio.h> -#include <string.h> -int main(){ -char hello[11]; -strcpy(hello,"hello"); -if(5==5){ -strcpy(hello,"goodbye "); -} -printf("%s%d%s",hello,6," test"); -} diff --git a/src/main.exe b/src/main.exeBinary files differ deleted file mode 100644 index 4f0961a..0000000 --- a/src/main.exe +++ /dev/null | 
