summaryrefslogtreecommitdiffstats
path: root/src/Compiler/ExecuteC.java
blob: 6240d04fb4afd79339a681e12fcbf2b98e65d1b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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;
import java.nio.file.Paths;
import java.nio.file.Files;

public class ExecuteC {
    public void compileAndExecuteC(List<String> code, String filename) {
        writeProgram(code, filename);
        if (!compileC(filename)){
            String output = runProgram(filename);
            System.out.println(output);
        }
        else{
            Language.displayError("Runtime Error");
        }
    }

    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 = Paths.get("build", String.format("%s.c", filename)).toFile();
            output = new BufferedWriter(new FileWriter(file));
            for(String line:codeLines){
                output.write(line+"\n");
            }
            output.close();
        } catch ( IOException e ) {
            e.printStackTrace();
        }

    }

    public Boolean compileC(String filename){
        try{
            String s= null;
            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;
             while ((s = stdError.readLine()) != null) {
                 error=true;
             }
             return error;
        } catch (IOException e){
            e.printStackTrace();
        }
        return false;
    }

    public String runProgram(String filename){
        
        try{
            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()));
            
            String s = null;
            String output="";
            while ((s = stdInput.readLine()) != null) {
                output+=s;
            }
            return output;
        } catch (IOException e){
            e.printStackTrace();
        }
        System.out.println();
        return null;
    } 
}