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/>.
36 struct scm *g_cells = arena;
38 struct scm *g_cells = (struct scm*)arena;
40 char *g_chars = arena;
42 int foo () {puts ("t: foo\n"); return 0;};
43 int bar (int i) {puts ("t: bar\n"); return 0;};
45 int (*function) (void);
49 struct function g_fun = {&exit,1,"fun"};
50 struct function g_foo = {&foo,0,"foo"};
51 struct function g_bar = {&bar,1,"bar"};
56 struct function g_functions[2];
59 enum type_t {TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TREF, TSPECIAL, TSTRING, TSYMBOL, TVALUES, TVECTOR, TBROKEN_HEART};
67 #define TYPE(x) (g_cells[x].type)
68 #define CAR(x) g_cells[x].car
69 #define CDR(x) g_cells[x].cdr
70 #define VALUE(x) g_cells[x].cdr
72 #define CAAR(x) CAR (CAR (x))
74 struct scm scm_fun = {TFUNCTION,0,0};
77 char *env[] = {"foo", "bar", "baz", 0};
102 if (c == 0) return c;
115 case TCHAR: {goto next;}
118 default: {goto next;}
156 char *p = (char*)g_chars;
158 puts ("t: read 0123456789\nt: ");
167 if (strcmp (g_chars, "0123456789")) return 1;
169 puts ("t: ungetc ('A') == getchar ()\n");
171 if (getchar () != 'A') return 1;
173 //ungetc ('\1', STDIN);
175 puts ("t: ungetc ();ungetc ();getchar ();getchar ()\n");
176 if (getchar () != 1) return 1;
177 //if (getchar () != '\0') return 1;
178 if (getchar () != 0) return 1;
180 puts ("t: i == 'm'\n");
183 if (i != 'm') return 1;
189 array_test (char **e)
193 puts ("a[i] = i-1\n");
195 for (int i=0; i < 3; i++) a[i] = i-1;
196 for (int i=0; i < 3; i++) if (a[i] != i-1) return 1;
206 puts ("env [0] == \"foo\"\n");
207 if (strcmp (env[0], "foo")) return 1;
209 puts ("env [1] == \"bar\"\n");
210 if (strcmp (env[1], "bar")) return 1;
212 puts ("t: **p in *env[]\n");
225 if (i != 3) return i;
228 puts ("t: *pp++ == \"foo\"\n");
229 if (strcmp (*pp++, "foo")) return 1;
231 puts ("t: *pp++ == \"bar\"\n");
232 if (strcmp (*pp++, "bar")) return 1;
235 puts ("t: buf[0]\n");
236 if (buf[0] != 'h') return 1;
238 puts ("t: buf + 1\n");
239 if (*(buf+1) != 'e') return 1;
243 if (**p != 'h') return 1;
245 puts ("t: *(p + 1)\n");
246 if (*(*p + 1) != 'e') return 1;
248 puts ("t: getenv ()");
249 if (!getenv ("SHELL")) return 1;
265 puts ("t: -1 < -2\n");
266 if (-1 < -2) return 1;
268 puts ("t: 0 < -1\n");
269 if (0 < -1) return 1;
277 puts ("t: -2 > -1\n");
278 if (-2 > -1) return 1;
280 puts ("t: -1 > 0\n");
281 if (-1 > 0) return 1;
283 puts ("t: 1 == inc (0)\n");
284 if (1 == inc (0)) goto ok0;
288 puts ("t: 0 < inc (0)\n");
289 if (0 < inc (0)) goto ok1;
293 puts ("t: inc (0) + 2 != 3\n");
294 if (inc (0) + inc (1) != 3) return 1;
307 puts ("t: i /= 4\n");
311 puts ("t: i *= 4\n");
315 puts ("t: 1 << 3\n");
316 if (1 << 3 != 8) return 1 << 3;
318 puts ("t: 3 << 4\n");
319 if (3 << 4 != 48) return 3 << 4;
321 puts ("t: 48 >> 3\n");
322 if (48 >> 4 != 3) return 48 >> 4;
324 puts ("t: 10 >> 1\n");
325 if (10 >> 1 != 5) return 10 >> 1;
328 if ((1 | 4) != 5) return 1 | 4;
332 if (-i != 3) return 1;
334 puts ("t: -1 + 2\n");
335 if (-1 + 2 != 1) return 1;
338 if ((1 & 3) != 1) return 1;
341 if ((1 | 2) != 3) return 1;
344 if ((1 ^ 3) != 2) return 1;
346 puts ("t: 3 == 3\n");
347 if ((3 == 3) != 1) return 1;
349 puts ("t: 3 != 3\n");
350 if ((3 != 3) != 0) return 1;
352 puts ("t: 011 == 15\n");
353 if (011 != 9) return 1;
355 puts ("t: 0b11 == 3\n");
356 if (0b11 != 3) return 1;
358 puts ("t: 0x11 == 3\n");
359 if (0x11 != 17) return 1;
361 return array_test (env);
373 make_cell (SCM type, SCM car, SCM cdr)
376 TYPE (x) = VALUE (type);
377 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
378 if (car) CAR (x) = CAR (car);
379 if (cdr) CDR(x) = CDR(cdr);
381 else if (VALUE (type) == TFUNCTION) {
382 if (car) CAR (x) = car;
383 if (cdr) CDR(x) = CDR(cdr);
395 VALUE (tmp_num) = TPAIR;
396 make_cell (tmp_num, 0, 1);
401 make_tmps_test (struct scm* cells)
403 puts ("t: tmp = g_free++\n");
405 puts ("t: cells[tmp].type = CHAR\n");
406 cells[tmp].type = TCHAR;
408 cells[tmp_num].type = TNUMBER;
410 return make_cell_test();
416 puts ("t: g_cells[0] = g_cells[1]\n");
420 g_cells[0] = g_cells[1];
421 if (TYPE (0) != 1) return 1;
422 if (CAR (0) != 2) return 2;
423 if (CDR (0) != 3) return 3;
425 puts ("t: g_cells[i] = g_cells[j]\n");
431 g_cells[i] = g_cells[j];
432 if (TYPE (0) != 4) return 1;
433 if (CAR (0) != 5) return 2;
434 if (CDR (0) != 6) return 3;
436 puts ("t: g_cells[0+add(0,0] = g_cells[0+inc(0)]\n");
440 g_cells[0+add(0, 0)] = g_cells[0+inc(0)];
441 if (TYPE (0) != 1) return 1;
442 if (CAR (0) != 2) return 2;
443 if (CDR (0) != 3) return 3;
445 g_cells[0].type = TNUMBER;
448 g_cells[1].type = TNUMBER;
452 puts ("t: TYPE (0) != TYPE (1)\n");
453 if (TYPE (0) == TYPE (1)) goto ok;
460 puts ("t: int c = VALUE (0)\n");
462 if (c != 1) return 1;
464 puts ("t: CAAR (0) != 2\n");
465 if (CAAR (0) != 2) return 1;
467 puts ("t: 2 != CAAR (0)\n");
468 if (2 != CAAR (0)) return 1;
470 g_cells[3].type = 0x64;
471 if (g_cells[3].type != 0x64)
472 return g_cells[3].type;
483 puts ("t: g_fun.arity != 1;\n");
484 if (g_fun.arity != 1) return 1;
486 puts ("t: g_fun.function != exit;\n");
487 if (g_fun.function != &exit) return 1;
489 puts ("t: struct fun = {&exit,1,\"exit\"};\n");
490 struct function fun = {&exit,1,"exit"};
492 puts ("t: fun.arity != 1;\n");
493 if (fun.arity != 1) return 1;
495 puts ("t: fun.function != exit;\n");
496 if (fun.function != &exit) return 1;
498 puts ("t: puts (fun.name)\n");
499 if (strcmp (fun.name, "exit")) return 1;
501 puts ("t: puts (g_fun.name)\n");
502 if (strcmp (g_fun.name, "fun")) return 1;
504 puts ("t: g_functions[g_function++] = g_foo;\n");
505 g_functions[g_function++] = g_foo;
507 puts ("t: pbar->arity == 1\n");
508 struct function* barp = &g_bar;
509 if (barp->arity != 1) return 1;
512 puts ("t: g_functions[g_cells[fn].cdr].arity\n");
513 if (g_functions[g_cells[fn].cdr].arity) return 1;
514 if (g_functions[g_cells[fn].cdr].arity != 0) return 1;
516 int (*functionx) (void) = 0;
517 functionx = g_functions[0].function;
518 puts ("t: functionx == foo\n");
519 if (functionx != foo) return 11;
521 puts ("t: g_functions[0].name\n");
522 if (strcmp (g_functions[0].name, "foo")) return 1;
524 puts ("t: (functionx) () == foo\n");
525 if ((functionx) () != 0) return 12;
527 puts ("t: g_functions[<foo>].arity\n");
528 if (g_functions[0].arity != 0) return 17;
531 g_functions[fn] = g_bar;
532 g_cells[fn].cdr = fn;
533 if (g_cells[fn].cdr != fn) return 13;
535 puts ("t: g_functions[g_cells[fn].cdr].function\n");
536 functionx = g_functions[g_cells[fn].cdr].function;
538 puts ("t: g_functions[1].name\n");
539 if (strcmp (g_functions[1].name, "bar")) return 1;
541 puts ("t: functionx == bar\n");
542 if (functionx != bar) return 15;
544 puts ("t: (functiony) (1) == bar\n");
545 int (*functiony) (int) = 0;
546 functiony = g_functions[g_cells[fn].cdr].function;
547 if ((functiony) (1) != 0) return 16;
549 puts ("t: g_functions[<bar>].arity\n");
550 if (g_functions[fn].arity != 1) return 18;
554 scm_fun.cdr = g_function;
555 //g_functions[g_function++] = g_fun;
556 g_functions[g_function] = g_fun;
558 g_cells[cell_fun] = scm_fun;
560 puts ("t: TYPE (cell_fun)\n");
561 if (TYPE (cell_fun) != TFUNCTION) return 1;
563 puts ("t: CAR (cell_fun)\n");
564 if (CAR (cell_fun) != 33) return 1;
566 puts ("t: CDR (cell_fun)\n");
567 if (CDR (cell_fun) != g_function) return 1;
569 return make_tmps_test (g_cells);
575 puts ("t: strcpy (buf, \"hallo\")\n");
577 strcpy (buf, "hallo");
578 if (strcmp (buf, "hallo")) return 1;
580 return struct_test ();
602 puts ("t: for (i=1; i<5; ++i)\n");
604 if (i != 5) return i;
606 puts ("t: while (i<3) i++\n");
609 if (i != 3) return i;
611 puts ("t: do i-- while (i>0)\n");
613 if (i != 0) return 1;
615 puts ("t: if (0)\n");
621 puts ("t: else 1\n");
628 puts ("t: else if 2\n");
630 puts ("t: if (f)\n");
633 puts ("t: if (one != 1)\n");
634 if (one != 1) return 1;
636 puts ("t: if (1 != one)\n");
637 if (1 != one) return 1;
639 puts ("t: if (one > 1)\n");
640 if (one > 1) return 1;
642 puts ("t: if (one < 0)\n");
643 if (one < 0) return 1;
645 puts ("t: if (one <= 0)\n");
646 if (one <= 0) return 1;
648 puts ("t: if (one >= 2)\n");
649 if (one >= 2) return 1;
651 puts ("t: if (strlen (\"\"))\n");
652 if (strlen ("")) return 1;
654 puts ("t: if (strlen (p) != 4)\n");
655 if (strlen (p) != 4) return 1;
657 puts ("t: if (!strlen (\".\"))\n");
658 if (!strlen (".")) return 1;
660 puts ("t: if (strcmp (p, \"foo\"))\n");
661 if (!strcmp (p, "foo")) return 1;
663 puts ("t: if (strcmp (p, \"t.c\\n\"))\n");
664 if (strcmp (p, "t.c\n")) return 1;
666 puts ("t: if (!1)\n");
669 puts ("t: if (one == 0)\n");
670 if (one == 0) return 1;
672 puts ("t: if (f != 0)\n");
673 if (one != 1) return 1;
675 puts ("t: if (1 && 0)\n");
676 if (1 && 0) return 1;
678 puts ("t: if (!t && f)\n");
679 if (!t && f) return 1;
681 puts ("t: if (t && !one)\n");
682 if (t && !one) return 1;
684 puts ("t: if (f || !t)\n");
685 if (f || !t) return 1;
687 puts ("t: if (i++)\n");
690 puts ("t: if (--i)\n");
693 puts ("t: i += 2\n");
695 if (i != 2) return 1;
697 puts ("t: i -= 2\n");
699 if (i != 0) return 1;
701 puts ("t: if (f = 0) ?\n");
704 puts ("t: if (!(t = 1)) ?\n");
705 if (!(t = 1)) return 1;
707 puts ("t: if ((f = 0) != 0) ?\n");
708 if ((f = 0) != 0) return 1;
710 puts ("t: if ((t = 1) != 1) ?\n");
711 if ((t = 1) != 1) return 1;
713 puts ("t: (one == 1) ?\n");
714 (one == 1) ? 1 : exit (1);
719 puts ("t: assert (1) ?\n");
722 puts ("t: assert (f==0) ?\n");
725 puts ("t: p[0] != 't'\n");
726 if (p[0] != 't') return p[0];
728 puts ("t: p[i] != 't'\n");
729 if (p[i] != 't') return p[i];
731 puts ("t: identity (p[i]) != 't'\n");
732 if (identity (p[i]) != 't') return identity (p[i]);
734 puts ("t: *g_chars != 'A'\n");
736 if (*g_chars != 'A') return 1;
738 puts ("t: *x != 'A'\n");
739 if (*x != 'A') return 1;
741 puts ("t: *y != 'A'\n");
742 if (*y != 'A') return 1;
744 puts ("t: *x != 'Q'\n");
746 if (*x != 'Q') return 1;
748 puts ("t: *x++ != 'C'\n");
750 if (*g_chars != 'C') return 1;
753 if (1 + 2 != 3) return 1;
756 if (2 - 1 != 1) return 1;
758 puts ("t: 1 << 3\n");
759 if (1 << 3 != 8) return 1;
761 puts ("t: 8 >> 3\n");
762 if (8 >> 3 != 1) return 1;
765 if (8 / 4 != 2) return 1;
767 puts ("t: inc (0)\n");
768 if (inc (0) != 1) return 1;
770 puts ("t: inc (inc (0))\n");
771 if (inc (inc (0)) != 2) return 1;
773 puts ("t: inc (inc (inc (0)))\n");
774 if (inc (inc (inc (0))) != 3) return 1;
776 puts ("t: add (1, 2)\n");
777 if (add (1, 2) != 3) return 1;
779 puts ("t: add (inc (0), inc (1))\n");
780 if (add (inc (0), inc (1)) != 3) return 1;
782 puts ("t: add (TSTRING, 3)\n");
783 if (add (TSTRING, 3) != 13) return 1;
785 puts ("t: add (inc (inc (0)), inc (inc (1)))\n");
786 if (add (inc (inc (0)), inc (inc (1))) != 5) return 1;
788 puts ("t: goto label\n");
789 if (label (1) != 0) return 1;
791 puts ("t: switch 0\n");
792 if (swits (0) != 0) return swits (0);
794 puts ("t: switch 1\n");
795 if (swits (1) != 1) return 1;
797 puts ("t: switch -1\n");
798 if (swits (-1) != 1) return 1;
800 puts ("t: switch -1\n");
801 if (swits (-2) != 2) return 1;
803 puts ("t: if (1)\n");
808 puts ("t: while (1) break;\n");
811 puts ("t: while (1) ... break;\n");
812 while (1) {f=0;break;}
814 puts ("t: while (1) {while (1) break;break;}\n");
815 while (1) {while (1) break;break;}
817 puts ("t: while () {continue;...}\n");
818 while (one--) {continue;one=1;}
821 puts ("t: while (1) { goto label; };\n");
827 puts ("t: if (0); return 1; else;\n");
828 if (0) return 1; else goto ok01;
831 puts ("t: if (t)\n");
836 puts ("t: if (one > 0)\n");
837 if (one > 0) goto ok2;
841 puts ("t: if (one < 2)\n");
842 if (one < 2) goto ok3;
846 puts ("t: if (one >= 0)\n");
847 if (one >= 0) goto ok30;
851 puts ("t: if (one >= 1)\n");
852 if (one >= 0) goto ok31;
856 puts ("t: if (one <= 2)\n");
857 if (one <= 2) goto ok32;
861 puts ("t: if (one <= 1)\n");
862 if (one <= 1) goto ok33;
866 puts ("t: if (strlen (\".\"))\n");
867 if (strlen (".")) goto ok4;
871 puts ("t: if (strlen (p) == 4)\n");
872 if (strlen (p) == 4) goto ok40;
875 puts ("t: if (!strcmp (p, \"t.c\\n\"))\n");
876 if (!strcmp (p, "t.c\n")) goto ok41;
880 puts ("t: if (strcmp (p, \"foo\"))\n");
881 if (strcmp (p, "foo")) goto ok42;
885 puts ("t: if (!0)\n");
890 puts ("t: if (one == 1)\n");
891 if (one == 1) goto ok6;
895 puts ("t: if (one != 0)\n");
896 if (one != 0) goto ok7;
900 puts ("t: if (1 && !0)\n");
901 if (1 && !0) goto ok8;
905 puts ("t: if (f || t)\n");
906 if (f || t) goto ok80;
910 puts ("t: if (++i)\n");
915 puts ("t: if (i--)\n");
920 puts ("t: *g_chars == 'B'\n");
922 if (*g_chars == 'B') goto ok11;
926 puts ("t: *x == 'B'\n");
928 if (*x == 'B') goto ok12;
932 puts ("t: *y == 'B'\n");
934 if (*y == 'B') goto ok13;
938 puts ("t: *x == 'R'\n");
940 if (*x == 'R') goto ok14;
944 puts ("t: *x++ == 'C'\n");
946 if (*g_chars == 'C') goto ok15;
950 puts ("t: itoa (33) == \"33\"\n");
951 if (strcmp (itoa (33), "33")) return 1;
953 puts ("strcmp (itoa (-1), \"-1\")\n");
954 if (strcmp (itoa (-1), "-1")) return 1;
956 puts ("strcmp (itoa (0), \"0\")\n");
957 if (strcmp (itoa (0), "0")) return 1;
959 puts ("strcmp (itoa (1), \"1\")\n");
960 if (strcmp (itoa (1), "1")) return 1;
962 puts ("void_func ()\n");
965 return string_test ();
970 main (int argc, char *argv[])
975 puts ("t: argv[0] == \"out/t....\"\n");
976 if (strncmp (argv[0], "out/t", 5)) return 1;
978 puts ("t: *argv\"\n");
982 puts ("t: if (argc > 1 && !strcmp (argv[1], \"--help\")\n");
983 if (argc > 1 && !strcmp (argv[1], "--help")) return 1;