-
Notifications
You must be signed in to change notification settings - Fork 9
/
DeleteLLVMComments.java
201 lines (187 loc) · 8.37 KB
/
DeleteLLVMComments.java
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class DeleteLLVMComments {
public static boolean isSpecifiedChar(char theChar) {
if (theChar == ')' || theChar == ']' || theChar == '}' || theChar == ' ') {
return true;
}
return false;
}
public static boolean isCharC(char theChar) {
if (theChar == 'c')
return true;
return false;
}
public static String getCStringContent(String theLine, int theLineIndex) {
String cStringContent = "";
for(int i = theLineIndex; i < theLine.length(); ++i) {
cStringContent += theLine.charAt(i);
if(theLineIndex == '\"') {
cStringContent += theLine.charAt(i);
break;
}
}
return cStringContent;
}
public static boolean isHexCharacter(char theChar) {
if ( theChar == '0'
|| theChar == '1'
|| theChar == '2'
|| theChar == '3'
|| theChar == '4'
|| theChar == '5'
|| theChar == '6'
|| theChar == '7'
|| theChar == '8'
|| theChar == '9'
|| theChar == 'a'
|| theChar == 'b'
|| theChar == 'c'
|| theChar == 'd'
|| theChar == 'e'
|| theChar == 'f'
|| theChar == 'A'
|| theChar == 'B'
|| theChar == 'C'
|| theChar == 'D'
|| theChar == 'E'
|| theChar == 'F') {
return true;
}
return false;
}
/**
* @author LiyiLi
* This Java program takes an LLVM program as input and eliminates
* all the comments from it. The resulting uncommented LLVM program
* is saved to a temporary string.
* This step is needed due to a limitation of the current K parser,
* which cannot parse LLVM comments, and it
* will be eliminated as soon as the K parser is fixed.
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
//if(args.length == 0){
// System.err.println("Haven't specified the input program.");
//}
String fileName = "printNum.ll";
File llvmFile = new File(fileName);
String newLine = "";
if(llvmFile.isFile()){//if input file exists.
try {
Scanner scanner = new Scanner(llvmFile);
/**
* Declare a variable to indicate whether or
* not the current scanning character is inside
* a double quote block.
* This algorithm works because users cannot write
* \" inside a string (starting by " and ending by ") in LLVM.
* That is why we can only use a variable (isInQuoteBlock) to keep track of
* whether or not the current character is located inside
* a string block of a LLVM program.
*/
boolean isInQuoteBlock = false;
while(scanner.hasNextLine()){
String currentLine = scanner.nextLine();
for(int i = 0; i < currentLine.length(); ++i){
if(isSpecifiedChar(currentLine.charAt(i))){ //reach to a specified character, ),],}
newLine += currentLine.charAt(i);
if(i+1 < currentLine.length()){ //checker index
if(isCharC(currentLine.charAt(i+1))){ //
i++;
newLine += currentLine.charAt(i);
for(int j = i + 1; j < currentLine.length(); ++j){
if(currentLine.charAt(j) == ' '){
i++;
newLine += currentLine.charAt(j);
} else {
break;
}
}
if(i+1 < currentLine.length()){
if(currentLine.charAt(i+1) == '\"'){
i++;
newLine += currentLine.charAt(i);
String content = getCStringContent(currentLine, i + 1);
for(int j = 0; j < content.length(); ++j){
newLine += content.charAt(j);
i++;
if(content.charAt(j) == '\\'){
if(j+1 < content.length()){
if(isHexCharacter(content.charAt(j+1))){
newLine += 'x';
}
}
}
}
} else {
continue;
}
} else {
break;
}
} else {
continue;
}
} else { //move the index from the specified character to the next, reach to the end of the line
break;
}
} else { //reach to a non-specified-character, then, just add it.
char currentChar = currentLine.charAt(i);
if(currentChar == '"'){
isInQuoteBlock = ! isInQuoteBlock;
} else if(!isInQuoteBlock
&& currentChar == ';'){
break;
}
newLine += currentChar;
}
}
newLine += "\n";
} // end of while loop
scanner.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {//if input file doesn't exist.
System.err.println("Input test file " +
"is not a file or does not exist.");
}
File output = new File(fileName+".temp");
PrintWriter printWriter = null;
try {
printWriter = new PrintWriter(output);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
printWriter.print(newLine);
printWriter.close();
Process p = null;
try {
p = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", "\"kast "+fileName+".temp\""});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
try {
while ((line = reader.readLine())!= null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}