-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMacroProcessor.java
More file actions
195 lines (171 loc) · 6.58 KB
/
MacroProcessor.java
File metadata and controls
195 lines (171 loc) · 6.58 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
import java.io.*;
import java.util.*;
// TODO: Using space between comma causes program to be whitespace(indentention) sensitive, fix it
// Need to create temp file properly right not it holds "macro_name label, arguments" but it should
// have "label macro_name, arguments"
class MacroNameTable {
public String name;
public int index;
public MacroNameTable(int index, String name) {
this.index = index;
this.name = name;
}
public int getIndex() {
return index;
}
public String getName() {
return name;
}
}
public class MacroProcessor {
private void createOutputFile(List<String> mdt) throws Exception {
PrintWriter printWriter = new PrintWriter("output.asm");
printWriter.close();
BufferedReader reader = new BufferedReader(new FileReader("macro.asm"));
BufferedWriter writer = new BufferedWriter(new FileWriter("temp.asm"));
boolean isToBeDeleted = false;
String currentLine = "";
while ((currentLine = reader.readLine()) != null) {
for (String toDelete : mdt) {
if (currentLine.trim().equals(toDelete)) {
isToBeDeleted = true;
}
}
if (isToBeDeleted) {
isToBeDeleted = false;
continue;
} else {
writer.write(currentLine + "\n");
}
}
reader.close();
writer.close();
}
private void pass2(List<String> mdt, List<MacroNameTable> mnt, HashMap<String, List<String>> ala)
throws Exception {
// TODO: Find a way where I won't require temporary file (temp.asm)
File file = new File("temp.asm");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("output.asm", true));
;
List<String> completeFile = new ArrayList<String>();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
if (line.length() > 0) {
completeFile.add(line + "\n");
}
}
boolean macroFound = false;
for (String currentLine : completeFile) {
// Running loop through mnt which contains definition and body of macros
for (int i = 0; i < mnt.size(); i++) {
// checks if macro name is present in a line
if (currentLine.contains(mnt.get(i).getName())) {
// If we found macro name in current line we set macroFound to true so in else block we
// canknow if macro is found or not, if it is we do not print extra line else we print it
macroFound = true;
// We have add 1 here as we don't want to print macro definition, we only want to print
// statements inside it
int index = mnt.get(i).getIndex() + 1;
String macro_name = mnt.get(i).getName();
List<String> oldTokens = ala.get(macro_name);
List<String> newTokens = new ArrayList<String>();
newTokens.addAll(Arrays.asList(currentLine.split(" |,")));
// If first char is "&" it means there is argument before macro name so we remove the
// macro name (i.e 1st index) else we simply remove first token i.e macro name
if (mdt.get(i).startsWith("&")) {
newTokens.remove(1);
} else {
newTokens.remove(0);
}
// index - 1 = defintion of macro. We need to check if there is argument(label) before
// macro name
if (mdt.get(index - 1).startsWith("&")) {
bufferedWriter.write(newTokens.get(0) + " ");
}
// Adding lines into output file until we don't find MEND
while (!mdt.get(index).equals("MEND")) {
String mdtLine = mdt.get(index);
for (int tokenIndex = 0; tokenIndex < newTokens.size(); tokenIndex++) {
mdtLine = mdtLine.replace(oldTokens.get(tokenIndex), newTokens.get(tokenIndex));
}
if (mdtLine.endsWith("\n")) {
bufferedWriter.write(mdtLine);
} else {
bufferedWriter.write(mdtLine + "\n");
}
index++;
}
}
}
if (!macroFound) {
bufferedWriter.write(currentLine);
}
macroFound = false;
}
bufferedReader.close();
bufferedWriter.close();
}
private List<String> createMDT() throws Exception {
File file = new File("macro.asm");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
List<String> mdt = new ArrayList<String>();
String line = "";
boolean macroFound = false;
// Currently only MACRO and MEND is accepted i.e it is case sensitive, to make it case
// insensitive just add .toUpperCase() in each if condition
// Created macro definition table
// Macro deifintion contains "MACRO" as a string as well
while ((line = bufferedReader.readLine()) != null) {
if (macroFound == true) {
mdt.add(line.trim());
if (line.trim().equals("MEND")) {
macroFound = false;
}
}
if (line.trim().equals("MACRO")) {
macroFound = true;
mdt.add(line.trim());
}
}
bufferedReader.close();
return mdt;
}
public static void main(String[] args) throws Exception {
MacroProcessor macroProcessor = new MacroProcessor();
List<String> mdt = macroProcessor.createMDT();
List<MacroNameTable> mnt = new ArrayList<MacroNameTable>();
HashMap<String, List<String>> ala = new HashMap<String, List<String>>();
for (int i = 0; i < mdt.size(); i++) {
if (mdt.get(i).equals("MACRO")) {
// incrementing i here so we don't have to increment i when trying to access index of m
i++;
String line = mdt.get(i);
String[] tokens = line.split(" |,");
List<String> arguments = new ArrayList<String>();
String macroName = "";
for (String token : tokens) {
if (token.startsWith("&")) {
arguments.add(token);
} else {
macroName = token;
}
}
// Assigning line to become linve from next char after whitespace is found (in order to find
// argumets which are seperated by ',')
mnt.add(new MacroNameTable(i, macroName));
ala.put(macroName, arguments);
}
}
System.out.println("**********Macro definition table**********");
System.out.println(mdt);
System.out.println("\n**********Macro name table**********");
for (MacroNameTable temp : mnt) {
System.out.println(temp.getName() + " " + temp.getIndex());
}
System.out.println("\n**********Argument array list**********");
System.out.println(ala);
macroProcessor.createOutputFile(mdt);
macroProcessor.pass2(mdt, mnt, ala);
} // Main method
}