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 (char const* msg, SCM x)
77 fprintf (stderr, msg);
79 fprintf (stderr, "\n");
84 assert_defined (SCM x, SCM e)
86 if (e == cell_undefined) error ("eval: 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);
101 return cell_unspecified;
105 check_apply (SCM f, SCM e)
107 char const* type = 0;
108 if (f == cell_f || f == cell_t) type = "bool";
109 if (TYPE (f) == CHAR) type = "char";
110 if (TYPE (f) == NUMBER) type = "number";
111 if (TYPE (f) == STRING) type = "string";
112 if (f == cell_unspecified) type = "*unspecified*";
113 if (f == cell_undefined) type = "*undefined*";
118 sprintf (buf, "cannot apply: %s:", type);
119 fprintf (stderr, " [");
121 fprintf (stderr, "]\n");
124 return cell_unspecified;
132 gc (gc_push_frame ());
133 char *p = (char*)g_cells;
137 fputc (g_stack >> 8, stdout);
138 fputc (g_stack % 256, stdout);
139 for (int i=0; i<g_free * sizeof(scm); i++)
140 fputc (*p++, stdout);
145 load_env (SCM a) ///((internal))
148 g_stdin = fopen ("module/mes/read-0.mes", "r");
149 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mes", "r");
150 if (!g_function) r0 = mes_builtins (r0);
151 r2 = read_input_file_env (r0);
157 bload_env (SCM a) ///((internal))
159 g_stdin = fopen ("module/mes/read-0.mo", "r");
160 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
161 char *p = (char*)g_cells;
162 assert (getchar () == 'M');
163 assert (getchar () == 'E');
164 assert (getchar () == 'S');
165 g_stack = getchar () << 8;
166 g_stack += getchar ();
173 g_free = (p-(char*)g_cells) / sizeof (scm);
177 r0 = mes_builtins (r0);