1 /* -*-comment-start: "//";comment-end:""-*-
2 * GNU Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 * This file is part of GNU Mes.
7 * GNU 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 * GNU 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 GNU Mes. If not, see <http://www.gnu.org/licenses/>.
52 typedef long function0_t;
53 typedef long function1_t;
54 typedef long function2_t;
55 typedef long function3_t;
56 typedef long functionn_t;
58 typedef SCM (*function0_t) (void);
59 typedef SCM (*function1_t) (SCM);
60 typedef SCM (*function2_t) (SCM, SCM);
61 typedef SCM (*function3_t) (SCM, SCM, SCM);
62 typedef SCM (*functionn_t) (SCM);
70 if (g_free > ARENA_SIZE)
71 assert (!"alloc: out of memory");
76 make_cell__ (long type, SCM car, SCM cdr)
86 make_cell_ (SCM type, SCM car, SCM cdr)
88 assert (TYPE (type) == TNUMBER);
89 long t = VALUE (type);
90 if (t == TCHAR || t == TNUMBER)
91 return make_cell__ (t, car ? CAR (car) : 0, cdr ? CDR (cdr) : 0);
92 return make_cell__ (t, car, cdr);
96 assoc_string (SCM x, SCM a) ///((internal))
98 while (a != cell_nil && (TYPE (CAAR (a)) != TSTRING || string_equal_p (x, CAAR (a)) == cell_f))
100 return a != cell_nil ? CAR (a) : cell_f;
106 return MAKE_NUMBER (TYPE (x));
110 // car_to_cell_ (SCM x)
116 // cdr_to_cell_ (SCM x)
122 // car_to_number_ (SCM x)
124 // return MAKE_NUMBER (CAR (x));
128 // cdr_to_number_ (SCM x)
130 // return MAKE_NUMBER (CDR (x));
136 return (TYPE (x) != TCONTINUATION && (TYPE (CAR (x)) == TPAIR // FIXME: this is weird
137 || TYPE (CAR (x)) == TREF
138 || TYPE (CAR (x)) == TSPECIAL
139 || TYPE (CAR (x)) == TSYMBOL
140 || TYPE (CAR (x)) == TSTRING)) ? CAR (x) : MAKE_NUMBER (CAR (x));
146 return (TYPE (x) != TCHAR
147 && TYPE (x) != TNUMBER
149 && (TYPE (CDR (x)) == TPAIR
150 || TYPE (CDR (x)) == TREF
151 || TYPE (CDR (x)) == TSPECIAL
152 || TYPE (CDR (x)) == TSYMBOL || TYPE (CDR (x)) == TSTRING)) ? CDR (x) : MAKE_NUMBER (CDR (x));
158 return make_cell__ (TPAIR, x, y);
165 if (TYPE (x) != TPAIR)
166 error (cell_symbol_not_a_pair, cons (x, cell_symbol_car));
175 if (TYPE (x) != TPAIR)
176 error (cell_symbol_not_a_pair, cons (x, cell_symbol_cdr));
182 list (SCM x) ///((arity . n))
190 return x == cell_nil ? cell_t : cell_f;
197 || ((TYPE (x) == TKEYWORD && TYPE (y) == TKEYWORD
198 && string_equal_p (x, y) == cell_t))
199 || (TYPE (x) == TCHAR && TYPE (y) == TCHAR
200 && VALUE (x) == VALUE (y))
201 || (TYPE (x) == TNUMBER && TYPE (y) == TNUMBER && VALUE (x) == VALUE (y))) ? cell_t : cell_f;
205 values (SCM x) ///((arity . n))
213 acons (SCM key, SCM value, SCM alist)
215 return cons (cons (key, value), alist);
219 length__ (SCM x) ///((internal))
222 while (x != cell_nil)
225 if (TYPE (x) != TPAIR)
235 return MAKE_NUMBER (length__ (x));
239 error (SCM key, SCM x)
243 if ((throw = module_ref (r0, cell_symbol_throw)) != cell_undefined)
244 return apply (throw, cons (key, cons (x, cell_nil)), r0);
246 display_error_ (key);
256 assert_defined (SCM x, SCM e) ///((internal))
258 if (e == cell_undefined)
259 return error (cell_symbol_unbound_variable, x);
264 check_formals (SCM f, SCM formals, SCM args) ///((internal))
266 long flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : length__ (formals);
267 long alen = length__ (args);
268 if (alen != flen && alen != -1 && flen != -1)
270 char *s = "apply: wrong number of arguments; expected: ";
277 SCM e = MAKE_STRING0 (s);
278 return error (cell_symbol_wrong_number_of_args, cons (e, f));
280 return cell_unspecified;
284 check_apply (SCM f, SCM e) ///((internal))
287 if (f == cell_f || f == cell_t)
291 if (f == cell_unspecified)
292 type = "*unspecified*";
293 if (f == cell_undefined)
294 type = "*undefined*";
295 if (TYPE (f) == TCHAR)
297 if (TYPE (f) == TNUMBER)
299 if (TYPE (f) == TSTRING)
301 if (TYPE (f) == TSTRUCT && builtin_p (f) == cell_f)
303 if (TYPE (f) == TBROKEN_HEART)
308 char *s = "cannot apply: ";
314 SCM e = MAKE_STRING0 (s);
315 return error (cell_symbol_wrong_type_arg, cons (e, f));
317 return cell_unspecified;
321 append2 (SCM x, SCM y)
325 if (TYPE (x) != TPAIR)
326 error (cell_symbol_not_a_pair, cons (x, cstring_to_symbol ("append2")));
328 while (x != cell_nil)
330 r = cons (CAR (x), r);
333 return reverse_x_ (r, y);
337 append_reverse (SCM x, SCM y)
341 if (TYPE (x) != TPAIR)
342 error (cell_symbol_not_a_pair, cons (x, cstring_to_symbol ("append-reverse")));
343 while (x != cell_nil)
345 y = cons (CAR (x), y);
352 reverse_x_ (SCM x, SCM t)
354 if (x != cell_nil && TYPE (x) != TPAIR)
355 error (cell_symbol_not_a_pair, cons (x, cstring_to_symbol ("core:reverse!")));
357 while (x != cell_nil)
368 pairlis (SCM x, SCM y, SCM a)
372 if (TYPE (x) != TPAIR)
373 return cons (cons (x, y), a);
374 return cons (cons (car (x), car (y)), pairlis (cdr (x), cdr (y), a));
380 if (TYPE (a) != TPAIR)
383 if (t == TSYMBOL || t == TSPECIAL)
384 while (a != cell_nil && x != CAAR (a))
386 else if (t == TCHAR || t == TNUMBER)
389 while (a != cell_nil && v != VALUE (CAAR (a)))
392 else if (t == TKEYWORD)
394 while (a != cell_nil && string_equal_p (x, CAAR (a)) == cell_f)
398 /* pointer equality, e.g. on strings. */
399 while (a != cell_nil && x != CAAR (a))
401 return a != cell_nil ? CAR (a) : cell_f;
407 if (TYPE (x) == TSTRING)
408 return assoc_string (x, a);
409 while (a != cell_nil && equal2_p (x, CAAR (a)) == cell_f)
411 return a != cell_nil ? CAR (a) : cell_f;
415 set_car_x (SCM x, SCM e)
417 if (TYPE (x) != TPAIR)
418 error (cell_symbol_not_a_pair, cons (x, cstring_to_symbol ("set-car!")));
420 return cell_unspecified;
424 set_cdr_x (SCM x, SCM e)
426 if (TYPE (x) != TPAIR)
427 error (cell_symbol_not_a_pair, cons (x, cstring_to_symbol ("set-cdr!")));
429 return cell_unspecified;
433 set_env_x (SCM x, SCM e, SCM a)
436 if (TYPE (x) == TVARIABLE)
439 p = assert_defined (x, module_variable (a, x));
440 if (TYPE (p) != TPAIR)
441 error (cell_symbol_not_a_pair, cons (p, x));
442 return set_cdr_x (p, e);
446 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
448 SCM cl = cons (cons (cell_closure, x), x);
451 return cell_unspecified;
455 make_closure_ (SCM args, SCM body, SCM a) ///((internal))
457 return make_cell__ (TCLOSURE, cell_f, cons (cons (cell_circular, a), cons (args, body)));
461 make_variable_ (SCM var) ///((internal))
463 return make_cell__ (TVARIABLE, var, 0);
467 macro_get_handle (SCM name)
469 if (TYPE (name) == TSYMBOL)
470 return hashq_get_handle (g_macros, name, cell_nil);
475 get_macro (SCM name) ///((internal))
477 SCM m = macro_get_handle (name);
479 return MACRO (CDR (m));
484 macro_set_x (SCM name, SCM value) ///((internal))
486 return hashq_set_x (g_macros, name, value);
490 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
499 return cell_unspecified;
503 add_formals (SCM formals, SCM x)
505 while (TYPE (x) == TPAIR)
507 formals = cons (CAR (x), formals);
510 if (TYPE (x) == TSYMBOL)
511 formals = cons (x, formals);
516 formal_p (SCM x, SCM formals) /// ((internal))
518 if (TYPE (formals) == TSYMBOL)
525 while (TYPE (formals) == TPAIR && CAR (formals) != x)
526 formals = CDR (formals);
527 if (TYPE (formals) == TSYMBOL)
529 return TYPE (formals) == TPAIR;
533 expand_variable_ (SCM x, SCM formals, int top_p) ///((internal))
535 while (TYPE (x) == TPAIR)
537 if (TYPE (CAR (x)) == TPAIR)
539 if (CAAR (x) == cell_symbol_lambda)
541 SCM f = CAR (CDAR (x));
542 formals = add_formals (formals, f);
544 else if (CAAR (x) == cell_symbol_define || CAAR (x) == cell_symbol_define_macro)
546 SCM f = CAR (CDAR (x));
547 formals = add_formals (formals, f);
549 if (CAAR (x) != cell_symbol_quote)
550 expand_variable_ (CAR (x), formals, 0);
554 if (CAR (x) == cell_symbol_lambda)
557 formals = add_formals (formals, f);
560 else if (CAR (x) == cell_symbol_define || CAR (x) == cell_symbol_define_macro)
563 if (top_p && TYPE (f) == TPAIR)
565 formals = add_formals (formals, f);
568 else if (CAR (x) == cell_symbol_quote)
569 return cell_unspecified;
570 else if (TYPE (CAR (x)) == TSYMBOL
571 && CAR (x) != cell_symbol_boot_module
572 && CAR (x) != cell_symbol_current_module
573 && CAR (x) != cell_symbol_primitive_load && !formal_p (CAR (x), formals))
575 SCM v = module_variable (r0, CAR (x));
577 CAR (x) = make_variable_ (v);
583 return cell_unspecified;
587 expand_variable (SCM x, SCM formals) ///((internal))
589 return expand_variable_ (x, formals, 1);
616 if (r3 == cell_vm_evlis2)
618 else if (r3 == cell_vm_evlis3)
620 else if (r3 == cell_vm_eval_check_func)
621 goto eval_check_func;
622 else if (r3 == cell_vm_eval2)
624 else if (r3 == cell_vm_apply2)
626 else if (r3 == cell_vm_if_expr)
628 else if (r3 == cell_vm_begin_eval)
630 else if (r3 == cell_vm_eval_set_x)
632 else if (r3 == cell_vm_macro_expand_car)
633 goto macro_expand_car;
634 else if (r3 == cell_vm_return)
636 else if (r3 == cell_vm_macro_expand_cdr)
637 goto macro_expand_cdr;
638 else if (r3 == cell_vm_eval_define)
640 else if (r3 == cell_vm_macro_expand)
642 else if (r3 == cell_vm_macro_expand_lambda)
643 goto macro_expand_lambda;
644 else if (r3 == cell_vm_eval_pmatch_car)
645 goto eval_pmatch_car;
646 else if (r3 == cell_vm_begin_expand_macro)
647 goto begin_expand_macro;
648 else if (r3 == cell_vm_macro_expand_define)
649 goto macro_expand_define;
650 else if (r3 == cell_vm_begin_expand_eval)
651 goto begin_expand_eval;
652 else if (r3 == cell_vm_call_with_current_continuation2)
653 goto call_with_current_continuation2;
654 else if (r3 == cell_vm_macro_expand_set_x)
655 goto macro_expand_set_x;
656 else if (r3 == cell_vm_eval_pmatch_cdr)
657 goto eval_pmatch_cdr;
658 else if (r3 == cell_vm_macro_expand_define_macro)
659 goto macro_expand_define_macro;
660 else if (r3 == cell_vm_begin_primitive_load)
661 goto begin_primitive_load;
663 else if (r3 == cell_vm_evlis)
665 else if (r3 == cell_vm_apply)
667 else if (r3 == cell_vm_eval)
669 else if (r3 == cell_vm_eval_macro_expand_eval)
670 goto eval_macro_expand_eval;
671 else if (r3 == cell_vm_eval_macro_expand_expand)
672 goto eval_macro_expand_expand;
673 else if (r3 == cell_vm_begin)
675 else if (r3 == cell_vm_begin_expand)
677 else if (r3 == cell_vm_begin_expand_primitive_load)
678 goto begin_expand_primitive_load;
679 else if (r3 == cell_vm_if)
681 else if (r3 == cell_vm_call_with_values2)
682 goto call_with_values2;
683 else if (r3 == cell_unspecified)
686 error (cell_symbol_system_error, MAKE_STRING0 ("eval/apply unknown continuation"));
691 if (TYPE (r1) != TPAIR)
693 push_cc (CAR (r1), r1, r0, cell_vm_evlis2);
696 push_cc (CDR (r2), r1, r0, cell_vm_evlis3);
703 g_stack_array[g_stack + FRAME_PROCEDURE] = CAR (r1);
705 if (t == TSTRUCT && builtin_p (CAR (r1)) == cell_t)
707 check_formals (CAR (r1), builtin_arity (CAR (r1)), CDR (r1));
708 r1 = apply_builtin (CAR (r1), CDR (r1)); /// FIXME: move into eval_apply
711 else if (t == TCLOSURE)
713 cl = CLOSURE (CAR (r1));
719 check_formals (CAR (r1), formals, CDR (r1));
720 p = pairlis (formals, args, aa);
721 call_lambda (body, p, aa, r0);
724 else if (t == TCONTINUATION)
726 v = CONTINUATION (CAR (r1));
729 for (t = 0; t < LENGTH (v); t++)
730 g_stack_array[STACK_SIZE - LENGTH (v) + t] = vector_ref_ (v, t);
731 g_stack = STACK_SIZE - LENGTH (v);
738 else if (t == TSPECIAL)
741 if (c == cell_vm_apply)
743 push_cc (cons (CADR (r1), CADDR (r1)), r1, r0, cell_vm_return);
746 else if (c == cell_vm_eval)
748 push_cc (CADR (r1), r1, CADDR (r1), cell_vm_return);
751 else if (c == cell_vm_begin_expand)
753 push_cc (cons (CADR (r1), cell_nil), r1, CADDR (r1), cell_vm_return);
756 else if (c == cell_call_with_current_continuation)
759 goto call_with_current_continuation;
762 check_apply (cell_f, CAR (r1));
764 else if (t == TSYMBOL)
766 if (CAR (r1) == cell_symbol_call_with_values)
769 goto call_with_values;
771 if (CAR (r1) == cell_symbol_current_module)
776 if (CAR (r1) == cell_symbol_boot_module)
784 if (CAAR (r1) == cell_symbol_lambda)
786 formals = CADR (CAR (r1));
788 body = CDDR (CAR (r1));
789 p = pairlis (formals, CDR (r1), r0);
790 check_formals (r1, formals, args);
791 call_lambda (body, p, p, r0);
795 // write_error_ (CAR (r1));
797 push_cc (CAR (r1), r1, r0, cell_vm_apply2);
800 check_apply (r1, CAR (r2));
801 r1 = cons (r1, CDR (r2));
809 if (c == cell_symbol_pmatch_car)
811 push_cc (CADR (r1), r1, r0, cell_vm_eval_pmatch_car);
819 else if (c == cell_symbol_pmatch_cdr)
821 push_cc (CADR (r1), r1, r0, cell_vm_eval_pmatch_cdr);
829 else if (c == cell_symbol_quote)
836 else if (c == cell_symbol_begin)
838 else if (c == cell_symbol_lambda)
840 r1 = make_closure_ (CADR (r1), CDDR (r1), r0);
843 else if (c == cell_symbol_if)
848 else if (c == cell_symbol_set_x)
850 push_cc (CAR (CDDR (r1)), r1, r0, cell_vm_eval_set_x);
853 r1 = set_env_x (CADR (r2), r1, r0);
856 else if (c == cell_vm_macro_expand)
858 push_cc (CADR (r1), r1, r0, cell_vm_eval_macro_expand_eval);
860 eval_macro_expand_eval:
861 push_cc (r1, r2, r0, cell_vm_eval_macro_expand_expand);
863 eval_macro_expand_expand:
868 if (TYPE (r1) == TPAIR && (CAR (r1) == cell_symbol_define || CAR (r1) == cell_symbol_define_macro))
870 global_p = CAAR (r0) != cell_closure;
871 macro_p = CAR (r1) == cell_symbol_define_macro;
875 if (TYPE (CADR (r1)) == TPAIR)
879 entry = assq (name, g_macros);
881 macro_set_x (name, cell_f);
885 entry = module_variable (r0, name);
887 module_define_x (m0, name, cell_f);
891 if (TYPE (CADR (r1)) != TPAIR)
893 push_cc (CAR (CDDR (r1)), r2, cons (cons (CADR (r1), CADR (r1)), r0), cell_vm_eval_define);
898 p = pairlis (CADR (r1), CADR (r1), r0);
899 formals = CDR (CADR (r1));
902 if (macro_p || global_p)
903 expand_variable (body, formals);
904 r1 = cons (cell_symbol_lambda, cons (formals, body));
905 push_cc (r1, r2, p, cell_vm_eval_define);
910 if (TYPE (CADR (r2)) == TPAIR)
914 entry = macro_get_handle (name);
915 r1 = MAKE_MACRO (name, r1);
916 set_cdr_x (entry, r1);
920 entry = module_variable (r0, name);
921 set_cdr_x (entry, r1);
925 entry = cons (name, r1);
926 aa = cons (entry, cell_nil);
927 set_cdr_x (aa, cdr (r0));
929 cl = module_variable (r0, cell_closure);
932 r1 = cell_unspecified;
935 push_cc (CAR (r1), r1, r0, cell_vm_eval_check_func);
939 push_cc (CDR (r2), r2, r0, cell_vm_eval2);
942 r1 = cons (CAR (r2), r1);
946 else if (t == TSYMBOL)
948 if (r1 == cell_symbol_boot_module)
950 if (r1 == cell_symbol_current_module)
952 if (r1 == cell_symbol_begin) // FIXME
957 r1 = assert_defined (r1, module_ref (r0, r1));
960 else if (t == TVARIABLE)
962 r1 = CDR (VARIABLE (r1));
965 else if (t == TBROKEN_HEART)
966 error (cell_symbol_system_error, r1);
975 if (TYPE (r1) != TPAIR || CAR (r1) == cell_symbol_quote)
978 if (CAR (r1) == cell_symbol_lambda)
980 push_cc (CDDR (r1), r1, r0, cell_vm_macro_expand_lambda);
988 if (TYPE (r1) == TPAIR && (macro = get_macro (CAR (r1))) != cell_f)
990 r1 = cons (macro, CDR (r1));
991 push_cc (r1, cell_nil, r0, cell_vm_macro_expand);
995 if (CAR (r1) == cell_symbol_define || CAR (r1) == cell_symbol_define_macro)
997 push_cc (CDDR (r1), r1, r0, cell_vm_macro_expand_define);
1002 if (CAR (r1) == cell_symbol_define_macro)
1004 push_cc (r1, r1, r0, cell_vm_macro_expand_define_macro);
1006 macro_expand_define_macro:
1012 if (CAR (r1) == cell_symbol_set_x)
1014 push_cc (CDDR (r1), r1, r0, cell_vm_macro_expand_set_x);
1022 if (TYPE (r1) == TPAIR
1023 && TYPE (CAR (r1)) == TSYMBOL
1024 && CAR (r1) != cell_symbol_begin
1025 && ((macro = macro_get_handle (cell_symbol_portable_macro_expand)) != cell_f)
1026 && ((expanders = module_ref (r0, cell_symbol_sc_expander_alist)) != cell_undefined)
1027 && ((macro = assq (CAR (r1), expanders)) != cell_f))
1029 sc_expand = module_ref (r0, cell_symbol_macro_expand);
1031 if (sc_expand != cell_undefined && sc_expand != cell_f)
1033 r1 = cons (sc_expand, cons (r1, cell_nil));
1038 push_cc (CAR (r1), r1, r0, cell_vm_macro_expand_car);
1044 if (CDR (r1) == cell_nil)
1047 push_cc (CDR (r1), r1, r0, cell_vm_macro_expand_cdr);
1058 x = cell_unspecified;
1059 while (r1 != cell_nil)
1062 if (TYPE (r1) == TPAIR)
1064 if (CAAR (r1) == cell_symbol_primitive_load)
1066 program = cons (CAR (r1), cell_nil);
1067 push_cc (program, r1, r0, cell_vm_begin_primitive_load);
1069 begin_primitive_load:
1075 if (TYPE (r1) == TPAIR && TYPE (CAR (r1)) == TPAIR)
1077 if (CAAR (r1) == cell_symbol_begin)
1078 r1 = append2 (CDAR (r1), CDR (r1));
1080 if (CDR (r1) == cell_nil)
1085 push_cc (CAR (r1), r1, r0, cell_vm_begin_eval);
1096 x = cell_unspecified;
1097 while (r1 != cell_nil)
1101 if (TYPE (r1) == TPAIR)
1103 if (TYPE (CAR (r1)) == TPAIR && CAAR (r1) == cell_symbol_begin)
1104 r1 = append2 (CDAR (r1), CDR (r1));
1105 if (CAAR (r1) == cell_symbol_primitive_load)
1107 push_cc (CADR (CAR (r1)), r1, r0, cell_vm_begin_expand_primitive_load);
1108 goto eval; // FIXME: expand too?!
1109 begin_expand_primitive_load:
1110 if (TYPE (r1) == TNUMBER && VALUE (r1) == 0)
1112 else if (TYPE (r1) == TSTRING)
1113 input = set_current_input_port (open_input_file (r1));
1114 else if (TYPE (r1) == TPORT)
1115 input = set_current_input_port (r1);
1119 push_cc (input, r2, r0, cell_vm_return);
1120 x = read_input_file_env (r0);
1122 module_printer (m0);
1126 set_current_input_port (input);
1127 r1 = cons (cell_symbol_begin, r1);
1134 push_cc (CAR (r1), r1, r0, cell_vm_begin_expand_macro);
1144 expand_variable (CAR (r1), cell_nil);
1145 push_cc (CAR (r1), r1, r0, cell_vm_begin_expand_eval);
1155 push_cc (CAR (r1), r1, r0, cell_vm_if_expr);
1165 if (CDDR (r1) != cell_nil)
1167 r1 = CAR (CDDR (r1));
1170 r1 = cell_unspecified;
1173 call_with_current_continuation:
1175 x = MAKE_CONTINUATION (g_continuations++);
1176 v = make_vector__ (STACK_SIZE - g_stack);
1177 for (t = g_stack; t < STACK_SIZE; t++)
1178 vector_set_x_ (v, t - g_stack, g_stack_array[t]);
1179 CONTINUATION (x) = v;
1181 push_cc (cons (CAR (r1), cons (x, cell_nil)), x, r0, cell_vm_call_with_current_continuation2);
1183 call_with_current_continuation2:
1184 v = make_vector__ (STACK_SIZE - g_stack);
1185 for (t = g_stack; t < STACK_SIZE; t++)
1186 vector_set_x_ (v, t - g_stack, g_stack_array[t]);
1187 CONTINUATION (r2) = v;
1191 push_cc (cons (CAR (r1), cell_nil), r1, r0, cell_vm_call_with_values2);
1194 if (TYPE (r1) == TVALUES)
1196 r1 = cons (CADR (r2), r1);
1207 apply (SCM f, SCM x, SCM a) ///((internal))
1209 push_cc (cons (f, x), cell_unspecified, r0, cell_unspecified);
1211 return eval_apply ();
1215 mes_g_stack (SCM a) ///((internal))
1217 //g_stack = g_free + ARENA_SIZE;
1218 g_stack = STACK_SIZE;
1227 init_symbol (long x, long type, char const *name)
1230 int length = strlen (name);
1231 SCM string = make_string (name, length);
1233 CDR (x) = STRING (string);
1234 hash_set_x (g_symbols, string, x);
1238 mes_symbols () ///((internal))
1240 g_free = cell_symbol_test + 1;
1241 g_symbol_max = g_free;
1242 g_symbols = make_hash_table_ (500);
1244 init_symbol (cell_nil, TSPECIAL, "()");
1245 init_symbol (cell_f, TSPECIAL, "#f");
1246 init_symbol (cell_t, TSPECIAL, "#t");
1247 init_symbol (cell_dot, TSPECIAL, ".");
1248 init_symbol (cell_arrow, TSPECIAL, "=>");
1249 init_symbol (cell_undefined, TSPECIAL, "*undefined*");
1250 init_symbol (cell_unspecified, TSPECIAL, "*unspecified*");
1251 init_symbol (cell_closure, TSPECIAL, "*closure*");
1252 init_symbol (cell_circular, TSPECIAL, "*circular*");
1253 init_symbol (cell_begin, TSPECIAL, "*begin*");
1254 init_symbol (cell_call_with_current_continuation, TSPECIAL, "*call/cc*");
1256 init_symbol (cell_vm_apply, TSPECIAL, "core:apply");
1257 init_symbol (cell_vm_apply2, TSPECIAL, "*vm-apply2*");
1258 init_symbol (cell_vm_begin, TSPECIAL, "*vm-begin*");
1259 init_symbol (cell_vm_begin_eval, TSPECIAL, "*vm:begin-eval*");
1260 init_symbol (cell_vm_begin_expand, TSPECIAL, "core:eval");
1261 init_symbol (cell_vm_begin_expand_eval, TSPECIAL, "*vm:begin-expand-eval*");
1262 init_symbol (cell_vm_begin_expand_macro, TSPECIAL, "*vm:begin-expand-macro*");
1263 init_symbol (cell_vm_begin_expand_primitive_load, TSPECIAL, "*vm:core:begin-expand-primitive-load*");
1264 init_symbol (cell_vm_begin_primitive_load, TSPECIAL, "*vm:core:begin-primitive-load*");
1265 init_symbol (cell_vm_begin_read_input_file, TSPECIAL, "*vm-begin-read-input-file*");
1266 init_symbol (cell_vm_call_with_current_continuation2, TSPECIAL, "*vm-call-with-current-continuation2*");
1267 init_symbol (cell_vm_call_with_values2, TSPECIAL, "*vm-call-with-values2*");
1268 init_symbol (cell_vm_eval, TSPECIAL, "core:eval-expanded");
1269 init_symbol (cell_vm_eval2, TSPECIAL, "*vm-eval2*");
1270 init_symbol (cell_vm_eval_check_func, TSPECIAL, "*vm-eval-check-func*");
1271 init_symbol (cell_vm_eval_define, TSPECIAL, "*vm-eval-define*");
1272 init_symbol (cell_vm_eval_macro_expand_eval, TSPECIAL, "*vm:eval-macro-expand-eval*");
1273 init_symbol (cell_vm_eval_macro_expand_expand, TSPECIAL, "*vm:eval-macro-expand-expand*");
1274 init_symbol (cell_vm_eval_pmatch_car, TSPECIAL, "*vm-eval-pmatch-car*");
1275 init_symbol (cell_vm_eval_pmatch_cdr, TSPECIAL, "*vm-eval-pmatch-cdr*");
1276 init_symbol (cell_vm_eval_set_x, TSPECIAL, "*vm-eval-set!*");
1277 init_symbol (cell_vm_evlis, TSPECIAL, "*vm-evlis*");
1278 init_symbol (cell_vm_evlis2, TSPECIAL, "*vm-evlis2*");
1279 init_symbol (cell_vm_evlis3, TSPECIAL, "*vm-evlis3*");
1280 init_symbol (cell_vm_if, TSPECIAL, "*vm-if*");
1281 init_symbol (cell_vm_if_expr, TSPECIAL, "*vm-if-expr*");
1282 init_symbol (cell_vm_macro_expand, TSPECIAL, "core:macro-expand");
1283 init_symbol (cell_vm_macro_expand_car, TSPECIAL, "*vm:core:macro-expand-car*");
1284 init_symbol (cell_vm_macro_expand_cdr, TSPECIAL, "*vm:macro-expand-cdr*");
1285 init_symbol (cell_vm_macro_expand_define, TSPECIAL, "*vm:core:macro-expand-define*");
1286 init_symbol (cell_vm_macro_expand_define_macro, TSPECIAL, "*vm:core:macro-expand-define-macro*");
1287 init_symbol (cell_vm_macro_expand_lambda, TSPECIAL, "*vm:core:macro-expand-lambda*");
1288 init_symbol (cell_vm_macro_expand_set_x, TSPECIAL, "*vm:core:macro-expand-set!*");
1289 init_symbol (cell_vm_return, TSPECIAL, "*vm-return*");
1291 init_symbol (cell_symbol_dot, TSYMBOL, "*dot*");
1292 init_symbol (cell_symbol_lambda, TSYMBOL, "lambda");
1293 init_symbol (cell_symbol_begin, TSYMBOL, "begin");
1294 init_symbol (cell_symbol_if, TSYMBOL, "if");
1295 init_symbol (cell_symbol_quote, TSYMBOL, "quote");
1296 init_symbol (cell_symbol_define, TSYMBOL, "define");
1297 init_symbol (cell_symbol_define_macro, TSYMBOL, "define-macro");
1299 init_symbol (cell_symbol_quasiquote, TSYMBOL, "quasiquote");
1300 init_symbol (cell_symbol_unquote, TSYMBOL, "unquote");
1301 init_symbol (cell_symbol_unquote_splicing, TSYMBOL, "unquote-splicing");
1302 init_symbol (cell_symbol_syntax, TSYMBOL, "syntax");
1303 init_symbol (cell_symbol_quasisyntax, TSYMBOL, "quasisyntax");
1304 init_symbol (cell_symbol_unsyntax, TSYMBOL, "unsyntax");
1305 init_symbol (cell_symbol_unsyntax_splicing, TSYMBOL, "unsyntax-splicing");
1307 init_symbol (cell_symbol_set_x, TSYMBOL, "set!");
1309 init_symbol (cell_symbol_sc_expand, TSYMBOL, "sc-expand");
1310 init_symbol (cell_symbol_macro_expand, TSYMBOL, "macro-expand");
1311 init_symbol (cell_symbol_portable_macro_expand, TSYMBOL, "portable-macro-expand");
1312 init_symbol (cell_symbol_sc_expander_alist, TSYMBOL, "*sc-expander-alist*");
1314 init_symbol (cell_symbol_call_with_values, TSYMBOL, "call-with-values");
1315 init_symbol (cell_symbol_call_with_current_continuation, TSYMBOL, "call-with-current-continuation");
1316 init_symbol (cell_symbol_boot_module, TSYMBOL, "boot-module");
1317 init_symbol (cell_symbol_current_module, TSYMBOL, "current-module");
1318 init_symbol (cell_symbol_primitive_load, TSYMBOL, "primitive-load");
1319 init_symbol (cell_symbol_read_input_file, TSYMBOL, "read-input-file");
1320 init_symbol (cell_symbol_write, TSYMBOL, "write");
1321 init_symbol (cell_symbol_display, TSYMBOL, "display");
1323 init_symbol (cell_symbol_car, TSYMBOL, "car");
1324 init_symbol (cell_symbol_cdr, TSYMBOL, "cdr");
1325 init_symbol (cell_symbol_not_a_number, TSYMBOL, "not-a-number");
1326 init_symbol (cell_symbol_not_a_pair, TSYMBOL, "not-a-pair");
1327 init_symbol (cell_symbol_system_error, TSYMBOL, "system-error");
1328 init_symbol (cell_symbol_throw, TSYMBOL, "throw");
1329 init_symbol (cell_symbol_unbound_variable, TSYMBOL, "unbound-variable");
1330 init_symbol (cell_symbol_wrong_number_of_args, TSYMBOL, "wrong-number-of-args");
1331 init_symbol (cell_symbol_wrong_type_arg, TSYMBOL, "wrong-type-arg");
1333 init_symbol (cell_symbol_buckets, TSYMBOL, "buckets");
1334 init_symbol (cell_symbol_builtin, TSYMBOL, "<builtin>");
1335 init_symbol (cell_symbol_frame, TSYMBOL, "<frame>");
1336 init_symbol (cell_symbol_hashq_table, TSYMBOL, "<hashq-table>");
1337 init_symbol (cell_symbol_module, TSYMBOL, "<module>");
1338 init_symbol (cell_symbol_procedure, TSYMBOL, "procedure");
1339 init_symbol (cell_symbol_record_type, TSYMBOL, "<record-type>");
1340 init_symbol (cell_symbol_size, TSYMBOL, "size");
1341 init_symbol (cell_symbol_stack, TSYMBOL, "<stack>");
1343 init_symbol (cell_symbol_argv, TSYMBOL, "%argv");
1344 init_symbol (cell_symbol_mes_datadir, TSYMBOL, "%datadir");
1345 init_symbol (cell_symbol_mes_version, TSYMBOL, "%version");
1347 init_symbol (cell_symbol_internal_time_units_per_second, TSYMBOL, "internal-time-units-per-second");
1348 init_symbol (cell_symbol_compiler, TSYMBOL, "%compiler");
1349 init_symbol (cell_symbol_arch, TSYMBOL, "%arch");
1351 init_symbol (cell_symbol_pmatch_car, TSYMBOL, "pmatch-car");
1352 init_symbol (cell_symbol_pmatch_cdr, TSYMBOL, "pmatch-cdr");
1354 init_symbol (cell_type_bytes, TSYMBOL, "<cell:bytes>");
1355 init_symbol (cell_type_char, TSYMBOL, "<cell:char>");
1356 init_symbol (cell_type_closure, TSYMBOL, "<cell:closure>");
1357 init_symbol (cell_type_continuation, TSYMBOL, "<cell:continuation>");
1358 init_symbol (cell_type_function, TSYMBOL, "<cell:function>");
1359 init_symbol (cell_type_keyword, TSYMBOL, "<cell:keyword>");
1360 init_symbol (cell_type_macro, TSYMBOL, "<cell:macro>");
1361 init_symbol (cell_type_number, TSYMBOL, "<cell:number>");
1362 init_symbol (cell_type_pair, TSYMBOL, "<cell:pair>");
1363 init_symbol (cell_type_port, TSYMBOL, "<cell:port>");
1364 init_symbol (cell_type_ref, TSYMBOL, "<cell:ref>");
1365 init_symbol (cell_type_special, TSYMBOL, "<cell:special>");
1366 init_symbol (cell_type_string, TSYMBOL, "<cell:string>");
1367 init_symbol (cell_type_struct, TSYMBOL, "<cell:struct>");
1368 init_symbol (cell_type_symbol, TSYMBOL, "<cell:symbol>");
1369 init_symbol (cell_type_values, TSYMBOL, "<cell:values>");
1370 init_symbol (cell_type_variable, TSYMBOL, "<cell:variable>");
1371 init_symbol (cell_type_vector, TSYMBOL, "<cell:vector>");
1372 init_symbol (cell_type_broken_heart, TSYMBOL, "<cell:broken-heart>");
1374 init_symbol (cell_symbol_test, TSYMBOL, "%%test");
1377 a = acons (cell_symbol_call_with_values, cell_symbol_call_with_values, a);
1378 a = acons (cell_symbol_boot_module, cell_symbol_boot_module, a);
1379 a = acons (cell_symbol_current_module, cell_symbol_current_module, a);
1380 a = acons (cell_symbol_call_with_current_continuation, cell_call_with_current_continuation, a);
1382 a = acons (cell_symbol_mes_version, MAKE_STRING0 (MES_VERSION), a);
1383 a = acons (cell_symbol_mes_datadir, MAKE_STRING0 (g_datadir), a);
1385 a = acons (cell_type_bytes, MAKE_NUMBER (TBYTES), a);
1386 a = acons (cell_type_char, MAKE_NUMBER (TCHAR), a);
1387 a = acons (cell_type_closure, MAKE_NUMBER (TCLOSURE), a);
1388 a = acons (cell_type_continuation, MAKE_NUMBER (TCONTINUATION), a);
1389 a = acons (cell_type_keyword, MAKE_NUMBER (TKEYWORD), a);
1390 a = acons (cell_type_macro, MAKE_NUMBER (TMACRO), a);
1391 a = acons (cell_type_number, MAKE_NUMBER (TNUMBER), a);
1392 a = acons (cell_type_pair, MAKE_NUMBER (TPAIR), a);
1393 a = acons (cell_type_port, MAKE_NUMBER (TPORT), a);
1394 a = acons (cell_type_ref, MAKE_NUMBER (TREF), a);
1395 a = acons (cell_type_special, MAKE_NUMBER (TSPECIAL), a);
1396 a = acons (cell_type_string, MAKE_NUMBER (TSTRING), a);
1397 a = acons (cell_type_struct, MAKE_NUMBER (TSTRUCT), a);
1398 a = acons (cell_type_symbol, MAKE_NUMBER (TSYMBOL), a);
1399 a = acons (cell_type_values, MAKE_NUMBER (TVALUES), a);
1400 a = acons (cell_type_variable, MAKE_NUMBER (TVARIABLE), a);
1401 a = acons (cell_type_vector, MAKE_NUMBER (TVECTOR), a);
1402 a = acons (cell_type_broken_heart, MAKE_NUMBER (TBROKEN_HEART), a);
1404 a = acons (cell_closure, a, a);
1410 mes_environment (int argc, char *argv[])
1412 SCM a = mes_symbols ();
1414 char *compiler = "gnuc";
1420 a = acons (cell_symbol_compiler, MAKE_STRING0 (compiler), a);
1430 #error arch not supported
1432 a = acons (cell_symbol_arch, MAKE_STRING0 (arch), a);
1436 for (int i = argc - 1; i >= 0; i--)
1437 lst = cons (MAKE_STRING0 (argv[i]), lst);
1438 a = acons (cell_symbol_argv, lst, a);
1441 return mes_g_stack (a);
1445 init_builtin (SCM builtin_type, char const *name, int arity, SCM (*function) (SCM), SCM a)
1447 SCM s = cstring_to_symbol (name);
1449 make_builtin (builtin_type, symbol_to_string (s), MAKE_NUMBER (arity),
1450 MAKE_NUMBER (function)), a);
1454 make_builtin_type () ///(internal))
1456 SCM record_type = cell_symbol_record_type;
1457 SCM fields = cell_nil;
1458 fields = cons (cstring_to_symbol ("address"), fields);
1459 fields = cons (cstring_to_symbol ("arity"), fields);
1460 fields = cons (cstring_to_symbol ("name"), fields);
1461 fields = cons (fields, cell_nil);
1462 fields = cons (cell_symbol_builtin, fields);
1463 return make_struct (record_type, fields, cell_unspecified);
1467 make_builtin (SCM builtin_type, SCM name, SCM arity, SCM function)
1469 SCM values = cell_nil;
1470 values = cons (function, values);
1471 values = cons (arity, values);
1472 values = cons (name, values);
1473 values = cons (cell_symbol_builtin, values);
1474 return make_struct (builtin_type, values, cstring_to_symbol ("builtin-printer"));
1478 builtin_name (SCM builtin)
1480 return struct_ref_ (builtin, 3);
1484 builtin_arity (SCM builtin)
1486 return struct_ref_ (builtin, 4);
1491 builtin_function (SCM builtin)
1493 return VALUE (struct_ref_ (builtin, 5));
1496 SCM (*builtin_function (SCM builtin)) (SCM)
1498 return (function1_t) VALUE (struct_ref_ (builtin, 5));
1505 return (TYPE (x) == TSTRUCT && struct_ref_ (x, 2) == cell_symbol_builtin) ? cell_t : cell_f;
1509 builtin_printer (SCM builtin)
1511 fdputs ("#<procedure ", __stdout);
1512 display_ (builtin_name (builtin));
1513 fdputc (' ', __stdout);
1514 int arity = VALUE (builtin_arity (builtin));
1516 fdputc ('_', __stdout);
1519 fdputc ('(', __stdout);
1520 for (int i = 0; i < arity; i++)
1523 fdputc (' ', __stdout);
1524 fdputc ('_', __stdout);
1527 fdputc ('>', __stdout);
1531 apply_builtin (SCM fn, SCM x) ///((internal))
1533 int arity = VALUE (builtin_arity (fn));
1534 if ((arity > 0 || arity == -1) && x != cell_nil && TYPE (CAR (x)) == TVALUES)
1535 x = cons (CADAR (x), CDR (x));
1536 if ((arity > 1 || arity == -1) && x != cell_nil && TYPE (CDR (x)) == TPAIR && TYPE (CADR (x)) == TVALUES)
1537 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
1540 FUNCTION fp = builtin_function (fn) if (arity == 0)
1542 else if (arity == 1)
1543 return fp (CAR (x));
1544 else if (arity == 2)
1545 return fp (CAR (x), CADR (x));
1546 else if (arity == 3)
1547 return fp (CAR (x), CADR (x), CAR (CDDR (x)));
1548 else if (arity == -1)
1550 #else // !__M2_PLANET__
1553 //function0_t fp = f->function;
1554 SCM (*fp) (void) = (function0_t) builtin_function (fn);
1557 else if (arity == 1)
1559 //function1_t fp = f->function;
1560 SCM (*fp) (SCM) = (function1_t) builtin_function (fn);
1561 return fp (CAR (x));
1563 else if (arity == 2)
1565 //function2_t fp = f->function;
1566 SCM (*fp) (SCM, SCM) = (function2_t) builtin_function (fn);
1567 return fp (CAR (x), CADR (x));
1569 else if (arity == 3)
1571 //function3_t fp = f->function;
1572 SCM (*fp) (SCM, SCM, SCM) = (function3_t) builtin_function (fn);
1573 return fp (CAR (x), CADR (x), CAR (CDDR (x)));
1575 else if (arity == -1)
1577 //functionn_t fp = f->function;
1578 SCM (*fp) (SCM) = (function1_t) builtin_function (fn);
1581 #endif //! __M2_PLANET__
1582 return cell_unspecified;
1586 mes_builtins (SCM a) ///((internal))
1588 // TODO minimal: cons, car, cdr, list, null_p, eq_p minus, plus
1589 // display_, display_error_, getenv
1591 SCM builtin_type = make_builtin_type ();
1594 a = init_builtin (builtin_type, "gc-check", 0, (function1_t) & gc_check, a);
1595 a = init_builtin (builtin_type, "gc", 0, (function1_t) & gc, a);
1597 a = init_builtin (builtin_type, "hashq", 2, (function1_t) & hashq, a);
1598 a = init_builtin (builtin_type, "hash", 2, (function1_t) & hash, a);
1599 a = init_builtin (builtin_type, "hashq-get-handle", 3, (function1_t) & hashq_get_handle, a);
1600 a = init_builtin (builtin_type, "hashq-ref", 3, (function1_t) & hashq_ref, a);
1601 a = init_builtin (builtin_type, "hash-ref", 3, (function1_t) & hash_ref, a);
1602 a = init_builtin (builtin_type, "hashq-set!", 3, (function1_t) & hashq_set_x, a);
1603 a = init_builtin (builtin_type, "hash-set!", 3, (function1_t) & hash_set_x, a);
1604 a = init_builtin (builtin_type, "hash-table-printer", 1, (function1_t) & hash_table_printer, a);
1605 a = init_builtin (builtin_type, "make-hash-table", 1, (function1_t) & make_hash_table, a);
1607 a = init_builtin (builtin_type, "core:display", 1, (function1_t) & display_, a);
1608 a = init_builtin (builtin_type, "core:display-error", 1, (function1_t) & display_error_, a);
1609 a = init_builtin (builtin_type, "core:display-port", 2, (function1_t) & display_port_, a);
1610 a = init_builtin (builtin_type, "core:write", 1, (function1_t) & write_, a);
1611 a = init_builtin (builtin_type, "core:write-error", 1, (function1_t) & write_error_, a);
1612 a = init_builtin (builtin_type, "core:write-port", 2, (function1_t) & write_port_, a);
1613 a = init_builtin (builtin_type, "exit", 1, (function1_t) & exit_, a);
1614 a = init_builtin (builtin_type, "frame-printer", 1, (function1_t) & frame_printer, a);
1615 a = init_builtin (builtin_type, "make-stack", -1, (function1_t) & make_stack, a);
1616 a = init_builtin (builtin_type, "stack-length", 1, (function1_t) & stack_length, a);
1617 a = init_builtin (builtin_type, "stack-ref", 2, (function1_t) & stack_ref, a);
1618 a = init_builtin (builtin_type, "xassq", 2, (function1_t) & xassq, a);
1619 a = init_builtin (builtin_type, "memq", 2, (function1_t) & memq, a);
1620 a = init_builtin (builtin_type, "equal2?", 2, (function1_t) & equal2_p, a);
1621 a = init_builtin (builtin_type, "last-pair", 1, (function1_t) & last_pair, a);
1622 a = init_builtin (builtin_type, "pair?", 1, (function1_t) & pair_p, a);
1624 a = init_builtin (builtin_type, ">", -1, (function1_t) & greater_p, a);
1625 a = init_builtin (builtin_type, "<", -1, (function1_t) & less_p, a);
1626 a = init_builtin (builtin_type, "=", -1, (function1_t) & is_p, a);
1627 a = init_builtin (builtin_type, "-", -1, (function1_t) & minus, a);
1628 a = init_builtin (builtin_type, "+", -1, (function1_t) & plus, a);
1629 a = init_builtin (builtin_type, "/", -1, (function1_t) & divide, a);
1630 a = init_builtin (builtin_type, "modulo", 2, (function1_t) & modulo, a);
1631 a = init_builtin (builtin_type, "*", -1, (function1_t) & multiply, a);
1632 a = init_builtin (builtin_type, "logand", -1, (function1_t) & logand, a);
1633 a = init_builtin (builtin_type, "logior", -1, (function1_t) & logior, a);
1634 a = init_builtin (builtin_type, "lognot", 1, (function1_t) & lognot, a);
1635 a = init_builtin (builtin_type, "logxor", -1, (function1_t) & logxor, a);
1636 a = init_builtin (builtin_type, "ash", 2, (function1_t) & ash, a);
1638 a = init_builtin (builtin_type, "core:make-cell", 3, (function1_t) & make_cell_, a);
1639 a = init_builtin (builtin_type, "core:type", 1, (function1_t) & type_, a);
1640 a = init_builtin (builtin_type, "core:car", 1, (function1_t) & car_, a);
1641 a = init_builtin (builtin_type, "core:cdr", 1, (function1_t) & cdr_, a);
1642 a = init_builtin (builtin_type, "cons", 2, (function1_t) & cons, a);
1643 a = init_builtin (builtin_type, "car", 1, (function1_t) & car, a);
1644 a = init_builtin (builtin_type, "cdr", 1, (function1_t) & cdr, a);
1645 a = init_builtin (builtin_type, "list", -1, (function1_t) & list, a);
1646 a = init_builtin (builtin_type, "null?", 1, (function1_t) & null_p, a);
1647 a = init_builtin (builtin_type, "eq?", 2, (function1_t) & eq_p, a);
1648 a = init_builtin (builtin_type, "values", -1, (function1_t) & values, a);
1649 a = init_builtin (builtin_type, "acons", 3, (function1_t) & acons, a);
1650 a = init_builtin (builtin_type, "length", 1, (function1_t) & length, a);
1651 a = init_builtin (builtin_type, "error", 2, (function1_t) & error, a);
1652 a = init_builtin (builtin_type, "append2", 2, (function1_t) & append2, a);
1653 a = init_builtin (builtin_type, "append-reverse", 2, (function1_t) & append_reverse, a);
1654 a = init_builtin (builtin_type, "core:reverse!", 2, (function1_t) & reverse_x_, a);
1655 a = init_builtin (builtin_type, "pairlis", 3, (function1_t) & pairlis, a);
1656 a = init_builtin (builtin_type, "assq", 2, (function1_t) & assq, a);
1657 a = init_builtin (builtin_type, "assoc", 2, (function1_t) & assoc, a);
1658 a = init_builtin (builtin_type, "set-car!", 2, (function1_t) & set_car_x, a);
1659 a = init_builtin (builtin_type, "set-cdr!", 2, (function1_t) & set_cdr_x, a);
1660 a = init_builtin (builtin_type, "set-env!", 3, (function1_t) & set_env_x, a);
1661 a = init_builtin (builtin_type, "macro-get-handle", 1, (function1_t) & macro_get_handle, a);
1662 a = init_builtin (builtin_type, "add-formals", 2, (function1_t) & add_formals, a);
1663 a = init_builtin (builtin_type, "eval-apply", 0, (function1_t) & eval_apply, a);
1664 a = init_builtin (builtin_type, "make-builtin-type", 0, (function1_t) & make_builtin_type, a);
1665 a = init_builtin (builtin_type, "make-builtin", 4, (function1_t) & make_builtin, a);
1666 a = init_builtin (builtin_type, "builtin-name", 1, (function1_t) & builtin_name, a);
1667 a = init_builtin (builtin_type, "builtin-arity", 1, (function1_t) & builtin_arity, a);
1668 a = init_builtin (builtin_type, "builtin?", 1, (function1_t) & builtin_p, a);
1669 a = init_builtin (builtin_type, "builtin-printer", 1, (function1_t) & builtin_printer, a);
1671 a = init_builtin (builtin_type, "make-module-type", 0, (function1_t) & make_module_type, a);
1672 a = init_builtin (builtin_type, "module-printer", 1, (function1_t) & module_printer, a);
1673 a = init_builtin (builtin_type, "module-variable", 2, (function1_t) & module_variable, a);
1674 a = init_builtin (builtin_type, "module-ref", 2, (function1_t) & module_ref, a);
1675 a = init_builtin (builtin_type, "module-define!", 3, (function1_t) & module_define_x, a);
1677 a = init_builtin (builtin_type, "peek-byte", 0, (function1_t) & peek_byte, a);
1678 a = init_builtin (builtin_type, "read-byte", 0, (function1_t) & read_byte, a);
1679 a = init_builtin (builtin_type, "unread-byte", 1, (function1_t) & unread_byte, a);
1680 a = init_builtin (builtin_type, "peek-char", 0, (function1_t) & peek_char, a);
1681 a = init_builtin (builtin_type, "read-char", -1, (function1_t) & read_char, a);
1682 a = init_builtin (builtin_type, "unread-char", 1, (function1_t) & unread_char, a);
1683 a = init_builtin (builtin_type, "write-char", -1, (function1_t) & write_char, a);
1684 a = init_builtin (builtin_type, "write-byte", -1, (function1_t) & write_byte, a);
1685 a = init_builtin (builtin_type, "getenv", 1, (function1_t) & getenv_, a);
1686 a = init_builtin (builtin_type, "setenv", 2, (function1_t) & setenv_, a);
1687 a = init_builtin (builtin_type, "access?", 2, (function1_t) & access_p, a);
1688 a = init_builtin (builtin_type, "current-input-port", 0, (function1_t) & current_input_port, a);
1689 a = init_builtin (builtin_type, "open-input-file", 1, (function1_t) & open_input_file, a);
1690 a = init_builtin (builtin_type, "open-input-string", 1, (function1_t) & open_input_string, a);
1691 a = init_builtin (builtin_type, "set-current-input-port", 1, (function1_t) & set_current_input_port, a);
1692 a = init_builtin (builtin_type, "current-output-port", 0, (function1_t) & current_output_port, a);
1693 a = init_builtin (builtin_type, "current-error-port", 0, (function1_t) & current_error_port, a);
1694 a = init_builtin (builtin_type, "open-output-file", -1, (function1_t) & open_output_file, a);
1695 a = init_builtin (builtin_type, "set-current-output-port", 1, (function1_t) & set_current_output_port, a);
1696 a = init_builtin (builtin_type, "set-current-error-port", 1, (function1_t) & set_current_error_port, a);
1697 a = init_builtin (builtin_type, "chmod", 2, (function1_t) & chmod_, a);
1698 a = init_builtin (builtin_type, "isatty?", 1, (function1_t) & isatty_p, a);
1699 a = init_builtin (builtin_type, "primitive-fork", 0, (function1_t) & primitive_fork, a);
1700 a = init_builtin (builtin_type, "execl", 2, (function1_t) & execl_, a);
1701 a = init_builtin (builtin_type, "core:waitpid", 2, (function1_t) & waitpid_, a);
1702 a = init_builtin (builtin_type, "current-time", 0, (function1_t) & current_time, a);
1703 a = init_builtin (builtin_type, "gettimeofday", 0, (function1_t) & gettimeofday_, a);
1704 a = init_builtin (builtin_type, "get-internal-run-time", 0, (function1_t) & get_internal_run_time, a);
1705 a = init_builtin (builtin_type, "getcwd", 0, (function1_t) & getcwd_, a);
1706 a = init_builtin (builtin_type, "dup", 1, (function1_t) & dup_, a);
1707 a = init_builtin (builtin_type, "dup2", 2, (function1_t) & dup2_, a);
1708 a = init_builtin (builtin_type, "delete-file", 1, (function1_t) & delete_file, a);
1710 a = init_builtin (builtin_type, "core:read-input-file-env", 2, (function1_t) & read_input_file_env_, a);
1711 a = init_builtin (builtin_type, "read-input-file-env", 1, (function1_t) & read_input_file_env, a);
1712 a = init_builtin (builtin_type, "read-env", 1, (function1_t) & read_env, a);
1713 a = init_builtin (builtin_type, "reader-read-sexp", 3, (function1_t) & reader_read_sexp, a);
1714 a = init_builtin (builtin_type, "reader-read-character", 0, (function1_t) & reader_read_character, a);
1715 a = init_builtin (builtin_type, "reader-read-binary", 0, (function1_t) & reader_read_binary, a);
1716 a = init_builtin (builtin_type, "reader-read-octal", 0, (function1_t) & reader_read_octal, a);
1717 a = init_builtin (builtin_type, "reader-read-hex", 0, (function1_t) & reader_read_hex, a);
1718 a = init_builtin (builtin_type, "reader-read-string", 0, (function1_t) & reader_read_string, a);
1720 a = init_builtin (builtin_type, "string=?", 2, (function1_t) & string_equal_p, a);
1721 a = init_builtin (builtin_type, "symbol->string", 1, (function1_t) & symbol_to_string, a);
1722 a = init_builtin (builtin_type, "symbol->keyword", 1, (function1_t) & symbol_to_keyword, a);
1723 a = init_builtin (builtin_type, "keyword->string", 1, (function1_t) & keyword_to_string, a);
1724 a = init_builtin (builtin_type, "string->symbol", 1, (function1_t) & string_to_symbol, a);
1725 a = init_builtin (builtin_type, "make-symbol", 1, (function1_t) & make_symbol, a);
1726 a = init_builtin (builtin_type, "string->list", 1, (function1_t) & string_to_list, a);
1727 a = init_builtin (builtin_type, "list->string", 1, (function1_t) & list_to_string, a);
1728 a = init_builtin (builtin_type, "read-string", -1, (function1_t) & read_string, a);
1729 a = init_builtin (builtin_type, "string-append", -1, (function1_t) & string_append, a);
1730 a = init_builtin (builtin_type, "string-length", 1, (function1_t) & string_length, a);
1731 a = init_builtin (builtin_type, "string-ref", 2, (function1_t) & string_ref, a);
1733 a = init_builtin (builtin_type, "make-struct", 3, (function1_t) & make_struct, a);
1734 a = init_builtin (builtin_type, "struct-length", 1, (function1_t) & struct_length, a);
1735 a = init_builtin (builtin_type, "struct-ref", 2, (function1_t) & struct_ref, a);
1736 a = init_builtin (builtin_type, "struct-set!", 3, (function1_t) & struct_set_x, a);
1738 a = init_builtin (builtin_type, "core:make-vector", 1, (function1_t) & make_vector_, a);
1739 a = init_builtin (builtin_type, "vector-length", 1, (function1_t) & vector_length, a);
1740 a = init_builtin (builtin_type, "vector-ref", 2, (function1_t) & vector_ref, a);
1741 a = init_builtin (builtin_type, "vector-entry", 1, (function1_t) & vector_entry, a);
1742 a = init_builtin (builtin_type, "vector-set!", 3, (function1_t) & vector_set_x, a);
1743 a = init_builtin (builtin_type, "list->vector", 1, (function1_t) & list_to_vector, a);
1744 a = init_builtin (builtin_type, "vector->list", 1, (function1_t) & vector_to_list, a);
1750 try_open_boot (char *file_name, char const *boot, char const *location)
1752 strcpy (file_name + strlen (file_name), boot);
1755 eputs ("mes: reading boot-0 [");
1761 int fd = mes_open (file_name, O_RDONLY, 0);
1762 if (g_debug && fd > 0)
1764 eputs ("mes: read boot-0: ");
1776 char file_name[1024];
1777 strcpy (g_datadir, ".");
1780 eputs (";;; pkgdatadir=");
1781 eputs (MES_PKGDATADIR);
1784 if (getenv ("MES_BOOT"))
1785 strcpy (boot, getenv ("MES_BOOT"));
1787 strcpy (boot, "boot-0.scm");
1788 if (getenv ("MES_PREFIX"))
1790 strcpy (g_datadir, getenv ("MES_PREFIX"));
1791 strcpy (file_name, g_datadir);
1792 strcpy (file_name + strlen (file_name), "/module/mes/");
1793 __stdin = try_open_boot (file_name, boot, "MES_PREFIX");
1796 strcpy (g_datadir, getenv ("MES_PREFIX"));
1797 strcpy (g_datadir + strlen (g_datadir), "/share/mes");
1798 strcpy (file_name, g_datadir);
1799 strcpy (file_name + strlen (file_name), "/module/mes/");
1800 __stdin = try_open_boot (file_name, boot, "MES_PREFIX/share/mes");
1805 strcpy (g_datadir, MES_PKGDATADIR);
1806 strcpy (file_name, g_datadir);
1807 strcpy (file_name + strlen (file_name), "/module/mes/");
1808 __stdin = try_open_boot (file_name, boot, "pkgdatadir");
1812 strcpy (g_datadir, "mes");
1813 strcpy (file_name, g_datadir);
1814 strcpy (file_name + strlen (file_name), "/module/mes/");
1815 __stdin = try_open_boot (file_name, boot, "mes");
1820 __stdin = try_open_boot (file_name, boot, "<boot>");
1824 eputs ("mes: boot failed: no such file: ");
1832 read_boot () ///((internal))
1834 r2 = read_input_file_env (r0);
1843 if (p = getenv ("MES_DEBUG"))
1851 main (int argc, char *argv[])
1855 SCM a = mes_environment (argc, argv);
1856 a = mes_builtins (a);
1858 m0 = make_initial_module (a);
1859 g_macros = make_hash_table_ (0);
1862 module_printer (m0);
1864 SCM program = read_boot ();
1865 push_cc (r2, cell_unspecified, r0, cell_unspecified);
1869 eputs ("\ngc stats: [");
1870 eputs (itoa (g_free));
1875 eputs ("program: ");
1879 r3 = cell_vm_begin_expand;
1889 module_printer (m0);
1891 eputs ("\ngc stats: [");
1892 eputs (itoa (g_free));
1897 eputs (itoa (g_free));
1904 write_error_ (g_ports);