-
Notifications
You must be signed in to change notification settings - Fork 0
/
identifiers.c
157 lines (129 loc) · 3.3 KB
/
identifiers.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
/**
* @file identifier.c
* @brief a file holding methods to help process identifiers
* @author johan Lanzrein
* */
#include "identifiers.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/**
* @brief declares a new identifier which is returned as a pointer.
* @param id the identifier to be declared
* @param name the name of the identifier
* @param lineDecl line where it is declared
* @param the filename where it was declared
* @param type the type of the identifier
* */
void ident_decl(identifier* id, char* name,int lineDecl,char* filename,enum types type){
if(!id){
fprintf(stderr,"Error : no more memory\n");
return;
}
id->lineDecl = lineDecl;
id->type = type;
id->name = malloc(strlen(name)+1);
if(!id->name){
fprintf(stderr,"Error : no more memory\n");
return;
}
strcpy(id->name,name);
id->filename = malloc(strlen(filename)+1);
if(!id->filename){
fprintf(stderr, "Error no memory\n");
return;
}
strcpy(id->filename,filename);
return ;
}
/**
* @brief frees the resources of the identifier
* @param id the identifier to free
* */
void ident_free(identifier* id){
free(id->name);
free(id);
return;
}
/**
* @brief compares two identifiers
* @param id1 the first id
* @param id2 the second id
* @return 0 if identifier are equal != else
* */
int ident_compare(identifier* id1, identifier* id2){
if(!id1 || !id2){
return 1;
}
return strcmp(id1->name,id2->name);//0 if equal
}
/****Functions for list *********************************/
/**
* @brief initializes a list of identifier
* @param il the identifier list to initialize
* */
void init_id_list(id_list* il){
il->ids = malloc(sizeof(identifier));
il->size = 0;
return;
}
/**
* @brief adds the identifier id to the list
* @param il the list where we add the identifier
* @param id the identifier
* */
void add_id(id_list* il, identifier* id){
int size = il->size;
for(int i = 0; i < size; i++){
if(!ident_compare(&il->ids[i],id)){
fprintf(stderr, "Error type check : identifier %s already declared line %d\n",il->ids[i].name,il->ids[i].lineDecl);
return;
}
}
//add id.
identifier* cpy = malloc(sizeof(identifier));;
ident_decl(cpy,id->name,id->lineDecl,id->filename,id->type);
//prepare some memory for the new array.
identifier* newArray;
newArray = realloc(il->ids, (size+1)*sizeof(identifier));
if(!newArray){
fprintf(stderr,"Error : no more memory!\n");
return;
}
//successful nbow add it.
newArray[size] = *cpy;
il->ids = newArray;
il->size++;
return;
}
/**
* @brief search if an identifier that matches the name and type exists
* @param il the list where to search
* @param name the name we want to have
* @param expectedType the type we are looking for
* @return -1 if identifier not declared, 1 if match in name and type, 0 if mismatch of type.
* */
int check_for_identifier(id_list* il, char* name, enum types expectedType){
int size = il->size;
for(int i = 0; i < size; i++){
identifier id = il->ids[i];
if(!strcmp(id.name,name)){
//if they match
//we want to have matching type
return id.type == expectedType;
}
}
return -1;
}
/**
* @brief delete the id list and clear it
* @param il the list to clear
* */
void delete_id_list(id_list* il){
if(il == NULL){
return;
}
il->size = 0;
free(il);
return;
}