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 ();
600 puts ("t: for (i=1; i<5; ++i)\n");
602 if (i != 5) return i;
604 puts ("t: while (i<3) i++\n");
607 if (i != 3) return i;
609 puts ("t: do i-- while (i>0)\n");
611 if (i != 0) return 1;
613 puts ("t: if (0)\n");
619 puts ("t: else 1\n");
626 puts ("t: else if 2\n");
628 puts ("t: if (f)\n");
631 puts ("t: if (one != 1)\n");
632 if (one != 1) return 1;
634 puts ("t: if (1 != one)\n");
635 if (1 != one) return 1;
637 puts ("t: if (one > 1)\n");
638 if (one > 1) return 1;
640 puts ("t: if (one < 0)\n");
641 if (one < 0) return 1;
643 puts ("t: if (one <= 0)\n");
644 if (one <= 0) return 1;
646 puts ("t: if (one >= 2)\n");
647 if (one >= 2) return 1;
649 puts ("t: if (strlen (\"\"))\n");
650 if (strlen ("")) return 1;
652 puts ("t: if (strlen (p) != 4)\n");
653 if (strlen (p) != 4) return 1;
655 puts ("t: if (!strlen (\".\"))\n");
656 if (!strlen (".")) return 1;
658 puts ("t: if (strcmp (p, \"foo\"))\n");
659 if (!strcmp (p, "foo")) return 1;
661 puts ("t: if (strcmp (p, \"t.c\\n\"))\n");
662 if (strcmp (p, "t.c\n")) return 1;
664 puts ("t: if (!1)\n");
667 puts ("t: if (one == 0)\n");
668 if (one == 0) return 1;
670 puts ("t: if (f != 0)\n");
671 if (one != 1) return 1;
673 puts ("t: if (1 && 0)\n");
674 if (1 && 0) return 1;
676 puts ("t: if (!t && f)\n");
677 if (!t && f) return 1;
679 puts ("t: if (t && !one)\n");
680 if (t && !one) return 1;
682 puts ("t: if (f || !t)\n");
683 if (f || !t) return 1;
685 puts ("t: if (i++)\n");
688 puts ("t: if (--i)\n");
691 puts ("t: i += 2\n");
693 if (i != 2) return 1;
695 puts ("t: i -= 2\n");
697 if (i != 0) return 1;
699 puts ("t: if (f = 0) ?\n");
702 puts ("t: if (!(t = 1)) ?\n");
703 if (!(t = 1)) return 1;
705 puts ("t: if ((f = 0) != 0) ?\n");
706 if ((f = 0) != 0) return 1;
708 puts ("t: if ((t = 1) != 1) ?\n");
709 if ((t = 1) != 1) return 1;
711 puts ("t: (one == 1) ?\n");
712 (one == 1) ? 1 : exit (1);
717 puts ("t: assert (1) ?\n");
720 puts ("t: assert (f==0) ?\n");
723 puts ("t: p[0] != 't'\n");
724 if (p[0] != 't') return p[0];
726 puts ("t: p[i] != 't'\n");
727 if (p[i] != 't') return p[i];
729 puts ("t: identity (p[i]) != 't'\n");
730 if (identity (p[i]) != 't') return identity (p[i]);
732 puts ("t: *g_chars != 'A'\n");
734 if (*g_chars != 'A') return 1;
736 puts ("t: *x != 'A'\n");
737 if (*x != 'A') return 1;
739 puts ("t: *y != 'A'\n");
740 if (*y != 'A') return 1;
742 puts ("t: *x != 'Q'\n");
744 if (*x != 'Q') return 1;
746 puts ("t: *x++ != 'C'\n");
748 if (*g_chars != 'C') return 1;
751 if (1 + 2 != 3) return 1;
754 if (2 - 1 != 1) return 1;
756 puts ("t: 1 << 3\n");
757 if (1 << 3 != 8) return 1;
759 puts ("t: 8 >> 3\n");
760 if (8 >> 3 != 1) return 1;
763 if (8 / 4 != 2) return 1;
765 puts ("t: inc (0)\n");
766 if (inc (0) != 1) return 1;
768 puts ("t: inc (inc (0))\n");
769 if (inc (inc (0)) != 2) return 1;
771 puts ("t: inc (inc (inc (0)))\n");
772 if (inc (inc (inc (0))) != 3) return 1;
774 puts ("t: add (1, 2)\n");
775 if (add (1, 2) != 3) return 1;
777 puts ("t: add (inc (0), inc (1))\n");
778 if (add (inc (0), inc (1)) != 3) return 1;
780 puts ("t: add (TSTRING, 3)\n");
781 if (add (TSTRING, 3) != 13) return 1;
783 puts ("t: add (inc (inc (0)), inc (inc (1)))\n");
784 if (add (inc (inc (0)), inc (inc (1))) != 5) return 1;
786 puts ("t: goto label\n");
787 if (label (1) != 0) return 1;
789 puts ("t: switch 0\n");
790 if (swits (0) != 0) return swits (0);
792 puts ("t: switch 1\n");
793 if (swits (1) != 1) return 1;
795 puts ("t: switch -1\n");
796 if (swits (-1) != 1) return 1;
798 puts ("t: switch -1\n");
799 if (swits (-2) != 2) return 1;
801 puts ("t: if (1)\n");
806 puts ("t: while (1) break;\n");
809 puts ("t: while (1) ... break;\n");
810 while (1) {f=0;break;}
812 puts ("t: while (1) ... break;\n");
813 while (1) {while (1) break;break;}
815 puts ("t: while (1) { goto label; };\n");
821 puts ("t: if (0); return 1; else;\n");
822 if (0) return 1; else goto ok01;
825 puts ("t: if (t)\n");
830 puts ("t: if (one > 0)\n");
831 if (one > 0) goto ok2;
835 puts ("t: if (one < 2)\n");
836 if (one < 2) goto ok3;
840 puts ("t: if (one >= 0)\n");
841 if (one >= 0) goto ok30;
845 puts ("t: if (one >= 1)\n");
846 if (one >= 0) goto ok31;
850 puts ("t: if (one <= 2)\n");
851 if (one <= 2) goto ok32;
855 puts ("t: if (one <= 1)\n");
856 if (one <= 1) goto ok33;
860 puts ("t: if (strlen (\".\"))\n");
861 if (strlen (".")) goto ok4;
865 puts ("t: if (strlen (p) == 4)\n");
866 if (strlen (p) == 4) goto ok40;
869 puts ("t: if (!strcmp (p, \"t.c\\n\"))\n");
870 if (!strcmp (p, "t.c\n")) goto ok41;
874 puts ("t: if (strcmp (p, \"foo\"))\n");
875 if (strcmp (p, "foo")) goto ok42;
879 puts ("t: if (!0)\n");
884 puts ("t: if (one == 1)\n");
885 if (one == 1) goto ok6;
889 puts ("t: if (one != 0)\n");
890 if (one != 0) goto ok7;
894 puts ("t: if (1 && !0)\n");
895 if (1 && !0) goto ok8;
899 puts ("t: if (f || t)\n");
900 if (f || t) goto ok80;
904 puts ("t: if (++i)\n");
909 puts ("t: if (i--)\n");
914 puts ("t: *g_chars == 'B'\n");
916 if (*g_chars == 'B') goto ok11;
920 puts ("t: *x == 'B'\n");
922 if (*x == 'B') goto ok12;
926 puts ("t: *y == 'B'\n");
928 if (*y == 'B') goto ok13;
932 puts ("t: *x == 'R'\n");
934 if (*x == 'R') goto ok14;
938 puts ("t: *x++ == 'C'\n");
940 if (*g_chars == 'C') goto ok15;
944 puts ("t: itoa (33) == \"33\"\n");
945 if (strcmp (itoa (33), "33")) return 1;
947 puts ("strcmp (itoa (-1), \"-1\")\n");
948 if (strcmp (itoa (-1), "-1")) return 1;
950 puts ("strcmp (itoa (0), \"0\")\n");
951 if (strcmp (itoa (0), "0")) return 1;
953 puts ("strcmp (itoa (1), \"1\")\n");
954 if (strcmp (itoa (1), "1")) return 1;
956 puts ("void_func ()\n");
959 return string_test ();
964 main (int argc, char *argv[])
969 puts ("t: argv[0] == \"out/t....\"\n");
970 if (strncmp (argv[0], "out/t", 5)) return 1;
972 puts ("t: *argv\"\n");
976 puts ("t: if (argc > 1 && !strcmp (argv[1], \"--help\")\n");
977 if (argc > 1 && !strcmp (argv[1], "--help")) return 1;