-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.c
347 lines (316 loc) · 6.8 KB
/
parse.c
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
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "asm.h"
#include "parse.h"
#include "error.h"
#include "symbol.h"
FILE *pInfile;
char InBuff[MAXINBUFF];
char FilenameBuff[80];
char *current_command = InBuff;
int current_command_type;
int k = 0;
void dump_buffer()
{
int i = 0;
while(InBuff[i] != '\0')
{
printf("%c", InBuff[i++]);
}
printf("\n");
}
void reset_buffer()
{
current_command = InBuff;
}
int find_line_num()
{
int i = 1;
char *k = InBuff;
while(current_command != k)
{
if(*k == '\n') { ++i; }
++k;
}
return i;
}
int search_command(const char *current_command, const char term)
{
int i = 0;
int found = 0;
while(*(current_command+i) != '\r' && *(current_command+i) != '\n' && *(current_command+i) != '\0')
{
if(*(current_command+i) == term) { ++found; }
++i;
}
return found;
}
void print_current_command()
{
int i = 0;
while(*(current_command+i) != '\r' && *(current_command+i) != '\n' && *(current_command+i) != '\0' && i <= 20)
{
printf("%c", *(current_command+i));
++i;
}
if(settings.code != 0 || settings.comments != 0) { printf("\n"); }
}
void init_parser(char *FilenameBuff)
{
if((pInfile = fopen(FilenameBuff, "rb")) == NULL)
exit_error(3, "Cannot Open Input (Source) File");
k = -1;
while(!feof(pInfile) && k < MAXINBUFF)
{
InBuff[++k] = fgetc(pInfile);
}
InBuff[k] = '\0';
if(k < MAXINBUFF)
{
/* printf("Input Buffer Loaded\n"); */
} else {
exit_error(4, "Input Too Large for Input Buffer");
}
}
int has_more_commands()
{
int i = 0;
while(isprint(*(current_command+i))) { ++i; }
if(*(current_command+i) == '\0') { return 0; }
if(isspace(*(current_command+i)))
{
do {
++i;
} while (isspace(*(current_command+i)));
if(*(current_command+i) == '\0') { return 0; }
}
return 1;
}
void advance()
{
do {
++current_command;
} while(*current_command != '\n' && *current_command != '\0');
while(isspace(*current_command)) { ++current_command; }
if(*(current_command+1) == '/')
{
++current_command;
advance();
}
}
int command_type()
{
if(*current_command == '@')
{
current_command_type = A_COMMAND;
return A_COMMAND;
} else if(search_command(current_command, '(') > 0 || search_command(current_command, ')') > 0) {
current_command_type = L_COMMAND;
return L_COMMAND;
} else if(!isdigit(*current_command)){
current_command_type = C_COMMAND;
return C_COMMAND;
} else if(isdigit(*current_command)){
current_command_type = C_COMMAND;
return C_COMMAND;
} else {
int i = find_line_num();
line_notification(i);
exit_error(7, "Incorrect Command Type");
return -1;
}
}
int symbol()
{
int convert = 0, i = 0;
int address = 0;
char sym[MAXSYMBOL];
/* determine if this is a symbol or an address */
convert = isdigit(*(current_command+1));
if(current_command_type == A_COMMAND)
{
if(convert == 0) /* text (symbolic) entries */
{
i = 0;
while(!isspace(*(current_command+i+1)))
{
sym[i] = *(current_command+i+1);
++i;
}
sym[i++] = '\0';
address = get_address(sym);
if(address == 0)
{
address = get_ram_address();
add_entry(sym,-1);
}
return address;
} else { /* numeric entries */
i = 0;
while(!isspace(*(current_command+i+1)))
{
sym[i] = *(current_command+i+1);
++i;
}
sym[i++] = '\0';
/* value looks like a string of chars must be converted */
address = (int)strtol(sym, (char **) NULL, 10);
return address;
}
}
/* TODO: THIS MUST BE CHANGED TO LOOK IN HASH FOR THIS SYMBOL
BY NOW IT HAS ALREADY BEEN ADDED TO THE HASH TABLE
*/
if(current_command_type == L_COMMAND)
{
if(convert == 0)
{
/* lookup this symbol in hash table and get it's address */
i = 0;
while(!isspace(*(current_command+i+1)) && *(current_command+i+1) != ')')
{
sym[i] = *(current_command+i+1);
++i;
}
sym[i++] = '\0';
return get_address(sym);
} else {
/* convert numbers into integer and return current symbol as integer */
i = 0;
while(!isspace(*(current_command+i+1)) && *(current_command+i+1) != ')')
{
sym[i] = *(current_command+i+1);
++i;
}
sym[i++] = '\0';
return (int)strtol(sym, (char **) NULL, 10);
}
}
exit_error(8, "Symbol Function Called on Incorrect Command Type.");
return 0;
}
int symbol_load()
{
char sym[MAXCOMMAND];
int i = 0;
while(!isspace(*(current_command+i+1)) && *(current_command+i+1) != ')')
{
sym[i] = *(current_command+i+1);
++i;
}
sym[i++] = '\0';
add_entry(sym, get_rom_address());
return 0;
}
int dest(char dest[])
{
int i = 0;
int dest_exist = 0;
if(current_command_type == C_COMMAND)
{
while(*(current_command+i) != '\n' && *(current_command+i) != '\0')
{
if(*(current_command+i) == '=') { ++dest_exist; }
++i;
}
if(dest_exist == 0) /* no dest in command */
{
dest[0] = '\0';
return 1;
}
i = 0;
while(*(current_command+i) != '=')
{
dest[i] = *(current_command+i);
i++;
}
dest[i] = '\0';
return 1;
}
i = find_line_num();
line_notification(i);
exit_error(8, "Symbol Function Called on Incorrect Command Type.");
return 0;
}
int comp(char comp[])
{
int i = 0;
int eq_exist = 0;
int semi_exist = 0;
char *delimiter = NULL;
while(*(current_command+i) != '\n' && *(current_command+i) != '\0')
{
if(*(current_command+i) == '=') { ++eq_exist; delimiter = current_command+i; }
if(*(current_command+i) == ';') { ++semi_exist; }
++i;
}
if(!semi_exist && !eq_exist)
{
line_notification(i);
exit_error(9, "Command Does Not Contain Proper Delimiter.");
return 0;
}
if(current_command_type == C_COMMAND)
{
if(eq_exist) /* found '=' in command -- skip to char after equal and copy till new line */
{
i = 0;
++delimiter;
while(*(delimiter+i) != ';' && !isspace(*(delimiter+i)) && *(delimiter+i) != '\0')
{
comp[i] = *(delimiter+i);
i++;
}
} else { /* no '=' in command -- copy until semicolon found */
i = 0;
while(*(current_command+i) != ';')
{
comp[i] = *(current_command+i);
i++;
}
}
comp[i] = '\0';
return 1;
}
i = find_line_num();
line_notification(i);
exit_error(8, "Symbol Function Called on Incorrect Command Type.");
return 0;
}
int jump(char jump[])
{
int i = 0;
char *delimiter = NULL;
int valid_command = 0;
if(current_command_type == C_COMMAND)
{
/* Test for semicolon before newline char */
i = 0;
while(*(current_command+i) != '\n' && *(current_command+i) != '\0')
{
if(*(current_command+i) == ';') { ++valid_command; }
++i;
}
if(valid_command == 0)
{
jump[0] = '\0';
return 1;
} /* no jump in command */
delimiter = strchr(current_command, ';');
++delimiter;
i = 0;
while(!isspace(*(delimiter+i)) && *(delimiter+i) != '\0')
{
jump[i] = *(delimiter+i);
++i;
}
jump[i] = '\0';
return 1;
}
i = find_line_num();
line_notification(i);
exit_error(8, "Symbol Function Called on Incorrect Command Type.");
return 0;
}