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.
 
 
 
 

209 lines
3.6 KiB

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