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 char *p = (char*)g_chars;
152 puts ("t: read 0123456789\nt: ");
161 if (strcmp (g_chars, "0123456789")) return 1;
163 puts ("t: ungetc ('A') == getchar ()\n");
165 if (getchar () != 'A') return 1;
167 //ungetc ('\1', STDIN);
169 puts ("t: ungetc ();ungetc ();getchar ();getchar ()\n");
170 if (getchar () != 1) return 1;
171 //if (getchar () != '\0') return 1;
172 if (getchar () != 0) return 1;
188 puts ("t: -1 < -2\n");
189 if (-1 < -2) return 1;
191 puts ("t: 0 < -1\n");
192 if (0 < -1) return 1;
200 puts ("t: -2 > -1\n");
201 if (-2 > -1) return 1;
203 puts ("t: -1 > 0\n");
204 if (-1 > 0) return 1;
206 puts ("t: 1 == inc (0)\n");
207 if (1 == inc (0)) goto ok0;
211 puts ("t: 0 < inc (0)\n");
212 if (0 < inc (0)) goto ok1;
216 puts ("t: inc (0) + 2 != 3\n");
217 if (inc (0) + inc (1) != 3) return 1;
230 puts ("t: i /= 4\n");
234 puts ("t: i *= 4\n");
238 puts ("t: 1 << 3\n");
239 if (1 << 3 != 8) return 1 << 3;
241 puts ("t: 3 << 4\n");
242 if (3 << 4 != 48) return 3 << 4;
244 puts ("t: 48 >> 3\n");
245 if (48 >> 4 != 3) return 48 >> 4;
247 puts ("t: 10 >> 1\n");
248 if (10 >> 1 != 5) return 10 >> 1;
251 if ((1 | 4) != 5) return 1 | 4;
255 if (-i != 3) return 1;
257 puts ("t: -1 + 2\n");
258 if (-1 + 2 != 1) return 1;
272 make_cell (SCM type, SCM car, SCM cdr)
275 TYPE (x) = VALUE (type);
276 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
277 if (car) CAR (x) = CAR (car);
278 if (cdr) CDR(x) = CDR(cdr);
280 else if (VALUE (type) == TFUNCTION) {
281 if (car) CAR (x) = car;
282 if (cdr) CDR(x) = CDR(cdr);
294 VALUE (tmp_num) = TPAIR;
295 make_cell (tmp_num, 0, 1);
300 make_tmps_test (struct scm* cells)
302 puts ("t: tmp = g_free++\n");
304 puts ("t: cells[tmp].type = CHAR\n");
305 cells[tmp].type = TCHAR;
307 cells[tmp_num].type = TNUMBER;
309 return make_cell_test();
315 puts ("t: g_cells[0] = g_cells[1]\n");
319 g_cells[0] = g_cells[1];
320 if (TYPE (0) != 1) return 1;
321 if (CAR (0) != 2) return 2;
322 if (CDR (0) != 3) return 3;
324 puts ("t: g_cells[i] = g_cells[j]\n");
330 g_cells[i] = g_cells[j];
331 if (TYPE (0) != 4) return 1;
332 if (CAR (0) != 5) return 2;
333 if (CDR (0) != 6) return 3;
335 puts ("t: g_cells[0+add(0,0] = g_cells[0+inc(0)]\n");
339 g_cells[0+add(0, 0)] = g_cells[0+inc(0)];
340 if (TYPE (0) != 1) return 1;
341 if (CAR (0) != 2) return 2;
342 if (CDR (0) != 3) return 3;
344 g_cells[0].type = TNUMBER;
347 g_cells[1].type = TNUMBER;
351 puts ("t: TYPE (0) != TYPE (1)\n");
352 if (TYPE (0) == TYPE (1)) goto ok;
359 puts ("t: int c = VALUE (0)\n");
361 if (c != 1) return 1;
363 puts ("t: CAAR (0) != 2\n");
364 if (CAAR (0) != 2) return 1;
366 puts ("t: 2 != CAAR (0)\n");
367 if (2 != CAAR (0)) return 1;
369 g_cells[3].type = 0x64;
370 if (g_cells[3].type != 0x64)
371 return g_cells[3].type;
382 puts ("t: g_fun.arity != 1;\n");
383 if (g_fun.arity != 1) return 1;
385 puts ("t: g_fun.function != exit;\n");
386 if (g_fun.function != &exit) return 1;
388 puts ("t: struct fun = {&exit,1,\"exit\"};\n");
389 struct function fun = {&exit,1,"exit"};
391 puts ("t: fun.arity != 1;\n");
392 if (fun.arity != 1) return 1;
394 puts ("t: fun.function != exit;\n");
395 if (fun.function != &exit) return 1;
397 puts ("t: puts (fun.name)\n");
398 if (strcmp (fun.name, "exit")) return 1;
400 puts ("t: puts (g_fun.name)\n");
401 if (strcmp (g_fun.name, "fun")) return 1;
403 puts ("t: g_functions[g_function++] = g_foo;\n");
404 g_functions[g_function++] = g_foo;
407 puts ("t: g_functions[g_cells[fn].cdr].arity\n");
408 if (g_functions[g_cells[fn].cdr].arity) return 1;
409 if (g_functions[g_cells[fn].cdr].arity != 0) return 1;
411 int (*functionx) (void) = 0;
412 functionx = g_functions[0].function;
413 puts ("t: functionx == foo\n");
414 if (functionx != foo) return 11;
416 puts ("t: g_functions[0].name\n");
417 if (strcmp (g_functions[0].name, "foo")) return 1;
419 puts ("t: (functionx) () == foo\n");
420 if ((functionx) () != 0) return 12;
422 puts ("t: g_functions[<foo>].arity\n");
423 if (g_functions[0].arity != 0) return 17;
426 g_functions[fn] = g_bar;
427 g_cells[fn].cdr = fn;
428 if (g_cells[fn].cdr != fn) return 13;
430 puts ("t: g_functions[g_cells[fn].cdr].function\n");
431 functionx = g_functions[g_cells[fn].cdr].function;
433 puts ("t: g_functions[1].name\n");
434 if (strcmp (g_functions[1].name, "bar")) return 1;
436 puts ("t: functionx == bar\n");
437 if (functionx != bar) return 15;
439 puts ("t: (functiony) (1) == bar\n");
440 int (*functiony) (int) = 0;
441 functiony = g_functions[g_cells[fn].cdr].function;
442 if ((functiony) (1) != 0) return 16;
444 puts ("t: g_functions[<bar>].arity\n");
445 if (g_functions[fn].arity != 1) return 18;
449 scm_fun.cdr = g_function;
450 //g_functions[g_function++] = g_fun;
451 g_functions[g_function] = g_fun;
453 g_cells[cell_fun] = scm_fun;
455 puts ("t: TYPE (cell_fun)\n");
456 if (TYPE (cell_fun) != TFUNCTION) return 1;
458 puts ("t: CAR (cell_fun)\n");
459 if (CAR (cell_fun) != 33) return 1;
461 puts ("t: CDR (cell_fun)\n");
462 if (CDR (cell_fun) != g_function) return 1;
464 return make_tmps_test (g_cells);
479 puts ("t: for (i=1; i<5; ++i)\n");
481 if (i != 5) return i;
483 puts ("t: while (i<3) i++\n");
486 if (i != 3) return i;
488 puts ("t: do i-- while (i>0)\n");
490 if (i != 0) return 1;
492 puts ("t: if (0)\n");
498 puts ("t: else 1\n");
505 puts ("t: else if 2\n");
507 puts ("t: if (f)\n");
510 puts ("t: if (one != 1)\n");
511 if (one != 1) return 1;
513 puts ("t: if (1 != one)\n");
514 if (1 != one) return 1;
516 puts ("t: if (one > 1)\n");
517 if (one > 1) return 1;
519 puts ("t: if (one < 0)\n");
520 if (one < 0) return 1;
522 puts ("t: if (one <= 0)\n");
523 if (one <= 0) return 1;
525 puts ("t: if (one >= 2)\n");
526 if (one >= 2) return 1;
528 puts ("t: if (strlen (\"\"))\n");
529 if (strlen ("")) return 1;
531 puts ("t: if (strlen (p) != 4)\n");
532 if (strlen (p) != 4) return 1;
534 puts ("t: if (!strlen (\".\"))\n");
535 if (!strlen (".")) return 1;
537 puts ("t: if (strcmp (p, \"foo\"))\n");
538 if (!strcmp (p, "foo")) return 1;
540 puts ("t: if (strcmp (p, \"t.c\\n\"))\n");
541 if (strcmp (p, "t.c\n")) return 1;
543 puts ("t: if (!1)\n");
546 puts ("t: if (one == 0)\n");
547 if (one == 0) return 1;
549 puts ("t: if (f != 0)\n");
550 if (one != 1) return 1;
552 puts ("t: if (1 && 0)\n");
553 if (1 && 0) return 1;
555 puts ("t: if (!t && f)\n");
556 if (!t && f) return 1;
558 puts ("t: if (t && !one)\n");
559 if (t && !one) return 1;
561 puts ("t: if (f || !t)\n");
562 if (f || !t) return 1;
564 puts ("t: if (i++)\n");
567 puts ("t: if (--i)\n");
570 puts ("t: i += 2\n");
572 if (i != 2) return 1;
574 puts ("t: i -= 2\n");
576 if (i != 0) return 1;
578 puts ("t: if (f = 0) ?\n");
581 puts ("t: if (!(t = 1)) ?\n");
582 if (!(t = 1)) return 1;
584 puts ("t: if ((f = 0) != 0) ?\n");
585 if ((f = 0) != 0) return 1;
587 puts ("t: if ((t = 1) != 1) ?\n");
588 if ((t = 1) != 1) return 1;
590 puts ("t: (one == 1) ?\n");
591 (one == 1) ? 1 : exit (1);
596 puts ("t: assert (1) ?\n");
599 puts ("t: assert (f==0) ?\n");
602 puts ("t: p[0] != 't'\n");
603 if (p[0] != 't') return p[0];
605 puts ("t: p[i] != 't'\n");
606 if (p[i] != 't') return p[i];
608 puts ("t: identity (p[i]) != 't'\n");
609 if (identity (p[i]) != 't') return identity (p[i]);
611 puts ("t: *g_chars != 'A'\n");
613 if (*g_chars != 'A') return 1;
615 puts ("t: *x != 'A'\n");
616 if (*x != 'A') return 1;
618 puts ("t: *y != 'A'\n");
619 if (*y != 'A') return 1;
621 puts ("t: *x != 'Q'\n");
623 if (*x != 'Q') return 1;
625 puts ("t: *x++ != 'C'\n");
627 if (*g_chars != 'C') return 1;
630 if (1 + 2 != 3) return 1;
633 if (2 - 1 != 1) return 1;
635 puts ("t: 1 << 3\n");
636 if (1 << 3 != 8) return 1;
638 puts ("t: 8 >> 3\n");
639 if (8 >> 3 != 1) return 1;
642 if (8 / 4 != 2) return 1;
644 puts ("t: inc (0)\n");
645 if (inc (0) != 1) return 1;
647 puts ("t: inc (inc (0))\n");
648 if (inc (inc (0)) != 2) return 1;
650 puts ("t: inc (inc (inc (0)))\n");
651 if (inc (inc (inc (0))) != 3) return 1;
653 puts ("t: add (1, 2)\n");
654 if (add (1, 2) != 3) return 1;
656 puts ("t: add (inc (0), inc (1))\n");
657 if (add (inc (0), inc (1)) != 3) return 1;
659 puts ("t: add (TSTRING, 3)\n");
660 if (add (TSTRING, 3) != 13) return 1;
662 puts ("t: add (inc (inc (0)), inc (inc (1)))\n");
663 if (add (inc (inc (0)), inc (inc (1))) != 5) return 1;
665 puts ("t: goto label\n");
666 if (label (1) != 0) return 1;
668 puts ("t: switch 0\n");
669 if (swits (0) != 0) return swits (0);
671 puts ("t: switch 1\n");
672 if (swits (1) != 1) return 1;
674 puts ("t: switch -1\n");
675 if (swits (-1) != 2) return 1;
677 puts ("t: if (1)\n");
682 puts ("t: while (1) { goto label; };\n");
688 puts ("t: if (0); return 1; else;\n");
689 if (0) return 1; else goto ok01;
692 puts ("t: if (t)\n");
697 puts ("t: if (one > 0)\n");
698 if (one > 0) goto ok2;
702 puts ("t: if (one < 2)\n");
703 if (one < 2) goto ok3;
707 puts ("t: if (one >= 0)\n");
708 if (one >= 0) goto ok30;
712 puts ("t: if (one >= 1)\n");
713 if (one >= 0) goto ok31;
717 puts ("t: if (one <= 2)\n");
718 if (one <= 2) goto ok32;
722 puts ("t: if (one <= 1)\n");
723 if (one <= 1) goto ok33;
727 puts ("t: if (strlen (\".\"))\n");
728 if (strlen (".")) goto ok4;
732 puts ("t: if (strlen (p) == 4)\n");
733 if (strlen (p) == 4) goto ok40;
736 puts ("t: if (!strcmp (p, \"t.c\\n\"))\n");
737 if (!strcmp (p, "t.c\n")) goto ok41;
741 puts ("t: if (strcmp (p, \"foo\"))\n");
742 if (strcmp (p, "foo")) goto ok42;
746 puts ("t: if (!0)\n");
751 puts ("t: if (one == 1)\n");
752 if (one == 1) goto ok6;
756 puts ("t: if (one != 0)\n");
757 if (one != 0) goto ok7;
761 puts ("t: if (1 && !0)\n");
762 if (1 && !0) goto ok8;
766 puts ("t: if (f || t)\n");
767 if (f || t) goto ok80;
771 puts ("t: if (++i)\n");
776 puts ("t: if (i--)\n");
781 puts ("t: *g_chars == 'B'\n");
783 if (*g_chars == 'B') goto ok11;
787 puts ("t: *x == 'B'\n");
789 if (*x == 'B') goto ok12;
793 puts ("t: *y == 'B'\n");
795 if (*y == 'B') goto ok13;
799 puts ("t: *x == 'R'\n");
801 if (*x == 'R') goto ok14;
805 puts ("t: *x++ == 'C'\n");
807 if (*g_chars == 'C') goto ok15;
811 puts ("t: itoa (33) == \"33\"\n");
812 if (strcmp (itoa (33), "33")) return 1;
814 return struct_test ();
819 main (int argc, char *argv[])
824 if (argc > 1 && !strcmp (argv[1], "--help")) return 1;
825 puts ("t: if (argc > 1 && !strcmp (argv[1], \"--help\")\n");
828 if (argc > 1) if (!strcmp (argv[1], "--help")) return 1;