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"
212 #include "reader.mes.h"
213 #include "vector.mes.h"
224 #define TYPE(x) g_cells[x].type
225 #define CAR(x) g_cells[x].car
226 #define CDR(x) g_cells[x].cdr
228 #define NTYPE(x) g_news[x].type
229 #define NCAR(x) g_news[x].car
230 #define NCDR(x) g_news[x].cdr
233 #define LENGTH(x) g_cells[x].car
234 #define REF(x) g_cells[x].car
235 #define STRING(x) g_cells[x].car
237 #define CLOSURE(x) g_cells[x].cdr
238 #define CONTINUATION(x) g_cells[x].cdr
240 #define FUNCTION(x) g_functions[g_cells[x].cdr]
241 #define MACRO(x) g_cells[x].cdr
242 #define VALUE(x) g_cells[x].cdr
243 #define VECTOR(x) g_cells[x].cdr
245 #define NLENGTH(x) g_news[x].car
247 #define NVALUE(x) g_news[x].cdr
248 #define NVECTOR(x) g_news[x].cdr
251 #define CONTINUATION(x) g_cells[x].cdr
252 #define HITS(x) g_cells[x].hits
253 #define LENGTH(x) g_cells[x].length
254 #define NAME(x) g_cells[x].name
255 #define STRING(x) g_cells[x].string
256 #define CLOSURE(x) g_cells[x].closure
257 #define MACRO(x) g_cells[x].macro
258 #define REF(x) g_cells[x].ref
259 #define VALUE(x) g_cells[x].value
260 #define VECTOR(x) g_cells[x].vector
261 #define FUNCTION(x) g_functions[g_cells[x].function]
263 #define NLENGTH(x) g_news[x].length
265 #define NVALUE(x) g_news[x].value
266 #define NVECTOR(x) g_news[x].vector
269 #define MAKE_CHAR(n) make_cell_ (tmp_num_ (TCHAR), 0, tmp_num2_ (n))
270 #define MAKE_CONTINUATION(n) make_cell_ (tmp_num_ (TCONTINUATION), n, g_stack)
271 #define MAKE_NUMBER(n) make_cell_ (tmp_num_ (TNUMBER), 0, tmp_num2_ (n))
272 #define MAKE_REF(n) make_cell_ (tmp_num_ (TREF), n, 0)
273 #define MAKE_STRING(x) make_cell_ (tmp_num_ (TSTRING), x, 0)
275 #define CAAR(x) CAR (CAR (x))
276 #define CADR(x) CAR (CDR (x))
277 #define CDAR(x) CDR (CAR (x))
278 #define CDDR(x) CDR (CDR (x))
279 #define CADAR(x) CAR (CDR (CAR (x)))
280 #define CADDR(x) CAR (CDR (CDR (x)))
281 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
286 assert (g_free + n < ARENA_SIZE);
302 VALUE (tmp_num2) = x;
307 make_cell_ (SCM type, SCM car, SCM cdr)
310 assert (TYPE (type) == TNUMBER);
311 TYPE (x) = VALUE (type);
312 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
313 if (car) CAR (x) = CAR (car);
314 if (cdr) CDR(x) = CDR(cdr);
316 else if (VALUE (type) == TFUNCTION) {
317 if (car) CAR (x) = car;
318 if (cdr) CDR(x) = CDR(cdr);
328 make_symbol_ (SCM s) ///((internal))
330 VALUE (tmp_num) = TSYMBOL;
331 SCM x = make_cell_ (tmp_num, s, 0);
332 g_symbols = cons (x, g_symbols);
337 list_of_char_equal_p (SCM a, SCM b) ///((internal))
339 while (a != cell_nil && b != cell_nil && VALUE (CAR (a)) == VALUE (CAR (b))) {
340 assert (TYPE (CAR (a)) == TCHAR);
341 assert (TYPE (CAR (b)) == TCHAR);
345 return (a == cell_nil && b == cell_nil) ? cell_t : cell_f;
349 lookup_symbol_ (SCM s)
353 if (list_of_char_equal_p (STRING (CAR (x)), s) == cell_t) break;
357 if (!x) x = make_symbol_ (s);
364 return MAKE_NUMBER (TYPE (x));
370 return (TYPE (x) != TCONTINUATION
371 && (TYPE (CAR (x)) == TPAIR // FIXME: this is weird
372 || TYPE (CAR (x)) == TREF
373 || TYPE (CAR (x)) == TSPECIAL
374 || TYPE (CAR (x)) == TSYMBOL
375 || TYPE (CAR (x)) == TSTRING)) ? CAR (x) : MAKE_NUMBER (CAR (x));
381 return (TYPE (CDR (x)) == TPAIR
382 || TYPE (CDR (x)) == TREF
383 || TYPE (CAR (x)) == TSPECIAL
384 || TYPE (CDR (x)) == TSYMBOL
385 || TYPE (CDR (x)) == TSTRING) ? CDR (x) : MAKE_NUMBER (CDR (x));
391 assert (TYPE (x) == TFUNCTION);
392 return MAKE_NUMBER (FUNCTION (x).arity);
398 VALUE (tmp_num) = TPAIR;
399 return make_cell_ (tmp_num, x, y);
406 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_car));
415 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_cdr));
421 list (SCM x) ///((arity . n))
429 return x == cell_nil ? cell_t : cell_f;
436 || ((TYPE (x) == TKEYWORD && TYPE (y) == TKEYWORD
437 && STRING (x) == STRING (y)))
438 || (TYPE (x) == TCHAR && TYPE (y) == TCHAR
439 && VALUE (x) == VALUE (y))
440 || (TYPE (x) == TNUMBER && TYPE (y) == TNUMBER
441 && VALUE (x) == VALUE (y)))
446 values (SCM x) ///((arity . n))
454 acons (SCM key, SCM value, SCM alist)
456 return cons (cons (key, value), alist);
463 while (x != cell_nil)
466 if (TYPE (x) != TPAIR) return MAKE_NUMBER (-1);
469 return MAKE_NUMBER (n);
472 SCM apply (SCM, SCM, SCM);
475 error (SCM key, SCM x)
479 if ((throw = assq_ref_env (cell_symbol_throw, r0)) != cell_undefined)
480 return apply (throw, cons (key, cons (x, cell_nil)), r0);
482 display_error_ (key);
490 cstring_to_list (char const* s)
495 p = cons (MAKE_CHAR (s[i]), p);
501 assert_defined (SCM x, SCM e) ///((internal))
503 if (e == cell_undefined) return error (cell_symbol_unbound_variable, x);
508 check_formals (SCM f, SCM formals, SCM args) ///((internal))
510 int flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : VALUE (length (formals));
511 int alen = VALUE (length (args));
512 if (alen != flen && alen != -1 && flen != -1)
514 char *s = "apply: wrong number of arguments; expected: ";
521 SCM e = MAKE_STRING (cstring_to_list (s));
522 return error (cell_symbol_wrong_number_of_args, cons (e, f));
524 return cell_unspecified;
528 check_apply (SCM f, SCM e) ///((internal))
531 if (f == cell_f || f == cell_t) type = "bool";
532 if (f == cell_nil) type = "nil";
533 if (f == cell_unspecified) type = "*unspecified*";
534 if (f == cell_undefined) type = "*undefined*";
535 if (TYPE (f) == TCHAR) type = "char";
536 if (TYPE (f) == TNUMBER) type = "number";
537 if (TYPE (f) == TSTRING) type = "string";
541 char *s = "cannot apply: ";
547 SCM e = MAKE_STRING (cstring_to_list (s));
548 return error (cell_symbol_wrong_type_arg, cons (e, f));
550 return cell_unspecified;
554 gc_push_frame () ///((internal))
556 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
557 g_stack = cons (frame, g_stack);
562 append2 (SCM x, SCM y)
564 if (x == cell_nil) return y;
565 assert (TYPE (x) == TPAIR);
566 return cons (car (x), append2 (cdr (x), y));
570 pairlis (SCM x, SCM y, SCM a)
574 if (TYPE (x) != TPAIR)
575 return cons (cons (x, y), a);
576 return cons (cons (car (x), car (y)),
577 pairlis (cdr (x), cdr (y), a));
583 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
584 && x != cell_nil && TYPE (CAR (x)) == TVALUES)
585 x = cons (CADAR (x), CDR (x));
586 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
587 && x != cell_nil && TYPE (CDR (x)) == TPAIR && TYPE (CADR (x)) == TVALUES)
588 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
589 switch (FUNCTION (fn).arity)
591 #if __MESC__ || !_POSIX_SOURCE
592 case 0: return (FUNCTION (fn).function) ();
593 case 1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (CAR (x));
594 case 2: return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x));
595 case 3: return ((SCM(*)(SCM,SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x), CAR (CDDR (x)));
596 case -1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
597 default: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
599 case 0: return FUNCTION (fn).function0 ();
600 case 1: return FUNCTION (fn).function1 (CAR (x));
601 case 2: return FUNCTION (fn).function2 (CAR (x), CADR (x));
602 case 3: return FUNCTION (fn).function3 (CAR (x), CADR (x), CAR (CDDR (x)));
603 case -1: return FUNCTION (fn).functionn (x);
607 return cell_unspecified;
613 //FIXME: move into fast-non eq_p-ing assq core:assq?
614 //while (a != cell_nil && x != CAAR (a)) a = CDR (a);
615 while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f) a = CDR (a);
616 return a != cell_nil ? CAR (a) : cell_f;
620 assq_ref_env (SCM x, SCM a)
623 if (x == cell_f) return cell_undefined;
628 set_car_x (SCM x, SCM e)
630 assert (TYPE (x) == TPAIR);
632 return cell_unspecified;
636 set_cdr_x (SCM x, SCM e)
638 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_set_cdr_x));
640 return cell_unspecified;
644 set_env_x (SCM x, SCM e, SCM a)
646 SCM p = assert_defined (x, assq (x, a));
647 if (TYPE (p) != TPAIR) error (cell_symbol_not_a_pair, cons (p, x));
648 return set_cdr_x (p, e);
652 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
654 SCM cl = cons (cons (cell_closure, x), x);
657 return cell_unspecified;
661 make_closure_ (SCM args, SCM body, SCM a) ///((internal))
663 return make_cell_ (tmp_num_ (TCLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
667 lookup_macro_ (SCM x, SCM a) ///((internal))
669 if (TYPE (x) != TSYMBOL) return cell_f;
670 SCM m = assq_ref_env (x, a);
671 if (TYPE (m) == TMACRO) return MACRO (m);
676 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
685 return cell_unspecified;
689 gc_peek_frame () ///((internal))
691 SCM frame = CAR (g_stack);
694 r3 = CAR (CDDR (frame));
695 r0 = CADR (CDDR (frame));
700 gc_pop_frame () ///((internal))
702 SCM frame = gc_peek_frame (g_stack);
703 g_stack = CDR (g_stack);
714 case cell_vm_evlis: goto evlis;
715 case cell_vm_evlis2: goto evlis2;
716 case cell_vm_evlis3: goto evlis3;
717 case cell_vm_apply: goto apply;
718 case cell_vm_apply2: goto apply2;
719 case cell_vm_eval: goto eval;
721 case cell_vm_eval_car: goto eval_car;
722 case cell_vm_eval_cdr: goto eval_cdr;
723 case cell_vm_eval_cons: goto eval_cons;
724 case cell_vm_eval_null_p: goto eval_null_p;
726 case cell_vm_eval_set_x: goto eval_set_x;
727 case cell_vm_eval_macro: goto eval_macro;
728 case cell_vm_eval_check_func: goto eval_check_func;
729 case cell_vm_eval2: goto eval2;
730 case cell_vm_macro_expand: goto macro_expand;
731 case cell_vm_begin: goto begin;
732 case cell_vm_begin_read_input_file: goto begin_read_input_file;
733 case cell_vm_begin2: goto begin2;
734 case cell_vm_if: goto vm_if;
735 case cell_vm_if_expr: goto if_expr;
736 case cell_vm_call_with_current_continuation2: goto call_with_current_continuation2;
737 case cell_vm_call_with_values2: goto call_with_values2;
738 case cell_vm_return: goto vm_return;
739 case cell_unspecified: return r1;
747 if (r1 == cell_nil) goto vm_return;
748 if (TYPE (r1) != TPAIR) goto eval;
749 push_cc (CAR (r1), r1, r0, cell_vm_evlis2);
752 push_cc (CDR (r2), r1, r0, cell_vm_evlis3);
760 switch (TYPE (CAR (r1)))
763 check_formals (CAR (r1), MAKE_NUMBER (FUNCTION (CAR (r1)).arity), CDR (r1));
764 r1 = call (CAR (r1), CDR (r1)); /// FIXME: move into eval_apply
769 SCM cl = CLOSURE (CAR (r1));
770 SCM formals = CADR (cl);
771 SCM body = CDDR (cl);
774 check_formals (CAR (r1), formals, CDR (r1));
775 SCM p = pairlis (formals, CDR (r1), aa);
776 call_lambda (body, p, aa, r0);
782 g_stack = CONTINUATION (CAR (r1));
793 push_cc (cons (CADR (r1), CADDR (r1)), r1, r0, cell_vm_return);
798 push_cc (CADR (r1), r1, CADDR (r1), cell_vm_return);
801 case cell_call_with_current_continuation:
804 goto call_with_current_continuation;
806 default: check_apply (cell_f, CAR (r1));
811 if (CAR (r1) == cell_symbol_call_with_values)
814 goto call_with_values;
816 if (CAR (r1) == cell_symbol_current_module)
827 case cell_symbol_lambda:
829 SCM formals = CADR (CAR (r1));
830 SCM body = CDDR (CAR (r1));
831 SCM p = pairlis (formals, CDR (r1), r0);
832 check_formals (r1, formals, CDR (r1));
833 call_lambda (body, p, p, r0);
839 push_cc (CAR (r1), r1, r0, cell_vm_apply2);
842 check_apply (r1, CAR (r2));
843 r1 = cons (r1, CDR (r2));
855 case cell_symbol_car:
857 push_cc (CADR (r1), r1, r0, cell_vm_eval_car); goto eval;
859 x = r1; gc_pop_frame (); r1 = CAR (x); goto eval_apply;
861 case cell_symbol_cdr:
863 push_cc (CADR (r1), r1, r0, cell_vm_eval_cdr); goto eval;
865 x = r1; gc_pop_frame (); r1 = CDR (x); goto eval_apply;
867 case cell_symbol_cons: {
868 push_cc (CDR (r1), r1, r0, cell_vm_eval_cons); goto evlis;
872 r1 = cons (CAR (x), CADR (x));
875 case cell_symbol_null_p:
877 push_cc (CADR (r1), r1, r0, cell_vm_eval_null_p);
880 x = r1; gc_pop_frame (); r1 = null_p (x); goto eval_apply;
882 #endif // FIXED_PRIMITIVES
883 case cell_symbol_quote:
885 x = r1; gc_pop_frame (); r1 = CADR (x); goto eval_apply;
887 case cell_symbol_begin: goto begin;
888 case cell_symbol_lambda:
890 r1 = make_closure_ (CADR (r1), CDDR (r1), assq (cell_closure, r0));
893 case cell_symbol_if: {r1=CDR (r1); goto vm_if;}
894 case cell_symbol_set_x:
896 push_cc (CAR (CDDR (r1)), r1, r0, cell_vm_eval_set_x);
900 r1 = set_env_x (CADR (x), r1, r0);
903 case cell_vm_macro_expand:
905 push_cc (CADR (r1), r1, r0, cell_vm_return);
909 push_cc (r1, r1, r0, cell_vm_eval_macro);
914 if (TYPE (r1) == TPAIR)
916 set_cdr_x (r2, CDR (r1));
917 set_car_x (r2, CAR (r1));
921 push_cc (CAR (r1), r1, r0, cell_vm_eval_check_func); goto eval;
923 push_cc (CDR (r2), r2, r0, cell_vm_eval2); goto evlis;
925 r1 = cons (CAR (r2), r1);
932 r1 = assert_defined (r1, assq_ref_env (r1, r0));
935 default: goto vm_return;
941 if (TYPE (r1) == TPAIR
942 && (macro = lookup_macro_ (CAR (r1), r0)) != cell_f)
944 r1 = cons (macro, CDR (r1));
947 else if (TYPE (r1) == TPAIR
948 && TYPE (CAR (r1)) == TSYMBOL
949 && ((expanders = assq_ref_env (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
950 && ((macro = assq (CAR (r1), expanders)) != cell_f))
952 SCM sc_expand = assq_ref_env (cell_symbol_macro_expand, r0);
953 if (sc_expand != cell_undefined && sc_expand != cell_f)
955 r1 = cons (sc_expand, cons (r1, cell_nil));
962 x = cell_unspecified;
963 while (r1 != cell_nil) {
965 if (TYPE (r1) == TPAIR && TYPE (CAR (r1)) == TPAIR)
967 if (CAAR (r1) == cell_symbol_begin)
968 r1 = append2 (CDAR (r1), CDR (r1));
969 else if (CAAR (r1) == cell_symbol_primitive_load)
971 push_cc (cons (cell_symbol_read_input_file, cell_nil), r1, r0, cell_vm_begin_read_input_file);
973 begin_read_input_file:
974 r1 = append2 (r1, CDR (r2));
977 if (CDR (r1) == cell_nil)
982 push_cc (CAR (r1), r1, r0, cell_vm_begin2);
992 push_cc (CAR (r1), r1, r0, cell_vm_if_expr);
1002 if (CDDR (r1) != cell_nil)
1004 r1 = CAR (CDDR (r1));
1007 r1 = cell_unspecified;
1010 call_with_current_continuation:
1012 x = MAKE_CONTINUATION (g_continuations++);
1014 push_cc (cons (CAR (r1), cons (x, cell_nil)), x, r0, cell_vm_call_with_current_continuation2);
1016 call_with_current_continuation2:
1017 CONTINUATION (r2) = g_stack;
1021 push_cc (cons (CAR (r1), cell_nil), r1, r0, cell_vm_call_with_values2);
1024 if (TYPE (r1) == TVALUES)
1026 r1 = cons (CADR (r2), r1);
1037 apply (SCM f, SCM x, SCM a) ///((internal))
1039 push_cc (cons (f, x), cell_unspecified, r0, cell_unspecified);
1041 return eval_apply ();
1045 mes_g_stack (SCM a) ///((internal))
1051 g_stack = cons (cell_nil, cell_nil);
1055 //
\f Environment setup
1058 make_tmps (struct scm* cells)
1061 cells[tmp].type = TCHAR;
1063 cells[tmp_num].type = TNUMBER;
1064 tmp_num2 = g_free++;
1065 cells[tmp_num2].type = TNUMBER;
1077 gc_init_cells () ///((internal))
1079 g_cells = (struct scm *)malloc (2*ARENA_SIZE*sizeof (struct scm));
1085 g_cells += sizeof (struct scm);
1095 gc_init_news () ///((internal))
1099 p -= sizeof (struct scm);
1100 p += ARENA_SIZE * sizeof (struct scm);
1103 g_news = g_cells-1 + ARENA_SIZE;
1106 NTYPE (0) = TVECTOR;
1110 g_news += sizeof (struct scm);
1120 mes_symbols () ///((internal))
1126 #include "mes.mes.symbols.i"
1128 #include "mes.symbols.i"
1131 g_symbol_max = g_free;
1132 make_tmps (g_cells);
1135 for (int i=1; i<g_symbol_max; i++)
1136 g_symbols = cons (i, g_symbols);
1141 #include "mes.mes.symbol-names.i"
1143 #include "mes.symbol-names.i"
1146 a = acons (cell_symbol_mes_version, MAKE_STRING (cstring_to_list (VERSION)), a);
1147 a = acons (cell_symbol_mes_prefix, MAKE_STRING (cstring_to_list (PREFIX)), a);
1149 a = acons (cell_symbol_dot, cell_dot, a);
1150 a = acons (cell_symbol_begin, cell_begin, a);
1151 a = acons (cell_symbol_call_with_values, cell_symbol_call_with_values, a);
1152 a = acons (cell_symbol_current_module, cell_symbol_current_module, a);
1153 a = acons (cell_symbol_call_with_current_continuation, cell_call_with_current_continuation, a);
1154 a = acons (cell_symbol_sc_expand, cell_f, a);
1157 a = acons (cell_symbol_gnuc, cell_t, a);
1158 a = acons (cell_symbol_mesc, cell_f, a);
1160 a = acons (cell_symbol_gnuc, cell_f, a);
1161 a = acons (cell_symbol_mesc, cell_t, a);
1164 a = acons (cell_closure, a, a);
1170 mes_environment () ///((internal))
1172 SCM a = mes_symbols ();
1173 return mes_g_stack (a);
1177 mes_builtins (SCM a) ///((internal))
1179 #if !__GNUC__ || !_POSIX_SOURCE
1180 #include "mes.mes.i"
1182 // Do not sort: Order of these includes define builtins
1183 #include "posix.mes.i"
1184 #include "math.mes.i"
1185 #include "lib.mes.i"
1186 #include "vector.mes.i"
1188 #include "reader.mes.i"
1190 #include "gc.mes.environment.i"
1191 #include "lib.mes.environment.i"
1192 #include "math.mes.environment.i"
1193 #include "mes.mes.environment.i"
1194 #include "posix.mes.environment.i"
1195 #include "reader.mes.environment.i"
1196 #include "vector.mes.environment.i"
1200 // Do not sort: Order of these includes define builtins
1208 #include "gc.environment.i"
1209 #include "lib.environment.i"
1210 #include "math.environment.i"
1211 #include "mes.environment.i"
1212 #include "posix.environment.i"
1213 #include "reader.environment.i"
1214 #include "vector.environment.i"
1219 fputs ("functions: ", STDERR);
1220 fputs (itoa (g_function), STDERR);
1221 fputs ("\n", STDERR);
1222 for (int i = 0; i < g_function; i++)
1224 fputs ("[", STDERR);
1225 fputs (itoa (i), STDERR);
1226 fputs ("]: ", STDERR);
1227 fputs (g_functions[i].name, STDERR);
1228 fputs ("\n", STDERR);
1230 fputs ("\n", STDERR);
1236 SCM read_input_file_env (SCM);
1239 load_env (SCM a) ///((internal))
1242 g_stdin = open ("module/mes/read-0.mes", O_RDONLY);
1243 char *read0 = MODULEDIR "mes/read-0.mes";
1244 g_stdin = g_stdin >= 0 ? g_stdin : open (read0, O_RDONLY);
1245 if (!g_function) r0 = mes_builtins (r0);
1246 r2 = read_input_file_env (r0);
1252 bload_env (SCM a) ///((internal))
1255 char *mo = "mes/read-0-32.mo";
1256 g_stdin = open ("module/mes/read-0-32.mo", O_RDONLY);
1257 char *read0 = MODULEDIR "mes/read-0-32.mo";
1258 g_stdin = g_stdin >= 0 ? g_stdin : open (read0, O_RDONLY);
1260 char *mo ="mes/read-0.mo";
1261 g_stdin = open ("module/mes/read-0.mo", O_RDONLY);
1262 g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "mes/read-0.mo", O_RDONLY);
1265 if (g_stdin < 0) {eputs ("no such file: ");eputs (mo);eputs ("\n");return 1;}
1266 assert (getchar () == 'M');
1267 assert (getchar () == 'E');
1268 assert (getchar () == 'S');
1270 if (g_debug) eputs ("*GOT MES*\n");
1271 g_stack = getchar () << 8;
1272 g_stack += getchar ();
1274 char *p = (char*)g_cells;
1281 g_free = (p-(char*)g_cells) / sizeof (struct scm);
1285 r0 = mes_builtins (r0);
1288 set_env_x (cell_symbol_gnuc, cell_t, r0);
1289 set_env_x (cell_symbol_mesc, cell_f, r0);
1291 set_env_x (cell_symbol_gnuc, cell_f, r0);
1292 set_env_x (cell_symbol_mesc, cell_t, r0);
1297 eputs ("symbols: ");
1299 while (s && s != cell_nil) {
1300 display_error_ (CAR (s));
1305 eputs ("functions: ");
1306 eputs (itoa (g_function));
1308 for (int i = 0; i < g_function; i++)
1313 eputs (g_functions[i].name);
1316 //display_error_ (r0);
1327 main (int argc, char *argv[])
1330 if (p = getenv ("MES_DEBUG")) g_debug = atoi (p);
1331 if (g_debug) {eputs (";;; MODULEDIR=");eputs (MODULEDIR);eputs ("\n");}
1332 if (p = getenv ("MES_MAX_ARENA")) MAX_ARENA_SIZE = atoi (p);
1333 if (p = getenv ("MES_ARENA")) ARENA_SIZE = atoi (p);
1334 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes [--dump|--load] < FILE\n");
1335 if (argc > 1 && !strcmp (argv[1], "--version")) {puts ("Mes ");puts (VERSION);puts ("\n");return 0;};
1338 r0 = mes_environment ();
1340 SCM program = (argc > 1 && !strcmp (argv[1], "--load"))
1341 ? bload_env (r0) : load_env (r0);
1342 g_tiny = argc > 2 && !strcmp (argv[2], "--tiny");
1343 if (argc > 1 && !strcmp (argv[1], "--dump")) return dump ();
1346 for (int i=argc-1; i>=0; i--) lst = cons (MAKE_STRING (cstring_to_list (argv[i])), lst);
1347 r0 = acons (cell_symbol_argv, lst, r0);
1348 push_cc (r2, cell_unspecified, r0, cell_unspecified);
1351 eputs ("program: ");
1352 display_error_ (r1);
1357 display_error_ (r1);
1362 eputs ("\nstats: [");
1363 eputs (itoa (g_free));