| 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}; | ||
| 43 | static bool print_has_stack = false; | ||
| 44 | |||
| 45 | const char *failed_str = "Error: print failed\n"; | ||
| 46 | |||
| 47 | // is_printable_string is turning out to be a headache. | ||
| 48 | // What do we want from this function??? | ||
| 49 | // | ||
| 50 | // Value | Print as | Condition | ||
| 51 | // [0] | [0] | | ||
| 52 | // [1] | [1] | | ||
| 53 | // "" | [0] | (array->size <= 1) => false | ||
| 54 | // "hej" | "hej" | printable characters followed by a 0 | ||
| 55 | // [65 66 67 0 65 66 67 0] | [65 66 67 0 65 66 67 0] | position of first 0 after printable characters = array->size-1 | ||
| 56 | // [0 65 66 0] | [0 65 66 0] | position of first 0 after printable characters = array->size-1 | ||
| 57 | 6573 | bool lbm_value_is_printable_string(lbm_value v, char **str) { | |
| 58 | 6573 | bool is_a_string = false; | |
| 59 | 6573 | lbm_array_header_t *array = lbm_dec_array_r(v); | |
| 60 |
2/2✓ Branch 0 taken 5564 times.
✓ Branch 1 taken 1009 times.
|
6573 | if (array) { |
| 61 | 5564 | char *c_data = (char *)array->data; | |
| 62 |
2/2✓ Branch 0 taken 5050 times.
✓ Branch 1 taken 514 times.
|
5564 | if (array->size > 1) { // nonzero length |
| 63 | 5050 | unsigned int i = 0; | |
| 64 | 5050 | is_a_string = true; | |
| 65 |
2/2✓ Branch 0 taken 17416 times.
✓ Branch 1 taken 4 times.
|
17420 | for (i = 0; i < array->size; i ++) { |
| 66 |
2/2✓ Branch 0 taken 3560 times.
✓ Branch 1 taken 13856 times.
|
17416 | if (c_data[i] == 0) break; |
| 67 |
5/6✓ Branch 0 taken 1519 times.
✓ Branch 1 taken 12337 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 1486 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 33 times.
|
13856 | if (!isprint((unsigned char)c_data[i]) && ((c_data[i] < 8) || c_data[i] > 13)) { |
| 68 | 1486 | is_a_string = false; | |
| 69 | 1486 | break; | |
| 70 | } | ||
| 71 | } | ||
| 72 |
5/6✓ Branch 0 taken 2036 times.
✓ Branch 1 taken 3014 times.
✓ Branch 2 taken 172 times.
✓ Branch 3 taken 1864 times.
✓ Branch 4 taken 172 times.
✗ Branch 5 not taken.
|
5050 | if (i > 0 && i != array->size-1 && c_data[i-1] != 0) is_a_string = false; |
| 73 |
2/2✓ Branch 0 taken 3182 times.
✓ Branch 1 taken 1868 times.
|
5050 | if (array->size-1 > i) is_a_string = false; |
| 74 |
2/2✓ Branch 0 taken 1864 times.
✓ Branch 1 taken 3186 times.
|
5050 | if (is_a_string) { |
| 75 | 1864 | *str = (char*)array->data; | |
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | 6573 | return is_a_string; | |
| 80 | } | ||
| 81 | |||
| 82 | 572348 | static int push_n(lbm_stack_t *s, lbm_uint *values, lbm_uint n) { | |
| 83 |
1/2✓ Branch 0 taken 572348 times.
✗ Branch 1 not taken.
|
572348 | if (s->sp + n < s->size) { |
| 84 |
2/2✓ Branch 0 taken 1154159 times.
✓ Branch 1 taken 572348 times.
|
1726507 | for (lbm_uint i = 0; i < n; i ++) { |
| 85 | 1154159 | s->data[s->sp+i] = values[i]; | |
| 86 | } | ||
| 87 | 572348 | s->sp+=n; | |
| 88 | 572348 | return 1; | |
| 89 | } | ||
| 90 | ✗ | return 0; | |
| 91 | } | ||
| 92 | |||
| 93 | 22390 | int lbm_print_init(lbm_uint print_stack_size) { | |
| 94 | |||
| 95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22390 times.
|
22390 | if (print_stack_size == 0) |
| 96 | ✗ | return 0; | |
| 97 | |||
| 98 | 22390 | lbm_uint *print_stack_storage = (lbm_uint*)lbm_malloc(print_stack_size * sizeof(lbm_uint)); | |
| 99 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22390 times.
|
22390 | if (!print_stack_storage) return 0; |
| 100 | |||
| 101 |
1/2✓ Branch 0 taken 22390 times.
✗ Branch 1 not taken.
|
22390 | if (lbm_stack_create(&print_stack, print_stack_storage, print_stack_size)) { |
| 102 | 22390 | print_has_stack = true; | |
| 103 | 22390 | return 1; | |
| 104 | } | ||
| 105 | ✗ | return 0; | |
| 106 | } | ||
| 107 | |||
| 108 | #define EMIT_BUFFER_SIZE 30 | ||
| 109 | |||
| 110 | #define EMIT_FAILED -1 | ||
| 111 | #define EMIT_OK 0 | ||
| 112 | |||
| 113 | 704315 | static int print_emit_string(lbm_char_channel_t *chan, char* str) { | |
| 114 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 704301 times.
|
704315 | if (str == NULL) return EMIT_FAILED; |
| 115 |
2/2✓ Branch 0 taken 1999692 times.
✓ Branch 1 taken 450589 times.
|
2450281 | while (*str != 0) { |
| 116 | 1999692 | int r = lbm_channel_write(chan, *str); | |
| 117 | 1999692 | str++; | |
| 118 |
2/2✓ Branch 0 taken 253712 times.
✓ Branch 1 taken 1745980 times.
|
1999692 | if (r != CHANNEL_SUCCESS) return EMIT_FAILED; |
| 119 | } | ||
| 120 | 450589 | return EMIT_OK; | |
| 121 | } | ||
| 122 | |||
| 123 | 224124 | static int print_emit_char(lbm_char_channel_t *chan, char c) { | |
| 124 | |||
| 125 | 224124 | int r = lbm_channel_write(chan, c); | |
| 126 |
2/2✓ Branch 0 taken 430 times.
✓ Branch 1 taken 223694 times.
|
224124 | if (r != CHANNEL_SUCCESS) return EMIT_FAILED; |
| 127 | 223694 | return EMIT_OK; | |
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | 2920 | static int emit_escape(lbm_char_channel_t *chan, char c) { | |
| 132 |
3/6✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
✓ Branch 5 taken 2776 times.
|
2920 | switch(c) { |
| 133 | 60 | case '"': return print_emit_string(chan, "\\\""); | |
| 134 | ✗ | case '\n': return print_emit_string(chan, "\\n"); | |
| 135 | ✗ | case '\r': return print_emit_string(chan, "\\r"); | |
| 136 | ✗ | case '\t': return print_emit_string(chan, "\\t"); | |
| 137 | 84 | case '\\': return print_emit_string(chan, "\\\\"); | |
| 138 | 2776 | default: | |
| 139 | 2776 | return print_emit_char(chan, c); | |
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | 685 | static int print_emit_string_value(lbm_char_channel_t *chan, char* str) { | |
| 144 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 685 times.
|
685 | if (str == NULL) return EMIT_FAILED; |
| 145 |
2/2✓ Branch 0 taken 2920 times.
✓ Branch 1 taken 685 times.
|
3605 | while (*str != 0) { |
| 146 | 2920 | int r = emit_escape(chan, *str++); | |
| 147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2920 times.
|
2920 | if (r != EMIT_OK) return r; |
| 148 | } | ||
| 149 | 685 | return EMIT_OK; | |
| 150 | } | ||
| 151 | |||
| 152 | 268424 | static int print_emit_symbol(lbm_char_channel_t *chan, lbm_value sym) { | |
| 153 | 268424 | char *str_ptr = (char*)lbm_get_name_by_symbol(lbm_dec_sym(sym)); | |
| 154 | 268424 | return print_emit_string(chan, str_ptr); | |
| 155 | } | ||
| 156 | |||
| 157 | 42446 | static int print_emit_i(lbm_char_channel_t *chan, lbm_int v) { | |
| 158 | char buf[EMIT_BUFFER_SIZE]; | ||
| 159 | 42446 | snprintf(buf, EMIT_BUFFER_SIZE, "%"PRI_INT, v); | |
| 160 | 42446 | return print_emit_string(chan, buf); | |
| 161 | } | ||
| 162 | |||
| 163 | 21610 | static int print_emit_u(lbm_char_channel_t *chan, lbm_uint v, bool ps) { | |
| 164 | char buf[EMIT_BUFFER_SIZE]; | ||
| 165 |
1/2✓ Branch 0 taken 21610 times.
✗ Branch 1 not taken.
|
21610 | snprintf(buf, EMIT_BUFFER_SIZE, "%"PRI_UINT"%s", v, ps ? "u" : ""); |
| 166 | 21610 | return print_emit_string(chan, buf); | |
| 167 | } | ||
| 168 | |||
| 169 | 301918 | static int print_emit_byte(lbm_char_channel_t *chan, uint8_t v, bool ps) { | |
| 170 | char buf[EMIT_BUFFER_SIZE]; | ||
| 171 |
2/2✓ Branch 0 taken 1008 times.
✓ Branch 1 taken 300910 times.
|
301918 | snprintf(buf, EMIT_BUFFER_SIZE, "%u%s", v, ps ? "b" : ""); |
| 172 | 301918 | return print_emit_string(chan, buf); | |
| 173 | } | ||
| 174 | |||
| 175 | 432 | static int print_emit_float(lbm_char_channel_t *chan, float v, bool ps) { | |
| 176 | char buf[EMIT_BUFFER_SIZE]; | ||
| 177 |
1/2✓ Branch 0 taken 432 times.
✗ Branch 1 not taken.
|
432 | snprintf(buf, EMIT_BUFFER_SIZE, "%"PRI_FLOAT"%s", (double)v, ps ? "f32" : ""); |
| 178 | 432 | return print_emit_string(chan, buf); | |
| 179 | } | ||
| 180 | |||
| 181 | 339 | static int print_emit_double(lbm_char_channel_t *chan, double v, bool ps) { | |
| 182 | char buf[EMIT_BUFFER_SIZE]; | ||
| 183 |
1/2✓ Branch 0 taken 339 times.
✗ Branch 1 not taken.
|
339 | snprintf(buf, EMIT_BUFFER_SIZE, "%lf%s", v, ps ? "f64" : ""); |
| 184 | 339 | return print_emit_string(chan, buf); | |
| 185 | } | ||
| 186 | |||
| 187 | 1130 | static int print_emit_u32(lbm_char_channel_t *chan, uint32_t v, bool ps) { | |
| 188 | char buf[EMIT_BUFFER_SIZE]; | ||
| 189 |
1/2✓ Branch 0 taken 1130 times.
✗ Branch 1 not taken.
|
1130 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRIu32"%s", v, ps ? "u32" : ""); |
| 190 | 1130 | return print_emit_string(chan, buf); | |
| 191 | } | ||
| 192 | |||
| 193 | 112 | static int print_emit_i32(lbm_char_channel_t *chan, int32_t v, bool ps) { | |
| 194 | char buf[EMIT_BUFFER_SIZE]; | ||
| 195 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRId32"%s", v, ps ? "i32" : ""); |
| 196 | 112 | return print_emit_string(chan, buf); | |
| 197 | } | ||
| 198 | |||
| 199 | 112 | static int print_emit_u64(lbm_char_channel_t *chan, uint64_t v, bool ps) { | |
| 200 | char buf[EMIT_BUFFER_SIZE]; | ||
| 201 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRIu64"%s", v, ps ? "u64" : ""); |
| 202 | 112 | return print_emit_string(chan, buf); | |
| 203 | } | ||
| 204 | |||
| 205 | 112 | static int print_emit_i64(lbm_char_channel_t *chan, int64_t v, bool ps) { | |
| 206 | char buf[EMIT_BUFFER_SIZE]; | ||
| 207 |
1/2✓ Branch 0 taken 112 times.
✗ Branch 1 not taken.
|
112 | snprintf(buf,EMIT_BUFFER_SIZE, "%"PRId64"%s", v, ps ? "i64" : ""); |
| 208 | 112 | return print_emit_string(chan, buf); | |
| 209 | } | ||
| 210 | |||
| 211 | 50874 | static int print_emit_continuation(lbm_char_channel_t *chan, lbm_value v) { | |
| 212 | char buf[EMIT_BUFFER_SIZE]; | ||
| 213 | 50874 | lbm_uint cont = (v & ~LBM_CONTINUATION_INTERNAL) >> LBM_ADDRESS_SHIFT; | |
| 214 | 50874 | snprintf(buf, EMIT_BUFFER_SIZE, "CONT[" "%"PRI_UINT"]", cont); | |
| 215 | 50874 | return print_emit_string(chan, buf); | |
| 216 | } | ||
| 217 | |||
| 218 | 2 | static int print_emit_custom(lbm_char_channel_t *chan, lbm_value v) { | |
| 219 | 2 | lbm_uint *custom = (lbm_uint*)lbm_car(v); | |
| 220 | int r; // NULL checks works against SYM_NIL. | ||
| 221 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | if (custom && custom[CUSTOM_TYPE_DESCRIPTOR]) { |
| 222 | 2 | r = print_emit_string(chan, (char*)custom[CUSTOM_TYPE_DESCRIPTOR]); | |
| 223 | } else { | ||
| 224 | ✗ | r = print_emit_string(chan, "INVALID_CUSTOM_TYPE"); | |
| 225 | } | ||
| 226 | 2 | return r; | |
| 227 | } | ||
| 228 | |||
| 229 | 56 | static int print_emit_defrag_mem(lbm_char_channel_t *chan, lbm_value v) { | |
| 230 | (void) v; | ||
| 231 | 56 | return print_emit_string(chan, "DM"); | |
| 232 | } | ||
| 233 | |||
| 234 | 12108 | static int print_emit_channel(lbm_char_channel_t *chan, lbm_value v) { | |
| 235 | (void) v; | ||
| 236 | 12108 | return print_emit_string(chan, "~CHANNEL~"); | |
| 237 | } | ||
| 238 | |||
| 239 | 3546 | static int print_emit_array_data(lbm_char_channel_t *chan, lbm_array_header_t *array) { | |
| 240 | |||
| 241 | 3546 | int r = print_emit_char(chan, '['); | |
| 242 | |||
| 243 |
1/2✓ Branch 0 taken 3546 times.
✗ Branch 1 not taken.
|
3546 | if (r == EMIT_OK) { |
| 244 | |||
| 245 |
2/2✓ Branch 0 taken 300910 times.
✓ Branch 1 taken 3546 times.
|
304456 | for (unsigned int i = 0; i < array->size; i ++) { |
| 246 | |||
| 247 | 300910 | char *c_data = (char*)array->data; | |
| 248 | 300910 | r = print_emit_byte(chan, (uint8_t)c_data[i], false); | |
| 249 | |||
| 250 |
4/4✓ Branch 0 taken 47286 times.
✓ Branch 1 taken 253624 times.
✓ Branch 2 taken 44468 times.
✓ Branch 3 taken 2818 times.
|
300910 | if (r == EMIT_OK && i != array->size - 1) { |
| 251 | 44468 | r = print_emit_char(chan, ' '); | |
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 |
2/2✓ Branch 0 taken 280 times.
✓ Branch 1 taken 3266 times.
|
3546 | if (r != EMIT_OK) return r; |
| 256 | 3266 | return print_emit_char(chan, ']'); | |
| 257 | } | ||
| 258 | ✗ | return r; | |
| 259 | } | ||
| 260 | |||
| 261 | 4455 | static int print_emit_bytearray(lbm_char_channel_t *chan, lbm_value v) { | |
| 262 | 4455 | int r = 0; | |
| 263 | char *str; | ||
| 264 | 4455 | lbm_array_header_t *array = lbm_dec_array_r(v); | |
| 265 |
2/2✓ Branch 0 taken 4231 times.
✓ Branch 1 taken 224 times.
|
4455 | if (array) { |
| 266 |
2/2✓ Branch 0 taken 685 times.
✓ Branch 1 taken 3546 times.
|
4231 | if (lbm_value_is_printable_string(v, &str)) { |
| 267 | 685 | r = print_emit_char(chan, '"'); | |
| 268 |
1/2✓ Branch 0 taken 685 times.
✗ Branch 1 not taken.
|
685 | if (r == EMIT_OK) { |
| 269 | 685 | r = print_emit_string_value(chan, str); | |
| 270 |
1/2✓ Branch 0 taken 685 times.
✗ Branch 1 not taken.
|
685 | if (r == EMIT_OK) { |
| 271 | 685 | r = print_emit_char(chan, '"'); | |
| 272 | } | ||
| 273 | } | ||
| 274 | } else { | ||
| 275 | 3546 | r= print_emit_array_data(chan, array); | |
| 276 | } | ||
| 277 | } else { | ||
| 278 | 224 | r = print_emit_string(chan, "[INVALID_ARRAY]"); | |
| 279 | } | ||
| 280 | 4455 | return r; | |
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | 336466 | static int lbm_print_internal(lbm_char_channel_t *chan, lbm_value v) { | |
| 285 | |||
| 286 | 336466 | lbm_stack_clear(&print_stack); | |
| 287 | 336466 | lbm_value start_print[2] = {v, PRINT}; | |
| 288 | 336466 | push_n(&print_stack, start_print, 2); | |
| 289 | 336466 | bool chan_full = false; | |
| 290 | lbm_value curr; | ||
| 291 | lbm_uint instr; | ||
| 292 | 336466 | int r = EMIT_FAILED; | |
| 293 | |||
| 294 |
3/4✓ Branch 0 taken 629576 times.
✓ Branch 1 taken 336288 times.
✓ Branch 2 taken 629576 times.
✗ Branch 3 not taken.
|
965864 | while (!lbm_stack_is_empty(&print_stack) && !chan_full) { |
| 295 | 629576 | lbm_pop(&print_stack, &instr); | |
| 296 |
8/10✓ Branch 0 taken 185 times.
✓ Branch 1 taken 553 times.
✓ Branch 2 taken 185 times.
✓ Branch 3 taken 53016 times.
✓ Branch 4 taken 62606 times.
✓ Branch 5 taken 52708 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3902 times.
✓ Branch 8 taken 456421 times.
✗ Branch 9 not taken.
|
629576 | switch (instr) { |
| 297 | 185 | case START_ARRAY: { | |
| 298 | 185 | lbm_pop(&print_stack, &curr); | |
| 299 | 185 | int res = 1; | |
| 300 | 185 | r = print_emit_string(chan, "[|"); | |
| 301 | 185 | lbm_array_header_t *arr = (lbm_array_header_t*)lbm_car(curr); | |
| 302 | 185 | lbm_uint size = arr->size / sizeof(lbm_value); | |
| 303 | 185 | lbm_value *arrdata = (lbm_value*)arr->data; | |
| 304 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | if (size >= 1) { |
| 305 | 185 | lbm_value continuation[5] = | |
| 306 | {1, // next index | ||
| 307 | 185 | (lbm_uint) arr, | |
| 308 | CONTINUE_ARRAY, | ||
| 309 | 185 | arrdata[0], // first elt | |
| 310 | PRINT}; | ||
| 311 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | res = res && push_n(&print_stack, continuation, 5); |
| 312 | } else { | ||
| 313 | ✗ | res = res && lbm_push(&print_stack, END_ARRAY); | |
| 314 | } | ||
| 315 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
|
185 | if (!res) { |
| 316 | ✗ | return EMIT_FAILED; | |
| 317 | } | ||
| 318 | 185 | break; | |
| 319 | } | ||
| 320 | 553 | case CONTINUE_ARRAY: { | |
| 321 | lbm_uint arr_ptr; | ||
| 322 | lbm_array_header_t *arr; | ||
| 323 | lbm_uint ix; | ||
| 324 | 553 | int res = 1; | |
| 325 | 553 | lbm_pop_2(&print_stack, &arr_ptr, &ix); | |
| 326 | 553 | arr = (lbm_array_header_t *)arr_ptr; | |
| 327 | 553 | lbm_value *arrdata = (lbm_value*)arr->data; | |
| 328 |
2/2✓ Branch 0 taken 368 times.
✓ Branch 1 taken 185 times.
|
553 | if (ix < (arr->size / sizeof(lbm_value))) { |
| 329 | 368 | r = print_emit_char(chan, ' '); | |
| 330 | 368 | lbm_value continuation[5] = | |
| 331 | 368 | {ix + 1, | |
| 332 | 368 | (lbm_uint) arr, | |
| 333 | CONTINUE_ARRAY, | ||
| 334 | 368 | arrdata[ix], | |
| 335 | PRINT}; | ||
| 336 |
2/4✓ Branch 0 taken 368 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 368 times.
✗ Branch 3 not taken.
|
368 | res = res && push_n(&print_stack, continuation, 5); |
| 337 | } else { | ||
| 338 |
2/4✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
|
185 | res = res && lbm_push(&print_stack, END_ARRAY); |
| 339 | } | ||
| 340 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 553 times.
|
553 | if (!res) { |
| 341 | ✗ | return EMIT_FAILED; | |
| 342 | } | ||
| 343 | 553 | break; | |
| 344 | } | ||
| 345 | 185 | case END_ARRAY: { | |
| 346 | 185 | r = print_emit_string(chan, "|]"); | |
| 347 | 185 | break; | |
| 348 | } | ||
| 349 | 53016 | case START_LIST: { | |
| 350 | 53016 | lbm_pop(&print_stack, &curr); | |
| 351 | 53016 | r = print_emit_char(chan, '('); | |
| 352 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 53012 times.
|
53016 | if (r != EMIT_OK) return r; |
| 353 | 53012 | lbm_value car_val = lbm_car(curr); | |
| 354 | 53012 | lbm_value cdr_val = lbm_cdr(curr); | |
| 355 | 53012 | int res = 1; | |
| 356 |
4/4✓ Branch 0 taken 8619 times.
✓ Branch 1 taken 44393 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 8605 times.
|
61631 | if (lbm_type_of(cdr_val) == LBM_TYPE_CONS || |
| 357 | 8619 | lbm_type_of(cdr_val) == (LBM_TYPE_CONS | LBM_PTR_TO_CONSTANT_BIT)) { | |
| 358 | 44407 | lbm_value cont[2] = {cdr_val, CONTINUE_LIST}; | |
| 359 |
2/4✓ Branch 0 taken 44407 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44407 times.
✗ Branch 3 not taken.
|
44407 | res = res && push_n(&print_stack, cont, 2); |
| 360 |
4/4✓ Branch 0 taken 5157 times.
✓ Branch 1 taken 3448 times.
✓ Branch 2 taken 4708 times.
✓ Branch 3 taken 449 times.
|
8605 | } else if (lbm_type_of(cdr_val) == LBM_TYPE_SYMBOL && |
| 361 | cdr_val == ENC_SYM_NIL) { | ||
| 362 |
2/4✓ Branch 0 taken 4708 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4708 times.
✗ Branch 3 not taken.
|
4708 | res = res && lbm_push(&print_stack, END_LIST); |
| 363 | } else { | ||
| 364 | 3897 | lbm_value cont[4] = {END_LIST, cdr_val, PRINT, PRINT_DOT}; | |
| 365 |
2/4✓ Branch 0 taken 3897 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3897 times.
✗ Branch 3 not taken.
|
3897 | res = res && push_n(&print_stack, cont, 4); |
| 366 | } | ||
| 367 | 53012 | lbm_value cont[2] = {car_val, PRINT}; | |
| 368 |
2/4✓ Branch 0 taken 53012 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53012 times.
✗ Branch 3 not taken.
|
53012 | res = res && push_n(&print_stack, cont,2); |
| 369 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 53012 times.
|
53012 | if (!res) { |
| 370 | ✗ | return EMIT_FAILED; | |
| 371 | } | ||
| 372 | 53012 | break; | |
| 373 | } | ||
| 374 | 62606 | case CONTINUE_LIST: { | |
| 375 | 62606 | int res = 1; | |
| 376 | 62606 | lbm_pop(&print_stack, &curr); | |
| 377 | |||
| 378 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62606 times.
|
62606 | if (lbm_type_of(curr) == LBM_TYPE_SYMBOL && |
| 379 | ✗ | curr == ENC_SYM_NIL) { | |
| 380 | 62516 | break; | |
| 381 | } | ||
| 382 | |||
| 383 | 62606 | lbm_value car_val = lbm_car(curr); | |
| 384 | 62606 | lbm_value cdr_val = lbm_cdr(curr); | |
| 385 | |||
| 386 | 62606 | r = print_emit_char(chan, ' '); | |
| 387 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 62516 times.
|
62606 | if (r != EMIT_OK) { |
| 388 | 90 | return r; | |
| 389 | } | ||
| 390 |
4/4✓ Branch 0 taken 44233 times.
✓ Branch 1 taken 18283 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 44225 times.
|
106749 | if (lbm_type_of(cdr_val) == LBM_TYPE_CONS || |
| 391 | 44233 | lbm_type_of(cdr_val) == (LBM_TYPE_CONS | LBM_PTR_TO_CONSTANT_BIT)) { | |
| 392 | 18291 | lbm_value cont[2] = {cdr_val, CONTINUE_LIST}; | |
| 393 |
2/4✓ Branch 0 taken 18291 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18291 times.
✗ Branch 3 not taken.
|
18291 | res = res && push_n(&print_stack, cont, 2); |
| 394 |
3/4✓ Branch 0 taken 44220 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 44220 times.
✗ Branch 3 not taken.
|
44225 | } else if (lbm_type_of(cdr_val) == LBM_TYPE_SYMBOL && |
| 395 | cdr_val == ENC_SYM_NIL) { | ||
| 396 |
2/4✓ Branch 0 taken 44220 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44220 times.
✗ Branch 3 not taken.
|
44220 | res = res && lbm_push(&print_stack, END_LIST); |
| 397 | } else { | ||
| 398 | 5 | lbm_value cont[4] = {END_LIST, cdr_val, PRINT, PRINT_DOT}; | |
| 399 |
2/4✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | res = res && push_n(&print_stack, cont, 4); |
| 400 | } | ||
| 401 | 62516 | lbm_value cont[2] = {car_val, PRINT}; | |
| 402 |
2/4✓ Branch 0 taken 62516 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 62516 times.
✗ Branch 3 not taken.
|
62516 | res = res && push_n(&print_stack, cont, 2); |
| 403 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 62516 times.
|
62516 | if (!res) { |
| 404 | ✗ | return EMIT_FAILED; | |
| 405 | } | ||
| 406 | 62516 | break; | |
| 407 | } | ||
| 408 | 52708 | case END_LIST: | |
| 409 | 52708 | r = print_emit_char(chan, ')'); | |
| 410 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 52652 times.
|
52708 | if (r != EMIT_OK) return r; |
| 411 | 52652 | break; | |
| 412 | ✗ | case PRINT_SPACE: | |
| 413 | ✗ | r = print_emit_char(chan, ' '); | |
| 414 | ✗ | if (r != EMIT_OK) return r; | |
| 415 | ✗ | break; | |
| 416 | 3902 | case PRINT_DOT: | |
| 417 | 3902 | r = print_emit_string(chan, " . "); | |
| 418 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 3874 times.
|
3902 | if (r != EMIT_OK) return r; |
| 419 | 3874 | break; | |
| 420 | 456421 | case PRINT: | |
| 421 | 456421 | lbm_pop(&print_stack, &curr); | |
| 422 | |||
| 423 | 456421 | lbm_type t = lbm_type_of(curr); | |
| 424 |
2/2✓ Branch 0 taken 122933 times.
✓ Branch 1 taken 333488 times.
|
456421 | if (lbm_is_ptr(curr)) |
| 425 | 122933 | t = t & LBM_PTR_TO_CONSTANT_MASK; // print constants normally | |
| 426 | |||
| 427 | switch(t) { | ||
| 428 | 53016 | case LBM_TYPE_CONS: { | |
| 429 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 53016 times.
|
53016 | if (lbm_dec_ptr(curr) == LBM_PTR_NULL) { |
| 430 | ✗ | print_emit_string(chan, " LBM_NULL "); | |
| 431 | } else { | ||
| 432 | 53016 | lbm_value cont[2] = {curr, START_LIST}; | |
| 433 | 53016 | int res = push_n(&print_stack, cont, 2); | |
| 434 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 53016 times.
|
53016 | if (!res) { |
| 435 | ✗ | print_emit_string(chan," ..."); | |
| 436 | ✗ | return EMIT_OK; | |
| 437 | } | ||
| 438 | } | ||
| 439 | 53016 | break; | |
| 440 | } | ||
| 441 | 268424 | case LBM_TYPE_SYMBOL: | |
| 442 | 268424 | r = print_emit_symbol(chan, curr); | |
| 443 | 268424 | break; | |
| 444 | 42446 | case LBM_TYPE_I: | |
| 445 | 42446 | r = print_emit_i(chan, lbm_dec_i(curr)); | |
| 446 | 42446 | break; | |
| 447 | 21610 | case LBM_TYPE_U: | |
| 448 | 21610 | r = print_emit_u(chan, lbm_dec_u(curr), true); | |
| 449 | 21610 | break; | |
| 450 | 1008 | case LBM_TYPE_CHAR: | |
| 451 | 1008 | r = print_emit_byte(chan, (uint8_t)lbm_dec_char(curr), true); | |
| 452 | 1008 | break; | |
| 453 | 432 | case LBM_TYPE_FLOAT: | |
| 454 | 432 | r = print_emit_float(chan, lbm_dec_float(curr), true); | |
| 455 | 432 | break; | |
| 456 | 339 | case LBM_TYPE_DOUBLE: | |
| 457 | 339 | r = print_emit_double(chan, lbm_dec_double(curr), true); | |
| 458 | 339 | break; | |
| 459 | 1130 | case LBM_TYPE_U32: | |
| 460 | 1130 | r = print_emit_u32(chan, lbm_dec_u32(curr), true); | |
| 461 | 1130 | break; | |
| 462 | 112 | case LBM_TYPE_I32: | |
| 463 | 112 | r = print_emit_i32(chan, lbm_dec_i32(curr), true); | |
| 464 | 112 | break; | |
| 465 | 112 | case LBM_TYPE_U64: | |
| 466 | 112 | r = print_emit_u64(chan, lbm_dec_u64(curr), true); | |
| 467 | 112 | break; | |
| 468 | 112 | case LBM_TYPE_I64: | |
| 469 | 112 | r = print_emit_i64(chan, lbm_dec_i64(curr), true); | |
| 470 | 112 | break; | |
| 471 | 50874 | case LBM_CONTINUATION_INTERNAL_TYPE: | |
| 472 | 50874 | r = print_emit_continuation(chan, curr); | |
| 473 | 50874 | break; | |
| 474 | 2 | case LBM_TYPE_CUSTOM: | |
| 475 | 2 | r = print_emit_custom(chan, curr); | |
| 476 | 2 | break; | |
| 477 | 12108 | case LBM_TYPE_CHANNEL: | |
| 478 | 12108 | r = print_emit_channel(chan, curr); | |
| 479 | 12108 | break; | |
| 480 | 4455 | case LBM_TYPE_ARRAY: | |
| 481 | 4455 | r = print_emit_bytearray(chan, curr); | |
| 482 | 4455 | break; | |
| 483 | 56 | case LBM_TYPE_DEFRAG_MEM: | |
| 484 | 56 | r = print_emit_defrag_mem(chan, curr); | |
| 485 | 56 | break; | |
| 486 | 185 | case LBM_TYPE_LISPARRAY: { | |
| 487 | 185 | lbm_value cont[2] = {curr, START_ARRAY}; | |
| 488 | 185 | int res = push_n(&print_stack, cont, 2); | |
| 489 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
|
185 | if (!res) { |
| 490 | ✗ | print_emit_string(chan, " ..."); | |
| 491 | ✗ | return EMIT_OK; | |
| 492 | } | ||
| 493 | 185 | break; | |
| 494 | } | ||
| 495 | ✗ | default: | |
| 496 | ✗ | return EMIT_FAILED; | |
| 497 | } | ||
| 498 | } | ||
| 499 | } | ||
| 500 | 336288 | return r; | |
| 501 | } | ||
| 502 | |||
| 503 | 336466 | int lbm_print_value(char *buf, unsigned int len, lbm_value v) { | |
| 504 | |||
| 505 | lbm_string_channel_state_t st; | ||
| 506 | lbm_char_channel_t chan; | ||
| 507 | |||
| 508 | 336466 | memset(buf, 0, len); | |
| 509 | 336466 | lbm_create_string_char_channel_size(&st, &chan, buf, len); | |
| 510 |
2/2✓ Branch 0 taken 335994 times.
✓ Branch 1 taken 472 times.
|
336466 | if (lbm_print_internal(&chan,v) == EMIT_OK) |
| 511 | 335994 | return 1; | |
| 512 | 472 | return 0; | |
| 513 | } | ||
| 514 |