-
Notifications
You must be signed in to change notification settings - Fork 0
/
passOne.java
207 lines (151 loc) · 4.07 KB
/
passOne.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
package Assembler;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
public class passOne {
String line, symbol, literal, opcode,vartype; // To store the parts of instruction
int location_counter, length, value;
// Different tables used
SymbolTable stable;
LabelTable Labtable;
literal_table littable;
final int end_statement = -2;
// Input file
BufferedReader reader;
int pointer;
// Opcodes
Opcodes op = new Opcodes();
public passOne(BufferedReader reader, SymbolTable s, LabelTable l, literal_table t, File f) {
stable = s;
Labtable = l;
littable = t;
this.reader = reader;
location_counter = 0;
pointer = 0;
}
// Checking comments
boolean checkcomment(String line) {
if (line.startsWith("//")) {
return true;
}
return false;
}
// Checking Symbol for storing variables
boolean checksymbol(String line){
symbol = line.substring(0, 8);
vartype = line.substring(8,10);
if (vartype.equals("DC")) {
return true;
}
return false;
}
// Checking label
boolean checkLabel(String line){
symbol = line.substring(0, 8);
opcode = line.substring(8,11);
if(op.getbinary(opcode) != null && !symbol.equals(" ") && !symbol.startsWith(" ")) {
return true;
}
return false;
}
// Checking Literal
boolean checkLiteral(String line) throws IllegalLiteralDeclaration{
literal = line.substring(20,Math.min(27,line.length()));
//System.out.println(literal);
if (literal.charAt(0) == '"') {
if (literal.charAt(1) == '=') {
return true;
}
else {
throw (new IllegalLiteralDeclaration());
}
}
return false;
}
// Function for passone
void passone() throws IOException {
line = reader.readLine();
while(line != null) {
pointer++;
//System.out.println(line);
if (!checkcomment(line) && line.length()!=0) {
if (line.substring(0, 3).equals("END")) {
break;
}
try {
if (line.length() <20) {
throw (new InvalidFormat());
}
if (checksymbol(line)) {
//System.out.println(111);
symbol = line.substring(0, 8);
String tempsymbol = symbol;
String temp = "";
int pp = 0;
while(tempsymbol.charAt(pp) != ' ') {
temp = temp + tempsymbol.substring(pp, pp+1);
pp++;
}
try {
if (stable.search(temp) != -1) {
throw (new MultipleDeclaration());
}
stable.add(line, location_counter);
}
catch(Exception e){
System.out.println("Line Number : " + ((int)(location_counter+1)));
System.out.println(e);
}
}
if (checkLabel(line)) {
symbol = line.substring(0, 8);
String tempsymbol = symbol;
String temp = "";
int pp = 0;
while(tempsymbol.charAt(pp) != ' ') {
temp = temp + tempsymbol.substring(pp, pp+1);
pp++;
}
try {
if (Labtable.search(temp) != -1) {
throw (new MultipleDeclaration());
}
Labtable.enter_new_label(line, location_counter);
}
catch(Exception e){
System.out.println("Line Number : " + ((int)(location_counter+1)));
System.out.println(e);
}
}
if (checkLiteral(line)) {
littable.enter_new_literal(line);
}
}
catch(Exception e) {
if (e.getClass().getName().equals("Assembler.InvalidFormat")) {
}
else {
System.out.println("Line Number " + ((int)pointer));
System.out.println(e);
}
}
// if (checksymbol(line)) {
// //System.out.println(111);
// stable.add(line, location_counter);
// }
//
// if (checkLabel(line)) {
// Labtable.enter_new_label(line, location_counter);
// }
//
// if (checkLiteral(line)) {
// littable.enter_new_literal(line);
// }
location_counter++;
//System.out.println(line.substring(0, 3));
}
line = reader.readLine();
}
location_counter = littable.address_assignment(location_counter);
}
}