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/>.
29 : // no outputs "=" (r)
37 write (int fd, char const* s, int n)
40 //syscall (SYS_write, fd, s, n));
48 : // no outputs "=" (r)
49 : "" (fd), "" (s), "" (n)
50 : "eax", "ebx", "ecx", "edx"
58 strlen (char const* s)
68 //write (STDOUT, s, strlen (s));
69 //int i = write (STDOUT, s, strlen (s));
78 //write (STDOUT, s, strlen (s));
79 //int i = write (STDOUT, s, strlen (s));
80 write (1, (char*)&c, 1);
85 strcmp (char const* a, char const* b)
87 while (*a && *b && *a == *b) {a++;b++;}
99 struct scm *g_cells = arena;
100 char *g_chars = arena;
103 int foo () {puts ("t: foo\n"); return 0;};
104 int bar (int i) {puts ("t: bar\n"); return 0;};
106 int (*function) (void);
109 struct function g_fun = {&exit, 1};
110 struct function g_foo = {&foo, 0};
111 struct function g_bar = {&bar, 1};
113 //void *functions[2];
116 struct function g_functions[2];
119 enum type_t {CHAR, CLOSURE, CONTINUATION, TFUNCTION, KEYWORD, MACRO, NUMBER, PAIR, REF, SPECIAL, TSTRING, SYMBOL, VALUES, TVECTOR, BROKEN_HEART};
126 int ARENA_SIZE = 200;
127 #define TYPE(x) (g_cells[x].type)
128 #define CAR(x) g_cells[x].car
129 #define CDR(x) g_cells[x].cdr
130 #define VALUE(x) g_cells[x].cdr
132 struct scm scm_fun = {TFUNCTION,0,0};
153 if (c == 0) return c;
166 case CHAR: {goto next;}
169 default: {goto next;}
208 puts ("read test\n");
209 char *p = (char*)g_chars;
211 puts ("t: read 0123456789\n");
220 if (strcmp (g_chars, "0123456789")) return 1;
246 make_cell (SCM type, SCM car, SCM cdr)
249 TYPE (x) = VALUE (type);
250 if (VALUE (type) == CHAR || VALUE (type) == NUMBER) {
251 if (car) CAR (x) = CAR (car);
252 if (cdr) CDR(x) = CDR(cdr);
254 else if (VALUE (type) == TFUNCTION) {
255 if (car) CAR (x) = car;
256 if (cdr) CDR(x) = CDR(cdr);
268 VALUE (tmp_num) = PAIR;
269 make_cell (tmp_num, 0, 1);
274 make_tmps_test (struct scm* cells)
276 puts ("t: tmp = g_free++\n");
278 puts ("t: cells[tmp].type = CHAR\n");
279 cells[tmp].type = CHAR;
281 cells[tmp_num].type = NUMBER;
283 return make_cell_test();
289 g_cells[3].type = 0x64;
290 if (g_cells[3].type != 0x64)
291 return g_cells[3].type;
302 puts ("t: struct fun = {&exit, 1};\n");
303 struct function fun = {&exit, 1};
305 puts ("t: g_fun.arity != 1;\n");
306 if (g_fun.arity != 1) return 1;
308 puts ("t: g_fun.function != exit;\n");
309 if (g_fun.function != &exit) return 1;
311 puts ("t: fun.arity != 1;\n");
312 if (fun.arity != 1) return 1;
314 puts ("t: fun.function != exit;\n");
315 if (fun.function != &exit) return 1;
317 puts ("t: g_functions[g_function++] = g_foo;\n");
318 g_functions[g_function++] = g_foo;
321 puts ("t: g_functions[g_cells[fn].cdr].arity\n");
324 if (g_functions[g_cells[fn].cdr].arity) return 1;
326 if (g_functions[g_cells[fn].cdr].arity != 0) return 1;
328 int (*functionx) (void) = 0;
329 functionx = g_functions[0].function;
330 puts ("t: *functionx == foo\n");
331 if (functionx != foo) return 11;
333 puts ("t: (*functionx) () == foo\n");
334 if ((functionx) () != 0) return 12;
336 puts ("t: g_functions[<foo>].arity\n");
337 if (g_functions[0].arity != 0) return 17;
340 g_functions[fn] = g_bar;
341 g_cells[fn].cdr = fn;
342 if (g_cells[fn].cdr != fn) return 13;
344 puts ("t: g_functions[g_cells[fn].cdr].function\n");
345 functionx = g_functions[g_cells[fn].cdr].function;
347 puts ("t: functionx == bar\n");
348 if (functionx != bar) return 15;
350 puts ("t: (*functiony) (1) == bar\n");
353 int (*functiony) (int) = 0;
354 functiony = g_functions[g_cells[fn].cdr].function;
355 if ((functiony) (1) != 0) return 16;
358 functionx = g_functions[g_cells[fn].cdr].function;
359 if ((functionx) (1) != 0) return 16;
362 puts ("t: g_functions[<bar>].arity;");
363 if (g_functions[fn].arity != 1) return 18;
365 scm_fun.cdr = g_function;
366 g_functions[g_function++] = g_fun;
368 g_cells[cell_fun] = scm_fun;
370 return make_tmps_test (g_cells);
385 puts ("t: if (0)\n");
391 puts ("t: else 1\n");
398 puts ("t: else if 2\n");
400 puts ("t: if (f)\n");
403 puts ("t: if (one > 1)\n");
404 if (one > 1) return 1;
406 puts ("t: if (one < 0)\n");
407 if (one < 0) return 1;
409 puts ("t: if (strlen (\"\"))\n");
410 if (strlen ("")) return 1;
412 puts ("t: if (strlen (p) != 4)\n");
413 if (strlen (p) != 4) return 1;
415 puts ("t: if (!strlen (\".\"))\n");
416 if (!strlen (".")) return 1;
418 puts ("t: if (strcmp (p, \"foo\"))\n");
419 if (!strcmp (p, "foo")) return 1;
421 puts ("t: if (strcmp (p, \"t.c\\n\"))\n");
422 if (strcmp (p, "t.c\n")) return 1;
424 puts ("t: if (!1)\n");
427 puts ("t: if (one == 0)\n");
428 if (one == 0) return 1;
430 puts ("t: if (f != 0)\n");
431 if (one != 1) return 1;
433 puts ("t: if (1 && 0)\n");
434 if (1 && 0) return 1;
436 puts ("t: if (!t && f)\n");
437 if (!t && f) return 1;
439 puts ("t: if (t && !one)\n");
440 if (t && !one) return 1;
442 puts ("t: if (f || !t)\n");
443 if (f || !t) return 1;
445 puts ("t: if (i++)\n");
448 puts ("t: if (--i)\n");
451 puts ("t: i += 2\n");
453 if (i != 2) return 1;
455 puts ("t: i -= 2\n");
457 if (i != 0) return 1;
459 puts ("t: (one == 1) ?\n");
460 (one == 1) ? 1 : exit (1);
465 puts ("t: *g_chars != 'A'\n");
467 if (*g_chars != 'A') return 1;
469 puts ("t: *x != 'A'\n");
470 if (*x != 'A') return 1;
472 puts ("t: *y != 'A'\n");
473 if (*y != 'A') return 1;
475 puts ("t: *x != 'Q'\n");
477 if (*x != 'Q') return 1;
479 puts ("t: *x++ != 'C'\n");
481 if (*g_chars != 'C') return 1;
484 if (1 + 2 != 3) return 1;
487 if (2 - 1 != 1) return 1;
489 puts ("t: 1 << 3\n");
490 if (1 << 3 != 8) return 1;
493 if (8 / 4 != 2) return 1;
495 puts ("t: inc (0)\n");
496 if (inc (0) != 1) return 1;
498 puts ("t: inc (inc (0))\n");
499 if (inc (inc (0)) != 2) return 1;
501 puts ("t: inc (inc (inc (0)))\n");
502 if (inc (inc (inc (0))) != 3) return 1;
504 puts ("t: add (1, 2)\n");
505 if (add (1, 2) != 3) return 1;
507 puts ("t: add (inc (0), inc (1))\n");
508 if (add (inc (0), inc (1)) != 3) return 1;
510 puts ("t: add (inc (inc (0)), inc (inc (1)))\n");
511 if (add (inc (inc (0)), inc (inc (1))) != 5) return 1;
513 puts ("t: goto label\n");
514 if (label (1) != 0) return 1;
516 puts ("t: switch 0\n");
517 if (swits (0) != 0) return swits (0);
519 puts ("t: switch 1\n");
520 if (swits (1) != 1) return 1;
522 puts ("t: switch -1\n");
523 if (swits (-1) != 2) return 1;
525 puts ("t: if (1)\n");
530 puts ("t: if (0); return 1; else;\n");
531 if (0) return 1; else goto ok01;
534 puts ("t: if (t)\n");
539 puts ("t: if (one > 0)\n");
540 if (one > 0) goto ok2;
544 puts ("t: if (one < 2)\n");
545 //if (one < 2) goto ok3;
546 if (one < 0x44) goto ok3;
550 puts ("t: if (strlen (\".\"))\n");
551 if (strlen (".")) goto ok4;
555 puts ("t: if (strlen (p) == 4)\n");
556 if (strlen (p) == 4) goto ok40;
559 puts ("t: if (!strcmp (p, \"t.c\\n\"))\n");
560 if (!strcmp (p, "t.c\n")) goto ok41;
564 puts ("t: if (strcmp (p, \"foo\"))\n");
565 if (strcmp (p, "foo")) goto ok42;
569 puts ("t: if (!0)\n");
574 puts ("t: if (one == 1)\n");
575 if (one == 1) goto ok6;
579 puts ("t: if (one != 0)\n");
580 if (one != 0) goto ok7;
584 puts ("t: if (1 && !0)\n");
585 if (1 && !0) goto ok8;
589 puts ("t: if (f || t)\n");
590 if (f || t) goto ok80;
594 puts ("t: if (++i)\n");
599 puts ("t: if (i--)\n");
604 puts ("t: *g_chars == 'B'\n");
606 if (*g_chars == 'B') goto ok11;
610 puts ("t: *x == 'B'\n");
612 if (*x == 'B') goto ok12;
616 puts ("t: *y == 'B'\n");
618 if (*y == 'B') goto ok13;
622 puts ("t: *x == 'R'\n");
624 if (*x == 'R') goto ok14;
628 puts ("t: *x++ == 'C'\n");
630 if (*g_chars == 'C') goto ok15;
634 puts ("t: for (i=1; i<5; ++i)\n");
636 if (i != 5) return i;
638 return struct_test ();
643 main (int argc, char *argv[])
646 // g_functions[fn] = g_bar;
647 // if (g_functions[fn].arity != 1) return 1;
651 if (argc > 1 && !strcmp (argv[1], "--help")) return 1;
652 puts ("t: if (argc > 1 && !strcmp (argv[1], \"--help\")\n");
655 if (argc > 1) if (!strcmp (argv[1], "--help")) return 1;
670 "mov %%ebp,%%eax\n\t"
674 "mov %%ebp,%%eax\n\t"
676 "movzbl (%%eax),%%eax\n\t"
683 : //no inputs "" (&main)