Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | Copyright 2018, 2020 - 2024 Joel Svensson svenssonjoel@yahoo.se | ||
3 | 2022 Benjamin Vedder | ||
4 | |||
5 | This program is free software: you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation, either version 3 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | #include <stdio.h> | ||
20 | #include <string.h> | ||
21 | #include <ctype.h> | ||
22 | #include <inttypes.h> | ||
23 | #include <lbm_types.h> | ||
24 | #include <lbm_custom_type.h> | ||
25 | |||
26 | #include "print.h" | ||
27 | #include "heap.h" | ||
28 | #include "symrepr.h" | ||
29 | #include "stack.h" | ||
30 | #include "lbm_channel.h" | ||
31 | |||
32 | #define PRINT 1 | ||
33 | #define PRINT_SPACE 2 | ||
34 | #define START_LIST 3 | ||
35 | #define CONTINUE_LIST 4 | ||
36 | #define END_LIST 5 | ||
37 | #define PRINT_DOT 6 | ||
38 | #define START_ARRAY 7 | ||
39 | #define CONTINUE_ARRAY 8 | ||
40 | #define END_ARRAY 9 | ||
41 | |||
42 | static lbm_stack_t print_stack = { NULL, 0, 0, 0}; | ||
43 | static bool print_has_stack = false; | ||
44 | |||
45 | const char *failed_str = "Error: print failed\n"; | ||
46 | |||
47 | 38907 | static int push_n(lbm_stack_t *s, lbm_uint *values, lbm_uint n) { | |
48 |
1/2✓ Branch 0 taken 38907 times.
✗ Branch 1 not taken.
|
38907 | if (s->sp + n < s->size) { |
49 |
2/2✓ Branch 0 taken 77870 times.
✓ Branch 1 taken 38907 times.
|
116777 | for (lbm_uint i = 0; i < n; i ++) { |
50 | 77870 | s->data[s->sp+i] = values[i]; | |
51 | } | ||
52 | 38907 | s->sp+=n; | |
53 | 38907 | return 1; | |
54 | } | ||
55 | ✗ | return 0; | |
56 | } | ||
57 | |||
58 | 1092 | bool lbm_value_is_printable_string(lbm_value v, char **str) { | |
59 | 1092 | bool is_a_string = false; | |
60 |
2/2✓ Branch 1 taken 448 times.
✓ Branch 2 taken 644 times.
|
1092 | if (lbm_is_array_r(v)) { |
61 | 448 | lbm_array_header_t *array = (lbm_array_header_t*)lbm_car(v); | |
62 | |||
63 | 448 | is_a_string = true; | |
64 | // TODO: Potential null deref. | ||
65 | // Highly unlikely that array is a recognizable NULL though. | ||
66 | // If it is incorrect, it is most likely arbitrary. | ||
67 | 448 | char *c_data = (char *)array->data; | |
68 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 392 times.
|
448 | if (array->size == 1) { |
69 | 56 | *str = c_data; | |
70 | 56 | return c_data[0] == 0; | |
71 | } | ||
72 | unsigned int i; | ||
73 |
2/2✓ Branch 0 taken 1988 times.
✓ Branch 1 taken 224 times.
|
2212 | for (i = 0; i < array->size; i ++) { |
74 |
4/4✓ Branch 0 taken 196 times.
✓ Branch 1 taken 1792 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 28 times.
|
1988 | if (c_data[i] == 0 && i > 0) break; |
75 |
3/4✓ Branch 0 taken 840 times.
✓ Branch 1 taken 980 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 840 times.
|
1820 | if (!isprint((unsigned char)c_data[i]) && !iscntrl((unsigned char)c_data[i])) { |
76 | ✗ | is_a_string = false; | |
77 | ✗ | break; | |
78 | } | ||
79 | } | ||
80 | |||
81 |
2/2✓ Branch 0 taken 224 times.
✓ Branch 1 taken 168 times.
|
392 | if (i == array->size) i--; |
82 |
3/4✓ Branch 0 taken 392 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 224 times.
✓ Branch 3 taken 168 times.
|
392 | if (i > 0 && c_data[i] != 0) is_a_string = false; |
83 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 224 times.
|
392 | if (is_a_string) { |
84 | 168 | *str = (char*)array->data; | |
85 | } | ||
86 | } | ||
87 | 1036 | return is_a_string; | |
88 | } | ||
89 | |||
90 | |||
91 | 17444 | int lbm_print_init(lbm_uint print_stack_size) { | |
92 | |||
93 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17444 times.
|
17444 | if (print_stack_size == 0) |
94 | ✗ | return 0; | |
95 | |||
96 | 17444 | lbm_uint *print_stack_storage = (lbm_uint*)lbm_malloc(print_stack_size * sizeof(lbm_uint)); | |
97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17444 times.
|
17444 | if (!print_stack_storage) return 0; |
98 | |||
99 |
1/2✓ Branch 1 taken 17444 times.
✗ Branch 2 not taken.
|
17444 | if (lbm_stack_create(&print_stack, print_stack_storage, print_stack_size)) { |
100 | 17444 | print_has_stack = true; | |
101 | 17444 | return 1; | |
102 | } | ||
103 | ✗ | return 0; | |
104 | } | ||
105 | |||
106 | #define EMIT_BUFFER_SIZE 30 | ||
107 | |||
108 | #define EMIT_FAILED -1 | ||
109 | #define EMIT_OK 0 | ||
110 | |||
111 | 37679 | static int print_emit_string(lbm_char_channel_t *chan, char* str) { | |
112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37679 times.
|
37679 | if (str == NULL) return EMIT_FAILED; |
113 |
2/2✓ Branch 0 taken 44193 times.
✓ Branch 1 taken 37675 times.
|
81868 | while (*str != 0) { |
114 | 44193 | int r = lbm_channel_write(chan, *str); | |
115 | 44193 | str++; | |
116 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 44189 times.
|
44193 | if (r != CHANNEL_SUCCESS) return EMIT_FAILED; |
117 | } | ||
118 | 37675 | return EMIT_OK; | |
119 | } | ||
120 | |||
121 | 2120 | static int print_emit_char(lbm_char_channel_t *chan, char c) { | |
122 | |||
123 | 2120 | int r = lbm_channel_write(chan, c); | |
124 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2116 times.
|
2120 | if (r != CHANNEL_SUCCESS) return EMIT_FAILED; |
125 | 2116 | return EMIT_OK; | |
126 | } | ||
127 | |||
128 | |||
129 | ✗ | static int emit_escape(lbm_char_channel_t *chan, char c) { | |
130 | ✗ | switch(c) { | |
131 | ✗ | case '"': return print_emit_string(chan, "\\\""); | |
132 | ✗ | case '\n': return print_emit_string(chan, "\\n"); | |
133 | ✗ | case '\r': return print_emit_string(chan, "\\r"); | |
134 | ✗ | case '\t': return print_emit_string(chan, "\\t"); | |
135 | ✗ | case '\\': return print_emit_string(chan, "\\\\"); | |
136 | ✗ | default: | |
137 | ✗ | return print_emit_char(chan, c); | |
138 | } | ||
139 | } | ||
140 | |||
141 | 28 | static int print_emit_string_value(lbm_char_channel_t *chan, char* str) { | |
142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (str == NULL) return EMIT_FAILED; |
143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | while (*str != 0) { |
144 | ✗ | int r = emit_escape(chan, *str++); | |
145 | ✗ | if (r != EMIT_OK) return r; | |
146 | } | ||
147 | 28 | return EMIT_OK; | |
148 | } | ||
149 | |||
150 | 35683 | static int print_emit_symbol(lbm_char_channel_t *chan, lbm_value sym) { | |
151 | 35683 | char *str_ptr = (char*)lbm_get_name_by_symbol(lbm_dec_sym(sym)); | |
152 | 35683 | return print_emit_string(chan, str_ptr); | |
153 | } | ||
154 | |||
155 | 1016 | static int print_emit_i(lbm_char_channel_t *chan, lbm_int v) { | |
156 | char buf[EMIT_BUFFER_SIZE]; | ||
157 | 1016 | snprintf(buf, EMIT_BUFFER_SIZE, "%"PRI_INT, v); | |
158 | 1016 | return print_emit_string(chan, buf); | |
159 | } | ||
160 | |||
161 | 84 | static int print_emit_u(lbm_char_channel_t *chan, lbm_uint v, bool ps) { | |
162 | char buf[EMIT_BUFFER_SIZE]; | ||
163 |
1/2✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 | snprintf(buf, EMIT_BUFFER_SIZE, "%"PRI_UINT"%s", v, ps ? "u" : ""); |
164 | 84 | return print_emit_string(chan, buf); | |
165 | } | ||
166 | |||
167 | 476 | static int print_emit_byte(lbm_char_channel_t *chan, uint8_t v, bool ps) { | |
168 | char buf[EMIT_BUFFER_SIZE]; | ||
169 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 420 times.
|
476 | snprintf(buf, EMIT_BUFFER_SIZE, "%u%s", v, ps ? "b" : ""); |
170 | 476 | return print_emit_string(chan, buf); | |
171 | } | ||
172 | |||
173 | 56 | static int print_emit_float(lbm_char_channel_t *chan, float v, bool ps) { | |
174 | char buf[EMIT_BUFFER_SIZE]; | ||
175 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf, EMIT_BUFFER_SIZE, "%"PRI_FLOAT"%s", (double)v, ps ? "f32" : ""); |
176 | 56 | return print_emit_string(chan, buf); | |
177 | } | ||
178 | |||
179 | 56 | static int print_emit_double(lbm_char_channel_t *chan, double v, bool ps) { | |
180 | char buf[EMIT_BUFFER_SIZE]; | ||
181 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf, EMIT_BUFFER_SIZE, "%lf%s", v, ps ? "f64" : ""); |
182 | 56 | return print_emit_string(chan, buf); | |
183 | } | ||
184 | |||
185 | 56 | static int print_emit_u32(lbm_char_channel_t *chan, uint32_t v, bool ps) { | |
186 | char buf[EMIT_BUFFER_SIZE]; | ||
187 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRIu32"%s", v, ps ? "u32" : ""); |
188 | 56 | return print_emit_string(chan, buf); | |
189 | } | ||
190 | |||
191 | 56 | static int print_emit_i32(lbm_char_channel_t *chan, int32_t v, bool ps) { | |
192 | char buf[EMIT_BUFFER_SIZE]; | ||
193 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRId32"%s", v, ps ? "i32" : ""); |
194 | 56 | return print_emit_string(chan, buf); | |
195 | } | ||
196 | |||
197 | 56 | static int print_emit_u64(lbm_char_channel_t *chan, uint64_t v, bool ps) { | |
198 | char buf[EMIT_BUFFER_SIZE]; | ||
199 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRIu64"%s", v, ps ? "u64" : ""); |
200 | 56 | return print_emit_string(chan, buf); | |
201 | } | ||
202 | |||
203 | 56 | static int print_emit_i64(lbm_char_channel_t *chan, int64_t v, bool ps) { | |
204 | char buf[EMIT_BUFFER_SIZE]; | ||
205 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRId64"%s", v, ps ? "i64" : ""); |
206 | 56 | return print_emit_string(chan, buf); | |
207 | } | ||
208 | |||
209 | 44 | static int print_emit_continuation(lbm_char_channel_t *chan, lbm_value v) { | |
210 | char buf[EMIT_BUFFER_SIZE]; | ||
211 | 44 | lbm_uint cont = (v & ~LBM_CONTINUATION_INTERNAL) >> LBM_ADDRESS_SHIFT; | |
212 | 44 | snprintf(buf, EMIT_BUFFER_SIZE, "CONT[" "%"PRI_UINT"]", cont); | |
213 | 44 | return print_emit_string(chan, buf); | |
214 | } | ||
215 | |||
216 | ✗ | static int print_emit_custom(lbm_char_channel_t *chan, lbm_value v) { | |
217 | ✗ | lbm_uint *custom = (lbm_uint*)lbm_car(v); | |
218 | int r; | ||
219 | ✗ | if (custom && custom[CUSTOM_TYPE_DESCRIPTOR]) { | |
220 | ✗ | r = print_emit_string(chan, (char*)custom[CUSTOM_TYPE_DESCRIPTOR]); | |
221 | } else { | ||
222 | ✗ | r = print_emit_string(chan, "Unspecified_Custom_Type"); | |
223 | } | ||
224 | ✗ | return r; | |
225 | } | ||
226 | |||
227 | 12 | static int print_emit_channel(lbm_char_channel_t *chan, lbm_value v) { | |
228 | (void) v; | ||
229 | 12 | return print_emit_string(chan, "~CHANNEL~"); | |
230 | } | ||
231 | |||
232 | 140 | static int print_emit_array_data(lbm_char_channel_t *chan, lbm_array_header_t *array) { | |
233 | |||
234 | 140 | int r = print_emit_char(chan, '['); | |
235 | |||
236 |
1/2✓ Branch 0 taken 140 times.
✗ Branch 1 not taken.
|
140 | if (r == EMIT_OK) { |
237 | |||
238 |
2/2✓ Branch 0 taken 420 times.
✓ Branch 1 taken 140 times.
|
560 | for (unsigned int i = 0; i < array->size; i ++) { |
239 | |||
240 | 420 | char *c_data = (char*)array->data; | |
241 | 420 | r = print_emit_byte(chan, (uint8_t)c_data[i], false); | |
242 | |||
243 |
3/4✓ Branch 0 taken 420 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 280 times.
✓ Branch 3 taken 140 times.
|
420 | if (r == EMIT_OK && i != array->size - 1) { |
244 | 280 | r = print_emit_char(chan, ' '); | |
245 | } | ||
246 | } | ||
247 | |||
248 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
|
140 | if (r != EMIT_OK) return r; |
249 | 140 | return print_emit_char(chan, ']'); | |
250 | } | ||
251 | ✗ | return r; | |
252 | } | ||
253 | |||
254 | 168 | static int print_emit_bytearray(lbm_char_channel_t *chan, lbm_value v) { | |
255 | |||
256 | char *str; | ||
257 | |||
258 |
2/2✓ Branch 1 taken 28 times.
✓ Branch 2 taken 140 times.
|
168 | if (lbm_value_is_printable_string(v, &str)) { |
259 | 28 | int r = print_emit_char(chan, '"'); | |
260 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (r == EMIT_OK) { |
261 | 28 | r = print_emit_string_value(chan, str); | |
262 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | if (r == EMIT_OK) { |
263 | 28 | r = print_emit_char(chan, '"'); | |
264 | } | ||
265 | } | ||
266 | 28 | return r; | |
267 | } | ||
268 | |||
269 | 140 | lbm_array_header_t *array = (lbm_array_header_t*)lbm_car(v); | |
270 | 140 | return print_emit_array_data(chan, array); | |
271 | } | ||
272 | |||
273 | |||
274 | 36523 | static int lbm_print_internal(lbm_char_channel_t *chan, lbm_value v) { | |
275 | |||
276 | 36523 | lbm_stack_clear(&print_stack); | |
277 | 36523 | lbm_value start_print[2] = {v, PRINT}; | |
278 | 36523 | push_n(&print_stack, start_print, 2); | |
279 | 36523 | bool chan_full = false; | |
280 | lbm_value curr; | ||
281 | lbm_uint instr; | ||
282 | 36523 | int r = EMIT_FAILED; | |
283 | |||
284 |
3/4✓ Branch 0 taken 39259 times.
✓ Branch 1 taken 36519 times.
✓ Branch 2 taken 39259 times.
✗ Branch 3 not taken.
|
112301 | while (!lbm_stack_is_empty(&print_stack) && !chan_full) { |
285 | 39259 | lbm_pop(&print_stack, &instr); | |
286 |
5/10✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 328 times.
✓ Branch 4 taken 852 times.
✓ Branch 5 taken 324 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 28 times.
✓ Branch 8 taken 37727 times.
✗ Branch 9 not taken.
|
39259 | switch (instr) { |
287 | ✗ | case START_ARRAY: { | |
288 | ✗ | lbm_pop(&print_stack, &curr); | |
289 | ✗ | int res = 1; | |
290 | ✗ | r = print_emit_char(chan, '['); | |
291 | ✗ | lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(curr); | |
292 | ✗ | lbm_uint ix = 0; | |
293 | ✗ | lbm_uint size = arr->size / sizeof(lbm_value); | |
294 | ✗ | lbm_value *arrdata = (lbm_value*)arr->data; | |
295 | ✗ | if (size >= 1) { | |
296 | ✗ | lbm_value continuation[5] = | |
297 | ✗ | {ix + 1, | |
298 | ✗ | (lbm_uint) arr, | |
299 | CONTINUE_ARRAY, | ||
300 | ✗ | arrdata[ix], | |
301 | PRINT}; | ||
302 | ✗ | res = res && push_n(&print_stack, continuation, 5); | |
303 | } else { | ||
304 | ✗ | res = res && lbm_push(&print_stack, END_LIST); | |
305 | } | ||
306 | ✗ | if (!res) { | |
307 | ✗ | return EMIT_FAILED; | |
308 | } | ||
309 | ✗ | break; | |
310 | } | ||
311 | ✗ | case CONTINUE_ARRAY: { | |
312 | lbm_uint arr_ptr; | ||
313 | lbm_array_header_t *arr; | ||
314 | lbm_uint ix; | ||
315 | ✗ | int res = 1; | |
316 | ✗ | lbm_pop_2(&print_stack, &arr_ptr, &ix); | |
317 | ✗ | arr = (lbm_array_header_t *)arr_ptr; | |
318 | ✗ | lbm_value *arrdata = (lbm_value*)arr->data; | |
319 | ✗ | if (ix < (arr->size / sizeof(lbm_value))) { | |
320 | ✗ | r = print_emit_char(chan, ' '); | |
321 | ✗ | lbm_value continuation[5] = | |
322 | ✗ | {ix + 1, | |
323 | ✗ | (lbm_uint) arr, | |
324 | CONTINUE_ARRAY, | ||
325 | ✗ | arrdata[ix], | |
326 | PRINT}; | ||
327 | ✗ | res = res && push_n(&print_stack, continuation, 5); | |
328 | } else { | ||
329 | ✗ | res = res && lbm_push(&print_stack, END_ARRAY); | |
330 | } | ||
331 | ✗ | if (!res) { | |
332 | ✗ | return EMIT_FAILED; | |
333 | } | ||
334 | ✗ | break; | |
335 | } | ||
336 | ✗ | case END_ARRAY: { | |
337 | ✗ | r = print_emit_char(chan, ']'); | |
338 | ✗ | break; | |
339 | } | ||
340 | 328 | case START_LIST: { | |
341 | 328 | lbm_pop(&print_stack, &curr); | |
342 | 328 | r = print_emit_char(chan, '('); | |
343 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 328 times.
|
328 | if (r != EMIT_OK) return r; |
344 | 328 | lbm_value car_val = lbm_car(curr); | |
345 | 328 | lbm_value cdr_val = lbm_cdr(curr); | |
346 | 328 | int res = 1; | |
347 |
3/4✓ Branch 1 taken 76 times.
✓ Branch 2 taken 252 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 76 times.
|
404 | if (lbm_type_of(cdr_val) == LBM_TYPE_CONS || |
348 | 76 | lbm_type_of(cdr_val) == (LBM_TYPE_CONS | LBM_PTR_TO_CONSTANT_BIT)) { | |
349 | 252 | lbm_value cont[2] = {cdr_val, CONTINUE_LIST}; | |
350 |
2/4✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 252 times.
✗ Branch 4 not taken.
|
252 | res = res && push_n(&print_stack, cont, 2); |
351 |
3/4✓ Branch 1 taken 48 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 48 times.
✗ Branch 4 not taken.
|
76 | } else if (lbm_type_of(cdr_val) == LBM_TYPE_SYMBOL && |
352 | cdr_val == ENC_SYM_NIL) { | ||
353 |
2/4✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 48 times.
✗ Branch 4 not taken.
|
48 | res = res && lbm_push(&print_stack, END_LIST); |
354 | } else { | ||
355 | 28 | lbm_value cont[4] = {END_LIST, cdr_val, PRINT, PRINT_DOT}; | |
356 |
2/4✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
|
28 | res = res && push_n(&print_stack, cont, 4); |
357 | } | ||
358 | 328 | lbm_value cont[2] = {car_val, PRINT}; | |
359 |
2/4✓ Branch 0 taken 328 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 328 times.
✗ Branch 4 not taken.
|
328 | res = res && push_n(&print_stack, cont,2); |
360 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 328 times.
|
328 | if (!res) { |
361 | ✗ | return EMIT_FAILED; | |
362 | } | ||
363 | 328 | break; | |
364 | } | ||
365 | 852 | case CONTINUE_LIST: { | |
366 | 852 | int res = 1; | |
367 | 852 | lbm_pop(&print_stack, &curr); | |
368 | |||
369 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 852 times.
|
852 | if (lbm_type_of(curr) == LBM_TYPE_SYMBOL && |
370 | ✗ | curr == ENC_SYM_NIL) { | |
371 | 848 | break; | |
372 | } | ||
373 | |||
374 | 852 | lbm_value car_val = lbm_car(curr); | |
375 | 852 | lbm_value cdr_val = lbm_cdr(curr); | |
376 | |||
377 | 852 | r = print_emit_char(chan, ' '); | |
378 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 848 times.
|
852 | if (r != EMIT_OK) { |
379 | 4 | return r; | |
380 | } | ||
381 |
3/4✓ Branch 1 taken 248 times.
✓ Branch 2 taken 600 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 248 times.
|
1096 | if (lbm_type_of(cdr_val) == LBM_TYPE_CONS || |
382 | 248 | lbm_type_of(cdr_val) == (LBM_TYPE_CONS | LBM_PTR_TO_CONSTANT_BIT)) { | |
383 | 600 | lbm_value cont[2] = {cdr_val, CONTINUE_LIST}; | |
384 |
2/4✓ Branch 0 taken 600 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 600 times.
✗ Branch 4 not taken.
|
600 | res = res && push_n(&print_stack, cont, 2); |
385 |
2/4✓ Branch 1 taken 248 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 248 times.
✗ Branch 4 not taken.
|
248 | } else if (lbm_type_of(cdr_val) == LBM_TYPE_SYMBOL && |
386 | cdr_val == ENC_SYM_NIL) { | ||
387 |
2/4✓ Branch 0 taken 248 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 248 times.
✗ Branch 4 not taken.
|
248 | res = res && lbm_push(&print_stack, END_LIST); |
388 | } else { | ||
389 | ✗ | lbm_value cont[4] = {END_LIST, cdr_val, PRINT, PRINT_DOT}; | |
390 | ✗ | res = res && push_n(&print_stack, cont, 4); | |
391 | } | ||
392 | 848 | lbm_value cont[2] = {car_val, PRINT}; | |
393 |
2/4✓ Branch 0 taken 848 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 848 times.
✗ Branch 4 not taken.
|
848 | res = res && push_n(&print_stack, cont, 2); |
394 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 848 times.
|
848 | if (!res) { |
395 | ✗ | return EMIT_FAILED; | |
396 | } | ||
397 | 848 | break; | |
398 | } | ||
399 | 324 | case END_LIST: | |
400 | 324 | r = print_emit_char(chan, ')'); | |
401 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 324 times.
|
324 | if (r != EMIT_OK) return r; |
402 | 324 | break; | |
403 | ✗ | case PRINT_SPACE: | |
404 | ✗ | r = print_emit_char(chan, ' '); | |
405 | ✗ | if (r != EMIT_OK) return r; | |
406 | ✗ | break; | |
407 | 28 | case PRINT_DOT: | |
408 | 28 | r = print_emit_string(chan, " . "); | |
409 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (r != EMIT_OK) return r; |
410 | 28 | break; | |
411 | 37727 | case PRINT: | |
412 | 37727 | lbm_pop(&print_stack, &curr); | |
413 | |||
414 | 37727 | lbm_type t = lbm_type_of(curr); | |
415 |
2/2✓ Branch 1 taken 888 times.
✓ Branch 2 taken 36839 times.
|
37727 | if (lbm_is_ptr(curr)) |
416 | 888 | t = t & LBM_PTR_TO_CONSTANT_MASK; // print constants normally | |
417 | |||
418 | switch(t) { | ||
419 | 328 | case LBM_TYPE_CONS: { | |
420 | 328 | lbm_value cont[2] = {curr, START_LIST}; | |
421 | 328 | int res = push_n(&print_stack, cont, 2); | |
422 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 328 times.
|
328 | if (!res) { |
423 | ✗ | print_emit_string(chan," ..."); | |
424 | ✗ | return EMIT_OK; | |
425 | } | ||
426 | 328 | break; | |
427 | } | ||
428 | 35683 | case LBM_TYPE_SYMBOL: | |
429 | 35683 | r = print_emit_symbol(chan, curr); | |
430 | 35683 | break; | |
431 | 1016 | case LBM_TYPE_I: | |
432 | 1016 | r = print_emit_i(chan, lbm_dec_i(curr)); | |
433 | 1016 | break; | |
434 | 84 | case LBM_TYPE_U: | |
435 | 84 | r = print_emit_u(chan, lbm_dec_u(curr), true); | |
436 | 84 | break; | |
437 | 56 | case LBM_TYPE_CHAR: | |
438 | 56 | r = print_emit_byte(chan, (uint8_t)lbm_dec_char(curr), true); | |
439 | 56 | break; | |
440 | 56 | case LBM_TYPE_FLOAT: | |
441 | 56 | r = print_emit_float(chan, lbm_dec_float(curr), true); | |
442 | 56 | break; | |
443 | 56 | case LBM_TYPE_DOUBLE: | |
444 | 56 | r = print_emit_double(chan, lbm_dec_double(curr), true); | |
445 | 56 | break; | |
446 | 56 | case LBM_TYPE_U32: | |
447 | 56 | r = print_emit_u32(chan, lbm_dec_u32(curr), true); | |
448 | 56 | break; | |
449 | 56 | case LBM_TYPE_I32: | |
450 | 56 | r = print_emit_i32(chan, lbm_dec_i32(curr), true); | |
451 | 56 | break; | |
452 | 56 | case LBM_TYPE_U64: | |
453 | 56 | r = print_emit_u64(chan, lbm_dec_u64(curr), true); | |
454 | 56 | break; | |
455 | 56 | case LBM_TYPE_I64: | |
456 | 56 | r = print_emit_i64(chan, lbm_dec_i64(curr), true); | |
457 | 56 | break; | |
458 | 44 | case LBM_CONTINUATION_INTERNAL_TYPE: | |
459 | 44 | r = print_emit_continuation(chan, curr); | |
460 | 44 | break; | |
461 | ✗ | case LBM_TYPE_CUSTOM: | |
462 | ✗ | r = print_emit_custom(chan, curr); | |
463 | ✗ | break; | |
464 | 12 | case LBM_TYPE_CHANNEL: | |
465 | 12 | r = print_emit_channel(chan, curr); | |
466 | 12 | break; | |
467 | 168 | case LBM_TYPE_ARRAY: | |
468 | 168 | r = print_emit_bytearray(chan, curr); | |
469 | 168 | break; | |
470 | ✗ | case LBM_TYPE_LISPARRAY: { | |
471 | ✗ | lbm_value cont[2] = {curr, START_ARRAY}; | |
472 | ✗ | int res = push_n(&print_stack, cont, 2); | |
473 | ✗ | if (!res) { | |
474 | ✗ | print_emit_string(chan, " ..."); | |
475 | ✗ | return EMIT_OK; | |
476 | } | ||
477 | ✗ | break; | |
478 | } | ||
479 | ✗ | default: | |
480 | ✗ | return EMIT_FAILED; | |
481 | } | ||
482 | 75778 | } | |
483 | } | ||
484 | 36519 | return r; | |
485 | } | ||
486 | |||
487 | 36523 | int lbm_print_value(char *buf, unsigned int len, lbm_value v) { | |
488 | |||
489 | lbm_string_channel_state_t st; | ||
490 | lbm_char_channel_t chan; | ||
491 | |||
492 | 36523 | memset(buf, 0, len); | |
493 | 36523 | lbm_create_string_char_channel_size(&st, &chan, buf, len); | |
494 |
2/2✓ Branch 1 taken 36519 times.
✓ Branch 2 taken 4 times.
|
36523 | if (lbm_print_internal(&chan,v) == EMIT_OK) |
495 | 36519 | return 1; | |
496 | 4 | return 0; | |
497 | } | ||
498 |