2005-05-08 07:39:51 +00:00
|
|
|
/*
|
2005-05-15 18:37:00 +00:00
|
|
|
* $Id: stx.c,v 1.13 2005-05-15 18:37:00 bacon Exp $
|
2005-05-08 07:39:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xp/stx/stx.h>
|
|
|
|
#include <xp/stx/memory.h>
|
2005-05-08 15:22:45 +00:00
|
|
|
#include <xp/stx/object.h>
|
2005-05-10 12:00:43 +00:00
|
|
|
#include <xp/stx/hash.h>
|
2005-05-08 07:39:51 +00:00
|
|
|
#include <xp/bas/memory.h>
|
2005-05-08 10:44:58 +00:00
|
|
|
#include <xp/bas/assert.h>
|
2005-05-08 07:39:51 +00:00
|
|
|
|
|
|
|
xp_stx_t* xp_stx_open (xp_stx_t* stx, xp_stx_word_t capacity)
|
|
|
|
{
|
|
|
|
if (stx == XP_NULL) {
|
|
|
|
stx = (xp_stx_t*) xp_malloc (xp_sizeof(stx));
|
|
|
|
if (stx == XP_NULL) return XP_NULL;
|
|
|
|
stx->__malloced = xp_true;
|
|
|
|
}
|
|
|
|
else stx->__malloced = xp_false;
|
|
|
|
|
|
|
|
if (xp_stx_memory_open (&stx->memory, capacity) == XP_NULL) {
|
|
|
|
if (stx->__malloced) xp_free (stx);
|
|
|
|
return XP_NULL;
|
|
|
|
}
|
|
|
|
|
2005-05-08 10:31:25 +00:00
|
|
|
stx->nil = XP_STX_NIL;
|
|
|
|
stx->true = XP_STX_TRUE;
|
|
|
|
stx->false = XP_STX_FALSE;
|
|
|
|
|
2005-05-10 06:08:57 +00:00
|
|
|
stx->symbol_table = XP_STX_NIL;
|
2005-05-10 15:15:58 +00:00
|
|
|
stx->class_symbol = XP_STX_NIL;
|
2005-05-10 12:00:43 +00:00
|
|
|
stx->class_metaclass = XP_STX_NIL;
|
2005-05-12 15:25:06 +00:00
|
|
|
stx->class_symbol_link = XP_STX_NIL;
|
2005-05-15 18:37:00 +00:00
|
|
|
stx->class_method = XP_STX_NIL;
|
|
|
|
stx->class_context = XP_STX_NIL;
|
2005-05-10 06:08:57 +00:00
|
|
|
|
2005-05-15 18:37:00 +00:00
|
|
|
stx->__wantabort = xp_false;
|
2005-05-08 07:39:51 +00:00
|
|
|
return stx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void xp_stx_close (xp_stx_t* stx)
|
|
|
|
{
|
|
|
|
xp_stx_memory_close (&stx->memory);
|
|
|
|
if (stx->__malloced) xp_free (stx);
|
|
|
|
}
|
|
|
|
|
2005-05-12 15:25:06 +00:00
|
|
|
static void __reset_symbol_link_class (xp_stx_t* stx, xp_stx_word_t idx)
|
|
|
|
{
|
|
|
|
XP_STX_CLASS(stx,idx) = stx->class_symbol_link;
|
|
|
|
}
|
|
|
|
|
2005-05-08 10:44:58 +00:00
|
|
|
int xp_stx_bootstrap (xp_stx_t* stx)
|
|
|
|
{
|
2005-05-12 15:25:06 +00:00
|
|
|
xp_stx_word_t symtab, symbol_Smalltalk;
|
2005-05-10 12:00:43 +00:00
|
|
|
xp_stx_word_t symbol_nil, symbol_true, symbol_false;
|
2005-05-10 08:21:10 +00:00
|
|
|
xp_stx_word_t symbol_Symbol, symbol_SymbolMeta;
|
|
|
|
xp_stx_word_t symbol_Metaclass, symbol_MetaclassMeta;
|
|
|
|
xp_stx_word_t class_Symbol, class_SymbolMeta;
|
|
|
|
xp_stx_word_t class_Metaclass, class_MetaclassMeta;
|
2005-05-12 15:33:38 +00:00
|
|
|
xp_stx_word_t class_Object, class_Class;
|
|
|
|
xp_stx_word_t tmp;
|
2005-05-08 11:16:07 +00:00
|
|
|
|
2005-05-10 08:21:10 +00:00
|
|
|
/* allocate three keyword objects */
|
2005-05-08 15:22:45 +00:00
|
|
|
stx->nil = xp_stx_alloc_object (stx, 0);
|
|
|
|
stx->true = xp_stx_alloc_object (stx, 0);
|
|
|
|
stx->false = xp_stx_alloc_object (stx, 0);
|
2005-05-08 10:44:58 +00:00
|
|
|
|
|
|
|
xp_assert (stx->nil == XP_STX_NIL);
|
|
|
|
xp_assert (stx->true == XP_STX_TRUE);
|
|
|
|
xp_assert (stx->false == XP_STX_FALSE);
|
|
|
|
|
2005-05-10 08:21:10 +00:00
|
|
|
/* build a symbol table */ // TODO: symbol table size
|
|
|
|
symtab = xp_stx_alloc_object (stx, 1000);
|
|
|
|
|
|
|
|
/* tweak the initial object structure */
|
|
|
|
symbol_Symbol =
|
|
|
|
xp_stx_alloc_string_object(stx, XP_STX_TEXT("Symbol"));
|
|
|
|
symbol_SymbolMeta =
|
2005-05-12 15:25:06 +00:00
|
|
|
xp_stx_alloc_string_object(stx,XP_STX_TEXT("Symbol class"));
|
2005-05-10 08:21:10 +00:00
|
|
|
symbol_Metaclass =
|
|
|
|
xp_stx_alloc_string_object(stx, XP_STX_TEXT("Metaclass"));
|
|
|
|
symbol_MetaclassMeta =
|
2005-05-12 15:25:06 +00:00
|
|
|
xp_stx_alloc_string_object(stx, XP_STX_TEXT("Metaclass class"));
|
2005-05-10 08:21:10 +00:00
|
|
|
|
2005-05-10 12:00:43 +00:00
|
|
|
class_Metaclass = xp_stx_alloc_object(stx, XP_STX_CLASS_SIZE);
|
|
|
|
class_MetaclassMeta = xp_stx_alloc_object(stx, XP_STX_CLASS_SIZE);
|
|
|
|
class_Symbol = xp_stx_alloc_object(stx, XP_STX_CLASS_SIZE);
|
|
|
|
class_SymbolMeta = xp_stx_alloc_object(stx, XP_STX_CLASS_SIZE);
|
2005-05-08 15:22:45 +00:00
|
|
|
|
2005-05-10 08:21:10 +00:00
|
|
|
XP_STX_CLASS(stx,symbol_SymbolMeta) = class_Symbol;
|
|
|
|
XP_STX_CLASS(stx,symbol_Metaclass) = class_Symbol;
|
|
|
|
XP_STX_CLASS(stx,symbol_MetaclassMeta) = class_Symbol;
|
|
|
|
|
|
|
|
XP_STX_CLASS(stx,class_Symbol) = class_SymbolMeta;
|
|
|
|
XP_STX_CLASS(stx,class_SymbolMeta) = class_Metaclass;
|
|
|
|
XP_STX_CLASS(stx,class_Metaclass) = class_MetaclassMeta;
|
|
|
|
XP_STX_CLASS(stx,class_MetaclassMeta) = class_Metaclass;
|
2005-05-10 12:00:43 +00:00
|
|
|
|
|
|
|
xp_stx_hash_insert (stx, symtab,
|
|
|
|
xp_stx_hash_string_object(stx, symbol_Symbol),
|
|
|
|
symbol_Symbol, class_Symbol);
|
|
|
|
xp_stx_hash_insert (stx, symtab,
|
|
|
|
xp_stx_hash_string_object(stx, symbol_SymbolMeta),
|
|
|
|
symbol_SymbolMeta, class_SymbolMeta);
|
|
|
|
xp_stx_hash_insert (stx, symtab,
|
|
|
|
xp_stx_hash_string_object(stx, symbol_Metaclass),
|
|
|
|
symbol_Metaclass, class_Metaclass);
|
|
|
|
xp_stx_hash_insert (stx, symtab,
|
|
|
|
xp_stx_hash_string_object(stx, symbol_MetaclassMeta),
|
|
|
|
symbol_MetaclassMeta, class_MetaclassMeta);
|
|
|
|
|
2005-05-12 15:25:06 +00:00
|
|
|
/* now ready to use new_symbol & new_class */
|
2005-05-10 16:20:53 +00:00
|
|
|
stx->symbol_table = symtab;
|
|
|
|
stx->class_symbol = class_Symbol;
|
|
|
|
stx->class_metaclass = class_Metaclass;
|
|
|
|
|
2005-05-12 15:25:06 +00:00
|
|
|
/* more initialization for symbol table */
|
|
|
|
stx->class_symbol_link =
|
|
|
|
xp_stx_new_class (stx, XP_STX_TEXT("SymbolLink"));
|
|
|
|
|
|
|
|
xp_stx_hash_traverse (stx, symtab, __reset_symbol_link_class);
|
|
|
|
XP_STX_CLASS(stx,symtab) =
|
|
|
|
xp_stx_new_class (stx, XP_STX_TEXT("Array"));
|
|
|
|
symbol_Smalltalk =
|
|
|
|
xp_stx_new_symbol (stx, XP_STX_TEXT("Smalltalk"));
|
|
|
|
xp_stx_hash_insert (stx, symtab,
|
|
|
|
xp_stx_hash_string_object(stx, symbol_Smalltalk),
|
|
|
|
symbol_Smalltalk, symtab);
|
|
|
|
|
2005-05-10 12:00:43 +00:00
|
|
|
/* more initialization for nil, true, false */
|
2005-05-12 15:25:06 +00:00
|
|
|
symbol_nil = xp_stx_new_symbol (stx, XP_STX_TEXT("nil"));
|
|
|
|
symbol_true = xp_stx_new_symbol (stx, XP_STX_TEXT("true"));
|
|
|
|
symbol_false = xp_stx_new_symbol (stx, XP_STX_TEXT("false"));
|
2005-05-10 12:00:43 +00:00
|
|
|
|
|
|
|
xp_stx_hash_insert (stx, symtab,
|
|
|
|
xp_stx_hash_string_object(stx, symbol_nil),
|
|
|
|
symbol_nil, stx->nil);
|
|
|
|
xp_stx_hash_insert (stx, symtab,
|
|
|
|
xp_stx_hash_string_object(stx, symbol_true),
|
|
|
|
symbol_true, stx->true);
|
|
|
|
xp_stx_hash_insert (stx, symtab,
|
|
|
|
xp_stx_hash_string_object(stx, symbol_false),
|
|
|
|
symbol_false, stx->false);
|
|
|
|
|
2005-05-12 15:25:06 +00:00
|
|
|
XP_STX_CLASS(stx,stx->nil) =
|
|
|
|
xp_stx_new_class (stx, XP_STX_TEXT("UndefinedObject"));
|
|
|
|
XP_STX_CLASS(stx,stx->true) =
|
|
|
|
xp_stx_new_class (stx, XP_STX_TEXT("True"));
|
|
|
|
XP_STX_CLASS(stx,stx->false) =
|
|
|
|
xp_stx_new_class (stx, XP_STX_TEXT("False"));
|
2005-05-08 15:22:45 +00:00
|
|
|
|
2005-05-12 15:33:38 +00:00
|
|
|
/* weave the class-metaclass chain */
|
|
|
|
class_Object = xp_stx_new_class (stx, XP_STX_TEXT("Object"));
|
|
|
|
class_Class = xp_stx_new_class (stx, XP_STX_TEXT("Class"));
|
|
|
|
tmp = XP_STX_CLASS(stx,class_Object);
|
|
|
|
XP_STX_AT(stx,tmp,XP_STX_CLASS_SUPERCLASS) = class_Class;
|
2005-05-08 11:16:07 +00:00
|
|
|
|
2005-05-15 18:37:00 +00:00
|
|
|
stx->class_method = xp_stx_new_class (stx, XP_STX_TEXT("Method"));
|
|
|
|
stx->class_context = xp_stx_new_class (stx, XP_STX_TEXT("Context"));
|
|
|
|
|
2005-05-08 10:44:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-05-08 10:31:25 +00:00
|
|
|
|