qse/ase/lsp/print.c

196 lines
4.8 KiB
C
Raw Normal View History

2005-02-04 15:39:11 +00:00
/*
2006-10-26 08:17:38 +00:00
* $Id: print.c,v 1.16 2006-10-26 08:17:38 bacon Exp $
2005-02-04 15:39:11 +00:00
*/
2006-10-26 08:17:38 +00:00
#include <ase/lsp/lsp_i.h>
2005-02-04 15:39:11 +00:00
2006-10-26 08:17:38 +00:00
#if 0
2006-10-24 04:22:40 +00:00
void ase_lsp_print_debug (ase_lsp_obj_t* obj)
2005-02-04 15:39:11 +00:00
{
2006-10-24 04:22:40 +00:00
switch (ASE_LSP_TYPE(obj)) {
case ASE_LSP_OBJ_NIL:
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T("nil"));
2005-02-04 15:39:11 +00:00
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_TRUE:
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T("t"));
2005-02-04 15:39:11 +00:00
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_INT:
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T("%d"), ASE_LSP_IVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_REAL:
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T("%f"), ASE_LSP_RVALUE(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-25 13:42:31 +00:00
case ASE_LSP_OBJ_SYM:
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T("%s"), ASE_LSP_SYMPTR(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-25 13:42:31 +00:00
case ASE_LSP_OBJ_STR:
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T("%s"), ASE_LSP_STRPTR(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_CONS:
2005-02-04 15:39:11 +00:00
{
2006-10-24 04:22:40 +00:00
ase_lsp_obj_t* p = obj;
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T("("));
2005-02-04 15:39:11 +00:00
do {
2006-10-24 04:22:40 +00:00
ase_lsp_print_debug (ASE_LSP_CAR(p));
p = ASE_LSP_CDR(p);
if (ASE_LSP_TYPE(p) != ASE_LSP_OBJ_NIL) {
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T(" "));
2006-10-24 04:22:40 +00:00
if (ASE_LSP_TYPE(p) != ASE_LSP_OBJ_CONS) {
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T(". "));
2006-10-24 04:22:40 +00:00
ase_lsp_print_debug (p);
2005-02-04 15:39:11 +00:00
}
}
2006-10-24 04:22:40 +00:00
} while (ASE_LSP_TYPE(p) != ASE_LSP_OBJ_NIL && ASE_LSP_TYPE(p) == ASE_LSP_OBJ_CONS);
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T(")"));
2005-02-04 15:39:11 +00:00
}
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_FUNC:
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T("func"));
2005-02-04 15:39:11 +00:00
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_MACRO:
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T("macro"));
2005-02-04 15:39:11 +00:00
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_PRIM:
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T("prim"));
2005-02-04 15:39:11 +00:00
break;
default:
2006-10-26 08:17:38 +00:00
ase_printf (ASE_T("unknown object type: %d"), ASE_LSP_TYPE(obj));
2005-02-04 15:39:11 +00:00
}
}
2006-10-26 08:17:38 +00:00
#endif
2005-02-04 15:39:11 +00:00
2005-09-18 10:23:19 +00:00
#define OUTPUT_STR(lsp,str) \
do { \
2006-10-26 08:17:38 +00:00
if (lsp->output_func(ASE_LSP_IO_WRITE, lsp->output_arg, (ase_char_t*)str, ase_lsp_strlen(str)) == -1) { \
2006-10-24 04:22:40 +00:00
lsp->errnum = ASE_LSP_ERR_OUTPUT; \
2005-09-18 13:23:32 +00:00
return -1; \
} \
} while (0)
#define OUTPUT_STRX(lsp,str,len) \
do { \
2006-10-26 08:17:38 +00:00
if (lsp->output_func(ASE_LSP_IO_WRITE, lsp->output_arg, (ase_char_t*)str, len) == -1) { \
2006-10-24 04:22:40 +00:00
lsp->errnum = ASE_LSP_ERR_OUTPUT; \
2005-09-18 10:23:19 +00:00
return -1; \
} \
} while (0)
2006-10-24 04:22:40 +00:00
static int __print (ase_lsp_t* lsp, const ase_lsp_obj_t* obj, ase_bool_t prt_cons_par)
2005-02-04 15:39:11 +00:00
{
2006-10-24 04:22:40 +00:00
ase_char_t buf[256];
2005-09-18 10:23:19 +00:00
2006-10-24 04:22:40 +00:00
if (lsp->output_func == ASE_NULL) {
lsp->errnum = ASE_LSP_ERR_OUTPUT_NOT_ATTACHED;
2005-09-18 10:23:19 +00:00
return -1;
}
2006-10-24 04:22:40 +00:00
switch (ASE_LSP_TYPE(obj)) {
case ASE_LSP_OBJ_NIL:
2006-10-26 08:17:38 +00:00
OUTPUT_STR (lsp, ASE_T("nil"));
2005-02-04 15:39:11 +00:00
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_TRUE:
2006-10-26 08:17:38 +00:00
OUTPUT_STR (lsp, ASE_T("t"));
2005-02-04 15:39:11 +00:00
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_INT:
2006-10-25 13:42:31 +00:00
if (ase_sizeof(ase_long_t) == ase_sizeof(int)) {
2006-10-26 08:17:38 +00:00
lsp->syscas.sprintf (
buf, ase_countof(buf),
ASE_T("%d"), ASE_LSP_IVALUE(obj));
2005-02-14 14:37:50 +00:00
}
2006-10-26 08:17:38 +00:00
else if (ase_sizeof(ase_long_t) == ase_sizeof(long))
{
lsp->syscas.sprintf (
buf, ase_countof(buf),
ASE_T("%ld"), ASE_LSP_IVALUE(obj));
}
#if defined(__BORLANDC__) || defined(_MSC_VER)
else if (ase_sizeof(ase_long_t) == ase_sizeof(__int64))
{
lsp->syscas.sprintf (
buf, ase_countof(buf),
ASE_T("%I64d"), ASE_LSP_IVALUE(obj));
2005-02-14 14:37:50 +00:00
}
2006-10-26 08:17:38 +00:00
#else
else if (ase_sizeof(ase_long_t) == ase_sizeof(long long))
{
lsp->syscas.sprintf (
buf, ase_countof(buf),
ASE_T("%lld"), ASE_LSP_IVALUE(obj));
2005-02-14 14:37:50 +00:00
}
2006-10-26 08:17:38 +00:00
#endif
2005-09-18 10:23:19 +00:00
OUTPUT_STR (lsp, buf);
2005-02-04 15:39:11 +00:00
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_REAL:
2006-10-25 13:42:31 +00:00
if (ase_sizeof(ase_real_t) == ase_sizeof(double)) {
2006-10-26 08:17:38 +00:00
lsp->syscas.sprintf (buf, ase_countof(buf), ASE_T("%f"),
2006-10-24 04:22:40 +00:00
(double)ASE_LSP_RVALUE(obj));
2005-09-20 11:19:15 +00:00
}
2006-10-25 13:42:31 +00:00
else if (ase_sizeof(ase_real_t) == ase_sizeof(long double)) {
2006-10-26 08:17:38 +00:00
lsp->syscas.sprintf (buf, ase_countof(buf), ASE_T("%Lf"),
2006-10-24 04:22:40 +00:00
(long double)ASE_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-25 13:42:31 +00:00
case ASE_LSP_OBJ_SYM:
2006-10-26 08:17:38 +00:00
OUTPUT_STR (lsp, ASE_LSP_SYMPTR(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-25 13:42:31 +00:00
case ASE_LSP_OBJ_STR:
2006-10-26 08:17:38 +00:00
OUTPUT_STR (lsp, ASE_LSP_STRPTR(obj));
2005-02-04 15:39:11 +00:00
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_CONS:
2005-02-04 15:39:11 +00:00
{
2006-10-24 04:22:40 +00:00
const ase_lsp_obj_t* p = obj;
2006-10-26 08:17:38 +00:00
if (prt_cons_par) OUTPUT_STR (lsp, ASE_T("("));
do
{
2006-10-24 04:22:40 +00:00
ase_lsp_print (lsp, ASE_LSP_CAR(p));
p = ASE_LSP_CDR(p);
2006-10-26 08:17:38 +00:00
if (p != lsp->mem->nil)
{
OUTPUT_STR (lsp, ASE_T(" "));
if (ASE_LSP_TYPE(p) != ASE_LSP_OBJ_CONS)
{
OUTPUT_STR (lsp, ASE_T(". "));
2006-10-24 04:22:40 +00:00
ase_lsp_print (lsp, p);
2005-02-04 15:39:11 +00:00
}
}
2006-10-26 08:17:38 +00:00
}
while (p != lsp->mem->nil && ASE_LSP_TYPE(p) == ASE_LSP_OBJ_CONS);
if (prt_cons_par) OUTPUT_STR (lsp, ASE_T(")"));
2005-02-04 15:39:11 +00:00
}
break;
2006-10-24 04:22:40 +00:00
case ASE_LSP_OBJ_FUNC:
2006-10-26 08:17:38 +00:00
/*OUTPUT_STR (lsp, ASE_T("func"));*/
OUTPUT_STR (lsp, ASE_T("(lambda "));
2006-10-24 04:22:40 +00:00
if (__print (lsp, ASE_LSP_FFORMAL(obj), ase_true) == -1) return -1;
2006-10-26 08:17:38 +00:00
OUTPUT_STR (lsp, ASE_T(" "));
2006-10-24 04:22:40 +00:00
if (__print (lsp, ASE_LSP_FBODY(obj), ase_false) == -1) return -1;
2006-10-26 08:17:38 +00:00
OUTPUT_STR (lsp, ASE_T(")"));
2006-10-24 04:22:40 +00:00
break;
case ASE_LSP_OBJ_MACRO:
2006-10-26 08:17:38 +00:00
/*OUTPUT_STR (lsp, ASE_T("macro"));*/
OUTPUT_STR (lsp, ASE_T("(macro "));
2006-10-24 04:22:40 +00:00
if (__print (lsp, ASE_LSP_FFORMAL(obj), ase_true) == -1) return -1;
2006-10-26 08:17:38 +00:00
OUTPUT_STR (lsp, ASE_T(" "));
2006-10-24 04:22:40 +00:00
if (__print (lsp, ASE_LSP_FBODY(obj), ase_false) == -1) return -1;
2006-10-26 08:17:38 +00:00
OUTPUT_STR (lsp, ASE_T(")"));
2006-10-24 04:22:40 +00:00
break;
case ASE_LSP_OBJ_PRIM:
2006-10-26 08:17:38 +00:00
OUTPUT_STR (lsp, ASE_T("prim"));
2005-02-04 15:39:11 +00:00
break;
default:
2006-10-26 08:17:38 +00:00
lsp->syscas.sprintf (buf, ase_countof(buf),
ASE_T("unknown object type: %d"), ASE_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-24 04:22:40 +00:00
int ase_lsp_print (ase_lsp_t* lsp, const ase_lsp_obj_t* obj)
2005-09-19 16:13:18 +00:00
{
2006-10-24 04:22:40 +00:00
return __print (lsp, obj, ase_true);
2005-09-19 16:13:18 +00:00
}