object printing
This commit is contained in:
parent
fe9a8a7039
commit
9fc5f10fc6
3 changed files with 64 additions and 1 deletions
1
main.c
1
main.c
|
|
@ -9,6 +9,7 @@ int main(void) {
|
||||||
addroot(&p);
|
addroot(&p);
|
||||||
|
|
||||||
collect();
|
collect();
|
||||||
|
println(p);
|
||||||
|
|
||||||
gcfinalize();
|
gcfinalize();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
61
print.c
61
print.c
|
|
@ -1,3 +1,62 @@
|
||||||
|
#include "inttypes.h"
|
||||||
#include "wscm.h"
|
#include "wscm.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
// TODO.
|
void print(O obj);
|
||||||
|
|
||||||
|
void printcons(O obj) {
|
||||||
|
O c = obj;
|
||||||
|
I f = 1;
|
||||||
|
|
||||||
|
printf("(");
|
||||||
|
while (c != NIL && !IMM(c)) {
|
||||||
|
H *h = UNBOX(c);
|
||||||
|
if (h->type != OBJ_CONS) {
|
||||||
|
printf(" . ");
|
||||||
|
print(c);
|
||||||
|
printf(")");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
C *p = (C *)(h + 1);
|
||||||
|
if (!f)
|
||||||
|
printf(" ");
|
||||||
|
f = 0;
|
||||||
|
print(p->head);
|
||||||
|
c = p->tail;
|
||||||
|
}
|
||||||
|
if (c != NIL && !IMM(c)) {
|
||||||
|
printf(" . ");
|
||||||
|
print(c);
|
||||||
|
}
|
||||||
|
printf(")");
|
||||||
|
}
|
||||||
|
|
||||||
|
void print(O obj) {
|
||||||
|
if (obj == NIL) {
|
||||||
|
printf("()");
|
||||||
|
} else if (IMM(obj)) {
|
||||||
|
printf("%" PRIdPTR, ORD(obj));
|
||||||
|
} else {
|
||||||
|
void *x = (void *)UNBOX(obj);
|
||||||
|
if (((const U8 *)x) >= heap.from.start && ((const U8 *)x) < heap.from.end) {
|
||||||
|
H *h = (H *)x;
|
||||||
|
switch (h->type) {
|
||||||
|
case OBJ_CONS:
|
||||||
|
printcons(obj);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("<obj type=%" PRIdPTR " @ %p>", h->type, (void *)h);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If pointer is outside the heap, it's a symbol.
|
||||||
|
S *s = (S *)x;
|
||||||
|
printf("%.*s", (int)s->len, s->data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void println(O obj) {
|
||||||
|
print(obj);
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
|
|
||||||
3
wscm.h
3
wscm.h
|
|
@ -83,3 +83,6 @@ O mksym(const char *str);
|
||||||
|
|
||||||
O cons(O head, O tail);
|
O cons(O head, O tail);
|
||||||
C *uncons(O obj);
|
C *uncons(O obj);
|
||||||
|
|
||||||
|
void print(O obj);
|
||||||
|
void println(O obj);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue