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.
 
 
 
 

613 lines
12 KiB

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