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/>.
22 #define FIXED_PRIMITIVES 0
31 #define NYACC_CAR nyacc_car
32 #define NYACC_CDR nyacc_cdr
42 void *malloc (size_t i);
43 int open (char const *s, int mode);
44 int read (int fd, void* buf, size_t n);
45 void write (int fd, char const* s, int n);
54 : // no outputs "=" (r)
62 getenv (char const* p)
68 read (int fd, void* buf, size_t n)
71 //syscall (SYS_write, fd, s, n));
80 : "" (fd), "" (buf), "" (n)
81 : "eax", "ebx", "ecx", "edx"
87 open (char const *s, int mode)
90 //syscall (SYS_open, mode));
108 int r = read (g_stdin, &c, 1);
109 if (r < 1) return -1;
114 write (int fd, char const* s, int n)
117 //syscall (SYS_write, fd, s, n));
123 "mov $0x4, %%eax\n\t"
125 : // no outputs "=" (r)
126 : "" (fd), "" (s), "" (n)
127 : "eax", "ebx", "ecx", "edx"
134 //write (STDOUT, s, strlen (s));
135 //int i = write (STDOUT, s, strlen (s));
136 write (1, (char*)&c, 1);
144 int len = size + sizeof (size);
145 //n = mmap (0, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0 );
154 //munmap ((void*)p, *n);
163 strlen (char const* s)
171 strcmp (char const* a, char const* b)
173 while (*a && *b && *a == *b) {a++;b++;}
180 //write (STDOUT, s, strlen (s));
181 //int i = write (STDOUT, s, strlen (s));
188 eputs (char const* s)
190 //write (STDERR, s, strlen (s));
191 //int i = write (STDERR, s, strlen (s));
210 *p-- = '0' + (x % 10);
222 assert_fail (char* s)
224 eputs ("assert fail:");
235 #define assert(x) ((x) ? (void)0 : assert_fail ("boo:" #x))
237 //#define assert(x) ((x) ? (void)0 : assert_fail ("boo:" #x))
238 #define assert(x) ((x) ? (void)0 : assert_fail (0))
260 #if __NYACC__ || FIXME_NYACC
261 enum type_t {CHAR, CLOSURE, CONTINUATION, TFUNCTION, KEYWORD, MACRO, NUMBER, PAIR, REF, SPECIAL, TSTRING, SYMBOL, VALUES, TVECTOR, BROKEN_HEART};
263 enum type_t {CHAR, CLOSURE, CONTINUATION, FUNCTION, KEYWORD, MACRO, NUMBER, PAIR, REF, SPECIAL, STRING, SYMBOL, VALUES, VECTOR, BROKEN_HEART};
272 typedef int (*f_t) (void);
274 int (*function) (void);
278 struct scm *g_cells = arena;
282 // struct scm scm_nil = {SPECIAL, "()"};
283 // struct scm scm_f = {SPECIAL, "#f"};
284 // struct scm scm_t = {SPECIAL, "#t"};
285 // struct scm_dot = {SPECIAL, "."};
286 // struct scm_arrow = {SPECIAL, "=>"};
287 // struct scm_undefined = {SPECIAL, "*undefined*"};
288 // struct scm_unspecified = {SPECIAL, "*unspecified*"};
289 // struct scm_closure = {SPECIAL, "*closure*"};
290 // struct scm_circular = {SPECIAL, "*circular*"};
291 // struct scm_begin = {SPECIAL, "*begin*"};
293 // struct scm_vm_apply = {SPECIAL, "core:apply"};
294 // struct scm_vm_apply2 = {SPECIAL, "*vm-apply2*"};
296 // struct scm_vm_eval = {SPECIAL, "core:eval"};
298 // struct scm_vm_begin = {SPECIAL, "*vm-begin*"};
299 // //scm scm_vm_begin_read_input_file = {SPECIAL, "*vm-begin-read-input-file*"};
300 // struct scm_vm_begin2 = {SPECIAL, "*vm-begin2*"};
302 // struct scm_vm_return = {SPECIAL, "*vm-return*"};
304 // //#include "mes.symbols.h"
310 // #define cell_arrow 5
311 #define cell_undefined 6
312 #define cell_unspecified 7
313 #define cell_closure 8
314 #define cell_circular 9
315 #define cell_begin 10
316 #define cell_symbol_dot 11
317 #define cell_symbol_lambda 12
318 #define cell_symbol_begin 13
319 #define cell_symbol_if 14
320 #define cell_symbol_quote 15
321 #define cell_symbol_set_x 16
323 #define cell_vm_apply 45
324 #define cell_vm_apply2 46
326 #define cell_vm_eval 47
328 #define cell_vm_begin 56
329 //#define cell_vm_begin_read_input_file 57
330 #define cell_vm_begin2 58
332 #define cell_vm_return 63
338 int ARENA_SIZE = 200;
339 struct function g_functions[5];
345 SCM make_cell (SCM type, SCM car, SCM cdr);
347 struct function fun_make_cell = {&make_cell, 3};
348 struct scm scm_make_cell = {TFUNCTION,0,0};
354 SCM cons (SCM x, SCM y);
356 struct function fun_cons = {&cons, 2};
357 struct scm scm_cons = {TFUNCTION,0,0};
365 struct function fun_car = {&car, 1};
366 struct scm scm_car = {TFUNCTION,0,0};
374 struct function fun_cdr = {&cdr, 1};
375 struct scm scm_cdr = {TFUNCTION,0,0};
379 // SCM eq_p (SCM x, SCM y);
380 // struct function fun_eq_p = {&eq_p, 2};
381 // scm scm_eq_p = {TFUNCTION,0,0};// "eq?", 0};
384 #define TYPE(x) (g_cells[x].type)
386 #define CAR(x) g_cells[x].car
387 #define LENGTH(x) g_cells[x].car
388 #define STRING(x) g_cells[x].car
390 #define CDR(x) g_cells[x].cdr
392 //#define CLOSURE(x) g_cells[x].closure
394 #define CONTINUATION(x) g_cells[x].cdr
396 //#define FUNCTION(x) g_functions[g_cells[x].function]
399 #define FUNCTION(x) g_functions[g_cells[x].cdr]
400 #define VALUE(x) g_cells[x].cdr
401 #define VECTOR(x) g_cells[x].cdr
403 #define MAKE_CHAR(n) make_cell (tmp_num_ (CHAR), 0, tmp_num2_ (n))
404 //#define MAKE_CONTINUATION(n) make_cell (tmp_num_ (CONTINUATION), n, g_stack)
405 #define MAKE_NUMBER(n) make_cell (tmp_num_ (NUMBER), 0, tmp_num2_ (n))
406 //#define MAKE_REF(n) make_cell (tmp_num_ (REF), n, 0)
409 #define CAAR(x) CAR (CAR (x))
410 // #define CDAR(x) CDR (CAR (x))
411 #define CADAR(x) CAR (CDR (CAR (x)))
412 // #define CADDR(x) CAR (CDR (CDR (x)))
413 // #define CDDDR(x) CDR (CDR (CDR (x)))
414 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
415 #define CADR(x) CAR (CDR (x))
418 #if __NYACC__ || FIXME_NYACC
419 #define MAKE_STRING(x) make_cell (tmp_num_ (TSTRING), x, 0)
421 // #define MAKE_STRING(x) make_cell (tmp_num_ (STRING), x, 0)
429 assert (g_free + n < ARENA_SIZE);
437 make_cell (SCM type, SCM car, SCM cdr)
442 assert (TYPE (type) == NUMBER);
444 TYPE (x) = VALUE (type);
445 if (VALUE (type) == CHAR || VALUE (type) == NUMBER) {
446 if (car) CAR (x) = CAR (car);
447 if (cdr) CDR(x) = CDR(cdr);
449 else if (VALUE (type) == TFUNCTION) {
450 if (car) CAR (x) = car;
451 if (cdr) CDR(x) = CDR(cdr);
470 VALUE (tmp_num2) = x;
482 VALUE (tmp_num) = PAIR;
483 return make_cell (tmp_num, x, y);
498 if (TYPE (x) != PAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_car));
515 if (TYPE (x) != PAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_cdr));
521 // eq_p (SCM x, SCM y)
524 // || ((TYPE (x) == KEYWORD && TYPE (y) == KEYWORD
525 // && STRING (x) == STRING (y)))
526 // || (TYPE (x) == CHAR && TYPE (y) == CHAR
527 // && VALUE (x) == VALUE (y))
528 // || (TYPE (x) == NUMBER && TYPE (y) == NUMBER
529 // && VALUE (x) == VALUE (y)))
530 // ? cell_t : cell_f;
536 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
537 g_stack = cons (frame, g_stack);
542 append2 (SCM x, SCM y)
544 if (x == cell_nil) return y;
547 assert (TYPE (x) == PAIR);
549 return cons (car (x), append2 (cdr (x), y));
553 pairlis (SCM x, SCM y, SCM a)
557 if (TYPE (x) != PAIR)
558 return cons (cons (x, y), a);
559 return cons (cons (car (x), car (y)),
560 pairlis (cdr (x), cdr (y), a));
566 //while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f) a = CDR (a);
567 while (a != cell_nil && x == CAAR (a)) a = CDR (a);
568 return a != cell_nil ? car (a) : cell_f;
574 assq_ref_env (SCM x, SCM a)
577 if (x == cell_f) return cell_undefined;
585 assert_defined (SCM x, SCM e)
587 if (e != cell_undefined) return e;
588 // error (cell_symbol_unbound_variable, x);
589 puts ("unbound variable");
596 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
606 return cell_unspecified;
610 SCM caar (SCM x) {return car (car (x));}
611 SCM cadr (SCM x) {return car (cdr (x));}
612 SCM cdar (SCM x) {return cdr (car (x));}
613 SCM cddr (SCM x) {return cdr (cdr (x));}
615 // Weirdness: wrong function labeling
616 // SCM cadr (SCM x) {
620 // SCM cddr (SCM x) {
636 // if (g_free + GC_SAFETY > ARENA_SIZE)
637 // gc_pop_frame (gc (gc_push_frame ()));
641 case cell_vm_apply: {goto apply;}
642 case cell_unspecified: {return r1;}
649 switch (TYPE (car (r1)))
652 puts ("apply.function\n");
653 //check_formals (car (r1), MAKE_NUMBER (FUNCTION (car (r1)).arity), cdr (r1));
654 r1 = call (car (r1), cdr (r1));
669 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
670 && x != cell_nil && TYPE (CAR (x)) == VALUES)
671 x = cons (CADAR (x), CDR (x));
672 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
673 && x != cell_nil && TYPE (CDR (x)) == PAIR && TYPE (CADR (x)) == VALUES)
674 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
675 switch (FUNCTION (fn).arity)
677 // case 0: return FUNCTION (fn).function0 ();
678 // case 1: return FUNCTION (fn).function1 (car (x));
679 // case 2: return FUNCTION (fn).function2 (car (x), cadr (x));
680 // case 3: return FUNCTION (fn).function3 (car (x), cadr (x), car (cddr (x)));
681 // case -1: return FUNCTION (fn).functionn (x);
682 case 0: {return (FUNCTION (fn).function) ();}
683 case 1: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (car (x));}
684 //case 2: {return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (car (x), cadr (x));}
685 case 2: {return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (car (x), car (cdr (x)));}
686 case 3: {return ((SCM(*)(SCM,SCM,SCM))(FUNCTION (fn).function)) (car (x), cadr (x), car (cddr (x)));}
689 case -1: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);}
691 default: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);}
694 return cell_unspecified;
700 SCM frame = car (g_stack);
704 r3 = car (cddr (frame));
705 r0 = cadr (cddr (frame));
726 SCM frame = gc_peek_frame (g_stack);
727 g_stack = cdr (g_stack);
732 mes_g_stack (SCM a) ///((internal))
738 g_stack = cons (cell_nil, cell_nil);
742 //
\f Environment setup
744 make_tmps (struct scm* cells)
747 cells[tmp].type = CHAR;
749 cells[tmp_num].type = NUMBER;
751 cells[tmp_num2].type = NUMBER;
758 VALUE (tmp_num) = SYMBOL;
759 SCM x = make_cell (tmp_num, s, 0);
760 g_symbols = cons (x, g_symbols);
770 SCM x = lookup_symbol_ (s);
772 return x ? x : make_symbol_ (s);
776 cstring_to_list (char const* s)
781 p = cons (MAKE_CHAR (s[i]), p);
786 acons (SCM key, SCM value, SCM alist)
788 return cons (cons (key, value), alist);
798 // g_cells = (scm *)malloc (2*ARENA_SIZE*sizeof(scm));
800 // #if __NYACC__ || FIXME_NYACC
801 // TYPE (0) = TVECTOR;
803 // // TYPE (0) = VECTOR;
805 // LENGTH (0) = 1000;
815 mes_symbols () ///((internal))
821 //#include "mes.symbols.i"
824 // g_cells[cell_nil] = scm_nil;
827 // g_cells[cell_f] = scm_f;
830 // g_cells[cell_t] = scm_t;
833 // g_cells[cell_dot] = scm_dot;
836 // g_cells[cell_arrow] = scm_arrow;
839 // g_cells[cell_undefined] = scm_undefined;
842 // g_cells[cell_unspecified] = scm_unspecified;
845 // g_cells[cell_closure] = scm_closure;
848 // g_cells[cell_circular] = scm_circular;
851 // g_cells[cell_begin] = scm_begin;
856 // g_cells[cell_vm_apply] = scm_vm_apply;
859 // g_cells[cell_vm_apply2] = scm_vm_apply2;
862 // g_cells[cell_vm_eval] = scm_vm_eval;
867 // g_cells[cell_vm_begin] = scm_vm_begin;
870 // g_cells[cell_vm_begin_read_input_file] = scm_vm_begin_read_input_file;
873 // g_cells[cell_vm_begin2] = scm_vm_begin2;
878 // g_cells[cell_vm_return] = scm_vm_return;
882 g_symbol_max = g_free;
886 for (int i=1; i<g_symbol_max; i++)
887 g_symbols = cons (i, g_symbols);
892 //#include "mes.symbol-names.i"
894 // g_cells[cell_nil].car = cstring_to_list (scm_nil.name);
895 // g_cells[cell_f].car = cstring_to_list (scm_f.name);
896 // g_cells[cell_t].car = cstring_to_list (scm_t.name);
897 // g_cells[cell_dot].car = cstring_to_list (scm_dot.name);
898 // g_cells[cell_arrow].car = cstring_to_list (scm_arrow.name);
899 // g_cells[cell_undefined].car = cstring_to_list (scm_undefined.name);
900 // g_cells[cell_unspecified].car = cstring_to_list (scm_unspecified.name);
901 // g_cells[cell_closure].car = cstring_to_list (scm_closure.name);
902 // g_cells[cell_circular].car = cstring_to_list (scm_circular.name);
903 // g_cells[cell_begin].car = cstring_to_list (scm_begin.name);
906 // a = acons (cell_symbol_mes_version, MAKE_STRING (cstring_to_list (VERSION)), a);
907 // a = acons (cell_symbol_mes_prefix, MAKE_STRING (cstring_to_list (PREFIX)), a);
909 a = acons (cell_symbol_dot, cell_dot, a);
910 a = acons (cell_symbol_begin, cell_begin, a);
911 a = acons (cell_closure, a, a);
913 // a = acons (cell_symbol_call_with_current_continuation, cell_call_with_current_continuation, a);
914 // a = acons (cell_symbol_sc_expand, cell_f, a);
920 make_closure (SCM args, SCM body, SCM a)
922 return make_cell (tmp_num_ (CLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
926 mes_environment () ///((internal))
943 // #include "posix.i"
944 // #include "reader.i"
946 // #include "lib.environment.i"
947 // #include "math.environment.i"
948 // #include "mes.environment.i"
949 // #include "posix.environment.i"
950 // #include "reader.environment.i"
953 scm_make_cell.cdr = g_function;
954 g_functions[g_function++] = fun_make_cell;
955 cell_make_cell = g_free++;
956 g_cells[cell_make_cell] = scm_make_cell;
958 scm_cons.cdr = g_function;
959 g_functions[g_function++] = fun_cons;
960 cell_cons = g_free++;
961 g_cells[cell_cons] = scm_cons;
963 scm_car.cdr = g_function;
964 g_functions[g_function++] = fun_car;
966 g_cells[cell_car] = scm_car;
968 scm_cdr.cdr = g_function;
969 g_functions[g_function++] = fun_cdr;
971 g_cells[cell_cdr] = scm_cdr;
973 // scm_make_cell.string = cstring_to_list (scm_make_cell.name);
974 // g_cells[cell_make_cell].string = MAKE_STRING (scm_make_cell.string);
975 // a = acons (make_symbol (scm_make_cell.string), cell_make_cell, a);
977 // scm_cons.string = cstring_to_list (scm_cons.name);
978 // g_cells[cell_cons].string = MAKE_STRING (scm_cons.string);
979 // a = acons (make_symbol (scm_cons.string), cell_cons, a);
981 // scm_car.string = cstring_to_list (scm_car.name);
982 // g_cells[cell_car].string = MAKE_STRING (scm_car.string);
983 // a = acons (make_symbol (scm_car.string), cell_car, a);
985 // scm_cdr.string = cstring_to_list (scm_cdr.name);
986 // g_cells[cell_cdr].string = MAKE_STRING (scm_cdr.string);
987 // a = acons (make_symbol (scm_cdr.string), cell_cdr, a);
993 bload_env (SCM a) ///((internal))
995 g_stdin = open ("module/mes/read-0.mo", 0);
998 //g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
1000 char *p = (char*)g_cells;
1001 assert (getchar () == 'M');
1002 assert (getchar () == 'E');
1003 assert (getchar () == 'S');
1004 g_stack = getchar () << 8;
1005 g_stack += getchar ();
1012 g_free = (p-(char*)g_cells) / sizeof (struct scm);
1016 r0 = mes_builtins (r0);
1023 TYPE (0) = 0x6c6c6168;
1024 CAR (0) = 0x6a746f6f;
1025 CDR (0) = 0x00002165;
1028 CAR (1) = 0x2d2d2d2d;
1029 CDR (1) = 0x3e3e3e3e;
1031 TYPE (9) = 0x2d2d2d2d;
1032 CAR (9) = 0x2d2d2d2d;
1033 CDR (9) = 0x3e3e3e3e;
1040 TYPE (11) = TFUNCTION;
1041 CAR (11) = 0x58585858;
1053 CAR (13) = 0x58585858;
1061 CAR (15) = 0x58585858;
1070 //puts ("<display>\n");
1075 //puts ("<char>\n");
1077 putchar (VALUE (x));
1082 //puts ("<function>\n");
1095 //puts ("<number>\n");
1097 putchar (48 + VALUE (x));
1108 //puts ("<pair>\n");
1109 //if (cont != cell_f) puts "(");
1111 if (x && x != cell_nil) display_ (CAR (x));
1112 if (CDR (x) && CDR (x) != cell_nil)
1115 if (TYPE (CDR (x)) != PAIR)
1126 //if (cont != cell_f) puts (")");
1132 //puts ("<default>\n");
1141 simple_bload_env (SCM a) ///((internal))
1144 char *mo = "module/mes/hack-32.mo";
1147 g_stdin = open (mo, 0);
1148 if (g_stdin < 0) {eputs ("no such file: module/mes/read-0-32.mo\n");return 1;}
1150 char *p = (char*)g_cells;
1156 puts (itoa (g_stdin));
1160 assert (getchar () == 'M');
1161 assert (getchar () == 'E');
1162 assert (getchar () == 'S');
1163 puts (" *GOT MES*\n");
1164 g_stack = getchar () << 8;
1165 g_stack += getchar ();
1169 puts (itoa (g_stack));
1181 puts ("read done\n");
1183 g_free = (p-(char*)g_cells) / sizeof (struct scm);
1185 if (g_free != 15) exit (33);
1194 r0 = mes_builtins (r0);
1196 if (g_free != 19) exit (34);
1199 puts ("cells read: ");
1200 puts (itoa (g_free));
1204 puts (itoa (g_symbols));
1206 // display_ (g_symbols);
1216 if (TYPE (12) != PAIR)
1235 string_to_cstring (SCM s)
1237 static char buf[1024];
1240 while (s != cell_nil)
1242 *p++ = VALUE (car (s));
1253 #if __NYACC__ || FIXME_NYACC
1254 if (TYPE (x) == TSTRING)
1256 // if (TYPE (x) == STRING)
1258 eputs (string_to_cstring (x));
1259 // else if ((write = assq_ref_env (cell_symbol_write, r0)) != cell_undefined)
1260 // apply (assq_ref_env (cell_symbol_display, r0), cons (x, cons (MAKE_NUMBER (2), cell_nil)), r0);
1261 #if __NYACC__ || FIXME_NYACC
1262 else if (TYPE (x) == SPECIAL || TYPE (x) == TSTRING || TYPE (x) == SYMBOL)
1264 // else if (TYPE (x) == SPECIAL || TYPE (x) == STRING || TYPE (x) == SYMBOL)
1266 eputs (string_to_cstring (x));
1267 else if (TYPE (x) == NUMBER)
1268 eputs (itoa (VALUE (x)));
1270 eputs ("display: undefined\n");
1271 return cell_unspecified;
1275 main (int argc, char *argv[])
1277 puts ("Hello mini-mes!\n");
1279 //g_debug = getenv ("MES_DEBUG");
1281 //if (getenv ("MES_ARENA")) ARENA_SIZE = atoi (getenv ("MES_ARENA"));
1282 if (argc > 1 && !strcmp (argv[1], "--help")) return eputs ("Usage: mes [--dump|--load] < FILE");
1284 if (argc > 1 && !strcmp (argv[1], "--version")) {eputs ("Mes ");return eputs (VERSION);};
1286 if (argc > 1 && !strcmp (argv[1], "--version")) {eputs ("Mes ");return eputs ("0.4");};
1290 r0 = mes_environment ();
1293 SCM program = simple_bload_env (r0);
1295 SCM program = (argc > 1 && !strcmp (argv[1], "--load"))
1296 ? bload_env (r0) : load_env (r0);
1297 if (argc > 1 && !strcmp (argv[1], "--dump")) return dump ();
1302 puts (itoa(g_free));
1306 push_cc (r2, cell_unspecified, r0, cell_unspecified);
1308 // puts ("g_stack: ");
1309 // display_ (g_stack);
1315 puts (itoa(g_free));
1319 puts (itoa(g_stack));
1339 //r3 = cell_vm_begin;
1352 eputs ("\nstats: [");
1353 eputs (itoa (g_free));
1366 "mov %%ebp,%%eax\n\t"
1370 "mov %%ebp,%%eax\n\t"
1372 "movzbl (%%eax),%%eax\n\t"
1378 : //no inputs "" (&main)