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
| void Init(Environnement *Env)
{
unsigned int i;
for (i = 0; i < MAX; i++)
{
Env->tab[i].type = INDETERMINE;
Env->tab[i].nom = NULL;
Env->tab[i].nb_param = 0;
Env->tab[i].fct = NULL;
Env->tab[i].a = NULL;
Env->tab[i].etat = LIBRE;
}
}
int hash(const char *nom)
{
int som = 0;
int i;
size_t lg = strlen(nom);
for (i = 0; i < lg; i++)
som += nom[i];
return som % MAX;
}
/* Recherche la position d'un nom dans la table */
unsigned int RechercheNom(Environnement *Env, const char *nom)
{
unsigned int pos ;
for (pos = 0U; pos < MAX; ++pos)
if (Env->tab[pos].nom != NULL && (!(strcmp(nom, Env->tab[pos].nom))))
return pos;
return MAX;
}
int AddVariable(Environnement *Env, const char *nom, Arbre a)
{
unsigned pos = RechercheNom(Env, nom);
if (pos != MAX) /* deja present */
{
fprintf(stderr,"erreur,cette variable est deja presente\n");
return 1;
}
unsigned int indice = hash(nom);
unsigned int marque = indice;
if (Env->tab[indice].etat == OCCUPE)
{
indice ++;
while (Env->tab[indice].etat == OCCUPE && indice != marque)
{
if (indice < MAX)
indice ++;
else
indice -= MAX;
}
}
if (indice == marque)
{
fprintf(stderr, "la table est remplit\n");
return 1 ;
}
Env->tab[indice].type = 0;
Env->tab[indice].nom = (char*)malloc(strlen(nom) + 1);
if (Env->tab[indice].nom == NULL)
{
fprintf(stderr, "erreur allocation\n");
return 1 ;
}
strcpy(Env->tab[indice].nom, nom);
Env->tab[indice].a = a;
Env->tab[indice].etat = OCCUPE;
return 0;
}
/* Ajout d'une fonction dans la table des symboles */
int AddFonction(Environnement *Env, const char *nom, int nb_param, char **tabparam, Arbre a)
{
unsigned pos = RechercheNom(Env, nom);
if (pos != MAX) /* deja present */
{
fprintf(stderr,"erreur,cette fonction est deja presente\n");
return 1;
}
unsigned int indice = hash(nom);
unsigned int marque = indice;
if (Env->tab[indice].etat == OCCUPE)
{
indice ++;
while (Env->tab[indice].etat == OCCUPE && indice != marque)
{
if (indice < MAX)
indice ++;
else
indice -= MAX;
}
}
if (indice == marque)
{
fprintf(stderr, "la table est remplit\n");
return 1 ;
}
Env->tab[indice].type = 1;
Env->tab[indice].nom = (char*)malloc(strlen(nom) + 1);
if (Env->tab[indice].nom == NULL)
{
fprintf(stderr, "erreur allocation\n");
return 1 ;
}
strcpy(Env->tab[indice].nom, nom);
Env->tab[indice].nb_param = nb_param;
int i ;
Env->tab[indice].fct = (TableFonction*)malloc(sizeof(TableFonction) * nb_param);
if (Env->tab[indice].fct == NULL)
return 1;
for (i = 0; i < nb_param; i++)
{
Env->tab[indice].fct[i].param = malloc(strlen(tabparam[i]) + 1);
if (Env->tab[indice].fct[i].param == NULL)
{
fprintf(stderr, "erreur allocation\n");
return 1;
}
strcpy(Env->tab[indice].fct[i].param, tabparam[i]);
Env->tab[indice].fct[i].arbresparam = NULL;
}
Env->tab[indice].a = a;
Env->tab[indice].etat = OCCUPE;
return 0;
}
/* Recherche si une variable appartient la table et donne sa valeur si c'est le cas */
int RechercheVariable(Environnement *Env,const char *nom, Arbre *arbre)
{
unsigned pos = RechercheNom(Env,nom);
/* la variable n'a pas ete trouvee */
if (pos == MAX) return 1;
/* trouvee */
if(Env->tab[pos].type != 0)
{
fprintf(stderr,"ce symbole n'est pas une variable\n");
return 1;
}
*arbre = Env->tab[pos].a;
return 0;
}
/* Recherche d'une fonction dans la table des symboles */
int RechercheFonction(Environnement *Env, const char *nom, int *nb_param, char **tabparam, Arbre *a)
{
unsigned pos = RechercheNom(Env, nom);
if (pos == MAX) /* pas trouve */
return 1 ;
*nb_param = Env->tab[pos].nb_param;
tabparam = malloc(sizeof(*tabparam) * (*nb_param));
if (tabparam == NULL)
return 1;
int i;
for (i = 0; i < (*nb_param); i++)
{
tabparam[i] = malloc(sizeof(**tabparam) * (strlen(Env->tab[pos].fct[i].param) + 1));
if (tabparam[i] == NULL)
return 1;
strcpy(tabparam[i],Env->tab[pos].fct[i].param);
}
*a = Env->tab[pos].a;
return 0;
}
int main(int argc, char *argv[])
{
Environnement Env;
Init(&Env);
if(AddVariable(&Env,argv[1],NULL))
fprintf(stderr,"erreur\n");
else
printf("%s",argv[1]);
return 0;
} |
Partager