00001
00008 #include"table.h"
00009 #include<stdio.h>
00010 #include<stdlib.h>
00011 #include<string.h>
00012 #include"erreur.h"
00013
00015 static table_t t=NULL;
00017 static int mem;
00019 static int mem_pret=0;
00020
00021
00029 static int recupereArgument(const char *arg, int *r);
00030
00037 static char* split (char* s, char c);
00038
00049 static int op_f(const char *arg, int *r, int(*op)(int,int));
00050
00056 static int print(const char *arg);
00057
00065 static int add_f(const char *arg, int *r);
00066
00074 static int mult_f(const char *arg, int *r);
00075
00083 static int sub_f(const char *arg, int *r);
00084
00092 static int quo_f(const char *arg, int *r);
00093
00101 static int mod_f(const char *arg, int *r);
00102
00103
00104 int recupereArgument(const char *arg, int *r){
00105 char *p;
00106
00107 *r=(int)strtol(arg, &p, 0);
00108 if(*p=='\0')
00109 return 1;
00110
00111 if(strcmp(arg,"$")==0){
00112 if(!mem_pret){
00113 num_erreur=E_INIT;
00114 return 0;
00115 }
00116 *r=mem;
00117 return 1;
00118 }
00119
00120 if(!t){
00121 num_erreur=E_INIT;
00122 return 0;
00123 }
00124 return find(t,arg,r);
00125 }
00126
00127 char* split (char* s, char c){
00128 char* p=strchr(s,c);
00129 if(!p) return NULL;
00130 *p='\0';
00131 return p+1;
00132 }
00133
00134 int op_f(const char *arg, int *r, int(*op)(int,int)){
00135 int x,y;
00136 char s[200];
00137 char *p;
00138
00139 strcpy(s,arg);
00140
00141 p=split(s,' ');
00142 if(!p){
00143 num_erreur=E_SYNTAXE;
00144 return 0;
00145 }
00146
00147 if( !recupereArgument(s, &x) || !recupereArgument(p, &y))
00148 return 0;
00149 mem=*r=op(x,y);
00150 mem_pret=1;
00151 return 1;
00152 }
00153
00154
00155
00156
00157
00158
00159 #define DEFOP(NOM,OP) \
00160 static int NOM(int x, int y){ \
00161 return x OP y; \
00162 } \
00163 \
00164 int NOM##_f(const char *arg, int *r){ \
00165 return op_f(arg,r,NOM); \
00166 }
00167
00168
00169 DEFOP(add,+)
00170 DEFOP(sub,-)
00171 DEFOP(mult,*)
00172 DEFOP(quo,/)
00173 DEFOP(mod,%)
00174
00175 #undef DEFOP
00176
00177 int print(const char *arg){
00178 int x;
00179 if( !recupereArgument(arg, &x)) return 0;
00180 printf("--> %d\n",x);
00181 return 1;
00182 }
00183
00184 int traiteConstante(const char* ligne){
00185 char s[200];
00186 char *p;
00187 int x;
00188
00189 strcpy(s,ligne);
00190
00191 p=split(s,'=');
00192 if(!p){
00193 num_erreur=E_SYNTAXE;
00194 return 0;
00195 }
00196
00197 if(!recupereArgument(p,&x)){
00198 return 0;
00199 }
00200
00201 if(!t)
00202 t=init_table();
00203 return add_symbole(t,s,x);
00204 }
00205
00206 int traiteInstruction(const char* ligne){
00207 char s[200];
00208 char *p;
00209 static char* com[]= {"add", "sub", "mult", "div", "mod"};
00210 static int (*fct[])(const char*, int*)={add_f, sub_f, mult_f, quo_f, mod_f};
00211 int i;
00212
00213 strcpy(s,ligne);
00214
00215 p=split(s,' ');
00216 if(!p){
00217 num_erreur=E_SYNTAXE;
00218 return 0;
00219 }
00220
00221 for(i=0;i<5;i++)
00222 if(strcmp(s,com[i])==0) return fct[i](p, &mem);
00223 if(strcmp(s,"print")==0) return print(p);
00224
00225 num_erreur=E_UNSUP;
00226 return 0;
00227 }
00228
00229 void end(){
00230 free_table(t);
00231 t=NULL;
00232 mem_pret=0;
00233 }