Nife version Beta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

391 lines
7.7 KiB

  1. /* Copyright (C) 2011-2013 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or any
  5. later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>
  12. *******************************************************************/
  13. #include "conf.h"
  14. /* stackV.c */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <strings.h>
  19. #include "nife.h"
  20. #include "mth.h"
  21. #include "err.h"
  22. #include "lib.h"
  23. #include "stackV.h"
  24. #include "stackF.h"
  25. #include "stackN.h"
  26. #include "stackC.h"
  27. #include "stackL.h"
  28. /* types of var */
  29. #define VT_I 0 /* Initial = INTERGER NULL */
  30. #define VT_B 1 /* BOOLEAN */
  31. #define VT_C 2 /* CHARACTER STRING */
  32. #define VT_N 3 /* NUMBER */
  33. #define VT_L 4 /* LIB FUNCTION */
  34. #define VT_F 5 /* USER FUNCTION */
  35. #define VT_V 9 /* VARIABLE (for "install_v ... in" only) */
  36. #define VT_NO -1 /* cf ci-dessous */
  37. static void * stackV = VIDE;
  38. /* modeExeVar guide le traitement des variables
  39. * si affectation : un des VT_x sinon -1
  40. * *************************************/
  41. static int modeExeVar=VT_NO;
  42. struct Var {
  43. char *l; /* libelle */
  44. void *n; /* next */
  45. union {
  46. bool b;
  47. void *a; /* adresse num/char/code */
  48. };
  49. short t; /* type */
  50. };
  51. static void cpVar(void * S, void *D)
  52. {
  53. struct Var *vS, *vD;
  54. if (S==D) return;
  55. vS = (struct Var *)S;
  56. vD = (struct Var *)D;
  57. vD->t = vS->t;
  58. vD->a = vS->a;
  59. /* duplication if number !! */
  60. if (vD->t == VT_N)
  61. vD->a = duplicateNum(vS->a, 0);
  62. }
  63. void initVar(char *Lib)
  64. {
  65. void * M, *L;
  66. struct Var * N;
  67. if ((M = malloc(sizeof(struct Var))) == NULL) stopErr("initVar","malloc");
  68. if ((L = malloc(strlen(Lib)+1)) == NULL) stopErr("initVar","malloc");
  69. strcpy((char*)L,Lib);
  70. N = (struct Var*)M;
  71. N->l = (char*)L;
  72. N->n = stackV;
  73. N->t = VT_I;
  74. stackV = M;
  75. }
  76. static void setCodeVar(struct Var * Elt, short t, void* A)
  77. {
  78. switch (Elt->t) { /* TODO VT_F */
  79. case VT_C :
  80. free(Elt->a);
  81. break;
  82. case VT_N :
  83. if (nbOnStack(Elt->a) == 0) free(Elt->a);
  84. else numVarOff(Elt->a);
  85. break;
  86. default : /* VT_I, VT_B, VT_L, VT_F */
  87. break;
  88. }
  89. Elt->t = t;
  90. Elt->a = A;
  91. }
  92. static void rmVar(void **Ref, struct Var * Elt)
  93. {
  94. if (Elt->t==VT_N) setCodeVar(Elt, VT_I, VIDE);
  95. *Ref = Elt->n;
  96. free((void*)Elt->l);
  97. free((void*)Elt);
  98. }
  99. void rmLastVar(void)
  100. {
  101. if (stackV != VIDE) rmVar(&stackV, (struct Var *)stackV);
  102. else messErr(23);
  103. }
  104. void IF_show_stackV(void)
  105. {
  106. void * Next;
  107. struct Var * N;
  108. Next = stackV;
  109. while (Next != VIDE) {
  110. N = (struct Var*) Next;
  111. printf(" %-25s : Type ",N->l);
  112. switch(N->t) {
  113. case VT_I :
  114. printf("initial (NULL)");
  115. break;
  116. case VT_B :
  117. printf("Boolean ");
  118. if (N->b) printf("TRUE"); else printf("FALSE");
  119. break;
  120. case VT_C :
  121. printf("String \"%s\"", (char*)N->a);
  122. break;
  123. case VT_N :
  124. printf("Number ");
  125. printNumber(N->a);
  126. break;
  127. case VT_L :
  128. printf("Lib. Fct. %s", libByAddr(N->a));
  129. break;
  130. case VT_F :
  131. printf("User Fct. %s", fctByAddr(N->a));
  132. break;
  133. }
  134. printf("\n");
  135. Next = N->n;
  136. }
  137. printf("<end of variable list>\n");
  138. }
  139. static void newVar(char * S)
  140. {
  141. char Lib[LDFLT+1];
  142. strncpy(Lib,S,LDFLT);
  143. Lib[LDFLT]='\0';
  144. initVar(Lib);
  145. dropTrSuite();
  146. }
  147. void IF_debVar(void)
  148. {
  149. putTrSuite(newVar);
  150. }
  151. void IF_debVarCS(void)
  152. {
  153. char *v;
  154. v = getString();
  155. if (v != NULL) {
  156. initVar(v);
  157. free((void*)v);
  158. }
  159. }
  160. void * varByName(char * L)
  161. {
  162. void * Next;
  163. struct Var * N;
  164. Next = stackV;
  165. while (Next != VIDE) {
  166. N = (struct Var*) Next;
  167. if (strcmp(N->l,L)==0) return(Next);
  168. Next = N->n;
  169. }
  170. return VIDE;
  171. }
  172. char * varByAddr(void * A) /* non optimise mais C'EST FAIT EXPRES !! */
  173. {
  174. void * Next;
  175. struct Var * N;
  176. Next = stackV;
  177. while (Next != VIDE) {
  178. N = (struct Var*) Next;
  179. if (Next==A) return(N->l);
  180. Next = N->n;
  181. }
  182. return NULL;
  183. }
  184. char * varByAddrA(void * A)
  185. {
  186. void * Next;
  187. struct Var * N;
  188. Next = stackV;
  189. while (Next != VIDE) {
  190. N = (struct Var*) Next;
  191. if (N->a==A) return(N->l);
  192. Next = N->n;
  193. }
  194. return NULL;
  195. }
  196. int isVarChar(void * A)
  197. {
  198. struct Var * N;
  199. N = (struct Var*) A;
  200. if (N->t == VT_C) return 1;
  201. return 0;
  202. }
  203. static void exec_Var(void * A)
  204. {
  205. void * C;
  206. struct Var * N;
  207. void (*f) (void);
  208. N = (struct Var*) A;
  209. /* printf("executeVar %s %d !!\n",N->l, N->t);*/
  210. switch(N->t) {
  211. case VT_B :
  212. putBool(N->b);
  213. break;
  214. case VT_C :
  215. putString((char*)N->a);
  216. break;
  217. case VT_N :
  218. if (nbOnStack(N->a) > 0) {
  219. C = duplicateNum(N->a,1);
  220. N->a = C;
  221. }
  222. putVar(N->a);
  223. break;
  224. case VT_L :
  225. f = (PFV)(N->a);
  226. f();
  227. break;
  228. case VT_F :
  229. execFctV(N->a);
  230. break;
  231. default : /* VT_I */
  232. break;
  233. }
  234. }
  235. static void delVar(char * L)
  236. {
  237. void ** PNext;
  238. struct Var * N;
  239. dropTrSuite();
  240. PNext = &stackV;
  241. while (*PNext != VIDE) {
  242. N = (struct Var*) *PNext;
  243. if (strcmp(N->l,L)==0) {
  244. rmVar(PNext, N);
  245. return;
  246. }
  247. PNext = &N->n;
  248. }
  249. messErr(24);
  250. }
  251. void IF_delVar(void)
  252. {
  253. putTrSuite(delVar);
  254. }
  255. void putInVar(void * A, short t)
  256. {
  257. struct Var * N;
  258. N = (struct Var*) A;
  259. switch(t) {
  260. case VT_B :
  261. setCodeVar(N, t, VIDE);
  262. N->b = getBool();
  263. break;
  264. case VT_C :
  265. setCodeVar(N, t, getString());
  266. break;
  267. case VT_N :
  268. setCodeVar(N, t, getVar());
  269. break;
  270. case VT_L :
  271. case VT_F :
  272. setCodeVar(N, t, FCT_INST);
  273. break;
  274. default :
  275. setCodeVar(N, VT_I, VIDE);
  276. break;
  277. }
  278. }
  279. /* PLUS UTILISEE ***********************
  280. static void updateVar(char * L, short t)
  281. {
  282. void * Next;
  283. struct Var * N;
  284. Next = stackV;
  285. while (Next != VIDE) {
  286. N = (struct Var*) Next;
  287. if (strcmp(N->l,L)==0) {
  288. putInVar(Next, t);
  289. return;
  290. }
  291. Next = N->n;
  292. }
  293. messErr(24);
  294. }
  295. *****************/
  296. void IF_setVarN(void)
  297. {
  298. modeExeVar=VT_N;
  299. }
  300. void IF_setVarC(void)
  301. {
  302. modeExeVar=VT_C;
  303. }
  304. void IF_setVarB(void)
  305. {
  306. modeExeVar=VT_B;
  307. }
  308. void IF_setVarI(void)
  309. {
  310. modeExeVar=VT_I;
  311. }
  312. void IF_setVarLF(void)
  313. {
  314. switch (FCT_TYP) {
  315. case 0 :
  316. modeExeVar=VT_I;
  317. break;
  318. case 1 :
  319. modeExeVar=VT_L;
  320. break;
  321. case 2 :
  322. modeExeVar=VT_F;
  323. break;
  324. case 3 :
  325. modeExeVar=VT_V;
  326. break;
  327. }
  328. }
  329. void executeVar(void * A)
  330. {
  331. if (fctEnCours) makeFct(T_VAR, A);
  332. else {
  333. if (modeExeVar == VT_NO) exec_Var(A);
  334. else {
  335. if (modeExeVar == VT_V) cpVar(FCT_INST,A);
  336. else putInVar(A, modeExeVar);
  337. modeExeVar=VT_NO;
  338. }
  339. }
  340. }
  341. int IF_execVar(char * L)
  342. {
  343. void * Next;
  344. struct Var * N;
  345. Next = stackV;
  346. while (Next != VIDE) {
  347. N = (struct Var*) Next;
  348. if (strcmp(N->l,L)==0) {
  349. executeVar(Next);
  350. return 1;
  351. }
  352. Next = N->n;
  353. }
  354. return 0;
  355. }