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/>.
34 struct scm *g_cells = arena;
36 struct scm *g_cells = (struct scm*)arena;
38 char *g_chars = arena;
40 int foo () {puts ("t: foo\n"); return 0;};
41 int bar (int i) {puts ("t: bar\n"); return 0;};
43 int (*function) (void);
47 struct function g_fun = {&exit,1,"fun"};
48 struct function g_foo = {&foo,0,"foo"};
49 struct function g_bar = {&bar,1,"bar"};
54 struct function g_functions[2];
57 enum type_t {TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TREF, TSPECIAL, TSTRING, TSYMBOL, TVALUES, TVECTOR, TBROKEN_HEART};
65 #define TYPE(x) (g_cells[x].type)
66 #define CAR(x) g_cells[x].car
67 #define CDR(x) g_cells[x].cdr
68 #define VALUE(x) g_cells[x].cdr
70 #define CAAR(x) CAR (CAR (x))
72 struct scm scm_fun = {TFUNCTION,0,0};
75 char *env[] = {"foo", "bar", "baz", 0};
100 if (c == 0) return c;
113 case TCHAR: {goto next;}
116 default: {goto next;}
154 char *p = (char*)g_chars;
156 puts ("t: read 0123456789\nt: ");
165 if (strcmp (g_chars, "0123456789")) return 1;
167 puts ("t: ungetc ('A') == getchar ()\n");
169 if (getchar () != 'A') return 1;
171 //ungetc ('\1', STDIN);
173 puts ("t: ungetc ();ungetc ();getchar ();getchar ()\n");
174 if (getchar () != 1) return 1;
175 //if (getchar () != '\0') return 1;
176 if (getchar () != 0) return 1;
178 puts ("t: i == 'm'\n");
181 if (i != 'm') return 1;
187 array_test (char **e)
191 puts ("a[i] = i-1\n");
193 for (int i=0; i < 3; i++) a[i] = i-1;
194 for (int i=0; i < 3; i++) if (a[i] != i-1) return 1;
204 puts ("env [0] == \"foo\"\n");
205 if (strcmp (env[0], "foo")) return 1;
207 puts ("env [1] == \"bar\"\n");
208 if (strcmp (env[1], "bar")) return 1;
210 puts ("t: **p in *env[]\n");
223 if (i != 3) return i;
226 puts ("t: *pp++ == \"foo\"\n");
227 if (strcmp (*pp++, "foo")) return 1;
229 puts ("t: *pp++ == \"bar\"\n");
230 if (strcmp (*pp++, "bar")) return 1;
233 puts ("t: buf[0]\n");
234 if (buf[0] != 'h') return 1;
236 puts ("t: buf + 1\n");
237 if (*(buf+1) != 'e') return 1;
241 if (**p != 'h') return 1;
243 puts ("t: *(p + 1)\n");
244 if (*(*p + 1) != 'e') return 1;
246 puts ("t: getenv ()");
247 if (!getenv ("SHELL")) return 1;
263 puts ("t: -1 < -2\n");
264 if (-1 < -2) return 1;
266 puts ("t: 0 < -1\n");
267 if (0 < -1) return 1;
275 puts ("t: -2 > -1\n");
276 if (-2 > -1) return 1;
278 puts ("t: -1 > 0\n");
279 if (-1 > 0) return 1;
281 puts ("t: 1 == inc (0)\n");
282 if (1 == inc (0)) goto ok0;
286 puts ("t: 0 < inc (0)\n");
287 if (0 < inc (0)) goto ok1;
291 puts ("t: inc (0) + 2 != 3\n");
292 if (inc (0) + inc (1) != 3) return 1;
305 puts ("t: i /= 4\n");
309 puts ("t: i *= 4\n");
313 puts ("t: 1 << 3\n");
314 if (1 << 3 != 8) return 1 << 3;
316 puts ("t: 3 << 4\n");
317 if (3 << 4 != 48) return 3 << 4;
319 puts ("t: 48 >> 3\n");
320 if (48 >> 4 != 3) return 48 >> 4;
322 puts ("t: 10 >> 1\n");
323 if (10 >> 1 != 5) return 10 >> 1;
326 if ((1 | 4) != 5) return 1 | 4;
330 if (-i != 3) return 1;
332 puts ("t: -1 + 2\n");
333 if (-1 + 2 != 1) return 1;
335 return array_test (env);
347 make_cell (SCM type, SCM car, SCM cdr)
350 TYPE (x) = VALUE (type);
351 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
352 if (car) CAR (x) = CAR (car);
353 if (cdr) CDR(x) = CDR(cdr);
355 else if (VALUE (type) == TFUNCTION) {
356 if (car) CAR (x) = car;
357 if (cdr) CDR(x) = CDR(cdr);
369 VALUE (tmp_num) = TPAIR;
370 make_cell (tmp_num, 0, 1);
375 make_tmps_test (struct scm* cells)
377 puts ("t: tmp = g_free++\n");
379 puts ("t: cells[tmp].type = CHAR\n");
380 cells[tmp].type = TCHAR;
382 cells[tmp_num].type = TNUMBER;
384 return make_cell_test();
390 puts ("t: g_cells[0] = g_cells[1]\n");
394 g_cells[0] = g_cells[1];
395 if (TYPE (0) != 1) return 1;
396 if (CAR (0) != 2) return 2;
397 if (CDR (0) != 3) return 3;
399 puts ("t: g_cells[i] = g_cells[j]\n");
405 g_cells[i] = g_cells[j];
406 if (TYPE (0) != 4) return 1;
407 if (CAR (0) != 5) return 2;
408 if (CDR (0) != 6) return 3;
410 puts ("t: g_cells[0+add(0,0] = g_cells[0+inc(0)]\n");
414 g_cells[0+add(0, 0)] = g_cells[0+inc(0)];
415 if (TYPE (0) != 1) return 1;
416 if (CAR (0) != 2) return 2;
417 if (CDR (0) != 3) return 3;
419 g_cells[0].type = TNUMBER;
422 g_cells[1].type = TNUMBER;
426 puts ("t: TYPE (0) != TYPE (1)\n");
427 if (TYPE (0) == TYPE (1)) goto ok;
434 puts ("t: int c = VALUE (0)\n");
436 if (c != 1) return 1;
438 puts ("t: CAAR (0) != 2\n");
439 if (CAAR (0) != 2) return 1;
441 puts ("t: 2 != CAAR (0)\n");
442 if (2 != CAAR (0)) return 1;
444 g_cells[3].type = 0x64;
445 if (g_cells[3].type != 0x64)
446 return g_cells[3].type;
457 puts ("t: g_fun.arity != 1;\n");
458 if (g_fun.arity != 1) return 1;
460 puts ("t: g_fun.function != exit;\n");
461 if (g_fun.function != &exit) return 1;
463 puts ("t: struct fun = {&exit,1,\"exit\"};\n");
464 struct function fun = {&exit,1,"exit"};
466 puts ("t: fun.arity != 1;\n");
467 if (fun.arity != 1) return 1;
469 puts ("t: fun.function != exit;\n");
470 if (fun.function != &exit) return 1;
472 puts ("t: puts (fun.name)\n");
473 if (strcmp (fun.name, "exit")) return 1;
475 puts ("t: puts (g_fun.name)\n");
476 if (strcmp (g_fun.name, "fun")) return 1;
478 puts ("t: g_functions[g_function++] = g_foo;\n");
479 g_functions[g_function++] = g_foo;
482 puts ("t: g_functions[g_cells[fn].cdr].arity\n");
483 if (g_functions[g_cells[fn].cdr].arity) return 1;
484 if (g_functions[g_cells[fn].cdr].arity != 0) return 1;
486 int (*functionx) (void) = 0;
487 functionx = g_functions[0].function;
488 puts ("t: functionx == foo\n");
489 if (functionx != foo) return 11;
491 puts ("t: g_functions[0].name\n");
492 if (strcmp (g_functions[0].name, "foo")) return 1;
494 puts ("t: (functionx) () == foo\n");
495 if ((functionx) () != 0) return 12;
497 puts ("t: g_functions[<foo>].arity\n");
498 if (g_functions[0].arity != 0) return 17;
501 g_functions[fn] = g_bar;
502 g_cells[fn].cdr = fn;
503 if (g_cells[fn].cdr != fn) return 13;
505 puts ("t: g_functions[g_cells[fn].cdr].function\n");
506 functionx = g_functions[g_cells[fn].cdr].function;
508 puts ("t: g_functions[1].name\n");
509 if (strcmp (g_functions[1].name, "bar")) return 1;
511 puts ("t: functionx == bar\n");
512 if (functionx != bar) return 15;
514 puts ("t: (functiony) (1) == bar\n");
515 int (*functiony) (int) = 0;
516 functiony = g_functions[g_cells[fn].cdr].function;
517 if ((functiony) (1) != 0) return 16;
519 puts ("t: g_functions[<bar>].arity\n");
520 if (g_functions[fn].arity != 1) return 18;
524 scm_fun.cdr = g_function;
525 //g_functions[g_function++] = g_fun;
526 g_functions[g_function] = g_fun;
528 g_cells[cell_fun] = scm_fun;
530 puts ("t: TYPE (cell_fun)\n");
531 if (TYPE (cell_fun) != TFUNCTION) return 1;
533 puts ("t: CAR (cell_fun)\n");
534 if (CAR (cell_fun) != 33) return 1;
536 puts ("t: CDR (cell_fun)\n");
537 if (CDR (cell_fun) != g_function) return 1;
539 return make_tmps_test (g_cells);
554 puts ("t: for (i=1; i<5; ++i)\n");
556 if (i != 5) return i;
558 puts ("t: while (i<3) i++\n");
561 if (i != 3) return i;
563 puts ("t: do i-- while (i>0)\n");
565 if (i != 0) return 1;
567 puts ("t: if (0)\n");
573 puts ("t: else 1\n");
580 puts ("t: else if 2\n");
582 puts ("t: if (f)\n");
585 puts ("t: if (one != 1)\n");
586 if (one != 1) return 1;
588 puts ("t: if (1 != one)\n");
589 if (1 != one) return 1;
591 puts ("t: if (one > 1)\n");
592 if (one > 1) return 1;
594 puts ("t: if (one < 0)\n");
595 if (one < 0) return 1;
597 puts ("t: if (one <= 0)\n");
598 if (one <= 0) return 1;
600 puts ("t: if (one >= 2)\n");
601 if (one >= 2) return 1;
603 puts ("t: if (strlen (\"\"))\n");
604 if (strlen ("")) return 1;
606 puts ("t: if (strlen (p) != 4)\n");
607 if (strlen (p) != 4) return 1;
609 puts ("t: if (!strlen (\".\"))\n");
610 if (!strlen (".")) return 1;
612 puts ("t: if (strcmp (p, \"foo\"))\n");
613 if (!strcmp (p, "foo")) return 1;
615 puts ("t: if (strcmp (p, \"t.c\\n\"))\n");
616 if (strcmp (p, "t.c\n")) return 1;
618 puts ("t: if (!1)\n");
621 puts ("t: if (one == 0)\n");
622 if (one == 0) return 1;
624 puts ("t: if (f != 0)\n");
625 if (one != 1) return 1;
627 puts ("t: if (1 && 0)\n");
628 if (1 && 0) return 1;
630 puts ("t: if (!t && f)\n");
631 if (!t && f) return 1;
633 puts ("t: if (t && !one)\n");
634 if (t && !one) return 1;
636 puts ("t: if (f || !t)\n");
637 if (f || !t) return 1;
639 puts ("t: if (i++)\n");
642 puts ("t: if (--i)\n");
645 puts ("t: i += 2\n");
647 if (i != 2) return 1;
649 puts ("t: i -= 2\n");
651 if (i != 0) return 1;
653 puts ("t: if (f = 0) ?\n");
656 puts ("t: if (!(t = 1)) ?\n");
657 if (!(t = 1)) return 1;
659 puts ("t: if ((f = 0) != 0) ?\n");
660 if ((f = 0) != 0) return 1;
662 puts ("t: if ((t = 1) != 1) ?\n");
663 if ((t = 1) != 1) return 1;
665 puts ("t: (one == 1) ?\n");
666 (one == 1) ? 1 : exit (1);
671 puts ("t: assert (1) ?\n");
674 puts ("t: assert (f==0) ?\n");
677 puts ("t: p[0] != 't'\n");
678 if (p[0] != 't') return p[0];
680 puts ("t: p[i] != 't'\n");
681 if (p[i] != 't') return p[i];
683 puts ("t: identity (p[i]) != 't'\n");
684 if (identity (p[i]) != 't') return identity (p[i]);
686 puts ("t: *g_chars != 'A'\n");
688 if (*g_chars != 'A') return 1;
690 puts ("t: *x != 'A'\n");
691 if (*x != 'A') return 1;
693 puts ("t: *y != 'A'\n");
694 if (*y != 'A') return 1;
696 puts ("t: *x != 'Q'\n");
698 if (*x != 'Q') return 1;
700 puts ("t: *x++ != 'C'\n");
702 if (*g_chars != 'C') return 1;
705 if (1 + 2 != 3) return 1;
708 if (2 - 1 != 1) return 1;
710 puts ("t: 1 << 3\n");
711 if (1 << 3 != 8) return 1;
713 puts ("t: 8 >> 3\n");
714 if (8 >> 3 != 1) return 1;
717 if (8 / 4 != 2) return 1;
719 puts ("t: inc (0)\n");
720 if (inc (0) != 1) return 1;
722 puts ("t: inc (inc (0))\n");
723 if (inc (inc (0)) != 2) return 1;
725 puts ("t: inc (inc (inc (0)))\n");
726 if (inc (inc (inc (0))) != 3) return 1;
728 puts ("t: add (1, 2)\n");
729 if (add (1, 2) != 3) return 1;
731 puts ("t: add (inc (0), inc (1))\n");
732 if (add (inc (0), inc (1)) != 3) return 1;
734 puts ("t: add (TSTRING, 3)\n");
735 if (add (TSTRING, 3) != 13) return 1;
737 puts ("t: add (inc (inc (0)), inc (inc (1)))\n");
738 if (add (inc (inc (0)), inc (inc (1))) != 5) return 1;
740 puts ("t: goto label\n");
741 if (label (1) != 0) return 1;
743 puts ("t: switch 0\n");
744 if (swits (0) != 0) return swits (0);
746 puts ("t: switch 1\n");
747 if (swits (1) != 1) return 1;
749 puts ("t: switch -1\n");
750 if (swits (-1) != 1) return 1;
752 puts ("t: switch -1\n");
753 if (swits (-2) != 2) return 1;
755 puts ("t: if (1)\n");
760 puts ("t: while (1) break;\n");
763 puts ("t: while (1) ... break;\n");
764 while (1) {f=0;break;}
766 puts ("t: while (1) ... break;\n");
767 while (1) {while (1) break;break;}
769 puts ("t: while (1) { goto label; };\n");
775 puts ("t: if (0); return 1; else;\n");
776 if (0) return 1; else goto ok01;
779 puts ("t: if (t)\n");
784 puts ("t: if (one > 0)\n");
785 if (one > 0) goto ok2;
789 puts ("t: if (one < 2)\n");
790 if (one < 2) goto ok3;
794 puts ("t: if (one >= 0)\n");
795 if (one >= 0) goto ok30;
799 puts ("t: if (one >= 1)\n");
800 if (one >= 0) goto ok31;
804 puts ("t: if (one <= 2)\n");
805 if (one <= 2) goto ok32;
809 puts ("t: if (one <= 1)\n");
810 if (one <= 1) goto ok33;
814 puts ("t: if (strlen (\".\"))\n");
815 if (strlen (".")) goto ok4;
819 puts ("t: if (strlen (p) == 4)\n");
820 if (strlen (p) == 4) goto ok40;
823 puts ("t: if (!strcmp (p, \"t.c\\n\"))\n");
824 if (!strcmp (p, "t.c\n")) goto ok41;
828 puts ("t: if (strcmp (p, \"foo\"))\n");
829 if (strcmp (p, "foo")) goto ok42;
833 puts ("t: if (!0)\n");
838 puts ("t: if (one == 1)\n");
839 if (one == 1) goto ok6;
843 puts ("t: if (one != 0)\n");
844 if (one != 0) goto ok7;
848 puts ("t: if (1 && !0)\n");
849 if (1 && !0) goto ok8;
853 puts ("t: if (f || t)\n");
854 if (f || t) goto ok80;
858 puts ("t: if (++i)\n");
863 puts ("t: if (i--)\n");
868 puts ("t: *g_chars == 'B'\n");
870 if (*g_chars == 'B') goto ok11;
874 puts ("t: *x == 'B'\n");
876 if (*x == 'B') goto ok12;
880 puts ("t: *y == 'B'\n");
882 if (*y == 'B') goto ok13;
886 puts ("t: *x == 'R'\n");
888 if (*x == 'R') goto ok14;
892 puts ("t: *x++ == 'C'\n");
894 if (*g_chars == 'C') goto ok15;
898 puts ("t: itoa (33) == \"33\"\n");
899 if (strcmp (itoa (33), "33")) return 1;
901 puts ("strcmp (itoa (-1), \"-1\")\n");
902 if (strcmp (itoa (-1), "-1")) return 1;
907 puts ("strcmp (itoa (0), \"0\")\n");
908 if (strcmp (itoa (0), "0")) return 1;
910 puts ("strcmp (itoa (1), \"1\")\n");
911 if (strcmp (itoa (1), "1")) return 1;
913 return struct_test ();
918 main (int argc, char *argv[])
923 puts ("t: argv[0] == \"out/t....\"\n");
924 if (strncmp (argv[0], "out/t", 5)) return 1;
926 puts ("t: *argv\"\n");
930 puts ("t: if (argc > 1 && !strcmp (argv[1], \"--help\")\n");
931 if (argc > 1 && !strcmp (argv[1], "--help")) return 1;