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;
186 puts ("t: -1 < -2\n");
187 if (-1 < -2) return 1;
189 puts ("t: 0 < -1\n");
190 if (0 < -1) return 1;
198 puts ("t: -2 > -1\n");
199 if (-2 > -1) return 1;
201 puts ("t: -1 > 0\n");
202 if (-1 > 0) return 1;
216 puts ("t: 1 << 3\n");
217 if (1 << 3 != 8) return 1 << 3;
219 puts ("t: 3 << 4\n");
220 if (3 << 4 != 48) return 3 << 4;
222 puts ("t: 48 >> 3\n");
223 if (48 >> 4 != 3) return 48 >> 4;
225 puts ("t: 10 >> 1\n");
226 if (10 >> 1 != 5) return 10 >> 1;
229 if ((1 | 4) != 5) return 1 | 4;
233 if (-i != 3) return 1;
235 puts ("t: -1 + 2\n");
236 if (-1 + 2 != 1) return 1;
250 make_cell (SCM type, SCM car, SCM cdr)
253 TYPE (x) = VALUE (type);
254 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
255 if (car) CAR (x) = CAR (car);
256 if (cdr) CDR(x) = CDR(cdr);
258 else if (VALUE (type) == TFUNCTION) {
259 if (car) CAR (x) = car;
260 if (cdr) CDR(x) = CDR(cdr);
272 VALUE (tmp_num) = TPAIR;
273 make_cell (tmp_num, 0, 1);
278 make_tmps_test (struct scm* cells)
280 puts ("t: tmp = g_free++\n");
282 puts ("t: cells[tmp].type = CHAR\n");
283 cells[tmp].type = TCHAR;
285 cells[tmp_num].type = TNUMBER;
287 return make_cell_test();
293 g_cells[0].type = TNUMBER;
294 g_cells[1].type = TNUMBER;
295 puts ("t: TYPE (0) != TYPE (1)\n");
296 if (TYPE (0) == TYPE (1)) goto ok;
303 puts ("t: int c = VALUE (0)\n");
305 if (c != 1) return 1;
307 puts ("t: CAAR (0) != 2\n");
308 if (CAAR (0) != 2) return 1;
310 puts ("t: 2 != CAAR (0)\n");
311 if (2 != CAAR (0)) return 1;
313 g_cells[3].type = 0x64;
314 if (g_cells[3].type != 0x64)
315 return g_cells[3].type;
326 puts ("t: g_fun.arity != 1;\n");
327 if (g_fun.arity != 1) return 1;
329 puts ("t: g_fun.function != exit;\n");
330 if (g_fun.function != &exit) return 1;
332 puts ("t: struct fun = {&exit,1,\"exit\"};\n");
333 struct function fun = {&exit,1,"exit"};
335 puts ("t: fun.arity != 1;\n");
336 if (fun.arity != 1) return 1;
338 puts ("t: fun.function != exit;\n");
339 if (fun.function != &exit) return 1;
341 puts ("t: puts (fun.name)\n");
342 if (strcmp (fun.name, "exit")) return 1;
344 puts ("t: puts (g_fun.name)\n");
345 if (strcmp (g_fun.name, "fun")) return 1;
347 puts ("t: g_functions[g_function++] = g_foo;\n");
348 g_functions[g_function++] = g_foo;
351 puts ("t: g_functions[g_cells[fn].cdr].arity\n");
352 if (g_functions[g_cells[fn].cdr].arity) return 1;
353 if (g_functions[g_cells[fn].cdr].arity != 0) return 1;
355 int (*functionx) (void) = 0;
356 functionx = g_functions[0].function;
357 puts ("t: functionx == foo\n");
358 if (functionx != foo) return 11;
360 puts ("t: g_functions[0].name\n");
361 if (strcmp (g_functions[0].name, "foo")) return 1;
363 puts ("t: (functionx) () == foo\n");
364 if ((functionx) () != 0) return 12;
366 puts ("t: g_functions[<foo>].arity\n");
367 if (g_functions[0].arity != 0) return 17;
370 g_functions[fn] = g_bar;
371 g_cells[fn].cdr = fn;
372 if (g_cells[fn].cdr != fn) return 13;
374 puts ("t: g_functions[g_cells[fn].cdr].function\n");
375 functionx = g_functions[g_cells[fn].cdr].function;
377 puts ("t: g_functions[1].name\n");
378 if (strcmp (g_functions[1].name, "bar")) return 1;
380 puts ("t: functionx == bar\n");
381 if (functionx != bar) return 15;
383 puts ("t: (functiony) (1) == bar\n");
384 int (*functiony) (int) = 0;
385 functiony = g_functions[g_cells[fn].cdr].function;
386 if ((functiony) (1) != 0) return 16;
388 puts ("t: g_functions[<bar>].arity\n");
389 if (g_functions[fn].arity != 1) return 18;
393 scm_fun.cdr = g_function;
394 //g_functions[g_function++] = g_fun;
395 g_functions[g_function] = g_fun;
397 g_cells[cell_fun] = scm_fun;
399 puts ("t: TYPE (cell_fun)\n");
400 if (TYPE (cell_fun) != TFUNCTION) return 1;
402 puts ("t: CAR (cell_fun)\n");
403 if (CAR (cell_fun) != 33) return 1;
405 puts ("t: CDR (cell_fun)\n");
406 if (CDR (cell_fun) != g_function) return 1;
408 return make_tmps_test (g_cells);
423 puts ("t: for (i=1; i<5; ++i)\n");
425 if (i != 5) return i;
427 puts ("t: while (i<3) i++\n");
430 if (i != 3) return i;
432 puts ("t: do i-- while (i>0)\n");
434 if (i != 0) return 1;
436 puts ("t: if (0)\n");
442 puts ("t: else 1\n");
449 puts ("t: else if 2\n");
451 puts ("t: if (f)\n");
454 puts ("t: if (one != 1)\n");
455 if (one != 1) return 1;
457 puts ("t: if (1 != one)\n");
458 if (1 != one) return 1;
460 puts ("t: if (one > 1)\n");
461 if (one > 1) return 1;
463 puts ("t: if (one < 0)\n");
464 if (one < 0) return 1;
466 puts ("t: if (one <= 0)\n");
467 if (one <= 0) return 1;
469 puts ("t: if (one >= 2)\n");
470 if (one >= 2) return 1;
472 puts ("t: if (strlen (\"\"))\n");
473 if (strlen ("")) return 1;
475 puts ("t: if (strlen (p) != 4)\n");
476 if (strlen (p) != 4) return 1;
478 puts ("t: if (!strlen (\".\"))\n");
479 if (!strlen (".")) return 1;
481 puts ("t: if (strcmp (p, \"foo\"))\n");
482 if (!strcmp (p, "foo")) return 1;
484 puts ("t: if (strcmp (p, \"t.c\\n\"))\n");
485 if (strcmp (p, "t.c\n")) return 1;
487 puts ("t: if (!1)\n");
490 puts ("t: if (one == 0)\n");
491 if (one == 0) return 1;
493 puts ("t: if (f != 0)\n");
494 if (one != 1) return 1;
496 puts ("t: if (1 && 0)\n");
497 if (1 && 0) return 1;
499 puts ("t: if (!t && f)\n");
500 if (!t && f) return 1;
502 puts ("t: if (t && !one)\n");
503 if (t && !one) return 1;
505 puts ("t: if (f || !t)\n");
506 if (f || !t) return 1;
508 puts ("t: if (i++)\n");
511 puts ("t: if (--i)\n");
514 puts ("t: i += 2\n");
516 if (i != 2) return 1;
518 puts ("t: i -= 2\n");
520 if (i != 0) return 1;
522 puts ("t: if (f = 0) ?\n");
525 puts ("t: if (!(t = 1)) ?\n");
526 if (!(t = 1)) return 1;
528 puts ("t: if ((f = 0) != 0) ?\n");
529 if ((f = 0) != 0) return 1;
531 puts ("t: if ((t = 1) != 1) ?\n");
532 if ((t = 1) != 1) return 1;
534 puts ("t: (one == 1) ?\n");
535 (one == 1) ? 1 : exit (1);
540 puts ("t: assert (1) ?\n");
543 puts ("t: assert (f==0) ?\n");
546 puts ("t: p[0] != 't'\n");
547 if (p[0] != 't') return p[0];
549 puts ("t: p[i] != 't'\n");
550 if (p[i] != 't') return p[i];
552 puts ("t: identity (p[i]) != 't'\n");
553 if (identity (p[i]) != 't') return identity (p[i]);
555 puts ("t: *g_chars != 'A'\n");
557 if (*g_chars != 'A') return 1;
559 puts ("t: *x != 'A'\n");
560 if (*x != 'A') return 1;
562 puts ("t: *y != 'A'\n");
563 if (*y != 'A') return 1;
565 puts ("t: *x != 'Q'\n");
567 if (*x != 'Q') return 1;
569 puts ("t: *x++ != 'C'\n");
571 if (*g_chars != 'C') return 1;
574 if (1 + 2 != 3) return 1;
577 if (2 - 1 != 1) return 1;
579 puts ("t: 1 << 3\n");
580 if (1 << 3 != 8) return 1;
582 puts ("t: 8 >> 3\n");
583 if (8 >> 3 != 1) return 1;
586 if (8 / 4 != 2) return 1;
588 puts ("t: inc (0)\n");
589 if (inc (0) != 1) return 1;
591 puts ("t: inc (inc (0))\n");
592 if (inc (inc (0)) != 2) return 1;
594 puts ("t: inc (inc (inc (0)))\n");
595 if (inc (inc (inc (0))) != 3) return 1;
597 puts ("t: add (1, 2)\n");
598 if (add (1, 2) != 3) return 1;
600 puts ("t: add (inc (0), inc (1))\n");
601 if (add (inc (0), inc (1)) != 3) return 1;
603 puts ("t: add (TSTRING, 3)\n");
604 if (add (TSTRING, 3) != 13) return 1;
606 puts ("t: add (inc (inc (0)), inc (inc (1)))\n");
607 if (add (inc (inc (0)), inc (inc (1))) != 5) return 1;
609 puts ("t: goto label\n");
610 if (label (1) != 0) return 1;
612 puts ("t: switch 0\n");
613 if (swits (0) != 0) return swits (0);
615 puts ("t: switch 1\n");
616 if (swits (1) != 1) return 1;
618 puts ("t: switch -1\n");
619 if (swits (-1) != 2) return 1;
621 puts ("t: if (1)\n");
626 puts ("t: while (1) { goto label; };\n");
632 puts ("t: if (0); return 1; else;\n");
633 if (0) return 1; else goto ok01;
636 puts ("t: if (t)\n");
641 puts ("t: if (one > 0)\n");
642 if (one > 0) goto ok2;
646 puts ("t: if (one < 2)\n");
647 if (one < 2) goto ok3;
651 puts ("t: if (one >= 0)\n");
652 if (one >= 0) goto ok30;
656 puts ("t: if (one >= 1)\n");
657 if (one >= 0) goto ok31;
661 puts ("t: if (one <= 2)\n");
662 if (one <= 2) goto ok32;
666 puts ("t: if (one <= 1)\n");
667 if (one <= 1) goto ok33;
671 puts ("t: if (strlen (\".\"))\n");
672 if (strlen (".")) goto ok4;
676 puts ("t: if (strlen (p) == 4)\n");
677 if (strlen (p) == 4) goto ok40;
680 puts ("t: if (!strcmp (p, \"t.c\\n\"))\n");
681 if (!strcmp (p, "t.c\n")) goto ok41;
685 puts ("t: if (strcmp (p, \"foo\"))\n");
686 if (strcmp (p, "foo")) goto ok42;
690 puts ("t: if (!0)\n");
695 puts ("t: if (one == 1)\n");
696 if (one == 1) goto ok6;
700 puts ("t: if (one != 0)\n");
701 if (one != 0) goto ok7;
705 puts ("t: if (1 && !0)\n");
706 if (1 && !0) goto ok8;
710 puts ("t: if (f || t)\n");
711 if (f || t) goto ok80;
715 puts ("t: if (++i)\n");
720 puts ("t: if (i--)\n");
725 puts ("t: *g_chars == 'B'\n");
727 if (*g_chars == 'B') goto ok11;
731 puts ("t: *x == 'B'\n");
733 if (*x == 'B') goto ok12;
737 puts ("t: *y == 'B'\n");
739 if (*y == 'B') goto ok13;
743 puts ("t: *x == 'R'\n");
745 if (*x == 'R') goto ok14;
749 puts ("t: *x++ == 'C'\n");
751 if (*g_chars == 'C') goto ok15;
755 puts ("t: itoa (33) == \"33\"\n");
756 if (strcmp (itoa (33), "33")) return 1;
758 return struct_test ();
763 main (int argc, char *argv[])
768 if (argc > 1 && !strcmp (argv[1], "--help")) return 1;
769 puts ("t: if (argc > 1 && !strcmp (argv[1], \"--help\")\n");
772 if (argc > 1) if (!strcmp (argv[1], "--help")) return 1;