-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableSymboles.c
184 lines (140 loc) · 4.2 KB
/
tableSymboles.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "tableSymboles.h"
extern void yyerrorSemantic(char *s);
symbole * _allouerSymbole(){
symbole * pointer = (symbole *) malloc(sizeof(symbole));
return pointer;
}
symbole * creerSymbole(char * nom, int type, bool isConstant, int length){
symbole * pointer = _allouerSymbole();
strcpy(pointer->nom, nom);
pointer->type = type;
pointer->isConstant = isConstant;
pointer->hasBeenInitialized = false;
return pointer;
}
void insererSymbole(symbole ** tableSymboles, symbole * nouveauSymbole){
if(nouveauSymbole == NULL)
return;
if (tableSymboles != NULL){
nouveauSymbole->suivant = *tableSymboles;
}
*tableSymboles = nouveauSymbole;
}
void afficherTableSymboles(symbole * tableSymboles){
if(tableSymboles == NULL){
printf("Table des symboles est vide");
return;
}
symbole * pointer = tableSymboles;
printf("************************ TABLE DES SYMBOLES ************************\n");
printf("--------------------------------------------------------------------\n");
printf("\tName\t\tType\t\tConstant\tValue \n");
while(pointer != NULL){
printf("--------------------------------------------------------------------\n");
char type[COLS];
getTypeChar(pointer, type);
printf("\t%s", pointer->nom);
printf("\t\t%s", type);
printf("\t%s", pointer->isConstant ? "Oui" : "Non");
if(pointer->hasBeenInitialized){
printf("\t\t%s", pointer->valeur);
}
printf("\n");
pointer=pointer->suivant;
}
printf("********************************************************************\n");
}
symbole * rechercherSymbole(symbole * tableSymboles, char * nom){
if(tableSymboles == NULL || nom == NULL){
return NULL;
}
symbole * pointer = tableSymboles;
while(pointer!=NULL){
if (!strcmp(pointer->nom, nom)){
return pointer;
}
pointer = pointer->suivant;
}
return NULL;
}
void getNom(symbole * symbole, char * nom){
if(symbole == NULL || nom == NULL){
return;
}
strcpy(nom, symbole->nom);
}
void getValeur(symbole * symbole, char * valeur){
if(symbole == NULL || !symbole->hasBeenInitialized || valeur == NULL){
return;
}
strcpy(valeur, symbole->valeur);
}
int getType(symbole * symbole){
if(symbole == NULL){
return -1;
}
return symbole->type;
}
void _mapTypeIntToChar(int type, char * typeChar){
if(typeChar == NULL)
return;
switch (type){
case TYPE_INTEGER:
sprintf(typeChar, "%s", "Integer\t");
break;
case TYPE_FLOAT:
sprintf(typeChar, "%s", "Float\t");
break;
case TYPE_STRING:
sprintf(typeChar, "%s", "String\t");
break;
case TYPE_BOOLEAN:
sprintf(typeChar, "%s", "Boolean\t");
break;
case TYPE_ARRAY_BOOLEAN:
sprintf(typeChar, "%s", "Boolean[]");
break;
case TYPE_ARRAY_FLOAT:
sprintf(typeChar, "%s", "Float[]");
break;
case TYPE_ARRAY_INTEGER:
sprintf(typeChar, "%s", "Integer[]");
break;
case TYPE_ARRAY_STRING:
sprintf(typeChar, "%s", "String[]");
break;
default:
break;
}
}
void getTypeChar(symbole * symbole, char * type){
if(symbole == NULL || type == NULL){
return;
}
_mapTypeIntToChar(symbole->type, type);
}
void setValeur(symbole * symbole, char * valeur){
if(symbole == NULL){
return;
}
if(symbole->hasBeenInitialized && symbole->isConstant){
return;
}
if(symbole->type <= 3)
strcpy(symbole->valeur, valeur);
symbole->hasBeenInitialized = true;
}
void setTabValeur(symbole * symbole, char tabValeur[ROWS][COLS], int length){
if(symbole == NULL){
return;
}
if(symbole->hasBeenInitialized && symbole->isConstant){
printf("Can't reassign a vlue to a constant");
return;
}
symbole->hasBeenInitialized = true;
}