-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverterAssem.java
65 lines (61 loc) · 1.81 KB
/
ConverterAssem.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
import java.util.Arrays;
/**
* Authors: Diane Zhang, Kayla Dixon
* Project: CS321 Project 2 Assembler
* Instructor: Prof. Al Madi
* Increment: 3
* Issue: 10
*/
public class ConverterAssem{
public static void main(String[] args) {
String postfixExpression = "AX BY C * + D4 E - /";
MyStack<String> stack = new MyStack<String>();
String[] tokens = postfixExpression.split(" ");
String[] operators = {"*", "+", "-", "/"};
int i = 0;
int tempNum = 1;
while (i < tokens.length) {
String t = tokens[i];
if (!Arrays.asList(operators).contains(t)) {
stack.push(t);
} else {
String right = stack.pop();
String left = stack.pop();
//stack.push(evaluate(left, t, right, tempNum));
tempNum++;
}
i++;
//System.out.println("Iteration "+ i);
//System.out.println(stack);
}
if (stack.isEmpty()) {
System.out.println("Error");
}
}
public static String evaluate(String l, String op, String r, int index){
String str = "LD " + l;
System.out.println(str);
switch (op) {
case "*":
str = "ML ";
break;
case "+":
str = "AD ";
break;
case "-":
str = "SB ";
break;
case "/":
str = "DV ";
break;
default:
System.out.println("Error");
System.exit(0);
}
str += r;
System.out.println(str);
str= "ST " + "TMP" + Integer.toString(index);
System.out.println(str);
return("TMP" + Integer.toString(index));
}
}