-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
226 lines (195 loc) · 5.09 KB
/
functions.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
#include "functions.h"
#include "identifiers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/**
* @brief creates a new function and stores it in a pointer
* @param f the function created
* @param args the arguments of the function
* @param argc the argument count
* @param name the name of the function
* @param lineDecl the line where the funciton is declared
* @param filename the file name of the funciton
* */
void create_function(function* f,identifier* args,size_t argc,char* name,int lineDecl,char* filename){
if(!f){
fprintf(stderr,"Error :function is null!\n");
return;
}
f->arguments = malloc(sizeof(identifier)*argc);
if(!f->arguments){
fprintf(stderr,"Error : no more memory!\n");
return;
}
f->argc = argc;
f->lineDecl = lineDecl;
memcpy(f->arguments,args,sizeof(identifier)*argc);
f->name = malloc(strlen(name)+1);
if(!f->name){
fprintf(stderr,"Error : no more memory!\n");
return;
}
strcpy(f->name,name);
f->filename = malloc(strlen(filename)+1);
if(!f->filename){
fprintf(stderr,"Error : no more memory!\n");
return;
}
strcpy(f->filename,filename);
return ;
}
/**
* @brief adds a return type to a function
* @param f the function which we add a return type to
* @param returnType the return type to bre added
* */
void add_return_type(function* f,enum types returnType){
if(f->returnType){
fprintf(stderr,"Type checking error : return type already defined !\n");
return;
}
f->returnType = returnType;
return;
}
/**
* @brief check if the return type is the one expected
* @param f the function that is matched
* @param given the type expected
* @return 1 if the types matches, 0 else
* */
int check_return_type(function* f, enum types given){
return f->returnType == given;
}
/**
* @brief free the data of the funciton
* @param f the function to free
* */
void free_function(function* f){
free(f->arguments);
free(f->name);
free(f);
return;
}
/**
* @brief compares two function to see if they mare identical
* @param f1 the first functiojn
* @param f2 the second one
* @return 0 if they match, != 0 else
* */
int functions_equals(function* f1, function* f2){
if(!strcmp(f1->name,f2->name) && f1->argc==f2->argc){
//name is the same. and same argc !
if(f1->returnType == f2->returnType){
//same return type!
//need to check if they are the same arg types.
for(int i = 0; i < f1->argc; i++){
if(f1->arguments[i].type != f2->arguments[i].type){
//if any argument is different type
return 1;
}
}
//we havent returned anything so they match
return 0;
}
}
//not the same.
return 1;
}
/**
* @brief initializes a list
* @param fl the function list to initialize
* */
void init_list(func_list* fl){
if(!fl){
fprintf(stderr,"Error : Function list is null!\n");
return;
}
fl->size = 0;
fl->functions = malloc(sizeof(function));
}
/**
* @brief chekcs if a function is in the function list.
* @param fl the function list to search from
* @param f the function to search for
* @return the idx of the function in fl if successful. <0 else.
* */
int check_if_exists(func_list* fl,function* f){
if(!f || !fl){
return -1;
}
for(int i = 0; i < fl->size;i++){
if(0==functions_equals(&fl->functions[i],f)){
return i;
}
}
//no match found.
return -1;
}
/**
* @brief add a function to the function list
* @param fl the list to add it to
* @param f the function to add
* */
void add_function(func_list* fl, function* f){
//check if the function has already been declared.
if(!fl || !f){
fprintf(stderr,"Error : function or function list has not been allocated\n");
return;
}
int i = check_if_exists(fl,f);
if(i > 0){
fprintf(stderr,"Type checking error : function %s has already been declared line : %d\n",f->name,fl->functions[i].lineDecl);
return;
}
//create a copy of the function and add it.
function* cpy = malloc(sizeof(function));
if(!cpy){
fprintf(stderr,"Error : no more memory!\n");
return;
}
copyfunc(cpy,f);
//prepare some memory for the new array.
int size = fl->size;
function* newArray;
newArray = realloc(fl->functions, (size+1)*sizeof(function));
if(!newArray){
fprintf(stderr,"Error : no more memory!\n");
return;
}
//successful nbow add it.
newArray[size] = *cpy;
fl->functions = newArray;
fl->size++;
return;
}
/**
* @brief copy a function.
* @param copy the new function
* @param orig the original function
* */
void copyfunc(function* copy,function* orig){
if(!orig || !copy){
return;
}
copy->name = malloc(sizeof(strlen(orig->name)));
strcpy(copy->name,orig->name);
copy->returnType = orig->returnType;
copy->argc = orig->argc;
copy->arguments = malloc(copy->argc*sizeof(identifier));
if(!copy->arguments || !copy->name){
fprintf(stderr,"Error : no more memory!\n");
return;
}
memcpy(copy->arguments,orig->arguments,copy->argc*sizeof(identifier));
copy->lineDecl = orig->lineDecl;
return;
}
/**
* @brief clear all data of the function list
* @param fl the function list to clear
* */
void clear_func_list(func_list* fl){
free(fl->functions);
free(fl);
}