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;
223 if ((1 | 4) != 5) return 1 | 4;
237 make_cell (SCM type, SCM car, SCM cdr)
240 TYPE (x) = VALUE (type);
241 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
242 if (car) CAR (x) = CAR (car);
243 if (cdr) CDR(x) = CDR(cdr);
245 else if (VALUE (type) == TFUNCTION) {
246 if (car) CAR (x) = car;
247 if (cdr) CDR(x) = CDR(cdr);
259 VALUE (tmp_num) = TPAIR;
260 make_cell (tmp_num, 0, 1);
265 make_tmps_test (struct scm* cells)
267 puts ("t: tmp = g_free++\n");
269 puts ("t: cells[tmp].type = CHAR\n");
270 cells[tmp].type = TCHAR;
272 cells[tmp_num].type = TNUMBER;
274 return make_cell_test();
280 g_cells[0].type = TNUMBER;
281 g_cells[1].type = TNUMBER;
282 puts ("t: TYPE (0) != TYPE (1)\n");
283 if (TYPE (0) == TYPE (1)) goto ok;
290 puts ("t: int c = VALUE (0)\n");
292 if (c != 1) return 1;
294 puts ("t: CAAR (0) != 2\n");
295 if (CAAR (0) != 2) return 1;
297 puts ("t: 2 != CAAR (0)\n");
298 if (2 != CAAR (0)) return 1;
300 g_cells[3].type = 0x64;
301 if (g_cells[3].type != 0x64)
302 return g_cells[3].type;
313 puts ("t: g_fun.arity != 1;\n");
314 if (g_fun.arity != 1) return 1;
316 puts ("t: g_fun.function != exit;\n");
317 if (g_fun.function != &exit) return 1;
319 puts ("t: struct fun = {&exit,1,\"exit\"};\n");
320 struct function fun = {&exit,1,"exit"};
322 puts ("t: fun.arity != 1;\n");
323 if (fun.arity != 1) return 1;
325 puts ("t: fun.function != exit;\n");
326 if (fun.function != &exit) return 1;
328 puts ("t: puts (fun.name)\n");
329 if (strcmp (fun.name, "exit")) return 1;
331 puts ("t: puts (g_fun.name)\n");
332 if (strcmp (g_fun.name, "fun")) return 1;
334 puts ("t: g_functions[g_function++] = g_foo;\n");
335 g_functions[g_function++] = g_foo;
338 puts ("t: g_functions[g_cells[fn].cdr].arity\n");
339 if (g_functions[g_cells[fn].cdr].arity) return 1;
340 if (g_functions[g_cells[fn].cdr].arity != 0) return 1;
342 int (*functionx) (void) = 0;
343 functionx = g_functions[0].function;
344 puts ("t: functionx == foo\n");
345 if (functionx != foo) return 11;
347 puts ("t: g_functions[0].name\n");
348 if (strcmp (g_functions[0].name, "foo")) return 1;
350 puts ("t: (functionx) () == foo\n");
351 if ((functionx) () != 0) return 12;
353 puts ("t: g_functions[<foo>].arity\n");
354 if (g_functions[0].arity != 0) return 17;
357 g_functions[fn] = g_bar;
358 g_cells[fn].cdr = fn;
359 if (g_cells[fn].cdr != fn) return 13;
361 puts ("t: g_functions[g_cells[fn].cdr].function\n");
362 functionx = g_functions[g_cells[fn].cdr].function;
364 puts ("t: g_functions[1].name\n");
365 if (strcmp (g_functions[1].name, "bar")) return 1;
367 puts ("t: functionx == bar\n");
368 if (functionx != bar) return 15;
370 puts ("t: (functiony) (1) == bar\n");
371 int (*functiony) (int) = 0;
372 functiony = g_functions[g_cells[fn].cdr].function;
373 if ((functiony) (1) != 0) return 16;
375 puts ("t: g_functions[<bar>].arity\n");
376 if (g_functions[fn].arity != 1) return 18;
380 scm_fun.cdr = g_function;
381 //g_functions[g_function++] = g_fun;
382 g_functions[g_function] = g_fun;
384 g_cells[cell_fun] = scm_fun;
386 puts ("t: TYPE (cell_fun)\n");
387 if (TYPE (cell_fun) != TFUNCTION) return 1;
389 puts ("t: CAR (cell_fun)\n");
390 if (CAR (cell_fun) != 33) return 1;
392 puts ("t: CDR (cell_fun)\n");
393 if (CDR (cell_fun) != g_function) return 1;
395 return make_tmps_test (g_cells);
410 puts ("t: for (i=1; i<5; ++i)\n");
412 if (i != 5) return i;
414 puts ("t: while (i<3) i++\n");
417 if (i != 3) return i;
419 puts ("t: do i-- while (i>0)\n");
421 if (i != 0) return 1;
423 puts ("t: if (0)\n");
429 puts ("t: else 1\n");
436 puts ("t: else if 2\n");
438 puts ("t: if (f)\n");
441 puts ("t: if (one != 1)\n");
442 if (one != 1) return 1;
444 puts ("t: if (1 != one)\n");
445 if (1 != one) return 1;
447 puts ("t: if (one > 1)\n");
448 if (one > 1) return 1;
450 puts ("t: if (one < 0)\n");
451 if (one < 0) return 1;
453 puts ("t: if (one <= 0)\n");
454 if (one <= 0) return 1;
456 puts ("t: if (one >= 2)\n");
457 if (one >= 2) return 1;
459 puts ("t: if (strlen (\"\"))\n");
460 if (strlen ("")) return 1;
462 puts ("t: if (strlen (p) != 4)\n");
463 if (strlen (p) != 4) return 1;
465 puts ("t: if (!strlen (\".\"))\n");
466 if (!strlen (".")) return 1;
468 puts ("t: if (strcmp (p, \"foo\"))\n");
469 if (!strcmp (p, "foo")) return 1;
471 puts ("t: if (strcmp (p, \"t.c\\n\"))\n");
472 if (strcmp (p, "t.c\n")) return 1;
474 puts ("t: if (!1)\n");
477 puts ("t: if (one == 0)\n");
478 if (one == 0) return 1;
480 puts ("t: if (f != 0)\n");
481 if (one != 1) return 1;
483 puts ("t: if (1 && 0)\n");
484 if (1 && 0) return 1;
486 puts ("t: if (!t && f)\n");
487 if (!t && f) return 1;
489 puts ("t: if (t && !one)\n");
490 if (t && !one) return 1;
492 puts ("t: if (f || !t)\n");
493 if (f || !t) return 1;
495 puts ("t: if (i++)\n");
498 puts ("t: if (--i)\n");
501 puts ("t: i += 2\n");
503 if (i != 2) return 1;
505 puts ("t: i -= 2\n");
507 if (i != 0) return 1;
509 puts ("t: if (f = 0) ?\n");
512 puts ("t: if (!(t = 1)) ?\n");
513 if (!(t = 1)) return 1;
515 puts ("t: if ((f = 0) != 0) ?\n");
516 if ((f = 0) != 0) return 1;
518 puts ("t: if ((t = 1) != 1) ?\n");
519 if ((t = 1) != 1) return 1;
521 puts ("t: (one == 1) ?\n");
522 (one == 1) ? 1 : exit (1);
527 puts ("t: assert (1) ?\n");
530 puts ("t: assert (f==0) ?\n");
533 puts ("t: p[0] != 't'\n");
534 if (p[0] != 't') return p[0];
536 puts ("t: p[i] != 't'\n");
537 if (p[i] != 't') return p[i];
539 puts ("t: identity (p[i]) != 't'\n");
540 if (identity (p[i]) != 't') return identity (p[i]);
542 puts ("t: *g_chars != 'A'\n");
544 if (*g_chars != 'A') return 1;
546 puts ("t: *x != 'A'\n");
547 if (*x != 'A') return 1;
549 puts ("t: *y != 'A'\n");
550 if (*y != 'A') return 1;
552 puts ("t: *x != 'Q'\n");
554 if (*x != 'Q') return 1;
556 puts ("t: *x++ != 'C'\n");
558 if (*g_chars != 'C') return 1;
561 if (1 + 2 != 3) return 1;
564 if (2 - 1 != 1) return 1;
566 puts ("t: 1 << 3\n");
567 if (1 << 3 != 8) return 1;
570 if (8 / 4 != 2) return 1;
572 puts ("t: inc (0)\n");
573 if (inc (0) != 1) return 1;
575 puts ("t: inc (inc (0))\n");
576 if (inc (inc (0)) != 2) return 1;
578 puts ("t: inc (inc (inc (0)))\n");
579 if (inc (inc (inc (0))) != 3) return 1;
581 puts ("t: add (1, 2)\n");
582 if (add (1, 2) != 3) return 1;
584 puts ("t: add (inc (0), inc (1))\n");
585 if (add (inc (0), inc (1)) != 3) return 1;
587 puts ("t: add (TSTRING, 3)\n");
588 if (add (TSTRING, 3) != 13) return 1;
590 puts ("t: add (inc (inc (0)), inc (inc (1)))\n");
591 if (add (inc (inc (0)), inc (inc (1))) != 5) return 1;
593 puts ("t: goto label\n");
594 if (label (1) != 0) return 1;
596 puts ("t: switch 0\n");
597 if (swits (0) != 0) return swits (0);
599 puts ("t: switch 1\n");
600 if (swits (1) != 1) return 1;
602 puts ("t: switch -1\n");
603 if (swits (-1) != 2) return 1;
605 puts ("t: if (1)\n");
610 puts ("t: while (1) { goto label; };\n");
616 puts ("t: if (0); return 1; else;\n");
617 if (0) return 1; else goto ok01;
620 puts ("t: if (t)\n");
625 puts ("t: if (one > 0)\n");
626 if (one > 0) goto ok2;
630 puts ("t: if (one < 2)\n");
631 if (one < 2) goto ok3;
635 puts ("t: if (one >= 0)\n");
636 if (one >= 0) goto ok30;
640 puts ("t: if (one >= 1)\n");
641 if (one >= 0) goto ok31;
645 puts ("t: if (one <= 2)\n");
646 if (one <= 2) goto ok32;
650 puts ("t: if (one <= 1)\n");
651 if (one <= 1) goto ok33;
655 puts ("t: if (strlen (\".\"))\n");
656 if (strlen (".")) goto ok4;
660 puts ("t: if (strlen (p) == 4)\n");
661 if (strlen (p) == 4) goto ok40;
664 puts ("t: if (!strcmp (p, \"t.c\\n\"))\n");
665 if (!strcmp (p, "t.c\n")) goto ok41;
669 puts ("t: if (strcmp (p, \"foo\"))\n");
670 if (strcmp (p, "foo")) goto ok42;
674 puts ("t: if (!0)\n");
679 puts ("t: if (one == 1)\n");
680 if (one == 1) goto ok6;
684 puts ("t: if (one != 0)\n");
685 if (one != 0) goto ok7;
689 puts ("t: if (1 && !0)\n");
690 if (1 && !0) goto ok8;
694 puts ("t: if (f || t)\n");
695 if (f || t) goto ok80;
699 puts ("t: if (++i)\n");
704 puts ("t: if (i--)\n");
709 puts ("t: *g_chars == 'B'\n");
711 if (*g_chars == 'B') goto ok11;
715 puts ("t: *x == 'B'\n");
717 if (*x == 'B') goto ok12;
721 puts ("t: *y == 'B'\n");
723 if (*y == 'B') goto ok13;
727 puts ("t: *x == 'R'\n");
729 if (*x == 'R') goto ok14;
733 puts ("t: *x++ == 'C'\n");
735 if (*g_chars == 'C') goto ok15;
739 puts ("t: itoa (33) == \"33\"\n");
740 if (strcmp (itoa (33), "33")) return 1;
742 return struct_test ();
747 main (int argc, char *argv[])
752 if (argc > 1 && !strcmp (argv[1], "--help")) return 1;
753 puts ("t: if (argc > 1 && !strcmp (argv[1], \"--help\")\n");
756 if (argc > 1) if (!strcmp (argv[1], "--help")) return 1;