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/>.
28 int ARENA_SIZE = 10000000;
30 int ARENA_SIZE = 100000;
32 int MAX_ARENA_SIZE = 20000000;
34 //int GC_SAFETY_DIV = 400;
35 //int GC_SAFETY = ARENA_SIZE / 400;
44 SCM g_continuations = 0;
56 enum type_t {TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TREF, TSPECIAL, TSTRING, TSYMBOL, TVALUES, TVECTOR, TBROKEN_HEART};
65 int (*function) (void);
70 typedef SCM (*function0_t) (void);
71 typedef SCM (*function1_t) (SCM);
72 typedef SCM (*function2_t) (SCM, SCM);
73 typedef SCM (*function3_t) (SCM, SCM, SCM);
74 typedef SCM (*functionn_t) (SCM);
77 function0_t function0;
78 function1_t function1;
79 function2_t function2;
80 function3_t function3;
81 functionn_t functionn;
111 struct scm *g_cells = foobar;
112 struct scm *g_news = foobar;
114 struct scm *g_cells = 0;
115 struct scm *g_news = 0;
118 struct scm scm_nil = {TSPECIAL, "()",0};
119 struct scm scm_f = {TSPECIAL, "#f",0};
120 struct scm scm_t = {TSPECIAL, "#t",0};
121 struct scm scm_dot = {TSPECIAL, ".",0};
122 struct scm scm_arrow = {TSPECIAL, "=>",0};
123 struct scm scm_undefined = {TSPECIAL, "*undefined*",0};
124 struct scm scm_unspecified = {TSPECIAL, "*unspecified*",0};
125 struct scm scm_closure = {TSPECIAL, "*closure*",0};
126 struct scm scm_circular = {TSPECIAL, "*circular*",0};
127 struct scm scm_begin = {TSPECIAL, "*begin*",0};
129 struct scm scm_symbol_dot = {TSYMBOL, "*dot*",0};
130 struct scm scm_symbol_lambda = {TSYMBOL, "lambda",0};
131 struct scm scm_symbol_begin = {TSYMBOL, "begin",0};
132 struct scm scm_symbol_if = {TSYMBOL, "if",0};
133 struct scm scm_symbol_quote = {TSYMBOL, "quote",0};
134 #if 1 //MES_C_DEFINE // snarfing makes these always needed for linking
135 struct scm scm_symbol_define = {TSYMBOL, "define",0};
136 struct scm scm_symbol_define_macro = {TSYMBOL, "define-macro",0};
139 #if 1 //MES_C_READER // snarfing makes these always needed for linking
140 struct scm scm_symbol_quasiquote = {TSYMBOL, "quasiquote", 0};
141 struct scm scm_symbol_unquote = {TSYMBOL, "unquote", 0};
142 struct scm scm_symbol_unquote_splicing = {TSYMBOL, "unquote-splicing", 0};
143 struct scm scm_symbol_syntax = {TSYMBOL, "syntax",0};
144 struct scm scm_symbol_quasisyntax = {TSYMBOL, "quasisyntax", 0};
145 struct scm scm_symbol_unsyntax = {TSYMBOL, "unsyntax", 0};
146 struct scm scm_symbol_unsyntax_splicing = {TSYMBOL, "unsyntax-splicing", 0};
147 #endif // MES_C_READER
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_sc_expander_alist = {TSYMBOL, "*sc-expander-alist*",0};
155 struct scm scm_symbol_call_with_values = {TSYMBOL, "call-with-values",0};
156 struct scm scm_call_with_current_continuation = {TSPECIAL, "*call/cc*",0};
157 struct scm scm_symbol_call_with_current_continuation = {TSYMBOL, "call-with-current-continuation",0};
158 struct scm scm_symbol_current_module = {TSYMBOL, "current-module",0};
159 struct scm scm_symbol_primitive_load = {TSYMBOL, "primitive-load",0};
160 struct scm scm_symbol_read_input_file = {TSYMBOL, "read-input-file",0};
161 struct scm scm_symbol_write = {TSYMBOL, "write",0};
162 struct scm scm_symbol_display = {TSYMBOL, "display",0};
164 struct scm scm_symbol_throw = {TSYMBOL, "throw",0};
165 struct scm scm_symbol_not_a_pair = {TSYMBOL, "not-a-pair",0};
166 struct scm scm_symbol_system_error = {TSYMBOL, "system-error",0};
167 struct scm scm_symbol_wrong_number_of_args = {TSYMBOL, "wrong-number-of-args",0};
168 struct scm scm_symbol_wrong_type_arg = {TSYMBOL, "wrong-type-arg",0};
169 struct scm scm_symbol_unbound_variable = {TSYMBOL, "unbound-variable",0};
171 struct scm scm_symbol_argv = {TSYMBOL, "%argv",0};
172 struct scm scm_symbol_mes_prefix = {TSYMBOL, "%prefix",0};
173 struct scm scm_symbol_mes_version = {TSYMBOL, "%version",0};
175 struct scm scm_symbol_car = {TSYMBOL, "car",0};
176 struct scm scm_symbol_cdr = {TSYMBOL, "cdr",0};
177 struct scm scm_symbol_null_p = {TSYMBOL, "null?",0};
178 struct scm scm_symbol_eq_p = {TSYMBOL, "eq?",0};
179 struct scm scm_symbol_cons = {TSYMBOL, "cons",0};
181 struct scm scm_vm_evlis = {TSPECIAL, "*vm-evlis*",0};
182 struct scm scm_vm_evlis2 = {TSPECIAL, "*vm-evlis2*",0};
183 struct scm scm_vm_evlis3 = {TSPECIAL, "*vm-evlis3*",0};
184 struct scm scm_vm_apply = {TSPECIAL, "core:apply",0};
185 struct scm scm_vm_apply2 = {TSPECIAL, "*vm-apply2*",0};
186 struct scm scm_vm_eval = {TSPECIAL, "core:eval",0};
188 //MES_FIXED_PRIMITIVES
189 struct scm scm_vm_eval_car = {TSPECIAL, "*vm-eval-car*",0};
190 struct scm scm_vm_eval_cdr = {TSPECIAL, "*vm-eval-cdr*",0};
191 struct scm scm_vm_eval_cons = {TSPECIAL, "*vm-eval-cons*",0};
192 struct scm scm_vm_eval_null_p = {TSPECIAL, "*vm-eval-null-p*",0};
193 #if 1 //MES_C_DEFINE // snarfing makes these always needed for linking
194 struct scm scm_vm_eval_define = {TSPECIAL, "*vm-eval-define*",0};
197 struct scm scm_vm_eval_set_x = {TSPECIAL, "*vm-eval-set!*",0};
198 struct scm scm_vm_eval_macro = {TSPECIAL, "*vm-eval-macro*",0};
199 struct scm scm_vm_eval_check_func = {TSPECIAL, "*vm-eval-check-func*",0};
200 struct scm scm_vm_eval2 = {TSPECIAL, "*vm-eval2*",0};
201 struct scm scm_vm_macro_expand = {TSPECIAL, "core:macro-expand",0};
202 struct scm scm_vm_begin = {TSPECIAL, "*vm-begin*",0};
203 struct scm scm_vm_begin_read_input_file = {TSPECIAL, "*vm-begin-read-input-file*",0};
204 struct scm scm_vm_begin2 = {TSPECIAL, "*vm-begin2*",0};
205 struct scm scm_vm_if = {TSPECIAL, "*vm-if*",0};
206 struct scm scm_vm_if_expr = {TSPECIAL, "*vm-if-expr*",0};
207 struct scm scm_vm_call_with_values2 = {TSPECIAL, "*vm-call-with-values2*",0};
208 struct scm scm_vm_call_with_current_continuation2 = {TSPECIAL, "*vm-call-with-current-continuation2*",0};
209 struct scm scm_vm_return = {TSPECIAL, "*vm-return*",0};
211 struct scm scm_symbol_gnuc = {TSYMBOL, "%gnuc",0};
212 struct scm scm_symbol_mesc = {TSYMBOL, "%mesc",0};
213 struct scm scm_symbol_c_reader = {TSYMBOL, "%c-reader",0};
214 struct scm scm_symbol_c_define = {TSYMBOL, "%c-define",0};
216 struct scm scm_test = {TSYMBOL, "test",0};
219 #include "mes.mes.symbols.h"
221 #include "mes.symbols.h"
228 struct function g_functions[200];
231 #if !__GNUC__ || !_POSIX_SOURCE
234 #include "math.mes.h"
236 #include "posix.mes.h"
237 #include "reader.mes.h"
238 #include "vector.mes.h"
249 #define TYPE(x) g_cells[x].type
250 #define CAR(x) g_cells[x].car
251 #define CDR(x) g_cells[x].cdr
253 #define NTYPE(x) g_news[x].type
254 #define NCAR(x) g_news[x].car
255 #define NCDR(x) g_news[x].cdr
258 #define LENGTH(x) g_cells[x].car
259 #define REF(x) g_cells[x].car
260 #define STRING(x) g_cells[x].car
262 #define CLOSURE(x) g_cells[x].cdr
263 #define CONTINUATION(x) g_cells[x].cdr
265 #define FUNCTION(x) g_functions[g_cells[x].cdr]
266 #define MACRO(x) g_cells[x].cdr
267 #define VALUE(x) g_cells[x].cdr
268 #define VECTOR(x) g_cells[x].cdr
270 #define NLENGTH(x) g_news[x].car
272 #define NVALUE(x) g_news[x].cdr
273 #define NVECTOR(x) g_news[x].cdr
276 #define CONTINUATION(x) g_cells[x].cdr
277 #define HITS(x) g_cells[x].hits
278 #define LENGTH(x) g_cells[x].length
279 #define NAME(x) g_cells[x].name
280 #define STRING(x) g_cells[x].string
281 #define CLOSURE(x) g_cells[x].closure
282 #define MACRO(x) g_cells[x].macro
283 #define REF(x) g_cells[x].ref
284 #define VALUE(x) g_cells[x].value
285 #define VECTOR(x) g_cells[x].vector
286 #define FUNCTION(x) g_functions[g_cells[x].function]
288 #define NLENGTH(x) g_news[x].length
290 #define NVALUE(x) g_news[x].value
291 #define NVECTOR(x) g_news[x].vector
294 #define MAKE_CHAR(n) make_cell_ (tmp_num_ (TCHAR), 0, tmp_num2_ (n))
295 #define MAKE_CONTINUATION(n) make_cell_ (tmp_num_ (TCONTINUATION), n, g_stack)
296 #define MAKE_NUMBER(n) make_cell_ (tmp_num_ (TNUMBER), 0, tmp_num2_ (n))
297 #define MAKE_REF(n) make_cell_ (tmp_num_ (TREF), n, 0)
298 #define MAKE_STRING(x) make_cell_ (tmp_num_ (TSTRING), x, 0)
300 #define MAKE_KEYWORD(x) make_cell_ (tmp_num_ (TKEYWORD), x, 0)
303 #define MAKE_MACRO(name, x) make_cell_ (tmp_num_ (TMACRO), STRING (name), x)
306 #define CAAR(x) CAR (CAR (x))
307 #define CADR(x) CAR (CDR (x))
308 #define CDAR(x) CDR (CAR (x))
309 #define CDDR(x) CDR (CDR (x))
310 #define CADAR(x) CAR (CDR (CAR (x)))
311 #define CADDR(x) CAR (CDR (CDR (x)))
312 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
317 assert (g_free + n < ARENA_SIZE);
333 VALUE (tmp_num2) = x;
338 make_cell_ (SCM type, SCM car, SCM cdr)
341 assert (TYPE (type) == TNUMBER);
342 TYPE (x) = VALUE (type);
343 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
344 if (car) CAR (x) = CAR (car);
345 if (cdr) CDR(x) = CDR(cdr);
347 else if (VALUE (type) == TFUNCTION) {
348 if (car) CAR (x) = car;
349 if (cdr) CDR(x) = CDR(cdr);
359 make_symbol_ (SCM s) ///((internal))
361 VALUE (tmp_num) = TSYMBOL;
362 SCM x = make_cell_ (tmp_num, s, 0);
363 g_symbols = cons (x, g_symbols);
368 list_of_char_equal_p (SCM a, SCM b) ///((internal))
370 while (a != cell_nil && b != cell_nil && VALUE (CAR (a)) == VALUE (CAR (b))) {
371 assert (TYPE (CAR (a)) == TCHAR);
372 assert (TYPE (CAR (b)) == TCHAR);
376 return (a == cell_nil && b == cell_nil) ? cell_t : cell_f;
380 lookup_symbol_ (SCM s)
384 if (list_of_char_equal_p (STRING (CAR (x)), s) == cell_t) break;
388 if (!x) x = make_symbol_ (s);
395 return MAKE_NUMBER (TYPE (x));
401 return (TYPE (x) != TCONTINUATION
402 && (TYPE (CAR (x)) == TPAIR // FIXME: this is weird
403 || TYPE (CAR (x)) == TREF
404 || TYPE (CAR (x)) == TSPECIAL
405 || TYPE (CAR (x)) == TSYMBOL
406 || TYPE (CAR (x)) == TSTRING)) ? CAR (x) : MAKE_NUMBER (CAR (x));
412 return (TYPE (CDR (x)) == TPAIR
413 || TYPE (CDR (x)) == TREF
414 || TYPE (CAR (x)) == TSPECIAL
415 || TYPE (CDR (x)) == TSYMBOL
416 || TYPE (CDR (x)) == TSTRING) ? CDR (x) : MAKE_NUMBER (CDR (x));
422 assert (TYPE (x) == TFUNCTION);
423 return MAKE_NUMBER (FUNCTION (x).arity);
429 VALUE (tmp_num) = TPAIR;
430 return make_cell_ (tmp_num, x, y);
437 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_car));
446 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_cdr));
452 list (SCM x) ///((arity . n))
460 return x == cell_nil ? cell_t : cell_f;
467 || ((TYPE (x) == TKEYWORD && TYPE (y) == TKEYWORD
468 && STRING (x) == STRING (y)))
469 || (TYPE (x) == TCHAR && TYPE (y) == TCHAR
470 && VALUE (x) == VALUE (y))
471 || (TYPE (x) == TNUMBER && TYPE (y) == TNUMBER
472 && VALUE (x) == VALUE (y)))
477 values (SCM x) ///((arity . n))
485 acons (SCM key, SCM value, SCM alist)
487 return cons (cons (key, value), alist);
494 while (x != cell_nil)
497 if (TYPE (x) != TPAIR) return MAKE_NUMBER (-1);
500 return MAKE_NUMBER (n);
503 SCM apply (SCM, SCM, SCM);
506 error (SCM key, SCM x)
510 if ((throw = assq_ref_env (cell_symbol_throw, r0)) != cell_undefined)
511 return apply (throw, cons (key, cons (x, cell_nil)), r0);
513 display_error_ (key);
521 cstring_to_list (char const* s)
526 p = cons (MAKE_CHAR (s[i]), p);
532 assert_defined (SCM x, SCM e) ///((internal))
534 if (e == cell_undefined) return error (cell_symbol_unbound_variable, x);
539 check_formals (SCM f, SCM formals, SCM args) ///((internal))
541 int flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : VALUE (length (formals));
542 int alen = VALUE (length (args));
543 if (alen != flen && alen != -1 && flen != -1)
545 char *s = "apply: wrong number of arguments; expected: ";
552 SCM e = MAKE_STRING (cstring_to_list (s));
553 return error (cell_symbol_wrong_number_of_args, cons (e, f));
555 return cell_unspecified;
559 check_apply (SCM f, SCM e) ///((internal))
562 if (f == cell_f || f == cell_t) type = "bool";
563 if (f == cell_nil) type = "nil";
564 if (f == cell_unspecified) type = "*unspecified*";
565 if (f == cell_undefined) type = "*undefined*";
566 if (TYPE (f) == TCHAR) type = "char";
567 if (TYPE (f) == TNUMBER) type = "number";
568 if (TYPE (f) == TSTRING) type = "string";
572 char *s = "cannot apply: ";
578 SCM e = MAKE_STRING (cstring_to_list (s));
579 return error (cell_symbol_wrong_type_arg, cons (e, f));
581 return cell_unspecified;
585 gc_push_frame () ///((internal))
587 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
588 g_stack = cons (frame, g_stack);
593 append2 (SCM x, SCM y)
595 if (x == cell_nil) return y;
596 assert (TYPE (x) == TPAIR);
597 return cons (car (x), append2 (cdr (x), y));
601 pairlis (SCM x, SCM y, SCM a)
605 if (TYPE (x) != TPAIR)
606 return cons (cons (x, y), a);
607 return cons (cons (car (x), car (y)),
608 pairlis (cdr (x), cdr (y), a));
614 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
615 && x != cell_nil && TYPE (CAR (x)) == TVALUES)
616 x = cons (CADAR (x), CDR (x));
617 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
618 && x != cell_nil && TYPE (CDR (x)) == TPAIR && TYPE (CADR (x)) == TVALUES)
619 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
620 switch (FUNCTION (fn).arity)
622 #if __MESC__ || !_POSIX_SOURCE
623 case 0: return (FUNCTION (fn).function) ();
624 case 1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (CAR (x));
625 case 2: return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x));
626 case 3: return ((SCM(*)(SCM,SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x), CAR (CDDR (x)));
627 case -1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
628 default: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
630 case 0: return FUNCTION (fn).function0 ();
631 case 1: return FUNCTION (fn).function1 (CAR (x));
632 case 2: return FUNCTION (fn).function2 (CAR (x), CADR (x));
633 case 3: return FUNCTION (fn).function3 (CAR (x), CADR (x), CAR (CDDR (x)));
634 case -1: return FUNCTION (fn).functionn (x);
638 return cell_unspecified;
650 while (a != cell_nil && v != VALUE (CAAR (a))) a = CDR (a); break;
655 while (a != cell_nil && v != STRING (CAAR (a))) a = CDR (a); break;
660 while (a != cell_nil && x != CAAR (a)) a = CDR (a); break;
662 return a != cell_nil ? CAR (a) : cell_f;
666 assq_ref_env (SCM x, SCM a)
669 if (x == cell_f) return cell_undefined;
674 set_car_x (SCM x, SCM e)
676 assert (TYPE (x) == TPAIR);
678 return cell_unspecified;
682 set_cdr_x (SCM x, SCM e)
684 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_set_cdr_x));
686 return cell_unspecified;
690 set_env_x (SCM x, SCM e, SCM a)
692 SCM p = assert_defined (x, assq (x, a));
693 if (TYPE (p) != TPAIR) error (cell_symbol_not_a_pair, cons (p, x));
694 return set_cdr_x (p, e);
698 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
700 SCM cl = cons (cons (cell_closure, x), x);
703 return cell_unspecified;
707 make_closure_ (SCM args, SCM body, SCM a) ///((internal))
709 return make_cell_ (tmp_num_ (TCLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
713 lookup_macro_ (SCM x, SCM a) ///((internal))
715 if (TYPE (x) != TSYMBOL) return cell_f;
716 SCM m = assq_ref_env (x, a);
717 if (TYPE (m) == TMACRO) return MACRO (m);
722 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
731 return cell_unspecified;
735 gc_peek_frame () ///((internal))
737 SCM frame = CAR (g_stack);
740 r3 = CAR (CDDR (frame));
741 r0 = CADR (CDDR (frame));
746 gc_pop_frame () ///((internal))
748 SCM frame = gc_peek_frame (g_stack);
749 g_stack = CDR (g_stack);
760 case cell_vm_evlis: goto evlis;
761 case cell_vm_evlis2: goto evlis2;
762 case cell_vm_evlis3: goto evlis3;
763 case cell_vm_apply: goto apply;
764 case cell_vm_apply2: goto apply2;
765 case cell_vm_eval: goto eval;
766 #if MES_FIXED_PRIMITIVES
767 case cell_vm_eval_car: goto eval_car;
768 case cell_vm_eval_cdr: goto eval_cdr;
769 case cell_vm_eval_cons: goto eval_cons;
770 case cell_vm_eval_null_p: goto eval_null_p;
773 case cell_vm_eval_define: goto eval_define;
775 case cell_vm_eval_set_x: goto eval_set_x;
776 case cell_vm_eval_macro: goto eval_macro;
777 case cell_vm_eval_check_func: goto eval_check_func;
778 case cell_vm_eval2: goto eval2;
779 case cell_vm_macro_expand: goto macro_expand;
780 case cell_vm_begin: goto begin;
781 case cell_vm_begin_read_input_file: goto begin_read_input_file;
782 case cell_vm_begin2: goto begin2;
783 case cell_vm_if: goto vm_if;
784 case cell_vm_if_expr: goto if_expr;
785 case cell_vm_call_with_current_continuation2: goto call_with_current_continuation2;
786 case cell_vm_call_with_values2: goto call_with_values2;
787 case cell_vm_return: goto vm_return;
788 case cell_unspecified: return r1;
796 if (r1 == cell_nil) goto vm_return;
797 if (TYPE (r1) != TPAIR) goto eval;
798 push_cc (CAR (r1), r1, r0, cell_vm_evlis2);
801 push_cc (CDR (r2), r1, r0, cell_vm_evlis3);
809 switch (TYPE (CAR (r1)))
812 check_formals (CAR (r1), MAKE_NUMBER (FUNCTION (CAR (r1)).arity), CDR (r1));
813 r1 = call (CAR (r1), CDR (r1)); /// FIXME: move into eval_apply
818 SCM cl = CLOSURE (CAR (r1));
819 SCM formals = CADR (cl);
820 SCM body = CDDR (cl);
823 check_formals (CAR (r1), formals, CDR (r1));
824 SCM p = pairlis (formals, CDR (r1), aa);
825 call_lambda (body, p, aa, r0);
831 g_stack = CONTINUATION (CAR (r1));
842 push_cc (cons (CADR (r1), CADDR (r1)), r1, r0, cell_vm_return);
847 push_cc (CADR (r1), r1, CADDR (r1), cell_vm_return);
850 case cell_call_with_current_continuation:
853 goto call_with_current_continuation;
855 default: check_apply (cell_f, CAR (r1));
860 if (CAR (r1) == cell_symbol_call_with_values)
863 goto call_with_values;
865 if (CAR (r1) == cell_symbol_current_module)
876 case cell_symbol_lambda:
878 SCM formals = CADR (CAR (r1));
879 SCM body = CDDR (CAR (r1));
880 SCM p = pairlis (formals, CDR (r1), r0);
881 check_formals (r1, formals, CDR (r1));
882 call_lambda (body, p, p, r0);
888 push_cc (CAR (r1), r1, r0, cell_vm_apply2);
891 check_apply (r1, CAR (r2));
892 r1 = cons (r1, CDR (r2));
903 #if MES_FIXED_PRIMITIVES
904 case cell_symbol_car:
906 push_cc (CADR (r1), r1, r0, cell_vm_eval_car); goto eval;
908 x = r1; gc_pop_frame (); r1 = CAR (x); goto eval_apply;
910 case cell_symbol_cdr:
912 push_cc (CADR (r1), r1, r0, cell_vm_eval_cdr); goto eval;
914 x = r1; gc_pop_frame (); r1 = CDR (x); goto eval_apply;
916 case cell_symbol_cons: {
917 push_cc (CDR (r1), r1, r0, cell_vm_eval_cons); goto evlis;
921 r1 = cons (CAR (x), CADR (x));
924 case cell_symbol_null_p:
926 push_cc (CADR (r1), r1, r0, cell_vm_eval_null_p);
929 x = r1; gc_pop_frame (); r1 = null_p (x); goto eval_apply;
931 #endif // MES_FIXED_PRIMITIVES
932 case cell_symbol_quote:
934 x = r1; gc_pop_frame (); r1 = CADR (x); goto eval_apply;
936 case cell_symbol_begin: goto begin;
937 case cell_symbol_lambda:
939 r1 = make_closure_ (CADR (r1), CDDR (r1), assq (cell_closure, r0));
942 case cell_symbol_if: {r1=CDR (r1); goto vm_if;}
943 case cell_symbol_set_x:
945 push_cc (CAR (CDDR (r1)), r1, r0, cell_vm_eval_set_x);
949 r1 = set_env_x (CADR (x), r1, r0);
952 case cell_vm_macro_expand:
954 push_cc (CADR (r1), r1, r0, cell_vm_return);
958 push_cc (r1, r1, r0, cell_vm_eval_macro);
963 if (TYPE (r1) == TPAIR)
965 set_cdr_x (r2, CDR (r1));
966 set_car_x (r2, CAR (r1));
971 if (TYPE (r1) == TPAIR
972 && (CAR (r1) == cell_symbol_define
973 || CAR (r1) == cell_symbol_define_macro))
976 if (TYPE (r2) != TPAIR)
978 push_cc (CAR (CDDR (r1)), r2, cons (cons (CADR (r1), CADR (r1)), r0), cell_vm_eval_define);
984 SCM p = pairlis (CADR (r1), CADR (r1), r0);
985 SCM args = CDR (CADR (r1));
986 SCM body = CDDR (r1);
987 r1 = cons (cell_symbol_lambda, cons (args, body));
988 push_cc (r1, r2, p, cell_vm_eval_define);
992 if (CAAR (CAAR (g_stack)) == cell_symbol_define_macro
993 || CAR (CAAR (g_stack)) == cell_symbol_define_macro)
994 r1 = MAKE_MACRO (r2, r1);
995 SCM entry = cons (r2, r1);
996 SCM aa = cons (entry, cell_nil);
997 set_cdr_x (aa, cdr (r0));
999 SCM cl = assq (cell_closure, r0);
1004 #endif // MES_C_DEFINE
1006 push_cc (CAR (r1), r1, r0, cell_vm_eval_check_func); goto eval;
1008 push_cc (CDR (r2), r2, r0, cell_vm_eval2); goto evlis;
1010 r1 = cons (CAR (r2), r1);
1017 r1 = assert_defined (r1, assq_ref_env (r1, r0));
1020 default: goto vm_return;
1026 if (TYPE (r1) == TPAIR
1027 && (macro = lookup_macro_ (CAR (r1), r0)) != cell_f)
1029 r1 = cons (macro, CDR (r1));
1032 else if (TYPE (r1) == TPAIR
1033 && TYPE (CAR (r1)) == TSYMBOL
1034 && ((expanders = assq_ref_env (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
1035 && ((macro = assq (CAR (r1), expanders)) != cell_f))
1037 SCM sc_expand = assq_ref_env (cell_symbol_macro_expand, r0);
1038 if (sc_expand != cell_undefined && sc_expand != cell_f)
1040 r1 = cons (sc_expand, cons (r1, cell_nil));
1047 x = cell_unspecified;
1048 while (r1 != cell_nil) {
1050 if (TYPE (r1) == TPAIR && TYPE (CAR (r1)) == TPAIR)
1052 if (CAAR (r1) == cell_symbol_begin)
1053 r1 = append2 (CDAR (r1), CDR (r1));
1054 else if (CAAR (r1) == cell_symbol_primitive_load)
1056 push_cc (cons (cell_symbol_read_input_file, cell_nil), r1, r0, cell_vm_begin_read_input_file);
1058 begin_read_input_file:
1059 r1 = append2 (r1, CDR (r2));
1062 if (CDR (r1) == cell_nil)
1067 push_cc (CAR (r1), r1, r0, cell_vm_begin2);
1077 push_cc (CAR (r1), r1, r0, cell_vm_if_expr);
1087 if (CDDR (r1) != cell_nil)
1089 r1 = CAR (CDDR (r1));
1092 r1 = cell_unspecified;
1095 call_with_current_continuation:
1097 x = MAKE_CONTINUATION (g_continuations++);
1099 push_cc (cons (CAR (r1), cons (x, cell_nil)), x, r0, cell_vm_call_with_current_continuation2);
1101 call_with_current_continuation2:
1102 CONTINUATION (r2) = g_stack;
1106 push_cc (cons (CAR (r1), cell_nil), r1, r0, cell_vm_call_with_values2);
1109 if (TYPE (r1) == TVALUES)
1111 r1 = cons (CADR (r2), r1);
1122 apply (SCM f, SCM x, SCM a) ///((internal))
1124 push_cc (cons (f, x), cell_unspecified, r0, cell_unspecified);
1126 return eval_apply ();
1130 mes_g_stack (SCM a) ///((internal))
1136 g_stack = cons (cell_nil, cell_nil);
1140 //
\f Environment setup
1143 make_tmps (struct scm* cells)
1146 cells[tmp].type = TCHAR;
1148 cells[tmp_num].type = TNUMBER;
1149 tmp_num2 = g_free++;
1150 cells[tmp_num2].type = TNUMBER;
1162 gc_init_cells () ///((internal))
1164 g_cells = (struct scm *)malloc (2*ARENA_SIZE*sizeof (struct scm));
1170 g_cells += sizeof (struct scm);
1180 gc_init_news () ///((internal))
1184 p -= sizeof (struct scm);
1185 p += ARENA_SIZE * sizeof (struct scm);
1188 g_news = g_cells-1 + ARENA_SIZE;
1191 NTYPE (0) = TVECTOR;
1195 g_news += sizeof (struct scm);
1205 mes_symbols () ///((internal))
1211 #include "mes.mes.symbols.i"
1213 #include "mes.symbols.i"
1216 g_symbol_max = g_free;
1217 make_tmps (g_cells);
1220 for (int i=1; i<g_symbol_max; i++)
1221 g_symbols = cons (i, g_symbols);
1226 #include "mes.mes.symbol-names.i"
1228 #include "mes.symbol-names.i"
1231 a = acons (cell_symbol_mes_version, MAKE_STRING (cstring_to_list (VERSION)), a);
1232 a = acons (cell_symbol_mes_prefix, MAKE_STRING (cstring_to_list (PREFIX)), a);
1234 a = acons (cell_symbol_dot, cell_dot, a);
1235 a = acons (cell_symbol_begin, cell_begin, a);
1236 a = acons (cell_symbol_call_with_values, cell_symbol_call_with_values, a);
1237 a = acons (cell_symbol_current_module, cell_symbol_current_module, a);
1238 a = acons (cell_symbol_call_with_current_continuation, cell_call_with_current_continuation, a);
1239 a = acons (cell_symbol_sc_expand, cell_f, a);
1242 a = acons (cell_symbol_gnuc, cell_t, a);
1243 a = acons (cell_symbol_mesc, cell_f, a);
1245 a = acons (cell_symbol_gnuc, cell_f, a);
1246 a = acons (cell_symbol_mesc, cell_t, a);
1250 a = acons (cell_symbol_c_reader, cell_t, a);
1252 a = acons (cell_symbol_c_reader, cell_f, a);
1256 a = acons (cell_symbol_c_define, cell_t, a);
1258 a = acons (cell_symbol_c_define, cell_f, a);
1260 a = acons (cell_closure, a, a);
1266 mes_environment () ///((internal))
1268 SCM a = mes_symbols ();
1269 return mes_g_stack (a);
1273 mes_builtins (SCM a) ///((internal))
1275 #if !__GNUC__ || !_POSIX_SOURCE
1276 #include "mes.mes.i"
1278 // Do not sort: Order of these includes define builtins
1279 #include "posix.mes.i"
1280 #include "math.mes.i"
1281 #include "lib.mes.i"
1282 #include "vector.mes.i"
1284 #include "reader.mes.i"
1286 #include "gc.mes.environment.i"
1287 #include "lib.mes.environment.i"
1288 #include "math.mes.environment.i"
1289 #include "mes.mes.environment.i"
1290 #include "posix.mes.environment.i"
1291 #include "reader.mes.environment.i"
1292 #include "vector.mes.environment.i"
1296 // Do not sort: Order of these includes define builtins
1304 #include "gc.environment.i"
1305 #include "lib.environment.i"
1306 #include "math.environment.i"
1307 #include "mes.environment.i"
1308 #include "posix.environment.i"
1309 #include "reader.environment.i"
1310 #include "vector.environment.i"
1315 fputs ("functions: ", STDERR);
1316 fputs (itoa (g_function), STDERR);
1317 fputs ("\n", STDERR);
1318 for (int i = 0; i < g_function; i++)
1320 fputs ("[", STDERR);
1321 fputs (itoa (i), STDERR);
1322 fputs ("]: ", STDERR);
1323 fputs (g_functions[i].name, STDERR);
1324 fputs ("\n", STDERR);
1326 fputs ("\n", STDERR);
1332 SCM read_input_file_env (SCM);
1335 load_env (SCM a) ///((internal))
1339 if (getenv ("MES_PREFIX"))
1342 strcpy (buf, getenv ("MES_PREFIX"));
1343 strcpy (buf + strlen (buf), "/module");
1344 strcpy (buf + strlen (buf), "/mes/read-0.mes");
1345 if (getenv ("MES_DEBUG"))
1347 eputs ("MES_PREFIX reading read-0:");
1351 g_stdin = open (buf, O_RDONLY);
1355 char *read0 = MODULEDIR "mes/read-0.mes";
1356 if (getenv ("MES_DEBUG"))
1358 eputs ("MODULEDIR reading read-0:");
1362 g_stdin = open (read0, O_RDONLY);
1366 if (getenv ("MES_DEBUG"))
1368 eputs (". reading read-0:");
1369 eputs ("module/mes/read-0.mes");
1372 g_stdin = open ("module/mes/read-0.mes", O_RDONLY);
1376 eputs ("boot failed, read-0.mes not found\n");
1380 if (!g_function) r0 = mes_builtins (r0);
1381 r2 = read_input_file_env (r0);
1387 bload_env (SCM a) ///((internal))
1390 char *mo = "mes/read-0-32.mo";
1391 g_stdin = open ("module/mes/read-0-32.mo", O_RDONLY);
1392 char *read0 = MODULEDIR "mes/read-0-32.mo";
1393 g_stdin = g_stdin >= 0 ? g_stdin : open (read0, O_RDONLY);
1395 char *mo ="mes/read-0.mo";
1396 g_stdin = open ("module/mes/read-0.mo", O_RDONLY);
1397 g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "mes/read-0.mo", O_RDONLY);
1400 if (g_stdin < 0) {eputs ("no such file: ");eputs (mo);eputs ("\n");return 1;}
1401 assert (getchar () == 'M');
1402 assert (getchar () == 'E');
1403 assert (getchar () == 'S');
1405 if (g_debug) eputs ("*GOT MES*\n");
1406 g_stack = getchar () << 8;
1407 g_stack += getchar ();
1409 char *p = (char*)g_cells;
1416 g_free = (p-(char*)g_cells) / sizeof (struct scm);
1420 r0 = mes_builtins (r0);
1423 set_env_x (cell_symbol_gnuc, cell_t, r0);
1424 set_env_x (cell_symbol_mesc, cell_f, r0);
1426 set_env_x (cell_symbol_gnuc, cell_f, r0);
1427 set_env_x (cell_symbol_mesc, cell_t, r0);
1432 eputs ("symbols: ");
1434 while (s && s != cell_nil) {
1435 display_error_ (CAR (s));
1440 eputs ("functions: ");
1441 eputs (itoa (g_function));
1443 for (int i = 0; i < g_function; i++)
1448 eputs (g_functions[i].name);
1451 //display_error_ (r0);
1462 main (int argc, char *argv[])
1465 if (p = getenv ("MES_DEBUG")) g_debug = atoi (p);
1466 if (g_debug) {eputs (";;; MODULEDIR=");eputs (MODULEDIR);eputs ("\n");}
1467 if (p = getenv ("MES_MAX_ARENA")) MAX_ARENA_SIZE = atoi (p);
1468 if (p = getenv ("MES_ARENA")) ARENA_SIZE = atoi (p);
1469 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes [--dump|--load] < FILE\n");
1470 if (argc > 1 && !strcmp (argv[1], "--version")) {puts ("Mes ");puts (VERSION);puts ("\n");return 0;};
1473 r0 = mes_environment ();
1475 SCM program = (argc > 1 && !strcmp (argv[1], "--load"))
1476 ? bload_env (r0) : load_env (r0);
1477 g_tiny = argc > 2 && !strcmp (argv[2], "--tiny");
1478 if (argc > 1 && !strcmp (argv[1], "--dump")) return dump ();
1481 for (int i=argc-1; i>=0; i--) lst = cons (MAKE_STRING (cstring_to_list (argv[i])), lst);
1482 r0 = acons (cell_symbol_argv, lst, r0);
1483 push_cc (r2, cell_unspecified, r0, cell_unspecified);
1486 eputs ("program: ");
1487 display_error_ (r1);
1492 display_error_ (r1);
1497 eputs ("\nstats: [");
1498 eputs (itoa (g_free));