-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterp.c
153 lines (146 loc) · 5.06 KB
/
interp.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
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arbre.h"
#include "pppascal.tab.h"
#include "interp.h"
/*-------------------------------------------------------------------*/
/* ----------------------------types---------------------------------*/
/* NOE,ENVTY,BILENVTY : definis dans arbre.h */
/*-------------------------------------------------------------------*/
/* ------------------VARIABLES GLOBALES ------------------------------*/
/* le tas; (NIL=0); "vraies" adresses >=1 */
int TAS[TAILLEMEM];
/* ADR[i]=adresse dans le tas du tab i */
int ADR[TAILLEADR];
/* TAL[i]=taille du tab i */
int TAL[TAILLEADR];
int ptasl=1; /* premiere place libre dans TAS[] */
int padrl=1; /* premiere place libre dans ADR[] */
/*--------------------------------------------------------------------*/
/* ----------------------traitement--memoire--------------------------*/
void init_memoire()
{int i=0;
while (i < TAILLEMEM)
TAS[i++]=0;
i=0;
while (i < TAILLEADR)
{ADR[i++]=0;
TAL[i]=0;
}
}
/* decrit la memoire: */
/* ADR[i]: adresse du tableau i dans le TAS */
/* TAL[i]: taille du tableau i; de ADR[i] a ADR[i] + TAL[i] */
/* TAS: tableau (statique) contenant tous les tableaux (dynamiques) */
void ecrire_memoire(int maxadr, int maxtal, int maxtas)
{int i;
printf("Le tableau ADR:\n");
printf("------------------------:\n");
for(i=0; i < maxadr;i++)
printf("%d:",ADR[i]);
printf("\n");
printf("Le tableau TAL:\n");
printf("------------------------:\n");
for(i=0; i < maxadr;i++)
printf("%d:",TAL[i]);
printf("\n");
printf("Le tableau TAS:\n");
printf("------------------------:\n");
for(i=0; i < maxtas;i++)
printf("%d:",TAS[i]);
printf("\n");
return;
}
/*--------------------------------------------------------------------*/
/*---------------semantique-------------------------------------------*/
/* N.B.allocation dynamique de tableaux; mais pas de ramasse-miettes! */
/* semantique op a grands pas des expressions */
/* fait agir e sur rho_gb, le modifie, retourne val(e) */
int semval(BILENVTY rho_gb,NOE e)
{ if(e != NULL)
{ENVTY pos;
int res,taille;
switch(e->codop)
{
case Ind:
{int tab=semval(rho_gb,e->FG); /* adresse du tableau */
int ind=semval(rho_gb,e->FD); /* index dans le tableau */
return(TAS[ADR[tab]+ind]);
}
case Pl:case Mo:case Mu:case And:case Or:case Lt:case Eq:/* op binaire */
return(eval(e->codop,semval(rho_gb,e->FG),semval(rho_gb,e->FD)));
case Not: /* operation unaire */
return(eval(e->codop,semval(rho_gb,e->FG),0));
case I: /* numeral */
return (atoi(e->ETIQ));
case V: /* variable */
{pos=rechty(e->ETIQ,rho_gb.debut);
return(pos->VAL); /* rho_g(var) */
}
case NewAr: /* creation tableau */
{taille=semval(rho_gb,e->FD); /* taille du tableau */
res=padrl;
ADR[res]=ptasl;
TAL[res]=taille;
padrl++;ptasl+=taille; /* mise a jour allocateur memoire */
return(res);
}
default: return(EXIT_FAILURE); /* codop inconnu au bataillon */
}
}
else
return(EXIT_FAILURE);
}
/* semantique op a grands pas des commandes */
/* fait agir c sur rho_gb, le modifie */
void semop_gp(BILENVTY rho_gb, NOE c)
{char *lhs; int rhs; int cond;
if(c != NULL)
{switch(c->codop)
{case Mp:
semop_gp(rho_gb, c->FG);
break;
case Af:
if (c->FG->codop==V) /* affectation a une variable */
{lhs= c->FG->ETIQ;
printf("lhs vaut %s \n",lhs);
rhs= semval(rho_gb, c->FD);
printf("rhs vaut %d \n",rhs);
affectb(rho_gb, lhs, rhs);
}
else
{assert(c->FG->codop==Ind);/* affectation a un tableau */
int tabl= semval(rho_gb, c->FG->FG);
int index=semval(rho_gb, c->FG->FD);
rhs=semval(rho_gb, c->FD);
TAS[ADR[tabl]+index]=rhs;
/*TODO: tester que index < taille */
}
break;
case Sk: break;
case Se:
semop_gp(rho_gb, c->FG);
semop_gp(rho_gb, c->FD);
break;
case If:
cond= semval(rho_gb, c->FG);
if (cond!=0) /* cas ou cond !=0 */
semop_gp(rho_gb, c->FD->FG);
else /* cas ou cond ==0 */
semop_gp(rho_gb, c->FD->FD);
break;
case Wh:
cond= semval(rho_gb, c->FG);
if (cond != 0) /* on execute seq(corps,c)*/
{semop_gp(rho_gb, c->FD);
semop_gp(rho_gb, c);
}
break;
default: break;
}
};
return;
}