forked from AkshytaKatyal/Two-pass-Assembler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile_input.java
232 lines (201 loc) · 4.83 KB
/
File_input.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
package pack1;
//import pack1.Symbol;
//import java.math.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
//class DuplicateLabelException extends Exception
//{
// DuplicateLabelException()
// {
// super("Duplicate Label");
//
// }
//}
//class InvalidOpcodeException extends Exception
//{
// InvalidOpcodeException()
// {
// super("Invalid Opcode");
// }
//}
public class File_input {
static ArrayList<String> finput=new ArrayList<String>();
public static void main(String[] args) throws Exception{
File file = new File("D:\\eclipse-workspace\\caos\\src\\pack1\\inp.txt");
Table_P1.main(args);
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
int lc=0;
int line=0;
while ((st = br.readLine()) != null)
{
line++;
if(st.isEmpty()==false)
{
if(!st.substring(0,2).equals("//"))
{
finput.add(st);
if(!(st.substring(0,5).equals(" ")))
{
String str=st.substring(0,5);
int temp=0;
for(int i=0;i<5;i++)
{
if(str.charAt(i)==' ')
{
temp=i;
break;
}
temp=temp+1;
}
int flag=0;
for(int j=0;j<Table_P1.symTable.size();j++)
{
//System.out.print(Table_P1.symTable.get(j).name+" ");
if(Table_P1.symTable.get(j).name.compareTo(str.substring(0,temp))==0)
{
flag=1;
break;
}
}
//System.out.println();
if(flag==0)
{
if(st.substring(7,10).equals("DC "))
{
String s=st.substring(12,17);
int save=0;
for(int i=0;i<5;i++)
{
if(s.charAt(i)==' ')
{
save=i;
break;
}
save=save+1;
}
//int n=Integer.parseInt(s.substring(0, save));
Table_P1.symTable.add(new Symbol(str.substring(0,temp),"variable",s.substring(0, save),lc));
}
else
{
int check=0;
for(int j=0;j<Table_P1.opcodes.size();j++)
{
//System.out.println(Table_P1.opcodes.get(j).op+" "+st.substring(7,10));
if(Table_P1.opcodes.get(j).op.compareTo(st.substring(7,10))==0)
{
check=1;
}
}
//error
if(check==0)
{
Table_P1.ff.add(new error(line,1,lc));
//System.out.println("Invalid opcode");
}
else
{
Table_P1.symTable.add(new Symbol(str.substring(0,temp),"label",null,lc));
}
}
}
else
{
//error
Table_P1.ff.add(new error(line,2,lc));
// System.out.println("Duplicate label");
//throw(new DuplicateLabelException());
}
}
else
{
int check=0;
//System.out.println(Table_P1.opcodes.size());
for(int j=0;j<Table_P1.opcodes.size();j++)
{
//System.out.println(Table_P1.opcodes.get(j).op+" "+st.substring(7,10));
if(Table_P1.opcodes.get(j).op.compareTo(st.substring(7,10))==0)
{
check=1;
break;
}
}
//error
if(check==0)
{
Table_P1.ff.add(new error(line,1,lc));
//System.out.println("Invalid opcode");
}
}
//System.out.println(st+" "+st.length());
String a=st.substring(12,17);
int val=0;
for(int b=0;b<5;b++)
{
if(a.charAt(b)==' ')
{
break;
}
val=val+1;
}
int f=0;
if(st.substring(7,10).equalsIgnoreCase("STP"))
{
for(int x=12;x<st.length();x++)
{
if(st.charAt(x)=='/')
{
break;
}
else if(st.charAt(x)!=' ')
{
Table_P1.ff.add(new error(line,3,lc));
f=1;
break;
}
}
}
else
{
if(val==0 )
{
f=1;
Table_P1.ff.add(new error(line,3,lc));
}
for(int x=12+val+1;x<st.length();x++)
{
if(st.charAt(x)=='/')
{
break;
}
else if(st.charAt(x)!=' ')
{
Table_P1.ff.add(new error(line,3,lc));
f=1;
break;
}
}
if(f==0)
{
a=a.substring(0,val);
if(a.charAt(0)=='=')
{
Table_P1.Literals.add(new literal(a.substring(1,a.length()),lc));
}
}
}
lc++;
}
}
}
br.close();
// for(int i=0;i<Table_P1.symTable.size();i++)
// {
// System.out.println(Table_P1.symTable.get(i).name+" "+Table_P1.symTable.get(i).type+" "+Table_P1.symTable.get(i).value+" "+Table_P1.symTable.get(i).offset);
// }
//
}
}