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"
38 SCM g_continuations = 0;
42 SCM r1 = 0; // param 1
43 SCM r2 = 0; // save 2+load/dump
44 SCM r3 = 0; // continuation
46 enum type_t {TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TREF, TSPECIAL, TSTRING, TSYMBOL, TVALUES, TVECTOR, TBROKEN_HEART};
55 int (*function) (void);
61 struct scm *g_cells = arena;
63 struct scm *g_cells = (struct scm*)arena;
70 // #define cell_arrow 5
71 #define cell_undefined 6
72 #define cell_unspecified 7
73 #define cell_closure 8
74 #define cell_circular 9
76 #define cell_symbol_dot 11
77 #define cell_symbol_lambda 12
78 #define cell_symbol_begin 13
79 #define cell_symbol_if 14
80 #define cell_symbol_quote 15
81 #define cell_symbol_set_x 16
83 #define cell_vm_apply 45
84 #define cell_vm_apply2 46
86 #define cell_vm_eval 47
88 #define cell_vm_begin 56
89 //#define cell_vm_begin_read_input_file 57
90 #define cell_vm_begin2 58
92 #define cell_vm_return 63
99 struct function g_functions[5];
103 SCM make_cell_ (SCM type, SCM car, SCM cdr);
104 struct function fun_make_cell_ = {&make_cell_,3,"core:make-cell"};
105 struct scm scm_make_cell_ = {TFUNCTION,0,0};
106 //, "core:make-cell", 0};
109 SCM cons (SCM x, SCM y);
110 struct function fun_cons = {&cons,2,"cons"};
111 struct scm scm_cons = {TFUNCTION,0,0};
116 struct function fun_car = {&car,1,"car"};
117 struct scm scm_car = {TFUNCTION,0,0};
122 struct function fun_cdr = {&cdr,1,"cdr"};
123 struct scm scm_cdr = {TFUNCTION,0,0};
127 // SCM eq_p (SCM x, SCM y);
128 // struct function fun_eq_p = {&eq_p,2,"eq?"};
129 // scm scm_eq_p = {TFUNCTION,0,0};
132 #define TYPE(x) (g_cells[x].type)
134 #define CAR(x) g_cells[x].car
135 #define LENGTH(x) g_cells[x].car
136 #define STRING(x) g_cells[x].car
138 #define CDR(x) g_cells[x].cdr
139 #define CONTINUATION(x) g_cells[x].cdr
141 #define FUNCTION(x) g_functions[g_cells[x].cdr]
142 #define VALUE(x) g_cells[x].cdr
143 #define VECTOR(x) g_cells[x].cdr
145 #define MAKE_CHAR(n) make_cell_ (tmp_num_ (TCHAR), 0, tmp_num2_ (n))
146 #define MAKE_NUMBER(n) make_cell_ (tmp_num_ (TNUMBER), 0, tmp_num2_ (n))
148 #define CAAR(x) CAR (CAR (x))
149 #define CADAR(x) CAR (CDR (CAR (x)))
150 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
151 #define CADR(x) CAR (CDR (x))
153 #define MAKE_STRING(x) make_cell_ (tmp_num_ (TSTRING), x, 0)
158 assert (g_free + n < ARENA_SIZE);
165 make_cell_ (SCM type, SCM car, SCM cdr)
168 assert (TYPE (type) == TNUMBER);
169 TYPE (x) = VALUE (type);
170 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
171 if (car) CAR (x) = CAR (car);
172 if (cdr) CDR(x) = CDR(cdr);
174 else if (VALUE (type) == TFUNCTION) {
175 if (car) CAR (x) = car;
176 if (cdr) CDR(x) = CDR(cdr);
195 VALUE (tmp_num2) = x;
202 VALUE (tmp_num) = TPAIR;
203 return make_cell_ (tmp_num, x, y);
221 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
222 g_stack = cons (frame, g_stack);
227 append2 (SCM x, SCM y)
229 if (x == cell_nil) return y;
230 assert (TYPE (x) == TPAIR);
231 return cons (car (x), append2 (cdr (x), y));
235 pairlis (SCM x, SCM y, SCM a)
239 if (TYPE (x) != TPAIR)
240 return cons (cons (x, y), a);
241 return cons (cons (car (x), car (y)),
242 pairlis (cdr (x), cdr (y), a));
248 while (a != cell_nil && x == CAAR (a)) a = CDR (a);
249 return a != cell_nil ? car (a) : cell_f;
253 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
263 return cell_unspecified;
266 SCM caar (SCM x) {return car (car (x));}
267 SCM cadr (SCM x) {return car (cdr (x));}
268 SCM cdar (SCM x) {return cdr (car (x));}
269 SCM cddr (SCM x) {return cdr (cdr (x));}
283 case cell_vm_apply: {goto apply;}
284 case cell_unspecified: {return r1;}
291 switch (TYPE (car (r1)))
294 puts ("apply.function\n");
295 r1 = call (car (r1), cdr (r1));
310 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
311 && x != cell_nil && TYPE (CAR (x)) == TVALUES)
312 x = cons (CADAR (x), CDR (x));
313 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
314 && x != cell_nil && TYPE (CDR (x)) == TPAIR && TYPE (CADR (x)) == TVALUES)
315 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
316 switch (FUNCTION (fn).arity)
318 case 0: {return (FUNCTION (fn).function) ();}
319 case 1: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (car (x));}
320 case 2: {return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (car (x), cadr (x));}
321 case 3: {return ((SCM(*)(SCM,SCM,SCM))(FUNCTION (fn).function)) (car (x), cadr (x), car (cddr (x)));}
322 case -1: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);}
324 return cell_unspecified;
330 SCM frame = car (g_stack);
333 r3 = car (cddr (frame));
334 r0 = cadr (cddr (frame));
341 SCM frame = gc_peek_frame (g_stack);
342 g_stack = cdr (g_stack);
347 mes_g_stack (SCM a) ///((internal))
353 g_stack = cons (cell_nil, cell_nil);
357 //
\f Environment setup
359 make_tmps (struct scm* cells)
362 cells[tmp].type = TCHAR;
364 cells[tmp_num].type = TNUMBER;
366 cells[tmp_num2].type = TNUMBER;
373 VALUE (tmp_num) = TSYMBOL;
374 SCM x = make_cell_ (tmp_num, s, 0);
375 g_symbols = cons (x, g_symbols);
383 return x ? x : make_symbol_ (s);
387 acons (SCM key, SCM value, SCM alist)
389 return cons (cons (key, value), alist);
404 mes_symbols () ///((internal))
410 //#include "mes.symbols.i"
413 // g_cells[cell_nil] = scm_nil;
416 // g_cells[cell_f] = scm_f;
419 // g_cells[cell_t] = scm_t;
422 // g_cells[cell_dot] = scm_dot;
425 // g_cells[cell_arrow] = scm_arrow;
428 // g_cells[cell_undefined] = scm_undefined;
431 // g_cells[cell_unspecified] = scm_unspecified;
434 // g_cells[cell_closure] = scm_closure;
437 // g_cells[cell_circular] = scm_circular;
440 // g_cells[cell_begin] = scm_begin;
445 // g_cells[cell_vm_apply] = scm_vm_apply;
448 // g_cells[cell_vm_apply2] = scm_vm_apply2;
451 // g_cells[cell_vm_eval] = scm_vm_eval;
456 // g_cells[cell_vm_begin] = scm_vm_begin;
459 // g_cells[cell_vm_begin_read_input_file] = scm_vm_begin_read_input_file;
462 // g_cells[cell_vm_begin2] = scm_vm_begin2;
467 // g_cells[cell_vm_return] = scm_vm_return;
471 g_symbol_max = g_free;
475 for (int i=1; i<g_symbol_max; i++)
476 g_symbols = cons (i, g_symbols);
480 a = acons (cell_symbol_dot, cell_dot, a);
481 a = acons (cell_symbol_begin, cell_begin, a);
482 a = acons (cell_closure, a, a);
488 make_closure (SCM args, SCM body, SCM a)
490 return make_cell_ (tmp_num_ (TCLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
494 mes_environment () ///((internal))
511 // #include "posix.i"
512 // #include "reader.i"
514 // #include "lib.environment.i"
515 // #include "math.environment.i"
516 // #include "mes.environment.i"
517 // #include "posix.environment.i"
518 // #include "reader.environment.i"
520 scm_make_cell_.cdr = g_function;
521 g_functions[g_function++] = fun_make_cell_;
522 cell_make_cell_ = g_free++;
523 g_cells[cell_make_cell_] = scm_make_cell_;
525 scm_cons.cdr = g_function;
526 g_functions[g_function++] = fun_cons;
527 cell_cons = g_free++;
528 g_cells[cell_cons] = scm_cons;
530 scm_car.cdr = g_function;
531 g_functions[g_function++] = fun_car;
533 g_cells[cell_car] = scm_car;
535 scm_cdr.cdr = g_function;
536 g_functions[g_function++] = fun_cdr;
538 g_cells[cell_cdr] = scm_cdr;
544 bload_env (SCM a) ///((internal))
546 g_stdin = open ("module/mes/read-0.mo", 0);
549 //g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
551 char *p = (char*)g_cells;
552 assert (getchar () == 'M');
553 assert (getchar () == 'E');
554 assert (getchar () == 'S');
555 g_stack = getchar () << 8;
556 g_stack += getchar ();
563 g_free = (p-(char*)g_cells) / sizeof (struct scm);
567 r0 = mes_builtins (r0);
574 TYPE (0) = 0x6c6c6168;
575 CAR (0) = 0x6a746f6f;
576 CDR (0) = 0x00002165;
579 CAR (1) = 0x2d2d2d2d;
580 CDR (1) = 0x3e3e3e3e;
582 TYPE (9) = 0x2d2d2d2d;
583 CAR (9) = 0x2d2d2d2d;
584 CDR (9) = 0x3e3e3e3e;
591 TYPE (11) = TFUNCTION;
592 CAR (11) = 0x58585858;
604 CAR (13) = 0x58585858;
612 CAR (15) = 0x58585858;
621 //puts ("<display>\n");
633 //puts ("<function>\n");
635 puts ("core:make-cell");
646 //puts ("<number>\n");
648 puts (itoa (VALUE (x)));
660 //if (cont != cell_f) puts "(");
662 if (x && x != cell_nil) display_ (CAR (x));
663 if (CDR (x) && CDR (x) != cell_nil)
666 if (TYPE (CDR (x)) != TPAIR)
677 //if (cont != cell_f) puts (")");
685 case 1: {puts ("()"); break;}
686 case 2: {puts ("#f"); break;}
687 case 3: {puts ("#t"); break;}
705 case 11: {puts (" . "); break;}
706 case 12: {puts ("lambda"); break;}
707 case 13: {puts ("begin"); break;}
708 case 14: {puts ("if"); break;}
709 case 15: {puts ("quote"); break;}
710 case 37: {puts ("car"); break;}
711 case 38: {puts ("cdr"); break;}
712 case 39: {puts ("null?"); break;}
713 case 40: {puts ("eq?"); break;}
714 case 41: {puts ("cons"); break;}
730 //puts ("<default>\n");
733 puts (itoa (TYPE (x)));
747 simple_bload_env (SCM a) ///((internal))
750 char *mo = "module/mes/tiny-0-32.mo";
753 g_stdin = open (mo, 0);
754 if (g_stdin < 0) {eputs ("no such file: module/mes/tiny-0-32.mo\n");return 1;}
756 char *p = (char*)g_cells;
759 assert (getchar () == 'M');
760 assert (getchar () == 'E');
761 assert (getchar () == 'S');
762 puts (" *GOT MES*\n");
764 g_stack = getchar () << 8;
765 g_stack += getchar ();
768 puts (itoa (g_stack));
778 puts ("read done\n");
780 g_free = (p-(char*)g_cells) / sizeof (struct scm);
782 if (g_free != 15) exit (33);
787 r0 = mes_builtins (r0);
789 if (g_free != 19) exit (34);
791 puts ("cells read: ");
792 puts (itoa (g_free));
796 puts (itoa (g_symbols));
798 // display_ (g_symbols);
807 if (TYPE (12) != TPAIR)
824 main (int argc, char *argv[])
826 puts ("Hello cons-mes!\n");
827 if (argc > 1 && !strcmp (argv[1], "--help")) return eputs ("Usage: mes [--dump|--load] < FILE");
829 if (argc > 1 && !strcmp (argv[1], "--version")) {eputs ("Mes ");return eputs (VERSION);};
831 if (argc > 1 && !strcmp (argv[1], "--version")) {eputs ("Mes ");return eputs ("0.4");};
835 r0 = mes_environment ();
837 SCM program = simple_bload_env (r0);
843 push_cc (r2, cell_unspecified, r0, cell_unspecified);
850 puts (itoa(g_stack));