-
Notifications
You must be signed in to change notification settings - Fork 0
/
pprinter.c
131 lines (109 loc) · 2.56 KB
/
pprinter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* pprinter.c
*
* implements the pretty printer in pprinter.h
*
*/
#include "pprinter.h"
#include "sina_types.h"
#include <stdio.h>
void pprint_integer(integer_chunk*);
void pprint_symbol(symbol_chunk*);
void pprint_escaped_symbol(escaped_symbol_chunk*);
void pprint_list_head(list_head_chunk*);
void pprint_block(block_chunk*);
void pprint_native(native_chunk*);
void pprint_vm_state(sinavm_data* vm)
{
chunk_header* chunk = (chunk_header*) vm->ds;
printf("ds: "); pprint(chunk); printf("\n");
printf("cs: "); pprint((chunk_header*) vm->cs); printf("\n");
}
void pprint(chunk_header* chunk)
{
chunk_type type = chunk->type;
switch (type)
{
case INTEGER_CHUNK:
pprint_integer((integer_chunk*) chunk);
break;
case SYMBOL_CHUNK:
pprint_symbol((symbol_chunk*) chunk);
break;
case ESCAPED_SYMBOL_CHUNK:
pprint_escaped_symbol(
(escaped_symbol_chunk*) chunk);
break;
case LIST_HEAD_CHUNK:
pprint_list_head((list_head_chunk*) chunk);
break;
case BLOCK_CHUNK:
pprint_block((block_chunk*) chunk);
break;
case NATIVE_CHUNK:
pprint_native((native_chunk*) chunk);
break;
default:
printf("pprint: Bad chunk type: %d\n", type);
}
}
void pprint_integer(integer_chunk* chunk)
{
printf(" %d ", chunk->value);
}
void pprint_symbol(symbol_chunk* chunk)
{
printf(" %s ", symbols_find(chunk->symbol));
}
void pprint_escaped_symbol(escaped_symbol_chunk* chunk)
{
printf(" :%s ", symbols_find(chunk->symbol));
}
void pprint_list_head(list_head_chunk* chunk)
{
printf(" ( ");
list_node_chunk* node;
for (node = chunk->first; node != NULL; node = node->next)
{
pprint((chunk_header*) node->data);
}
printf(" ) ");
}
void pprint_block(block_chunk* chunk)
{
printf(" { ");
list_node_chunk* node;
for (node = chunk->current; node != NULL; node = node->next)
{
pprint((chunk_header*) node->data);
}
printf(" } ");
}
void pprint_native(native_chunk* native)
{
printf("native<%x>", native->func);
}
void pprint_chunk_info(chunk_header* chunk)
{
int type = chunk->type;
int colour = chunk->colour;
/* HACK: LIST_NODE_CHUNK is big enough to hold them all... */
list_node_chunk* node = (list_node_chunk*) chunk;
switch (type)
{
case INTEGER_CHUNK:
case SYMBOL_CHUNK:
case ESCAPED_SYMBOL_CHUNK:
case NATIVE_CHUNK:
printf("<t=%d c=%d v=%d>", type, colour, node->next);
break;
case LIST_HEAD_CHUNK:
case LIST_NODE_CHUNK:
case BLOCK_CHUNK:
printf("<t=%d c=%d v1=%x v2=%x>", type, colour,
node->next, node->data);
default:
printf("pprint_chunk_info: unknown type %d, colour=%d",
type, colour);
}
}