qse/ase/test/stx/stx.c

235 lines
6.3 KiB
C
Raw Normal View History

2005-05-12 15:25:06 +00:00
#include <xp/stx/stx.h>
2005-05-19 16:41:10 +00:00
#ifdef _DOS
#include <stdio.h>
#define xp_printf printf
#else
#include <xp/bas/stdio.h>
2005-05-21 15:55:50 +00:00
#include <xp/bas/locale.h>
2005-05-19 16:41:10 +00:00
#endif
2005-05-06 17:18:29 +00:00
2005-05-23 15:51:03 +00:00
#include <xp/stx/bootstrp.h>
2005-05-15 18:37:00 +00:00
#include <xp/stx/object.h>
2005-05-18 04:01:51 +00:00
#include <xp/stx/symbol.h>
2005-05-15 18:37:00 +00:00
#include <xp/stx/context.h>
2005-05-22 15:24:57 +00:00
#include <xp/stx/class.h>
2005-05-12 15:25:06 +00:00
#include <xp/stx/hash.h>
2005-05-15 18:37:00 +00:00
2005-05-18 04:01:51 +00:00
void print_symbol_names (xp_stx_t* stx, xp_stx_word_t sym)
{
2005-05-20 04:01:12 +00:00
xp_printf (XP_TEXT("%lu [%s]\n"), (unsigned long)sym, &XP_STX_CHARAT(stx,sym,0));
2005-05-18 04:01:51 +00:00
}
void print_symbol_names_2 (xp_stx_t* stx, xp_stx_word_t idx)
2005-05-12 15:25:06 +00:00
{
2005-05-29 16:51:16 +00:00
xp_stx_word_t key = XP_STX_WORDAT(stx,idx,XP_STX_PAIRLINK_KEY);
xp_stx_word_t value = XP_STX_WORDAT(stx,idx,XP_STX_PAIRLINK_VALUE);
2005-05-25 16:44:05 +00:00
xp_printf (XP_TEXT("%lu [%s] %lu\n"),
(unsigned long)key, &XP_STX_CHARAT(stx,key,0), (unsigned long)value);
2005-05-12 15:25:06 +00:00
}
2005-05-29 16:51:16 +00:00
void print_superclasses (xp_stx_t* stx, const xp_char_t* name)
2005-05-26 03:27:44 +00:00
{
xp_stx_word_t n;
xp_stx_class_t* obj;
n = xp_stx_lookup_class (stx, name);
xp_printf (XP_TEXT("Class hierarchy for the class '%s'\n"), name);
while (n != stx->nil) {
obj = (xp_stx_class_t*)XP_STX_WORD_OBJECT(stx,n);
xp_printf (XP_TEXT("%lu, %s\n"),
(unsigned long)obj->name,
XP_STX_DATA(stx, obj->name));
n = obj->superclass;
}
}
2005-05-29 16:51:16 +00:00
void print_metaclass_superclasses (xp_stx_t* stx, const xp_char_t* name)
2005-05-26 03:27:44 +00:00
{
xp_stx_word_t n, x;
xp_stx_metaclass_t* obj;
xp_stx_class_t* xobj;
n = xp_stx_lookup_class (stx, name);
n = XP_STX_CLASS(stx,n);
xp_printf (XP_TEXT("Class hierarchy for the metaclass '%s class'\n"), name);
while (n != stx->nil) {
/*if (n == stx->class_class) break; */
if (XP_STX_CLASS(stx,n) != stx->class_metaclass) break;
obj = (xp_stx_metaclass_t*)XP_STX_WORD_OBJECT(stx,n);
x = obj->instance_class;
xobj = (xp_stx_class_t*)XP_STX_WORD_OBJECT(stx,x);
xp_printf (XP_TEXT("%lu, %s class\n"),
(unsigned long)xobj->name,
XP_STX_DATA(stx, xobj->name));
n = obj->superclass;
}
while (n != stx->nil) {
xobj = (xp_stx_class_t*)XP_STX_WORD_OBJECT(stx,n);
xp_printf (XP_TEXT("%lu, %s\n"),
(unsigned long)xobj->name,
XP_STX_DATA(stx, xobj->name));
n = xobj->superclass;
}
}
2005-05-26 15:39:32 +00:00
void print_class_name (xp_stx_t* stx, xp_stx_word_t class, int tabs)
{
xp_stx_class_t* xobj;
xobj = (xp_stx_class_t*)XP_STX_WORD_OBJECT(stx,class);
while (tabs-- > 0) xp_printf (XP_TEXT(" "));
xp_printf (XP_TEXT("%s [%lu]\n"),
XP_STX_DATA(stx, xobj->name),
2005-05-29 16:51:16 +00:00
(unsigned long)class);
}
void print_metaclass_name (xp_stx_t* stx, xp_stx_word_t class, int tabs)
{
xp_stx_metaclass_t* obj;
xp_stx_class_t* xobj;
obj = (xp_stx_metaclass_t*)XP_STX_WORD_OBJECT(stx,class);
xobj = (xp_stx_class_t*)XP_STX_WORD_OBJECT(stx,obj->instance_class);
while (tabs-- > 0) xp_printf (XP_TEXT(" "));
xp_printf (XP_TEXT("%s class [%lu]\n"),
XP_STX_DATA(stx, xobj->name),
(unsigned long)class);
2005-05-26 15:39:32 +00:00
}
void print_subclass_names (xp_stx_t* stx, xp_stx_word_t class, int tabs)
{
xp_stx_class_t* obj;
obj = (xp_stx_class_t*)XP_STX_WORD_OBJECT(stx,class);
2005-05-29 16:51:16 +00:00
if (obj->header.class == stx->class_metaclass) {
print_metaclass_name (stx, class, tabs);
}
else {
print_class_name (stx, class, tabs);
}
2005-05-26 15:39:32 +00:00
if (obj->subclasses != stx->nil) {
xp_stx_word_t count = XP_STX_SIZE(stx, obj->subclasses);
while (count-- > 0) {
print_subclass_names (stx,
2005-05-29 16:51:16 +00:00
XP_STX_WORDAT(stx,obj->subclasses,count), tabs + 1);
2005-05-26 15:39:32 +00:00
}
}
}
void print_subclasses (xp_stx_t* stx, const xp_char_t* name)
{
xp_stx_word_t class;
class = xp_stx_lookup_class (stx, name);
2005-05-29 16:51:16 +00:00
xp_printf (XP_TEXT("== NORMAL == \n"));
2005-05-26 15:39:32 +00:00
print_subclass_names (stx, class, 0);
2005-05-29 16:51:16 +00:00
xp_printf (XP_TEXT("== META == \n"));
print_subclass_names (stx, XP_STX_CLASS(stx,class), 0);
2005-05-26 15:39:32 +00:00
}
2005-05-15 18:37:00 +00:00
int xp_main (int argc, xp_char_t* argv[])
2005-05-06 17:18:29 +00:00
{
2005-05-08 10:31:25 +00:00
xp_stx_t stx;
2005-05-06 17:18:29 +00:00
xp_stx_word_t i;
2005-05-21 15:55:50 +00:00
#ifndef _DOS
2005-05-20 04:49:08 +00:00
if (xp_setlocale () == -1) {
printf ("cannot set locale\n");
return -1;
}
2005-05-21 15:55:50 +00:00
#endif
2005-05-20 04:49:08 +00:00
2005-05-19 16:41:10 +00:00
if (argc != 2) { /* TODO: argument processing */
2005-05-19 15:08:04 +00:00
xp_printf (XP_TEXT("Usage: %s [-f imageFile] MainClass\n"), argv[0]);
2005-05-15 18:37:00 +00:00
return -1;
}
2005-05-10 08:21:10 +00:00
if (xp_stx_open (&stx, 10000) == XP_NULL) {
2005-05-08 10:44:58 +00:00
xp_printf (XP_TEXT("cannot open stx\n"));
2005-05-06 17:18:29 +00:00
return -1;
}
2005-05-08 10:44:58 +00:00
if (xp_stx_bootstrap(&stx) == -1) {
xp_stx_close (&stx);
xp_printf (XP_TEXT("cannot bootstrap\n"));
return -1;
}
2005-05-08 10:31:25 +00:00
2005-05-20 04:01:12 +00:00
xp_printf (XP_TEXT("stx.nil %lu\n"), (unsigned long)stx.nil);
xp_printf (XP_TEXT("stx.true %lu\n"), (unsigned long)stx.true);
xp_printf (XP_TEXT("stx.false %lu\n"), (unsigned long)stx.false);
2005-05-18 04:01:51 +00:00
xp_printf (XP_TEXT("-------------\n"));
2005-05-08 10:39:40 +00:00
2005-05-18 04:01:51 +00:00
xp_stx_traverse_symbol_table (&stx, print_symbol_names);
xp_printf (XP_TEXT("-------------\n"));
xp_stx_hash_traverse (&stx, stx.smalltalk, print_symbol_names_2);
xp_printf (XP_TEXT("-------------\n"));
2005-05-12 15:25:06 +00:00
2005-05-29 16:51:16 +00:00
print_superclasses (&stx, XP_STX_TEXT("Array"));
2005-05-22 17:02:58 +00:00
xp_printf (XP_TEXT("-------------\n"));
2005-05-29 16:51:16 +00:00
print_metaclass_superclasses (&stx, XP_STX_TEXT("Array"));
2005-05-22 15:24:57 +00:00
xp_printf (XP_TEXT("-------------\n"));
2005-05-29 16:51:16 +00:00
print_superclasses (&stx, XP_STX_TEXT("False"));
2005-05-26 03:27:44 +00:00
xp_printf (XP_TEXT("-------------\n"));
2005-05-29 16:51:16 +00:00
print_metaclass_superclasses (&stx, XP_STX_TEXT("False"));
2005-05-26 03:27:44 +00:00
xp_printf (XP_TEXT("-------------\n"));
2005-05-29 16:51:16 +00:00
print_superclasses (&stx, XP_STX_TEXT("Metaclass"));
2005-05-26 03:27:44 +00:00
xp_printf (XP_TEXT("-------------\n"));
2005-05-29 16:51:16 +00:00
print_metaclass_superclasses (&stx, XP_STX_TEXT("Metaclass"));
2005-05-26 03:27:44 +00:00
xp_printf (XP_TEXT("-------------\n"));
2005-05-29 16:51:16 +00:00
print_superclasses (&stx, XP_STX_TEXT("Class"));
2005-05-26 03:27:44 +00:00
xp_printf (XP_TEXT("-------------\n"));
2005-05-29 16:51:16 +00:00
print_metaclass_superclasses (&stx, XP_STX_TEXT("Class"));
2005-05-25 16:44:05 +00:00
xp_printf (XP_TEXT("-------------\n"));
2005-05-26 15:39:32 +00:00
print_subclasses (&stx, XP_STX_TEXT("Object"));
xp_printf (XP_TEXT("-------------\n"));
2005-05-22 15:24:57 +00:00
2005-05-23 15:51:03 +00:00
#if 0
2005-05-15 18:37:00 +00:00
{
2005-05-23 15:51:03 +00:00
xp_stx_word_t method_name;
2005-05-15 18:37:00 +00:00
xp_stx_word_t main_class;
xp_stx_word_t method, context;
2005-05-06 17:18:29 +00:00
2005-05-15 18:37:00 +00:00
method_name = xp_stx_new_symbol (&stx,XP_STX_TEXT("main"));
2005-05-23 15:51:03 +00:00
main_class = xp_stx_lookup_class (&stx,argv[1]);
if (main_class == stx.nil) {
2005-05-15 18:37:00 +00:00
xp_printf (XP_TEXT("non-existent class: %s\n"), argv[1]);
return -1;
}
2005-05-23 15:51:03 +00:00
/*
2005-05-15 18:37:00 +00:00
method = xp_stx_alloc_byte_object (&stx,100);
XP_STX_CLASS(&stx,method) = stx.class_method;
2005-05-23 15:51:03 +00:00
*/
method = xp_stx_instantiate (&stx, XP_STX_TEXT("Method"));
2005-05-06 17:18:29 +00:00
2005-05-15 18:37:00 +00:00
XP_STX_BYTEAT(&stx,method,0) = PUSH_OBJECT;
XP_STX_BYTEAT(&stx,method,1) = main_class;
XP_STX_BYTEAT(&stx,method,2) = SEND_UNARY_MESSAGE;
XP_STX_BYTEAT(&stx,method,3) = method_name;
XP_STX_BYTEAT(&stx,method,4) = HALT;
2005-05-23 15:51:03 +00:00
/*
2005-05-15 18:37:00 +00:00
context = xp_stx_new_context (&stx, method, stx.nil, stx.nil);
2005-05-23 15:51:03 +00:00
*/
context = xp_stx_instantiate (&stx, XP_STX_TEXT("Context"));
2005-05-15 18:37:00 +00:00
xp_stx_run_context (&stx, context);
2005-05-06 17:18:29 +00:00
}
2005-05-23 15:51:03 +00:00
#endif
2005-05-15 18:37:00 +00:00
2005-05-08 10:31:25 +00:00
xp_stx_close (&stx);
2005-05-20 04:01:12 +00:00
xp_printf (XP_TEXT("== End of program ==\n"));
2005-05-06 17:18:29 +00:00
return 0;
}