1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016 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/>.
21 SCM caar (SCM x) {return car (car (x));}
22 SCM cadr (SCM x) {return car (cdr (x));}
23 SCM cdar (SCM x) {return cdr (car (x));}
24 SCM cddr (SCM x) {return cdr (cdr (x));}
27 xassq (SCM x, SCM a) ///for speed in core only
29 while (a != cell_nil && x != CDAR (a)) a = CDR (a);
30 return a != cell_nil ? CAR (a) : cell_f;
40 if (TYPE (x) != PAIR) return MAKE_NUMBER (-1);
43 return MAKE_NUMBER (n);
47 list (SCM x) ///((arity . n))
53 exit_ (SCM x) ///((name . "exit"))
55 assert (TYPE (x) == NUMBER);
60 string_to_cstring (SCM s)
62 static char buf[1024];
67 *p++ = VALUE (car (s));
75 error (SCM key, SCM x)
78 if ((throw = assq_ref_cache (cell_symbol_throw, r0)) != cell_undefined)
79 return apply (throw, cons (key, cons (x, cell_nil)), r0);
84 assert_defined (SCM x, SCM e)
86 if (e == cell_undefined) return error (cell_symbol_unbound_variable, x);
91 check_formals (SCM f, SCM formals, SCM args)
93 int flen = (TYPE (formals) == NUMBER) ? VALUE (formals) : VALUE (length (formals));
94 int alen = VALUE (length (args));
95 if (alen != flen && alen != -1 && flen != -1)
98 sprintf (buf, "apply: wrong number of arguments; expected: %d, got: %d: ", flen, alen);
99 SCM e = MAKE_STRING (cstring_to_list (buf));
100 return error (cell_symbol_wrong_number_of_args, cons (e, f));
102 return cell_unspecified;
106 check_apply (SCM f, SCM e)
108 char const* type = 0;
109 if (f == cell_f || f == cell_t) type = "bool";
110 if (f == cell_nil) type = "nil";
111 if (f == cell_unspecified) type = "*unspecified*";
112 if (f == cell_undefined) type = "*undefined*";
113 if (TYPE (f) == CHAR) type = "char";
114 if (TYPE (f) == NUMBER) type = "number";
115 if (TYPE (f) == STRING) type = "string";
120 sprintf (buf, "cannot apply: %s:", type);
121 fprintf (stderr, " [");
123 fprintf (stderr, "]\n");
124 SCM e = MAKE_STRING (cstring_to_list (buf));
125 return error (cell_symbol_wrong_type_arg, cons (e, f));
127 return cell_unspecified;
135 gc (gc_push_frame ());
136 char *p = (char*)g_cells;
140 fputc (g_stack >> 8, stdout);
141 fputc (g_stack % 256, stdout);
142 if (getenv ("MES_HACK"))
144 TYPE (9) = 0x2d2d2d2d;
145 CAR (9) = 0x2d2d2d2d;
146 CDR (9) = 0x3e3e3e3e;
153 CAR (11) = 0x58585858;
161 CAR (11) = 0x58585858;
164 TYPE (14) = 0x3c3c3c3c;
165 CAR (14) = 0x2d2d2d2d;
166 CDR (14) = 0x2d2d2d2d;
170 for (int i=0; i<g_free * sizeof(scm); i++)
171 fputc (*p++, stdout);
176 load_env (SCM a) ///((internal))
179 g_stdin = fopen ("module/mes/read-0.mes", "r");
180 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mes", "r");
181 if (!g_function) r0 = mes_builtins (r0);
182 r2 = read_input_file_env (r0);
188 bload_env (SCM a) ///((internal))
190 g_stdin = fopen ("module/mes/read-0.mo", "r");
191 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
192 char *p = (char*)g_cells;
193 assert (getchar () == 'M');
194 assert (getchar () == 'E');
195 assert (getchar () == 'S');
196 g_stack = getchar () << 8;
197 g_stack += getchar ();
204 g_free = (p-(char*)g_cells) / sizeof (scm);
208 r0 = mes_builtins (r0);