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 1
31 #define NYACC_CAR nyacc_car
32 #define NYACC_CDR nyacc_cdr
35 int ARENA_SIZE = 200000;
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));
104 int puts (char const*);
105 char const* itoa (int);
111 int r = read (g_stdin, &c, 1);
112 if (r < 1) return -1;
124 write (int fd, char const* s, int n)
127 //syscall (SYS_write, fd, s, n));
133 "mov $0x4, %%eax\n\t"
135 : // no outputs "=" (r)
136 : "" (fd), "" (s), "" (n)
137 : "eax", "ebx", "ecx", "edx"
144 //write (STDOUT, s, strlen (s));
145 //int i = write (STDOUT, s, strlen (s));
146 write (1, (char*)&c, 1);
154 int len = size + sizeof (size);
155 //n = mmap (0, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0 );
164 //munmap ((void*)p, *n);
173 strlen (char const* s)
181 strcmp (char const* a, char const* b)
183 while (*a && *b && *a == *b) {a++;b++;}
190 //write (STDOUT, s, strlen (s));
191 //int i = write (STDOUT, s, strlen (s));
198 eputs (char const* s)
200 //write (STDERR, s, strlen (s));
201 //int i = write (STDERR, s, strlen (s));
220 *p-- = '0' + (x % 10);
232 assert_fail (char* s)
234 eputs ("assert fail:");
245 #define assert(x) ((x) ? (void)0 : assert_fail ("boo:" #x))
247 //#define assert(x) ((x) ? (void)0 : assert_fail ("boo:" #x))
248 #define assert(x) ((x) ? (void)0 : assert_fail (0))
259 SCM g_continuations = 0;
271 #if __NYACC__ || FIXME_NYACC
272 enum type_t {CHAR, TCLOSURE, TCONTINUATION, TFUNCTION, KEYWORD, MACRO, NUMBER, PAIR, REF, SPECIAL, TSTRING, SYMBOL, VALUES, TVECTOR, BROKEN_HEART};
274 enum type_t {CHAR, CLOSURE, CONTINUATION, FUNCTION, KEYWORD, MACRO, NUMBER, PAIR, REF, SPECIAL, STRING, SYMBOL, VALUES, VECTOR, BROKEN_HEART};
283 typedef int (*f_t) (void);
285 int (*function) (void);
289 struct scm *g_cells = arena;
293 // struct scm scm_nil = {SPECIAL, "()"};
294 // struct scm scm_f = {SPECIAL, "#f"};
295 // struct scm scm_t = {SPECIAL, "#t"};
296 // struct scm_dot = {SPECIAL, "."};
297 // struct scm_arrow = {SPECIAL, "=>"};
298 // struct scm_undefined = {SPECIAL, "*undefined*"};
299 // struct scm_unspecified = {SPECIAL, "*unspecified*"};
300 // struct scm_closure = {SPECIAL, "*closure*"};
301 // struct scm_circular = {SPECIAL, "*circular*"};
302 // struct scm_begin = {SPECIAL, "*begin*"};
304 // struct scm_vm_apply = {SPECIAL, "core:apply"};
305 // struct scm_vm_apply2 = {SPECIAL, "*vm-apply2*"};
307 // struct scm_vm_eval = {SPECIAL, "core:eval"};
309 // struct scm_vm_begin = {SPECIAL, "*vm-begin*"};
310 // //scm scm_vm_begin_read_input_file = {SPECIAL, "*vm-begin-read-input-file*"};
311 // struct scm_vm_begin2 = {SPECIAL, "*vm-begin2*"};
313 // struct scm_vm_return = {SPECIAL, "*vm-return*"};
315 // //#include "mes.symbols.h"
321 // #define cell_arrow 5
322 #define cell_undefined 6
323 #define cell_unspecified 7
324 #define cell_closure 8
325 #define cell_circular 9
326 #define cell_begin 10
327 #define cell_symbol_dot 11
328 #define cell_symbol_lambda 12
329 #define cell_symbol_begin 13
330 #define cell_symbol_if 14
331 #define cell_symbol_quote 15
332 #define cell_symbol_set_x 16
333 #define cell_symbol_sc_expand 17
334 #define cell_symbol_macro_expand 18
335 #define cell_symbol_sc_expander_alist 19
336 #define cell_symbol_call_with_values 20
337 #define cell_call_with_current_continuation 21
338 #define cell_symbol_call_with_current_continuation 22
339 #define cell_symbol_current_module 23
340 #define cell_symbol_primitive_load 24
341 #define cell_symbol_read_input_file 25
343 #define cell_symbol_car 37
344 #define cell_symbol_cdr 38
345 #define cell_symbol_null_p 39
346 #define cell_symbol_eq_p 40
347 #define cell_symbol_cons 41
349 #define cell_vm_evlis 42
350 #define cell_vm_evlis2 43
351 #define cell_vm_evlis3 44
352 #define cell_vm_apply 45
353 #define cell_vm_apply2 46
354 #define cell_vm_eval 47
355 #define cell_vm_eval_car 48
356 #define cell_vm_eval_cdr 49
357 #define cell_vm_eval_cons 50
358 #define cell_vm_eval_null_p 51
359 #define cell_vm_eval_set_x 52
360 #define cell_vm_eval_macro 53
361 #define cell_vm_eval2 54
362 #define cell_vm_macro_expand 55
363 #define cell_vm_begin 56
364 #define cell_vm_begin_read_input_file 57
365 #define cell_vm_begin2 58
366 #define cell_vm_if 59
367 #define cell_vm_if_expr 60
368 #define cell_vm_call_with_values2 61
369 #define cell_vm_call_with_current_continuation2 62
370 #define cell_vm_return 63
379 struct function g_functions[5];
385 SCM make_cell (SCM type, SCM car, SCM cdr);
387 struct function fun_make_cell = {&make_cell, 3};
390 struct scm scm_make_cell = {TFUNCTION,"make-cell",0};
392 struct scm scm_make_cell = {TFUNCTION,0,0};
398 SCM cons (SCM x, SCM y);
400 struct function fun_cons = {&cons, 2};
402 struct scm scm_cons = {TFUNCTION,"cons",0};
404 struct scm scm_cons = {TFUNCTION,0,0};
412 struct function fun_car = {&car, 1};
414 struct scm scm_car = {TFUNCTION,"car",0};
416 struct scm scm_car = {TFUNCTION,0,0};
424 struct function fun_cdr = {&cdr, 1};
426 struct scm scm_cdr = {TFUNCTION,"cdr",0};
428 struct scm scm_cdr = {TFUNCTION,0,0};
432 // SCM eq_p (SCM x, SCM y);
433 // struct function fun_eq_p = {&eq_p, 2};
434 // scm scm_eq_p = {TFUNCTION,0,0};// "eq?", 0};
437 #define TYPE(x) (g_cells[x].type)
439 #define CAR(x) g_cells[x].car
440 #define LENGTH(x) g_cells[x].car
441 #define STRING(x) g_cells[x].car
443 #define CDR(x) g_cells[x].cdr
444 #define CLOSURE(x) g_cells[x].cdr
445 #define CONTINUATION(x) g_cells[x].cdr
447 //#define FUNCTION(x) g_functions[g_cells[x].function]
450 #define FUNCTION(x) g_functions[g_cells[x].cdr]
451 #define VALUE(x) g_cells[x].cdr
452 #define VECTOR(x) g_cells[x].cdr
454 #define MAKE_CHAR(n) make_cell (tmp_num_ (CHAR), 0, tmp_num2_ (n))
455 #define MAKE_CONTINUATION(n) make_cell (tmp_num_ (TCONTINUATION), n, g_stack)
456 #define MAKE_NUMBER(n) make_cell (tmp_num_ (NUMBER), 0, tmp_num2_ (n))
457 //#define MAKE_REF(n) make_cell (tmp_num_ (REF), n, 0)
460 #define CAAR(x) CAR (CAR (x))
461 // #define CDAR(x) CDR (CAR (x))
462 #define CADAR(x) CAR (CDR (CAR (x)))
463 #define CADDR(x) CAR (CDR (CDR (x)))
464 // #define CDDDR(x) CDR (CDR (CDR (x)))
465 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
466 #define CADR(x) CAR (CDR (x))
469 #if __NYACC__ || FIXME_NYACC
470 #define MAKE_STRING(x) make_cell (tmp_num_ (TSTRING), x, 0)
472 // #define MAKE_STRING(x) make_cell (tmp_num_ (STRING), x, 0)
478 assert (g_free + n < ARENA_SIZE);
487 make_cell (SCM type, SCM car, SCM cdr)
491 puts ("make_cell type=");
494 puts ("make_cell type.type=");
495 puts (itoa (TYPE (type)));
498 if (TYPE (type) != NUMBER)
500 puts ("type != NUMBER\n");
501 if (TYPE (type) < 10) puts ("type < 10\n");
502 if (TYPE (type) < 20) puts ("type < 20\n");
503 if (TYPE (type) < 30) puts ("type < 30\n");
504 if (TYPE (type) < 40) puts ("type < 40\n");
505 if (TYPE (type) < 50) puts ("type < 50\n");
506 if (TYPE (type) < 60) puts ("type < 60\n");
508 //assert (TYPE (type) == NUMBER);
509 TYPE (x) = VALUE (type);
510 if (VALUE (type) == CHAR || VALUE (type) == NUMBER) {
511 if (car) CAR (x) = CAR (car);
512 if (cdr) CDR(x) = CDR(cdr);
514 else if (VALUE (type) == TFUNCTION) {
515 if (car) CAR (x) = car;
516 if (cdr) CDR(x) = CDR(cdr);
535 VALUE (tmp_num2) = x;
547 VALUE (tmp_num) = PAIR;
548 return make_cell (tmp_num, x, y);
563 if (TYPE (x) != PAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_car));
580 if (TYPE (x) != PAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_cdr));
588 return x == cell_nil ? cell_t : cell_f;
592 // eq_p (SCM x, SCM y)
595 // || ((TYPE (x) == KEYWORD && TYPE (y) == KEYWORD
596 // && STRING (x) == STRING (y)))
597 // || (TYPE (x) == CHAR && TYPE (y) == CHAR
598 // && VALUE (x) == VALUE (y))
599 // || (TYPE (x) == NUMBER && TYPE (y) == NUMBER
600 // && VALUE (x) == VALUE (y)))
601 // ? cell_t : cell_f;
605 assert_defined (SCM x, SCM e)
607 if (e != cell_undefined) return e;
608 // error (cell_symbol_unbound_variable, x);
609 puts ("unbound variable");
617 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
618 g_stack = cons (frame, g_stack);
623 append2 (SCM x, SCM y)
625 if (x == cell_nil) return y;
628 assert (TYPE (x) == PAIR);
630 return cons (car (x), append2 (cdr (x), y));
634 pairlis (SCM x, SCM y, SCM a)
638 if (TYPE (x) != PAIR)
639 return cons (cons (x, y), a);
640 return cons (cons (car (x), car (y)),
641 pairlis (cdr (x), cdr (y), a));
647 //while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f) a = CDR (a);
648 while (a != cell_nil && x == CAAR (a)) a = CDR (a);
649 return a != cell_nil ? car (a) : cell_f;
653 assq_ref_env (SCM x, SCM a)
656 if (x == cell_f) return cell_undefined;
661 set_car_x (SCM x, SCM e)
663 assert (TYPE (x) == PAIR);
665 return cell_unspecified;
669 set_cdr_x (SCM x, SCM e)
671 //if (TYPE (x) != PAIR) error (cell_symbol_not_a_pair, cons (x, cell_set_cdr_x));
673 return cell_unspecified;
677 set_env_x (SCM x, SCM e, SCM a)
679 SCM p = assert_defined (x, assq (x, a));
680 //if (TYPE (p) != PAIR) error (cell_symbol_not_a_pair, cons (p, x));
681 return set_cdr_x (p, e);
685 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
687 SCM cl = cons (cons (cell_closure, x), x);
690 return cell_unspecified;
694 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
704 return cell_unspecified;
707 SCM caar (SCM x) {return car (car (x));}
708 SCM cadr (SCM x) {return car (cdr (x));}
709 SCM cdar (SCM x) {return cdr (car (x));}
710 SCM cddr (SCM x) {return cdr (cdr (x));}
714 SCM make_closure (SCM,SCM,SCM);
722 puts ("e/a: enter\n");
724 // if (g_free + GC_SAFETY > ARENA_SIZE)
725 // gc_pop_frame (gc (gc_push_frame ()));
729 case cell_vm_apply: {goto apply;}
730 case cell_unspecified: {return r1;}
737 puts ("e/a: apply\n");
738 switch (TYPE (car (r1)))
741 puts ("apply.function\n");
742 //check_formals (car (r1), MAKE_NUMBER (FUNCTION (car (r1)).arity), cdr (r1));
743 r1 = call (car (r1), cdr (r1));
757 puts ("e/a: enter\n");
759 puts ("e/a: eval_apply\n");
760 // if (g_free + GC_SAFETY > ARENA_SIZE)
761 // gc_pop_frame (gc (gc_push_frame ()));
765 case cell_vm_evlis: goto evlis;
766 case cell_vm_evlis2: goto evlis2;
767 case cell_vm_evlis3: goto evlis3;
768 case cell_vm_apply: goto apply;
769 case cell_vm_apply2: goto apply2;
770 case cell_vm_eval: goto eval;
772 case cell_vm_eval_car: goto eval_car;
773 case cell_vm_eval_cdr: goto eval_cdr;
774 case cell_vm_eval_cons: goto eval_cons;
775 case cell_vm_eval_null_p: goto eval_null_p;
777 case cell_vm_eval_set_x: goto eval_set_x;
778 case cell_vm_eval_macro: goto eval_macro;
779 case cell_vm_eval2: goto eval2;
780 case cell_vm_macro_expand: goto macro_expand;
781 case cell_vm_begin: goto begin;
782 ///case cell_vm_begin_read_input_file: goto begin_read_input_file;
783 case cell_vm_begin2: goto begin2;
784 case cell_vm_if: goto vm_if;
785 case cell_vm_if_expr: goto if_expr;
786 case cell_vm_call_with_current_continuation2: goto call_with_current_continuation2;
787 case cell_vm_call_with_values2: goto call_with_values2;
788 case cell_vm_return: goto vm_return;
789 case cell_unspecified: return r1;
796 puts ("e/a: evlis\n");
797 if (r1 == cell_nil) goto vm_return;
798 if (TYPE (r1) != PAIR) goto eval;
799 push_cc (car (r1), r1, r0, cell_vm_evlis2);
802 push_cc (cdr (r2), r1, r0, cell_vm_evlis3);
809 puts ("e/a: apply\n");
810 switch (TYPE (car (r1)))
813 //check_formals (car (r1), MAKE_NUMBER (FUNCTION (car (r1)).arity), cdr (r1));
814 r1 = call (car (r1), cdr (r1)); /// FIXME: move into eval_apply
819 SCM cl = CLOSURE (car (r1));
820 SCM formals = cadr (cl);
821 SCM body = cddr (cl);
824 //check_formals (car (r1), formals, cdr (r1));
825 SCM p = pairlis (formals, cdr (r1), aa);
826 call_lambda (body, p, aa, r0);
832 g_stack = CONTINUATION (CAR (r1));
843 push_cc (cons (CADR (r1), CADDR (r1)), r1, r0, cell_vm_return);
848 push_cc (CADR (r1), r1, CADDR (r1), cell_vm_return);
851 case cell_call_with_current_continuation:
854 goto call_with_current_continuation;
856 //default: check_apply (cell_f, car (r1));
861 if (car (r1) == cell_symbol_call_with_values)
864 goto call_with_values;
866 if (car (r1) == cell_symbol_current_module)
877 case cell_symbol_lambda:
879 SCM formals = cadr (car (r1));
880 SCM body = cddr (car (r1));
881 SCM p = pairlis (formals, cdr (r1), r0);
882 //check_formals (r1, formals, cdr (r1));
883 call_lambda (body, p, p, r0);
889 push_cc (car (r1), r1, r0, cell_vm_apply2);
892 //check_apply (r1, car (r2));
893 r1 = cons (r1, cdr (r2));
897 puts ("e/a: eval\n");
905 case cell_symbol_car:
907 push_cc (CADR (r1), r1, r0, cell_vm_eval_car); goto eval;
909 x = r1; gc_pop_frame (); r1 = car (x); goto eval_apply;
911 case cell_symbol_cdr:
913 push_cc (CADR (r1), r1, r0, cell_vm_eval_cdr); goto eval;
915 x = r1; gc_pop_frame (); r1 = cdr (x); goto eval_apply;
917 case cell_symbol_cons: {
918 push_cc (CDR (r1), r1, r0, cell_vm_eval_cons); goto evlis;
922 r1 = cons (CAR (x), CADR (x));
925 case cell_symbol_null_p:
927 push_cc (CADR (r1), r1, r0, cell_vm_eval_null_p);
930 x = r1; gc_pop_frame (); r1 = null_p (x); goto eval_apply;
932 #endif // FIXED_PRIMITIVES
933 case cell_symbol_quote:
935 x = r1; gc_pop_frame (); r1 = cadr (x); goto eval_apply;
937 case cell_symbol_begin: goto begin;
938 case cell_symbol_lambda:
940 r1 = make_closure (cadr (r1), cddr (r1), assq (cell_closure, r0));
943 case cell_symbol_if: {r1=cdr (r1); goto vm_if;}
944 case cell_symbol_set_x:
946 push_cc (car (cddr (r1)), r1, r0, cell_vm_eval_set_x);
950 r1 = set_env_x (cadr (x), r1, r0);
953 case cell_vm_macro_expand:
955 push_cc (cadr (r1), r1, r0, cell_vm_return);
959 push_cc (r1, r1, r0, cell_vm_eval_macro);
965 if (TYPE (r1) == PAIR)
967 set_cdr_x (r2, cdr (r1));
968 set_car_x (r2, car (r1));
972 push_cc (CDR (r1), r1, r0, cell_vm_eval2); goto evlis;
974 r1 = cons (car (r2), r1);
981 r1 = assert_defined (r1, assq_ref_env (r1, r0));
984 default: {goto vm_return;}
991 if (TYPE (r1) == PAIR
992 && (macro = lookup_macro (car (r1), r0)) != cell_f) // FIXME GNUC
994 r1 = cons (macro, CDR (r1));
997 else if (TYPE (r1) == PAIR
998 && TYPE (CAR (r1)) == SYMBOL
999 && ((expanders = assq_ref_env (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
1000 && ((macro = assq (CAR (r1), expanders)) != cell_f))
1002 SCM sc_expand = assq_ref_env (cell_symbol_macro_expand, r0);
1003 if (sc_expand != cell_undefined && sc_expand != cell_f)
1005 r1 = cons (sc_expand, cons (r1, cell_nil));
1012 puts ("e/a: begin\n");
1013 x = cell_unspecified;
1014 while (r1 != cell_nil) {
1015 if (TYPE (r1) == PAIR && TYPE (CAR (r1)) == PAIR)
1017 if (caar (r1) == cell_symbol_begin)
1020 r1 = append2 (cdar (r1), cdr (r1));
1022 else if (caar (r1) == cell_symbol_primitive_load)
1024 push_cc (cons (cell_symbol_read_input_file, cell_nil), r1, r0, cell_vm_begin_read_input_file);
1026 begin_read_input_file:
1027 r1 = append2 (r1, cdr (r2));
1031 if (CDR (r1) == cell_nil)
1037 push_cc (CAR (r1), r1, r0, cell_vm_begin2);
1047 push_cc (car (r1), r1, r0, cell_vm_if_expr);
1057 if (cddr (r1) != cell_nil)
1059 r1 = car (cddr (r1));
1062 r1 = cell_unspecified;
1065 call_with_current_continuation:
1069 x = MAKE_CONTINUATION (g_continuations++);
1071 x = MAKE_CONTINUATION (g_continuations);
1075 push_cc (cons (car (r1), cons (x, cell_nil)), x, r0, cell_vm_call_with_current_continuation2);
1077 call_with_current_continuation2:
1078 CONTINUATION (r2) = g_stack;
1082 push_cc (cons (car (r1), cell_nil), r1, r0, cell_vm_call_with_values2);
1085 if (TYPE (r1) == VALUES)
1087 r1 = cons (cadr (r2), r1);
1091 puts ("e/a: vm-return\n");
1103 call (SCM fn, SCM x)
1106 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
1107 && x != cell_nil && TYPE (CAR (x)) == VALUES)
1108 x = cons (CADAR (x), CDR (x));
1109 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
1110 && x != cell_nil && TYPE (CDR (x)) == PAIR && TYPE (CADR (x)) == VALUES)
1111 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
1118 puts (itoa (TYPE (fn)));
1120 puts (itoa (CDR (fn)));
1126 puts (itoa (FUNCTION (fn).arity));
1129 switch (FUNCTION (fn).arity)
1131 // case 0: return FUNCTION (fn).function0 ();
1132 // case 1: return FUNCTION (fn).function1 (car (x));
1133 // case 2: return FUNCTION (fn).function2 (car (x), cadr (x));
1134 // case 3: return FUNCTION (fn).function3 (car (x), cadr (x), car (cddr (x)));
1135 // case -1: return FUNCTION (fn).functionn (x);
1136 case 0: {return (FUNCTION (fn).function) ();}
1137 case 1: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (car (x));}
1138 case 2: {return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (car (x), cadr (x));}
1139 case 3: {return ((SCM(*)(SCM,SCM,SCM))(FUNCTION (fn).function)) (car (x), cadr (x), car (cddr (x)));}
1142 case -1: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);}
1144 default: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);}
1147 return cell_unspecified;
1153 SCM frame = car (g_stack);
1158 r3 = car (cddr (frame));
1159 r0 = cadr (cddr (frame));
1180 SCM frame = gc_peek_frame (g_stack);
1181 g_stack = cdr (g_stack);
1186 mes_g_stack (SCM a) ///((internal))
1192 g_stack = cons (cell_nil, cell_nil);
1196 //
\f Environment setup
1198 make_tmps (struct scm* cells)
1201 cells[tmp].type = CHAR;
1203 cells[tmp_num].type = NUMBER;
1204 tmp_num2 = g_free++;
1205 cells[tmp_num2].type = NUMBER;
1210 make_symbol_ (SCM s)
1212 VALUE (tmp_num) = SYMBOL;
1213 SCM x = make_cell (tmp_num, s, 0);
1214 g_symbols = cons (x, g_symbols);
1224 SCM x = lookup_symbol_ (s);
1226 return x ? x : make_symbol_ (s);
1230 cstring_to_list (char const* s)
1235 p = cons (MAKE_CHAR (s[i]), p);
1240 acons (SCM key, SCM value, SCM alist)
1242 return cons (cons (key, value), alist);
1252 // g_cells = (scm *)malloc (2*ARENA_SIZE*sizeof(scm));
1254 // #if __NYACC__ || FIXME_NYACC
1255 // TYPE (0) = TVECTOR;
1257 // // TYPE (0) = VECTOR;
1259 // LENGTH (0) = 1000;
1269 mes_symbols () ///((internal))
1275 //#include "mes.symbols.i"
1278 // g_cells[cell_nil] = scm_nil;
1281 // g_cells[cell_f] = scm_f;
1284 // g_cells[cell_t] = scm_t;
1287 // g_cells[cell_dot] = scm_dot;
1290 // g_cells[cell_arrow] = scm_arrow;
1293 // g_cells[cell_undefined] = scm_undefined;
1296 // g_cells[cell_unspecified] = scm_unspecified;
1299 // g_cells[cell_closure] = scm_closure;
1302 // g_cells[cell_circular] = scm_circular;
1305 // g_cells[cell_begin] = scm_begin;
1310 // g_cells[cell_vm_apply] = scm_vm_apply;
1313 // g_cells[cell_vm_apply2] = scm_vm_apply2;
1316 // g_cells[cell_vm_eval] = scm_vm_eval;
1321 // g_cells[cell_vm_begin] = scm_vm_begin;
1324 // g_cells[cell_vm_begin_read_input_file] = scm_vm_begin_read_input_file;
1327 // g_cells[cell_vm_begin2] = scm_vm_begin2;
1332 // g_cells[cell_vm_return] = scm_vm_return;
1336 //g_cells[cell_test] = scm_test;
1340 g_symbol_max = g_free;
1341 make_tmps (g_cells);
1344 for (int i=1; i<g_symbol_max; i++)
1345 g_symbols = cons (i, g_symbols);
1350 //#include "mes.symbol-names.i"
1352 // g_cells[cell_nil].car = cstring_to_list (scm_nil.name);
1353 // g_cells[cell_f].car = cstring_to_list (scm_f.name);
1354 // g_cells[cell_t].car = cstring_to_list (scm_t.name);
1355 // g_cells[cell_dot].car = cstring_to_list (scm_dot.name);
1356 // g_cells[cell_arrow].car = cstring_to_list (scm_arrow.name);
1357 // g_cells[cell_undefined].car = cstring_to_list (scm_undefined.name);
1358 // g_cells[cell_unspecified].car = cstring_to_list (scm_unspecified.name);
1359 // g_cells[cell_closure].car = cstring_to_list (scm_closure.name);
1360 // g_cells[cell_circular].car = cstring_to_list (scm_circular.name);
1361 // g_cells[cell_begin].car = cstring_to_list (scm_begin.name);
1364 // a = acons (cell_symbol_mes_version, MAKE_STRING (cstring_to_list (VERSION)), a);
1365 // a = acons (cell_symbol_mes_prefix, MAKE_STRING (cstring_to_list (PREFIX)), a);
1367 a = acons (cell_symbol_dot, cell_dot, a);
1368 a = acons (cell_symbol_begin, cell_begin, a);
1369 a = acons (cell_closure, a, a);
1371 // a = acons (cell_symbol_call_with_current_continuation, cell_call_with_current_continuation, a);
1372 // a = acons (cell_symbol_sc_expand, cell_f, a);
1378 make_closure (SCM args, SCM body, SCM a)
1380 return make_cell (tmp_num_ (TCLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
1384 mes_environment () ///((internal))
1388 a = mes_g_stack (a);
1393 mes_builtins (SCM a)
1400 // #include "math.i"
1401 // #include "posix.i"
1402 // #include "reader.i"
1404 // #include "lib.environment.i"
1405 // #include "math.environment.i"
1406 // #include "mes.environment.i"
1407 // #include "posix.environment.i"
1408 // #include "reader.environment.i"
1411 scm_make_cell.cdr = g_function;
1412 g_functions[g_function++] = fun_make_cell;
1413 cell_make_cell = g_free++;
1414 g_cells[cell_make_cell] = scm_make_cell;
1416 scm_cons.cdr = g_function;
1417 g_functions[g_function++] = fun_cons;
1419 puts ("BUILTIN cons=");
1420 puts (itoa (g_free));
1423 cell_cons = g_free++;
1424 g_cells[cell_cons] = scm_cons;
1426 scm_car.cdr = g_function;
1427 g_functions[g_function++] = fun_car;
1428 cell_car = g_free++;
1429 g_cells[cell_car] = scm_car;
1431 scm_cdr.cdr = g_function;
1432 g_functions[g_function++] = fun_cdr;
1433 cell_cdr = g_free++;
1434 g_cells[cell_cdr] = scm_cdr;
1437 //scm_make_cell.string = cstring_to_list (scm_make_cell.name);
1438 //g_cells[cell_make_cell].string = MAKE_STRING (scm_make_cell.string);
1439 //a = acons (make_symbol (scm_make_cell.string), cell_make_cell, a);
1441 scm_make_cell.car = cstring_to_list (scm_make_cell.car);
1443 g_cells[cell_make_cell].car = MAKE_STRING (scm_make_cell.car);
1445 a = acons (make_symbol (scm_make_cell.car), cell_make_cell, a);
1448 //scm_cons.string = cstring_to_list (scm_cons.name);
1449 //g_cells[cell_cons].string = MAKE_STRING (scm_cons.string);
1450 //a = acons (make_symbol (scm_cons.string), cell_cons, a);
1451 scm_cons.car = cstring_to_list (scm_cons.car);
1452 g_cells[cell_cons].car = MAKE_STRING (scm_cons.car);
1453 a = acons (make_symbol (scm_cons.car), cell_cons, a);
1455 //scm_car.string = cstring_to_list (scm_car.name);
1456 //g_cells[cell_car].string = MAKE_STRING (scm_car.string);
1457 //a = acons (make_symbol (scm_cons.string), cell_cons, a);
1458 scm_car.car = cstring_to_list (scm_car.car);
1459 g_cells[cell_car].car = MAKE_STRING (scm_car.car);
1460 a = acons (make_symbol (scm_cons.car), cell_cons, a);
1462 //scm_cdr.string = cstring_to_list (scm_cdr.name);
1463 //g_cells[cell_cdr].string = MAKE_STRING (scm_cdr.string);
1464 //a = acons (make_symbol (scm_cdr.string), cell_cdr, a);
1465 scm_cdr.car = cstring_to_list (scm_cdr.car);
1466 g_cells[cell_cdr].car = MAKE_STRING (scm_cdr.car);
1467 a = acons (make_symbol (scm_cdr.car), cell_cdr, a);
1476 bload_env (SCM a) ///((internal))
1478 g_stdin = open ("module/mes/read-0.mo", 0);
1481 //g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
1483 char *p = (char*)g_cells;
1484 assert (getchar () == 'M');
1485 assert (getchar () == 'E');
1486 assert (getchar () == 'S');
1487 g_stack = getchar () << 8;
1488 g_stack += getchar ();
1495 g_free = (p-(char*)g_cells) / sizeof (struct scm);
1499 r0 = mes_builtins (r0);
1506 TYPE (0) = 0x6c6c6168;
1507 CAR (0) = 0x6a746f6f;
1508 CDR (0) = 0x00002165;
1511 CAR (1) = 0x2d2d2d2d;
1512 CDR (1) = 0x3e3e3e3e;
1514 TYPE (9) = 0x2d2d2d2d;
1515 CAR (9) = 0x2d2d2d2d;
1516 CDR (9) = 0x3e3e3e3e;
1524 CAR (11) = 0x58585858;
1532 CAR (13) = 0x58585858;
1535 TYPE (14) = 0x58585858;
1536 CAR (14) = 0x58585858;
1537 CDR (14) = 0x58585858;
1539 TYPE (14) = 0x58585858;
1540 CAR (14) = 0x58585858;
1541 CDR (14) = 0x58585858;
1548 TYPE (11) = TFUNCTION;
1549 CAR (11) = 0x58585858;
1561 CAR (13) = 0x58585858;
1569 CAR (15) = 0x58585858;
1601 //puts ("<display>\n");
1606 //puts ("<char>\n");
1608 putchar (VALUE (x));
1613 //puts ("<function>\n");
1626 //puts ("<number>\n");
1628 puts (itoa (VALUE (x)));
1639 //puts ("<pair>\n");
1640 //if (cont != cell_f) puts "(");
1642 if (x && x != cell_nil) display_ (CAR (x));
1643 if (CDR (x) && CDR (x) != cell_nil)
1646 if (TYPE (CDR (x)) != PAIR)
1657 //if (cont != cell_f) puts (")");
1665 case 1: {puts ("()"); break;}
1666 case 2: {puts ("#f"); break;}
1667 case 3: {puts ("#t"); break;}
1685 case 11: {puts (" . "); break;}
1686 case 12: {puts ("lambda"); break;}
1687 case 13: {puts ("begin"); break;}
1688 case 14: {puts ("if"); break;}
1689 case 15: {puts ("quote"); break;}
1690 case 37: {puts ("car"); break;}
1691 case 38: {puts ("cdr"); break;}
1692 case 39: {puts ("null?"); break;}
1693 case 40: {puts ("eq?"); break;}
1694 case 41: {puts ("cons"); break;}
1710 //puts ("<default>\n");
1713 puts (itoa (TYPE (x)));
1729 simple_bload_env (SCM a) ///((internal))
1733 char *mo = "module/mes/hack-32.mo";
1735 char *mo = "mini-0-32.mo";
1740 g_stdin = open (mo, 0);
1741 if (g_stdin < 0) {eputs ("no such file: module/mes/read-0-32.mo\n");return 1;}
1743 char *p = (char*)g_cells;
1749 puts (itoa (g_stdin));
1753 assert (getchar () == 'M');
1754 assert (getchar () == 'E');
1755 assert (getchar () == 'S');
1756 puts (" *GOT MES*\n");
1757 g_stack = getchar () << 8;
1758 g_stack += getchar ();
1761 puts (itoa (g_stack));
1773 puts ("read done\n");
1775 g_free = (p-(char*)g_cells) / sizeof (struct scm);
1780 puts ("XXcells read: ");
1781 puts (itoa (g_free));
1797 eputs (itoa (g_stack));
1802 if (g_free != 15) exit (33);
1808 r0 = mes_builtins (r0);
1810 ///if (g_free != 19) exit (34);
1813 puts ("cells read: ");
1814 puts (itoa (g_free));
1818 puts (itoa (g_symbols));
1833 if (TYPE (12) != PAIR)
1853 string_to_cstring (SCM s)
1855 static char buf[1024];
1858 while (s != cell_nil)
1860 *p++ = VALUE (car (s));
1871 #if __NYACC__ || FIXME_NYACC
1872 if (TYPE (x) == TSTRING)
1874 // if (TYPE (x) == STRING)
1876 eputs (string_to_cstring (x));
1877 // else if ((write = assq_ref_env (cell_symbol_write, r0)) != cell_undefined)
1878 // apply (assq_ref_env (cell_symbol_display, r0), cons (x, cons (MAKE_NUMBER (2), cell_nil)), r0);
1879 #if __NYACC__ || FIXME_NYACC
1880 else if (TYPE (x) == SPECIAL || TYPE (x) == TSTRING || TYPE (x) == SYMBOL)
1882 // else if (TYPE (x) == SPECIAL || TYPE (x) == STRING || TYPE (x) == SYMBOL)
1884 eputs (string_to_cstring (x));
1885 else if (TYPE (x) == NUMBER)
1886 eputs (itoa (VALUE (x)));
1888 eputs ("display: undefined\n");
1889 return cell_unspecified;
1893 main (int argc, char *argv[])
1895 puts ("Hello mini-mes!\n");
1897 //g_debug = getenv ("MES_DEBUG");
1899 //if (getenv ("MES_ARENA")) ARENA_SIZE = atoi (getenv ("MES_ARENA"));
1900 if (argc > 1 && !strcmp (argv[1], "--help")) return eputs ("Usage: mes [--dump|--load] < FILE");
1902 if (argc > 1 && !strcmp (argv[1], "--version")) {eputs ("Mes ");return eputs (VERSION);};
1904 if (argc > 1 && !strcmp (argv[1], "--version")) {eputs ("Mes ");return eputs ("0.4");};
1908 r0 = mes_environment ();
1911 SCM program = simple_bload_env (r0);
1913 SCM program = (argc > 1 && !strcmp (argv[1], "--load"))
1914 ? bload_env (r0) : load_env (r0);
1915 if (argc > 1 && !strcmp (argv[1], "--dump")) return dump ();
1918 //if (r2 != 10) r2 = CAR (r2);
1919 push_cc (r2, cell_unspecified, r0, cell_unspecified);
1922 // puts ("stack: ");
1923 // display_ (g_stack);
1927 puts (itoa(g_free));
1931 puts (itoa(g_stack));
1953 if (r1 != 10) r1 = CAR (r1);
1958 //r1 = cons_eval_apply ();
1966 puts ("result r1=");
1970 puts ("result r1.type=");
1971 puts (itoa (TYPE (r1)));
1985 eputs ("\nstats: [");
1986 eputs (itoa (g_free));
1999 "mov %%ebp,%%eax\n\t"
2003 "mov %%ebp,%%eax\n\t"
2005 "movzbl (%%eax),%%eax\n\t"
2011 : //no inputs "" (&main)