-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineprocess.c
381 lines (302 loc) · 7.31 KB
/
lineprocess.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include "lineprocess.h"
#include "symbollist.h"
#include "symbol.h"
int instructions_memory = 0;
int data_memory = 0;
char ** entry_list = NULL;
extern int get_instructions_counter(int size);
extern int get_data_counter(int size);
extern bool is_command(char * command);
extern int calculate_command_space(char ** commandline);
extern void reset_instructions_counter();
extern void reset_data_counter();
extern void instruction_to_bits(char ** words);
extern void data_to_bits(char ** words);
char ** convert_line_to_words_array(char *line);
bool is_external(char word[]);
bool is_entry(char word[]);
bool is_macro(char word[]);
bool is_symbol_assign(char ** words);
void handle_external_symbol(char ** words);
void handle_macro_symbol(char ** words);
void handle_symbol_assign(char ** words);
void handle_command(char ** words);
void add_to_entry_list(char * entry);
void convert_symbol_assign_to_ouput_item(char ** words);
void convert_command_to_output_item(char ** words);
void convert_data_to_output_item(char ** words);
void reset_memory_counters();
void reset_entry_list();
void first_loop_process(char *line)
{
char ** words = convert_line_to_words_array(line);
if(is_entry(words[0])) {/* ignore entry type symbols */
return;
}else if(is_external(words[0])) {/* handle external symbols */
handle_external_symbol(words);
return;
}else if(is_macro(words[0])) {/* handle macro symbols */
handle_macro_symbol(words);
return;
}else if(is_symbol_assign(words)) { /* handle symbol assign */
handle_symbol_assign(words);
return;
} else if(is_command(words[0])) { /* handle commands */
handle_command(words);
return;
}
return;
}
void second_loop_process(char *line)
{
char ** words = convert_line_to_words_array(line);
if (is_macro(words[0])) {/* ignore macro */
return;
}else if(is_entry(words[0])) { /* add to entry list */
add_to_entry_list(words[1]);
return;
} else if (is_symbol_assign(words)) {/* handle symbols */
convert_symbol_assign_to_ouput_item(words);
return;
} else if (is_command(words[0])) {/* handle commands */
convert_command_to_output_item(words);
return;
}
}
/**
* converts a string to an array of words without the whitespaces
*/
char ** convert_line_to_words_array(char *line)
{
char * nextWord;
char ** wordArray;
int i = 0;
char *lineClone;
wordArray = NULL;
/* allocate space same as line */
lineClone = (char *) malloc((strlen(line)+1)* sizeof(char));
/* clone line */
strcpy(lineClone, line);
/*grabs the next word from the string (without whitespaces)*/
nextWord = strtok(lineClone, WORD_SPLIT_TOKENS);
while (nextWord != NULL)
{
/*reallocating space from the next word in the array*/
wordArray = (char **) realloc(wordArray, (i + 1) * sizeof(char *));
/* allocate space for the incoming word*/
wordArray[i] = (char *) malloc(sizeof(nextWord) * sizeof(char));
/* actually setting the word */
wordArray[i] = nextWord;
/* grabing the next word from the string */
nextWord = strtok (NULL, WORD_SPLIT_TOKENS);
i++;
}
/* add NULL to last index to indicate the end of the array */
wordArray = (char **) realloc(wordArray, i * sizeof(char *));
wordArray[i] = (char *) malloc(sizeof(nextWord) * sizeof(char));
wordArray[i] = NULL;
/* free alloctated spaces */
lineClone = NULL;
free(lineClone);
free(nextWord);
return wordArray;
}
/**
* checks weather sign is the .external key word
*/
bool is_external(char word[])
{
if(strcmp(EXTERNAL_SIGN, word) == 0) {
return true;
}
return false;
}
/**
* checks weather sign is the .entry key word
*/
bool is_entry(char word[])
{
if(strcmp(ENTRY_SIGN, word) == 0) {
return true;
}
return false;
}
/**
* checks weather sign is the .define key word
*/
bool is_macro(char word[])
{
if(strcmp(MACRO_SIGN, word) == 0) {
return true;
}
return false;
}
/**
* checks weather sign a regular symbol assign
*/
bool is_symbol_assign(char ** words)
{
int i = 0;
char * word;
unsigned wordLength;
while(words[i]) {
word = words[i];
wordLength = strlen(words[i]);
/* checks if the line is variable assign command */
if(word[wordLength-1] == SYMBOL_ASSIGN_SIGN) {
return true;
}
i++;
}
return false;
}
/**
* adds symbol to list
*/
void handle_external_symbol(char ** words)
{
Symbol symbol;
/* create symbol */
symbol.name = words[1];
symbol.type = EMPTY;
symbol.value = EXTERNAL_ADDRESS_VALUE;
symbol.size = 0;
symbol.isMacro = false;
symbol.isExternal = true;
/* add to symbols list */
add_symbol_to_list(symbol);
}
/**
* adds symbol to list
*/
void handle_macro_symbol(char ** words)
{
int i = 0;
Symbol symbol;
int macroValue;
/* prepare macro valus */
while(words[i]) {
/* get value after "=" sign */
if(strcmp(words[i], "=") == 0) {
macroValue = atoi(words[i+1]);
break;
}
i++;
}
/* prepare symbol */
symbol.name = words[1];
symbol.type = EMPTY;
symbol.value = macroValue;
symbol.size = 1;
symbol.isMacro = true;
symbol.isExternal = false;
/* add to symbols list */
add_symbol_to_list(symbol);
}
void handle_symbol_assign(char ** words)
{
Symbol symbol;
char * symbolName;
SymbolType symbolType;
int size;
int value;
symbolName = get_symbol_name(words);
symbolType = get_symbol_type(words);
size = calculate_symbol_memory_size(words, symbolType, symbolName);
/* differrent value for data and instructions */
if (symbolType == COMMAND) {
value = get_instructions_counter(size);
} else if (symbolType == DATA) {
value = get_data_counter(size);
}
/* create symbol */
symbol.name = symbolName;
symbol.type = symbolType;
symbol.value = value;
symbol.size = size;
symbol.isMacro = false;
symbol.isExternal = false;
add_symbol_to_list(symbol);
/* free allocated space */
symbolName = NULL;
free(symbolName);
}
void handle_command(char ** words)
{
int size = calculate_command_space(words);
/* only increment counter */
get_instructions_counter(size);
}
void update_data_symbols_addresses()
{
int ic = get_instructions_counter(0);
symbolListPtr current = symbolListHead;
while(current != NULL) {
/* only data symbols */
if (current->symbol.type == DATA) {
/* updates adres after end of ic*/
current->symbol.value += ic;
}
current = current->next;
}
}
void reset_counters()
{
reset_instructions_counter();
reset_data_counter();
}
void set_saved_memory_cells(int instructions, int data)
{
instructions_memory = instructions;
data_memory = data;
}
void add_to_entry_list(char * entry)
{
int i = 0;
if (entry_list != NULL) {
while(entry_list[i]) {
i++;
}
}
entry_list = (char **) realloc(entry_list, (i + 1) * sizeof(char *));
entry_list[i] = (char *) malloc(sizeof(entry));
entry_list[i] = entry;
return;
}
void convert_symbol_assign_to_ouput_item(char ** words)
{
char * name;
char ** action;
name = get_symbol_name(words);
action = remove_symbol_name(words, name);
if (is_command(action[0])) {
convert_command_to_output_item(action);
} else { /* is data symbol */
convert_data_to_output_item(action);
}
}
void convert_command_to_output_item(char ** words)
{
instruction_to_bits(words);
}
void convert_data_to_output_item(char ** words)
{
data_to_bits(words);
}
void reset_memory_counters()
{
instructions_memory = 0;
data_memory = 0;
reset_entry_list();
}
void reset_entry_list()
{
int i = 0;
if (entry_list) {
while(entry_list[i]) {
entry_list[i] = NULL;
free(entry_list[i]);
i++;
}
}
}