-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol.c
165 lines (138 loc) · 3.69 KB
/
symbol.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
#include "symbol.h"
#include "symbollist.h"
#include "instructionsregister.h"
#include "dataregister.h"
extern int get_file_line();
extern void set_error_mode();
bool is_valid_symbol_data_type(char * type);
bool is_valid_symbol_command_type(char * command);
/**
* returns symbol name/label
*/
char * get_symbol_name(char ** words)
{
int i = 0;
char * word;
char * name;
char * newName;
unsigned wordLength;
while(words[i]) {
word = words[i];
wordLength = strlen(words[i]);
name = (char *) malloc(wordLength * sizeof(char));
if(word[wordLength-1] == SYMBOL_ASSIGN_SIGN) {
newName = (char *) realloc(name, wordLength * sizeof(char)); /* change to realloc anat */
strncat(newName, word, wordLength-1);
break;
}
i++;
}
return newName; /* anat changed */
}
unsigned int get_symbol_type(char ** words) /* anat i think that according to this we should insert to the symbols table*/
{ /* anat so we should add more ifs and whiles */
int i = 0;
SymbolType type;
while(words[i]) {
/* word starts with DATA initializer */
if (words[i][0] == '.')
{
/* validate approved type */
if(!is_valid_symbol_data_type(words[i]))
{
printf("unknown data type %s on line %d\n", words[i], get_file_line());
set_error_mode();
}
if ((strcmp(words[i], DATA_TYPE_DATA) == 0) || (strcmp(words[i], DATA_TYPE_STRING) == 0))
{
type = DATA;
break;
}
else if ((strcmp(words[i], DATA_TYPE_ENTRY) == 0))
{
type = ENTRY;
break;
}
else if((strcmp(words[i], DATA_TYPE_EXTERN) == 0))
{
type = EXTERNAL;
break;
}
}
i++;
}
if(type != DATA && type != ENTRY && type != EXTERNAL) { /* anat chacham */
/* validate COMMAND type */
if(!is_valid_symbol_command_type(words[1]))
{
printf("unknown command %s on line %d\n", words[1], get_file_line());
set_error_mode();
}
type = CODE; /* anat COMMAND was here */
}
return type;
}
unsigned int calculate_symbol_memory_size(char ** words, int type, char * name)
{
unsigned int size = 0;
char ** data;
/* only data without symbol name */
data = remove_symbol_name(words, name);
if (type == ENTRY) { /* anat COMMAND was here */
/* calculate command */
size = calculate_command_space(data);
} else if (type == DATA) {
/* calculate data */
size = calculate_data_space(data);
}
return size;
}
char ** remove_symbol_name(char ** words, char * name)
{
int i = 0;
int counter = 0;
char ** newWords = NULL;
/* clone name */
char * nameClone = (char *) malloc(strlen(name) * sizeof(char));
strcpy(nameClone, name);
/* concat assign symbol to match the one in the original file */
strcat(nameClone, ":");
/**
* copy words array without symbols name
*/
while(words[i]) {
if(strcmp(nameClone, words[i]) != 0) {
/*reallocating space from the next word in the array*/
newWords = (char **) realloc(newWords, (counter + 1) * sizeof(char *));
/* allocate space for the incoming word*/
newWords[counter] = (char *) malloc(sizeof(words[i]) * sizeof(char));
/* actually setting the word */
newWords[counter] = words[i];
counter++;
}
i++;
}
/* add NULL to last index to indicate the end of the array */
newWords = (char **) realloc(newWords, counter * sizeof(char *));
newWords[counter] = (char *) malloc(sizeof(char));
newWords[counter] = NULL;
/*free space*/
nameClone = NULL;
free(nameClone);
return newWords;
}
bool is_valid_symbol_data_type(char * type)
{
if((strcmp(type, DATA_TYPE_DATA) == 0) || (strcmp(type, DATA_TYPE_STRING) == 0)
|| (strcmp(type, DATA_TYPE_ENTRY) == 0) || (strcmp(type, DATA_TYPE_EXTERN) == 0)) {
return true;
}
return false;
}
bool is_valid_symbol_command_type(char * command)
{
if(is_command(command)) {
return true;
}
return false;
}