core: Add some error checking.
[mes.git] / lib.c
1 /* -*-comment-start: "//";comment-end:""-*-
2  * Mes --- Maxwell Equations of Software
3  * Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
4  *
5  * This file is part of Mes.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
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));}
25 SCM caaar (SCM x) {return car (car (car (x)));}
26 SCM caadr (SCM x) {return car (car (cdr (x)));}
27 SCM caddr (SCM x) {return car (cdr (cdr (x)));}
28 SCM cdadr (SCM x) {return cdr (car (cdr (x)));}
29 SCM cadar (SCM x) {return car (cdr (car (x)));}
30 SCM cddar (SCM x) {return cdr (cdr (car (x)));}
31 SCM cdddr (SCM x) {return cdr (cdr (cdr (x)));}
32 SCM cadddr (SCM x) {return car (cdr (cdr (cdr (x))));}
33
34 SCM
35 length (SCM x)
36 {
37   int n = 0;
38   while (x != cell_nil)
39     {
40       n++;
41       if (TYPE (x) != PAIR) return MAKE_NUMBER (-1);
42       x = cdr (x);
43     }
44   return MAKE_NUMBER (n);
45 }
46
47 SCM
48 list (SCM x) ///((arity . n))
49 {
50   return x;
51 }
52
53 SCM
54 exit_ (SCM x) ///((name . "exit"))
55 {
56   assert (TYPE (x) == NUMBER);
57   exit (VALUE (x));
58 }
59
60 char const*
61 string_to_cstring (SCM s)
62 {
63   static char buf[1024];
64   char *p = buf;
65   s = STRING (s);
66   while (s != cell_nil)
67     {
68       *p++ = VALUE (car (s));
69       s = cdr (s);
70     }
71   *p = 0;
72   return buf;
73 }
74
75 int
76 error (char const* msg, SCM x)
77 {
78   fprintf (stderr, msg);
79   if (x) stderr_ (x);
80   fprintf (stderr, "\n");
81   assert(!msg);
82 }
83
84 SCM
85 assert_defined (SCM x, SCM e)
86 {
87   if (e == cell_undefined) error ("eval: unbound variable: ", x);
88   return e;
89 }
90
91 SCM
92 check_formals (SCM f, SCM formals, SCM args)
93 {
94   int flen = (TYPE (formals) == NUMBER) ? VALUE (formals) : VALUE (length (formals));
95   int alen = VALUE (length (args));
96   if (alen != flen && alen != -1 && flen != -1)
97     {
98       char buf[1024];
99       sprintf (buf, "apply: wrong number of arguments; expected: %d, got: %d: ", flen, alen);
100       error (buf, f);
101     }
102   return cell_unspecified;
103 }
104
105 SCM
106 check_apply (SCM f, SCM e)
107 {
108   char const* type = 0;
109   if (f == cell_f || f == cell_t) type = "bool";
110   if (TYPE (f) == CHAR) type = "char";
111   if (TYPE (f) == NUMBER) type = "number";
112   if (TYPE (f) == STRING) type = "string";
113   if (f == cell_unspecified) type = "*unspecified*";
114   if (f == cell_undefined) type =  "*undefined*";
115
116   if (type)
117     {
118       char buf[1024];
119       sprintf (buf, "cannot apply: %s:", type);
120       fprintf (stderr, " [");
121       stderr_ (e);
122       fprintf (stderr, "]\n");
123       error (buf, f);
124     }
125   return cell_unspecified;
126 }