-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.java
439 lines (376 loc) · 15.2 KB
/
Parser.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package sample;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class Parser {
String ErrorMsg = new String();
public boolean err = false;
public Assembler assem = new Assembler();
public String[] parameters = new String[3];
private HashMap<String, Integer> operations = new HashMap<>();
static int ProgramCounter = 0;
//Each Label and its index.
public static HashMap<String, Integer> Labels = new HashMap<>();
public Parser() {
//Arguments 3 and Registers.
//AllRegisters3.add();
/**********************************************With 2 Parameters************************************************/
operations.put("lw", 2);//Register offset(R)
operations.put("sw", 2);//Register offset(R)
operations.put("lui", 2);//Register Constant
/**********************************************With 3 Parameters************************************************/
operations.put("add", 3);//Register Register Register
operations.put("addi", 3);//Register Register Constant
operations.put("and", 3);//Register Register Register
operations.put("andi", 3);//Register Register Constant
operations.put("beq", 3);//Register Register offset
operations.put("bne", 3);//Register Register offset
operations.put("sub", 3);//Register Register Register
operations.put("or", 3);//Register Register Register
operations.put("ori", 3);//Register Register Constant
operations.put("sll", 3);//Register Register Shiftamount
operations.put("slt", 3);//Register Register Register
operations.put("slti", 3);//Register Register Constant
/**********************************************With 1 Parameters************************************************/
operations.put("jr", 1);//Address
operations.put("j", 1);//Address
/**********************************************With 0 Parameters************************************************/
operations.put("label", 0);
}
//add $t0 $s0 $0
//add $t0 $s0 $0
public void parse(String line) {
String[] tst = line.split(" ");
String key = tst[0];
switch (key) {
/**********************************************With 3 Parameters************************************************/
case "add":
String[] arr = {tst[1], tst[2], tst[3]};
assem.add(arr);
break;
case "addi":
String[] arr2 = {tst[1], tst[2], tst[3]};
assem.addi(arr2);
break;
case "sub":
String[] arr3 = {tst[1], tst[2], tst[3]};
assem.sub(arr3);
break;
case "bne":
String[] arr9 = {tst[1], tst[2], tst[3]};
assem.bne(arr9);
break;
case "beq":
String[] arr10 = {tst[1], tst[2], tst[3]};
assem.beq(arr10);
break;
case "and":
String[] arr0 = {tst[1], tst[2], tst[3]};
assem.and(arr0);
break;
case "andi":
String[] arr12 = {tst[1], tst[2], tst[3]};
assem.andi(arr12);
break;
case "or":
String[] arr11 = {tst[1], tst[2], tst[3]};
assem.or(arr11);
break;
case "ori":
String[] arr13 = {tst[1], tst[2], tst[3]};
assem.ori(arr13);
break;
case "slti":
String[] arr6 = {tst[1], tst[2], tst[3]};
assem.slti(arr6);
break;
case "slt":
String[] arr7 = {tst[1], tst[2], tst[3]};
assem.slt(arr7);
break;
case "sll":
String[] arr8 = {tst[1], tst[2], tst[3]};
assem.sll(arr8);
break;
/**********************************************With 2 Parameters************************************************/
case "sw":
String[] arr4 = {tst[1], tst[2]};
assem.sw(arr4);
break;
case "lui":
String[] arr5 = {tst[1], tst[2]};
assem.lui(arr5);
break;
case "lw":
String[] arr15 = {tst[1], tst[2]};
assem.lw(arr15);
break;
/**********************************************With 1 Parameters************************************************/
case "jr":
String[] arr14 = {tst[1]};
assem.jr(arr14);
break;
case "j":
String[] arr17 = {tst[1]};
assem.j(arr17);
break;
/**********************************************With 0 Parameters************************************************/
case "label":
// String[] arr16 = {tst[1]};
// assem.j(arr16);
break;
}
}
public void Validate(ArrayList<String> tst) {
String Line;
for (int j = 0; j < tst.size(); j++) {
Line = tst.get(j); //FirstLine add $t0 $s1 $0
if (Line.contains(":")) {
Line = Line.substring(0, Line.length() - 1);//label
Labels.put(Line, j);
}
}
for (; ProgramCounter < tst.size(); ProgramCounter++) {
Line = tst.get(ProgramCounter); //FirstLine add $t0 $s1 $0
//ErrorMsg"From Validate Line: " + Line);
Memory.memorydata[ProgramCounter] = Line;
String[] strarr = Line.split(" ");
String x = strarr[0];//add
//Label case
if (Line.contains(":")) {
continue;
}
if (operations.containsKey(x))//Check Instruction Exsitance.
{
int nom_of_arguments = operations.get(x);
int len = strarr.length - 1;
// ErrorMsg"From Validate len " + len + " From Validate len " + nom_of_arguments);
if (nom_of_arguments != len)//Check Number of Arguments.
{
ErrorMsg = "\nError in line (" + (ProgramCounter + 1) + ") : " + x + " Takes " + operations.get(x) + " Arguments.\n";
err = true;
} else// if(nom_of_arguments == len) // Check Arguments Validation.
{
if (nom_of_arguments == 3) {
check_3_ArgumentsValidation(nom_of_arguments, x, strarr, Line);
} else if (nom_of_arguments == 2) {
check_2_ArgumentsValidation(nom_of_arguments, x, strarr, Line);
} else if (nom_of_arguments == 1) {
//strarr[1].equals("j") || strarr[1].equals("jr")) &&
if ((x.equals("j") && (Labels.containsKey(strarr[1])))) {
parse(Line);
} else if (x.equals("jr") && CheckRegisterValidation(strarr[1])) {
parse(Line);
} else {
ErrorMsg = "\nError in line (" + (ProgramCounter + 1) + ") Check Instruction name.\n";
err = true;
}
}
}
}
//beq bne jr j
else {
ErrorMsg = "\nError in line (" + (ProgramCounter + 1) + ") Check Instruction name.\n";
err = true;
}
}
}
//Called to check register validation.
public boolean CheckRegisterValidation(String s) //$s0
{
String x = s;
boolean charValid = false, strvalid = false;
if ((s.equals("$0")) || (s.equals("$at"))) {
strvalid = true;
return true;
}
//Zero Register || $ char nom
if (((s.charAt(0) == '$'))) //&& !Character.isLetter(s.charAt(1)
{
s = s.substring(1);
//ErrorMsg"From chaeckregistervalidation s after $ " + s);
char c = s.charAt(0);
if (s.charAt(0) == 'v' || s.charAt(0) == 'a' || s.charAt(0) == 't' || s.charAt(0) == 's' || s.charAt(0) == 'k') {
//ErrorMsg"here");
s = s.substring(1);
if (Valid0_1(s, c) || Valid0_3(s, c) || Valid0_7(s, c) || Valid0_9(s, c)) {
return true;
}
}
}
return false;
}
//$v0–$v1 , $k0–$k1
public boolean Valid0_1(String c, char k) {
int nom = Integer.parseInt(c);
if (nom >= 0 && nom <= 1 && (k == 'v' || k == 'k')) {
return true;
}
return false;
}
//$s0–$s7
public boolean Valid0_7(String c, char k) {
int nom = Integer.parseInt(c);
if (nom >= 0 && nom <= 7 && (k == 's')) {
return true;
}
return false;
}
//$t0–$t7 , $t8–$t9
public boolean Valid0_9(String c, char k) {
int nom = Integer.parseInt(c);
if (nom >= 0 && nom <= 9 && (k == 't')) {
return true;
}
return false;
}
//$v0–$v1 , $k0–$k1
public boolean Valid0_3(String c, char k) {
int nom = Integer.parseInt(c);
if (nom >= 0 && nom <= 1 && (k == 'a')) {
return true;
}
return false;
}
public boolean isNumber(String t) {
try {
int constant = Integer.parseInt(t);
return true;
} catch (NumberFormatException o) {
return false;
}
}
public boolean isOffset(String t) {
if (isNumber(t)) {
int x = Integer.parseInt(t);
if (x % 4 == 0 || x == 0) {
return true;
}
}
return false;
}
public boolean isOffsetsw_lw(String t) {
ErrorMsg = "t : " + t + " " + t.length();
//err = true;
String x = "";
x += t.charAt(0);
//$s0 8($s2)
if (t.length() == 5 && isNumber(x) && t.charAt(1) == '(' && t.charAt(2) == '$' && t.charAt(4) == ')') {
String s = "";
s += t.charAt(2);//$s0
s += t.charAt(3);
int nom = Integer.parseInt(x);
if (CheckRegisterValidation(s) && (nom % 4 == 0)) {
return true;
}
} else if (t.length() == 6 && isNumber(x) && t.charAt(1) == '(' && t.charAt(2) == '$' && t.charAt(5) == ')') //8($s0)
{
String s = "";
s += t.charAt(2);//$s0
s += t.charAt(3);
s += t.charAt(4);
int nom = Integer.parseInt(x);
if (CheckRegisterValidation(s) && (nom % 4 == 0)) {
return true;
}
}
return false;
}
public void check_3_ArgumentsValidation(int nom_of_arguments, String x, String[] strarr, String Line) {
int counter = 0;
//bne $ $ l (Map)
if (x.equals("bne") || x.equals("beq")) {
for (int i = 0; i < 2; i++) {
if (CheckRegisterValidation(strarr[i + 1])) // 1 2
{
counter++;
}
}
String str = strarr[3];
if (Labels.containsKey(str)) {
parse(Line);
}
}
if ((x.equals("or") || x.equals("and") || x.equals("add") || x.equals("sub") || x.equals("slt"))) {
for (int j = 0; j < nom_of_arguments; j++)//0 1 2
{
if (CheckRegisterValidation(strarr[j + 1])) // 1 2 3
{
counter++;
}
}
// ErrorMsg"From Validate Counter : " + counter);
if (counter != nom_of_arguments) {
ErrorMsg = "\nError in line (" + ProgramCounter + ") Check Instruction arguments.\n";
err = true;
} else
parse(Line);
}
//3 Arguments---->Register Register Contant
else if ((x.equals("andi") || x.equals("addi") || x.equals("slti"))) {
for (int j = 0; j < nom_of_arguments - 1; j++)//0 1 2
{
if (CheckRegisterValidation(strarr[j + 1])) // 1 2 3
{
counter++;
}
}
// ErrorMsg"From Validate Counter : " + counter);
if (counter == nom_of_arguments - 1 && isNumber(strarr[3])) {
parse(Line);
} else {
ErrorMsg = "\nError in line (" + ProgramCounter + ") Check Instruction arguments.\n";
err = true;
}
} else if (x.equals("ori")) {
for (int i = 0; i < nom_of_arguments - 2; i++) {
if (CheckRegisterValidation(strarr[i + 1])) // 1 2 3
{
counter++;
}
}
if (counter == nom_of_arguments - 2 && (isHexNumber(strarr[3]) || isNumber(strarr[3])))//0x245F
{
parse(Line);
} else {
ErrorMsg = "\nError in line (" + ProgramCounter + ") Check Instruction arguments.\n";
err = true;
}
} else if (x.equals("sll")) {
if (CheckRegisterValidation(strarr[1]) && CheckRegisterValidation(strarr[2]) && isNumber(strarr[3])) {
parse(Line);
} else {
ErrorMsg = "\nError in line (" + (ProgramCounter + 1) + ") Check Instruction arguments.\n";
err = true;
}
}
}
public void check_2_ArgumentsValidation(int nom_of_arguments, String x, String[] strarr, String Line) {
int counter = 0;
if ((x.equals("lui"))) {
if (CheckRegisterValidation(strarr[1]) && isHexNumber(strarr[2])) {
parse(Line);
} else {
ErrorMsg = "\nError in line (" + (ProgramCounter + 1) + ") Check Instruction arguments.\n";
err = true;
}
}
if ((x.equals("sw") || x.equals("lw"))) {
if (CheckRegisterValidation(strarr[1]) && isOffsetsw_lw(strarr[2])) {
parse(Line);
} else {
ErrorMsg = "\nError in line (" + (ProgramCounter + 1) + ") Check Instruction arguments.\n";
err = true;
}
}
}
public boolean isHexNumber(String x) {
x = x.substring(2);
try {
Long.parseLong(x, 16);
return true;
} catch (NumberFormatException ex) {
return false;
}
}
}