2003-08-30 12:47:54 -04:00
|
|
|
/* type.c: Built-in and user-defined Scheme types.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
|
|
|
|
* Copyright 2002, 2003 Sam Hocevar <sam@zoy.org>, Paris
|
|
|
|
*
|
|
|
|
* This software was derived from Elk 1.2, which was Copyright 1987, 1988,
|
|
|
|
* 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
|
|
|
|
* by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
|
|
|
|
* between TELES and Nixdorf Microprocessor Engineering, Berlin).
|
|
|
|
*
|
|
|
|
* Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
|
|
|
|
* owners or individual owners of copyright in this software, grant to any
|
|
|
|
* person or company a worldwide, royalty free, license to
|
|
|
|
*
|
|
|
|
* i) copy this software,
|
|
|
|
* ii) prepare derivative works based on this software,
|
|
|
|
* iii) distribute copies of this software or derivative works,
|
|
|
|
* iv) perform this software, or
|
|
|
|
* v) display this software,
|
|
|
|
*
|
|
|
|
* provided that this notice is not removed and that neither Oliver Laumann
|
|
|
|
* nor Teles nor Nixdorf are deemed to have made any representations as to
|
|
|
|
* the suitability of this software for any purpose nor are held responsible
|
|
|
|
* for any defects of this software.
|
|
|
|
*
|
|
|
|
* THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
|
2003-08-19 15:19:38 -04:00
|
|
|
*/
|
|
|
|
|
2003-09-06 07:25:29 -04:00
|
|
|
#include "config.h"
|
2003-08-19 15:19:38 -04:00
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2003-09-06 07:25:29 -04:00
|
|
|
#include "kernel.h"
|
|
|
|
|
2003-08-19 15:19:38 -04:00
|
|
|
#define TYPE_GROW 10
|
|
|
|
|
|
|
|
TYPEDESCR *Types;
|
|
|
|
int Num_Types, Max_Type;
|
|
|
|
|
|
|
|
char *builtin_types[] = {
|
|
|
|
"0integer", "1integer" /* bignum */, "1real", "0null", "0boolean",
|
|
|
|
"0unbound", "0special", "0character", "1symbol", "1pair",
|
|
|
|
"1environment", "1string", "1vector", "1primitive", "1compound",
|
|
|
|
"1control-point", "1promise", "1port", "0end-of-file", "1autoload",
|
|
|
|
"1macro", "1!!broken-heart!!",
|
|
|
|
#ifdef GENERATIONAL_GC
|
|
|
|
"0align_8byte", "0freespace",
|
|
|
|
#endif
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
void Wrong_Type (Object x, register int t) {
|
2003-08-19 15:19:38 -04:00
|
|
|
Wrong_Type_Combination (x, Types[t].name);
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
void Wrong_Type_Combination (Object x, register char const *name) {
|
|
|
|
register int t = TYPE(x);
|
2003-08-19 15:19:38 -04:00
|
|
|
char buf[100];
|
|
|
|
|
|
|
|
if (t < 0 || t >= Num_Types)
|
2003-09-02 04:12:11 -04:00
|
|
|
Panic ("bad type1");
|
2003-08-19 15:19:38 -04:00
|
|
|
sprintf (buf, "wrong argument type %s (expected %s)",
|
2003-09-02 04:12:11 -04:00
|
|
|
Types[t].name, name);
|
2003-08-19 15:19:38 -04:00
|
|
|
Primitive_Error (buf);
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
Object P_Type (Object x) {
|
|
|
|
register int t = TYPE(x);
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
if (t < 0 || t >= Num_Types)
|
2003-09-02 04:12:11 -04:00
|
|
|
Panic ("bad type2");
|
2003-08-19 15:19:38 -04:00
|
|
|
return Intern (Types[t].name);
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
int Define_Type (register int t, char const *name,
|
2003-09-02 04:12:11 -04:00
|
|
|
int (*size)(), int const_size, int (*eqv)(), int (*equal)(),
|
|
|
|
int (*print)(), int (*visit)()) {
|
2003-08-19 15:19:38 -04:00
|
|
|
register TYPEDESCR *p;
|
|
|
|
|
|
|
|
Set_Error_Tag ("define-type");
|
|
|
|
if (t != 0)
|
2003-09-02 04:12:11 -04:00
|
|
|
Fatal_Error("first arg of Define_Type() must be 0");
|
2003-08-19 15:19:38 -04:00
|
|
|
if (Num_Types == Max_Type) {
|
2003-09-02 04:12:11 -04:00
|
|
|
Max_Type += TYPE_GROW;
|
|
|
|
Types = (TYPEDESCR *)Safe_Realloc((char *)Types,
|
|
|
|
Max_Type * sizeof(TYPEDESCR));
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
Disable_Interrupts;
|
|
|
|
p = &Types[Num_Types++];
|
|
|
|
p->haspointer = 1;
|
|
|
|
p->name = name;
|
|
|
|
p->size = size;
|
|
|
|
p->const_size = const_size;
|
|
|
|
p->eqv = eqv;
|
|
|
|
p->equal = equal;
|
|
|
|
p->print = print;
|
|
|
|
p->visit = visit;
|
|
|
|
Enable_Interrupts;
|
|
|
|
return Num_Types-1;
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
void Init_Type() {
|
2003-08-19 15:19:38 -04:00
|
|
|
int i, bytes;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
Num_Types = (sizeof(builtin_types) - 1) / sizeof(char *);
|
|
|
|
Max_Type = Num_Types + TYPE_GROW;
|
|
|
|
bytes = Max_Type * sizeof(TYPEDESCR);
|
|
|
|
Types = (TYPEDESCR *)Safe_Malloc(bytes);
|
2003-08-19 15:24:23 -04:00
|
|
|
memset(Types, 0, bytes);
|
|
|
|
for (i = 0; (p = builtin_types[i]); i++) {
|
2003-09-02 04:12:11 -04:00
|
|
|
Types[i].haspointer = *p != '0';
|
2006-03-02 16:04:10 -05:00
|
|
|
Types[i].name = p + 1; /* Skip first character */
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
}
|