-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcirc1.c
208 lines (193 loc) · 8.63 KB
/
circ1.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
#include "helpers.h"
#include "symch.c"
int circ1(list *head , symbol *symhead,data *datahead,char obfile[namelen] ){
int ic = start ,dc = 0 ;
int i , j , l , haserror = 1 , cntln = 0;
int datahelp = TRUE ;
char savename[namelen] , endln[namelen] ; /* the name for data struct */
list *current = head ; /* always start with empty word */
symbol *symcurrent = symhead ;
symbol *symhelper = symhead ; /* for macro */
data *datacurrent = datahead ;
FILE *FP=fopen(obfile,"a");
while(current != NULL) /* scaning all the words in the prog */
{
if (current -> word[strlen(current -> word) - 1] == '\n') /* end of line */
cntln++ ;
if (strcmp(current -> word,".define") == 0) /* case macro */
{
current = current -> next ; /* geting the name */
strcpy(symcurrent ->name ,current -> word) ; /* save name */
current = current -> next -> next ; /* after the name it will always be = "num" */
symcurrent -> value = atoi(current -> word) ; /* convert to int */
symcurrent -> type = macro ;
/*symcurrent -> cmtype = none ;*/
symcurrent-> next = malloc(sizeof(symbol)) ;
symcurrent = symcurrent-> next ;
cntln++ ;
}
else
if (strcmp(current -> word,".extern") == 0) /* case .extern */
{
current = current -> next ; /* next word after .extern supposed to be the name */
cntln++ ; /* name is end of line */
strcpy(symcurrent ->name ,current -> word) ;/* save name*/
symcurrent -> value = 0 ;
symcurrent -> type = external ;
symcurrent-> next = malloc(sizeof(symbol)) ;
symcurrent = symcurrent-> next ;
}
else
if (strchr(current -> word,':') != NULL) /* case symbol */
{
strcpy(symcurrent -> name,strtok(current -> word , ":")) ; /*omiting ':' */
strcpy(savename,symcurrent -> name) ; /* saving name for data */
symcurrent -> value = ic ;
if(strcmp(current -> next -> word,".data") == 0 /* case instruct */
|| strcmp(current -> next -> word,".string") == 0
|| strcmp(current -> next -> word,".entry") == 0
|| strcmp(current -> next -> word,".extern") == 0
)
{
symcurrent -> type = instrct ;
if (strcmp(current -> next -> word,".string") == 0 || strcmp(current -> next -> word,".data") == 0 ) /* case data */
symcurrent -> value = ic + dc ;
}
else
symcurrent -> type = cmnd ;
/*symcurrent -> cmtype = none ;*/
symcurrent-> next = malloc(sizeof(symbol)) ;
symcurrent = symcurrent-> next ;
}
else
if(strcmp(current -> word,".data") == 0)
{
current = current -> next ; /* the Variables comes after the word .data */
cntln++ ;
i = 0 ;
datahelp = TRUE ; /* helps detrmin if we are at the end of the line */
strcpy(endln , " ") ;/* making sure the word is empty */
while(datahelp) /* stops at the end of line */
{
if(current -> word[strlen(current -> word) - 1] == '\n')/* last word is end of line so making sure to enter the loop */
{
datahelp = FALSE ;
for (l=0;l < strlen(current -> word)-1;l++) /* copying the name without \n */
endln[l] = current -> word[l] ;
endln[l] = '\0' ;
}
if(isdigit(current -> word[0]) || current -> word[0] == '-' ||current -> word[0] == '+') /* case number */
{
strcpy(datacurrent -> name ,savename) ;
datacurrent -> index = i ;
i++ ;
if (datahelp) /* end line or not */
datacurrent -> val = atoi(current ->word) ;
else
datacurrent -> val = atoi(endln) ;
datacurrent -> type = tdata;
datacurrent-> next = malloc(sizeof(data)) ;
datacurrent = datacurrent -> next ;
dc++ ;
}
else
{
if(strcmp(current -> word,",") != 0) /* case macro */
{
symhelper = symhead ; /* helps search in the sym struct for macro , cant use macro without defining him first */
while(symhelper != NULL)
{
if(strcmp(symhelper -> name,current -> word) == 0 || (strcmp(symhelper -> name,endln) == 0 && datahelp == FALSE) )
{
strcpy(datacurrent -> name ,savename) ; /* entering the saved name from previous loop */
datacurrent -> index = i ;
i++ ;
datacurrent -> val = symhelper -> value ; /* the value of the macro */
datacurrent -> type = tdata;
datacurrent-> next = malloc(sizeof(data)) ;
datacurrent = datacurrent -> next ;
dc++ ;
break ; /* stop the search */
}
else
{
symhelper = symhelper -> next ;
}
}
if(symhelper == NULL)
{
printf("macro not found, error in line %d\n",cntln) ;
haserror = 0 ;
}
}
}
if(datahelp) /* start new line at the end of the while , not here */
current = current -> next ;
}
/* current = current -> next ;*/
}
else
if(strcmp(current -> word,".string") == 0)
{
current = current -> next ;
/* cntln ;*/
i = 0 ; /* index inside data */
j = 1 ; /* start at char[1] */
while(j < strlen(current -> word) - 2)/* not including the lest and first chaer wich is " */
{
strcpy(datacurrent -> name ,savename) ;
datacurrent -> index = i ;
i++ ;
datacurrent -> val = (int) (current ->word[j]) ;
datacurrent -> type = tstring;
datacurrent-> next = malloc(sizeof(data)) ;
datacurrent = datacurrent -> next ;
dc++ ;
j++ ;
}
strcpy(datacurrent -> name ,savename) ; /* entering 0 to mark end of string */
datacurrent -> index = i ;
i++ ;
datacurrent -> val = (int) ('0') ;
datacurrent -> type = tstring;
datacurrent-> next = malloc(sizeof(data)) ;
datacurrent = datacurrent -> next ;
dc++ ;
j++ ;
/* current = current -> next ; */
}
else
if(conv_enum(current -> word) != NONE) /* case command */
{
ic++ ;
}
else
if (strchr(current -> word,'[') != NULL && (strchr(current -> word,']') != NULL)) /* case arry */
ic = ic + 2 ;
else
if(conv_enum3(current -> word) != rNONE) /* case register */
{
if (strcmp(current -> next -> word , ",") == 0 && conv_enum3(current -> next -> next -> word) != rNONE)
{
current = current -> next -> next ; /*making sure i dont count twice if i have 2 registers in succession */
}
ic++ ;
}
else
if (strcmp(current -> word,".entry") == 0)
{
current = current -> next ;/* entry doesnt efect ic */
}
else
if (strcmp(current -> word,",") != 0)
{
ic ++ ;
}
current = current -> next ; /* next word */
}
if(symch(symhead) == 0)/*Checks that there are no values twice */
haserror = 0 ;
fprintf(FP," %d %d\n",ic,dc);
fclose(FP);
return haserror ; /* retrun 0 if there are errors */
}