-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
243 lines (187 loc) · 6.07 KB
/
Main.java
File metadata and controls
243 lines (187 loc) · 6.07 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import java.io.*;
import java.net.URL;
public class Main {
public static void main(String[] args) throws IOException {
//BufferedReader firstIN = new BufferedReader(new InputStreamReader(System.in));
String inFileName = args[0];//firstIN.readLine();//input file name from Standard IO --> written in PsuedoC
//URL url = Main.class.getResource(inFileName);
File PsuedoC = new File(inFileName);//new File(url.getPath());
System.out.println("Filename: "+inFileName);
//System.out.println("Filepath: "+url.getPath());
BufferedReader in = new BufferedReader(new FileReader(PsuedoC));
String[] pop = inFileName.split("//");
String outPutFileName = pop[pop.length-1].replace(".slc", ".c");
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outPutFileName)), true);
//PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), true);
String currLine;
out.print("#include <stdio.h>\r\n" +
"#include <string.h>\r\n" +
"\r\n" +
"int main () { \n");
int counter = 1;
while(in.ready()) {
currLine = in.readLine();
String ssss = converter(currLine);
out.println(ssss);
System.out.println("Line "+counter+" interpreted");
System.out.println("Line "+counter+": "+ currLine);
System.out.println("Line "+counter+": "+ ssss);
counter++;
}
out.println("\n }");
in.close();
out.close();
}//end of main method
static boolean isInt(String str)
{
try
{
// checking valid integer using parseInt() method
Integer.parseInt(str);
return true;
}
catch (NumberFormatException e)
{
return false;
}
}// end of isInt()
static boolean isFloat(String str)
{
try
{
// checking valid float using parseInt() method
Float.parseFloat(str);
return true;
}
catch (NumberFormatException e)
{
return false;
}
}// end of isFloat()
static String converter(String in) {
String out = "";
if(typeDetector(in) == 0) {//VAR
String[] varLine = in.split(" ");
String curVarType = ((Object)varLine[2]).getClass().getSimpleName();
String curVar = varLine[2];
if (curVar.charAt(0) != '\"' && curVar.charAt(curVar.length()-1) != '\"')
curVarType = "Integer";
if(curVarType.equals("Integer")) {
out = "int "+ varLine[0] + " = " + Integer.parseInt(varLine[2])+";";
}
else if(curVarType.equals("Double")) {
out = "float "+ varLine[0] + " = " + varLine[2]+";";
}
else if(in.contains(" \"") || in.contains("\" ") || in.contains("\";")) {
out = "char "+ varLine[0] + "[] = \"" + in.substring(in.indexOf("\""), in.length()-1).replace("\"", "").replace(";", "") +"\";";
}
else if(curVarType.equals("Char")) {
out = "char "+ varLine[0] + " = " + varLine[2]+";";
}
else if(curVarType.equals("Boolean")) {
out = "boolean "+ varLine[0] + " = " + varLine[2]+";";
}
}
else if(typeDetector(in) == 1) {//LOOP
String[] varLine = in.split(" ");
String start = varLine[1];
//int end = Integer.parseInt(varLine[3]);
String s = varLine[3];
if (s.contains("{"))
{
s.replace("{", "");
}
String end = s;
out += "for(int i="+start+";i<"+end+"; i++) {\n";
}
else if(typeDetector(in) == 2) {//IF-ELSE
String[] varLine = in.split(" ");
out = "";
}
else if(typeDetector(in) == 3) {//FUN
out = "";
}
else if(typeDetector(in) == 4) {//PRINT
String varLine = in.replace("output", "").replace(")", "").replace("(", "").replace(";", "").trim();
System.out.println("debugging varline: " + varLine);
//System.out.println(varLine);
// if (GlobalVar.Record.contains(varLine+" ="))
// {
// System.out.println("Probably gonna be a var printing");
// if(GlobalVar.Record.substring(GlobalVar.Record.indexOf(varLine+" =")-1, GlobalVar.Record.indexOf(";", GlobalVar.Record.indexOf(varLine+" =")-1) ).contains("\"")) {//string var
// //plz w
// System.out.println("This is a String Variable to be outprinted");
// out = "printf(\"%s\", " + varLine + ");";
// }
//
//// else if(in.contains("0") || in.contains("1") || in.contains("2") ||
//// in.contains("3") || in.contains("4") || in.contains("5") ||
//// in.contains("6") || in.contains("7") || in.contains("8") ||
//// in.contains("9")
//// ){//double var
//// double var = Double.parseDouble(varLine);
//// System.out.println("This is a Num Variable to be outprinted");
//// out = "printf(\"%d\", " + var + ");";
//// }
//// else {
////
//// }
// }
//
// //else
if(!in.contains("="))//else
{
System.out.println("This is a String LITERAL to be outprinted");
out = "printf(" + varLine + ");";
}
/*
* int a = 10;
char *intStr = itoa(a);
string str = string(intStr);
*/
else {
out = "int hooplass = "+varLine+"\n char *intStr = itoa(hooplass);\nstring str = string(intStr)\nprintf(str);\n";
}
}
else if(typeDetector(in) == 5) {
out = in;
}
GlobalVar.addToString(out);
return out;
}//end of converter
static int typeDetector(String line) {
String[] myLine = line.split(" ");
if(line.contains(" is ") ) {
return 0;//VARIABLE
}
else if(myLine[0].equals("from")) {
return 1;//LOOP
}
else if(myLine[0].equals("if")) {
return 2;//IF-ELSE
}
else if(myLine[0].equals("function")) {
return 3;//FUNCTION
}
else if(myLine[0].contains("output")) {
return 4;//Print
}
else if(myLine[0].contains("{") || myLine[0].contains("}")) {
return 5;//Brackets
}
return -1;
}
}//end of class
class GlobalVar{
static String Record = "";
static public void addToString(String s) {
Record += s+"\n";
}
static public boolean substringExists(String ss) {
if(Record.contains(ss)) {
return true;
}
return false;
}
}
//lolwut