add pic_debug function

This commit is contained in:
Yuichi Nishiwaki 2013-10-10 17:48:01 +09:00
parent 0c382c2bdb
commit b640dc08e8
4 changed files with 47 additions and 0 deletions

View File

@ -19,4 +19,6 @@ pic_value pic_cdr(pic_state *, pic_value);
pic_value pic_intern_cstr(pic_state *, const char *); pic_value pic_intern_cstr(pic_state *, const char *);
void pic_debug(pic_state *, pic_value);
#endif #endif

View File

@ -14,6 +14,9 @@ typedef struct {
} pic_value; } pic_value;
enum pic_tt { enum pic_tt {
/* immediate */
PIC_TT_NIL,
/* heap */
PIC_TT_PAIR, PIC_TT_PAIR,
PIC_TT_SYMBOL PIC_TT_SYMBOL
}; };
@ -36,6 +39,11 @@ struct pic_symbol {
char *name; char *name;
}; };
#define pic_pair_ptr(o) ((struct pic_pair *)o->u->data)
#define pic_symbol_ptr(o) ((struct pic_symbol *)o->u->data)
enum pic_tt pic_type(pic_value v);
pic_value pic_nil_value(); pic_value pic_nil_value();
pic_value pic_obj_value(struct pic_object *obj); pic_value pic_obj_value(struct pic_object *obj);

View File

@ -1,5 +1,16 @@
#include "picrin.h" #include "picrin.h"
enum pic_tt
pic_type(pic_value v)
{
switch (v->type) {
case PIC_VTYPE_NIL:
return PIC_TT_NIL;
case PIC_VTYPE_HEAP:
return v->u->data->tt;
}
}
pic_value pic_value
pic_nil_value() pic_nil_value()
{ {

26
src/write.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include "picrin.h"
void
pic_debug(pic_state *pic, pic_value obj)
{
switch (pic_type(obj)) {
case PIC_TT_NIL:
printf("()");
break;
case PIC_TT_PAIR:
printf("(");
pic_debug(pic, pic_car(pic, obj));
printf(" . ");
pic_debug(pic, pic_cdr(pic, obj));
printf(")");
break;
case PIC_TT_SYMBOL:
printf("%s", pic_symbol_ptr(obj)->name);
break;
default:
abort();
}
}