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.
 
 
 
 

240 lines
4.3 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. /* stackL.c */
  14. #include "conf.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include "nife.h"
  18. #include "mth.h"
  19. #include "err.h"
  20. #include "debug.h"
  21. #include "stackL.h"
  22. void IF_stackL_clear(void)
  23. {
  24. _MODIF_i_StackL_(0);
  25. }
  26. void putBool(bool B)
  27. {
  28. int i;
  29. i=i_StackL;
  30. stackL[i++] = B;
  31. _MODIF_i_StackL_(i);
  32. if (i == LSTACKL) stopErr("putBool",NULL);
  33. }
  34. void IF_dupL(void)
  35. {
  36. int i;
  37. i=i_StackL;
  38. if (i) {
  39. putBool(stackL[i-1]);
  40. } else messErr(5);
  41. }
  42. void IF_swapL(void)
  43. {
  44. bool B;
  45. int i;
  46. i=i_StackL;
  47. if (i > 1) {
  48. B = stackL[i-1];
  49. stackL[i-1] = stackL[i-2];
  50. stackL[i-2] = B;
  51. } else messErr(20);
  52. }
  53. void IF_andL(void)
  54. {
  55. bool B;
  56. int i;
  57. i=i_StackL;
  58. if (i > 1) {
  59. B = stackL[i-1] & stackL[i-2];
  60. stackL[i-2] = B;
  61. i--;
  62. _MODIF_i_StackL_(i);
  63. } else messErr(20);
  64. }
  65. void IF_orL(void)
  66. {
  67. bool B;
  68. int i;
  69. i=i_StackL;
  70. if (i > 1) {
  71. B = stackL[i-1] | stackL[i-2];
  72. stackL[i-2] = B;
  73. i--;
  74. _MODIF_i_StackL_(i);
  75. } else messErr(20);
  76. }
  77. void IF_xorL(void)
  78. {
  79. bool B;
  80. int i;
  81. i=i_StackL;
  82. if (i> 1) {
  83. if (stackL[i-1] == stackL[i-2]) B=FALSE;
  84. else B = TRUE;
  85. stackL[i-2] = B;
  86. i--;
  87. _MODIF_i_StackL_(i);
  88. } else messErr(20);
  89. }
  90. void IF_overL(void)
  91. {
  92. int i;
  93. i=i_StackL;
  94. if (i > 1) {
  95. putBool(stackL[i-2]);
  96. } else messErr(20);
  97. }
  98. bool getBool(void)
  99. {
  100. int i;
  101. i=i_StackL;
  102. if (i) {
  103. i--;
  104. _MODIF_i_StackL_(i);
  105. return(stackL[i]);
  106. } else messErr(5);
  107. return -1;
  108. }
  109. void IF_typeL(void)
  110. {
  111. int i;
  112. i=i_StackL;
  113. if (i) {
  114. i--;
  115. _MODIF_i_StackL_(i);
  116. if(stackL[i]) printf("true\n");
  117. else printf("false\n");
  118. } else messErr(5);
  119. }
  120. void negBool(void)
  121. {
  122. int i;
  123. i=i_StackL;
  124. if (i) {
  125. i--;
  126. if(stackL[i]) stackL[i]= FALSE;
  127. else stackL[i]= TRUE;
  128. } else messErr(5);
  129. }
  130. void IF_true(void)
  131. {
  132. putBool(TRUE);
  133. }
  134. void IF_false(void)
  135. {
  136. putBool(FALSE);
  137. }
  138. void IF_dropL(void)
  139. {
  140. getBool();
  141. }
  142. void IF_show_stackL(void)
  143. {
  144. int i,j=0,I;
  145. char s;
  146. I=i_StackL;
  147. for(i=I-1;i>=0;i--) {
  148. if (stackL[i]) printf(" TRUE");
  149. else printf(" FALSE");
  150. if (j==0) printf(" <- top");
  151. printf("\n");
  152. j++;
  153. if (j==NBLIG) break;
  154. }
  155. if (i>0) {
  156. if (i==1) s=' ';
  157. else s='s';
  158. printf(" ... and %d other%c boolean%c !\n",I-NBLIG,s,s);
  159. } else printf("<end of logical stack>\n");
  160. }
  161. void IFD_show_stackL(void)
  162. {
  163. _IFD_BEGIN_
  164. IF_show_stackL();
  165. _IFD_END_
  166. }
  167. void dump_stackL(int fd)
  168. {
  169. uint32_t n;
  170. n = i_StackL;
  171. write(fd, (void*)&n, sizeof(n));
  172. if (n) write(fd, (void*)stackL, n);
  173. dump_rest_pr(0,n,"logical");
  174. }
  175. void restore_stackL(int fd)
  176. {
  177. uint32_t n=0;
  178. if (read(fd, (void*)&n, sizeof(n)) != sizeof(n)) return;
  179. IF_stackL_clear();
  180. if (n) {
  181. read(fd, (void*)stackL, n);
  182. _MODIF_i_StackL_(n);
  183. }
  184. dump_rest_pr(1,n,"logical");
  185. }
  186. /* gestion des meta-stacks */
  187. void IF_new_stackL(void)
  188. {
  189. if (G_i_TStackL == LSTACKS) {
  190. messErr(60); return;
  191. }
  192. G_TiStackL[G_i_TStackL] = i_StackL;
  193. G_TStackL[G_i_TStackL++] = stackL;
  194. stackL = G_TStackL[G_i_TStackL];
  195. i_StackL = G_TiStackL[G_i_TStackL];
  196. if (stackL == (bool *)0) {
  197. if ((stackL = (bool*)malloc(sizeof(bool*)*LSTACKL)) == NULL)
  198. stopErr("IF_new_stackL","malloc");
  199. i_StackL=0;
  200. }
  201. }
  202. void IF_old_stackL(void)
  203. {
  204. if (G_i_TStackL == 0) {
  205. messErr(61); return;
  206. }
  207. G_TiStackL[G_i_TStackL] = i_StackL;
  208. G_TStackL[G_i_TStackL--] = stackL;
  209. stackL = G_TStackL[G_i_TStackL];
  210. i_StackL = G_TiStackL[G_i_TStackL];
  211. }