From eba31c969bb289bb79f844a23acc1604825d05ff Mon Sep 17 00:00:00 2001
From: jwansek <eddie.atten.ea29@gmail.com>
Date: Mon, 8 Nov 2021 14:21:59 +0000
Subject: changed output name to same as source file, added build folder, linux
 compatability

---
 src/Compiler/ExecuteC.java |  51 ++++++++++++++++++++++++++++++++++-----------
 src/Compiler/Language.java |  10 ++++-----
 src/Makefile               |   7 +++++++
 src/anotherexample.txt     |   6 ++++++
 src/main.c                 |  10 ---------
 src/main.exe               | Bin 297288 -> 0 bytes
 6 files changed, 56 insertions(+), 28 deletions(-)
 create mode 100644 src/Makefile
 create mode 100644 src/anotherexample.txt
 delete mode 100644 src/main.c
 delete mode 100644 src/main.exe

(limited to 'src')

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.exe
deleted file mode 100644
index 4f0961a..0000000
Binary files a/src/main.exe and /dev/null differ
-- 
cgit v1.2.3