-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pwacodec.java
313 lines (270 loc) · 8.95 KB
/
Pwacodec.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package eduproc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Pwacodec extends AbstractCodec {
int indents;
int lastChange;
public void init() {
regexMap.put("call function", "^\\s*[^.]*\\.?(\\w+)\\((.+)\\);$");
regexMap.put("for each","for \\((\\w+) (\\w+) ?: ?(\\.+)\\)\\{$");
regexMap.put("for", "^\\s*for \\(\\w+ (\\w+) ?= ?(\\d+); ?\\w+ ?< ?(\\d+); ?\\w\\+\\+\\) ?\\{");
regexMap.put("if", "^\\s*if\\s*?\\((.+)\\)\\{$");
regexMap.put("while", "^\\s*while\\s*\\((.+)\\)\\{$");
regexMap.put("else if", "^\\s*else if\\s*?\\((.+)\\)\\{$");
regexMap.put("define function", "^\\s*(\\w+) (\\w+) ?\\(([\\w\\s]*)\\) ?\\{");
regexMap.put("set to function", "^\\s*(\\w+) ?= ? (.+) ?\\((.*)\\);");
regexMap.put("create object", "^\\s*(\\w+) (\\w+) ?= ?new \\w+ ?\\((.*)\\);");
regexMap.put("declare variable", "^\\s*(\\w+) (\\w+);$");
regexMap.put("display value", "^\\s*System\\.out\\.println ?\\((.+)\\);");
regexMap.put("set variable", "^\\s*(.+) ?= ?(?!\\s*new)(.+);");
regexMap.put("end", "^\\s*\\}$");
regexMap.put("else", "^\\s*else ?\\{$");
regexMap.put("comment", "^\\s*//.+$");
regexMap.put("main method", "^\\s*public static void main \\(.+\\)\\{$");
for (String s : regexMap.keySet()) {
System.out.println("added pattern!");
patternMap.put(s, Pattern.compile(regexMap.get(s)));
}
indents = 0;
}
@Override
public ArrayList <String> convert(String inString) {
HashMap<String, Matcher> matcherMap = new HashMap<String, Matcher>();
for (String s : patternMap.keySet()) {
matcherMap.put(s, patternMap.get(s).matcher(inString.toLowerCase()));
System.out.println("added matcher!");
System.out.println(matcherMap.get(s));
}
String match = null;
Matcher mData = null;
// for, if, while, else if, define function, set to function, create
// object, declare variable, display value,
// print string, set variable, end, else
for (String s : matcherMap.keySet()) {
System.out.println(s);
if (matcherMap.get(s).matches()) {
System.out.println(s);
System.out.println(matcherMap.get(s));
match = s;
mData = matcherMap.get(s);
break;
}
}
if (match == null || mData == null) {
return bottle (FAIL, "NOTE: THIS DOESN'T MAKE SENSE\n" + "NOTE:" + inString, inString, match);
}
if (match.equals("end"))
indents--;
return bottle (SUCCESS, indent(indents) + matchSwitch(mData, match, inString), inString, match);
}
String matchSwitch(Matcher mData, String match, String inString) {
switch (match) {
case "comment":
return comment (mData.group(1));
case "for":
indents++;
return forLoop(mData.group(1), mData.group(2), mData.group(3));
case "if":
indents++;
return ifStatement(convertCondition(mData.group(1)));
case "while":
indents++;
return whileLoop(convertCondition(mData.group(1)));
case "else if":
indents++;
return elseIfStatement(convertCondition(mData.group(1)));
case "define function":
indents++;
return functionDeclare(mData.group(1), mData.group(2), mData.group(3));
case "set to function":
return set2Fcn(mData.group(1), mData.group(2), mData.group(3));
case "create object":
return objectCreation(mData.group(1), mData.group(2), new String[] { mData.group(3) });
case "declare variable":
return createVar(mData.group(1), mData.group(2));
case "main method":
indents++;
return "main method";
case "display value":
return printStatement(mData.group(1));
case "print string":
return printStatement(mData.group(1));
case "set variable":
return setVar(mData.group(1), mData.group(2));
case "end":
return end();
case "else":
return elseStatement();
default:
break;
}
// TODO Auto-generated method stub
return null;
}
@Override
public String ifStatement(String condition) {
// TODO Auto-generated method stub
return "if " + condition + ", then";
}
@Override
public String elseIfStatement(String condition) {
// TODO Auto-generated method stub
return "otherwise, if " + condition + ", then";
}
@Override
public String whileLoop(String condition) {
// TODO Auto-generated method stub
return "as long as " + condition + ", then";
}
@Override
public String objectCreation(String type, String name, String[] parameters) {
// TODO Auto-generated method stub
return "make a new " + type + " called " + name + " with parameters " + parameters[0];
}
@Override
public String objectCreation(String type, String name) {
// TODO Auto-generated method stub
return "make a new " + type + " called " + name;
}
@Override
public String forLoop(String name, String min, String max) {
// TODO Auto-generated method stub
return "for every integer " + name + " between " + min + " and " + max + ",";
}
@Override
public String printStatement(String toPrint) {
// TODO Auto-generated method stub
return "display " + toPrint;
}
@Override
public String functionDeclare(String returnType, String functionName, String... params) {
// TODO Auto-generated method stub
return "function called " + functionName + " that returns " + returnType + " with parameters"
+ String.join(",", params) + ",";
}
@Override
public String functionDeclare(String returnType, String functionName) {
// TODO Auto-generated method stub
return "function called " + functionName + " that returns " + returnType + ",";
}
@Override
public String createVar(String type, String name) {
// TODO Auto-generated method stub
return "call a " + type + " " + name;
}
@Override
public String setVar(String var, String value) {
// TODO Auto-generated method stub
return "set " + var + " to " + value;
}
@Override
public String set2Fcn(String var, String fcn) {
// TODO Auto-generated method stub
return "calculate the value of " + var + " with function " + fcn;
}
@Override
public String set2Fcn(String var, String fcn, String... params) {
// TODO Auto-generated method stub
return "calculate the value of " + var + " with function " + fcn + " with parameters "
+ String.join(",", params);
}
@Override
public String convertCondition(String condition) {
// TODO Auto-generated method stub
if (condition.contains("<=")) {
return condition.replace("<=", " is less than or equal to ");
} else if (condition.contains("<")) {
return condition.replace("<", " is less than ");
} else if (condition.contains("==")) {
return condition.replace("==", " is equal to ");
} else if (condition.contains(">")) {
return condition.replace(">", " is greater than ");
} else if (condition.contains(">=")) {
return condition.replace(">=", " is greater than or equal to");
}
return null;
}
@Override
public String end() {
// TODO Auto-generated method stub
return "end";
}
@Override
public String elseStatement() {
// TODO Auto-generated method stub
return "otherwise,";
}
@Override
public List<String> elements(String inString) {
// TODO Auto-generated method stub
HashMap<String, Matcher> matcherMap = new HashMap<String, Matcher>();
for (String s : patternMap.keySet()) {
matcherMap.put(s, patternMap.get(s).matcher(inString));
}
String match = null;
Matcher mData = null;
// for, if, while, else if, define function, set to function, create
// object, declare variable, display value,
// print string, set variable, end, else
for (String s : matcherMap.keySet()) {
System.out.println(s);
if (matcherMap.get(s).matches()) {
System.out.println(s);
System.out.println(matcherMap.get(s));
match = s;
mData = matcherMap.get(s);
break;
}
}
if (match == null) {
return listPacker("syntax error", inString);
}
switch (match) {
case "for":
return listPacker(match, mData.group(1), mData.group(2), mData.group(3));
case "print":
return listPacker(match, mData.group(1));
case "main function":
return listPacker(match);
case "if":
return listPacker(match, mData.group(1));
case "say":
return listPacker(match, mData.group(1));
case "else if":
return listPacker(match, mData.group(1));
case "while":
return listPacker(match, mData.group(1));
case "var declare":
return listPacker(match, mData.group(1), mData.group(2));
case "object creation":
return listPacker(match, mData.group(1), mData.group(2), mData.group(3));
case "declare function":
return listPacker(match, mData.group(2), mData.group(1), mData.group(3));
case "declare function np":
return listPacker(match, mData.group(2), mData.group(1));
case "object creation np":
return listPacker(match, mData.group(1), mData.group(2));
case "set to fcn np":
return listPacker(match, mData.group(1), mData.group(2));
case "set":
return listPacker(match, mData.group(1), mData.group(2));
case "else":
return listPacker(match);
case "end":
return listPacker(match);
default:
return null;
}
}
public Pwacodec() {
super();
}
@Override
public String comment(String in) {
// TODO Auto-generated method stub
return "NOTE:" +in;
}
}