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);
290 struct scm *g_cells = arena;
294 // struct scm scm_nil = {SPECIAL, "()"};
295 // struct scm scm_f = {SPECIAL, "#f"};
296 // struct scm scm_t = {SPECIAL, "#t"};
297 // struct scm_dot = {SPECIAL, "."};
298 // struct scm_arrow = {SPECIAL, "=>"};
299 // struct scm_undefined = {SPECIAL, "*undefined*"};
300 // struct scm_unspecified = {SPECIAL, "*unspecified*"};
301 // struct scm_closure = {SPECIAL, "*closure*"};
302 // struct scm_circular = {SPECIAL, "*circular*"};
303 // struct scm_begin = {SPECIAL, "*begin*"};
305 // struct scm_vm_apply = {SPECIAL, "core:apply"};
306 // struct scm_vm_apply2 = {SPECIAL, "*vm-apply2*"};
308 // struct scm_vm_eval = {SPECIAL, "core:eval"};
310 // struct scm_vm_begin = {SPECIAL, "*vm-begin*"};
311 // //scm scm_vm_begin_read_input_file = {SPECIAL, "*vm-begin-read-input-file*"};
312 // struct scm_vm_begin2 = {SPECIAL, "*vm-begin2*"};
314 // struct scm_vm_return = {SPECIAL, "*vm-return*"};
316 // //#include "mes.symbols.h"
322 // #define cell_arrow 5
323 #define cell_undefined 6
324 #define cell_unspecified 7
325 #define cell_closure 8
326 #define cell_circular 9
327 #define cell_begin 10
328 #define cell_symbol_dot 11
329 #define cell_symbol_lambda 12
330 #define cell_symbol_begin 13
331 #define cell_symbol_if 14
332 #define cell_symbol_quote 15
333 #define cell_symbol_set_x 16
334 #define cell_symbol_sc_expand 17
335 #define cell_symbol_macro_expand 18
336 #define cell_symbol_sc_expander_alist 19
337 #define cell_symbol_call_with_values 20
338 #define cell_call_with_current_continuation 21
339 #define cell_symbol_call_with_current_continuation 22
340 #define cell_symbol_current_module 23
341 #define cell_symbol_primitive_load 24
342 #define cell_symbol_read_input_file 25
344 #define cell_symbol_car 37
345 #define cell_symbol_cdr 38
346 #define cell_symbol_null_p 39
347 #define cell_symbol_eq_p 40
348 #define cell_symbol_cons 41
350 #define cell_vm_evlis 42
351 #define cell_vm_evlis2 43
352 #define cell_vm_evlis3 44
353 #define cell_vm_apply 45
354 #define cell_vm_apply2 46
355 #define cell_vm_eval 47
356 #define cell_vm_eval_car 48
357 #define cell_vm_eval_cdr 49
358 #define cell_vm_eval_cons 50
359 #define cell_vm_eval_null_p 51
360 #define cell_vm_eval_set_x 52
361 #define cell_vm_eval_macro 53
362 #define cell_vm_eval2 54
363 #define cell_vm_macro_expand 55
364 #define cell_vm_begin 56
365 #define cell_vm_begin_read_input_file 57
366 #define cell_vm_begin2 58
367 #define cell_vm_if 59
368 #define cell_vm_if_expr 60
369 #define cell_vm_call_with_values2 61
370 #define cell_vm_call_with_current_continuation2 62
371 #define cell_vm_return 63
380 struct function g_functions[5];
386 SCM make_cell (SCM type, SCM car, SCM cdr);
388 struct function fun_make_cell = {&make_cell,3,"make-cell"};
389 struct scm scm_make_cell = {TFUNCTION,0,0};
394 SCM cons (SCM x, SCM y);
396 struct function fun_cons = {&cons,2,"cons"};
397 struct scm scm_cons = {TFUNCTION,0,0};
404 struct function fun_car = {&car,1,"car"};
405 struct scm scm_car = {TFUNCTION,0,0};
412 struct function fun_cdr = {&cdr,1,"cdr"};
413 struct scm scm_cdr = {TFUNCTION,0,0};
416 // SCM eq_p (SCM x, SCM y);
417 // struct function fun_eq_p = {&eq_p, 2};
418 // scm scm_eq_p = {TFUNCTION,0,0};// "eq?", 0};
421 #define TYPE(x) (g_cells[x].type)
423 #define CAR(x) g_cells[x].car
424 #define LENGTH(x) g_cells[x].car
425 #define STRING(x) g_cells[x].car
427 #define CDR(x) g_cells[x].cdr
428 #define CLOSURE(x) g_cells[x].cdr
429 #define CONTINUATION(x) g_cells[x].cdr
431 //#define FUNCTION(x) g_functions[g_cells[x].function]
434 #define FUNCTION(x) g_functions[g_cells[x].cdr]
435 #define VALUE(x) g_cells[x].cdr
436 #define VECTOR(x) g_cells[x].cdr
438 #define MAKE_CHAR(n) make_cell (tmp_num_ (CHAR), 0, tmp_num2_ (n))
439 #define MAKE_CONTINUATION(n) make_cell (tmp_num_ (TCONTINUATION), n, g_stack)
440 #define MAKE_NUMBER(n) make_cell (tmp_num_ (NUMBER), 0, tmp_num2_ (n))
441 //#define MAKE_REF(n) make_cell (tmp_num_ (REF), n, 0)
444 #define CAAR(x) CAR (CAR (x))
445 // #define CDAR(x) CDR (CAR (x))
446 #define CADAR(x) CAR (CDR (CAR (x)))
447 #define CADDR(x) CAR (CDR (CDR (x)))
448 // #define CDDDR(x) CDR (CDR (CDR (x)))
449 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
450 #define CADR(x) CAR (CDR (x))
453 #if __NYACC__ || FIXME_NYACC
454 #define MAKE_STRING(x) make_cell (tmp_num_ (TSTRING), x, 0)
456 // #define MAKE_STRING(x) make_cell (tmp_num_ (STRING), x, 0)
462 assert (g_free + n < ARENA_SIZE);
471 make_cell (SCM type, SCM car, SCM cdr)
475 puts ("make_cell type=");
478 puts ("make_cell type.type=");
479 puts (itoa (TYPE (type)));
482 if (TYPE (type) != NUMBER)
484 puts ("type != NUMBER\n");
485 if (TYPE (type) < 10) puts ("type < 10\n");
486 if (TYPE (type) < 20) puts ("type < 20\n");
487 if (TYPE (type) < 30) puts ("type < 30\n");
488 if (TYPE (type) < 40) puts ("type < 40\n");
489 if (TYPE (type) < 50) puts ("type < 50\n");
490 if (TYPE (type) < 60) puts ("type < 60\n");
492 //assert (TYPE (type) == NUMBER);
493 TYPE (x) = VALUE (type);
494 if (VALUE (type) == CHAR || VALUE (type) == NUMBER) {
495 if (car) CAR (x) = CAR (car);
496 if (cdr) CDR(x) = CDR(cdr);
498 else if (VALUE (type) == TFUNCTION) {
499 if (car) CAR (x) = car;
500 if (cdr) CDR(x) = CDR(cdr);
519 VALUE (tmp_num2) = x;
531 VALUE (tmp_num) = PAIR;
532 return make_cell (tmp_num, x, y);
547 if (TYPE (x) != PAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_car));
564 if (TYPE (x) != PAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_cdr));
572 return x == cell_nil ? cell_t : cell_f;
576 // eq_p (SCM x, SCM y)
579 // || ((TYPE (x) == KEYWORD && TYPE (y) == KEYWORD
580 // && STRING (x) == STRING (y)))
581 // || (TYPE (x) == CHAR && TYPE (y) == CHAR
582 // && VALUE (x) == VALUE (y))
583 // || (TYPE (x) == NUMBER && TYPE (y) == NUMBER
584 // && VALUE (x) == VALUE (y)))
585 // ? cell_t : cell_f;
589 assert_defined (SCM x, SCM e)
591 if (e != cell_undefined) return e;
592 // error (cell_symbol_unbound_variable, x);
593 puts ("unbound variable");
601 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
602 g_stack = cons (frame, g_stack);
607 append2 (SCM x, SCM y)
609 if (x == cell_nil) return y;
612 assert (TYPE (x) == PAIR);
614 return cons (car (x), append2 (cdr (x), y));
618 pairlis (SCM x, SCM y, SCM a)
622 if (TYPE (x) != PAIR)
623 return cons (cons (x, y), a);
624 return cons (cons (car (x), car (y)),
625 pairlis (cdr (x), cdr (y), a));
631 //while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f) a = CDR (a);
632 while (a != cell_nil && x == CAAR (a)) a = CDR (a);
633 return a != cell_nil ? car (a) : cell_f;
637 assq_ref_env (SCM x, SCM a)
640 if (x == cell_f) return cell_undefined;
645 set_car_x (SCM x, SCM e)
647 assert (TYPE (x) == PAIR);
649 return cell_unspecified;
653 set_cdr_x (SCM x, SCM e)
655 //if (TYPE (x) != PAIR) error (cell_symbol_not_a_pair, cons (x, cell_set_cdr_x));
657 return cell_unspecified;
661 set_env_x (SCM x, SCM e, SCM a)
663 SCM p = assert_defined (x, assq (x, a));
664 //if (TYPE (p) != PAIR) error (cell_symbol_not_a_pair, cons (p, x));
665 return set_cdr_x (p, e);
669 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
671 SCM cl = cons (cons (cell_closure, x), x);
674 return cell_unspecified;
678 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
688 return cell_unspecified;
691 SCM caar (SCM x) {return car (car (x));}
692 SCM cadr (SCM x) {return car (cdr (x));}
693 SCM cdar (SCM x) {return cdr (car (x));}
694 SCM cddr (SCM x) {return cdr (cdr (x));}
698 SCM make_closure (SCM,SCM,SCM);
706 puts ("e/a: enter\n");
708 // if (g_free + GC_SAFETY > ARENA_SIZE)
709 // gc_pop_frame (gc (gc_push_frame ()));
713 case cell_vm_apply: {goto apply;}
714 case cell_unspecified: {return r1;}
721 puts ("e/a: apply\n");
722 switch (TYPE (car (r1)))
725 puts ("apply.function\n");
726 //check_formals (car (r1), MAKE_NUMBER (FUNCTION (car (r1)).arity), cdr (r1));
727 r1 = call (car (r1), cdr (r1));
741 puts ("e/a: enter\n");
743 puts ("e/a: eval_apply\n");
744 // if (g_free + GC_SAFETY > ARENA_SIZE)
745 // gc_pop_frame (gc (gc_push_frame ()));
749 case cell_vm_evlis: goto evlis;
750 case cell_vm_evlis2: goto evlis2;
751 case cell_vm_evlis3: goto evlis3;
752 case cell_vm_apply: goto apply;
753 case cell_vm_apply2: goto apply2;
754 case cell_vm_eval: goto eval;
756 case cell_vm_eval_car: goto eval_car;
757 case cell_vm_eval_cdr: goto eval_cdr;
758 case cell_vm_eval_cons: goto eval_cons;
759 case cell_vm_eval_null_p: goto eval_null_p;
761 case cell_vm_eval_set_x: goto eval_set_x;
762 case cell_vm_eval_macro: goto eval_macro;
763 case cell_vm_eval2: goto eval2;
764 case cell_vm_macro_expand: goto macro_expand;
765 case cell_vm_begin: goto begin;
766 ///case cell_vm_begin_read_input_file: goto begin_read_input_file;
767 case cell_vm_begin2: goto begin2;
768 case cell_vm_if: goto vm_if;
769 case cell_vm_if_expr: goto if_expr;
770 case cell_vm_call_with_current_continuation2: goto call_with_current_continuation2;
771 case cell_vm_call_with_values2: goto call_with_values2;
772 case cell_vm_return: goto vm_return;
773 case cell_unspecified: return r1;
780 puts ("e/a: evlis\n");
781 if (r1 == cell_nil) goto vm_return;
782 if (TYPE (r1) != PAIR) goto eval;
783 push_cc (car (r1), r1, r0, cell_vm_evlis2);
786 push_cc (cdr (r2), r1, r0, cell_vm_evlis3);
793 puts ("e/a: apply\n");
794 switch (TYPE (car (r1)))
797 //check_formals (car (r1), MAKE_NUMBER (FUNCTION (car (r1)).arity), cdr (r1));
798 r1 = call (car (r1), cdr (r1)); /// FIXME: move into eval_apply
803 SCM cl = CLOSURE (car (r1));
804 SCM formals = cadr (cl);
805 SCM body = cddr (cl);
808 //check_formals (car (r1), formals, cdr (r1));
809 SCM p = pairlis (formals, cdr (r1), aa);
810 call_lambda (body, p, aa, r0);
816 g_stack = CONTINUATION (CAR (r1));
827 push_cc (cons (CADR (r1), CADDR (r1)), r1, r0, cell_vm_return);
832 push_cc (CADR (r1), r1, CADDR (r1), cell_vm_return);
835 case cell_call_with_current_continuation:
838 goto call_with_current_continuation;
840 //default: check_apply (cell_f, car (r1));
845 if (car (r1) == cell_symbol_call_with_values)
848 goto call_with_values;
850 if (car (r1) == cell_symbol_current_module)
861 case cell_symbol_lambda:
863 SCM formals = cadr (car (r1));
864 SCM body = cddr (car (r1));
865 SCM p = pairlis (formals, cdr (r1), r0);
866 //check_formals (r1, formals, cdr (r1));
867 call_lambda (body, p, p, r0);
873 push_cc (car (r1), r1, r0, cell_vm_apply2);
876 //check_apply (r1, car (r2));
877 r1 = cons (r1, cdr (r2));
881 puts ("e/a: eval\n");
889 case cell_symbol_car:
891 push_cc (CADR (r1), r1, r0, cell_vm_eval_car); goto eval;
893 x = r1; gc_pop_frame (); r1 = car (x); goto eval_apply;
895 case cell_symbol_cdr:
897 push_cc (CADR (r1), r1, r0, cell_vm_eval_cdr); goto eval;
899 x = r1; gc_pop_frame (); r1 = cdr (x); goto eval_apply;
901 case cell_symbol_cons: {
902 push_cc (CDR (r1), r1, r0, cell_vm_eval_cons); goto evlis;
906 r1 = cons (CAR (x), CADR (x));
909 case cell_symbol_null_p:
911 push_cc (CADR (r1), r1, r0, cell_vm_eval_null_p);
914 x = r1; gc_pop_frame (); r1 = null_p (x); goto eval_apply;
916 #endif // FIXED_PRIMITIVES
917 case cell_symbol_quote:
919 x = r1; gc_pop_frame (); r1 = cadr (x); goto eval_apply;
921 case cell_symbol_begin: goto begin;
922 case cell_symbol_lambda:
924 r1 = make_closure (cadr (r1), cddr (r1), assq (cell_closure, r0));
927 case cell_symbol_if: {r1=cdr (r1); goto vm_if;}
928 case cell_symbol_set_x:
930 push_cc (car (cddr (r1)), r1, r0, cell_vm_eval_set_x);
934 r1 = set_env_x (cadr (x), r1, r0);
937 case cell_vm_macro_expand:
939 push_cc (cadr (r1), r1, r0, cell_vm_return);
943 push_cc (r1, r1, r0, cell_vm_eval_macro);
949 if (TYPE (r1) == PAIR)
951 set_cdr_x (r2, cdr (r1));
952 set_car_x (r2, car (r1));
956 push_cc (CDR (r1), r1, r0, cell_vm_eval2); goto evlis;
958 r1 = cons (car (r2), r1);
965 r1 = assert_defined (r1, assq_ref_env (r1, r0));
968 default: {goto vm_return;}
975 if (TYPE (r1) == PAIR
976 && (macro = lookup_macro (car (r1), r0)) != cell_f) // FIXME GNUC
978 r1 = cons (macro, CDR (r1));
981 else if (TYPE (r1) == PAIR
982 && TYPE (CAR (r1)) == SYMBOL
983 && ((expanders = assq_ref_env (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
984 && ((macro = assq (CAR (r1), expanders)) != cell_f))
986 SCM sc_expand = assq_ref_env (cell_symbol_macro_expand, r0);
987 if (sc_expand != cell_undefined && sc_expand != cell_f)
989 r1 = cons (sc_expand, cons (r1, cell_nil));
996 puts ("e/a: begin\n");
997 x = cell_unspecified;
998 while (r1 != cell_nil) {
999 if (TYPE (r1) == PAIR && TYPE (CAR (r1)) == PAIR)
1001 if (caar (r1) == cell_symbol_begin)
1004 r1 = append2 (cdar (r1), cdr (r1));
1006 else if (caar (r1) == cell_symbol_primitive_load)
1008 push_cc (cons (cell_symbol_read_input_file, cell_nil), r1, r0, cell_vm_begin_read_input_file);
1010 begin_read_input_file:
1011 r1 = append2 (r1, cdr (r2));
1015 if (CDR (r1) == cell_nil)
1021 push_cc (CAR (r1), r1, r0, cell_vm_begin2);
1031 push_cc (car (r1), r1, r0, cell_vm_if_expr);
1041 if (cddr (r1) != cell_nil)
1043 r1 = car (cddr (r1));
1046 r1 = cell_unspecified;
1049 call_with_current_continuation:
1053 x = MAKE_CONTINUATION (g_continuations++);
1055 x = MAKE_CONTINUATION (g_continuations);
1059 push_cc (cons (car (r1), cons (x, cell_nil)), x, r0, cell_vm_call_with_current_continuation2);
1061 call_with_current_continuation2:
1062 CONTINUATION (r2) = g_stack;
1066 push_cc (cons (car (r1), cell_nil), r1, r0, cell_vm_call_with_values2);
1069 if (TYPE (r1) == VALUES)
1071 r1 = cons (cadr (r2), r1);
1075 puts ("e/a: vm-return\n");
1087 call (SCM fn, SCM x)
1090 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
1091 && x != cell_nil && TYPE (CAR (x)) == VALUES)
1092 x = cons (CADAR (x), CDR (x));
1093 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
1094 && x != cell_nil && TYPE (CDR (x)) == PAIR && TYPE (CADR (x)) == VALUES)
1095 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
1102 puts (itoa (TYPE (fn)));
1104 puts (itoa (CDR (fn)));
1110 puts (itoa (FUNCTION (fn).arity));
1113 switch (FUNCTION (fn).arity)
1115 // case 0: return FUNCTION (fn).function0 ();
1116 // case 1: return FUNCTION (fn).function1 (car (x));
1117 // case 2: return FUNCTION (fn).function2 (car (x), cadr (x));
1118 // case 3: return FUNCTION (fn).function3 (car (x), cadr (x), car (cddr (x)));
1119 // case -1: return FUNCTION (fn).functionn (x);
1120 case 0: {return (FUNCTION (fn).function) ();}
1121 case 1: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (car (x));}
1122 case 2: {return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (car (x), cadr (x));}
1123 case 3: {return ((SCM(*)(SCM,SCM,SCM))(FUNCTION (fn).function)) (car (x), cadr (x), car (cddr (x)));}
1126 case -1: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);}
1128 default: {return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);}
1131 return cell_unspecified;
1137 SCM frame = car (g_stack);
1142 r3 = car (cddr (frame));
1143 r0 = cadr (cddr (frame));
1164 SCM frame = gc_peek_frame (g_stack);
1165 g_stack = cdr (g_stack);
1170 mes_g_stack (SCM a) ///((internal))
1176 g_stack = cons (cell_nil, cell_nil);
1180 //
\f Environment setup
1182 make_tmps (struct scm* cells)
1185 cells[tmp].type = CHAR;
1187 cells[tmp_num].type = NUMBER;
1188 tmp_num2 = g_free++;
1189 cells[tmp_num2].type = NUMBER;
1194 make_symbol_ (SCM s)
1196 VALUE (tmp_num) = SYMBOL;
1197 SCM x = make_cell (tmp_num, s, 0);
1198 g_symbols = cons (x, g_symbols);
1208 SCM x = lookup_symbol_ (s);
1210 return x ? x : make_symbol_ (s);
1214 cstring_to_list (char const* s)
1223 p = cons (MAKE_CHAR (s[i]), p);
1225 p = cons (MAKE_CHAR (*x), p);
1233 acons (SCM key, SCM value, SCM alist)
1235 return cons (cons (key, value), alist);
1245 // g_cells = (scm *)malloc (2*ARENA_SIZE*sizeof(scm));
1247 // #if __NYACC__ || FIXME_NYACC
1248 // TYPE (0) = TVECTOR;
1250 // // TYPE (0) = VECTOR;
1252 // LENGTH (0) = 1000;
1262 mes_symbols () ///((internal))
1268 //#include "mes.symbols.i"
1271 // g_cells[cell_nil] = scm_nil;
1274 // g_cells[cell_f] = scm_f;
1277 // g_cells[cell_t] = scm_t;
1280 // g_cells[cell_dot] = scm_dot;
1283 // g_cells[cell_arrow] = scm_arrow;
1286 // g_cells[cell_undefined] = scm_undefined;
1289 // g_cells[cell_unspecified] = scm_unspecified;
1292 // g_cells[cell_closure] = scm_closure;
1295 // g_cells[cell_circular] = scm_circular;
1298 // g_cells[cell_begin] = scm_begin;
1303 // g_cells[cell_vm_apply] = scm_vm_apply;
1306 // g_cells[cell_vm_apply2] = scm_vm_apply2;
1309 // g_cells[cell_vm_eval] = scm_vm_eval;
1314 // g_cells[cell_vm_begin] = scm_vm_begin;
1317 // g_cells[cell_vm_begin_read_input_file] = scm_vm_begin_read_input_file;
1320 // g_cells[cell_vm_begin2] = scm_vm_begin2;
1325 // g_cells[cell_vm_return] = scm_vm_return;
1329 //g_cells[cell_test] = scm_test;
1333 g_symbol_max = g_free;
1334 make_tmps (g_cells);
1337 for (int i=1; i<g_symbol_max; i++)
1338 g_symbols = cons (i, g_symbols);
1343 //#include "mes.symbol-names.i"
1345 // g_cells[cell_nil].car = cstring_to_list (scm_nil.name);
1346 // g_cells[cell_f].car = cstring_to_list (scm_f.name);
1347 // g_cells[cell_t].car = cstring_to_list (scm_t.name);
1348 // g_cells[cell_dot].car = cstring_to_list (scm_dot.name);
1349 // g_cells[cell_arrow].car = cstring_to_list (scm_arrow.name);
1350 // g_cells[cell_undefined].car = cstring_to_list (scm_undefined.name);
1351 // g_cells[cell_unspecified].car = cstring_to_list (scm_unspecified.name);
1352 // g_cells[cell_closure].car = cstring_to_list (scm_closure.name);
1353 // g_cells[cell_circular].car = cstring_to_list (scm_circular.name);
1354 // g_cells[cell_begin].car = cstring_to_list (scm_begin.name);
1357 // a = acons (cell_symbol_mes_version, MAKE_STRING (cstring_to_list (VERSION)), a);
1358 // a = acons (cell_symbol_mes_prefix, MAKE_STRING (cstring_to_list (PREFIX)), a);
1360 a = acons (cell_symbol_dot, cell_dot, a);
1361 a = acons (cell_symbol_begin, cell_begin, a);
1362 a = acons (cell_closure, a, a);
1364 // a = acons (cell_symbol_call_with_current_continuation, cell_call_with_current_continuation, a);
1365 // a = acons (cell_symbol_sc_expand, cell_f, a);
1371 make_closure (SCM args, SCM body, SCM a)
1373 return make_cell (tmp_num_ (TCLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
1377 mes_environment () ///((internal))
1381 a = mes_g_stack (a);
1386 mes_builtins (SCM a)
1393 // #include "math.i"
1394 // #include "posix.i"
1395 // #include "reader.i"
1397 // #include "lib.environment.i"
1398 // #include "math.environment.i"
1399 // #include "mes.environment.i"
1400 // #include "posix.environment.i"
1401 // #include "reader.environment.i"
1404 scm_make_cell.cdr = g_function;
1405 g_functions[g_function++] = fun_make_cell;
1406 cell_make_cell = g_free++;
1407 g_cells[cell_make_cell] = scm_make_cell;
1409 scm_cons.cdr = g_function;
1410 g_functions[g_function++] = fun_cons;
1412 puts ("BUILTIN cons=");
1413 puts (itoa (g_free));
1416 cell_cons = g_free++;
1417 g_cells[cell_cons] = scm_cons;
1419 scm_car.cdr = g_function;
1420 g_functions[g_function++] = fun_car;
1421 cell_car = g_free++;
1422 g_cells[cell_car] = scm_car;
1424 scm_cdr.cdr = g_function;
1425 g_functions[g_function++] = fun_cdr;
1426 cell_cdr = g_free++;
1427 g_cells[cell_cdr] = scm_cdr;
1430 //scm_make_cell.string = cstring_to_list (scm_make_cell.name);
1431 //g_cells[cell_make_cell].string = MAKE_STRING (scm_make_cell.string);
1432 //a = acons (make_symbol (scm_make_cell.string), cell_make_cell, a);
1434 scm_make_cell.car = cstring_to_list (fun_make_cell.name);
1436 g_cells[cell_make_cell].car = MAKE_STRING (scm_make_cell.car);
1438 a = acons (make_symbol (scm_make_cell.car), cell_make_cell, a);
1441 //scm_cons.string = cstring_to_list (scm_cons.name);
1442 //g_cells[cell_cons].string = MAKE_STRING (scm_cons.string);
1443 //a = acons (make_symbol (scm_cons.string), cell_cons, a);
1444 scm_cons.car = cstring_to_list (fun_cons.name);
1445 g_cells[cell_cons].car = MAKE_STRING (scm_cons.car);
1446 a = acons (make_symbol (scm_cons.car), cell_cons, a);
1448 //scm_car.string = cstring_to_list (scm_car.name);
1449 //g_cells[cell_car].string = MAKE_STRING (scm_car.string);
1450 //a = acons (make_symbol (scm_cons.string), cell_cons, a);
1451 scm_car.car = cstring_to_list (fun_car.name);
1452 g_cells[cell_car].car = MAKE_STRING (scm_car.car);
1453 a = acons (make_symbol (scm_cons.car), cell_cons, a);
1455 //scm_cdr.string = cstring_to_list (scm_cdr.name);
1456 //g_cells[cell_cdr].string = MAKE_STRING (scm_cdr.string);
1457 //a = acons (make_symbol (scm_cdr.string), cell_cdr, a);
1458 scm_cdr.car = cstring_to_list (fun_cdr.name);
1459 g_cells[cell_cdr].car = MAKE_STRING (scm_cdr.car);
1460 a = acons (make_symbol (scm_cdr.car), cell_cdr, a);
1469 bload_env (SCM a) ///((internal))
1471 g_stdin = open ("module/mes/read-0.mo", 0);
1474 //g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
1476 char *p = (char*)g_cells;
1477 assert (getchar () == 'M');
1478 assert (getchar () == 'E');
1479 assert (getchar () == 'S');
1480 g_stack = getchar () << 8;
1481 g_stack += getchar ();
1488 g_free = (p-(char*)g_cells) / sizeof (struct scm);
1492 r0 = mes_builtins (r0);
1499 TYPE (0) = 0x6c6c6168;
1500 CAR (0) = 0x6a746f6f;
1501 CDR (0) = 0x00002165;
1504 CAR (1) = 0x2d2d2d2d;
1505 CDR (1) = 0x3e3e3e3e;
1507 TYPE (9) = 0x2d2d2d2d;
1508 CAR (9) = 0x2d2d2d2d;
1509 CDR (9) = 0x3e3e3e3e;
1517 CAR (11) = 0x58585858;
1525 CAR (13) = 0x58585858;
1528 TYPE (14) = 0x58585858;
1529 CAR (14) = 0x58585858;
1530 CDR (14) = 0x58585858;
1532 TYPE (14) = 0x58585858;
1533 CAR (14) = 0x58585858;
1534 CDR (14) = 0x58585858;
1541 TYPE (11) = TFUNCTION;
1542 CAR (11) = 0x58585858;
1554 CAR (13) = 0x58585858;
1562 CAR (15) = 0x58585858;
1594 //puts ("<display>\n");
1599 //puts ("<char>\n");
1601 putchar (VALUE (x));
1606 //puts ("<function>\n");
1619 //puts ("<number>\n");
1621 puts (itoa (VALUE (x)));
1632 //puts ("<pair>\n");
1633 //if (cont != cell_f) puts "(");
1635 if (x && x != cell_nil) display_ (CAR (x));
1636 if (CDR (x) && CDR (x) != cell_nil)
1639 if (TYPE (CDR (x)) != PAIR)
1650 //if (cont != cell_f) puts (")");
1658 case 1: {puts ("()"); break;}
1659 case 2: {puts ("#f"); break;}
1660 case 3: {puts ("#t"); break;}
1678 case 11: {puts (" . "); break;}
1679 case 12: {puts ("lambda"); break;}
1680 case 13: {puts ("begin"); break;}
1681 case 14: {puts ("if"); break;}
1682 case 15: {puts ("quote"); break;}
1683 case 37: {puts ("car"); break;}
1684 case 38: {puts ("cdr"); break;}
1685 case 39: {puts ("null?"); break;}
1686 case 40: {puts ("eq?"); break;}
1687 case 41: {puts ("cons"); break;}
1703 //puts ("<default>\n");
1706 puts (itoa (TYPE (x)));
1722 simple_bload_env (SCM a) ///((internal))
1726 char *mo = "module/mes/hack-32.mo";
1728 char *mo = "mini-0-32.mo";
1733 g_stdin = open (mo, 0);
1734 if (g_stdin < 0) {eputs ("no such file: module/mes/read-0-32.mo\n");return 1;}
1736 char *p = (char*)g_cells;
1742 puts (itoa (g_stdin));
1746 assert (getchar () == 'M');
1747 assert (getchar () == 'E');
1748 assert (getchar () == 'S');
1749 puts (" *GOT MES*\n");
1750 g_stack = getchar () << 8;
1751 g_stack += getchar ();
1754 puts (itoa (g_stack));
1766 puts ("read done\n");
1768 g_free = (p-(char*)g_cells) / sizeof (struct scm);
1773 puts ("XXcells read: ");
1774 puts (itoa (g_free));
1790 eputs (itoa (g_stack));
1795 if (g_free != 15) exit (33);
1801 r0 = mes_builtins (r0);
1803 ///if (g_free != 19) exit (34);
1806 puts ("cells read: ");
1807 puts (itoa (g_free));
1811 puts (itoa (g_symbols));
1826 if (TYPE (12) != PAIR)
1846 string_to_cstring (SCM s)
1848 static char buf[1024];
1851 while (s != cell_nil)
1853 *p++ = VALUE (car (s));
1864 #if __NYACC__ || FIXME_NYACC
1865 if (TYPE (x) == TSTRING)
1867 // if (TYPE (x) == STRING)
1869 eputs (string_to_cstring (x));
1870 // else if ((write = assq_ref_env (cell_symbol_write, r0)) != cell_undefined)
1871 // apply (assq_ref_env (cell_symbol_display, r0), cons (x, cons (MAKE_NUMBER (2), cell_nil)), r0);
1872 #if __NYACC__ || FIXME_NYACC
1873 else if (TYPE (x) == SPECIAL || TYPE (x) == TSTRING || TYPE (x) == SYMBOL)
1875 // else if (TYPE (x) == SPECIAL || TYPE (x) == STRING || TYPE (x) == SYMBOL)
1877 eputs (string_to_cstring (x));
1878 else if (TYPE (x) == NUMBER)
1879 eputs (itoa (VALUE (x)));
1881 eputs ("display: undefined\n");
1882 return cell_unspecified;
1886 main (int argc, char *argv[])
1888 puts ("Hello mini-mes!\n");
1890 //g_debug = getenv ("MES_DEBUG");
1892 //if (getenv ("MES_ARENA")) ARENA_SIZE = atoi (getenv ("MES_ARENA"));
1893 if (argc > 1 && !strcmp (argv[1], "--help")) return eputs ("Usage: mes [--dump|--load] < FILE");
1895 if (argc > 1 && !strcmp (argv[1], "--version")) {eputs ("Mes ");return eputs (VERSION);};
1897 if (argc > 1 && !strcmp (argv[1], "--version")) {eputs ("Mes ");return eputs ("0.4");};
1901 r0 = mes_environment ();
1904 SCM program = simple_bload_env (r0);
1906 SCM program = (argc > 1 && !strcmp (argv[1], "--load"))
1907 ? bload_env (r0) : load_env (r0);
1908 if (argc > 1 && !strcmp (argv[1], "--dump")) return dump ();
1911 //if (r2 != 10) r2 = CAR (r2);
1912 push_cc (r2, cell_unspecified, r0, cell_unspecified);
1915 // puts ("stack: ");
1916 // display_ (g_stack);
1920 puts (itoa(g_free));
1924 puts (itoa(g_stack));
1946 if (r1 != 10) r1 = CAR (r1);
1951 //r1 = cons_eval_apply ();
1959 puts ("result r1=");
1963 puts ("result r1.type=");
1964 puts (itoa (TYPE (r1)));
1978 eputs ("\nstats: [");
1979 eputs (itoa (g_free));
1992 "mov %%ebp,%%eax\n\t"
1996 "mov %%ebp,%%eax\n\t"
1998 "movzbl (%%eax),%%eax\n\t"
2004 : //no inputs "" (&main)