1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
5 * This file is part of Mes.
7 * 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.
12 * 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.
17 * You should have received a copy of the GNU General Public License
18 * along with Mes. If not, see <http://www.gnu.org/licenses/>.
24 #define assert(x) ((x) ? (void)0 : assert_fail (#x))
34 struct scm *g_cells = arena;
35 char *g_chars = arena;
37 int foo () {puts ("t: foo\n"); return 0;};
38 int bar (int i) {puts ("t: bar\n"); return 0;};
40 int (*function) (void);
44 struct function g_fun = {&exit,1,"fun"};
45 struct function g_foo = {&foo,0,"foo"};
46 struct function g_bar = {&bar,1,"bar"};
51 struct function g_functions[2];
54 enum type_t {TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TREF, TSPECIAL, TSTRING, TSYMBOL, TVALUES, TVECTOR, TBROKEN_HEART};
62 #define TYPE(x) (g_cells[x].type)
63 #define CAR(x) g_cells[x].car
64 #define CDR(x) g_cells[x].cdr
65 #define VALUE(x) g_cells[x].cdr
67 #define CAAR(x) CAR (CAR (x))
69 struct scm scm_fun = {TFUNCTION,0,0};
108 case TCHAR: {goto next;}
111 default: {goto next;}
150 puts ("read test\n");
151 char *p = (char*)g_chars;
153 puts ("t: read 0123456789\n");
162 if (strcmp (g_chars, "0123456789")) return 1;
188 make_cell (SCM type, SCM car, SCM cdr)
191 TYPE (x) = VALUE (type);
192 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
193 if (car) CAR (x) = CAR (car);
194 if (cdr) CDR(x) = CDR(cdr);
196 else if (VALUE (type) == TFUNCTION) {
197 if (car) CAR (x) = car;
198 if (cdr) CDR(x) = CDR(cdr);
210 VALUE (tmp_num) = TPAIR;
211 make_cell (tmp_num, 0, 1);
216 make_tmps_test (struct scm* cells)
218 puts ("t: tmp = g_free++\n");
220 puts ("t: cells[tmp].type = CHAR\n");
221 cells[tmp].type = TCHAR;
223 cells[tmp_num].type = TNUMBER;
225 return make_cell_test();
234 puts ("t: int c = VALUE (0)\n");
236 if (c != 1) return 1;
238 puts ("t: CAAR (0) != 2\n");
239 if (CAAR (0) != 2) return 1;
241 puts ("t: 2 != CAAR (0)\n");
242 if (2 != CAAR (0)) return 1;
244 g_cells[3].type = 0x64;
245 if (g_cells[3].type != 0x64)
246 return g_cells[3].type;
257 puts ("t: g_fun.arity != 1;\n");
258 if (g_fun.arity != 1) return 1;
260 puts ("t: g_fun.function != exit;\n");
261 if (g_fun.function != &exit) return 1;
263 puts ("t: struct fun = {&exit,1,\"exit\"};\n");
264 struct function fun = {&exit,1,"exit"};
266 puts ("t: fun.arity != 1;\n");
267 if (fun.arity != 1) return 1;
269 puts ("t: fun.function != exit;\n");
270 if (fun.function != &exit) return 1;
272 puts ("t: puts (fun.name)\n");
273 if (strcmp (fun.name, "exit")) return 1;
275 puts ("t: puts (g_fun.name)\n");
276 if (strcmp (g_fun.name, "fun")) return 1;
278 puts ("t: g_functions[g_function++] = g_foo;\n");
279 g_functions[g_function++] = g_foo;
282 puts ("t: g_functions[g_cells[fn].cdr].arity\n");
283 if (g_functions[g_cells[fn].cdr].arity) return 1;
284 if (g_functions[g_cells[fn].cdr].arity != 0) return 1;
286 int (*functionx) (void) = 0;
287 functionx = g_functions[0].function;
288 puts ("t: functionx == foo\n");
289 if (functionx != foo) return 11;
291 puts ("t: g_functions[0].name\n");
292 if (strcmp (g_functions[0].name, "foo")) return 1;
294 puts ("t: (functionx) () == foo\n");
295 if ((functionx) () != 0) return 12;
297 puts ("t: g_functions[<foo>].arity\n");
298 if (g_functions[0].arity != 0) return 17;
301 g_functions[fn] = g_bar;
302 g_cells[fn].cdr = fn;
303 if (g_cells[fn].cdr != fn) return 13;
305 puts ("t: g_functions[g_cells[fn].cdr].function\n");
306 functionx = g_functions[g_cells[fn].cdr].function;
308 puts ("t: g_functions[1].name\n");
309 if (strcmp (g_functions[1].name, "bar")) return 1;
311 puts ("t: functionx == bar\n");
312 if (functionx != bar) return 15;
314 puts ("t: (functiony) (1) == bar\n");
315 int (*functiony) (int) = 0;
316 functiony = g_functions[g_cells[fn].cdr].function;
317 if ((functiony) (1) != 0) return 16;
319 puts ("t: g_functions[<bar>].arity;");
320 if (g_functions[fn].arity != 1) return 18;
322 scm_fun.cdr = g_function;
323 g_functions[g_function++] = g_fun;
325 g_cells[cell_fun] = scm_fun;
327 return make_tmps_test (g_cells);
342 puts ("t: for (i=1; i<5; ++i)\n");
344 if (i != 5) return i;
346 puts ("t: while (i<3) i++\n");
349 if (i != 3) return i;
351 puts ("t: do i-- while (i>0)\n");
353 if (i != 0) return 1;
355 puts ("t: if (0)\n");
361 puts ("t: else 1\n");
368 puts ("t: else if 2\n");
370 puts ("t: if (f)\n");
373 puts ("t: if (one != 1)\n");
374 if (one != 1) return 1;
376 puts ("t: if (1 != one)\n");
377 if (1 != one) return 1;
379 puts ("t: if (one > 1)\n");
380 if (one > 1) return 1;
382 puts ("t: if (one < 0)\n");
383 if (one < 0) return 1;
385 puts ("t: if (strlen (\"\"))\n");
386 if (strlen ("")) return 1;
388 puts ("t: if (strlen (p) != 4)\n");
389 if (strlen (p) != 4) return 1;
391 puts ("t: if (!strlen (\".\"))\n");
392 if (!strlen (".")) return 1;
394 puts ("t: if (strcmp (p, \"foo\"))\n");
395 if (!strcmp (p, "foo")) return 1;
397 puts ("t: if (strcmp (p, \"t.c\\n\"))\n");
398 if (strcmp (p, "t.c\n")) return 1;
400 puts ("t: if (!1)\n");
403 puts ("t: if (one == 0)\n");
404 if (one == 0) return 1;
406 puts ("t: if (f != 0)\n");
407 if (one != 1) return 1;
409 puts ("t: if (1 && 0)\n");
410 if (1 && 0) return 1;
412 puts ("t: if (!t && f)\n");
413 if (!t && f) return 1;
415 puts ("t: if (t && !one)\n");
416 if (t && !one) return 1;
418 puts ("t: if (f || !t)\n");
419 if (f || !t) return 1;
421 puts ("t: if (i++)\n");
424 puts ("t: if (--i)\n");
427 puts ("t: i += 2\n");
429 if (i != 2) return 1;
431 puts ("t: i -= 2\n");
433 if (i != 0) return 1;
435 puts ("t: if (f = 0) ?\n");
438 puts ("t: if (!(t = 1)) ?\n");
439 if (!(t = 1)) return 1;
441 puts ("t: if ((f = 0) != 0) ?\n");
442 if ((f = 0) != 0) return 1;
444 puts ("t: if ((t = 1) != 1) ?\n");
445 if ((t = 1) != 1) return 1;
447 puts ("t: (one == 1) ?\n");
448 (one == 1) ? 1 : exit (1);
453 puts ("t: assert (1) ?\n");
456 puts ("t: assert (f==0) ?\n");
459 puts ("t: p[0] != 't'\n");
460 if (p[0] != 't') return p[0];
462 puts ("t: p[i] != 't'\n");
463 if (p[i] != 't') return p[i];
465 puts ("t: identity (p[i]) != 't'\n");
466 if (identity (p[i]) != 't') return identity (p[i]);
468 puts ("t: *g_chars != 'A'\n");
470 if (*g_chars != 'A') return 1;
472 puts ("t: *x != 'A'\n");
473 if (*x != 'A') return 1;
475 puts ("t: *y != 'A'\n");
476 if (*y != 'A') return 1;
478 puts ("t: *x != 'Q'\n");
480 if (*x != 'Q') return 1;
482 puts ("t: *x++ != 'C'\n");
484 if (*g_chars != 'C') return 1;
487 if (1 + 2 != 3) return 1;
490 if (2 - 1 != 1) return 1;
492 puts ("t: 1 << 3\n");
493 if (1 << 3 != 8) return 1;
496 if (8 / 4 != 2) return 1;
498 puts ("t: inc (0)\n");
499 if (inc (0) != 1) return 1;
501 puts ("t: inc (inc (0))\n");
502 if (inc (inc (0)) != 2) return 1;
504 puts ("t: inc (inc (inc (0)))\n");
505 if (inc (inc (inc (0))) != 3) return 1;
507 puts ("t: add (1, 2)\n");
508 if (add (1, 2) != 3) return 1;
510 puts ("t: add (inc (0), inc (1))\n");
511 if (add (inc (0), inc (1)) != 3) return 1;
513 puts ("t: add (TSTRING, 3)\n");
514 if (add (TSTRING, 3) != 13) return 1;
516 puts ("t: add (inc (inc (0)), inc (inc (1)))\n");
517 if (add (inc (inc (0)), inc (inc (1))) != 5) return 1;
519 puts ("t: goto label\n");
520 if (label (1) != 0) return 1;
522 puts ("t: switch 0\n");
523 if (swits (0) != 0) return swits (0);
525 puts ("t: switch 1\n");
526 if (swits (1) != 1) return 1;
528 puts ("t: switch -1\n");
529 if (swits (-1) != 2) return 1;
531 puts ("t: if (1)\n");
536 puts ("t: while (1) { goto label; };\n");
542 puts ("t: if (0); return 1; else;\n");
543 if (0) return 1; else goto ok01;
546 puts ("t: if (t)\n");
551 puts ("t: if (one > 0)\n");
552 if (one > 0) goto ok2;
556 puts ("t: if (one < 2)\n");
557 //if (one < 2) goto ok3;
558 if (one < 0x44) goto ok3;
562 puts ("t: if (strlen (\".\"))\n");
563 if (strlen (".")) goto ok4;
567 puts ("t: if (strlen (p) == 4)\n");
568 if (strlen (p) == 4) goto ok40;
571 puts ("t: if (!strcmp (p, \"t.c\\n\"))\n");
572 if (!strcmp (p, "t.c\n")) goto ok41;
576 puts ("t: if (strcmp (p, \"foo\"))\n");
577 if (strcmp (p, "foo")) goto ok42;
581 puts ("t: if (!0)\n");
586 puts ("t: if (one == 1)\n");
587 if (one == 1) goto ok6;
591 puts ("t: if (one != 0)\n");
592 if (one != 0) goto ok7;
596 puts ("t: if (1 && !0)\n");
597 if (1 && !0) goto ok8;
601 puts ("t: if (f || t)\n");
602 if (f || t) goto ok80;
606 puts ("t: if (++i)\n");
611 puts ("t: if (i--)\n");
616 puts ("t: *g_chars == 'B'\n");
618 if (*g_chars == 'B') goto ok11;
622 puts ("t: *x == 'B'\n");
624 if (*x == 'B') goto ok12;
628 puts ("t: *y == 'B'\n");
630 if (*y == 'B') goto ok13;
634 puts ("t: *x == 'R'\n");
636 if (*x == 'R') goto ok14;
640 puts ("t: *x++ == 'C'\n");
642 if (*g_chars == 'C') goto ok15;
646 puts ("t: itoa (33) == \"33\"\n");
647 if (strcmp (itoa (33), "33")) return 1;
649 return struct_test ();
654 main (int argc, char *argv[])
659 if (argc > 1 && !strcmp (argv[1], "--help")) return 1;
660 puts ("t: if (argc > 1 && !strcmp (argv[1], \"--help\")\n");
663 if (argc > 1) if (!strcmp (argv[1], "--help")) return 1;