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/>.
27 int ARENA_SIZE = 100000;
28 int MAX_ARENA_SIZE = 20000000;
29 //int GC_SAFETY_DIV = 400;
30 //int GC_SAFETY = ARENA_SIZE / 400;
39 SCM g_continuations = 0;
51 enum type_t {TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TREF, TSPECIAL, TSTRING, TSYMBOL, TVALUES, TVECTOR, TBROKEN_HEART};
60 int (*function) (void);
65 typedef SCM (*function0_t) (void);
66 typedef SCM (*function1_t) (SCM);
67 typedef SCM (*function2_t) (SCM, SCM);
68 typedef SCM (*function3_t) (SCM, SCM, SCM);
69 typedef SCM (*functionn_t) (SCM);
72 function0_t function0;
73 function1_t function1;
74 function2_t function2;
75 function3_t function3;
76 functionn_t functionn;
106 struct scm *g_cells = foobar;
107 struct scm *g_news = foobar;
109 struct scm *g_cells = 0;
110 struct scm *g_news = 0;
113 struct scm scm_nil = {TSPECIAL, "()",0};
114 struct scm scm_f = {TSPECIAL, "#f",0};
115 struct scm scm_t = {TSPECIAL, "#t",0};
116 struct scm scm_dot = {TSPECIAL, ".",0};
117 struct scm scm_arrow = {TSPECIAL, "=>",0};
118 struct scm scm_undefined = {TSPECIAL, "*undefined*",0};
119 struct scm scm_unspecified = {TSPECIAL, "*unspecified*",0};
120 struct scm scm_closure = {TSPECIAL, "*closure*",0};
121 struct scm scm_circular = {TSPECIAL, "*circular*",0};
122 struct scm scm_begin = {TSPECIAL, "*begin*",0};
124 struct scm scm_symbol_dot = {TSYMBOL, "*dot*",0};
125 struct scm scm_symbol_lambda = {TSYMBOL, "lambda",0};
126 struct scm scm_symbol_begin = {TSYMBOL, "begin",0};
127 struct scm scm_symbol_if = {TSYMBOL, "if",0};
128 struct scm scm_symbol_quote = {TSYMBOL, "quote",0};
129 struct scm scm_symbol_set_x = {TSYMBOL, "set!",0};
131 struct scm scm_symbol_sc_expand = {TSYMBOL, "sc-expand",0};
132 struct scm scm_symbol_macro_expand = {TSYMBOL, "macro-expand",0};
133 struct scm scm_symbol_sc_expander_alist = {TSYMBOL, "*sc-expander-alist*",0};
135 struct scm scm_symbol_call_with_values = {TSYMBOL, "call-with-values",0};
136 struct scm scm_call_with_current_continuation = {TSPECIAL, "*call/cc*",0};
137 struct scm scm_symbol_call_with_current_continuation = {TSYMBOL, "call-with-current-continuation",0};
138 struct scm scm_symbol_current_module = {TSYMBOL, "current-module",0};
139 struct scm scm_symbol_primitive_load = {TSYMBOL, "primitive-load",0};
140 struct scm scm_symbol_read_input_file = {TSYMBOL, "read-input-file",0};
141 struct scm scm_symbol_write = {TSYMBOL, "write",0};
142 struct scm scm_symbol_display = {TSYMBOL, "display",0};
144 struct scm scm_symbol_throw = {TSYMBOL, "throw",0};
145 struct scm scm_symbol_not_a_pair = {TSYMBOL, "not-a-pair",0};
146 struct scm scm_symbol_system_error = {TSYMBOL, "system-error",0};
147 struct scm scm_symbol_wrong_number_of_args = {TSYMBOL, "wrong-number-of-args",0};
148 struct scm scm_symbol_wrong_type_arg = {TSYMBOL, "wrong-type-arg",0};
149 struct scm scm_symbol_unbound_variable = {TSYMBOL, "unbound-variable",0};
151 struct scm scm_symbol_argv = {TSYMBOL, "%argv",0};
152 struct scm scm_symbol_mes_prefix = {TSYMBOL, "%prefix",0};
153 struct scm scm_symbol_mes_version = {TSYMBOL, "%version",0};
155 struct scm scm_symbol_car = {TSYMBOL, "car",0};
156 struct scm scm_symbol_cdr = {TSYMBOL, "cdr",0};
157 struct scm scm_symbol_null_p = {TSYMBOL, "null?",0};
158 struct scm scm_symbol_eq_p = {TSYMBOL, "eq?",0};
159 struct scm scm_symbol_cons = {TSYMBOL, "cons",0};
161 struct scm scm_vm_evlis = {TSPECIAL, "*vm-evlis*",0};
162 struct scm scm_vm_evlis2 = {TSPECIAL, "*vm-evlis2*",0};
163 struct scm scm_vm_evlis3 = {TSPECIAL, "*vm-evlis3*",0};
164 struct scm scm_vm_apply = {TSPECIAL, "core:apply",0};
165 struct scm scm_vm_apply2 = {TSPECIAL, "*vm-apply2*",0};
166 struct scm scm_vm_eval = {TSPECIAL, "core:eval",0};
169 struct scm scm_vm_eval_car = {TSPECIAL, "*vm-eval-car*",0};
170 struct scm scm_vm_eval_cdr = {TSPECIAL, "*vm-eval-cdr*",0};
171 struct scm scm_vm_eval_cons = {TSPECIAL, "*vm-eval-cons*",0};
172 struct scm scm_vm_eval_null_p = {TSPECIAL, "*vm-eval-null-p*",0};
174 struct scm scm_vm_eval_set_x = {TSPECIAL, "*vm-eval-set!*",0};
175 struct scm scm_vm_eval_macro = {TSPECIAL, "*vm-eval-macro*",0};
176 struct scm scm_vm_eval_check_func = {TSPECIAL, "*vm-eval-check-func*",0};
177 struct scm scm_vm_eval2 = {TSPECIAL, "*vm-eval2*",0};
178 struct scm scm_vm_macro_expand = {TSPECIAL, "core:macro-expand",0};
179 struct scm scm_vm_begin = {TSPECIAL, "*vm-begin*",0};
180 struct scm scm_vm_begin_read_input_file = {TSPECIAL, "*vm-begin-read-input-file*",0};
181 struct scm scm_vm_begin2 = {TSPECIAL, "*vm-begin2*",0};
182 struct scm scm_vm_if = {TSPECIAL, "*vm-if*",0};
183 struct scm scm_vm_if_expr = {TSPECIAL, "*vm-if-expr*",0};
184 struct scm scm_vm_call_with_values2 = {TSPECIAL, "*vm-call-with-values2*",0};
185 struct scm scm_vm_call_with_current_continuation2 = {TSPECIAL, "*vm-call-with-current-continuation2*",0};
186 struct scm scm_vm_return = {TSPECIAL, "*vm-return*",0};
188 struct scm scm_symbol_gnuc = {TSYMBOL, "%gnuc",0};
189 struct scm scm_symbol_mesc = {TSYMBOL, "%mesc",0};
191 struct scm scm_test = {TSYMBOL, "test",0};
194 #include "mes.mes.symbols.h"
196 #include "mes.symbols.h"
203 struct function g_functions[200];
206 #if !__GNUC__ || !_POSIX_SOURCE
209 #include "math.mes.h"
211 #include "posix.mes.h"
213 #include "reader.mes.h"
215 #include "vector.mes.h"
226 #define TYPE(x) g_cells[x].type
227 #define CAR(x) g_cells[x].car
228 #define CDR(x) g_cells[x].cdr
230 #define NTYPE(x) g_news[x].type
231 #define NCAR(x) g_news[x].car
232 #define NCDR(x) g_news[x].cdr
235 #define LENGTH(x) g_cells[x].car
236 #define REF(x) g_cells[x].car
237 #define STRING(x) g_cells[x].car
239 #define CLOSURE(x) g_cells[x].cdr
240 #define CONTINUATION(x) g_cells[x].cdr
242 #define FUNCTION(x) g_functions[g_cells[x].cdr]
243 #define MACRO(x) g_cells[x].cdr
244 #define VALUE(x) g_cells[x].cdr
245 #define VECTOR(x) g_cells[x].cdr
247 #define NLENGTH(x) g_news[x].car
249 #define NVALUE(x) g_news[x].cdr
250 #define NVECTOR(x) g_news[x].cdr
253 #define CONTINUATION(x) g_cells[x].cdr
254 #define HITS(x) g_cells[x].hits
255 #define LENGTH(x) g_cells[x].length
256 #define NAME(x) g_cells[x].name
257 #define STRING(x) g_cells[x].string
258 #define CLOSURE(x) g_cells[x].closure
259 #define MACRO(x) g_cells[x].macro
260 #define REF(x) g_cells[x].ref
261 #define VALUE(x) g_cells[x].value
262 #define VECTOR(x) g_cells[x].vector
263 #define FUNCTION(x) g_functions[g_cells[x].function]
265 #define NLENGTH(x) g_news[x].length
267 #define NVALUE(x) g_news[x].value
268 #define NVECTOR(x) g_news[x].vector
271 #define MAKE_CHAR(n) make_cell_ (tmp_num_ (TCHAR), 0, tmp_num2_ (n))
272 #define MAKE_CONTINUATION(n) make_cell_ (tmp_num_ (TCONTINUATION), n, g_stack)
273 #define MAKE_NUMBER(n) make_cell_ (tmp_num_ (TNUMBER), 0, tmp_num2_ (n))
274 #define MAKE_REF(n) make_cell_ (tmp_num_ (TREF), n, 0)
275 #define MAKE_STRING(x) make_cell_ (tmp_num_ (TSTRING), x, 0)
277 #define CAAR(x) CAR (CAR (x))
278 #define CADR(x) CAR (CDR (x))
279 #define CDAR(x) CDR (CAR (x))
280 #define CDDR(x) CDR (CDR (x))
281 #define CADAR(x) CAR (CDR (CAR (x)))
282 #define CADDR(x) CAR (CDR (CDR (x)))
283 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
288 assert (g_free + n < ARENA_SIZE);
304 VALUE (tmp_num2) = x;
309 make_cell_ (SCM type, SCM car, SCM cdr)
312 assert (TYPE (type) == TNUMBER);
313 TYPE (x) = VALUE (type);
314 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
315 if (car) CAR (x) = CAR (car);
316 if (cdr) CDR(x) = CDR(cdr);
318 else if (VALUE (type) == TFUNCTION) {
319 if (car) CAR (x) = car;
320 if (cdr) CDR(x) = CDR(cdr);
330 make_symbol_ (SCM s) ///((internal))
332 VALUE (tmp_num) = TSYMBOL;
333 SCM x = make_cell_ (tmp_num, s, 0);
334 g_symbols = cons (x, g_symbols);
339 list_of_char_equal_p (SCM a, SCM b) ///((internal))
341 while (a != cell_nil && b != cell_nil && VALUE (CAR (a)) == VALUE (CAR (b))) {
342 assert (TYPE (CAR (a)) == TCHAR);
343 assert (TYPE (CAR (b)) == TCHAR);
347 return (a == cell_nil && b == cell_nil) ? cell_t : cell_f;
351 lookup_symbol_ (SCM s)
355 if (list_of_char_equal_p (STRING (CAR (x)), s) == cell_t) break;
359 if (!x) x = make_symbol_ (s);
366 return MAKE_NUMBER (TYPE (x));
372 return (TYPE (x) != TCONTINUATION
373 && (TYPE (CAR (x)) == TPAIR // FIXME: this is weird
374 || TYPE (CAR (x)) == TREF
375 || TYPE (CAR (x)) == TSPECIAL
376 || TYPE (CAR (x)) == TSYMBOL
377 || TYPE (CAR (x)) == TSTRING)) ? CAR (x) : MAKE_NUMBER (CAR (x));
383 return (TYPE (CDR (x)) == TPAIR
384 || TYPE (CDR (x)) == TREF
385 || TYPE (CAR (x)) == TSPECIAL
386 || TYPE (CDR (x)) == TSYMBOL
387 || TYPE (CDR (x)) == TSTRING) ? CDR (x) : MAKE_NUMBER (CDR (x));
393 assert (TYPE (x) == TFUNCTION);
394 return MAKE_NUMBER (FUNCTION (x).arity);
400 VALUE (tmp_num) = TPAIR;
401 return make_cell_ (tmp_num, x, y);
408 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_car));
417 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_cdr));
423 list (SCM x) ///((arity . n))
431 return x == cell_nil ? cell_t : cell_f;
438 || ((TYPE (x) == TKEYWORD && TYPE (y) == TKEYWORD
439 && STRING (x) == STRING (y)))
440 || (TYPE (x) == TCHAR && TYPE (y) == TCHAR
441 && VALUE (x) == VALUE (y))
442 || (TYPE (x) == TNUMBER && TYPE (y) == TNUMBER
443 && VALUE (x) == VALUE (y)))
448 values (SCM x) ///((arity . n))
456 acons (SCM key, SCM value, SCM alist)
458 return cons (cons (key, value), alist);
465 while (x != cell_nil)
468 if (TYPE (x) != TPAIR) return MAKE_NUMBER (-1);
471 return MAKE_NUMBER (n);
474 SCM apply (SCM, SCM, SCM);
477 error (SCM key, SCM x)
481 if ((throw = assq_ref_env (cell_symbol_throw, r0)) != cell_undefined)
482 return apply (throw, cons (key, cons (x, cell_nil)), r0);
484 display_error_ (key);
492 cstring_to_list (char const* s)
497 p = cons (MAKE_CHAR (s[i]), p);
503 assert_defined (SCM x, SCM e) ///((internal))
505 if (e == cell_undefined) return error (cell_symbol_unbound_variable, x);
510 check_formals (SCM f, SCM formals, SCM args) ///((internal))
512 int flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : VALUE (length (formals));
513 int alen = VALUE (length (args));
514 if (alen != flen && alen != -1 && flen != -1)
516 char *s = "apply: wrong number of arguments; expected: ";
523 SCM e = MAKE_STRING (cstring_to_list (s));
524 return error (cell_symbol_wrong_number_of_args, cons (e, f));
526 return cell_unspecified;
530 check_apply (SCM f, SCM e) ///((internal))
533 if (f == cell_f || f == cell_t) type = "bool";
534 if (f == cell_nil) type = "nil";
535 if (f == cell_unspecified) type = "*unspecified*";
536 if (f == cell_undefined) type = "*undefined*";
537 if (TYPE (f) == TCHAR) type = "char";
538 if (TYPE (f) == TNUMBER) type = "number";
539 if (TYPE (f) == TSTRING) type = "string";
543 char *s = "cannot apply: ";
549 SCM e = MAKE_STRING (cstring_to_list (s));
550 return error (cell_symbol_wrong_type_arg, cons (e, f));
552 return cell_unspecified;
556 gc_push_frame () ///((internal))
558 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
559 g_stack = cons (frame, g_stack);
564 append2 (SCM x, SCM y)
566 if (x == cell_nil) return y;
567 assert (TYPE (x) == TPAIR);
568 return cons (car (x), append2 (cdr (x), y));
572 pairlis (SCM x, SCM y, SCM a)
576 if (TYPE (x) != TPAIR)
577 return cons (cons (x, y), a);
578 return cons (cons (car (x), car (y)),
579 pairlis (cdr (x), cdr (y), a));
585 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
586 && x != cell_nil && TYPE (CAR (x)) == TVALUES)
587 x = cons (CADAR (x), CDR (x));
588 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
589 && x != cell_nil && TYPE (CDR (x)) == TPAIR && TYPE (CADR (x)) == TVALUES)
590 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
591 switch (FUNCTION (fn).arity)
593 #if __MESC__ || !_POSIX_SOURCE
594 case 0: return (FUNCTION (fn).function) ();
595 case 1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (CAR (x));
596 case 2: return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x));
597 case 3: return ((SCM(*)(SCM,SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x), CAR (CDDR (x)));
598 case -1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
599 default: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
601 case 0: return FUNCTION (fn).function0 ();
602 case 1: return FUNCTION (fn).function1 (CAR (x));
603 case 2: return FUNCTION (fn).function2 (CAR (x), CADR (x));
604 case 3: return FUNCTION (fn).function3 (CAR (x), CADR (x), CAR (CDDR (x)));
605 case -1: return FUNCTION (fn).functionn (x);
609 return cell_unspecified;
615 //FIXME: move into fast-non eq_p-ing assq core:assq?
616 //while (a != cell_nil && x != CAAR (a)) a = CDR (a);
617 while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f) a = CDR (a);
618 return a != cell_nil ? CAR (a) : cell_f;
622 assq_ref_env (SCM x, SCM a)
625 if (x == cell_f) return cell_undefined;
630 set_car_x (SCM x, SCM e)
632 assert (TYPE (x) == TPAIR);
634 return cell_unspecified;
638 set_cdr_x (SCM x, SCM e)
640 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_set_cdr_x));
642 return cell_unspecified;
646 set_env_x (SCM x, SCM e, SCM a)
648 SCM p = assert_defined (x, assq (x, a));
649 if (TYPE (p) != TPAIR) error (cell_symbol_not_a_pair, cons (p, x));
650 return set_cdr_x (p, e);
654 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
656 SCM cl = cons (cons (cell_closure, x), x);
659 return cell_unspecified;
663 make_closure_ (SCM args, SCM body, SCM a) ///((internal))
665 return make_cell_ (tmp_num_ (TCLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
669 lookup_macro_ (SCM x, SCM a) ///((internal))
671 if (TYPE (x) != TSYMBOL) return cell_f;
672 SCM m = assq_ref_env (x, a);
673 if (TYPE (m) == TMACRO) return MACRO (m);
678 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
687 return cell_unspecified;
691 gc_peek_frame () ///((internal))
693 SCM frame = CAR (g_stack);
696 r3 = CAR (CDDR (frame));
697 r0 = CADR (CDDR (frame));
702 gc_pop_frame () ///((internal))
704 SCM frame = gc_peek_frame (g_stack);
705 g_stack = CDR (g_stack);
716 case cell_vm_evlis: goto evlis;
717 case cell_vm_evlis2: goto evlis2;
718 case cell_vm_evlis3: goto evlis3;
719 case cell_vm_apply: goto apply;
720 case cell_vm_apply2: goto apply2;
721 case cell_vm_eval: goto eval;
723 case cell_vm_eval_car: goto eval_car;
724 case cell_vm_eval_cdr: goto eval_cdr;
725 case cell_vm_eval_cons: goto eval_cons;
726 case cell_vm_eval_null_p: goto eval_null_p;
728 case cell_vm_eval_set_x: goto eval_set_x;
729 case cell_vm_eval_macro: goto eval_macro;
730 case cell_vm_eval_check_func: goto eval_check_func;
731 case cell_vm_eval2: goto eval2;
732 case cell_vm_macro_expand: goto macro_expand;
733 case cell_vm_begin: goto begin;
734 case cell_vm_begin_read_input_file: goto begin_read_input_file;
735 case cell_vm_begin2: goto begin2;
736 case cell_vm_if: goto vm_if;
737 case cell_vm_if_expr: goto if_expr;
738 case cell_vm_call_with_current_continuation2: goto call_with_current_continuation2;
739 case cell_vm_call_with_values2: goto call_with_values2;
740 case cell_vm_return: goto vm_return;
741 case cell_unspecified: return r1;
749 if (r1 == cell_nil) goto vm_return;
750 if (TYPE (r1) != TPAIR) goto eval;
751 push_cc (CAR (r1), r1, r0, cell_vm_evlis2);
754 push_cc (CDR (r2), r1, r0, cell_vm_evlis3);
762 switch (TYPE (CAR (r1)))
765 check_formals (CAR (r1), MAKE_NUMBER (FUNCTION (CAR (r1)).arity), CDR (r1));
766 r1 = call (CAR (r1), CDR (r1)); /// FIXME: move into eval_apply
771 SCM cl = CLOSURE (CAR (r1));
772 SCM formals = CADR (cl);
773 SCM body = CDDR (cl);
776 check_formals (CAR (r1), formals, CDR (r1));
777 SCM p = pairlis (formals, CDR (r1), aa);
778 call_lambda (body, p, aa, r0);
784 g_stack = CONTINUATION (CAR (r1));
795 push_cc (cons (CADR (r1), CADDR (r1)), r1, r0, cell_vm_return);
800 push_cc (CADR (r1), r1, CADDR (r1), cell_vm_return);
803 case cell_call_with_current_continuation:
806 goto call_with_current_continuation;
808 default: check_apply (cell_f, CAR (r1));
813 if (CAR (r1) == cell_symbol_call_with_values)
816 goto call_with_values;
818 if (CAR (r1) == cell_symbol_current_module)
829 case cell_symbol_lambda:
831 SCM formals = CADR (CAR (r1));
832 SCM body = CDDR (CAR (r1));
833 SCM p = pairlis (formals, CDR (r1), r0);
834 check_formals (r1, formals, CDR (r1));
835 call_lambda (body, p, p, r0);
841 push_cc (CAR (r1), r1, r0, cell_vm_apply2);
844 check_apply (r1, CAR (r2));
845 r1 = cons (r1, CDR (r2));
857 case cell_symbol_car:
859 push_cc (CADR (r1), r1, r0, cell_vm_eval_car); goto eval;
861 x = r1; gc_pop_frame (); r1 = CAR (x); goto eval_apply;
863 case cell_symbol_cdr:
865 push_cc (CADR (r1), r1, r0, cell_vm_eval_cdr); goto eval;
867 x = r1; gc_pop_frame (); r1 = CDR (x); goto eval_apply;
869 case cell_symbol_cons: {
870 push_cc (CDR (r1), r1, r0, cell_vm_eval_cons); goto evlis;
874 r1 = cons (CAR (x), CADR (x));
877 case cell_symbol_null_p:
879 push_cc (CADR (r1), r1, r0, cell_vm_eval_null_p);
882 x = r1; gc_pop_frame (); r1 = null_p (x); goto eval_apply;
884 #endif // FIXED_PRIMITIVES
885 case cell_symbol_quote:
887 x = r1; gc_pop_frame (); r1 = CADR (x); goto eval_apply;
889 case cell_symbol_begin: goto begin;
890 case cell_symbol_lambda:
892 r1 = make_closure_ (CADR (r1), CDDR (r1), assq (cell_closure, r0));
895 case cell_symbol_if: {r1=CDR (r1); goto vm_if;}
896 case cell_symbol_set_x:
898 push_cc (CAR (CDDR (r1)), r1, r0, cell_vm_eval_set_x);
902 r1 = set_env_x (CADR (x), r1, r0);
905 case cell_vm_macro_expand:
907 push_cc (CADR (r1), r1, r0, cell_vm_return);
911 push_cc (r1, r1, r0, cell_vm_eval_macro);
916 if (TYPE (r1) == TPAIR)
918 set_cdr_x (r2, CDR (r1));
919 set_car_x (r2, CAR (r1));
923 push_cc (CAR (r1), r1, r0, cell_vm_eval_check_func); goto eval;
925 push_cc (CDR (r2), r2, r0, cell_vm_eval2); goto evlis;
927 r1 = cons (CAR (r2), r1);
934 r1 = assert_defined (r1, assq_ref_env (r1, r0));
937 default: goto vm_return;
943 if (TYPE (r1) == TPAIR
944 && (macro = lookup_macro_ (CAR (r1), r0)) != cell_f)
946 r1 = cons (macro, CDR (r1));
949 else if (TYPE (r1) == TPAIR
950 && TYPE (CAR (r1)) == TSYMBOL
951 && ((expanders = assq_ref_env (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
952 && ((macro = assq (CAR (r1), expanders)) != cell_f))
954 SCM sc_expand = assq_ref_env (cell_symbol_macro_expand, r0);
955 if (sc_expand != cell_undefined && sc_expand != cell_f)
957 r1 = cons (sc_expand, cons (r1, cell_nil));
964 x = cell_unspecified;
965 while (r1 != cell_nil) {
967 if (TYPE (r1) == TPAIR && TYPE (CAR (r1)) == TPAIR)
969 if (CAAR (r1) == cell_symbol_begin)
970 r1 = append2 (CDAR (r1), CDR (r1));
971 else if (CAAR (r1) == cell_symbol_primitive_load)
973 push_cc (cons (cell_symbol_read_input_file, cell_nil), r1, r0, cell_vm_begin_read_input_file);
975 begin_read_input_file:
976 r1 = append2 (r1, CDR (r2));
979 if (CDR (r1) == cell_nil)
984 push_cc (CAR (r1), r1, r0, cell_vm_begin2);
994 push_cc (CAR (r1), r1, r0, cell_vm_if_expr);
1004 if (CDDR (r1) != cell_nil)
1006 r1 = CAR (CDDR (r1));
1009 r1 = cell_unspecified;
1012 call_with_current_continuation:
1014 x = MAKE_CONTINUATION (g_continuations++);
1016 push_cc (cons (CAR (r1), cons (x, cell_nil)), x, r0, cell_vm_call_with_current_continuation2);
1018 call_with_current_continuation2:
1019 CONTINUATION (r2) = g_stack;
1023 push_cc (cons (CAR (r1), cell_nil), r1, r0, cell_vm_call_with_values2);
1026 if (TYPE (r1) == TVALUES)
1028 r1 = cons (CADR (r2), r1);
1039 apply (SCM f, SCM x, SCM a) ///((internal))
1041 push_cc (cons (f, x), cell_unspecified, r0, cell_unspecified);
1043 return eval_apply ();
1047 mes_g_stack (SCM a) ///((internal))
1053 g_stack = cons (cell_nil, cell_nil);
1057 //
\f Environment setup
1060 make_tmps (struct scm* cells)
1063 cells[tmp].type = TCHAR;
1065 cells[tmp_num].type = TNUMBER;
1066 tmp_num2 = g_free++;
1067 cells[tmp_num2].type = TNUMBER;
1079 gc_init_cells () ///((internal))
1081 g_cells = (struct scm *)malloc (2*ARENA_SIZE*sizeof (struct scm));
1087 g_cells += sizeof (struct scm);
1097 gc_init_news () ///((internal))
1101 p -= sizeof (struct scm);
1102 p += ARENA_SIZE * sizeof (struct scm);
1105 g_news = g_cells-1 + ARENA_SIZE;
1108 NTYPE (0) = TVECTOR;
1112 g_news += sizeof (struct scm);
1122 mes_symbols () ///((internal))
1128 #include "mes.mes.symbols.i"
1130 #include "mes.symbols.i"
1133 g_symbol_max = g_free;
1134 make_tmps (g_cells);
1137 for (int i=1; i<g_symbol_max; i++)
1138 g_symbols = cons (i, g_symbols);
1143 #include "mes.mes.symbol-names.i"
1145 #include "mes.symbol-names.i"
1148 a = acons (cell_symbol_mes_version, MAKE_STRING (cstring_to_list (VERSION)), a);
1149 a = acons (cell_symbol_mes_prefix, MAKE_STRING (cstring_to_list (PREFIX)), a);
1151 a = acons (cell_symbol_dot, cell_dot, a);
1152 a = acons (cell_symbol_begin, cell_begin, a);
1153 a = acons (cell_symbol_call_with_values, cell_symbol_call_with_values, a);
1154 a = acons (cell_symbol_current_module, cell_symbol_current_module, a);
1155 a = acons (cell_symbol_call_with_current_continuation, cell_call_with_current_continuation, a);
1156 a = acons (cell_symbol_sc_expand, cell_f, a);
1159 a = acons (cell_symbol_gnuc, cell_t, a);
1160 a = acons (cell_symbol_mesc, cell_f, a);
1162 a = acons (cell_symbol_gnuc, cell_f, a);
1163 a = acons (cell_symbol_mesc, cell_t, a);
1166 a = acons (cell_closure, a, a);
1172 mes_environment () ///((internal))
1174 SCM a = mes_symbols ();
1175 return mes_g_stack (a);
1179 mes_builtins (SCM a) ///((internal))
1181 #if !__GNUC__ || !_POSIX_SOURCE
1182 #include "mes.mes.i"
1184 // Do not sort: Order of these includes define builtins
1185 #include "posix.mes.i"
1186 #include "math.mes.i"
1187 #include "lib.mes.i"
1188 #include "vector.mes.i"
1191 #include "reader.mes.i"
1194 #include "gc.mes.environment.i"
1195 #include "lib.mes.environment.i"
1196 #include "math.mes.environment.i"
1197 #include "mes.mes.environment.i"
1198 #include "posix.mes.environment.i"
1200 #include "reader.mes.environment.i"
1202 #include "vector.mes.environment.i"
1206 // Do not sort: Order of these includes define builtins
1214 #include "gc.environment.i"
1215 #include "lib.environment.i"
1216 #include "math.environment.i"
1217 #include "mes.environment.i"
1218 #include "posix.environment.i"
1219 #include "reader.environment.i"
1220 #include "vector.environment.i"
1225 fputs ("functions: ", STDERR);
1226 fputs (itoa (g_function), STDERR);
1227 fputs ("\n", STDERR);
1228 for (int i = 0; i < g_function; i++)
1230 fputs ("[", STDERR);
1231 fputs (itoa (i), STDERR);
1232 fputs ("]: ", STDERR);
1233 fputs (g_functions[i].name, STDERR);
1234 fputs ("\n", STDERR);
1236 fputs ("\n", STDERR);
1243 load_env (SCM a) ///((internal))
1246 g_stdin = open ("module/mes/read-0.mes", O_RDONLY);
1247 g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "/mes/read-0.mes", O_RDONLY);
1248 if (!g_function) r0 = mes_builtins (r0);
1249 r2 = read_input_file_env (r0);
1255 bload_env (SCM a) ///((internal))
1258 char *mo = "mes/read-0-32.mo";
1259 g_stdin = open ("module/mes/read-0-32.mo", O_RDONLY);
1260 g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "/mes/read-0-32.mo", O_RDONLY);
1262 char *mo ="mes/read-0.mo";
1263 g_stdin = open ("module/mes/read-0.mo", O_RDONLY);
1264 g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "/mes/read-0.mo", O_RDONLY);
1267 if (g_stdin < 0) {eputs ("no such file: ");eputs (mo);eputs ("\n");return 1;}
1268 assert (getchar () == 'M');
1269 assert (getchar () == 'E');
1270 assert (getchar () == 'S');
1272 if (g_debug) eputs ("*GOT MES*\n");
1273 g_stack = getchar () << 8;
1274 g_stack += getchar ();
1276 char *p = (char*)g_cells;
1283 g_free = (p-(char*)g_cells) / sizeof (struct scm);
1287 r0 = mes_builtins (r0);
1290 set_env_x (cell_symbol_gnuc, cell_t, r0);
1291 set_env_x (cell_symbol_mesc, cell_f, r0);
1293 set_env_x (cell_symbol_gnuc, cell_f, r0);
1294 set_env_x (cell_symbol_mesc, cell_t, r0);
1299 eputs ("symbols: ");
1301 while (s && s != cell_nil) {
1302 display_error_ (CAR (s));
1307 eputs ("functions: ");
1308 eputs (itoa (g_function));
1310 for (int i = 0; i < g_function; i++)
1315 eputs (g_functions[i].name);
1318 //display_error_ (r0);
1326 #if _POSIX_SOURCE || MES_FULL
1331 main (int argc, char *argv[])
1334 if (p = getenv ("MES_DEBUG")) g_debug = atoi (p);
1335 if (g_debug) {eputs (";;; MODULEDIR=");eputs (MODULEDIR);eputs ("\n");}
1336 if (p = getenv ("MES_MAX_ARENA")) MAX_ARENA_SIZE = atoi (p);
1337 if (p = getenv ("MES_ARENA")) ARENA_SIZE = atoi (p);
1338 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes [--dump|--load] < FILE\n");
1339 if (argc > 1 && !strcmp (argv[1], "--version")) {puts ("Mes ");puts (VERSION);puts ("\n");return 0;};
1341 r0 = mes_environment ();
1344 SCM program = bload_env (r0);
1346 SCM program = (argc > 1 && !strcmp (argv[1], "--load"))
1347 ? bload_env (r0) : load_env (r0);
1348 g_tiny = argc > 2 && !strcmp (argv[2], "--tiny");
1349 if (argc > 1 && !strcmp (argv[1], "--dump")) return dump ();
1353 for (int i=argc-1; i>=0; i--) lst = cons (MAKE_STRING (cstring_to_list (argv[i])), lst);
1354 r0 = acons (cell_symbol_argv, lst, r0);
1355 push_cc (r2, cell_unspecified, r0, cell_unspecified);
1358 eputs ("program: ");
1359 display_error_ (r1);
1364 display_error_ (r1);
1369 eputs ("\nstats: [");
1370 eputs (itoa (g_free));