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/>.
22 #error "POSIX not supported"
31 int ARENA_SIZE = 100000;
32 int MAX_ARENA_SIZE = 40000000;
33 int GC_SAFETY = 10000;
41 SCM g_continuations = 0;
53 enum type_t {TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TREF, TSPECIAL, TSTRING, TSYMBOL, TVALUES, TVARIABLE, TVECTOR, TBROKEN_HEART};
61 int (*function) (void);
69 struct scm *g_cells = foobar;
70 struct scm *g_news = foobar;
72 struct scm *g_cells = 0;
73 struct scm *g_news = 0;
76 struct scm scm_nil = {TSPECIAL, "()",0};
77 struct scm scm_f = {TSPECIAL, "#f",0};
78 struct scm scm_t = {TSPECIAL, "#t",0};
79 struct scm scm_dot = {TSPECIAL, ".",0};
80 struct scm scm_arrow = {TSPECIAL, "=>",0};
81 struct scm scm_undefined = {TSPECIAL, "*undefined*",0};
82 struct scm scm_unspecified = {TSPECIAL, "*unspecified*",0};
83 struct scm scm_closure = {TSPECIAL, "*closure*",0};
84 struct scm scm_circular = {TSPECIAL, "*circular*",0};
85 struct scm scm_begin = {TSPECIAL, "*begin*",0};
87 struct scm scm_symbol_dot = {TSYMBOL, "*dot*",0};
88 struct scm scm_symbol_lambda = {TSYMBOL, "lambda",0};
89 struct scm scm_symbol_begin = {TSYMBOL, "begin",0};
90 struct scm scm_symbol_if = {TSYMBOL, "if",0};
91 struct scm scm_symbol_quote = {TSYMBOL, "quote",0};
92 struct scm scm_symbol_define = {TSYMBOL, "define",0};
93 struct scm scm_symbol_define_macro = {TSYMBOL, "define-macro",0};
95 struct scm scm_symbol_quasiquote = {TSYMBOL, "quasiquote", 0};
96 struct scm scm_symbol_unquote = {TSYMBOL, "unquote", 0};
97 struct scm scm_symbol_unquote_splicing = {TSYMBOL, "unquote-splicing", 0};
98 struct scm scm_symbol_syntax = {TSYMBOL, "syntax",0};
99 struct scm scm_symbol_quasisyntax = {TSYMBOL, "quasisyntax", 0};
100 struct scm scm_symbol_unsyntax = {TSYMBOL, "unsyntax", 0};
101 struct scm scm_symbol_unsyntax_splicing = {TSYMBOL, "unsyntax-splicing", 0};
103 struct scm scm_symbol_set_x = {TSYMBOL, "set!",0};
105 struct scm scm_symbol_sc_expand = {TSYMBOL, "sc-expand",0};
106 struct scm scm_symbol_macro_expand = {TSYMBOL, "macro-expand",0};
107 struct scm scm_symbol_sc_expander_alist = {TSYMBOL, "*sc-expander-alist*",0};
109 struct scm scm_symbol_call_with_values = {TSYMBOL, "call-with-values",0};
110 struct scm scm_call_with_current_continuation = {TSPECIAL, "*call/cc*",0};
111 struct scm scm_symbol_call_with_current_continuation = {TSYMBOL, "call-with-current-continuation",0};
112 struct scm scm_symbol_current_module = {TSYMBOL, "current-module",0};
113 struct scm scm_symbol_primitive_load = {TSYMBOL, "primitive-load",0};
114 struct scm scm_symbol_read_input_file = {TSYMBOL, "read-input-file",0};
115 struct scm scm_symbol_write = {TSYMBOL, "write",0};
116 struct scm scm_symbol_display = {TSYMBOL, "display",0};
118 struct scm scm_symbol_throw = {TSYMBOL, "throw",0};
119 struct scm scm_symbol_not_a_number = {TSYMBOL, "not-a-number",0};
120 struct scm scm_symbol_not_a_pair = {TSYMBOL, "not-a-pair",0};
121 struct scm scm_symbol_system_error = {TSYMBOL, "system-error",0};
122 struct scm scm_symbol_wrong_number_of_args = {TSYMBOL, "wrong-number-of-args",0};
123 struct scm scm_symbol_wrong_type_arg = {TSYMBOL, "wrong-type-arg",0};
124 struct scm scm_symbol_unbound_variable = {TSYMBOL, "unbound-variable",0};
126 struct scm scm_symbol_argv = {TSYMBOL, "%argv",0};
127 struct scm scm_symbol_mes_prefix = {TSYMBOL, "%prefix",0};
128 struct scm scm_symbol_mes_version = {TSYMBOL, "%version",0};
130 struct scm scm_symbol_car = {TSYMBOL, "car",0};
131 struct scm scm_symbol_cdr = {TSYMBOL, "cdr",0};
132 struct scm scm_symbol_null_p = {TSYMBOL, "null?",0};
133 struct scm scm_symbol_eq_p = {TSYMBOL, "eq?",0};
134 struct scm scm_symbol_cons = {TSYMBOL, "cons",0};
136 struct scm scm_vm_evlis = {TSPECIAL, "*vm-evlis*",0};
137 struct scm scm_vm_evlis2 = {TSPECIAL, "*vm-evlis2*",0};
138 struct scm scm_vm_evlis3 = {TSPECIAL, "*vm-evlis3*",0};
139 struct scm scm_vm_apply = {TSPECIAL, "core:apply",0};
140 struct scm scm_vm_apply2 = {TSPECIAL, "*vm-apply2*",0};
141 struct scm scm_vm_eval = {TSPECIAL, "core:eval",0};
142 struct scm scm_vm_eval_define = {TSPECIAL, "*vm-eval-define*",0};
144 //MES_FIXED_PRIMITIVES
145 struct scm scm_vm_eval_car = {TSPECIAL, "*vm-eval-car*",0};
146 struct scm scm_vm_eval_cdr = {TSPECIAL, "*vm-eval-cdr*",0};
147 struct scm scm_vm_eval_cons = {TSPECIAL, "*vm-eval-cons*",0};
148 struct scm scm_vm_eval_null_p = {TSPECIAL, "*vm-eval-null-p*",0};
150 struct scm scm_vm_eval_set_x = {TSPECIAL, "*vm-eval-set!*",0};
151 struct scm scm_vm_eval_macro_expand_eval = {TSPECIAL, "*vm:eval-macro-expand-eval*",0};
152 struct scm scm_vm_eval_macro_expand_expand = {TSPECIAL, "*vm:eval-macro-expand-expand*",0};
153 struct scm scm_vm_eval_check_func = {TSPECIAL, "*vm-eval-check-func*",0};
154 struct scm scm_vm_eval2 = {TSPECIAL, "*vm-eval2*",0};
155 struct scm scm_vm_macro_expand = {TSPECIAL, "core:macro-expand",0};
156 struct scm scm_vm_macro_expand_define = {TSPECIAL, "*vm:core:macro-expand-define*",0};
157 struct scm scm_vm_macro_expand_define_macro = {TSPECIAL, "*vm:core:macro-expand-define-macro*",0};
158 struct scm scm_vm_macro_expand_lambda = {TSPECIAL, "*vm:core:macro-expand-lambda*",0};
159 struct scm scm_vm_macro_expand_set_x = {TSPECIAL, "*vm:core:macro-expand-set!*",0};
160 struct scm scm_vm_begin_expand_primitive_load = {TSPECIAL, "*vm:core:begin-expand-primitive-load*",0};
161 struct scm scm_vm_begin_primitive_load = {TSPECIAL, "*vm:core:begin-primitive-load*",0};
162 struct scm scm_vm_macro_expand_car = {TSPECIAL, "*vm:core:macro-expand-car*",0};
163 struct scm scm_vm_macro_expand_cdr = {TSPECIAL, "*vm:macro-expand-cdr*",0};
164 struct scm scm_vm_begin_expand = {TSPECIAL, "*vm:begin-expand*",0};
165 struct scm scm_vm_begin_expand_eval = {TSPECIAL, "*vm:begin-expand-eval*",0};
166 struct scm scm_vm_begin_expand_macro = {TSPECIAL, "*vm:begin-expand-macro*",0};
167 struct scm scm_vm_begin = {TSPECIAL, "*vm-begin*",0};
168 struct scm scm_vm_begin_read_input_file = {TSPECIAL, "*vm-begin-read-input-file*",0};
169 struct scm scm_vm_begin_eval = {TSPECIAL, "*vm:begin-eval*",0};
170 struct scm scm_vm_if = {TSPECIAL, "*vm-if*",0};
171 struct scm scm_vm_if_expr = {TSPECIAL, "*vm-if-expr*",0};
172 struct scm scm_vm_call_with_values2 = {TSPECIAL, "*vm-call-with-values2*",0};
173 struct scm scm_vm_call_with_current_continuation2 = {TSPECIAL, "*vm-call-with-current-continuation2*",0};
174 struct scm scm_vm_return = {TSPECIAL, "*vm-return*",0};
176 struct scm scm_symbol_gnuc = {TSYMBOL, "%gnuc",0};
177 struct scm scm_symbol_mesc = {TSYMBOL, "%mesc",0};
179 struct scm scm_test = {TSYMBOL, "test",0};
181 #include "mes.mes.symbols.h"
187 struct function g_functions[200];
192 #include "math.mes.h"
194 #include "posix.mes.h"
195 // #include "reader.mes.h"
196 #include "vector.mes.h"
198 #define TYPE(x) g_cells[x].type
199 #define CAR(x) g_cells[x].car
200 #define CDR(x) g_cells[x].cdr
202 #define NTYPE(x) g_news[x].type
203 #define NCAR(x) g_news[x].car
204 #define NCDR(x) g_news[x].cdr
206 #define LENGTH(x) g_cells[x].car
207 #define REF(x) g_cells[x].car
208 #define STRING(x) g_cells[x].car
209 #define VARIABLE(x) g_cells[x].car
211 #define CLOSURE(x) g_cells[x].cdr
212 #define CONTINUATION(x) g_cells[x].cdr
214 #define FUNCTION(x) g_functions[g_cells[x].cdr]
215 #define MACRO(x) g_cells[x].cdr
216 #define VALUE(x) g_cells[x].cdr
217 #define VECTOR(x) g_cells[x].cdr
219 #define NLENGTH(x) g_news[x].car
221 #define NVALUE(x) g_news[x].cdr
222 #define NVECTOR(x) g_news[x].cdr
224 #define MAKE_CHAR(n) make_cell_ (tmp_num_ (TCHAR), 0, tmp_num2_ (n))
225 #define MAKE_CONTINUATION(n) make_cell_ (tmp_num_ (TCONTINUATION), n, g_stack)
226 #define MAKE_NUMBER(n) make_cell_ (tmp_num_ (TNUMBER), 0, tmp_num2_ (n))
227 #define MAKE_REF(n) make_cell_ (tmp_num_ (TREF), n, 0)
228 #define MAKE_STRING(x) make_cell_ (tmp_num_ (TSTRING), x, 0)
230 #define CAAR(x) CAR (CAR (x))
231 #define CADR(x) CAR (CDR (x))
232 #define CDAR(x) CDR (CAR (x))
233 #define CDDR(x) CDR (CDR (x))
234 #define CADAR(x) CAR (CDR (CAR (x)))
235 #define CADDR(x) CAR (CDR (CDR (x)))
236 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
241 assert (g_free + n < ARENA_SIZE);
257 VALUE (tmp_num2) = x;
262 make_cell_ (SCM type, SCM car, SCM cdr)
265 assert (TYPE (type) == TNUMBER);
266 TYPE (x) = VALUE (type);
267 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
268 if (car) CAR (x) = CAR (car);
269 if (cdr) CDR(x) = CDR(cdr);
271 else if (VALUE (type) == TFUNCTION) {
272 if (car) CAR (x) = car;
273 if (cdr) CDR(x) = CDR(cdr);
283 make_symbol_ (SCM s) ///((internal))
285 VALUE (tmp_num) = TSYMBOL;
286 SCM x = make_cell_ (tmp_num, s, 0);
287 g_symbols = cons (x, g_symbols);
292 list_of_char_equal_p (SCM a, SCM b) ///((internal))
294 while (a != cell_nil && b != cell_nil && VALUE (CAR (a)) == VALUE (CAR (b))) {
295 assert (TYPE (CAR (a)) == TCHAR);
296 assert (TYPE (CAR (b)) == TCHAR);
300 return (a == cell_nil && b == cell_nil) ? cell_t : cell_f;
304 lookup_symbol_ (SCM s)
308 if (list_of_char_equal_p (STRING (CAR (x)), s) == cell_t) break;
312 if (!x) x = make_symbol_ (s);
319 return MAKE_NUMBER (TYPE (x));
325 return (TYPE (x) != TCONTINUATION
326 && (TYPE (CAR (x)) == TPAIR // FIXME: this is weird
327 || TYPE (CAR (x)) == TREF
328 || TYPE (CAR (x)) == TSPECIAL
329 || TYPE (CAR (x)) == TSYMBOL
330 || TYPE (CAR (x)) == TSTRING)) ? CAR (x) : MAKE_NUMBER (CAR (x));
336 return (TYPE (CDR (x)) == TPAIR
337 || TYPE (CDR (x)) == TREF
338 || TYPE (CAR (x)) == TSPECIAL
339 || TYPE (CDR (x)) == TSYMBOL
340 || TYPE (CDR (x)) == TSTRING) ? CDR (x) : MAKE_NUMBER (CDR (x));
346 assert (TYPE (x) == TFUNCTION);
347 return MAKE_NUMBER (FUNCTION (x).arity);
353 VALUE (tmp_num) = TPAIR;
354 return make_cell_ (tmp_num, x, y);
360 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_car));
367 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_cdr));
372 list (SCM x) ///((arity . n))
380 return x == cell_nil ? cell_t : cell_f;
387 || ((TYPE (x) == TKEYWORD && TYPE (y) == TKEYWORD
388 && STRING (x) == STRING (y)))
389 || (TYPE (x) == TCHAR && TYPE (y) == TCHAR
390 && VALUE (x) == VALUE (y))
391 || (TYPE (x) == TNUMBER && TYPE (y) == TNUMBER
392 && VALUE (x) == VALUE (y)))
397 values (SCM x) ///((arity . n))
405 acons (SCM key, SCM value, SCM alist)
407 return cons (cons (key, value), alist);
414 while (x != cell_nil)
417 if (TYPE (x) != TPAIR) return MAKE_NUMBER (-1);
420 return MAKE_NUMBER (n);
423 SCM apply (SCM, SCM, SCM);
426 error (SCM key, SCM x)
429 if ((throw = assq_ref_env (cell_symbol_throw, r0)) != cell_undefined)
430 return apply (throw, cons (key, cons (x, cell_nil)), r0);
431 display_error_ (key);
439 cstring_to_list (char const* s)
444 p = cons (MAKE_CHAR (s[i]), p);
450 assert_defined (SCM x, SCM e) ///((internal))
452 if (e == cell_undefined) return error (cell_symbol_unbound_variable, x);
457 check_formals (SCM f, SCM formals, SCM args) ///((internal))
459 int flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : VALUE (length (formals));
460 int alen = VALUE (length (args));
461 if (alen != flen && alen != -1 && flen != -1)
463 char *s = "apply: wrong number of arguments; expected: ";
470 SCM e = MAKE_STRING (cstring_to_list (s));
471 return error (cell_symbol_wrong_number_of_args, cons (e, f));
473 return cell_unspecified;
477 check_apply (SCM f, SCM e) ///((internal))
480 if (f == cell_f || f == cell_t) type = "bool";
481 if (f == cell_nil) type = "nil";
482 if (f == cell_unspecified) type = "*unspecified*";
483 if (f == cell_undefined) type = "*undefined*";
484 if (TYPE (f) == TCHAR) type = "char";
485 if (TYPE (f) == TNUMBER) type = "number";
486 if (TYPE (f) == TSTRING) type = "string";
490 char *s = "cannot apply: ";
496 SCM e = MAKE_STRING (cstring_to_list (s));
497 return error (cell_symbol_wrong_type_arg, cons (e, f));
499 return cell_unspecified;
503 gc_push_frame () ///((internal))
505 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
506 g_stack = cons (frame, g_stack);
511 append2 (SCM x, SCM y)
513 if (x == cell_nil) return y;
514 assert (TYPE (x) == TPAIR);
515 return cons (car (x), append2 (cdr (x), y));
519 pairlis (SCM x, SCM y, SCM a)
523 if (TYPE (x) != TPAIR)
524 return cons (cons (x, y), a);
525 return cons (cons (car (x), car (y)),
526 pairlis (cdr (x), cdr (y), a));
532 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
533 && x != cell_nil && TYPE (CAR (x)) == TVALUES)
534 x = cons (CADAR (x), CDR (x));
535 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
536 && x != cell_nil && TYPE (CDR (x)) == TPAIR && TYPE (CADR (x)) == TVALUES)
537 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
538 switch (FUNCTION (fn).arity)
540 #if __MESC__ || !_POSIX_SOURCE
541 case 0: return (FUNCTION (fn).function) ();
542 case 1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (CAR (x));
543 case 2: return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x));
544 case 3: return ((SCM(*)(SCM,SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x), CAR (CDDR (x)));
545 case -1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
546 default: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
548 case 0: return FUNCTION (fn).function0 ();
549 case 1: return FUNCTION (fn).function1 (CAR (x));
550 case 2: return FUNCTION (fn).function2 (CAR (x), CADR (x));
551 case 3: return FUNCTION (fn).function3 (CAR (x), CADR (x), CAR (CDDR (x)));
552 case -1: return FUNCTION (fn).functionn (x);
556 return cell_unspecified;
562 //FIXME: move into fast-non eq_p-ing assq core:assq?
563 //while (a != cell_nil && x != CAAR (a)) a = CDR (a);
564 while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f) a = CDR (a);
565 return a != cell_nil ? CAR (a) : cell_f;
569 assq_ref_env (SCM x, SCM a)
572 if (x == cell_f) return cell_undefined;
577 set_car_x (SCM x, SCM e)
579 assert (TYPE (x) == TPAIR);
581 return cell_unspecified;
585 set_cdr_x (SCM x, SCM e)
587 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_set_cdr_x));
589 return cell_unspecified;
593 set_env_x (SCM x, SCM e, SCM a)
595 SCM p = assert_defined (x, assq (x, a));
596 if (TYPE (p) != TPAIR) error (cell_symbol_not_a_pair, cons (p, x));
597 return set_cdr_x (p, e);
601 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
603 SCM cl = cons (cons (cell_closure, x), x);
606 return cell_unspecified;
610 make_closure_ (SCM args, SCM body, SCM a) ///((internal))
612 return make_cell_ (tmp_num_ (TCLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
616 lookup_macro_ (SCM x, SCM a) ///((internal))
618 if (TYPE (x) != TSYMBOL) return cell_f;
619 SCM m = assq_ref_env (x, a);
620 if (TYPE (m) == TMACRO) return MACRO (m);
625 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
634 return cell_unspecified;
638 gc_peek_frame () ///((internal))
640 SCM frame = CAR (g_stack);
643 r3 = CAR (CDDR (frame));
644 r0 = CADR (CDDR (frame));
649 gc_pop_frame () ///((internal))
651 SCM frame = gc_peek_frame (g_stack);
652 g_stack = CDR (g_stack);
664 case cell_vm_evlis: goto evlis;
665 case cell_vm_evlis2: goto evlis2;
666 case cell_vm_evlis3: goto evlis3;
667 case cell_vm_apply: goto apply;
668 case cell_vm_apply2: goto apply2;
669 case cell_vm_eval: goto eval;
670 #if MES_FIXED_PRIMITIVES
671 case cell_vm_eval_car: goto eval_car;
672 case cell_vm_eval_cdr: goto eval_cdr;
673 case cell_vm_eval_cons: goto eval_cons;
674 case cell_vm_eval_null_p: goto eval_null_p;
676 case cell_vm_eval_set_x: goto eval_set_x;
677 case cell_vm_eval_macro: goto eval_macro;
678 case cell_vm_eval_check_func: goto eval_check_func;
679 case cell_vm_eval2: goto eval2;
680 case cell_vm_macro_expand: goto macro_expand;
681 case cell_vm_begin: goto begin;
682 case cell_vm_begin_read_input_file: goto begin_read_input_file;
683 case cell_vm_begin2: goto begin2;
684 case cell_vm_if: goto vm_if;
685 case cell_vm_if_expr: goto if_expr;
686 case cell_vm_call_with_current_continuation2: goto call_with_current_continuation2;
687 case cell_vm_call_with_values2: goto call_with_values2;
688 case cell_vm_return: goto vm_return;
689 case cell_unspecified: return r1;
697 if (r1 == cell_nil) goto vm_return;
698 if (TYPE (r1) != TPAIR) goto eval;
699 push_cc (CAR (r1), r1, r0, cell_vm_evlis2);
702 push_cc (CDR (r2), r1, r0, cell_vm_evlis3);
710 switch (TYPE (CAR (r1)))
713 check_formals (CAR (r1), MAKE_NUMBER (FUNCTION (CAR (r1)).arity), CDR (r1));
714 r1 = call (CAR (r1), CDR (r1)); /// FIXME: move into eval_apply
719 SCM cl = CLOSURE (CAR (r1));
720 SCM formals = CADR (cl);
721 SCM body = CDDR (cl);
724 check_formals (CAR (r1), formals, CDR (r1));
725 SCM p = pairlis (formals, CDR (r1), aa);
726 call_lambda (body, p, aa, r0);
732 g_stack = CONTINUATION (CAR (r1));
743 push_cc (cons (CADR (r1), CADDR (r1)), r1, r0, cell_vm_return);
748 push_cc (CADR (r1), r1, CADDR (r1), cell_vm_return);
751 case cell_call_with_current_continuation:
754 goto call_with_current_continuation;
756 default: check_apply (cell_f, CAR (r1));
761 if (CAR (r1) == cell_symbol_call_with_values)
764 goto call_with_values;
766 if (CAR (r1) == cell_symbol_current_module)
777 case cell_symbol_lambda:
779 SCM formals = CADR (CAR (r1));
780 SCM body = CDDR (CAR (r1));
781 SCM p = pairlis (formals, CDR (r1), r0);
782 check_formals (r1, formals, CDR (r1));
783 call_lambda (body, p, p, r0);
789 push_cc (CAR (r1), r1, r0, cell_vm_apply2);
792 check_apply (r1, CAR (r2));
793 r1 = cons (r1, CDR (r2));
804 #if MES_FIXED_PRIMITIVES
805 case cell_symbol_car:
807 push_cc (CADR (r1), r1, r0, cell_vm_eval_car); goto eval;
809 x = r1; gc_pop_frame (); r1 = CAR (x); goto eval_apply;
811 case cell_symbol_cdr:
813 push_cc (CADR (r1), r1, r0, cell_vm_eval_cdr); goto eval;
815 x = r1; gc_pop_frame (); r1 = CDR (x); goto eval_apply;
817 case cell_symbol_cons: {
818 push_cc (CDR (r1), r1, r0, cell_vm_eval_cons); goto evlis;
822 r1 = cons (CAR (x), CADR (x));
825 case cell_symbol_null_p:
827 push_cc (CADR (r1), r1, r0, cell_vm_eval_null_p);
830 x = r1; gc_pop_frame (); r1 = null_p (x); goto eval_apply;
832 #endif // MES_FIXED_PRIMITIVES
833 case cell_symbol_quote:
835 x = r1; gc_pop_frame (); r1 = CADR (x); goto eval_apply;
837 case cell_symbol_begin: goto begin;
838 case cell_symbol_lambda:
840 r1 = make_closure_ (CADR (r1), CDDR (r1), assq (cell_closure, r0));
843 case cell_symbol_if: {r1=CDR (r1); goto vm_if;}
844 case cell_symbol_set_x:
846 push_cc (CAR (CDDR (r1)), r1, r0, cell_vm_eval_set_x);
850 r1 = set_env_x (CADR (x), r1, r0);
853 case cell_vm_macro_expand:
855 push_cc (CADR (r1), r1, r0, cell_vm_return);
859 push_cc (r1, r1, r0, cell_vm_eval_macro);
864 if (TYPE (r1) == TPAIR)
866 set_cdr_x (r2, CDR (r1));
867 set_car_x (r2, CAR (r1));
871 push_cc (CAR (r1), r1, r0, cell_vm_eval_check_func); goto eval;
873 push_cc (CDR (r2), r2, r0, cell_vm_eval2); goto evlis;
875 r1 = cons (CAR (r2), r1);
882 r1 = assert_defined (r1, assq_ref_env (r1, r0));
885 default: goto vm_return;
891 if (TYPE (r1) == TPAIR
892 && (macro = lookup_macro_ (CAR (r1), r0)) != cell_f)
894 r1 = cons (macro, CDR (r1));
897 else if (TYPE (r1) == TPAIR
898 && TYPE (CAR (r1)) == TSYMBOL
899 && ((expanders = assq_ref_env (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
900 && ((macro = assq (CAR (r1), expanders)) != cell_f))
902 SCM sc_expand = assq_ref_env (cell_symbol_macro_expand, r0);
903 if (sc_expand != cell_undefined && sc_expand != cell_f)
905 r1 = cons (sc_expand, cons (r1, cell_nil));
912 x = cell_unspecified;
913 while (r1 != cell_nil) {
915 if (TYPE (r1) == TPAIR && TYPE (CAR (r1)) == TPAIR)
917 if (CAAR (r1) == cell_symbol_begin)
918 r1 = append2 (CDAR (r1), CDR (r1));
919 else if (CAAR (r1) == cell_symbol_primitive_load)
921 push_cc (cons (cell_symbol_read_input_file, cell_nil), r1, r0, cell_vm_begin_read_input_file);
923 begin_read_input_file:
924 r1 = append2 (r1, CDR (r2));
927 if (CDR (r1) == cell_nil)
932 push_cc (CAR (r1), r1, r0, cell_vm_begin2);
942 push_cc (CAR (r1), r1, r0, cell_vm_if_expr);
952 if (CDDR (r1) != cell_nil)
954 r1 = CAR (CDDR (r1));
957 r1 = cell_unspecified;
960 call_with_current_continuation:
962 x = MAKE_CONTINUATION (g_continuations++);
964 push_cc (cons (CAR (r1), cons (x, cell_nil)), x, r0, cell_vm_call_with_current_continuation2);
966 call_with_current_continuation2:
967 CONTINUATION (r2) = g_stack;
971 push_cc (cons (CAR (r1), cell_nil), r1, r0, cell_vm_call_with_values2);
974 if (TYPE (r1) == TVALUES)
976 r1 = cons (CADR (r2), r1);
988 apply (SCM f, SCM x, SCM a) ///((internal))
990 push_cc (cons (f, x), cell_unspecified, r0, cell_unspecified);
992 return eval_apply ();
996 mes_g_stack (SCM a) ///((internal))
1002 g_stack = cons (cell_nil, cell_nil);
1006 //
\f Environment setup
1009 make_tmps (struct scm* cells)
1012 cells[tmp].type = TCHAR;
1014 cells[tmp_num].type = TNUMBER;
1015 tmp_num2 = g_free++;
1016 cells[tmp_num2].type = TNUMBER;
1028 gc_init_cells () ///((internal))
1030 int size = ARENA_SIZE * 12;
1033 g_arena = (char*)malloc (size);
1041 //g_cells = (scm *)malloc (2*ARENA_SIZE*sizeof(scm));
1053 gc_init_news () ///((internal))
1055 eputs ("gc_init_news\n");
1056 ///g_news = g_cells-1 + ARENA_SIZE;
1057 //g_news = g_cells + ARENA_SIZE * 12 + GC_SAFETY * 6;
1059 // g_news = g_cells;
1060 int halfway = ARENA_SIZE * 12;
1061 int safety = GC_SAFETY * 12;
1062 safety = safety / 2;
1063 halfway = halfway + safety;
1064 // g_news = g_news + halfway;
1068 eputs (itoa (g_cells));
1070 eputs (itoa (halfway));
1072 eputs (itoa (g_news));
1073 eputs (" news - cells=");
1075 eputs (itoa (p - c));
1079 NTYPE (0) = TVECTOR;
1089 mes_symbols () ///((internal))
1094 #include "mes.mes.symbols.i"
1096 g_symbol_max = g_free;
1097 make_tmps (g_cells);
1100 for (int i=1; i<g_symbol_max; i++)
1101 g_symbols = cons (i, g_symbols);
1105 #include "mes.mes.symbol-names.i"
1107 a = acons (cell_symbol_mes_version, MAKE_STRING (cstring_to_list (VERSION)), a);
1108 a = acons (cell_symbol_mes_prefix, MAKE_STRING (cstring_to_list (PREFIX)), a);
1110 a = acons (cell_symbol_dot, cell_dot, a);
1111 a = acons (cell_symbol_begin, cell_begin, a);
1112 a = acons (cell_symbol_call_with_values, cell_symbol_call_with_values, a);
1113 a = acons (cell_symbol_current_module, cell_symbol_current_module, a);
1114 a = acons (cell_symbol_call_with_current_continuation, cell_call_with_current_continuation, a);
1115 a = acons (cell_symbol_sc_expand, cell_f, a);
1118 a = acons (cell_symbol_gnuc, cell_t, a);
1119 a = acons (cell_symbol_mesc, cell_f, a);
1121 a = acons (cell_symbol_gnuc, cell_f, a);
1122 a = acons (cell_symbol_mesc, cell_t, a);
1125 a = acons (cell_closure, a, a);
1131 mes_environment () ///((internal))
1133 SCM a = mes_symbols ();
1134 return mes_g_stack (a);
1138 mes_builtins (SCM a) ///((internal))
1140 #include "mes.mes.i"
1142 // Do not sort: Order of these includes define builtins
1143 #include "posix.mes.i"
1144 #include "math.mes.i"
1145 #include "lib.mes.i"
1146 #include "vector.mes.i"
1148 // #include "reader.mes.i"
1150 #include "gc.mes.environment.i"
1151 #include "lib.mes.environment.i"
1152 #include "math.mes.environment.i"
1153 #include "mes.mes.environment.i"
1154 #include "posix.mes.environment.i"
1155 // #include "reader.mes.environment.i"
1156 #include "vector.mes.environment.i"
1162 bload_env (SCM a) ///((internal))
1164 char *mo = "module/mes/read-0-32.mo";
1165 g_stdin = open (mo, 0);
1166 if (g_stdin < 0) {eputs ("no such file: ");eputs (mo);eputs ("\n");return 1;}
1167 assert (getchar () == 'M');
1168 assert (getchar () == 'E');
1169 assert (getchar () == 'S');
1170 eputs ("*GOT MES*\n");
1171 g_stack = getchar () << 8;
1172 g_stack += getchar ();
1174 char *p = (char*)g_cells;
1181 g_free = (p-(char*)g_cells) /
1185 r0 = mes_builtins (r0);
1188 set_env_x (cell_symbol_gnuc, cell_t, r0);
1189 set_env_x (cell_symbol_mesc, cell_f, r0);
1191 set_env_x (cell_symbol_gnuc, cell_f, r0);
1192 set_env_x (cell_symbol_mesc, cell_t, r0);
1197 eputs ("symbols: ");
1199 while (s && s != cell_nil) {
1200 display_error_ (CAR (s));
1205 eputs ("functions: ");
1206 eputs (itoa (g_function));
1208 for (int i = 0; i < g_function; i++)
1213 eputs (g_functions[i].name);
1216 //display_error_ (r0);
1226 main (int argc, char *argv[])
1229 if (p = getenv ("MES_DEBUG")) g_debug = atoi (p);
1230 if (g_debug) {eputs (";;; MODULEDIR=");eputs (MODULEDIR);eputs ("\n");}
1231 if (p = getenv ("MES_MAX_ARENA")) MAX_ARENA_SIZE = atoi (p);
1232 if (p = getenv ("MES_ARENA")) ARENA_SIZE = atoi (p);
1233 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes [--dump|--load] < FILE\n");
1234 if (argc > 1 && !strcmp (argv[1], "--version")) {puts ("Mes ");puts (VERSION);puts ("\n");return 0;};
1236 r0 = mes_environment ();
1238 SCM program = bload_env (r0);
1240 for (int i=argc-1; i>=0; i--) lst = cons (MAKE_STRING (cstring_to_list (argv[i])), lst);
1241 r0 = acons (cell_symbol_argv, lst, r0);
1242 push_cc (r2, cell_unspecified, r0, cell_unspecified);
1245 eputs ("program: ");
1246 display_error_ (r1);
1251 display_error_ (r1);
1256 eputs ("\nstats: [");
1257 eputs (itoa (g_free));