1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017,2018 Jan (janneke) 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/>.
27 int ARENA_SIZE = 20000000; // 32B: 100 MiB, 64b: 200 MiB
29 //int MAX_ARENA_SIZE = 89478485; // 32b: ~1GiB
30 int MAX_ARENA_SIZE = 80000000; // 32b: ~1GiB
32 int MAX_ARENA_SIZE = 200000000; // 32b: 2GiB, 64b: 4GiB
43 SCM g_continuations = 0;
55 SCM g_macros = 1; // cell_nil
58 enum type_t {TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TREF, TSPECIAL, TSTRING, TSYMBOL, TVALUES, TVARIABLE, TVECTOR, TBROKEN_HEART};
67 int (*function) (void);
72 typedef SCM (*function0_t) (void);
73 typedef SCM (*function1_t) (SCM);
74 typedef SCM (*function2_t) (SCM, SCM);
75 typedef SCM (*function3_t) (SCM, SCM, SCM);
76 typedef SCM (*functionn_t) (SCM);
79 function0_t function0;
80 function1_t function1;
81 function2_t function2;
82 function3_t function3;
83 functionn_t functionn;
115 struct scm *g_cells = foobar;
116 struct scm *g_news = foobar;
118 struct scm *g_cells = 0;
119 struct scm *g_news = 0;
122 struct scm scm_nil = {TSPECIAL, "()",0};
123 struct scm scm_f = {TSPECIAL, "#f",0};
124 struct scm scm_t = {TSPECIAL, "#t",0};
125 struct scm scm_dot = {TSPECIAL, ".",0};
126 struct scm scm_arrow = {TSPECIAL, "=>",0};
127 struct scm scm_undefined = {TSPECIAL, "*undefined*",0};
128 struct scm scm_unspecified = {TSPECIAL, "*unspecified*",0};
129 struct scm scm_closure = {TSPECIAL, "*closure*",0};
130 struct scm scm_circular = {TSPECIAL, "*circular*",0};
131 struct scm scm_begin = {TSPECIAL, "*begin*",0};
133 struct scm scm_symbol_dot = {TSYMBOL, "*dot*",0};
134 struct scm scm_symbol_lambda = {TSYMBOL, "lambda",0};
135 struct scm scm_symbol_begin = {TSYMBOL, "begin",0};
136 struct scm scm_symbol_if = {TSYMBOL, "if",0};
137 struct scm scm_symbol_quote = {TSYMBOL, "quote",0};
138 struct scm scm_symbol_define = {TSYMBOL, "define",0};
139 struct scm scm_symbol_define_macro = {TSYMBOL, "define-macro",0};
141 struct scm scm_symbol_quasiquote = {TSYMBOL, "quasiquote", 0};
142 struct scm scm_symbol_unquote = {TSYMBOL, "unquote", 0};
143 struct scm scm_symbol_unquote_splicing = {TSYMBOL, "unquote-splicing", 0};
144 struct scm scm_symbol_syntax = {TSYMBOL, "syntax",0};
145 struct scm scm_symbol_quasisyntax = {TSYMBOL, "quasisyntax", 0};
146 struct scm scm_symbol_unsyntax = {TSYMBOL, "unsyntax", 0};
147 struct scm scm_symbol_unsyntax_splicing = {TSYMBOL, "unsyntax-splicing", 0};
149 struct scm scm_symbol_set_x = {TSYMBOL, "set!",0};
151 struct scm scm_symbol_sc_expand = {TSYMBOL, "sc-expand",0};
152 struct scm scm_symbol_macro_expand = {TSYMBOL, "macro-expand",0};
153 struct scm scm_symbol_portable_macro_expand = {TSYMBOL, "portable-macro-expand",0};
154 struct scm scm_symbol_sc_expander_alist = {TSYMBOL, "*sc-expander-alist*",0};
156 struct scm scm_symbol_call_with_values = {TSYMBOL, "call-with-values",0};
157 struct scm scm_call_with_current_continuation = {TSPECIAL, "*call/cc*",0};
158 struct scm scm_symbol_call_with_current_continuation = {TSYMBOL, "call-with-current-continuation",0};
159 struct scm scm_symbol_current_module = {TSYMBOL, "current-module",0};
160 struct scm scm_symbol_primitive_load = {TSYMBOL, "primitive-load",0};
161 struct scm scm_symbol_read_input_file = {TSYMBOL, "read-input-file",0};
162 struct scm scm_symbol_write = {TSYMBOL, "write",0};
163 struct scm scm_symbol_display = {TSYMBOL, "display",0};
165 struct scm scm_symbol_throw = {TSYMBOL, "throw",0};
166 struct scm scm_symbol_not_a_number = {TSYMBOL, "not-a-number",0};
167 struct scm scm_symbol_not_a_pair = {TSYMBOL, "not-a-pair",0};
168 struct scm scm_symbol_system_error = {TSYMBOL, "system-error",0};
169 struct scm scm_symbol_wrong_number_of_args = {TSYMBOL, "wrong-number-of-args",0};
170 struct scm scm_symbol_wrong_type_arg = {TSYMBOL, "wrong-type-arg",0};
171 struct scm scm_symbol_unbound_variable = {TSYMBOL, "unbound-variable",0};
173 struct scm scm_symbol_argv = {TSYMBOL, "%argv",0};
174 struct scm scm_symbol_mes_prefix = {TSYMBOL, "%prefix",0};
175 struct scm scm_symbol_mes_version = {TSYMBOL, "%version",0};
177 struct scm scm_symbol_car = {TSYMBOL, "car",0};
178 struct scm scm_symbol_cdr = {TSYMBOL, "cdr",0};
179 struct scm scm_symbol_pmatch_car = {TSYMBOL, "pmatch-car",0};
180 struct scm scm_symbol_pmatch_cdr = {TSYMBOL, "pmatch-cdr",0};
182 struct scm scm_vm_evlis = {TSPECIAL, "*vm-evlis*",0};
183 struct scm scm_vm_evlis2 = {TSPECIAL, "*vm-evlis2*",0};
184 struct scm scm_vm_evlis3 = {TSPECIAL, "*vm-evlis3*",0};
185 struct scm scm_vm_apply = {TSPECIAL, "core:apply",0};
186 struct scm scm_vm_apply2 = {TSPECIAL, "*vm-apply2*",0};
187 struct scm scm_vm_eval = {TSPECIAL, "core:eval",0};
189 struct scm scm_vm_eval_pmatch_car = {TSPECIAL, "*vm-eval-pmatch-car*",0};
190 struct scm scm_vm_eval_pmatch_cdr = {TSPECIAL, "*vm-eval-pmatch-cdr*",0};
191 struct scm scm_vm_eval_define = {TSPECIAL, "*vm-eval-define*",0};
193 struct scm scm_vm_eval_set_x = {TSPECIAL, "*vm-eval-set!*",0};
194 struct scm scm_vm_eval_macro_expand_eval = {TSPECIAL, "*vm:eval-macro-expand-eval*",0};
195 struct scm scm_vm_eval_macro_expand_expand = {TSPECIAL, "*vm:eval-macro-expand-expand*",0};
196 struct scm scm_vm_eval_check_func = {TSPECIAL, "*vm-eval-check-func*",0};
197 struct scm scm_vm_eval2 = {TSPECIAL, "*vm-eval2*",0};
198 struct scm scm_vm_macro_expand = {TSPECIAL, "core:macro-expand",0};
199 struct scm scm_vm_macro_expand_define = {TSPECIAL, "*vm:core:macro-expand-define*",0};
200 struct scm scm_vm_macro_expand_define_macro = {TSPECIAL, "*vm:core:macro-expand-define-macro*",0};
201 struct scm scm_vm_macro_expand_lambda = {TSPECIAL, "*vm:core:macro-expand-lambda*",0};
202 struct scm scm_vm_macro_expand_set_x = {TSPECIAL, "*vm:core:macro-expand-set!*",0};
203 struct scm scm_vm_begin_expand_primitive_load = {TSPECIAL, "*vm:core:begin-expand-primitive-load*",0};
204 struct scm scm_vm_begin_primitive_load = {TSPECIAL, "*vm:core:begin-primitive-load*",0};
205 struct scm scm_vm_macro_expand_car = {TSPECIAL, "*vm:core:macro-expand-car*",0};
206 struct scm scm_vm_macro_expand_cdr = {TSPECIAL, "*vm:macro-expand-cdr*",0};
207 struct scm scm_vm_begin_expand = {TSPECIAL, "*vm:begin-expand*",0};
208 struct scm scm_vm_begin_expand_eval = {TSPECIAL, "*vm:begin-expand-eval*",0};
209 struct scm scm_vm_begin_expand_macro = {TSPECIAL, "*vm:begin-expand-macro*",0};
210 struct scm scm_vm_begin = {TSPECIAL, "*vm-begin*",0};
211 struct scm scm_vm_begin_read_input_file = {TSPECIAL, "*vm-begin-read-input-file*",0};
212 struct scm scm_vm_begin_eval = {TSPECIAL, "*vm:begin-eval*",0};
213 struct scm scm_vm_if = {TSPECIAL, "*vm-if*",0};
214 struct scm scm_vm_if_expr = {TSPECIAL, "*vm-if-expr*",0};
215 struct scm scm_vm_call_with_values2 = {TSPECIAL, "*vm-call-with-values2*",0};
216 struct scm scm_vm_call_with_current_continuation2 = {TSPECIAL, "*vm-call-with-current-continuation2*",0};
217 struct scm scm_vm_return = {TSPECIAL, "*vm-return*",0};
219 struct scm scm_symbol_gnuc = {TSYMBOL, "%gnuc",0};
220 struct scm scm_symbol_mesc = {TSYMBOL, "%mesc",0};
222 struct scm scm_test = {TSYMBOL, "test",0};
225 #include "mes.mes.symbols.h"
227 #include "mes.symbols.h"
234 struct function g_functions[200];
237 #if !__GNUC__ || !_POSIX_SOURCE
240 #include "math.mes.h"
242 #include "posix.mes.h"
243 #include "reader.mes.h"
244 #include "vector.mes.h"
255 #define TYPE(x) g_cells[x].type
256 #define CAR(x) g_cells[x].car
257 #define CDR(x) g_cells[x].cdr
259 #define NTYPE(x) g_news[x].type
260 #define NCAR(x) g_news[x].car
261 #define NCDR(x) g_news[x].cdr
264 #define LENGTH(x) g_cells[x].car
265 #define REF(x) g_cells[x].car
266 #define STRING(x) g_cells[x].car
267 #define VARIABLE(x) g_cells[x].car
268 #define VARIABLE_GLOBAL_P(x) g_cells[x].cdr
270 #define CLOSURE(x) g_cells[x].cdr
271 #define CONTINUATION(x) g_cells[x].cdr
273 #define FUNCTION(x) g_functions[g_cells[x].cdr]
274 #define FUNCTION0(x) g_functions[g_cells[x].cdr].function
275 #define MACRO(x) g_cells[x].cdr
276 #define VALUE(x) g_cells[x].cdr
277 #define VECTOR(x) g_cells[x].cdr
279 #define NLENGTH(x) g_news[x].car
281 #define NVALUE(x) g_news[x].cdr
282 #define NVECTOR(x) g_news[x].cdr
285 #define CONTINUATION(x) g_cells[x].cdr
286 #define HITS(x) g_cells[x].hits
287 #define LENGTH(x) g_cells[x].length
288 #define NAME(x) g_cells[x].name
289 #define STRING(x) g_cells[x].string
290 #define VARIABLE(x) g_cells[x].variable
291 #define VARIABLE_GLOBAL_P(x) g_cells[x].cdr
293 #define CLOSURE(x) g_cells[x].closure
294 #define MACRO(x) g_cells[x].macro
295 #define REF(x) g_cells[x].ref
296 #define VALUE(x) g_cells[x].value
297 #define VECTOR(x) g_cells[x].vector
298 #define FUNCTION(x) g_functions[g_cells[x].function]
299 #define FUNCTION0(x) g_functions[g_cells[x].function].function0
301 #define NLENGTH(x) g_news[x].length
303 #define NVALUE(x) g_news[x].value
304 #define NVECTOR(x) g_news[x].vector
307 #define MAKE_CHAR(n) make_cell_ (tmp_num_ (TCHAR), 0, tmp_num2_ (n))
308 #define MAKE_CONTINUATION(n) make_cell_ (tmp_num_ (TCONTINUATION), n, g_stack)
309 #define MAKE_NUMBER(n) make_cell_ (tmp_num_ (TNUMBER), 0, tmp_num2_ (n))
310 #define MAKE_REF(n) make_cell_ (tmp_num_ (TREF), n, 0)
311 #define MAKE_STRING(x) make_cell_ (tmp_num_ (TSTRING), x, 0)
312 #define MAKE_KEYWORD(x) make_cell_ (tmp_num_ (TKEYWORD), x, 0)
313 #define MAKE_MACRO(name, x) make_cell_ (tmp_num_ (TMACRO), STRING (name), x)
315 #define CAAR(x) CAR (CAR (x))
316 #define CADR(x) CAR (CDR (x))
317 #define CDAR(x) CDR (CAR (x))
318 #define CDDR(x) CDR (CDR (x))
319 #define CADAR(x) CAR (CDR (CAR (x)))
320 #define CADDR(x) CAR (CDR (CDR (x)))
321 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
326 assert (g_free + n < ARENA_SIZE);
342 VALUE (tmp_num2) = x;
347 make_cell_ (SCM type, SCM car, SCM cdr)
350 assert (TYPE (type) == TNUMBER);
351 TYPE (x) = VALUE (type);
352 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER)
359 else if (VALUE (type) == TFUNCTION)
375 make_symbol_ (SCM s) ///((internal))
377 VALUE (tmp_num) = TSYMBOL;
378 SCM x = make_cell_ (tmp_num, s, 0);
379 g_symbols = cons (x, g_symbols);
384 list_of_char_equal_p (SCM a, SCM b) ///((internal))
386 while (a != cell_nil && b != cell_nil && VALUE (CAR (a)) == VALUE (CAR (b)))
388 assert (TYPE (CAR (a)) == TCHAR);
389 assert (TYPE (CAR (b)) == TCHAR);
393 return (a == cell_nil && b == cell_nil) ? cell_t : cell_f;
397 lookup_symbol_ (SCM s)
402 if (list_of_char_equal_p (STRING (CAR (x)), s) == cell_t)
409 x = make_symbol_ (s);
416 return MAKE_NUMBER (TYPE (x));
422 return (TYPE (x) != TCONTINUATION
423 && (TYPE (CAR (x)) == TPAIR // FIXME: this is weird
424 || TYPE (CAR (x)) == TREF
425 || TYPE (CAR (x)) == TSPECIAL
426 || TYPE (CAR (x)) == TSYMBOL
427 || TYPE (CAR (x)) == TSTRING)) ? CAR (x) : MAKE_NUMBER (CAR (x));
433 return (TYPE (x) != TCHAR
434 && TYPE (x) != TNUMBER
435 && (TYPE (CDR (x)) == TPAIR
436 || TYPE (CDR (x)) == TREF
437 || TYPE (CDR (x)) == TSPECIAL
438 || TYPE (CDR (x)) == TSYMBOL
439 || TYPE (CDR (x)) == TSTRING)) ? CDR (x) : MAKE_NUMBER (CDR (x));
445 assert (TYPE (x) == TFUNCTION);
446 return MAKE_NUMBER (FUNCTION (x).arity);
452 VALUE (tmp_num) = TPAIR;
453 return make_cell_ (tmp_num, x, y);
460 if (TYPE (x) != TPAIR)
461 error (cell_symbol_not_a_pair, cons (x, cell_symbol_car));
470 if (TYPE (x) != TPAIR)
471 error (cell_symbol_not_a_pair, cons (x, cell_symbol_cdr));
477 list (SCM x) ///((arity . n))
485 return x == cell_nil ? cell_t : cell_f;
492 || ((TYPE (x) == TKEYWORD && TYPE (y) == TKEYWORD
493 && STRING (x) == STRING (y)))
494 || (TYPE (x) == TCHAR && TYPE (y) == TCHAR
495 && VALUE (x) == VALUE (y))
496 || (TYPE (x) == TNUMBER && TYPE (y) == TNUMBER
497 && VALUE (x) == VALUE (y)))
502 values (SCM x) ///((arity . n))
510 acons (SCM key, SCM value, SCM alist)
512 return cons (cons (key, value), alist);
519 while (x != cell_nil)
522 if (TYPE (x) != TPAIR)
523 return MAKE_NUMBER (-1);
526 return MAKE_NUMBER (n);
529 SCM apply (SCM, SCM, SCM);
532 error (SCM key, SCM x)
536 if ((throw = assq_ref_env (cell_symbol_throw, r0)) != cell_undefined)
537 return apply (throw, cons (key, cons (x, cell_nil)), r0);
539 display_error_ (key);
547 cstring_to_list (char const* s)
552 p = cons (MAKE_CHAR (s[i]), p);
558 assert_defined (SCM x, SCM e) ///((internal))
560 if (e == cell_undefined)
561 return error (cell_symbol_unbound_variable, x);
566 check_formals (SCM f, SCM formals, SCM args) ///((internal))
568 int flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : VALUE (length (formals));
569 int alen = VALUE (length (args));
570 if (alen != flen && alen != -1 && flen != -1)
572 char *s = "apply: wrong number of arguments; expected: ";
579 SCM e = MAKE_STRING (cstring_to_list (s));
580 return error (cell_symbol_wrong_number_of_args, cons (e, f));
582 return cell_unspecified;
586 check_apply (SCM f, SCM e) ///((internal))
589 if (f == cell_f || f == cell_t)
593 if (f == cell_unspecified)
594 type = "*unspecified*";
595 if (f == cell_undefined)
596 type = "*undefined*";
597 if (TYPE (f) == TCHAR)
599 if (TYPE (f) == TNUMBER)
601 if (TYPE (f) == TSTRING)
606 char *s = "cannot apply: ";
612 SCM e = MAKE_STRING (cstring_to_list (s));
613 return error (cell_symbol_wrong_type_arg, cons (e, f));
615 return cell_unspecified;
619 gc_push_frame () ///((internal))
621 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
622 g_stack = cons (frame, g_stack);
627 append2 (SCM x, SCM y)
631 if (TYPE (x) != TPAIR)
632 error (cell_symbol_not_a_pair, cons (x, cell_append2));
633 return cons (car (x), append2 (cdr (x), y));
637 pairlis (SCM x, SCM y, SCM a)
641 if (TYPE (x) != TPAIR)
642 return cons (cons (x, y), a);
643 return cons (cons (car (x), car (y)),
644 pairlis (cdr (x), cdr (y), a));
650 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
651 && x != cell_nil && TYPE (CAR (x)) == TVALUES)
652 x = cons (CADAR (x), CDR (x));
653 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
654 && x != cell_nil && TYPE (CDR (x)) == TPAIR && TYPE (CADR (x)) == TVALUES)
655 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
656 switch (FUNCTION (fn).arity)
658 #if __MESC__ || !_POSIX_SOURCE
659 case 0: return (FUNCTION (fn).function) ();
660 case 1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (CAR (x));
661 case 2: return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x));
662 case 3: return ((SCM(*)(SCM,SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x), CAR (CDDR (x)));
663 case -1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
664 default: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
666 case 0: return FUNCTION (fn).function0 ();
667 case 1: return FUNCTION (fn).function1 (CAR (x));
668 case 2: return FUNCTION (fn).function2 (CAR (x), CADR (x));
669 case 3: return FUNCTION (fn).function3 (CAR (x), CADR (x), CAR (CDDR (x)));
670 case -1: return FUNCTION (fn).functionn (x);
674 return cell_unspecified;
686 while (a != cell_nil && v != VALUE (CAAR (a)))
693 while (a != cell_nil && v != STRING (CAAR (a)))
700 while (a != cell_nil && x != CAAR (a))
703 return a != cell_nil ? CAR (a) : cell_f;
707 assq_ref_env (SCM x, SCM a)
711 return cell_undefined;
716 set_car_x (SCM x, SCM e)
718 if (TYPE (x) != TPAIR)
719 error (cell_symbol_not_a_pair, cons (x, cell_set_car_x));
721 return cell_unspecified;
725 set_cdr_x (SCM x, SCM e)
727 if (TYPE (x) != TPAIR)
728 error (cell_symbol_not_a_pair, cons (x, cell_set_cdr_x));
730 return cell_unspecified;
734 set_env_x (SCM x, SCM e, SCM a)
737 if (TYPE (x) == TVARIABLE)
740 p = assert_defined (x, assq (x, a));
741 if (TYPE (p) != TPAIR)
742 error (cell_symbol_not_a_pair, cons (p, x));
743 return set_cdr_x (p, e);
747 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
749 SCM cl = cons (cons (cell_closure, x), x);
752 return cell_unspecified;
756 make_closure_ (SCM args, SCM body, SCM a) ///((internal))
758 return make_cell_ (tmp_num_ (TCLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
762 make_variable_ (SCM var, SCM global_p) ///((internal))
764 return make_cell_ (tmp_num_ (TVARIABLE), var, global_p);
768 lookup_macro_ (SCM x, SCM a) ///((internal))
770 if (TYPE (x) != TSYMBOL)
774 return MACRO (CDR (m));
779 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
788 return cell_unspecified;
792 gc_peek_frame () ///((internal))
794 SCM frame = CAR (g_stack);
797 r3 = CAR (CDDR (frame));
798 r0 = CADR (CDDR (frame));
803 gc_pop_frame () ///((internal))
805 SCM frame = gc_peek_frame (g_stack);
806 g_stack = CDR (g_stack);
810 char const* string_to_cstring (SCM s);
813 add_formals (SCM formals, SCM x)
815 while (TYPE (x) == TPAIR)
817 formals = cons (CAR (x), formals);
820 if (TYPE (x) == TSYMBOL)
821 formals = cons (x, formals);
826 formal_p (SCM x, SCM formals) /// ((internal))
828 if (TYPE (formals) == TSYMBOL)
834 while (TYPE (formals) == TPAIR && CAR (formals) != x)
835 formals = CDR (formals);
836 if (TYPE (formals) == TSYMBOL)
838 return TYPE (formals) == TPAIR;
842 expand_variable_ (SCM x, SCM formals, int top_p) ///((internal))
844 while (TYPE (x) == TPAIR)
846 if (TYPE (CAR (x)) == TPAIR)
848 if (CAAR (x) == cell_symbol_lambda)
850 SCM f = CAR (CDAR (x));
851 formals = add_formals (formals, f);
853 else if (CAAR (x) == cell_symbol_define
854 || CAAR (x) == cell_symbol_define_macro)
856 SCM f = CAR (CDAR (x));
857 formals = add_formals (formals, f);
859 if (CAAR (x) != cell_symbol_quote)
860 expand_variable_ (CAR (x), formals, 0);
864 if (CAR (x) == cell_symbol_lambda)
867 formals = add_formals (formals, f);
870 else if (CAR (x) == cell_symbol_define
871 || CAR (x) == cell_symbol_define_macro)
874 if (top_p && TYPE (f) == TPAIR)
876 formals = add_formals (formals, f);
879 else if (CAR (x) == cell_symbol_quote)
880 return cell_unspecified;
881 else if (TYPE (CAR (x)) == TSYMBOL
882 && CAR (x) != cell_begin
883 && CAR (x) != cell_symbol_begin
884 && CAR (x) != cell_symbol_current_module
885 && CAR (x) != cell_symbol_primitive_load
886 && CAR (x) != cell_symbol_if // HMM
887 && !formal_p (CAR (x), formals))
889 SCM v = assq (CAR (x), r0);
891 CAR (x) = make_variable_ (v, cell_t);
897 return cell_unspecified;
901 expand_variable (SCM x, SCM formals) ///((internal))
903 return expand_variable_ (x, formals, 1);
913 case cell_vm_evlis: goto evlis;
914 case cell_vm_evlis2: goto evlis2;
915 case cell_vm_evlis3: goto evlis3;
916 case cell_vm_apply: goto apply;
917 case cell_vm_apply2: goto apply2;
918 case cell_vm_eval: goto eval;
919 case cell_vm_eval_pmatch_car: goto eval_pmatch_car;
920 case cell_vm_eval_pmatch_cdr: goto eval_pmatch_cdr;
921 case cell_vm_eval_define: goto eval_define;
922 case cell_vm_eval_set_x: goto eval_set_x;
923 case cell_vm_eval_macro_expand_eval: goto eval_macro_expand_eval;
924 case cell_vm_eval_macro_expand_expand: goto eval_macro_expand_expand;
925 case cell_vm_eval_check_func: goto eval_check_func;
926 case cell_vm_eval2: goto eval2;
927 case cell_vm_macro_expand: goto macro_expand;
928 case cell_vm_macro_expand_define: goto macro_expand_define;
929 case cell_vm_macro_expand_define_macro: goto macro_expand_define_macro;
930 case cell_vm_macro_expand_lambda: goto macro_expand_lambda;
931 case cell_vm_macro_expand_set_x: goto macro_expand_set_x;
932 case cell_vm_macro_expand_car: goto macro_expand_car;
933 case cell_vm_macro_expand_cdr: goto macro_expand_cdr;
934 case cell_vm_begin: goto begin;
935 case cell_vm_begin_eval: goto begin_eval;
936 case cell_vm_begin_primitive_load: goto begin_primitive_load;
937 case cell_vm_begin_expand: goto begin_expand;
938 case cell_vm_begin_expand_eval: goto begin_expand_eval;
939 case cell_vm_begin_expand_macro: goto begin_expand_macro;
940 case cell_vm_begin_expand_primitive_load: goto begin_expand_primitive_load;
941 case cell_vm_if: goto vm_if;
942 case cell_vm_if_expr: goto if_expr;
943 case cell_vm_call_with_current_continuation2: goto call_with_current_continuation2;
944 case cell_vm_call_with_values2: goto call_with_values2;
945 case cell_vm_return: goto vm_return;
946 case cell_unspecified: return r1;
956 if (TYPE (r1) != TPAIR)
958 push_cc (CAR (r1), r1, r0, cell_vm_evlis2);
961 push_cc (CDR (r2), r1, r0, cell_vm_evlis3);
969 switch (TYPE (CAR (r1)))
973 check_formals (CAR (r1), MAKE_NUMBER (FUNCTION (CAR (r1)).arity), CDR (r1));
974 r1 = call (CAR (r1), CDR (r1)); /// FIXME: move into eval_apply
979 SCM cl = CLOSURE (CAR (r1));
980 SCM body = CDDR (cl);
981 SCM formals = CADR (cl);
985 check_formals (CAR (r1), formals, CDR (r1));
986 SCM p = pairlis (formals, args, aa);
987 call_lambda (body, p, aa, r0);
993 g_stack = CONTINUATION (CAR (r1));
1004 push_cc (cons (CADR (r1), CADDR (r1)), r1, r0, cell_vm_return);
1009 push_cc (CADR (r1), r1, CADDR (r1), cell_vm_return);
1012 case cell_call_with_current_continuation:
1015 goto call_with_current_continuation;
1017 default: check_apply (cell_f, CAR (r1));
1022 if (CAR (r1) == cell_symbol_call_with_values)
1025 goto call_with_values;
1027 if (CAR (r1) == cell_symbol_current_module)
1038 case cell_symbol_lambda:
1040 SCM formals = CADR (CAR (r1));
1041 SCM args = CDR (r1);
1042 SCM body = CDDR (CAR (r1));
1043 SCM p = pairlis (formals, CDR (r1), r0);
1044 check_formals (r1, formals, args);
1045 call_lambda (body, p, p, r0);
1051 push_cc (CAR (r1), r1, r0, cell_vm_apply2);
1054 check_apply (r1, CAR (r2));
1055 r1 = cons (r1, CDR (r2));
1066 case cell_symbol_pmatch_car:
1068 push_cc (CADR (r1), r1, r0, cell_vm_eval_pmatch_car);
1076 case cell_symbol_pmatch_cdr:
1078 push_cc (CADR (r1), r1, r0, cell_vm_eval_pmatch_cdr);
1086 case cell_symbol_quote:
1093 case cell_symbol_begin: goto begin;
1094 case cell_symbol_lambda:
1096 r1 = make_closure_ (CADR (r1), CDDR (r1), r0);
1099 case cell_symbol_if:
1104 case cell_symbol_set_x:
1106 push_cc (CAR (CDDR (r1)), r1, r0, cell_vm_eval_set_x);
1109 r1 = set_env_x (CADR (r2), r1, r0);
1112 case cell_vm_macro_expand:
1114 push_cc (CADR (r1), r1, r0, cell_vm_eval_macro_expand_eval);
1116 eval_macro_expand_eval:
1117 push_cc (r1, r2, r0, cell_vm_eval_macro_expand_expand);
1119 eval_macro_expand_expand:
1124 if (TYPE (r1) == TPAIR
1125 && (CAR (r1) == cell_symbol_define
1126 || CAR (r1) == cell_symbol_define_macro))
1128 int global_p = CAAR (r0) != cell_closure;
1129 int macro_p = CAR (r1) == cell_symbol_define_macro;
1132 SCM name = CADR (r1);
1133 if (TYPE (CADR (r1)) == TPAIR)
1137 SCM entry = assq (name, g_macros);
1138 if (entry == cell_f)
1140 entry = cons (name, cell_f);
1141 g_macros = cons (entry, g_macros);
1146 SCM entry = assq (name, r0);
1147 if (entry == cell_f)
1149 entry = cons (name, cell_f);
1150 SCM aa = cons (entry, cell_nil);
1151 set_cdr_x (aa, cdr (r0));
1157 if (TYPE (CADR (r1)) != TPAIR)
1159 push_cc (CAR (CDDR (r1)), r2, cons (cons (CADR (r1), CADR (r1)), r0), cell_vm_eval_define);
1164 SCM p = pairlis (CADR (r1), CADR (r1), r0);
1165 SCM formals = CDR (CADR (r1));
1166 SCM body = CDDR (r1);
1168 if (macro_p || global_p)
1169 expand_variable (body, formals);
1170 r1 = cons (cell_symbol_lambda, cons (formals, body));
1171 push_cc (r1, r2, p, cell_vm_eval_define);
1175 SCM name = CADR (r2);
1176 if (TYPE (CADR (r2)) == TPAIR)
1179 // hmm, mes needs a reminder...
1180 global_p = CAAR (r0) != cell_closure;
1181 macro_p = CAR (r2) == cell_symbol_define_macro;
1185 SCM entry = assq (name, g_macros);
1186 r1 = MAKE_MACRO (name, r1);
1187 set_cdr_x (entry, r1);
1191 SCM entry = assq (name, r0);
1192 set_cdr_x (entry, r1);
1196 SCM entry = cons (name, r1);
1197 SCM aa = cons (entry, cell_nil);
1198 set_cdr_x (aa, cdr (r0));
1200 SCM cl = assq (cell_closure, r0);
1203 r1 = cell_unspecified;
1206 push_cc (CAR (r1), r1, r0, cell_vm_eval_check_func);
1209 push_cc (CDR (r2), r2, r0, cell_vm_eval2);
1212 r1 = cons (CAR (r2), r1);
1219 if (r1 == cell_symbol_current_module)
1221 if (r1 == cell_symbol_begin) // FIXME
1226 r1 = assert_defined (r1, assq_ref_env (r1, r0));
1231 r1 = CDR (VARIABLE (r1));
1234 default: goto vm_return;
1242 if (TYPE (r1) != TPAIR || CAR (r1) == cell_symbol_quote)
1245 if (CAR (r1) == cell_symbol_lambda)
1247 push_cc (CDDR (r1), r1, r0, cell_vm_macro_expand_lambda);
1249 macro_expand_lambda:
1255 if (TYPE (r1) == TPAIR
1256 && (macro = lookup_macro_ (CAR (r1), g_macros)) != cell_f)
1258 r1 = cons (macro, CDR (r1));
1259 push_cc (r1, cell_nil, r0, cell_vm_macro_expand);
1263 if (CAR (r1) == cell_symbol_define
1264 || CAR (r1) == cell_symbol_define_macro)
1266 push_cc (CDDR (r1), r1, r0, cell_vm_macro_expand_define);
1268 macro_expand_define:
1271 if (CAR (r1) == cell_symbol_define_macro)
1273 push_cc (r1, r1, r0, cell_vm_macro_expand_define_macro);
1275 macro_expand_define_macro:
1281 if (CAR (r1) == cell_symbol_set_x)
1283 push_cc (CDDR (r1), r1, r0, cell_vm_macro_expand_set_x);
1291 if (TYPE (r1) == TPAIR
1292 && TYPE (CAR (r1)) == TSYMBOL
1293 && CAR (r1) != cell_symbol_begin
1294 && ((macro = assq (cell_symbol_portable_macro_expand, g_macros)) != cell_f)
1295 && ((expanders = assq_ref_env (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
1296 && ((macro = assq (CAR (r1), expanders)) != cell_f))
1298 SCM sc_expand = assq_ref_env (cell_symbol_macro_expand, r0);
1300 if (sc_expand != cell_undefined && sc_expand != cell_f)
1302 r1 = cons (sc_expand, cons (r1, cell_nil));
1307 push_cc (CAR (r1), r1, r0, cell_vm_macro_expand_car);
1313 if (CDR (r1) == cell_nil)
1316 push_cc (CDR (r1), r1, r0, cell_vm_macro_expand_cdr);
1327 x = cell_unspecified;
1328 while (r1 != cell_nil)
1331 if (TYPE (r1) == TPAIR)
1333 if (CAAR (r1) == cell_symbol_primitive_load)
1335 SCM program = cons (CAR (r1), cell_nil);
1336 push_cc (program, r1, r0, cell_vm_begin_primitive_load);
1338 begin_primitive_load:
1344 if (TYPE (r1) == TPAIR && TYPE (CAR (r1)) == TPAIR)
1346 if (CAAR (r1) == cell_symbol_begin)
1347 r1 = append2 (CDAR (r1), CDR (r1));
1349 if (CDR (r1) == cell_nil)
1354 push_cc (CAR (r1), r1, r0, cell_vm_begin_eval);
1365 x = cell_unspecified;
1366 while (r1 != cell_nil)
1370 if (TYPE (r1) == TPAIR)
1372 if (TYPE (CAR (r1)) == TPAIR && CAAR (r1) == cell_symbol_begin)
1373 r1 = append2 (CDAR (r1), CDR (r1));
1374 if (CAAR (r1) == cell_symbol_primitive_load)
1376 push_cc (CADR (CAR (r1)), r1, r0, cell_vm_begin_expand_primitive_load);
1377 goto eval; // FIXME: expand too?!
1378 begin_expand_primitive_load:;
1379 SCM input; // = current_input_port ();
1380 if (TYPE (r1) == TNUMBER && VALUE (r1) == 0)
1382 else if (TYPE (r1) == TSTRING)
1383 input = set_current_input_port (open_input_file (r1));
1387 push_cc (input, r2, r0, cell_vm_return);
1388 x = read_input_file_env (r0);
1392 set_current_input_port (input);
1393 r1 = cons (cell_symbol_begin, r1);
1400 push_cc (CAR (r1), r1, r0, cell_vm_begin_expand_macro);
1410 expand_variable (CAR (r1), cell_nil);
1411 //eputs ("expanded r1="); write_error_ (CAR (r1)); eputs ("\n");
1412 push_cc (CAR (r1), r1, r0, cell_vm_begin_expand_eval);
1422 push_cc (CAR (r1), r1, r0, cell_vm_if_expr);
1432 if (CDDR (r1) != cell_nil)
1434 r1 = CAR (CDDR (r1));
1437 r1 = cell_unspecified;
1440 call_with_current_continuation:
1442 x = MAKE_CONTINUATION (g_continuations++);
1444 push_cc (cons (CAR (r1), cons (x, cell_nil)), x, r0, cell_vm_call_with_current_continuation2);
1446 call_with_current_continuation2:
1447 CONTINUATION (r2) = g_stack;
1451 push_cc (cons (CAR (r1), cell_nil), r1, r0, cell_vm_call_with_values2);
1454 if (TYPE (r1) == TVALUES)
1456 r1 = cons (CADR (r2), r1);
1467 apply (SCM f, SCM x, SCM a) ///((internal))
1469 push_cc (cons (f, x), cell_unspecified, r0, cell_unspecified);
1471 return eval_apply ();
1475 mes_g_stack (SCM a) ///((internal))
1481 g_stack = cons (cell_nil, cell_nil);
1485 //
\f Environment setup
1488 make_tmps (struct scm* cells)
1491 cells[tmp].type = TCHAR;
1493 cells[tmp_num].type = TNUMBER;
1494 tmp_num2 = g_free++;
1495 cells[tmp_num2].type = TNUMBER;
1507 gc_init_cells () ///((internal))
1509 g_cells = (struct scm *)malloc (2*ARENA_SIZE*sizeof (struct scm));
1515 g_cells += sizeof (struct scm);
1525 gc_init_news () ///((internal))
1529 p -= sizeof (struct scm);
1530 p += ARENA_SIZE * sizeof (struct scm);
1533 g_news = g_cells-1 + ARENA_SIZE;
1536 NTYPE (0) = TVECTOR;
1540 g_news += sizeof (struct scm);
1550 mes_symbols () ///((internal))
1556 #include "mes.mes.symbols.i"
1558 #include "mes.symbols.i"
1561 g_symbol_max = g_free;
1562 make_tmps (g_cells);
1565 for (int i=1; i<g_symbol_max; i++)
1566 g_symbols = cons (i, g_symbols);
1571 #include "mes.mes.symbol-names.i"
1573 #include "mes.symbol-names.i"
1576 a = acons (cell_symbol_mes_version, MAKE_STRING (cstring_to_list (VERSION)), a);
1577 a = acons (cell_symbol_mes_prefix, MAKE_STRING (cstring_to_list (PREFIX)), a);
1579 a = acons (cell_symbol_call_with_values, cell_symbol_call_with_values, a);
1580 a = acons (cell_symbol_current_module, cell_symbol_current_module, a);
1581 a = acons (cell_symbol_call_with_current_continuation, cell_call_with_current_continuation, a);
1585 a = acons (cell_symbol_gnuc, cell_t, a);
1586 a = acons (cell_symbol_mesc, cell_f, a);
1588 a = acons (cell_symbol_gnuc, cell_f, a);
1589 a = acons (cell_symbol_mesc, cell_t, a);
1591 a = acons (cell_closure, a, a);
1597 mes_environment () ///((internal))
1599 SCM a = mes_symbols ();
1600 return mes_g_stack (a);
1604 mes_builtins (SCM a) ///((internal))
1606 #if !__GNUC__ || !_POSIX_SOURCE
1607 #include "mes.mes.i"
1609 // Do not sort: Order of these includes define builtins
1610 #include "posix.mes.i"
1611 #include "math.mes.i"
1612 #include "lib.mes.i"
1613 #include "vector.mes.i"
1615 #include "reader.mes.i"
1617 #include "gc.mes.environment.i"
1618 #include "lib.mes.environment.i"
1619 #include "math.mes.environment.i"
1620 #include "mes.mes.environment.i"
1621 #include "posix.mes.environment.i"
1622 #include "reader.mes.environment.i"
1623 #include "vector.mes.environment.i"
1627 // Do not sort: Order of these includes define builtins
1635 #include "gc.environment.i"
1636 #include "lib.environment.i"
1637 #include "math.environment.i"
1638 #include "mes.environment.i"
1639 #include "posix.environment.i"
1640 #include "reader.environment.i"
1641 #include "vector.environment.i"
1646 fputs ("functions: ", STDERR);
1647 fputs (itoa (g_function), STDERR);
1648 fputs ("\n", STDERR);
1649 for (int i = 0; i < g_function; i++)
1651 fputs ("[", STDERR);
1652 fputs (itoa (i), STDERR);
1653 fputs ("]: ", STDERR);
1654 fputs (g_functions[i].name, STDERR);
1655 fputs ("\n", STDERR);
1657 fputs ("\n", STDERR);
1663 SCM read_input_file_env (SCM);
1666 load_env (SCM a) ///((internal))
1672 if (getenv ("MES_BOOT"))
1673 strcpy (boot, getenv ("MES_BOOT"));
1675 strcpy (boot, "boot-0.scm");
1676 if (getenv ("MES_PREFIX"))
1678 strcpy (buf, getenv ("MES_PREFIX"));
1679 strcpy (buf + strlen (buf), "/module");
1680 strcpy (buf + strlen (buf), "/mes/");
1681 strcpy (buf + strlen (buf), boot);
1682 if (getenv ("MES_DEBUG"))
1684 eputs ("MES_PREFIX reading boot-0:");
1688 g_stdin = open (buf, O_RDONLY);
1692 char const *prefix = MODULEDIR "mes/";
1693 strcpy (buf, prefix);
1694 strcpy (buf + strlen (buf), boot);
1695 if (getenv ("MES_DEBUG"))
1697 eputs ("MODULEDIR reading boot-0:");
1701 g_stdin = open (buf, O_RDONLY);
1705 strcpy (buf, "module/mes/");
1706 strcpy (buf + strlen (buf), boot);
1707 if (getenv ("MES_DEBUG"))
1709 eputs (". reading boot-0:");
1713 g_stdin = open (buf, O_RDONLY);
1717 if (getenv ("MES_DEBUG"))
1719 eputs (". reading boot-0:");
1723 g_stdin = open (boot, O_RDONLY);
1727 eputs ("mes: boot failed: no such file: ");
1734 r0 = mes_builtins (r0);
1735 r2 = read_input_file_env (r0);
1741 bload_env (SCM a) ///((internal))
1744 char *mo = "mes/read-0-32.mo";
1745 g_stdin = open ("module/mes/read-0-32.mo", O_RDONLY);
1746 char *read0 = MODULEDIR "mes/read-0-32.mo";
1747 g_stdin = g_stdin >= 0 ? g_stdin : open (read0, O_RDONLY);
1749 char *mo ="mes/read-0.mo";
1750 g_stdin = open ("module/mes/read-0.mo", O_RDONLY);
1751 g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "mes/read-0.mo", O_RDONLY);
1756 eputs ("no such file: ");eputs (mo);eputs ("\n");
1759 assert (getchar () == 'M');
1760 assert (getchar () == 'E');
1761 assert (getchar () == 'S');
1764 eputs ("*GOT MES*\n");
1765 g_stack = getchar () << 8;
1766 g_stack += getchar ();
1768 char *p = (char*)g_cells;
1775 g_free = (p-(char*)g_cells) / sizeof (struct scm);
1779 r0 = mes_builtins (r0);
1782 set_env_x (cell_symbol_gnuc, cell_t, r0);
1783 set_env_x (cell_symbol_mesc, cell_f, r0);
1785 set_env_x (cell_symbol_gnuc, cell_f, r0);
1786 set_env_x (cell_symbol_mesc, cell_t, r0);
1791 eputs ("symbols: ");
1793 while (s && s != cell_nil)
1795 display_error_ (CAR (s));
1800 eputs ("functions: ");
1801 eputs (itoa (g_function));
1803 for (int i = 0; i < g_function; i++)
1808 eputs (g_functions[i].name);
1820 main (int argc, char *argv[])
1823 if (p = getenv ("MES_DEBUG"))
1827 eputs (";;; MODULEDIR=");
1831 if (p = getenv ("MES_MAX_ARENA"))
1832 MAX_ARENA_SIZE = atoi (p);
1833 if (p = getenv ("MES_ARENA"))
1834 ARENA_SIZE = atoi (p);
1835 GC_SAFETY = ARENA_SIZE / 400;
1836 if (argc > 1 && !strcmp (argv[1], "--help"))
1837 return puts ("Usage: mes [--dump|--load] < FILE\n");
1838 if (argc > 1 && !strcmp (argv[1], "--version"))
1840 puts ("Mes ");puts (VERSION);puts ("\n");
1845 r0 = mes_environment ();
1847 SCM program = (argc > 1 && !strcmp (argv[1], "--load"))
1848 ? bload_env (r0) : load_env (r0);
1849 g_tiny = argc > 2 && !strcmp (argv[2], "--tiny");
1850 if (argc > 1 && !strcmp (argv[1], "--dump"))
1854 for (int i=argc-1; i>=0; i--) lst = cons (MAKE_STRING (cstring_to_list (argv[i])), lst);
1855 r0 = acons (cell_symbol_argv, lst, r0); // FIXME
1856 r0 = acons (cell_symbol_argv, lst, r0);
1857 push_cc (r2, cell_unspecified, r0, cell_unspecified);
1861 eputs ("\ngc stats: [");
1862 eputs (itoa (g_free));
1867 eputs ("program: ");
1873 eputs ("symbols: ");
1874 write_error_ (g_symbols);
1877 r3 = cell_vm_begin_expand;
1886 eputs ("\ngc stats: [");
1887 eputs (itoa (g_free));
1890 eputs (itoa (g_free));