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.
 
 
 
 

605 lines
12 KiB

  1. /* Copyright (C) 2011-2014 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 <unistd.h>
  18. #include <string.h>
  19. #include <strings.h>
  20. #include "nife.h"
  21. #include "mth.h"
  22. #include "err.h"
  23. #include "lib.h"
  24. #include "stackV.h"
  25. #include "stackF.h"
  26. #include "stackN.h"
  27. #include "stackC.h"
  28. #include "stackL.h"
  29. /* types of var */
  30. #define VT_I 0 /* Initial = INTERGER NULL */
  31. #define VT_B 1 /* BOOLEAN */
  32. #define VT_C 2 /* CHARACTER STRING */
  33. #define VT_N 3 /* NUMBER */
  34. #define VT_L 4 /* LIB FUNCTION */
  35. #define VT_F 5 /* USER FUNCTION */
  36. #define VT_XF 15 /* USER FUNCTION NOT IDENTIFIED during restore */
  37. #define VT_V 9 /* VARIABLE (for "install_v ... in" only) */
  38. #define VT_NO -1 /* cf ci-dessous */
  39. static void * stackV = VIDE;
  40. /* modeExeVar guide le traitement des variables
  41. * si affectation : un des VT_x sinon -1
  42. * *************************************/
  43. static int modeExeVar=VT_NO;
  44. struct Var {
  45. char *l; /* libelle */
  46. void *n; /* next */
  47. union {
  48. bool b;
  49. void *a; /* adresse num/char/code */
  50. };
  51. short t; /* type */
  52. };
  53. static void cpVar(void * S, void *D)
  54. {
  55. struct Var *vS, *vD;
  56. if (S==D) return;
  57. vS = (struct Var *)S;
  58. vD = (struct Var *)D;
  59. vD->t = vS->t;
  60. vD->a = vS->a;
  61. /* duplication if number !! */
  62. if (vD->t == VT_N)
  63. vD->a = duplicateNum(vS->a, 0);
  64. }
  65. void initVarSL(char *L)
  66. {
  67. void * M;
  68. struct Var * N;
  69. if ((M = malloc(sizeof(struct Var))) == NULL) stopErr("initVarSL","malloc");
  70. N = (struct Var*)M;
  71. N->l = L;
  72. N->n = stackV;
  73. N->t = VT_I;
  74. stackV = M;
  75. }
  76. void initVar(char *Lib)
  77. {
  78. void *L;
  79. if ((L = malloc(strlen(Lib)+1)) == NULL) stopErr("initVar","malloc");
  80. strcpy((char*)L,Lib);
  81. initVarSL((char*)L);
  82. }
  83. static void setCodeVar(struct Var * Elt, short t, void* A)
  84. {
  85. switch (Elt->t) { /* TODO VT_F */
  86. case VT_C :
  87. free(Elt->a);
  88. break;
  89. case VT_N :
  90. if (nbOnStack(Elt->a) == 0) free(Elt->a);
  91. else numVarOff(Elt->a);
  92. break;
  93. default : /* VT_I, VT_B, VT_L, VT_F */
  94. break;
  95. }
  96. Elt->t = t;
  97. Elt->a = A;
  98. }
  99. static void rmVar(void **Ref, struct Var * Elt)
  100. {
  101. if (Elt->t==VT_N) setCodeVar(Elt, VT_I, VIDE);
  102. *Ref = Elt->n;
  103. free((void*)Elt->l);
  104. free((void*)Elt);
  105. }
  106. void rmLastVar(void)
  107. {
  108. if (stackV != VIDE) rmVar(&stackV, (struct Var *)stackV);
  109. else messErr(23);
  110. }
  111. void IF_show_stackV(void)
  112. {
  113. void * Next;
  114. struct Var * N;
  115. Next = stackV;
  116. while (Next != VIDE) {
  117. N = (struct Var*) Next;
  118. printf(" %-25s : Type ",N->l);
  119. switch(N->t) {
  120. case VT_I :
  121. printf("initial (NULL)");
  122. break;
  123. case VT_B :
  124. printf("Boolean ");
  125. if (N->b) printf("TRUE"); else printf("FALSE");
  126. break;
  127. case VT_C :
  128. printf("String \"%s\"", (char*)N->a);
  129. break;
  130. case VT_N :
  131. printf("Number ");
  132. printNumber(N->a);
  133. break;
  134. case VT_L :
  135. printf("Lib. Fct. %s", libByAddr(N->a));
  136. break;
  137. case VT_F :
  138. printf("User Fct. %s", fctByAddr(N->a));
  139. break;
  140. default :
  141. printf("Undefined (%d) !??", N->t);
  142. }
  143. printf("\n");
  144. Next = N->n;
  145. }
  146. printf("<end of variables stack>\n");
  147. }
  148. static void newVar(char * S)
  149. {
  150. char Lib[LDFLT+1];
  151. strncpy(Lib,S,LDFLT);
  152. Lib[LDFLT]='\0';
  153. initVar(Lib);
  154. dropTrSuite();
  155. }
  156. void IF_debVar(void)
  157. {
  158. putTrSuite(newVar);
  159. }
  160. void IF_debVarCS(void)
  161. {
  162. char *v;
  163. v = getString();
  164. if (v != NULL) {
  165. initVar(v);
  166. free((void*)v);
  167. }
  168. }
  169. void * varByName(char * L)
  170. {
  171. void * Next;
  172. struct Var * N;
  173. Next = stackV;
  174. while (Next != VIDE) {
  175. N = (struct Var*) Next;
  176. if (strcmp(N->l,L)==0) return(Next);
  177. Next = N->n;
  178. }
  179. return VIDE;
  180. }
  181. char * varByAddr(void * A) /* non optimise mais C'EST FAIT EXPRES !! */
  182. {
  183. void * Next;
  184. struct Var * N;
  185. Next = stackV;
  186. while (Next != VIDE) {
  187. N = (struct Var*) Next;
  188. if (Next==A) return(N->l);
  189. Next = N->n;
  190. }
  191. return NULL;
  192. }
  193. char * varByAddrA(void * A)
  194. {
  195. void * Next;
  196. struct Var * N;
  197. Next = stackV;
  198. while (Next != VIDE) {
  199. N = (struct Var*) Next;
  200. if (N->a==A) return(N->l);
  201. Next = N->n;
  202. }
  203. return NULL;
  204. }
  205. long iVarByAddr(void * A)
  206. {
  207. void * Next;
  208. struct Var * N;
  209. long i=0;
  210. Next = stackV;
  211. while (Next != VIDE) {
  212. i++;
  213. if (Next==A) return(i);
  214. N = (struct Var*) Next;
  215. Next = N->n;
  216. }
  217. return 0L;
  218. }
  219. long iVarByAddrA(void * A)
  220. {
  221. void * Next;
  222. struct Var * N;
  223. long i=0;
  224. Next = stackV;
  225. while (Next != VIDE) {
  226. i++;
  227. N = (struct Var*) Next;
  228. if (N->a==A) return(i);
  229. Next = N->n;
  230. }
  231. return 0L;
  232. }
  233. void * varAddrByInd(long i)
  234. {
  235. void * Next;
  236. struct Var * N;
  237. long j=0;
  238. Next = stackV;
  239. while (Next != VIDE) {
  240. j++;
  241. if (i==j) return(Next);
  242. N = (struct Var*) Next;
  243. Next = N->n;
  244. }
  245. return NULL;
  246. }
  247. void * varAddrAByInd(long i)
  248. {
  249. void * Next;
  250. struct Var * N;
  251. long j=0;
  252. Next = stackV;
  253. while (Next != VIDE) {
  254. j++;
  255. N = (struct Var*) Next;
  256. if (i==j) return(N->a);
  257. Next = N->n;
  258. }
  259. return NULL;
  260. }
  261. int isVarChar(void * A)
  262. {
  263. struct Var * N;
  264. N = (struct Var*) A;
  265. if (N->t == VT_C) return 1;
  266. return 0;
  267. }
  268. static void exec_Var(void * A)
  269. {
  270. void * C;
  271. struct Var * N;
  272. void (*f) (void);
  273. N = (struct Var*) A;
  274. /* printf("executeVar %s %d !!\n",N->l, N->t);*/
  275. switch(N->t) {
  276. case VT_B :
  277. putBool(N->b);
  278. break;
  279. case VT_C :
  280. putString((char*)N->a);
  281. break;
  282. case VT_N :
  283. if (nbOnStack(N->a) > 0) {
  284. C = duplicateNum(N->a,1);
  285. N->a = C;
  286. }
  287. putVar(N->a);
  288. break;
  289. case VT_L :
  290. f = (PFV)(N->a);
  291. f();
  292. break;
  293. case VT_F :
  294. execFctV(N->a);
  295. break;
  296. default : /* VT_I */
  297. break;
  298. }
  299. }
  300. static void delVar(char * L)
  301. {
  302. void ** PNext;
  303. struct Var * N;
  304. dropTrSuite();
  305. PNext = &stackV;
  306. while (*PNext != VIDE) {
  307. N = (struct Var*) *PNext;
  308. if (strcmp(N->l,L)==0) {
  309. rmVar(PNext, N);
  310. return;
  311. }
  312. PNext = &N->n;
  313. }
  314. messErr(24);
  315. }
  316. void IF_delVar(void)
  317. {
  318. putTrSuite(delVar);
  319. }
  320. void putInVar(void * A, short t)
  321. {
  322. struct Var * N;
  323. N = (struct Var*) A;
  324. switch(t) {
  325. case VT_B :
  326. setCodeVar(N, t, VIDE);
  327. N->b = getBool();
  328. break;
  329. case VT_C :
  330. setCodeVar(N, t, getString());
  331. break;
  332. case VT_N :
  333. setCodeVar(N, t, getVar());
  334. break;
  335. case VT_L :
  336. case VT_F :
  337. setCodeVar(N, t, FCT_INST);
  338. break;
  339. default :
  340. setCodeVar(N, VT_I, VIDE);
  341. break;
  342. }
  343. }
  344. /* PLUS UTILISEE ***********************
  345. static void updateVar(char * L, short t)
  346. {
  347. void * Next;
  348. struct Var * N;
  349. Next = stackV;
  350. while (Next != VIDE) {
  351. N = (struct Var*) Next;
  352. if (strcmp(N->l,L)==0) {
  353. putInVar(Next, t);
  354. return;
  355. }
  356. Next = N->n;
  357. }
  358. messErr(24);
  359. }
  360. *****************/
  361. void IF_setVarN(void)
  362. {
  363. modeExeVar=VT_N;
  364. }
  365. void IF_setVarC(void)
  366. {
  367. modeExeVar=VT_C;
  368. }
  369. void IF_setVarB(void)
  370. {
  371. modeExeVar=VT_B;
  372. }
  373. void IF_setVarI(void)
  374. {
  375. modeExeVar=VT_I;
  376. }
  377. void IF_setVarLF(void)
  378. {
  379. switch (FCT_TYP) {
  380. case 0 :
  381. modeExeVar=VT_I;
  382. break;
  383. case 1 :
  384. modeExeVar=VT_L;
  385. break;
  386. case 2 :
  387. modeExeVar=VT_F;
  388. break;
  389. case 3 :
  390. modeExeVar=VT_V;
  391. break;
  392. }
  393. }
  394. void executeVar(void * A)
  395. {
  396. if (fctEnCours) makeFct(T_VAR, A);
  397. else {
  398. if (modeExeVar == VT_NO) exec_Var(A);
  399. else {
  400. if (modeExeVar == VT_V) cpVar(FCT_INST,A);
  401. else putInVar(A, modeExeVar);
  402. modeExeVar=VT_NO;
  403. }
  404. }
  405. }
  406. int IF_execVar(char * L)
  407. {
  408. void * Next;
  409. struct Var * N;
  410. Next = stackV;
  411. while (Next != VIDE) {
  412. N = (struct Var*) Next;
  413. if (strcmp(N->l,L)==0) {
  414. executeVar(Next);
  415. return 1;
  416. }
  417. Next = N->n;
  418. }
  419. return 0;
  420. }
  421. void dump_eltV(int fd, void *A)
  422. {
  423. struct Var * N;
  424. uint32_t a;
  425. int nc;
  426. N = (struct Var*)A;
  427. nc = write(fd, (void*)&(N->t), sizeof(N->t));
  428. dump_eltC(fd, N->l);
  429. switch(N->t) {
  430. case VT_I :
  431. break;
  432. case VT_B :
  433. nc = write(fd, (void*)&(N->b), sizeof(N->b));
  434. break;
  435. case VT_C :
  436. dump_eltC(fd, (char*)N->a);
  437. break;
  438. case VT_N :
  439. dump_eltN(fd, N->a, 0);
  440. break;
  441. case VT_L :
  442. a = iLibByAddr(N->a);
  443. nc = write(fd, (void*)&a, sizeof(a));
  444. break;
  445. case VT_F :
  446. a = iFctByAddr(N->a);
  447. nc = write(fd, (void*)&a, sizeof(a));
  448. break;
  449. default :
  450. printf("Var type %d !?\n", N->t);
  451. break;
  452. }
  453. }
  454. void dump_stackV(int fd)
  455. {
  456. void * Next, *A;
  457. struct Var * N;
  458. uint32_t n=0;
  459. long l, i, j;
  460. int nc;
  461. Next = stackV;
  462. while (Next != VIDE) {
  463. N = (struct Var*) Next;
  464. n++;
  465. Next = N->n;
  466. }
  467. nc = write(fd, (void*)&n, sizeof(n));
  468. for (i=n; i>0; i--) {
  469. Next = stackV;
  470. j=0;
  471. while (Next != VIDE) {
  472. N = (struct Var*) Next;
  473. j++;
  474. if (i==j) break;
  475. Next = N->n;
  476. }
  477. dump_eltV(fd, Next);
  478. }
  479. dump_rest_pr(0,n,"variables");
  480. }
  481. void restore_links_stackV(void)
  482. {
  483. void * Next;
  484. struct Var * N;
  485. long i, j=0;
  486. Next = stackV;
  487. while (Next != VIDE) {
  488. N = (struct Var*) Next;
  489. if (N->t == VT_XF) {
  490. i = (long)(N->a);
  491. N->a = fctByInd(i);
  492. N->t = VT_F;
  493. j++;
  494. }
  495. Next = N->n;
  496. }
  497. rest_links_pr(j, "user function", "variables");
  498. }
  499. static int NbARIL;
  500. void restore_eltV(int fd)
  501. {
  502. struct Var * N;
  503. short t;
  504. char *L;
  505. void *A;
  506. bool b;
  507. uint32_t i;
  508. long Vi;
  509. int nc;
  510. if (read(fd, (void*)&t, sizeof(t)) != sizeof(t)) return;
  511. L = restore_eltC(fd);
  512. initVarSL(L);
  513. /* printf("Var %s %d\n", L, t); */
  514. N = (struct Var *)stackV;
  515. switch(t) {
  516. case VT_I :
  517. A = VIDE;
  518. break;
  519. case VT_B :
  520. A = VIDE;
  521. nc = read(fd, (void*)&b, sizeof(b));
  522. break;
  523. case VT_C :
  524. A = (void*) restore_eltC(fd);
  525. break;
  526. case VT_N :
  527. A = restore_eltN(fd);
  528. break;
  529. case VT_L :
  530. nc = read(fd, (void*)&i, sizeof(i));
  531. A = libByInd(i);
  532. NbARIL++;
  533. break;
  534. case VT_F :
  535. nc = read(fd, (void*)&i, sizeof(i));
  536. /* A = fctByInd(i); not possible ! */
  537. Vi = (long)i;
  538. A = (void*)Vi;
  539. t = VT_XF;
  540. break;
  541. default :
  542. printf("Var type %d !?\n", N->t);
  543. break;
  544. }
  545. setCodeVar(N, t, A);
  546. if (t == VT_B) N->b = b;
  547. }
  548. void restore_stackV(int fd)
  549. {
  550. uint32_t n=0, i;
  551. if (read(fd, (void*)&n, sizeof(n)) != sizeof(n)) return;
  552. NbARIL=0;
  553. while (stackV != VIDE) rmVar(&stackV, (struct Var *)stackV);
  554. for (i=0; i<n; i++) {
  555. restore_eltV(fd);
  556. }
  557. dump_rest_pr(1,n,"variables");
  558. rest_links_pr(NbARIL, "library function", "variables");
  559. }