-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.c
232 lines (197 loc) · 4.72 KB
/
output.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
#include "output.h"
extern int instructions_memory;
extern int data_memory;
extern char ** entry_list;
OutputLinePtr outputLineHead = NULL;
ExternalItemList * external_list = NULL;
char outputLineSymbols [4] = {
'*',
'#',
'%',
'!'
};
extern int get_symbol_value_by_name(char * name);
void push_line_to_list(OutputLine line)
{
OutputLinePtr current = outputLineHead;
/* if head is empty*/
if(outputLineHead == NULL) {
/* push item to head*/
outputLineHead = (OutputLinePtr) malloc(sizeof(OutputLineItem));
outputLineHead->line = line;
outputLineHead->next = NULL;
} else {
/* go to last list item*/
while(current->next != NULL) {
current = (OutputLinePtr) current->next;
}
/* push line to last item */
current->next = (OutputLinePtr) malloc(sizeof(OutputLineItem));
current->next->line = line;
current->next->next = NULL;
}
}
void print_outputline_list()
{
OutputLinePtr current = outputLineHead;
if(outputLineHead == NULL) {
printf("\n the list is empty\n");
return;
}
printf("--------Outputline List-------\n");
while(current != NULL) {
printf("--------------------------\n");
printf("line number: %d\n",current->line.lineNumber);
printf("data: %s\n", current->line.bits);
printf("--------------------------\n");
current = current->next;
}
}
/*function that take a nuber and make it a bit with the given size */
char * decimal_to_bin(int n,int s)
{
int c, d, count;
char *pointer;
count = 0;
pointer = (char*)malloc(s+1);
for (c = s-1; c >= 0; c--) {
d = n >> c;
if (d & 1) {
*(pointer+count) = 1 + '0';
} else {
*(pointer+count) = 0 + '0';
}
count++;
}
*(pointer+count) = '\0';
return pointer;
}
int bin_to_decimal(char * bits)
{
int i = 0;
int dec = 0;
while (bits[i]) {
if (bits[i] == '1'){
dec = dec * 2 + 1;
} else if (bits[i] == '0') {
dec *= 2;
}
i++;
}
return dec;
}
void reset_output_list()
{
OutputLinePtr current = outputLineHead;
while(current) {
current->line.bits = NULL;
free(current->line.bits);
free(current);
current = current->next;
}
outputLineHead = NULL;
}
void add_to_external_list(char * name, int line)
{
ExternalItem external_item;
ExternalItemList * current = external_list;
external_item.line = line;
external_item.symbol = name;
if(external_list == NULL) {
external_list = (ExternalItemList *) malloc(sizeof(ExternalItemList));
external_list->external = external_item;
external_list->next = NULL;
} else {
while(current->next) {
current = current->next;
}
current->next = (ExternalItemList *) malloc(sizeof(ExternalItemList));
current->next->external = external_item;
current->next->next = NULL;
}
return;
}
void reset_external_list()
{
ExternalItemList * current = external_list;
while(current) {
current->external.symbol = NULL;
free(current->external.symbol);
free(current);
current = current->next;
}
external_list = NULL;
}
char * concat(const char *s1, const char *s2)
{
char *result = malloc(strlen(s1) + strlen(s2) + 1);
strcpy(result, s1);
strcat(result, s2);
return result;
}
void create_output_files(char * filename)
{
char * ofilename = concat(filename, ".ob");
char * extfilename = concat(filename, ".ext");
char * entfilename = concat(filename, ".ent");
create_o_file(ofilename);
create_ext_file(extfilename);
create_ent_file(entfilename);
}
void create_o_file(char * filename)
{
FILE *file;
OutputLinePtr current = outputLineHead;
file = fopen(filename, "w");
fprintf(file, " %d %d\n", instructions_memory, data_memory);
while(current) {
fprintf(file, "%d %s\n", current->line.lineNumber, bits_to_special_base4(current->line.bits));
current = current->next;
}
fclose(file);
}
char * bits_to_special_base4(char * bits)
{
int i = 0;
int index = 0;
char * currentBits = (char *) malloc(3 * sizeof(char));
char * symbolLine = (char *) malloc((strlen(bits)/2) * sizeof(char));
/* TODO add special 4 base logic */
while(bits[i]) {
currentBits[0] = bits[i];
currentBits[1] = bits[i+1];
currentBits[2] = '\0';
index = bin_to_decimal(currentBits);
symbolLine[(i+1)/2] = outputLineSymbols[index];
i+=2;
}
return symbolLine;
}
void create_ext_file(char * filename)
{
FILE *file;
ExternalItemList * current = external_list;
if (!current) { /* do not create unnecessery file */
return;
}
file = fopen(filename, "w");
while(current) {
fprintf(file, "%s %d\n", current->external.symbol, current->external.line);
current = current->next;
}
fclose(file);
}
void create_ent_file(char * filename)
{
FILE *file;
int i = 0;
if (!entry_list) { /* do not create unnecessery file */
return;
}
file = fopen(filename, "w");
while(entry_list[i]) {
fprintf(file, "%s %d\n", entry_list[i], get_symbol_value_by_name(entry_list[i]));
i++;
}
fclose(file);
}