qse/ase/lsp/print.c

153 lines
3.7 KiB
C
Raw Normal View History

2005-02-04 15:39:11 +00:00
/*
2005-09-18 13:23:32 +00:00
* $Id: print.c,v 1.9 2005-09-18 13:23:32 bacon Exp $
2005-02-04 15:39:11 +00:00
*/
2005-09-18 10:23:19 +00:00
#include <xp/lsp/lsp.h>
#include <xp/bas/stdio.h>
2005-09-18 13:06:43 +00:00
#include <xp/bas/string.h>
2005-02-04 15:39:11 +00:00
2005-09-18 10:23:19 +00:00
void xp_lsp_print_debug (xp_lsp_obj_t* obj)
2005-02-04 15:39:11 +00:00
{
2005-09-18 10:23:19 +00:00
switch (XP_LSP_TYPE(obj)) {
case XP_LSP_OBJ_NIL:
2005-02-04 16:23:34 +00:00
xp_printf (XP_TEXT("nil"));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_TRUE:
2005-02-04 16:23:34 +00:00
xp_printf (XP_TEXT("t"));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_INT:
xp_printf (XP_TEXT("%d"), XP_LSP_IVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_FLOAT:
xp_printf (XP_TEXT("%f"), XP_LSP_FVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_SYMBOL:
xp_printf (XP_TEXT("%s"), XP_LSP_SYMVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_STRING:
xp_printf (XP_TEXT("%s"), XP_LSP_STRVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_CONS:
2005-02-04 15:39:11 +00:00
{
2005-09-18 10:23:19 +00:00
xp_lsp_obj_t* p = obj;
2005-02-04 16:23:34 +00:00
xp_printf (XP_TEXT("("));
2005-02-04 15:39:11 +00:00
do {
2005-09-18 10:23:19 +00:00
xp_lsp_print_debug (XP_LSP_CAR(p));
p = XP_LSP_CDR(p);
if (XP_LSP_TYPE(p) != XP_LSP_OBJ_NIL) {
2005-02-04 16:23:34 +00:00
xp_printf (XP_TEXT(" "));
2005-09-18 10:23:19 +00:00
if (XP_LSP_TYPE(p) != XP_LSP_OBJ_CONS) {
2005-02-04 16:23:34 +00:00
xp_printf (XP_TEXT(". "));
2005-09-18 10:23:19 +00:00
xp_lsp_print_debug (p);
2005-02-04 15:39:11 +00:00
}
}
2005-09-18 10:23:19 +00:00
} while (XP_LSP_TYPE(p) != XP_LSP_OBJ_NIL && XP_LSP_TYPE(p) == XP_LSP_OBJ_CONS);
2005-02-04 16:23:34 +00:00
xp_printf (XP_TEXT(")"));
2005-02-04 15:39:11 +00:00
}
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_FUNC:
2005-02-04 16:23:34 +00:00
xp_printf (XP_TEXT("func"));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_MACRO:
2005-02-04 16:23:34 +00:00
xp_printf (XP_TEXT("macro"));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_PRIM:
2005-02-04 16:23:34 +00:00
xp_printf (XP_TEXT("prim"));
2005-02-04 15:39:11 +00:00
break;
default:
2005-09-18 10:23:19 +00:00
xp_printf (XP_TEXT("unknown object type: %d"), XP_LSP_TYPE(obj));
2005-02-04 15:39:11 +00:00
}
}
2005-09-18 10:23:19 +00:00
#define OUTPUT_STR(lsp,str) \
do { \
2005-09-18 13:23:32 +00:00
if (lsp->output_func(XP_LSP_IO_DATA, lsp->output_arg, (xp_char_t*)str, xp_strlen(str)) == -1) { \
lsp->errnum = XP_LSP_ERR_OUTPUT; \
return -1; \
} \
} while (0)
#define OUTPUT_STRX(lsp,str,len) \
do { \
if (lsp->output_func(XP_LSP_IO_DATA, lsp->output_arg, (xp_char_t*)str, len) == -1) { \
2005-09-18 10:23:19 +00:00
lsp->errnum = XP_LSP_ERR_OUTPUT; \
return -1; \
} \
} while (0)
int xp_lsp_print (xp_lsp_t* lsp, const xp_lsp_obj_t* obj)
2005-02-04 15:39:11 +00:00
{
2005-09-18 10:23:19 +00:00
xp_char_t buf[256];
2005-09-18 13:23:32 +00:00
if (lsp->output_func == XP_NULL) {
2005-09-18 10:23:19 +00:00
lsp->errnum = XP_LSP_ERR_OUTPUT_NOT_ATTACHED;
return -1;
}
switch (XP_LSP_TYPE(obj)) {
case XP_LSP_OBJ_NIL:
OUTPUT_STR (lsp, XP_TEXT("nil"));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_TRUE:
OUTPUT_STR (lsp, XP_TEXT("t"));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_INT:
if (xp_sizeof(xp_lsp_int_t) == xp_sizeof(int)) {
xp_sprintf (buf, xp_countof(buf), XP_TEXT("%d"), XP_LSP_IVALUE(obj));
2005-02-14 14:37:50 +00:00
}
2005-09-18 10:23:19 +00:00
else if (xp_sizeof(xp_lsp_int_t) == xp_sizeof(long)) {
xp_sprintf (buf, xp_countof(buf), XP_TEXT("%ld"), XP_LSP_IVALUE(obj));
2005-02-14 14:37:50 +00:00
}
2005-09-18 10:23:19 +00:00
else if (xp_sizeof(xp_lsp_int_t) == xp_sizeof(long long)) {
xp_sprintf (buf, xp_countof(buf), XP_TEXT("%lld"), XP_LSP_IVALUE(obj));
2005-02-14 14:37:50 +00:00
}
2005-09-18 10:23:19 +00:00
OUTPUT_STR (lsp, buf);
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_FLOAT:
xp_sprintf (buf, xp_countof(buf), XP_TEXT("%f"), XP_LSP_FVALUE(obj));
OUTPUT_STR (lsp, buf);
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_SYMBOL:
OUTPUT_STR (lsp, XP_LSP_SYMVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_STRING:
OUTPUT_STR (lsp, XP_LSP_STRVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_CONS:
2005-02-04 15:39:11 +00:00
{
2005-09-18 10:23:19 +00:00
const xp_lsp_obj_t* p = obj;
OUTPUT_STR (lsp, XP_TEXT("("));
2005-02-04 15:39:11 +00:00
do {
2005-09-18 10:23:19 +00:00
xp_lsp_print (lsp, XP_LSP_CAR(p));
p = XP_LSP_CDR(p);
2005-02-04 15:39:11 +00:00
if (p != lsp->mem->nil) {
2005-09-18 10:23:19 +00:00
OUTPUT_STR (lsp, XP_TEXT(" "));
if (XP_LSP_TYPE(p) != XP_LSP_OBJ_CONS) {
OUTPUT_STR (lsp, XP_TEXT(". "));
xp_lsp_print (lsp, p);
2005-02-04 15:39:11 +00:00
}
}
2005-09-18 10:23:19 +00:00
} while (p != lsp->mem->nil && XP_LSP_TYPE(p) == XP_LSP_OBJ_CONS);
OUTPUT_STR (lsp, XP_TEXT(")"));
2005-02-04 15:39:11 +00:00
}
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_FUNC:
OUTPUT_STR (lsp, XP_TEXT("func"));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_MACRO:
OUTPUT_STR (lsp, XP_TEXT("macro"));
2005-02-04 15:39:11 +00:00
break;
2005-09-18 10:23:19 +00:00
case XP_LSP_OBJ_PRIM:
OUTPUT_STR (lsp, XP_TEXT("prim"));
2005-02-04 15:39:11 +00:00
break;
default:
2005-09-18 10:23:19 +00:00
xp_sprintf (buf, xp_countof(buf),
XP_TEXT("unknown object type: %d"), XP_LSP_TYPE(obj));
OUTPUT_STR (lsp, buf);
2005-02-04 15:39:11 +00:00
}
2005-09-18 10:23:19 +00:00
return 0;
2005-02-04 15:39:11 +00:00
}