-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprint.c
More file actions
241 lines (226 loc) · 5.17 KB
/
Copy pathprint.c
File metadata and controls
241 lines (226 loc) · 5.17 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* Copyright (C) 2026 HardenedLinux Community
* Nala Ginrut <roy@hardenedlinux.org>
* Animula is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* Animula is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "print.h"
static inline void list_printer (const object_t obj)
{
ListHead head = ((list_t)obj->value)->list;
list_node_t node = NULL;
os_printk ("(");
SLIST_FOREACH (node, &head, next)
{
object_printer (node->obj);
if (SLIST_NEXT (node, next))
os_printk (" ");
}
os_printk (")");
}
// bytevector and mut_bytevector use the same printer function
static inline void bytevector_printer (const object_t obj)
{
bytevector_t bv = (bytevector_t)obj->value;
os_printk ("#u8(");
if (0 == bv->size)
{
os_printk (")");
return;
}
for (u16_t i = 0; i < (bv->size - 1); i++)
{
os_printk ("%u ", bv->vec[i]);
}
os_printk ("%u)", bv->vec[bv->size - 1]);
}
static inline void pair_printer (const object_t obj)
{
os_printk ("(");
object_printer (((pair_t)obj->value)->car);
os_printk (" . ");
object_printer (((pair_t)obj->value)->cdr);
os_printk (")");
}
static inline void vector_printer (const object_t obj)
{
vector_t v = (vector_t)obj->value;
os_printk ("#(");
for (u16_t i = 0; i < v->size; i++)
{
object_printer (v->vec[i]);
if (i < v->size - 1)
os_printk (" ");
}
os_printk (")");
}
static inline void rational_printer (const object_t obj)
{
hov_t value = (hov_t)obj->value;
numerator_t n = (numerator_t) (value >> 16);
denominator_t d = (denominator_t) (value & 0xffff);
if (obj->attr.type == rational_neg)
{
os_printk ("-%d/%d", n > 0 ? n : -n, d);
}
else
{
os_printk ("%d/%d", n > 0 ? n : -n, d);
}
}
static inline void complex_exact_printer (const object_t obj)
{
hov_t value = (hov_t)obj->value;
real_part_t r = (real_part_t) (value >> 16);
imag_part_t i = (imag_part_t) (value & 0xffff);
if (i >= 0)
{
os_printk ("%d+%di", r, i);
}
else
{
os_printk ("%d%di", r, i);
}
}
static inline void complex_inexact_printer (const object_t obj)
{
real_t r = {.v = read_uintptr_from_ptr ((char *)obj->value)};
real_t i = {.v = read_uintptr_from_ptr ((char *)obj->value + 4)};
if (i.f >= 0)
{
os_printk ("%.1f+%.1fi", r.f, i.f);
}
else
{
os_printk ("%.1f%.1fi", r.f, i.f);
}
}
void object_printer (const object_t obj)
{
if (NULL == obj)
{
os_printk ("NULL");
return;
}
switch (obj->attr.type)
{
case imm_int:
{
os_printk ("%d", (imm_int_t)obj->value);
break;
}
case string:
case mut_string:
case symbol:
{
os_printk ("%s", (char *)obj->value);
break;
}
case keyword:
{
os_printk ("#:%s", (char *)obj->value);
break;
}
case character:
{
os_printk ("#\\%c", (char)obj->value);
break;
}
case primitive:
{
u16_t pn = (u16_t)obj->value;
os_printk ("<primitive: %s %d>", prim_name (pn), pn);
break;
}
case closure_on_heap:
case closure_on_stack:
{
os_printk ("<closure: %p>", obj->value);
break;
}
case procedure:
{
os_printk ("<procedure: %p>", obj->value);
break;
}
case pair:
{
pair_printer (obj);
break;
}
case list:
{
list_printer (obj);
break;
}
case vector:
{
vector_printer (obj);
break;
}
case boolean:
{
os_printk ("#%s", obj->value ? "true" : "false");
break;
}
case real:
{
real_t r = {.v = (uintptr_t)obj->value};
// TODO: if number is too big, how to display
if (r.f > -0.001 && r.f < 0.001)
{
os_printk ("%e", r.f);
}
else
{
os_printk ("%f", r.f);
}
break;
}
case rational_pos:
case rational_neg:
{
rational_printer (obj);
break;
}
case complex_exact:
{
complex_exact_printer (obj);
break;
}
case complex_inexact:
{
complex_inexact_printer (obj);
break;
}
case mut_bytevector:
case bytevector:
{
bytevector_printer (obj);
break;
}
case null_obj:
{
os_printk ("()");
break;
}
case none:
{
os_printk ("<unspecified>");
break;
}
default:
{
os_printk ("object_print: Invalid object type %d\n", obj->attr.type);
PANIC ("VM stop!");
}
}
}