1 /* -*-comment-start: "//";comment-end:""-*-
2 * GNU Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 * This file is part of GNU Mes.
7 * GNU 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 * GNU 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 GNU Mes. If not, see <http://www.gnu.org/licenses/>.
22 //#define HAVE_UNION 1
24 #error "POSIX not supported"
35 int ARENA_SIZE = 200000; // 32b: 2MiB, 64b: 4 MiB
36 int MAX_ARENA_SIZE = 300000000;
37 long STACK_SIZE = 20000;
47 SCM g_continuations = 0;
52 SCM *g_stack_array = 0;
62 enum type_t {TCHAR, TCLOSURE, TCONTINUATION, TFUNCTION, TKEYWORD, TMACRO, TNUMBER, TPAIR, TPORT, TREF, TSPECIAL, TSTRING, TSTRUCT, TSYMBOL, TVALUES, TVARIABLE, TVECTOR, TBROKEN_HEART};
70 int (*function) (void);
78 struct scm *g_cells = foobar;
79 struct scm *g_news = foobar;
81 struct scm *g_cells = 0;
82 struct scm *g_news = 0;
85 struct scm scm_nil = {TSPECIAL, "()",0};
86 struct scm scm_f = {TSPECIAL, "#f",0};
87 struct scm scm_t = {TSPECIAL, "#t",0};
88 struct scm scm_dot = {TSPECIAL, ".",0};
89 struct scm scm_arrow = {TSPECIAL, "=>",0};
90 struct scm scm_undefined = {TSPECIAL, "*undefined*",0};
91 struct scm scm_unspecified = {TSPECIAL, "*unspecified*",0};
92 struct scm scm_closure = {TSPECIAL, "*closure*",0};
93 struct scm scm_circular = {TSPECIAL, "*circular*",0};
94 struct scm scm_begin = {TSPECIAL, "*begin*",0};
96 struct scm scm_symbol_dot = {TSYMBOL, "*dot*",0};
97 struct scm scm_symbol_lambda = {TSYMBOL, "lambda",0};
98 struct scm scm_symbol_begin = {TSYMBOL, "begin",0};
99 struct scm scm_symbol_if = {TSYMBOL, "if",0};
100 struct scm scm_symbol_quote = {TSYMBOL, "quote",0};
101 struct scm scm_symbol_define = {TSYMBOL, "define",0};
102 struct scm scm_symbol_define_macro = {TSYMBOL, "define-macro",0};
104 struct scm scm_symbol_quasiquote = {TSYMBOL, "quasiquote", 0};
105 struct scm scm_symbol_unquote = {TSYMBOL, "unquote", 0};
106 struct scm scm_symbol_unquote_splicing = {TSYMBOL, "unquote-splicing", 0};
107 struct scm scm_symbol_syntax = {TSYMBOL, "syntax",0};
108 struct scm scm_symbol_quasisyntax = {TSYMBOL, "quasisyntax", 0};
109 struct scm scm_symbol_unsyntax = {TSYMBOL, "unsyntax", 0};
110 struct scm scm_symbol_unsyntax_splicing = {TSYMBOL, "unsyntax-splicing", 0};
112 struct scm scm_symbol_set_x = {TSYMBOL, "set!",0};
114 struct scm scm_symbol_sc_expand = {TSYMBOL, "sc-expand",0};
115 struct scm scm_symbol_macro_expand = {TSYMBOL, "macro-expand",0};
116 struct scm scm_symbol_portable_macro_expand = {TSYMBOL, "portable-macro-expand",0};
117 struct scm scm_symbol_sc_expander_alist = {TSYMBOL, "*sc-expander-alist*",0};
119 struct scm scm_symbol_call_with_values = {TSYMBOL, "call-with-values",0};
120 struct scm scm_call_with_current_continuation = {TSPECIAL, "*call/cc*",0};
121 struct scm scm_symbol_call_with_current_continuation = {TSYMBOL, "call-with-current-continuation",0};
122 struct scm scm_symbol_boot_module = {TSYMBOL, "boot-module",0};
123 struct scm scm_symbol_current_module = {TSYMBOL, "current-module",0};
124 struct scm scm_symbol_primitive_load = {TSYMBOL, "primitive-load",0};
125 struct scm scm_symbol_read_input_file = {TSYMBOL, "read-input-file",0};
126 struct scm scm_symbol_write = {TSYMBOL, "write",0};
127 struct scm scm_symbol_display = {TSYMBOL, "display",0};
129 struct scm scm_symbol_throw = {TSYMBOL, "throw",0};
130 struct scm scm_symbol_not_a_number = {TSYMBOL, "not-a-number",0};
131 struct scm scm_symbol_not_a_pair = {TSYMBOL, "not-a-pair",0};
132 struct scm scm_symbol_system_error = {TSYMBOL, "system-error",0};
133 struct scm scm_symbol_wrong_number_of_args = {TSYMBOL, "wrong-number-of-args",0};
134 struct scm scm_symbol_wrong_type_arg = {TSYMBOL, "wrong-type-arg",0};
135 struct scm scm_symbol_unbound_variable = {TSYMBOL, "unbound-variable",0};
137 struct scm scm_symbol_argv = {TSYMBOL, "%argv",0};
138 struct scm scm_symbol_mes_prefix = {TSYMBOL, "%prefix",0};
139 struct scm scm_symbol_mes_version = {TSYMBOL, "%version",0};
141 struct scm scm_symbol_car = {TSYMBOL, "car",0};
142 struct scm scm_symbol_cdr = {TSYMBOL, "cdr",0};
143 struct scm scm_symbol_pmatch_car = {TSYMBOL, "pmatch-car",0};
144 struct scm scm_symbol_pmatch_cdr = {TSYMBOL, "pmatch-cdr",0};
146 struct scm scm_vm_evlis = {TSPECIAL, "*vm-evlis*",0};
147 struct scm scm_vm_evlis2 = {TSPECIAL, "*vm-evlis2*",0};
148 struct scm scm_vm_evlis3 = {TSPECIAL, "*vm-evlis3*",0};
149 struct scm scm_vm_apply = {TSPECIAL, "core:apply",0};
150 struct scm scm_vm_apply2 = {TSPECIAL, "*vm-apply2*",0};
151 struct scm scm_vm_eval = {TSPECIAL, "core:eval-sexp",0};
153 struct scm scm_vm_eval_pmatch_car = {TSPECIAL, "*vm-eval-pmatch-car*",0};
154 struct scm scm_vm_eval_pmatch_cdr = {TSPECIAL, "*vm-eval-pmatch-cdr*",0};
155 struct scm scm_vm_eval_define = {TSPECIAL, "*vm-eval-define*",0};
157 struct scm scm_vm_eval_set_x = {TSPECIAL, "*vm-eval-set!*",0};
158 struct scm scm_vm_eval_macro_expand_eval = {TSPECIAL, "*vm:eval-macro-expand-eval*",0};
159 struct scm scm_vm_eval_macro_expand_expand = {TSPECIAL, "*vm:eval-macro-expand-expand*",0};
160 struct scm scm_vm_eval_check_func = {TSPECIAL, "*vm-eval-check-func*",0};
161 struct scm scm_vm_eval2 = {TSPECIAL, "*vm-eval2*",0};
162 struct scm scm_vm_macro_expand = {TSPECIAL, "core:macro-expand",0};
163 struct scm scm_vm_macro_expand_define = {TSPECIAL, "*vm:core:macro-expand-define*",0};
164 struct scm scm_vm_macro_expand_define_macro = {TSPECIAL, "*vm:core:macro-expand-define-macro*",0};
165 struct scm scm_vm_macro_expand_lambda = {TSPECIAL, "*vm:core:macro-expand-lambda*",0};
166 struct scm scm_vm_macro_expand_set_x = {TSPECIAL, "*vm:core:macro-expand-set!*",0};
167 struct scm scm_vm_begin_expand_primitive_load = {TSPECIAL, "*vm:core:begin-expand-primitive-load*",0};
168 struct scm scm_vm_begin_primitive_load = {TSPECIAL, "*vm:core:begin-primitive-load*",0};
169 struct scm scm_vm_macro_expand_car = {TSPECIAL, "*vm:core:macro-expand-car*",0};
170 struct scm scm_vm_macro_expand_cdr = {TSPECIAL, "*vm:macro-expand-cdr*",0};
171 struct scm scm_vm_begin_expand = {TSPECIAL, "core:eval",0};
172 struct scm scm_vm_begin_expand_eval = {TSPECIAL, "*vm:begin-expand-eval*",0};
173 struct scm scm_vm_begin_expand_macro = {TSPECIAL, "*vm:begin-expand-macro*",0};
174 struct scm scm_vm_begin = {TSPECIAL, "*vm-begin*",0};
175 struct scm scm_vm_begin_read_input_file = {TSPECIAL, "*vm-begin-read-input-file*",0};
176 struct scm scm_vm_begin_eval = {TSPECIAL, "*vm:begin-eval*",0};
177 struct scm scm_vm_if = {TSPECIAL, "*vm-if*",0};
178 struct scm scm_vm_if_expr = {TSPECIAL, "*vm-if-expr*",0};
179 struct scm scm_vm_call_with_values2 = {TSPECIAL, "*vm-call-with-values2*",0};
180 struct scm scm_vm_call_with_current_continuation2 = {TSPECIAL, "*vm-call-with-current-continuation2*",0};
181 struct scm scm_vm_return = {TSPECIAL, "*vm-return*",0};
183 struct scm scm_type_char = {TSYMBOL, "<cell:char>",0};
184 struct scm scm_type_closure = {TSYMBOL, "<cell:closure>",0};
185 struct scm scm_type_continuation = {TSYMBOL, "<cell:continuation>",0};
186 struct scm scm_type_function = {TSYMBOL, "<cell:function>",0};
187 struct scm scm_type_keyword = {TSYMBOL, "<cell:keyword>",0};
188 struct scm scm_type_macro = {TSYMBOL, "<cell:macro>",0};
189 struct scm scm_type_number = {TSYMBOL, "<cell:number>",0};
190 struct scm scm_type_pair = {TSYMBOL, "<cell:pair>",0};
191 struct scm scm_type_port = {TSYMBOL, "<cell:port>",0};
192 struct scm scm_type_ref = {TSYMBOL, "<cell:ref>",0};
193 struct scm scm_type_special = {TSYMBOL, "<cell:special>",0};
194 struct scm scm_type_string = {TSYMBOL, "<cell:string>",0};
195 struct scm scm_type_struct = {TSYMBOL, "<cell:struct>",0};
196 struct scm scm_type_symbol = {TSYMBOL, "<cell:symbol>",0};
197 struct scm scm_type_values = {TSYMBOL, "<cell:values>",0};
198 struct scm scm_type_variable = {TSYMBOL, "<cell:variable>",0};
199 struct scm scm_type_vector = {TSYMBOL, "<cell:vector>",0};
200 struct scm scm_type_broken_heart = {TSYMBOL, "<cell:broken-heart>",0};
202 struct scm scm_symbol_compiler = {TSYMBOL, "%compiler",0};
203 struct scm scm_symbol_arch = {TSYMBOL, "%arch",0};
205 struct scm scm_test = {TSYMBOL, "test",0};
207 #include "mes.mes.symbols.h"
213 struct function g_functions[200];
219 #include "math.mes.h"
226 // #include "posix.mes.h"
228 //#include "vector.mes.h"
230 #define TYPE(x) g_cells[x].type
231 #define CAR(x) g_cells[x].car
232 #define CDR(x) g_cells[x].cdr
234 #define NTYPE(x) g_news[x].type
235 #define NCAR(x) g_news[x].car
236 #define NCDR(x) g_news[x].cdr
238 #define LENGTH(x) g_cells[x].car
239 #define REF(x) g_cells[x].car
240 #define STRING(x) g_cells[x].car
241 #define STRUCT(x) g_cells[x].cdr
242 #define VARIABLE(x) g_cells[x].car
244 #define CLOSURE(x) g_cells[x].cdr
245 #define CONTINUATION(x) g_cells[x].cdr
247 #define FUNCTION(x) g_functions[g_cells[x].cdr]
248 #define MACRO(x) g_cells[x].cdr
249 #define PORT(x) g_cells[x].cdr
250 #define VALUE(x) g_cells[x].cdr
251 #define VECTOR(x) g_cells[x].cdr
253 #define NLENGTH(x) g_news[x].car
255 #define NVALUE(x) g_news[x].cdr
256 #define NVECTOR(x) g_news[x].cdr
258 #define MAKE_CHAR(n) make_cell_ (tmp_num_ (TCHAR), 0, tmp_num2_ (n))
259 #define MAKE_CONTINUATION(n) make_cell_ (tmp_num_ (TCONTINUATION), n, g_stack)
260 #define MAKE_NUMBER(n) make_cell_ (tmp_num_ (TNUMBER), 0, tmp_num2_ (n))
261 #define MAKE_REF(n) make_cell_ (tmp_num_ (TREF), n, 0)
262 #define MAKE_STRING(x) make_cell_ (tmp_num_ (TSTRING), x, 0)
264 #define CAAR(x) CAR (CAR (x))
265 #define CADR(x) CAR (CDR (x))
266 #define CDAR(x) CDR (CAR (x))
267 #define CDDR(x) CDR (CDR (x))
268 #define CADAR(x) CAR (CDR (CAR (x)))
269 #define CADDR(x) CAR (CDR (CDR (x)))
270 #define CDADAR(x) CAR (CDR (CAR (CDR (x))))
275 assert (g_free + n < ARENA_SIZE);
291 VALUE (tmp_num2) = x;
296 make_cell_ (SCM type, SCM car, SCM cdr)
299 assert (TYPE (type) == TNUMBER);
300 TYPE (x) = VALUE (type);
301 if (VALUE (type) == TCHAR || VALUE (type) == TNUMBER) {
302 if (car) CAR (x) = CAR (car);
303 if (cdr) CDR(x) = CDR(cdr);
305 else if (VALUE (type) == TFUNCTION) {
306 if (car) CAR (x) = car;
307 if (cdr) CDR(x) = CDR(cdr);
317 make_symbol_ (SCM s) ///((internal))
319 VALUE (tmp_num) = TSYMBOL;
320 SCM x = make_cell_ (tmp_num, s, 0);
321 g_symbols = cons (x, g_symbols);
326 list_of_char_equal_p (SCM a, SCM b) ///((internal))
328 while (a != cell_nil && b != cell_nil && VALUE (CAR (a)) == VALUE (CAR (b))) {
329 assert (TYPE (CAR (a)) == TCHAR);
330 assert (TYPE (CAR (b)) == TCHAR);
334 return (a == cell_nil && b == cell_nil) ? cell_t : cell_f;
338 list_to_symbol (SCM s)
342 if (list_of_char_equal_p (STRING (CAR (x)), s) == cell_t) break;
346 if (!x) x = make_symbol_ (s);
353 return MAKE_NUMBER (TYPE (x));
359 return (TYPE (x) != TCONTINUATION
360 && (TYPE (CAR (x)) == TPAIR // FIXME: this is weird
361 || TYPE (CAR (x)) == TREF
362 || TYPE (CAR (x)) == TSPECIAL
363 || TYPE (CAR (x)) == TSYMBOL
364 || TYPE (CAR (x)) == TSTRING)) ? CAR (x) : MAKE_NUMBER (CAR (x));
370 return (TYPE (CDR (x)) == TPAIR
371 || TYPE (CDR (x)) == TREF
372 || TYPE (CAR (x)) == TSPECIAL
373 || TYPE (CDR (x)) == TSYMBOL
374 || TYPE (CDR (x)) == TSTRING) ? CDR (x) : MAKE_NUMBER (CDR (x));
380 assert (TYPE (x) == TFUNCTION);
381 return MAKE_NUMBER (FUNCTION (x).arity);
387 VALUE (tmp_num) = TPAIR;
388 return make_cell_ (tmp_num, x, y);
394 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_car));
401 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_symbol_cdr));
406 list (SCM x) ///((arity . n))
414 return x == cell_nil ? cell_t : cell_f;
421 || ((TYPE (x) == TKEYWORD && TYPE (y) == TKEYWORD
422 && STRING (x) == STRING (y)))
423 || (TYPE (x) == TCHAR && TYPE (y) == TCHAR
424 && VALUE (x) == VALUE (y))
425 || (TYPE (x) == TNUMBER && TYPE (y) == TNUMBER
426 && VALUE (x) == VALUE (y)))
431 values (SCM x) ///((arity . n))
439 acons (SCM key, SCM value, SCM alist)
441 return cons (cons (key, value), alist);
448 while (x != cell_nil)
451 if (TYPE (x) != TPAIR) return MAKE_NUMBER (-1);
454 return MAKE_NUMBER (n);
457 SCM apply (SCM, SCM, SCM);
460 error (SCM key, SCM x)
463 if ((throw = assq_ref_env (cell_symbol_throw, r0)) != cell_undefined)
464 return apply (throw, cons (key, cons (x, cell_nil)), r0);
465 display_error_ (key);
473 cstring_to_list (char const* s)
478 p = cons (MAKE_CHAR (s[i]), p);
484 assert_defined (SCM x, SCM e) ///((internal))
486 if (e == cell_undefined) return error (cell_symbol_unbound_variable, x);
491 check_formals (SCM f, SCM formals, SCM args) ///((internal))
493 int flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : VALUE (length (formals));
494 int alen = VALUE (length (args));
495 if (alen != flen && alen != -1 && flen != -1)
497 char *s = "apply: wrong number of arguments; expected: ";
504 SCM e = MAKE_STRING (cstring_to_list (s));
505 return error (cell_symbol_wrong_number_of_args, cons (e, f));
507 return cell_unspecified;
511 check_apply (SCM f, SCM e) ///((internal))
514 if (f == cell_f || f == cell_t) type = "bool";
515 if (f == cell_nil) type = "nil";
516 if (f == cell_unspecified) type = "*unspecified*";
517 if (f == cell_undefined) type = "*undefined*";
518 if (TYPE (f) == TCHAR) type = "char";
519 if (TYPE (f) == TNUMBER) type = "number";
520 if (TYPE (f) == TSTRING) type = "string";
524 char *s = "cannot apply: ";
530 SCM e = MAKE_STRING (cstring_to_list (s));
531 return error (cell_symbol_wrong_type_arg, cons (e, f));
533 return cell_unspecified;
537 gc_push_frame () ///((internal))
539 SCM frame = cons (r1, cons (r2, cons (r3, cons (r0, cell_nil))));
540 g_stack = cons (frame, g_stack);
545 append2 (SCM x, SCM y)
549 if (TYPE (x) != TPAIR)
550 error (cell_symbol_not_a_pair, cons (x, cell_append2));
552 while (x != cell_nil)
554 r = cons (CAR (x), r);
557 return reverse_x_ (r, y);
561 append_reverse (SCM x, SCM y)
565 if (TYPE (x) != TPAIR)
566 error (cell_symbol_not_a_pair, cons (x, cell_append_reverse));
567 while (x != cell_nil)
569 y = cons (CAR (x), y);
576 reverse_x_ (SCM x, SCM t)
578 if (TYPE (x) != TPAIR)
579 error (cell_symbol_not_a_pair, cons (x, cell_reverse_x_));
581 while (x != cell_nil)
592 pairlis (SCM x, SCM y, SCM a)
596 if (TYPE (x) != TPAIR)
597 return cons (cons (x, y), a);
598 return cons (cons (car (x), car (y)),
599 pairlis (cdr (x), cdr (y), a));
605 if ((FUNCTION (fn).arity > 0 || FUNCTION (fn).arity == -1)
606 && x != cell_nil && TYPE (CAR (x)) == TVALUES)
607 x = cons (CADAR (x), CDR (x));
608 if ((FUNCTION (fn).arity > 1 || FUNCTION (fn).arity == -1)
609 && x != cell_nil && TYPE (CDR (x)) == TPAIR && TYPE (CADR (x)) == TVALUES)
610 x = cons (CAR (x), cons (CDADAR (x), CDR (x)));
611 switch (FUNCTION (fn).arity)
613 #if __MESC__ || !_POSIX_SOURCE
614 case 0: return (FUNCTION (fn).function) ();
615 case 1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (CAR (x));
616 case 2: return ((SCM(*)(SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x));
617 case 3: return ((SCM(*)(SCM,SCM,SCM))(FUNCTION (fn).function)) (CAR (x), CADR (x), CAR (CDDR (x)));
618 case -1: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
619 default: return ((SCM(*)(SCM))(FUNCTION (fn).function)) (x);
621 case 0: return FUNCTION (fn).function0 ();
622 case 1: return FUNCTION (fn).function1 (CAR (x));
623 case 2: return FUNCTION (fn).function2 (CAR (x), CADR (x));
624 case 3: return FUNCTION (fn).function3 (CAR (x), CADR (x), CAR (CDDR (x)));
625 case -1: return FUNCTION (fn).functionn (x);
629 return cell_unspecified;
635 //FIXME: move into fast-non eq_p-ing assq core:assq?
636 //while (a != cell_nil && x != CAAR (a)) a = CDR (a);
637 while (a != cell_nil && eq_p (x, CAAR (a)) == cell_f) a = CDR (a);
638 return a != cell_nil ? CAR (a) : cell_f;
642 assq_ref_env (SCM x, SCM a)
645 if (x == cell_f) return cell_undefined;
650 set_car_x (SCM x, SCM e)
652 assert (TYPE (x) == TPAIR);
654 return cell_unspecified;
658 set_cdr_x (SCM x, SCM e)
660 if (TYPE (x) != TPAIR) error (cell_symbol_not_a_pair, cons (x, cell_set_cdr_x));
662 return cell_unspecified;
666 set_env_x (SCM x, SCM e, SCM a)
668 SCM p = assert_defined (x, assq (x, a));
669 if (TYPE (p) != TPAIR) error (cell_symbol_not_a_pair, cons (p, x));
670 return set_cdr_x (p, e);
674 call_lambda (SCM e, SCM x, SCM aa, SCM a) ///((internal))
676 SCM cl = cons (cons (cell_closure, x), x);
679 return cell_unspecified;
683 make_closure_ (SCM args, SCM body, SCM a) ///((internal))
685 return make_cell_ (tmp_num_ (TCLOSURE), cell_f, cons (cons (cell_circular, a), cons (args, body)));
689 lookup_macro_ (SCM x, SCM a) ///((internal))
691 if (TYPE (x) != TSYMBOL) return cell_f;
692 SCM m = assq_ref_env (x, a);
693 if (TYPE (m) == TMACRO) return MACRO (m);
698 push_cc (SCM p1, SCM p2, SCM a, SCM c) ///((internal))
707 return cell_unspecified;
711 gc_peek_frame () ///((internal))
713 SCM frame = CAR (g_stack);
716 r3 = CAR (CDDR (frame));
717 r0 = CADR (CDDR (frame));
722 gc_pop_frame () ///((internal))
724 SCM frame = gc_peek_frame (g_stack);
725 g_stack = CDR (g_stack);
729 char const* string_to_cstring (SCM s);
732 add_formals (SCM formals, SCM x)
734 while (TYPE (x) == TPAIR)
736 formals = cons (CAR (x), formals);
739 if (TYPE (x) == TSYMBOL)
740 formals = cons (x, formals);
747 return cell_unspecified;
751 apply (SCM f, SCM x, SCM a) ///((internal))
753 push_cc (cons (f, x), cell_unspecified, r0, cell_unspecified);
755 return eval_apply ();
759 mes_g_stack (SCM a) ///((internal))
765 g_stack = cons (cell_nil, cell_nil);
769 //
\f Environment setup
772 make_tmps (struct scm* cells)
775 cells[tmp].type = TCHAR;
777 cells[tmp_num].type = TNUMBER;
779 cells[tmp_num2].type = TNUMBER;
793 gc_init_cells () ///((internal))
795 int size = ARENA_SIZE * 12;
798 g_arena = (char*)malloc (size);
806 //g_cells = (scm *)malloc (2*ARENA_SIZE*sizeof(scm));
818 mes_symbols () ///((internal))
823 #include "mes.mes.symbols.i"
825 g_symbol_max = g_free;
829 for (int i=1; i<g_symbol_max; i++)
830 g_symbols = cons (i, g_symbols);
834 #include "mes.mes.symbol-names.i"
836 a = acons (cell_symbol_mes_version, MAKE_STRING (cstring_to_list (VERSION)), a);
837 a = acons (cell_symbol_mes_prefix, MAKE_STRING (cstring_to_list (PREFIX)), a);
839 a = acons (cell_symbol_dot, cell_dot, a);
840 a = acons (cell_symbol_begin, cell_begin, a);
841 a = acons (cell_symbol_call_with_values, cell_symbol_call_with_values, a);
842 a = acons (cell_symbol_current_module, cell_symbol_current_module, a);
843 a = acons (cell_symbol_call_with_current_continuation, cell_call_with_current_continuation, a);
844 a = acons (cell_symbol_sc_expand, cell_f, a);
846 char *compiler = "gcc";
852 a = acons (cell_symbol_compiler, MAKE_STRING (cstring_to_list (compiler)), a);
858 a = acons (cell_symbol_arch, MAKE_STRING (cstring_to_list (arch)), a);
860 a = acons (cell_closure, a, a);
866 mes_environment () ///((internal))
868 SCM a = mes_symbols ();
869 return mes_g_stack (a);
873 mes_builtins (SCM a) ///((internal))
877 // Do not sort: Order of these includes define builtins
879 #include "posix.mes.i"
880 #include "math.mes.i"
884 #include "vector.mes.i"
888 //#include "reader.mes.i"
891 #include "gc.mes.environment.i"
892 #include "lib.mes.environment.i"
894 #include "math.mes.environment.i"
896 #include "mes.mes.environment.i"
898 #include "posix.mes.environment.i"
899 //#include "reader.mes.environment.i"
900 #include "vector.mes.environment.i"
907 bload_env (SCM a) ///((internal))
909 char *mo = "module/mes/read-0-32.mo";
910 g_stdin = open (mo, 0);
911 if (g_stdin < 0) {eputs ("no such file: ");eputs (mo);eputs ("\n");return 1;}
912 assert (getchar () == 'M');
913 assert (getchar () == 'E');
914 assert (getchar () == 'S');
915 eputs ("*GOT MES*\n");
916 g_stack = getchar () << 8;
917 g_stack += getchar ();
919 char *p = (char*)g_cells;
926 g_free = (p-(char*)g_cells) /
930 r0 = mes_builtins (r0);
932 char *compiler = "gcc";
938 set_env_x (cell_symbol_compiler, MAKE_STRING (cstring_to_list (compiler)), r0);
944 set_env_x (cell_symbol_arch, MAKE_STRING (cstring_to_list (arch)), r0);
950 while (s && s != cell_nil) {
951 display_error_ (CAR (s));
956 eputs ("functions: ");
957 eputs (itoa (g_function));
959 for (int i = 0; i < g_function; i++)
964 eputs (g_functions[i].name);
967 //display_error_ (r0);
979 main (int argc, char *argv[])
982 if (p = getenv ("MES_DEBUG")) g_debug = atoi (p);
983 if (g_debug) {eputs (";;; MODULEDIR=");eputs (MODULEDIR);eputs ("\n");}
984 if (p = getenv ("MES_MAX_ARENA")) MAX_ARENA_SIZE = atoi (p);
985 if (p = getenv ("MES_ARENA")) ARENA_SIZE = atoi (p);
986 if (argc > 1 && !strcmp (argv[1], "--help")) return puts ("Usage: mes [--dump|--load] < FILE\n");
987 if (argc > 1 && !strcmp (argv[1], "--version")) {puts ("Mes ");puts (VERSION);puts ("\n");return 0;};
989 r0 = mes_environment ();
991 SCM program = bload_env (r0);
993 for (int i=argc-1; i>=0; i--) lst = cons (MAKE_STRING (cstring_to_list (argv[i])), lst);
994 r0 = acons (cell_symbol_argv, lst, r0);
995 push_cc (r2, cell_unspecified, r0, cell_unspecified);
1004 display_error_ (r1);
1009 eputs ("\nstats: [");
1010 eputs (itoa (g_free));