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 #error "POSIX not supported"
28 #define assert(x) ((x) ? (void)0 : assert_fail (#x))
42 SCM g_continuations = 0;
46 SCM r1 = 0; // param 1
47 SCM r2 = 0; // save 2+load/dump
48 SCM r3 = 0; // continuation
50 enum type_t {TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TREF, TSPECIAL, TSTRING, TSYMBOL, TVALUES, TVECTOR, TBROKEN_HEART};
59 int (*function) (void);
65 struct scm *g_cells = arena;
67 struct scm *g_cells = (struct scm*)arena;
74 // #define cell_arrow 5
75 #define cell_undefined 6
76 #define cell_unspecified 7
77 #define cell_closure 8
78 #define cell_circular 9
80 #define cell_symbol_dot 11
81 #define cell_symbol_lambda 12
82 #define cell_symbol_begin 13
83 #define cell_symbol_if 14
84 #define cell_symbol_quote 15
85 #define cell_symbol_set_x 16
87 #define cell_vm_apply 45
88 #define cell_vm_apply2 46
90 #define cell_vm_eval 47
92 #define cell_vm_begin 56
93 //#define cell_vm_begin_read_input_file 57
94 #define cell_vm_begin2 58
96 #define cell_vm_return 63
102 int ARENA_SIZE = 200;
103 struct function g_functions[5];
107 SCM make_cell_ (SCM type, SCM car, SCM cdr);
108 struct function fun_make_cell_ = {&make_cell_,3,"core:make-cell"};
109 struct scm scm_make_cell_ = {TFUNCTION,0,0};
110 //, "core:make-cell", 0};
113 SCM cons (SCM x, SCM y);
114 struct function fun_cons = {&cons,2,"cons"};
115 struct scm scm_cons = {TFUNCTION,0,0};
120 struct function fun_car = {&car,1,"car"};
121 struct scm scm_car = {TFUNCTION,0,0};
126 struct function fun_cdr = {&cdr,1,"cdr"};
127 struct scm scm_cdr = {TFUNCTION,0,0};
131 // SCM eq_p (SCM x, SCM y);
132 // struct function fun_eq_p = {&eq_p,2,"eq?"};
133 // scm scm_eq_p = {TFUNCTION,0,0};
136 #define TYPE(x) (g_cells[x].type)
138 #define CAR(x) g_cells[x].car
139 #define LENGTH(x) g_cells[x].car
140 #define STRING(x) g_cells[x].car
142 #define CDR(x) g_cells[x].cdr
143 #define CONTINUATION(x) g_cells[x].cdr
145 #define FUNCTION(x) g_functions[g_cells[x].cdr]
146 #define VALUE(x) g_cells[x].cdr
147 #define VECTOR(x) g_cells[x].cdr
149 #define MAKE_CHAR(n) make_cell_ (tmp_num_ (TCHAR), 0, tmp_num2_ (n))
150 #define MAKE_NUMBER(n) make_cell_ (tmp_num_ (TNUMBER), 0, tmp_num2_ (n))
152 #define CAAR(x) CAR (CAR (x))
153 #define CADAR(x) CAR (CDR (CAR (x)))
154 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
155 #define CADR(x) CAR (CDR (x))
157 #define MAKE_STRING(x) make_cell_ (tmp_num_ (TSTRING), x, 0)
162 assert (g_free + n < ARENA_SIZE);
169 make_cell_ (SCM type, SCM car, SCM cdr)
172 assert (TYPE (type) == TNUMBER);
173 TYPE (x) = VALUE (type);
174 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
175 if (car) CAR (x) = CAR (car);
176 if (cdr) CDR(x) = CDR(cdr);
178 else if (VALUE (type) == TFUNCTION) {
179 if (car) CAR (x) = car;
180 if (cdr) CDR(x) = CDR(cdr);
199 VALUE (tmp_num2) = x;
206 VALUE (tmp_num) = TPAIR;
207 return make_cell_ (tmp_num, x, y);
225 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
226 g_stack = cons (frame, g_stack);
231 append2 (SCM x, SCM y)
233 if (x == cell_nil) return y;
234 assert (TYPE (x) == TPAIR);
235 return cons (car (x), append2 (cdr (x), y));
239 pairlis (SCM x, SCM y, SCM a)
243 if (TYPE (x) != TPAIR)
244 return cons (cons (x, y), a);
245 return cons (cons (car (x), car (y)),
246 pairlis (cdr (x), cdr (y), a));
252 while (a != cell_nil && x == CAAR (a)) a = CDR (a);
253 return a != cell_nil ? car (a) : cell_f;
257 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
267 return cell_unspecified;
270 SCM caar (SCM x) {return car (car (x));}
271 SCM cadr (SCM x) {return car (cdr (x));}
272 SCM cdar (SCM x) {return cdr (car (x));}
273 SCM cddr (SCM x) {return cdr (cdr (x));}
287 case cell_vm_apply: {goto apply;}
288 case cell_unspecified: {return r1;}
295 switch (TYPE (car (r1)))
298 puts ("apply.function\n");
299 r1 = call (car (r1), cdr (r1));
314 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
315 && x != cell_nil && TYPE (CAR (x)) == TVALUES)
316 x = cons (CADAR (x), CDR (x));
317 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
318 && x != cell_nil && TYPE (CDR (x)) == TPAIR && TYPE (CADR (x)) == TVALUES)
319 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
320 switch (FUNCTION (fn).arity)
322 case 0: {return (FUNCTION (fn).function) ();}
323 case 1: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (car (x));}
324 case 2: {return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (car (x), cadr (x));}
325 case 3: {return ((SCM(*)(SCM,SCM,SCM))(FUNCTION (fn).function)) (car (x), cadr (x), car (cddr (x)));}
326 case -1: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);}
328 return cell_unspecified;
334 SCM frame = car (g_stack);
337 r3 = car (cddr (frame));
338 r0 = cadr (cddr (frame));
345 SCM frame = gc_peek_frame (g_stack);
346 g_stack = cdr (g_stack);
351 mes_g_stack (SCM a) ///((internal))
357 g_stack = cons (cell_nil, cell_nil);
361 //
\f Environment setup
363 make_tmps (struct scm* cells)
366 cells[tmp].type = TCHAR;
368 cells[tmp_num].type = TNUMBER;
370 cells[tmp_num2].type = TNUMBER;
377 VALUE (tmp_num) = TSYMBOL;
378 SCM x = make_cell_ (tmp_num, s, 0);
379 g_symbols = cons (x, g_symbols);
387 return x ? x : make_symbol_ (s);
391 acons (SCM key, SCM value, SCM alist)
393 return cons (cons (key, value), alist);
408 mes_symbols () ///((internal))
414 //#include "mes.symbols.i"
417 // g_cells[cell_nil] = scm_nil;
420 // g_cells[cell_f] = scm_f;
423 // g_cells[cell_t] = scm_t;
426 // g_cells[cell_dot] = scm_dot;
429 // g_cells[cell_arrow] = scm_arrow;
432 // g_cells[cell_undefined] = scm_undefined;
435 // g_cells[cell_unspecified] = scm_unspecified;
438 // g_cells[cell_closure] = scm_closure;
441 // g_cells[cell_circular] = scm_circular;
444 // g_cells[cell_begin] = scm_begin;
449 // g_cells[cell_vm_apply] = scm_vm_apply;
452 // g_cells[cell_vm_apply2] = scm_vm_apply2;
455 // g_cells[cell_vm_eval] = scm_vm_eval;
460 // g_cells[cell_vm_begin] = scm_vm_begin;
463 // g_cells[cell_vm_begin_read_input_file] = scm_vm_begin_read_input_file;
466 // g_cells[cell_vm_begin2] = scm_vm_begin2;
471 // g_cells[cell_vm_return] = scm_vm_return;
475 g_symbol_max = g_free;
479 for (int i=1; i<g_symbol_max; i++)
480 g_symbols = cons (i, g_symbols);
484 a = acons (cell_symbol_dot, cell_dot, a);
485 a = acons (cell_symbol_begin, cell_begin, a);
486 a = acons (cell_closure, a, a);
492 make_closure (SCM args, SCM body, SCM a)
494 return make_cell_ (tmp_num_ (TCLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
498 mes_environment () ///((internal))
515 // #include "posix.i"
516 // #include "reader.i"
518 // #include "lib.environment.i"
519 // #include "math.environment.i"
520 // #include "mes.environment.i"
521 // #include "posix.environment.i"
522 // #include "reader.environment.i"
524 scm_make_cell_.cdr = g_function;
525 g_functions[g_function++] = fun_make_cell_;
526 cell_make_cell_ = g_free++;
527 g_cells[cell_make_cell_] = scm_make_cell_;
529 scm_cons.cdr = g_function;
530 g_functions[g_function++] = fun_cons;
531 cell_cons = g_free++;
532 g_cells[cell_cons] = scm_cons;
534 scm_car.cdr = g_function;
535 g_functions[g_function++] = fun_car;
537 g_cells[cell_car] = scm_car;
539 scm_cdr.cdr = g_function;
540 g_functions[g_function++] = fun_cdr;
542 g_cells[cell_cdr] = scm_cdr;
548 bload_env (SCM a) ///((internal))
550 g_stdin = open ("module/mes/read-0.mo", 0);
553 //g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
555 char *p = (char*)g_cells;
556 assert (getchar () == 'M');
557 assert (getchar () == 'E');
558 assert (getchar () == 'S');
559 g_stack = getchar () << 8;
560 g_stack += getchar ();
567 g_free = (p-(char*)g_cells) / sizeof (struct scm);
571 r0 = mes_builtins (r0);
578 TYPE (0) = 0x6c6c6168;
579 CAR (0) = 0x6a746f6f;
580 CDR (0) = 0x00002165;
583 CAR (1) = 0x2d2d2d2d;
584 CDR (1) = 0x3e3e3e3e;
586 TYPE (9) = 0x2d2d2d2d;
587 CAR (9) = 0x2d2d2d2d;
588 CDR (9) = 0x3e3e3e3e;
595 TYPE (11) = TFUNCTION;
596 CAR (11) = 0x58585858;
608 CAR (13) = 0x58585858;
616 CAR (15) = 0x58585858;
625 //puts ("<display>\n");
637 //puts ("<function>\n");
639 puts ("core:make-cell");
650 //puts ("<number>\n");
652 puts (itoa (VALUE (x)));
664 //if (cont != cell_f) puts "(");
666 if (x && x != cell_nil) display_ (CAR (x));
667 if (CDR (x) && CDR (x) != cell_nil)
670 if (TYPE (CDR (x)) != TPAIR)
681 //if (cont != cell_f) puts (")");
689 case 1: {puts ("()"); break;}
690 case 2: {puts ("#f"); break;}
691 case 3: {puts ("#t"); break;}
709 case 11: {puts (" . "); break;}
710 case 12: {puts ("lambda"); break;}
711 case 13: {puts ("begin"); break;}
712 case 14: {puts ("if"); break;}
713 case 15: {puts ("quote"); break;}
714 case 37: {puts ("car"); break;}
715 case 38: {puts ("cdr"); break;}
716 case 39: {puts ("null?"); break;}
717 case 40: {puts ("eq?"); break;}
718 case 41: {puts ("cons"); break;}
734 //puts ("<default>\n");
737 puts (itoa (TYPE (x)));
751 simple_bload_env (SCM a) ///((internal))
754 char *mo = "module/mes/tiny-0-32.mo";
757 g_stdin = open (mo, 0);
758 if (g_stdin < 0) {eputs ("no such file: module/mes/tiny-0-32.mo\n");return 1;}
760 char *p = (char*)g_cells;
763 assert (getchar () == 'M');
764 assert (getchar () == 'E');
765 assert (getchar () == 'S');
766 puts (" *GOT MES*\n");
768 g_stack = getchar () << 8;
769 g_stack += getchar ();
772 puts (itoa (g_stack));
782 puts ("read done\n");
784 g_free = (p-(char*)g_cells) / sizeof (struct scm);
786 if (g_free != 15) exit (33);
791 r0 = mes_builtins (r0);
793 if (g_free != 19) exit (34);
795 puts ("cells read: ");
796 puts (itoa (g_free));
800 puts (itoa (g_symbols));
802 // display_ (g_symbols);
811 if (TYPE (12) != TPAIR)
828 main (int argc, char *argv[])
830 puts ("Hello cons-mes!\n");
831 if (argc > 1 && !strcmp (argv[1], "--help")) return eputs ("Usage: mes [--dump|--load] < FILE");
833 if (argc > 1 && !strcmp (argv[1], "--version")) {eputs ("Mes ");return eputs (VERSION);};
835 if (argc > 1 && !strcmp (argv[1], "--version")) {eputs ("Mes ");return eputs ("0.4");};
839 r0 = mes_environment ();
841 SCM program = simple_bload_env (r0);
847 push_cc (r2, cell_unspecified, r0, cell_unspecified);
854 puts (itoa(g_stack));