summaryrefslogtreecommitdiffstats
path: root/src/Compiler/ExecuteC.java
blob: 128f5414342eba58083ebd283aedddbb2d07281f (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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.Path;
import java.nio.file.Files;

public class ExecuteC {

    Path buildToDir;
    String filename;

    public void compileAndExecuteC(List<String> code, String outPathStr, boolean executeAfter, boolean leaveCFile) {

        Path outPath = Path.of(outPathStr);
        int numElements = outPath.getNameCount();
        filename = outPath.getName(numElements - 1).toString();
        if (numElements == 1) {
            buildToDir = Paths.get("build");
        } else {
            buildToDir = outPath.subpath(0, numElements - 1);
        }
        
        try {
            Files.createDirectories(buildToDir);
        } catch (IOException e) {
            e.printStackTrace();
        }

        File cProgram = writeProgram(code);
        if (!compileC()) {
            if (executeAfter) {
                String output = runProgram();
                System.out.println(output);
            }
        } else {
            Language.displayError(0, "Runtime Error");
        }
        if (!leaveCFile) {
            cProgram.delete();
        }
    }

    public void compileAndExecuteC(List<String> code) {
        compileAndExecuteC(code, "main", true, true);
    }

    public File writeProgram(List<String> codeLines){
        BufferedWriter output = null;
        File file = null;
        try {
            file = Paths.get(buildToDir.toString(), 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();
        }
        return file;
    }

    public Boolean compileC(){
        try{
            Process p;
            if (System.getProperty("os.name").equals("Linux")) {
                p = Runtime.getRuntime().exec(String.format(
                    "gcc %s/%s.c -o %s/%s", 
                    buildToDir.toString(), filename, buildToDir.toString(), filename
                ));
            } else {
                p = Runtime.getRuntime().exec(String.format(
                    "cmd /C gcc %s -o %s", 
                    Paths.get(buildToDir.toString(), String.format("%s.c", filename)).toString(), 
                    Paths.get(buildToDir.toString(), String.format("%s.exe", filename)).toString()
                ));
            }
            BufferedReader stdError = new BufferedReader(new 
                InputStreamReader(p.getErrorStream()));
            boolean error=false;
             while ((stdError.readLine()) != null) {
                 error=true;
             }
             return error;
        } catch (IOException e){
            e.printStackTrace();
        }
        return false;
    }

    public String runProgram(){
        
        try{
            ProcessBuilder probuilder;
            if (System.getProperty("os.name").equals("Linux")) {
                String[] command = {"sh", "-c", Paths.get(buildToDir.toString(), filename).toString()};
                probuilder = new ProcessBuilder(command);
            } else {
                String[] command = {"cmd", "/C", Paths.get(buildToDir.toString(), 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+"\n";
            }
            return output;
        } catch (IOException e){
            e.printStackTrace();
        }
        System.out.println();
        return null;
    } 
}