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/>.
42 #define FIXED_PRIMITIVES 1
44 int ARENA_SIZE = 100000;
45 int MAX_ARENA_SIZE = 20000000;
46 //int GC_SAFETY_DIV = 400;
47 //int GC_SAFETY = ARENA_SIZE / 400;
51 enum type_t {TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TREF, TSPECIAL, TSTRING, TSYMBOL, TVALUES, TVECTOR, TBROKEN_HEART};
52 typedef SCM (*function0_t) (void);
53 typedef SCM (*function1_t) (SCM);
54 typedef SCM (*function2_t) (SCM, SCM);
55 typedef SCM (*function3_t) (SCM, SCM, SCM);
56 typedef SCM (*functionn_t) (SCM);
59 function0_t function0;
60 function1_t function1;
61 function2_t function2;
62 function3_t function3;
63 functionn_t functionn;
89 struct scm scm_nil = {TSPECIAL, "()",0};
90 struct scm scm_f = {TSPECIAL, "#f",0};
91 struct scm scm_t = {TSPECIAL, "#t",0};
92 struct scm scm_dot = {TSPECIAL, ".",0};
93 struct scm scm_arrow = {TSPECIAL, "=>",0};
94 struct scm scm_undefined = {TSPECIAL, "*undefined*",0};
95 struct scm scm_unspecified = {TSPECIAL, "*unspecified*",0};
96 struct scm scm_closure = {TSPECIAL, "*closure*",0};
97 struct scm scm_circular = {TSPECIAL, "*circular*",0};
98 struct scm scm_begin = {TSPECIAL, "*begin*",0};
100 struct scm scm_symbol_dot = {TSYMBOL, "*dot*",0};
101 struct scm scm_symbol_lambda = {TSYMBOL, "lambda",0};
102 struct scm scm_symbol_begin = {TSYMBOL, "begin",0};
103 struct scm scm_symbol_if = {TSYMBOL, "if",0};
104 struct scm scm_symbol_quote = {TSYMBOL, "quote",0};
105 struct scm scm_symbol_set_x = {TSYMBOL, "set!",0};
107 struct scm scm_symbol_sc_expand = {TSYMBOL, "sc-expand",0};
108 struct scm scm_symbol_macro_expand = {TSYMBOL, "macro-expand",0};
109 struct scm scm_symbol_sc_expander_alist = {TSYMBOL, "*sc-expander-alist*",0};
111 struct scm scm_symbol_call_with_values = {TSYMBOL, "call-with-values",0};
112 struct scm scm_call_with_current_continuation = {TSPECIAL, "*call/cc*",0};
113 struct scm scm_symbol_call_with_current_continuation = {TSYMBOL, "call-with-current-continuation",0};
114 struct scm scm_symbol_current_module = {TSYMBOL, "current-module",0};
115 struct scm scm_symbol_primitive_load = {TSYMBOL, "primitive-load",0};
116 struct scm scm_symbol_read_input_file = {TSYMBOL, "read-input-file",0};
117 struct scm scm_symbol_write = {TSYMBOL, "write",0};
118 struct scm scm_symbol_display = {TSYMBOL, "display",0};
120 struct scm scm_symbol_throw = {TSYMBOL, "throw",0};
121 struct scm scm_symbol_not_a_pair = {TSYMBOL, "not-a-pair",0};
122 struct scm scm_symbol_system_error = {TSYMBOL, "system-error",0};
123 struct scm scm_symbol_wrong_number_of_args = {TSYMBOL, "wrong-number-of-args",0};
124 struct scm scm_symbol_wrong_type_arg = {TSYMBOL, "wrong-type-arg",0};
125 struct scm scm_symbol_unbound_variable = {TSYMBOL, "unbound-variable",0};
127 struct scm scm_symbol_argv = {TSYMBOL, "%argv",0};
128 struct scm scm_symbol_mes_prefix = {TSYMBOL, "%prefix",0};
129 struct scm scm_symbol_mes_version = {TSYMBOL, "%version",0};
131 struct scm scm_symbol_car = {TSYMBOL, "car",0};
132 struct scm scm_symbol_cdr = {TSYMBOL, "cdr",0};
133 struct scm scm_symbol_null_p = {TSYMBOL, "null?",0};
134 struct scm scm_symbol_eq_p = {TSYMBOL, "eq?",0};
135 struct scm scm_symbol_cons = {TSYMBOL, "cons",0};
137 struct scm scm_vm_evlis = {TSPECIAL, "*vm-evlis*",0};
138 struct scm scm_vm_evlis2 = {TSPECIAL, "*vm-evlis2*",0};
139 struct scm scm_vm_evlis3 = {TSPECIAL, "*vm-evlis3*",0};
140 struct scm scm_vm_apply = {TSPECIAL, "core:apply",0};
141 struct scm scm_vm_apply2 = {TSPECIAL, "*vm-apply2*",0};
142 struct scm scm_vm_eval = {TSPECIAL, "core:eval",0};
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 = {TSPECIAL, "*vm-eval-macro*",0};
152 struct scm scm_vm_eval_check_func = {TSPECIAL, "*vm-eval-check-func*",0};
153 struct scm scm_vm_eval2 = {TSPECIAL, "*vm-eval2*",0};
154 struct scm scm_vm_macro_expand = {TSPECIAL, "core:macro-expand",0};
155 struct scm scm_vm_begin = {TSPECIAL, "*vm-begin*",0};
156 struct scm scm_vm_begin_read_input_file = {TSPECIAL, "*vm-begin-read-input-file*",0};
157 struct scm scm_vm_begin2 = {TSPECIAL, "*vm-begin2*",0};
158 struct scm scm_vm_if = {TSPECIAL, "*vm-if*",0};
159 struct scm scm_vm_if_expr = {TSPECIAL, "*vm-if-expr*",0};
160 struct scm scm_vm_call_with_values2 = {TSPECIAL, "*vm-call-with-values2*",0};
161 struct scm scm_vm_call_with_current_continuation2 = {TSPECIAL, "*vm-call-with-current-continuation2*",0};
162 struct scm scm_vm_return = {TSPECIAL, "*vm-return*",0};
164 struct scm scm_symbol_gnuc = {TSYMBOL, "%gnuc",0};
165 struct scm scm_symbol_mesc = {TSYMBOL, "%mesc",0};
167 struct scm scm_test = {TSYMBOL, "test",0};
172 struct scm *g_news = 0;
174 bool g_debug = false;
176 #include "mes.symbols.h"
182 struct function g_functions[200];
185 SCM g_continuations = 0;
189 SCM r1 = 0; // param 1
190 SCM r2 = 0; // save 2+load/dump
191 SCM r3 = 0; // continuation
201 #define CAR(x) g_cells[x].car
202 #define CDR(x) g_cells[x].cdr
203 #define CONTINUATION(x) g_cells[x].cdr
204 #define HITS(x) g_cells[x].hits
205 #define LENGTH(x) g_cells[x].length
206 #define NAME(x) g_cells[x].name
207 #define STRING(x) g_cells[x].string
208 #define TYPE(x) g_cells[x].type
209 #define CLOSURE(x) g_cells[x].closure
210 #define MACRO(x) g_cells[x].macro
211 #define REF(x) g_cells[x].ref
212 #define VALUE(x) g_cells[x].value
213 #define VECTOR(x) g_cells[x].vector
214 #define FUNCTION(x) g_functions[g_cells[x].function]
216 #define NTYPE(x) g_news[x].type
218 #define NCAR(x) g_news[x].car
219 #define NLENGTH(x) g_news[x].length
221 #define NCDR(x) g_news[x].cdr
222 #define NVALUE(x) g_news[x].value
223 #define NVECTOR(x) g_news[x].vector
226 #define CAAR(x) CAR (CAR (x))
227 #define CADR(x) CAR (CDR (x))
228 #define CDAR(x) CDR (CAR (x))
229 #define CDDR(x) CDR (CDR (x))
230 #define CADAR(x) CAR (CDR (CAR (x)))
231 #define CADDR(x) CAR (CDR (CDR (x)))
232 #define CDDDR(x) CDR (CDR (CDR (x)))
233 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
235 #define MAKE_CHAR(n) make_cell_ (tmp_num_ (TCHAR), 0, tmp_num2_ (n))
236 #define MAKE_CONTINUATION(n) make_cell_ (tmp_num_ (TCONTINUATION), n, g_stack)
237 #define MAKE_NUMBER(n) make_cell_ (tmp_num_ (TNUMBER), 0, tmp_num2_ (n))
238 #define MAKE_REF(n) make_cell_ (tmp_num_ (TREF), n, 0)
239 #define MAKE_STRING(x) make_cell_ (tmp_num_ (TSTRING), x, 0)
241 SCM vm_call (function0_t f, SCM p1, SCM a);
242 char const* itoa(int);
244 #define eputs(s) fputs(s, stderr)
249 g_cells[tmp_num].value = x;
256 g_cells[tmp_num2].value = x;
263 assert (g_free + n < ARENA_SIZE);
270 make_cell_ (SCM type, SCM car, SCM cdr)
273 assert (TYPE (type) == TNUMBER);
274 TYPE (x) = VALUE (type);
275 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
276 if (car) CAR (x) = CAR (car);
277 if (cdr) CDR (x) = CDR (cdr);
278 } else if (VALUE (type) == TFUNCTION) {
279 if (car) CAR (x) = car;
280 if (cdr) CDR (x) = CDR (cdr);
291 g_cells[tmp_num].value = TSYMBOL;
292 SCM x = make_cell_ (tmp_num, s, 0);
293 g_symbols = cons (x, g_symbols);
298 list_of_char_equal_p (SCM a, SCM b) ///((internal))
300 while (a != cell_nil && b != cell_nil && VALUE (car (a)) == VALUE (car (b))) {
301 assert (TYPE (car (a)) == TCHAR);
302 assert (TYPE (car (b)) == TCHAR);
306 return (a == cell_nil && b == cell_nil) ? cell_t : cell_f;
310 lookup_symbol_ (SCM s)
314 if (list_of_char_equal_p (STRING (car (x)), s) == cell_t) break;
318 if (!x) x = make_symbol_ (s);
325 return MAKE_NUMBER (TYPE (x));
331 return (TYPE (x) != TCONTINUATION
332 && (TYPE (CAR (x)) == TPAIR // FIXME: this is weird
333 || TYPE (CAR (x)) == TREF
334 || TYPE (CAR (x)) == TSPECIAL
335 || TYPE (CAR (x)) == TSYMBOL
336 || TYPE (CAR (x)) == TSTRING)) ? CAR (x) : MAKE_NUMBER (CAR (x));
342 return (TYPE (CDR (x)) == TPAIR
343 || TYPE (CDR (x)) == TREF
344 || TYPE (CAR (x)) == TSPECIAL
345 || TYPE (CDR (x)) == TSYMBOL
346 || TYPE (CDR (x)) == TSTRING) ? CDR (x) : MAKE_NUMBER (CDR (x));
352 assert (TYPE (x) == TFUNCTION);
353 return MAKE_NUMBER (FUNCTION (x).arity);
359 g_cells[tmp_num].value = TPAIR;
360 return make_cell_ (tmp_num, x, y);
366 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_car));
373 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_cdr));
378 list (SCM x) ///((arity . n))
386 return x == cell_nil ? cell_t : cell_f;
393 || ((TYPE (x) == TKEYWORD && TYPE (y) == TKEYWORD
394 && STRING (x) == STRING (y)))
395 || (TYPE (x) == TCHAR && TYPE (y) == TCHAR
396 && VALUE (x) == VALUE (y))
397 || (TYPE (x) == TNUMBER && TYPE (y) == TNUMBER
398 && VALUE (x) == VALUE (y)))
403 values (SCM x) ///((arity . n))
411 acons (SCM key, SCM value, SCM alist)
413 return cons (cons (key, value), alist);
421 while (x != cell_nil)
424 if (TYPE (x) != TPAIR) return MAKE_NUMBER (-1);
427 return MAKE_NUMBER (n);
430 SCM apply (SCM, SCM, SCM);
433 error (SCM key, SCM x)
436 if ((throw = assq_ref_env (cell_symbol_throw, r0)) != cell_undefined)
437 return apply (throw, cons (key, cons (x, cell_nil)), r0);
438 display_error_ (key);
446 cstring_to_list (char const* s)
451 p = cons (MAKE_CHAR (s[i]), p);
456 append2 (SCM x, SCM y)
458 if (x == cell_nil) return y;
459 assert (TYPE (x) == TPAIR);
460 return cons (car (x), append2 (cdr (x), y));
464 pairlis (SCM x, SCM y, SCM a)
468 if (TYPE (x) != TPAIR)
469 return cons (cons (x, y), a);
470 return cons (cons (car (x), car (y)),
471 pairlis (cdr (x), cdr (y), a));
477 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
478 && x != cell_nil && TYPE (CAR (x)) == TVALUES)
479 x = cons (CADAR (x), CDR (x));
480 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
481 && x != cell_nil && TYPE (CDR (x)) == TPAIR && TYPE (CADR (x)) == TVALUES)
482 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
483 switch (FUNCTION (fn).arity)
485 case 0: return FUNCTION (fn).function0 ();
486 case 1: return FUNCTION (fn).function1 (car (x));
487 case 2: return FUNCTION (fn).function2 (car (x), CADR (x));
488 case 3: return FUNCTION (fn).function3 (car (x), CADR (x), car (CDDR (x)));
489 case -1: return FUNCTION (fn).functionn (x);
492 return cell_unspecified;
498 while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f) a = CDR (a);
499 return a != cell_nil ? CAR (a) : cell_f;
503 assq_ref_env (SCM x, SCM a)
506 if (x == cell_f) return cell_undefined;
511 set_car_x (SCM x, SCM e)
513 assert (TYPE (x) == TPAIR);
515 return cell_unspecified;
519 set_cdr_x (SCM x, SCM e)
521 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_set_cdr_x));
523 return cell_unspecified;
526 SCM assert_defined (SCM, SCM);
529 set_env_x (SCM x, SCM e, SCM a)
531 SCM p = assert_defined (x, assq (x, a));
532 if (TYPE (p) != TPAIR) error (cell_symbol_not_a_pair, cons (p, x));
533 return set_cdr_x (p, e);
537 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
539 SCM cl = cons (cons (cell_closure, x), x);
542 return cell_unspecified;
546 make_closure_ (SCM args, SCM body, SCM a) ///((internal))xs
548 return make_cell_ (tmp_num_ (TCLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
552 lookup_macro_ (SCM x, SCM a) ///((internal))
554 if (TYPE (x) != TSYMBOL) return cell_f;
555 SCM m = assq_ref_env (x, a);
556 if (TYPE (m) == TMACRO) return MACRO (m);
560 SCM check_apply (SCM, SCM);
561 SCM check_formals (SCM, SCM, SCM);
562 SCM push_cc (SCM, SCM, SCM, SCM);
564 SCM gc_push_frame ();
573 case cell_vm_evlis: goto evlis;
574 case cell_vm_evlis2: goto evlis2;
575 case cell_vm_evlis3: goto evlis3;
576 case cell_vm_apply: goto apply;
577 case cell_vm_apply2: goto apply2;
578 case cell_vm_eval: goto eval;
580 case cell_vm_eval_car: goto eval_car;
581 case cell_vm_eval_cdr: goto eval_cdr;
582 case cell_vm_eval_cons: goto eval_cons;
583 case cell_vm_eval_null_p: goto eval_null_p;
585 case cell_vm_eval_set_x: goto eval_set_x;
586 case cell_vm_eval_macro: goto eval_macro;
587 case cell_vm_eval_check_func: goto eval_check_func;
588 case cell_vm_eval2: goto eval2;
589 case cell_vm_macro_expand: goto macro_expand;
590 case cell_vm_begin: goto begin;
591 case cell_vm_begin_read_input_file: goto begin_read_input_file;
592 case cell_vm_begin2: goto begin2;
593 case cell_vm_if: goto vm_if;
594 case cell_vm_if_expr: goto if_expr;
595 case cell_vm_call_with_current_continuation2: goto call_with_current_continuation2;
596 case cell_vm_call_with_values2: goto call_with_values2;
597 case cell_vm_return: goto vm_return;
598 case cell_unspecified: return r1;
607 if (r1 == cell_nil) goto vm_return;
608 if (TYPE (r1) != TPAIR) goto eval;
609 push_cc (car (r1), r1, r0, cell_vm_evlis2);
612 push_cc (cdr (r2), r1, r0, cell_vm_evlis3);
620 switch (TYPE (car (r1)))
623 check_formals (car (r1), MAKE_NUMBER (FUNCTION (car (r1)).arity), cdr (r1));
624 r1 = call (car (r1), cdr (r1)); /// FIXME: move into eval_apply
629 SCM cl = CLOSURE (car (r1));
630 SCM formals = CADR (cl);
631 SCM body = CDDR (cl);
634 check_formals (car (r1), formals, cdr (r1));
635 SCM p = pairlis (formals, cdr (r1), aa);
636 call_lambda (body, p, aa, r0);
642 g_stack = CONTINUATION (CAR (r1));
653 push_cc (cons (CADR (r1), CADDR (r1)), r1, r0, cell_vm_return);
658 push_cc (CADR (r1), r1, CADDR (r1), cell_vm_return);
661 case cell_call_with_current_continuation:
664 goto call_with_current_continuation;
666 default: check_apply (cell_f, car (r1));
671 if (car (r1) == cell_symbol_call_with_values)
674 goto call_with_values;
676 if (car (r1) == cell_symbol_current_module)
687 case cell_symbol_lambda:
689 SCM formals = CADR (car (r1));
690 SCM body = CDDR (car (r1));
691 SCM p = pairlis (formals, cdr (r1), r0);
692 check_formals (r1, formals, cdr (r1));
693 call_lambda (body, p, p, r0);
699 push_cc (car (r1), r1, r0, cell_vm_apply2);
702 check_apply (r1, car (r2));
703 r1 = cons (r1, cdr (r2));
715 case cell_symbol_car:
717 push_cc (CADR (r1), r1, r0, cell_vm_eval_car); goto eval;
719 x = r1; gc_pop_frame (); r1 = car (x); goto eval_apply;
721 case cell_symbol_cdr:
723 push_cc (CADR (r1), r1, r0, cell_vm_eval_cdr); goto eval;
725 x = r1; gc_pop_frame (); r1 = cdr (x); goto eval_apply;
727 case cell_symbol_cons: {
728 push_cc (CDR (r1), r1, r0, cell_vm_eval_cons); goto evlis;
732 r1 = cons (CAR (x), CADR (x));
735 case cell_symbol_null_p:
737 push_cc (CADR (r1), r1, r0, cell_vm_eval_null_p);
740 x = r1; gc_pop_frame (); r1 = null_p (x); goto eval_apply;
742 #endif // FIXED_PRIMITIVES
743 case cell_symbol_quote:
745 x = r1; gc_pop_frame (); r1 = CADR (x); goto eval_apply;
747 case cell_symbol_begin: goto begin;
748 case cell_symbol_lambda:
750 r1 = make_closure_ (CADR (r1), CDDR (r1), assq (cell_closure, r0));
753 case cell_symbol_if: {r1=cdr (r1); goto vm_if;}
754 case cell_symbol_set_x:
756 push_cc (car (CDDR (r1)), r1, r0, cell_vm_eval_set_x);
760 r1 = set_env_x (CADR (x), r1, r0);
763 case cell_vm_macro_expand:
765 push_cc (CADR (r1), r1, r0, cell_vm_return);
769 push_cc (r1, r1, r0, cell_vm_eval_macro);
775 if (TYPE (r1) == TPAIR)
777 set_cdr_x (r2, cdr (r1));
778 set_car_x (r2, car (r1));
782 push_cc (car (r1), r1, r0, cell_vm_eval_check_func); goto eval;
784 push_cc (CDR (r2), r2, r0, cell_vm_eval2); goto evlis;
786 r1 = cons (car (r2), r1);
793 r1 = assert_defined (r1, assq_ref_env (r1, r0));
796 default: goto vm_return;
802 if (TYPE (r1) == TPAIR
803 && (macro = lookup_macro_ (car (r1), r0)) != cell_f)
805 r1 = cons (macro, CDR (r1));
808 else if (TYPE (r1) == TPAIR
809 && TYPE (CAR (r1)) == TSYMBOL
810 && ((expanders = assq_ref_env (cell_symbol_sc_expander_alist, r0)) != cell_undefined)
811 && ((macro = assq (CAR (r1), expanders)) != cell_f))
813 SCM sc_expand = assq_ref_env (cell_symbol_macro_expand, r0);
814 if (sc_expand != cell_undefined && sc_expand != cell_f)
816 r1 = cons (sc_expand, cons (r1, cell_nil));
823 x = cell_unspecified;
824 while (r1 != cell_nil) {
826 if (TYPE (r1) == TPAIR && TYPE (CAR (r1)) == TPAIR)
828 if (CAAR (r1) == cell_symbol_begin)
829 r1 = append2 (CDAR (r1), cdr (r1));
830 else if (CAAR (r1) == cell_symbol_primitive_load)
832 push_cc (cons (cell_symbol_read_input_file, cell_nil), r1, r0, cell_vm_begin_read_input_file);
834 begin_read_input_file:
835 r1 = append2 (r1, cdr (r2));
838 if (CDR (r1) == cell_nil)
843 push_cc (CAR (r1), r1, r0, cell_vm_begin2);
853 push_cc (car (r1), r1, r0, cell_vm_if_expr);
863 if (CDDR (r1) != cell_nil)
865 r1 = car (CDDR (r1));
868 r1 = cell_unspecified;
871 call_with_current_continuation:
873 x = MAKE_CONTINUATION (g_continuations++);
875 push_cc (cons (car (r1), cons (x, cell_nil)), x, r0, cell_vm_call_with_current_continuation2);
877 call_with_current_continuation2:
878 CONTINUATION (r2) = g_stack;
882 push_cc (cons (car (r1), cell_nil), r1, r0, cell_vm_call_with_values2);
885 if (TYPE (r1) == TVALUES)
887 r1 = cons (CADR (r2), r1);
898 gc_peek_frame () ///((internal))
900 SCM frame = car (g_stack);
903 r3 = car (CDDR (frame));
904 r0 = CADR (CDDR (frame));
909 gc_pop_frame () ///((internal))
911 SCM frame = gc_peek_frame (g_stack);
912 g_stack = cdr (g_stack);
917 gc_push_frame () ///((internal))
919 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
920 return g_stack = cons (frame, g_stack);
924 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
933 return cell_unspecified;
937 apply (SCM f, SCM x, SCM a) ///((internal))
939 push_cc (cons (f, x), cell_unspecified, r0, cell_unspecified);
941 return eval_apply ();
945 make_tmps (struct scm* cells)
948 cells[tmp].type = TCHAR;
950 cells[tmp_num].type = TNUMBER;
952 cells[tmp_num2].type = TNUMBER;
955 //
\f Environment setup
957 gc_init_cells () ///((internal))
959 g_cells = (struct scm *)malloc (2*ARENA_SIZE*sizeof(struct scm));
960 g_cells[0].type = TVECTOR;
961 g_cells[0].length = 1000;
962 g_cells[0].vector = 0;
964 g_cells[0].type = TCHAR;
965 g_cells[0].value = 'c';
969 gc_init_news () ///((internal))
971 g_news = g_cells-1 + ARENA_SIZE;
972 g_news[0].type = TVECTOR;
973 g_news[0].length = 1000;
974 g_news[0].vector = 0;
976 g_news[0].type = TCHAR;
977 g_news[0].value = 'n';
981 mes_symbols () ///((internal))
986 #include "mes.symbols.i"
988 g_symbol_max = g_free;
992 for (int i=1; i<g_symbol_max; i++)
993 g_symbols = cons (i, g_symbols);
997 #include "mes.symbol-names.i"
999 a = acons (cell_symbol_mes_version, MAKE_STRING (cstring_to_list (VERSION)), a);
1000 a = acons (cell_symbol_mes_prefix, MAKE_STRING (cstring_to_list (PREFIX)), a);
1003 a = acons (cell_symbol_label, cell_t, a);
1005 a = acons (cell_symbol_dot, cell_dot, a);
1006 a = acons (cell_symbol_begin, cell_begin, a);
1007 a = acons (cell_symbol_call_with_values, cell_symbol_call_with_values, a);
1008 a = acons (cell_symbol_current_module, cell_symbol_current_module, a);
1009 a = acons (cell_symbol_call_with_current_continuation, cell_call_with_current_continuation, a);
1010 a = acons (cell_symbol_sc_expand, cell_f, a);
1013 a = acons (cell_symbol_gnuc, cell_t, a);
1014 a = acons (cell_symbol_mesc, cell_f, a);
1016 a = acons (cell_symbol_gnuc, cell_f, a);
1017 a = acons (cell_symbol_mesc, cell_t, a);
1020 a = acons (cell_closure, a, a);
1026 mes_builtins (SCM a) ///((internal))
1030 // Do not sort: Order of these includes define builtins
1038 #include "gc.environment.i"
1039 #include "lib.environment.i"
1040 #include "math.environment.i"
1041 #include "mes.environment.i"
1042 #include "posix.environment.i"
1043 #include "reader.environment.i"
1044 #include "vector.environment.i"
1048 fputs ("functions: ", stderr);
1049 fputs (itoa (g_function), stderr);
1050 fputs ("\n", stderr);
1051 for (int i = 0; i < g_function; i++)
1053 fputs ("[", stderr);
1054 fputs (itoa (i), stderr);
1055 fputs ("]: ", stderr);
1056 fputs (g_functions[i].name, stderr);
1057 fputs ("\n", stderr);
1059 fputs ("\n", stderr);
1066 mes_g_stack (SCM a) ///((internal))
1072 g_stack = cons (cell_nil, cell_nil);
1077 mes_environment () ///((internal))
1079 SCM a = mes_symbols ();
1080 return mes_g_stack (a);
1093 assert_defined (SCM x, SCM e) ///((internal))
1095 if (e == cell_undefined) return error (cell_symbol_unbound_variable, x);
1100 check_formals (SCM f, SCM formals, SCM args) ///((internal))
1102 int flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : VALUE (length (formals));
1103 int alen = VALUE (length (args));
1104 if (alen != flen && alen != -1 && flen != -1)
1107 sprintf (buf, "apply: wrong number of arguments; expected: %d, got: %d: ", flen, alen);
1108 SCM e = MAKE_STRING (cstring_to_list (buf));
1109 return error (cell_symbol_wrong_number_of_args, cons (e, f));
1111 return cell_unspecified;
1115 check_apply (SCM f, SCM e) ///((internal))
1117 char const* type = 0;
1118 if (f == cell_f || f == cell_t) type = "bool";
1119 if (f == cell_nil) type = "nil";
1120 if (f == cell_unspecified) type = "*unspecified*";
1121 if (f == cell_undefined) type = "*undefined*";
1122 if (TYPE (f) == TCHAR) type = "char";
1123 if (TYPE (f) == TNUMBER) type = "number";
1124 if (TYPE (f) == TSTRING) type = "string";
1129 sprintf (buf, "cannot apply: %s:", type);
1130 fprintf (stderr, " [");
1132 fprintf (stderr, "]\n");
1133 SCM e = MAKE_STRING (cstring_to_list (buf));
1134 return error (cell_symbol_wrong_type_arg, cons (e, f));
1136 return cell_unspecified;
1140 load_env (SCM a) ///((internal))
1143 g_stdin = open ("module/mes/read-0.mes", O_RDONLY);
1144 g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "mes/read-0.mes", O_RDONLY);
1145 if (!g_function) r0 = mes_builtins (r0);
1146 r2 = read_input_file_env (r0);
1152 bload_env (SCM a) ///((internal))
1155 g_stdin = fopen ("module/mes/read-0-32.mo", O_RDONLY);
1157 g_stdin = open ("module/mes/read-0.mo", O_RDONLY);
1158 g_stdin = g_stdin >= 0 ? g_stdin : open (MODULEDIR "mes/read-0.mo", O_RDONLY);
1161 char *p = (char*)g_cells;
1162 assert (getchar () == 'M');
1163 assert (getchar () == 'E');
1164 assert (getchar () == 'S');
1165 g_stack = getchar () << 8;
1166 g_stack += getchar ();
1173 g_free = (p-(char*)g_cells) / sizeof (struct scm);
1177 r0 = mes_builtins (r0);
1182 main (int argc, char *argv[])
1185 g_debug = getenv ("MES_DEBUG");
1186 if (g_debug) {eputs ("MODULEDIR=");eputs (MODULEDIR);eputs ("\n");}
1188 if (getenv ("MES_ARENA")) ARENA_SIZE = atoi (getenv ("MES_ARENA"));
1189 if (getenv ("MES_MAX_ARENA")) MAX_ARENA_SIZE = atoi (getenv ("MES_MAX_ARENA"));
1190 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes [--dump|--load] < FILE");
1191 if (argc > 1 && !strcmp (argv[1], "--version")) {puts ("Mes ");puts (VERSION);return 0;};
1193 r0 = mes_environment ();
1195 SCM program = (argc > 1 && !strcmp (argv[1], "--load"))
1196 ? bload_env (r0) : load_env (r0);
1197 if (argc > 1 && !strcmp (argv[1], "--dump")) return dump ();
1200 for (int i=argc; i; i--) lst = cons (MAKE_STRING (cstring_to_list (argv[i-1])), lst);
1201 r0 = acons (cell_symbol_argv, lst, r0);
1203 if (g_debug) {eputs ("program: "); display_error_ (r2); eputs ("\n");}
1204 push_cc (r2, cell_unspecified, r0, cell_unspecified);
1207 display_error_ (r1);
1211 if (g_debug) fprintf (stderr, "\nstats: [%d]\n", g_free);