| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2018, 2020 - 2025 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}; | ||
| 43 | static bool print_has_stack = false; | ||
| 44 | |||
| 45 | const char *failed_str = "Error: print failed\n"; | ||
| 46 | |||
| 47 | 48028 | static int push_n(lbm_stack_t *s, lbm_uint *values, lbm_uint n) { | |
| 48 |
1/2✓ Branch 0 taken 48028 times.
✗ Branch 1 not taken.
|
48028 | if (s->sp + n < s->size) { |
| 49 |
2/2✓ Branch 0 taken 96112 times.
✓ Branch 1 taken 48028 times.
|
144140 | for (lbm_uint i = 0; i < n; i ++) { |
| 50 | 96112 | s->data[s->sp+i] = values[i]; | |
| 51 | } | ||
| 52 | 48028 | s->sp+=n; | |
| 53 | 48028 | return 1; | |
| 54 | } | ||
| 55 | ✗ | return 0; | |
| 56 | } | ||
| 57 | |||
| 58 | 1456 | bool lbm_value_is_printable_string(lbm_value v, char **str) { | |
| 59 | 1456 | bool is_a_string = false; | |
| 60 |
2/2✓ Branch 1 taken 560 times.
✓ Branch 2 taken 896 times.
|
1456 | if (lbm_is_array_r(v)) { |
| 61 | 560 | lbm_array_header_t *array = (lbm_array_header_t*)lbm_car(v); | |
| 62 | // TODO: Potential null deref. | ||
| 63 | // Highly unlikely that array is a recognizable NULL though. | ||
| 64 | // If it is incorrect, it is most likely arbitrary. | ||
| 65 | 560 | char *c_data = (char *)array->data; | |
| 66 | 560 | unsigned int i = 0; | |
| 67 |
3/4✓ Branch 0 taken 560 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 532 times.
✓ Branch 3 taken 28 times.
|
560 | if (array->size >= 1 && c_data[0] != 0) { // nonzero length and ix 0 is not 0 |
| 68 | 532 | is_a_string = true; | |
| 69 |
1/2✓ Branch 0 taken 1988 times.
✗ Branch 1 not taken.
|
1988 | for (i = 0; i < array->size; i ++) { |
| 70 |
2/2✓ Branch 0 taken 252 times.
✓ Branch 1 taken 1736 times.
|
1988 | if (c_data[i] == 0) break; |
| 71 |
5/6✓ Branch 0 taken 308 times.
✓ Branch 1 taken 1428 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 280 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 28 times.
|
1736 | if (!isprint((unsigned char)c_data[i]) && ((c_data[i] < 8) || c_data[i] > 13)) { |
| 72 | 280 | is_a_string = false; | |
| 73 | 280 | break; | |
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 |
3/4✓ Branch 0 taken 252 times.
✓ Branch 1 taken 308 times.
✓ Branch 2 taken 252 times.
✗ Branch 3 not taken.
|
560 | if (i != array->size-1 && c_data[i-1] != 0) is_a_string = false; |
| 78 |
2/2✓ Branch 0 taken 252 times.
✓ Branch 1 taken 308 times.
|
560 | if (is_a_string) { |
| 79 | 252 | *str = (char*)array->data; | |
| 80 | } | ||
| 81 | } | ||
| 82 | 1456 | return is_a_string; | |
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | 21504 | int lbm_print_init(lbm_uint print_stack_size) { | |
| 87 | |||
| 88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21504 times.
|
21504 | if (print_stack_size == 0) |
| 89 | ✗ | return 0; | |
| 90 | |||
| 91 | 21504 | lbm_uint *print_stack_storage = (lbm_uint*)lbm_malloc(print_stack_size * sizeof(lbm_uint)); | |
| 92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 21504 times.
|
21504 | if (!print_stack_storage) return 0; |
| 93 | |||
| 94 |
1/2✓ Branch 1 taken 21504 times.
✗ Branch 2 not taken.
|
21504 | if (lbm_stack_create(&print_stack, print_stack_storage, print_stack_size)) { |
| 95 | 21504 | print_has_stack = true; | |
| 96 | 21504 | return 1; | |
| 97 | } | ||
| 98 | ✗ | return 0; | |
| 99 | } | ||
| 100 | |||
| 101 | #define EMIT_BUFFER_SIZE 30 | ||
| 102 | |||
| 103 | #define EMIT_FAILED -1 | ||
| 104 | #define EMIT_OK 0 | ||
| 105 | |||
| 106 | 127174 | static int print_emit_string(lbm_char_channel_t *chan, char* str) { | |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 127174 times.
|
127174 | if (str == NULL) return EMIT_FAILED; |
| 108 |
2/2✓ Branch 0 taken 133770 times.
✓ Branch 1 taken 48224 times.
|
181994 | while (*str != 0) { |
| 109 | 133770 | int r = lbm_channel_write(chan, *str); | |
| 110 | 133770 | str++; | |
| 111 |
2/2✓ Branch 0 taken 78950 times.
✓ Branch 1 taken 54820 times.
|
133770 | if (r != CHANNEL_SUCCESS) return EMIT_FAILED; |
| 112 | } | ||
| 113 | 48224 | return EMIT_OK; | |
| 114 | } | ||
| 115 | |||
| 116 | 4192 | static int print_emit_char(lbm_char_channel_t *chan, char c) { | |
| 117 | |||
| 118 | 4192 | int r = lbm_channel_write(chan, c); | |
| 119 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4188 times.
|
4192 | if (r != CHANNEL_SUCCESS) return EMIT_FAILED; |
| 120 | 4188 | return EMIT_OK; | |
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | ✗ | static int emit_escape(lbm_char_channel_t *chan, char c) { | |
| 125 | ✗ | switch(c) { | |
| 126 | ✗ | case '"': return print_emit_string(chan, "\\\""); | |
| 127 | ✗ | case '\n': return print_emit_string(chan, "\\n"); | |
| 128 | ✗ | case '\r': return print_emit_string(chan, "\\r"); | |
| 129 | ✗ | case '\t': return print_emit_string(chan, "\\t"); | |
| 130 | ✗ | case '\\': return print_emit_string(chan, "\\\\"); | |
| 131 | ✗ | default: | |
| 132 | ✗ | return print_emit_char(chan, c); | |
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | ✗ | static int print_emit_string_value(lbm_char_channel_t *chan, char* str) { | |
| 137 | ✗ | if (str == NULL) return EMIT_FAILED; | |
| 138 | ✗ | while (*str != 0) { | |
| 139 | ✗ | int r = emit_escape(chan, *str++); | |
| 140 | ✗ | if (r != EMIT_OK) return r; | |
| 141 | } | ||
| 142 | ✗ | return EMIT_OK; | |
| 143 | } | ||
| 144 | |||
| 145 | 43869 | static int print_emit_symbol(lbm_char_channel_t *chan, lbm_value sym) { | |
| 146 | 43869 | char *str_ptr = (char*)lbm_get_name_by_symbol(lbm_dec_sym(sym)); | |
| 147 | 43869 | return print_emit_string(chan, str_ptr); | |
| 148 | } | ||
| 149 | |||
| 150 | 1603 | static int print_emit_i(lbm_char_channel_t *chan, lbm_int v) { | |
| 151 | char buf[EMIT_BUFFER_SIZE]; | ||
| 152 | 1603 | snprintf(buf, EMIT_BUFFER_SIZE, "%"PRI_INT, v); | |
| 153 | 1603 | return print_emit_string(chan, buf); | |
| 154 | } | ||
| 155 | |||
| 156 | 92 | static int print_emit_u(lbm_char_channel_t *chan, lbm_uint v, bool ps) { | |
| 157 | char buf[EMIT_BUFFER_SIZE]; | ||
| 158 |
1/2✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
|
92 | snprintf(buf, EMIT_BUFFER_SIZE, "%"PRI_UINT"%s", v, ps ? "u" : ""); |
| 159 | 92 | return print_emit_string(chan, buf); | |
| 160 | } | ||
| 161 | |||
| 162 | 81186 | static int print_emit_byte(lbm_char_channel_t *chan, uint8_t v, bool ps) { | |
| 163 | char buf[EMIT_BUFFER_SIZE]; | ||
| 164 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 81130 times.
|
81186 | snprintf(buf, EMIT_BUFFER_SIZE, "%u%s", v, ps ? "b" : ""); |
| 165 | 81186 | return print_emit_string(chan, buf); | |
| 166 | } | ||
| 167 | |||
| 168 | 56 | static int print_emit_float(lbm_char_channel_t *chan, float v, bool ps) { | |
| 169 | char buf[EMIT_BUFFER_SIZE]; | ||
| 170 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf, EMIT_BUFFER_SIZE, "%"PRI_FLOAT"%s", (double)v, ps ? "f32" : ""); |
| 171 | 56 | return print_emit_string(chan, buf); | |
| 172 | } | ||
| 173 | |||
| 174 | 56 | static int print_emit_double(lbm_char_channel_t *chan, double v, bool ps) { | |
| 175 | char buf[EMIT_BUFFER_SIZE]; | ||
| 176 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf, EMIT_BUFFER_SIZE, "%lf%s", v, ps ? "f64" : ""); |
| 177 | 56 | return print_emit_string(chan, buf); | |
| 178 | } | ||
| 179 | |||
| 180 | 56 | static int print_emit_u32(lbm_char_channel_t *chan, uint32_t v, bool ps) { | |
| 181 | char buf[EMIT_BUFFER_SIZE]; | ||
| 182 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRIu32"%s", v, ps ? "u32" : ""); |
| 183 | 56 | return print_emit_string(chan, buf); | |
| 184 | } | ||
| 185 | |||
| 186 | 56 | static int print_emit_i32(lbm_char_channel_t *chan, int32_t v, bool ps) { | |
| 187 | char buf[EMIT_BUFFER_SIZE]; | ||
| 188 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRId32"%s", v, ps ? "i32" : ""); |
| 189 | 56 | return print_emit_string(chan, buf); | |
| 190 | } | ||
| 191 | |||
| 192 | 56 | static int print_emit_u64(lbm_char_channel_t *chan, uint64_t v, bool ps) { | |
| 193 | char buf[EMIT_BUFFER_SIZE]; | ||
| 194 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRIu64"%s", v, ps ? "u64" : ""); |
| 195 | 56 | return print_emit_string(chan, buf); | |
| 196 | } | ||
| 197 | |||
| 198 | 56 | static int print_emit_i64(lbm_char_channel_t *chan, int64_t v, bool ps) { | |
| 199 | char buf[EMIT_BUFFER_SIZE]; | ||
| 200 |
1/2✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
|
56 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRId64"%s", v, ps ? "i64" : ""); |
| 201 | 56 | return print_emit_string(chan, buf); | |
| 202 | } | ||
| 203 | |||
| 204 | 48 | static int print_emit_continuation(lbm_char_channel_t *chan, lbm_value v) { | |
| 205 | char buf[EMIT_BUFFER_SIZE]; | ||
| 206 | 48 | lbm_uint cont = (v & ~LBM_CONTINUATION_INTERNAL) >> LBM_ADDRESS_SHIFT; | |
| 207 | 48 | snprintf(buf, EMIT_BUFFER_SIZE, "CONT[" "%"PRI_UINT"]", cont); | |
| 208 | 48 | return print_emit_string(chan, buf); | |
| 209 | } | ||
| 210 | |||
| 211 | ✗ | static int print_emit_custom(lbm_char_channel_t *chan, lbm_value v) { | |
| 212 | ✗ | lbm_uint *custom = (lbm_uint*)lbm_car(v); | |
| 213 | int r; // NULL checks works against SYM_NIL. | ||
| 214 | ✗ | if (custom && custom[CUSTOM_TYPE_DESCRIPTOR]) { | |
| 215 | ✗ | r = print_emit_string(chan, (char*)custom[CUSTOM_TYPE_DESCRIPTOR]); | |
| 216 | } else { | ||
| 217 | ✗ | r = print_emit_string(chan, "INVALID_CUSTOM_TYPE"); | |
| 218 | } | ||
| 219 | ✗ | return r; | |
| 220 | } | ||
| 221 | |||
| 222 | ✗ | static int print_emit_defrag_mem(lbm_char_channel_t *chan, lbm_value v) { | |
| 223 | (void) v; | ||
| 224 | ✗ | return print_emit_string(chan, "DM"); | |
| 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 | 168 | static int print_emit_array_data(lbm_char_channel_t *chan, lbm_array_header_t *array) { | |
| 233 | |||
| 234 | 168 | int r = print_emit_char(chan, '['); | |
| 235 | |||
| 236 |
1/2✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
|
168 | if (r == EMIT_OK) { |
| 237 | |||
| 238 |
2/2✓ Branch 0 taken 81130 times.
✓ Branch 1 taken 168 times.
|
81298 | for (unsigned int i = 0; i < array->size; i ++) { |
| 239 | |||
| 240 | 81130 | char *c_data = (char*)array->data; | |
| 241 | 81130 | r = print_emit_byte(chan, (uint8_t)c_data[i], false); | |
| 242 | |||
| 243 |
4/4✓ Branch 0 taken 2184 times.
✓ Branch 1 taken 78946 times.
✓ Branch 2 taken 2044 times.
✓ Branch 3 taken 140 times.
|
81130 | if (r == EMIT_OK && i != array->size - 1) { |
| 244 | 2044 | r = print_emit_char(chan, ' '); | |
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 140 times.
|
168 | 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 | 168 | int r = 0; | |
| 256 | char *str; | ||
| 257 |
1/2✓ Branch 1 taken 168 times.
✗ Branch 2 not taken.
|
168 | if (lbm_is_array_r(v)) { |
| 258 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 168 times.
|
168 | if (lbm_value_is_printable_string(v, &str)) { |
| 259 | ✗ | r = print_emit_char(chan, '"'); | |
| 260 | ✗ | if (r == EMIT_OK) { | |
| 261 | ✗ | r = print_emit_string_value(chan, str); | |
| 262 | ✗ | if (r == EMIT_OK) { | |
| 263 | ✗ | r = print_emit_char(chan, '"'); | |
| 264 | } | ||
| 265 | } | ||
| 266 | } else { | ||
| 267 | 168 | lbm_array_header_t *array = (lbm_array_header_t*)lbm_car(v); | |
| 268 | 168 | r= print_emit_array_data(chan, array); | |
| 269 | } | ||
| 270 | } else { | ||
| 271 | ✗ | r = print_emit_string(chan, "[INVALID_ARRAY]"); | |
| 272 | } | ||
| 273 | 168 | return r; | |
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | 45140 | static int lbm_print_internal(lbm_char_channel_t *chan, lbm_value v) { | |
| 278 | |||
| 279 | 45140 | lbm_stack_clear(&print_stack); | |
| 280 | 45140 | lbm_value start_print[2] = {v, PRINT}; | |
| 281 | 45140 | push_n(&print_stack, start_print, 2); | |
| 282 | 45140 | bool chan_full = false; | |
| 283 | lbm_value curr; | ||
| 284 | lbm_uint instr; | ||
| 285 | 45140 | int r = EMIT_FAILED; | |
| 286 | |||
| 287 |
3/4✓ Branch 0 taken 48464 times.
✓ Branch 1 taken 45136 times.
✓ Branch 2 taken 48464 times.
✗ Branch 3 not taken.
|
138740 | while (!lbm_stack_is_empty(&print_stack) && !chan_full) { |
| 288 | 48464 | lbm_pop(&print_stack, &instr); | |
| 289 |
5/10✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 412 times.
✓ Branch 4 taken 1020 times.
✓ Branch 5 taken 408 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 28 times.
✓ Branch 8 taken 46596 times.
✗ Branch 9 not taken.
|
48464 | switch (instr) { |
| 290 | ✗ | case START_ARRAY: { | |
| 291 | ✗ | lbm_pop(&print_stack, &curr); | |
| 292 | ✗ | int res = 1; | |
| 293 | ✗ | r = print_emit_char(chan, '['); | |
| 294 | ✗ | lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(curr); | |
| 295 | ✗ | lbm_uint ix = 0; | |
| 296 | ✗ | lbm_uint size = arr->size / sizeof(lbm_value); | |
| 297 | ✗ | lbm_value *arrdata = (lbm_value*)arr->data; | |
| 298 | ✗ | if (size >= 1) { | |
| 299 | ✗ | lbm_value continuation[5] = | |
| 300 | ✗ | {ix + 1, | |
| 301 | ✗ | (lbm_uint) arr, | |
| 302 | CONTINUE_ARRAY, | ||
| 303 | ✗ | arrdata[ix], | |
| 304 | PRINT}; | ||
| 305 | ✗ | res = res && push_n(&print_stack, continuation, 5); | |
| 306 | } else { | ||
| 307 | ✗ | res = res && lbm_push(&print_stack, END_LIST); | |
| 308 | } | ||
| 309 | ✗ | if (!res) { | |
| 310 | ✗ | return EMIT_FAILED; | |
| 311 | } | ||
| 312 | ✗ | break; | |
| 313 | } | ||
| 314 | ✗ | case CONTINUE_ARRAY: { | |
| 315 | lbm_uint arr_ptr; | ||
| 316 | lbm_array_header_t *arr; | ||
| 317 | lbm_uint ix; | ||
| 318 | ✗ | int res = 1; | |
| 319 | ✗ | lbm_pop_2(&print_stack, &arr_ptr, &ix); | |
| 320 | ✗ | arr = (lbm_array_header_t *)arr_ptr; | |
| 321 | ✗ | lbm_value *arrdata = (lbm_value*)arr->data; | |
| 322 | ✗ | if (ix < (arr->size / sizeof(lbm_value))) { | |
| 323 | ✗ | r = print_emit_char(chan, ' '); | |
| 324 | ✗ | lbm_value continuation[5] = | |
| 325 | ✗ | {ix + 1, | |
| 326 | ✗ | (lbm_uint) arr, | |
| 327 | CONTINUE_ARRAY, | ||
| 328 | ✗ | arrdata[ix], | |
| 329 | PRINT}; | ||
| 330 | ✗ | res = res && push_n(&print_stack, continuation, 5); | |
| 331 | } else { | ||
| 332 | ✗ | res = res && lbm_push(&print_stack, END_ARRAY); | |
| 333 | } | ||
| 334 | ✗ | if (!res) { | |
| 335 | ✗ | return EMIT_FAILED; | |
| 336 | } | ||
| 337 | ✗ | break; | |
| 338 | } | ||
| 339 | ✗ | case END_ARRAY: { | |
| 340 | ✗ | r = print_emit_char(chan, ']'); | |
| 341 | ✗ | break; | |
| 342 | } | ||
| 343 | 412 | case START_LIST: { | |
| 344 | 412 | lbm_pop(&print_stack, &curr); | |
| 345 | 412 | r = print_emit_char(chan, '('); | |
| 346 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
|
412 | if (r != EMIT_OK) return r; |
| 347 | 412 | lbm_value car_val = lbm_car(curr); | |
| 348 | 412 | lbm_value cdr_val = lbm_cdr(curr); | |
| 349 | 412 | int res = 1; | |
| 350 |
3/4✓ Branch 1 taken 76 times.
✓ Branch 2 taken 336 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 76 times.
|
488 | if (lbm_type_of(cdr_val) == LBM_TYPE_CONS || |
| 351 | 76 | lbm_type_of(cdr_val) == (LBM_TYPE_CONS | LBM_PTR_TO_CONSTANT_BIT)) { | |
| 352 | 336 | lbm_value cont[2] = {cdr_val, CONTINUE_LIST}; | |
| 353 |
2/4✓ Branch 0 taken 336 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 336 times.
✗ Branch 4 not taken.
|
336 | res = res && push_n(&print_stack, cont, 2); |
| 354 |
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 && |
| 355 | cdr_val == ENC_SYM_NIL) { | ||
| 356 |
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); |
| 357 | } else { | ||
| 358 | 28 | lbm_value cont[4] = {END_LIST, cdr_val, PRINT, PRINT_DOT}; | |
| 359 |
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); |
| 360 | } | ||
| 361 | 412 | lbm_value cont[2] = {car_val, PRINT}; | |
| 362 |
2/4✓ Branch 0 taken 412 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 412 times.
✗ Branch 4 not taken.
|
412 | res = res && push_n(&print_stack, cont,2); |
| 363 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
|
412 | if (!res) { |
| 364 | ✗ | return EMIT_FAILED; | |
| 365 | } | ||
| 366 | 412 | break; | |
| 367 | } | ||
| 368 | 1020 | case CONTINUE_LIST: { | |
| 369 | 1020 | int res = 1; | |
| 370 | 1020 | lbm_pop(&print_stack, &curr); | |
| 371 | |||
| 372 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1020 times.
|
1020 | if (lbm_type_of(curr) == LBM_TYPE_SYMBOL && |
| 373 | ✗ | curr == ENC_SYM_NIL) { | |
| 374 | 1016 | break; | |
| 375 | } | ||
| 376 | |||
| 377 | 1020 | lbm_value car_val = lbm_car(curr); | |
| 378 | 1020 | lbm_value cdr_val = lbm_cdr(curr); | |
| 379 | |||
| 380 | 1020 | r = print_emit_char(chan, ' '); | |
| 381 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1016 times.
|
1020 | if (r != EMIT_OK) { |
| 382 | 4 | return r; | |
| 383 | } | ||
| 384 |
3/4✓ Branch 1 taken 332 times.
✓ Branch 2 taken 684 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 332 times.
|
1348 | if (lbm_type_of(cdr_val) == LBM_TYPE_CONS || |
| 385 | 332 | lbm_type_of(cdr_val) == (LBM_TYPE_CONS | LBM_PTR_TO_CONSTANT_BIT)) { | |
| 386 | 684 | lbm_value cont[2] = {cdr_val, CONTINUE_LIST}; | |
| 387 |
2/4✓ Branch 0 taken 684 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 684 times.
✗ Branch 4 not taken.
|
684 | res = res && push_n(&print_stack, cont, 2); |
| 388 |
2/4✓ Branch 1 taken 332 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 332 times.
✗ Branch 4 not taken.
|
332 | } else if (lbm_type_of(cdr_val) == LBM_TYPE_SYMBOL && |
| 389 | cdr_val == ENC_SYM_NIL) { | ||
| 390 |
2/4✓ Branch 0 taken 332 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 332 times.
✗ Branch 4 not taken.
|
332 | res = res && lbm_push(&print_stack, END_LIST); |
| 391 | } else { | ||
| 392 | ✗ | lbm_value cont[4] = {END_LIST, cdr_val, PRINT, PRINT_DOT}; | |
| 393 | ✗ | res = res && push_n(&print_stack, cont, 4); | |
| 394 | } | ||
| 395 | 1016 | lbm_value cont[2] = {car_val, PRINT}; | |
| 396 |
2/4✓ Branch 0 taken 1016 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1016 times.
✗ Branch 4 not taken.
|
1016 | res = res && push_n(&print_stack, cont, 2); |
| 397 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1016 times.
|
1016 | if (!res) { |
| 398 | ✗ | return EMIT_FAILED; | |
| 399 | } | ||
| 400 | 1016 | break; | |
| 401 | } | ||
| 402 | 408 | case END_LIST: | |
| 403 | 408 | r = print_emit_char(chan, ')'); | |
| 404 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 408 times.
|
408 | if (r != EMIT_OK) return r; |
| 405 | 408 | break; | |
| 406 | ✗ | case PRINT_SPACE: | |
| 407 | ✗ | r = print_emit_char(chan, ' '); | |
| 408 | ✗ | if (r != EMIT_OK) return r; | |
| 409 | ✗ | break; | |
| 410 | 28 | case PRINT_DOT: | |
| 411 | 28 | r = print_emit_string(chan, " . "); | |
| 412 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (r != EMIT_OK) return r; |
| 413 | 28 | break; | |
| 414 | 46596 | case PRINT: | |
| 415 | 46596 | lbm_pop(&print_stack, &curr); | |
| 416 | |||
| 417 | 46596 | lbm_type t = lbm_type_of(curr); | |
| 418 |
2/2✓ Branch 1 taken 976 times.
✓ Branch 2 taken 45620 times.
|
46596 | if (lbm_is_ptr(curr)) |
| 419 | 976 | t = t & LBM_PTR_TO_CONSTANT_MASK; // print constants normally | |
| 420 | |||
| 421 | switch(t) { | ||
| 422 | 412 | case LBM_TYPE_CONS: { | |
| 423 | 412 | lbm_value cont[2] = {curr, START_LIST}; | |
| 424 | 412 | int res = push_n(&print_stack, cont, 2); | |
| 425 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 412 times.
|
412 | if (!res) { |
| 426 | ✗ | print_emit_string(chan," ..."); | |
| 427 | ✗ | return EMIT_OK; | |
| 428 | } | ||
| 429 | 412 | break; | |
| 430 | } | ||
| 431 | 43869 | case LBM_TYPE_SYMBOL: | |
| 432 | 43869 | r = print_emit_symbol(chan, curr); | |
| 433 | 43869 | break; | |
| 434 | 1603 | case LBM_TYPE_I: | |
| 435 | 1603 | r = print_emit_i(chan, lbm_dec_i(curr)); | |
| 436 | 1603 | break; | |
| 437 | 92 | case LBM_TYPE_U: | |
| 438 | 92 | r = print_emit_u(chan, lbm_dec_u(curr), true); | |
| 439 | 92 | break; | |
| 440 | 56 | case LBM_TYPE_CHAR: | |
| 441 | 56 | r = print_emit_byte(chan, (uint8_t)lbm_dec_char(curr), true); | |
| 442 | 56 | break; | |
| 443 | 56 | case LBM_TYPE_FLOAT: | |
| 444 | 56 | r = print_emit_float(chan, lbm_dec_float(curr), true); | |
| 445 | 56 | break; | |
| 446 | 56 | case LBM_TYPE_DOUBLE: | |
| 447 | 56 | r = print_emit_double(chan, lbm_dec_double(curr), true); | |
| 448 | 56 | break; | |
| 449 | 56 | case LBM_TYPE_U32: | |
| 450 | 56 | r = print_emit_u32(chan, lbm_dec_u32(curr), true); | |
| 451 | 56 | break; | |
| 452 | 56 | case LBM_TYPE_I32: | |
| 453 | 56 | r = print_emit_i32(chan, lbm_dec_i32(curr), true); | |
| 454 | 56 | break; | |
| 455 | 56 | case LBM_TYPE_U64: | |
| 456 | 56 | r = print_emit_u64(chan, lbm_dec_u64(curr), true); | |
| 457 | 56 | break; | |
| 458 | 56 | case LBM_TYPE_I64: | |
| 459 | 56 | r = print_emit_i64(chan, lbm_dec_i64(curr), true); | |
| 460 | 56 | break; | |
| 461 | 48 | case LBM_CONTINUATION_INTERNAL_TYPE: | |
| 462 | 48 | r = print_emit_continuation(chan, curr); | |
| 463 | 48 | break; | |
| 464 | ✗ | case LBM_TYPE_CUSTOM: | |
| 465 | ✗ | r = print_emit_custom(chan, curr); | |
| 466 | ✗ | break; | |
| 467 | 12 | case LBM_TYPE_CHANNEL: | |
| 468 | 12 | r = print_emit_channel(chan, curr); | |
| 469 | 12 | break; | |
| 470 | 168 | case LBM_TYPE_ARRAY: | |
| 471 | 168 | r = print_emit_bytearray(chan, curr); | |
| 472 | 168 | break; | |
| 473 | ✗ | case LBM_TYPE_DEFRAG_MEM: | |
| 474 | ✗ | r = print_emit_defrag_mem(chan, curr); | |
| 475 | ✗ | break; | |
| 476 | ✗ | case LBM_TYPE_LISPARRAY: { | |
| 477 | ✗ | lbm_value cont[2] = {curr, START_ARRAY}; | |
| 478 | ✗ | int res = push_n(&print_stack, cont, 2); | |
| 479 | ✗ | if (!res) { | |
| 480 | ✗ | print_emit_string(chan, " ..."); | |
| 481 | ✗ | return EMIT_OK; | |
| 482 | } | ||
| 483 | ✗ | break; | |
| 484 | } | ||
| 485 | ✗ | default: | |
| 486 | ✗ | return EMIT_FAILED; | |
| 487 | } | ||
| 488 | 93600 | } | |
| 489 | } | ||
| 490 | 45136 | return r; | |
| 491 | } | ||
| 492 | |||
| 493 | 45140 | int lbm_print_value(char *buf, unsigned int len, lbm_value v) { | |
| 494 | |||
| 495 | lbm_string_channel_state_t st; | ||
| 496 | lbm_char_channel_t chan; | ||
| 497 | |||
| 498 | 45140 | memset(buf, 0, len); | |
| 499 | 45140 | lbm_create_string_char_channel_size(&st, &chan, buf, len); | |
| 500 |
2/2✓ Branch 1 taken 45108 times.
✓ Branch 2 taken 32 times.
|
45140 | if (lbm_print_internal(&chan,v) == EMIT_OK) |
| 501 | 45108 | return 1; | |
| 502 | 32 | return 0; | |
| 503 | } | ||
| 504 |