-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabvar.c
80 lines (70 loc) · 1.34 KB
/
tabvar.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
#include <stdio.h>
#include <string.h>
#include "tabvar.h"
char *str_type(Type t){
switch(t){
case INT:
return "int";
case FLOAT:
return "float";
case BOOL:
return "boolean";
case STR:
return "string";
case UNIT:
return "unit";
default:
return "no types";
}
}
void print_value(Variable v){
switch(v.t){
case INT:
printf("%d",v.val.val_i);
break;
case FLOAT:
printf("%f",v.val.val_f);
break;
case BOOL:
printf("%s",v.val.val_b ? "true":"false");
break;
case STR:
printf("%s",v.val.string);
break;
case UNIT:
printf("()");break;
default:
printf("#ERRINVALUE");
}
}
int ajouter_var (Variable *w,MemVar *m){
Variable res;
int index=chercher_var(w->nom,m,&res);
if (index!=-1)
m->tab[index]=*w;
else {
if ( m->taille >999){
printf ("erreur de segmentation\n");
return -1;
}
if ( m->taille < 0)
m->taille = 0;
m->tab[m->taille]=*w;
m->taille++;
}
return 0;
}
int chercher_var (char *var,MemVar *m,Variable *res){
int i;
for(i=0;i<m->taille;i++){
if (strcmp(m->tab[i].nom,var)==0){
*res=m->tab[i];
return i;
}
}
return -1;
}
void copie_environnement (MemVar *m ,MemVar *m2){
for (m2->taille=0; m2->taille<m->taille; m2->taille++)
m2->tab[ m2->taille]=m->tab[m2->taille];
}