63-struct-cell.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. int
  25. add (int a, int b)
  26. {
  27. return a + b;
  28. }
  29. int
  30. inc (int i)
  31. {
  32. return i + 1;
  33. }
  34. struct scm
  35. {
  36. int type;
  37. int car;
  38. int cdr;
  39. };
  40. int bla = 1234;
  41. char g_arena[84];
  42. #if __MESC__
  43. struct scm *g_cells = g_arena;
  44. #else
  45. struct scm *g_cells = (struct scm *) g_arena;
  46. #endif
  47. char *g_chars = g_arena;
  48. int
  49. foo ()
  50. {
  51. oputs ("t: foo\n");
  52. return 0;
  53. };
  54. int
  55. bar (int i)
  56. {
  57. oputs ("t: bar\n");
  58. return 0;
  59. };
  60. struct function
  61. {
  62. int (*function) (void);
  63. int arity;
  64. //long arity;
  65. char *name;
  66. };
  67. struct function g_fun = { &exit, 1, "fun" };
  68. struct function g_foo = { &foo, 0, "foo" };
  69. struct function g_bar = { &bar, 1, "bar" };
  70. void *functions[2];
  71. //int functions[2];
  72. struct function g_functions[2];
  73. int g_function = 0;
  74. enum type_t
  75. { TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TREF, TSPECIAL, TSTRING,
  76. TSYMBOL, TVALUES, TVECTOR, TBROKEN_HEART };
  77. typedef int SCM;
  78. int g_free = 3;
  79. SCM tmp;
  80. SCM tmp_num;
  81. int ARENA_SIZE = 200;
  82. #define TYPE(x) g_cells[x].type
  83. #define CAR(x) g_cells[x].car
  84. #define CDR(x) g_cells[x].cdr
  85. #define VALUE(x) g_cells[x].cdr
  86. #define CAAR(x) CAR (CAR (x))
  87. struct scm scm_fun = { TFUNCTION, 0, 0 };
  88. SCM cell_fun;
  89. int
  90. main ()
  91. {
  92. oputs ("\n");
  93. oputs ("t: g_cells[0] = g_cells[1]\n");
  94. TYPE (1) = 1;
  95. CAR (1) = 2;
  96. CDR (1) = 3;
  97. g_cells[0] = g_cells[1];
  98. if (TYPE (0) != 1)
  99. return 1;
  100. if (CAR (0) != 2)
  101. return 2;
  102. if (CDR (0) != 3)
  103. return 3;
  104. oputs ("t: g_cells[i] = g_cells[j]\n");
  105. int i = 0;
  106. int j = 1;
  107. TYPE (1) = 4;
  108. CAR (1) = 5;
  109. CDR (1) = 6;
  110. g_cells[i] = g_cells[j];
  111. if (TYPE (0) != 4)
  112. return 4;
  113. if (CAR (0) != 5)
  114. return 5;
  115. if (CDR (0) != 6)
  116. return 6;
  117. oputs ("t: g_cells[0+add(0,0] = g_cells[0+inc(0)]\n");
  118. TYPE (1) = 1;
  119. CAR (1) = 2;
  120. CDR (1) = 3;
  121. g_cells[0 + add (0, 0)] = g_cells[0 + inc (0)];
  122. if (TYPE (0) != 1)
  123. return 7;
  124. if (CAR (0) != 2)
  125. return 9;
  126. if (CDR (0) != 3)
  127. return 9;
  128. g_cells[0].type = TNUMBER;
  129. g_cells[0].car = 0;
  130. g_cells[0].cdr = 0;
  131. g_cells[1].type = TNUMBER;
  132. g_cells[1].car = 0;
  133. g_cells[1].cdr = 0;
  134. oputs ("t: TYPE (0) == TYPE (1)\n");
  135. if (TYPE (0) == TYPE (1))
  136. goto ok;
  137. return 10;
  138. ok:
  139. g_cells[0].car = 1;
  140. g_cells[1].car = 2;
  141. oputs ("t: int c = VALUE (0)\n");
  142. int c = CAR (0);
  143. if (c != 1)
  144. return 11;
  145. oputs ("t: CAAR (0) != 2\n");
  146. if (CAAR (0) != 2)
  147. return 12;
  148. oputs ("t: 2 != CAAR (0)\n");
  149. if (2 != CAAR (0))
  150. return 13;
  151. g_cells[3].type = 0x64;
  152. if (g_cells[3].type != 0x64)
  153. return g_cells[3].type;
  154. TYPE (4) = 4;
  155. if (TYPE (4) != 4)
  156. return 14;
  157. CDR (3) = 0x22;
  158. CDR (4) = 0x23;
  159. if (CDR (3) != 0x22)
  160. return 15;
  161. oputs ("t: g_fun.arity != 1;\n");
  162. if (g_fun.arity != 1)
  163. return 16;
  164. oputs ("t: g_fun.function != exit;\n");
  165. if (g_fun.function != &exit)
  166. return 17;
  167. oputs ("t: struct fun = {&exit,1,\"exit\"};\n");
  168. struct function fun = { &exit, 1, "exit" };
  169. oputs ("t: fun.arity != 1;\n");
  170. if (fun.arity != 1)
  171. return 18;
  172. oputs ("t: fun.function != exit;\n");
  173. if (fun.function != &exit)
  174. return 19;
  175. oputs ("t: oputs (fun.name)\n");
  176. if (strcmp (fun.name, "exit"))
  177. return 20;
  178. oputs ("t: oputs (g_fun.name)\n");
  179. if (strcmp (g_fun.name, "fun"))
  180. return 21;
  181. oputs ("t: g_functions[g_function++] = g_foo;\n");
  182. g_functions[g_function++] = g_foo;
  183. oputs ("t: pbar->arity == 1\n");
  184. struct function *barp = &g_bar;
  185. if (barp->arity != 1)
  186. return 22;
  187. int fn = 0;
  188. oputs ("t: g_functions[g_cells[fn].cdr].arity\n");
  189. if (g_functions[g_cells[fn].cdr].arity)
  190. return 23;
  191. if (g_functions[g_cells[fn].cdr].arity != 0)
  192. return 24;
  193. int (*functionx) (void) = 0;
  194. functionx = g_functions[0].function;
  195. oputs ("t: functionx == foo\n");
  196. if (functionx != foo)
  197. return 25;
  198. oputs ("t: g_functions[0].name\n");
  199. if (strcmp (g_functions[0].name, "foo"))
  200. return 26;
  201. oputs ("t: (functionx) () == foo\n");
  202. if ((functionx) () != 0)
  203. return 27;
  204. oputs ("t: g_functions[<foo>].arity\n");
  205. if (g_functions[0].arity != 0)
  206. return 28;
  207. fn++;
  208. g_functions[fn] = g_bar;
  209. g_cells[fn].cdr = fn;
  210. if (g_cells[fn].cdr != fn)
  211. return 29;
  212. oputs ("t: g_functions[g_cells[fn].cdr].function\n");
  213. functionx = g_functions[g_cells[fn].cdr].function;
  214. oputs ("t: g_functions[1].name\n");
  215. if (strcmp (g_functions[1].name, "bar"))
  216. return 30;
  217. oputs ("t: functionx == bar\n");
  218. if (functionx != bar)
  219. return 31;
  220. oputs ("t: (functiony) (1) == bar\n");
  221. int (*functiony) (int) = 0;
  222. functiony = g_functions[g_cells[fn].cdr].function;
  223. if ((functiony) (1) != 0)
  224. return 32;
  225. oputs ("t: g_functions[<bar>].arity\n");
  226. if (g_functions[fn].arity != 1)
  227. return 33;
  228. // fake name
  229. scm_fun.car = 33;
  230. scm_fun.cdr = g_function;
  231. g_function++;
  232. oputs ("fun");
  233. g_functions[g_function] = g_fun;
  234. cell_fun = g_free++;
  235. g_cells[cell_fun] = scm_fun;
  236. oputs ("t: TYPE (cell_fun)\n");
  237. if (TYPE (cell_fun) != TFUNCTION)
  238. return 34;
  239. oputs ("t: CAR (cell_fun)\n");
  240. if (CAR (cell_fun) != 33)
  241. return 35;
  242. // FIXME!
  243. // oputs ("t: CDR (cell_fun)\n");
  244. // if (CDR (cell_fun) != g_function)
  245. // return 36;
  246. return 0;
  247. }