qse/ase/lsp/print.c

175 lines
4.7 KiB
C
Raw Normal View History

2005-02-04 15:39:11 +00:00
/*
2006-10-22 13:10:46 +00:00
* $Id: print.c,v 1.13 2006-10-22 13:10:46 bacon Exp $
2005-02-04 15:39:11 +00:00
*/
2006-10-22 13:10:46 +00:00
#include <sse/lsp/lsp.h>
#include <sse/bas/stdio.h>
#include <sse/bas/string.h>
2005-02-04 15:39:11 +00:00
2006-10-22 13:10:46 +00:00
void sse_lsp_print_debug (sse_lsp_obj_t* obj)
2005-02-04 15:39:11 +00:00
{
2006-10-22 13:10:46 +00:00
switch (SSE_LSP_TYPE(obj)) {
case SSE_LSP_OBJ_NIL:
sse_printf (SSE_TEXT("nil"));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_TRUE:
sse_printf (SSE_TEXT("t"));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_INT:
sse_printf (SSE_TEXT("%d"), SSE_LSP_IVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_REAL:
sse_printf (SSE_TEXT("%f"), SSE_LSP_RVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_SYMBOL:
sse_printf (SSE_TEXT("%s"), SSE_LSP_SYMVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_STRING:
sse_printf (SSE_TEXT("%s"), SSE_LSP_STRVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_CONS:
2005-02-04 15:39:11 +00:00
{
2006-10-22 13:10:46 +00:00
sse_lsp_obj_t* p = obj;
sse_printf (SSE_TEXT("("));
2005-02-04 15:39:11 +00:00
do {
2006-10-22 13:10:46 +00:00
sse_lsp_print_debug (SSE_LSP_CAR(p));
p = SSE_LSP_CDR(p);
if (SSE_LSP_TYPE(p) != SSE_LSP_OBJ_NIL) {
sse_printf (SSE_TEXT(" "));
if (SSE_LSP_TYPE(p) != SSE_LSP_OBJ_CONS) {
sse_printf (SSE_TEXT(". "));
sse_lsp_print_debug (p);
2005-02-04 15:39:11 +00:00
}
}
2006-10-22 13:10:46 +00:00
} while (SSE_LSP_TYPE(p) != SSE_LSP_OBJ_NIL && SSE_LSP_TYPE(p) == SSE_LSP_OBJ_CONS);
sse_printf (SSE_TEXT(")"));
2005-02-04 15:39:11 +00:00
}
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_FUNC:
sse_printf (SSE_TEXT("func"));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_MACRO:
sse_printf (SSE_TEXT("macro"));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_PRIM:
sse_printf (SSE_TEXT("prim"));
2005-02-04 15:39:11 +00:00
break;
default:
2006-10-22 13:10:46 +00:00
sse_printf (SSE_TEXT("unknown object type: %d"), SSE_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 { \
2006-10-22 13:10:46 +00:00
if (lsp->output_func(SSE_LSP_IO_DATA, lsp->output_arg, (sse_char_t*)str, sse_strlen(str)) == -1) { \
lsp->errnum = SSE_LSP_ERR_OUTPUT; \
2005-09-18 13:23:32 +00:00
return -1; \
} \
} while (0)
#define OUTPUT_STRX(lsp,str,len) \
do { \
2006-10-22 13:10:46 +00:00
if (lsp->output_func(SSE_LSP_IO_DATA, lsp->output_arg, (sse_char_t*)str, len) == -1) { \
lsp->errnum = SSE_LSP_ERR_OUTPUT; \
2005-09-18 10:23:19 +00:00
return -1; \
} \
} while (0)
2006-10-22 13:10:46 +00:00
static int __print (sse_lsp_t* lsp, const sse_lsp_obj_t* obj, sse_bool_t prt_cons_par)
2005-02-04 15:39:11 +00:00
{
2006-10-22 13:10:46 +00:00
sse_char_t buf[256];
2005-09-18 10:23:19 +00:00
2006-10-22 13:10:46 +00:00
if (lsp->output_func == SSE_NULL) {
lsp->errnum = SSE_LSP_ERR_OUTPUT_NOT_ATTACHED;
2005-09-18 10:23:19 +00:00
return -1;
}
2006-10-22 13:10:46 +00:00
switch (SSE_LSP_TYPE(obj)) {
case SSE_LSP_OBJ_NIL:
OUTPUT_STR (lsp, SSE_TEXT("nil"));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_TRUE:
OUTPUT_STR (lsp, SSE_TEXT("t"));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_INT:
if (sse_sizeof(sse_lsp_int_t) == sse_sizeof(int)) {
sse_sprintf (buf, sse_countof(buf), SSE_TEXT("%d"), SSE_LSP_IVALUE(obj));
2005-02-14 14:37:50 +00:00
}
2006-10-22 13:10:46 +00:00
else if (sse_sizeof(sse_lsp_int_t) == sse_sizeof(long)) {
sse_sprintf (buf, sse_countof(buf), SSE_TEXT("%ld"), SSE_LSP_IVALUE(obj));
2005-02-14 14:37:50 +00:00
}
2006-10-22 13:10:46 +00:00
else if (sse_sizeof(sse_lsp_int_t) == sse_sizeof(long long)) {
sse_sprintf (buf, sse_countof(buf), SSE_TEXT("%lld"), SSE_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;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_REAL:
if (sse_sizeof(sse_lsp_real_t) == sse_sizeof(double)) {
sse_sprintf (buf, sse_countof(buf), SSE_TEXT("%f"),
(double)SSE_LSP_RVALUE(obj));
2005-09-20 11:19:15 +00:00
}
2006-10-22 13:10:46 +00:00
else if (sse_sizeof(sse_lsp_real_t) == sse_sizeof(long double)) {
sse_sprintf (buf, sse_countof(buf), SSE_TEXT("%Lf"),
(long double)SSE_LSP_RVALUE(obj));
2005-09-20 11:19:15 +00:00
}
2005-09-18 10:23:19 +00:00
OUTPUT_STR (lsp, buf);
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_SYMBOL:
OUTPUT_STR (lsp, SSE_LSP_SYMVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_STRING:
OUTPUT_STR (lsp, SSE_LSP_STRVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_CONS:
2005-02-04 15:39:11 +00:00
{
2006-10-22 13:10:46 +00:00
const sse_lsp_obj_t* p = obj;
if (prt_cons_par) OUTPUT_STR (lsp, SSE_TEXT("("));
2005-02-04 15:39:11 +00:00
do {
2006-10-22 13:10:46 +00:00
sse_lsp_print (lsp, SSE_LSP_CAR(p));
p = SSE_LSP_CDR(p);
2005-02-04 15:39:11 +00:00
if (p != lsp->mem->nil) {
2006-10-22 13:10:46 +00:00
OUTPUT_STR (lsp, SSE_TEXT(" "));
if (SSE_LSP_TYPE(p) != SSE_LSP_OBJ_CONS) {
OUTPUT_STR (lsp, SSE_TEXT(". "));
sse_lsp_print (lsp, p);
2005-02-04 15:39:11 +00:00
}
}
2006-10-22 13:10:46 +00:00
} while (p != lsp->mem->nil && SSE_LSP_TYPE(p) == SSE_LSP_OBJ_CONS);
if (prt_cons_par) OUTPUT_STR (lsp, SSE_TEXT(")"));
2005-02-04 15:39:11 +00:00
}
break;
2006-10-22 13:10:46 +00:00
case SSE_LSP_OBJ_FUNC:
/*OUTPUT_STR (lsp, SSE_TEXT("func"));*/
OUTPUT_STR (lsp, SSE_TEXT("(lambda "));
if (__print (lsp, SSE_LSP_FFORMAL(obj), sse_true) == -1) return -1;
OUTPUT_STR (lsp, SSE_TEXT(" "));
if (__print (lsp, SSE_LSP_FBODY(obj), sse_false) == -1) return -1;
OUTPUT_STR (lsp, SSE_TEXT(")"));
break;
case SSE_LSP_OBJ_MACRO:
/*OUTPUT_STR (lsp, SSE_TEXT("macro"));*/
OUTPUT_STR (lsp, SSE_TEXT("(macro "));
if (__print (lsp, SSE_LSP_FFORMAL(obj), sse_true) == -1) return -1;
OUTPUT_STR (lsp, SSE_TEXT(" "));
if (__print (lsp, SSE_LSP_FBODY(obj), sse_false) == -1) return -1;
OUTPUT_STR (lsp, SSE_TEXT(")"));
break;
case SSE_LSP_OBJ_PRIM:
OUTPUT_STR (lsp, SSE_TEXT("prim"));
2005-02-04 15:39:11 +00:00
break;
default:
2006-10-22 13:10:46 +00:00
sse_sprintf (buf, sse_countof(buf),
SSE_TEXT("unknown object type: %d"), SSE_LSP_TYPE(obj));
2005-09-18 10:23:19 +00:00
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
}
2006-10-22 13:10:46 +00:00
int sse_lsp_print (sse_lsp_t* lsp, const sse_lsp_obj_t* obj)
2005-09-19 16:13:18 +00:00
{
2006-10-22 13:10:46 +00:00
return __print (lsp, obj, sse_true);
2005-09-19 16:13:18 +00:00
}