Commit 5b4ab876fea2b98d1b775fad0ccd64906a956f77

Thomas de Grivel 2024-08-09T21:10:34

wip buf_inspect pretty printer

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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index f83e914..2981238 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -535,7 +535,7 @@ sw buf_inspect_call_args_size (s_pretty *pretty, const s_list *args)
     return r;
   result += r;
   while (args) {
-    if ((r = buf_inspect_tag_size(&args->tag)) < 0)
+    if ((r = buf_inspect_tag_size(pretty, &args->tag)) < 0)
       return r;
     result += r;
     if ((args = list_next(args))) {
@@ -608,14 +608,14 @@ sw buf_inspect_call_brackets_size (s_pretty *pretty, const s_call *call)
   address = &next->tag.data.array;
   assert(address->dimension == 1);
   array = &call->arguments->tag;
-  if ((r = buf_inspect_tag_size(array)) < 0)
+  if ((r = buf_inspect_tag_size(pretty, array)) < 0)
     return r;
   result += r;
   while (i < address->dimensions[0].count) {
     if ((r = buf_write_1_size(pretty, "[")) < 0)
       return r;
     result += r;
-    if ((r = buf_inspect_uw_size(((uw *) address->data)
+    if ((r = buf_inspect_uw_size(pretty, ((uw *) address->data)
                                  + i)) < 0)
       return r;
     result += r;
@@ -687,8 +687,67 @@ sw buf_inspect_call_if_then_else (s_buf *buf, const s_call *call)
 
 sw buf_inspect_call_if_then_else_size (s_pretty *pretty, const s_call *call)
 {
-  (void) call;
-  return -1;
+  s_tag *condition;
+  s_tag *else_;
+  uw i;
+  s_pretty pretty_save;
+  sw r;
+  sw result = 0;
+  s_tag *then;
+  if (call->ident.module != &g_sym_KC3 ||
+      call->ident.sym != &g_sym_if_then_else ||
+      list_length(call->arguments) != 3)
+    return -2;
+  if ((r = buf_write_1_size(pretty, "if ")) < 0)
+    return r;
+  result += r;
+  condition = &call->arguments->tag;
+  if ((r = buf_inspect_tag_size(pretty, condition)) < 0)
+    return r;
+  result += r;
+  then = &list_next(call->arguments)->tag;
+    pretty_save = *pretty;
+  if (then->type == TAG_BLOCK) {
+    if ((r = buf_write_1_size(pretty, " do")) < 0)
+      return r;
+    result += r;
+    pretty_indent(pretty, 2);
+    i = 0;
+    while (i < then->data.block.count) {
+      if ((r = buf_write_1_size(pretty, "\n")) < 0)
+        return r;
+      result += r;
+      if ((r = buf_inspect_tag_size(pretty, then->data.block.tag + i)) < 0)
+        return r;
+      i++;
+    }
+  }
+  else {
+    pretty_indent(pretty, 2);
+    if ((r = buf_inspect_tag_size(pretty, then)) < 0)
+      return r;
+  }
+  result += r;
+  *pretty = pretty_save;
+  else_ = &list_next(list_next(call->arguments))->tag;
+  if (else_->type != TAG_VOID) {
+    if ((r = buf_write_1_size(pretty, "\nelse")) < 0)
+      return r;
+    result += r;
+    if (then->type == TAG_BLOCK) {
+      if ((r = buf_inspect_block_inner_size(pretty,
+                                            &else_->data.block)) < 0)
+        return r;
+    }
+    else
+      if ((r = buf_inspect_tag_size(pretty, else_)) < 0)
+        return r;
+    result += r;
+  }
+  if ((r = buf_write_1_size(pretty, "\nend")) < 0)
+    return r;
+  result += r;
+  return result;
 }
 
 sw buf_inspect_call_op (s_buf *buf, const s_call *call, s8 op_precedence)
@@ -762,9 +821,71 @@ sw buf_inspect_call_op (s_buf *buf, const s_call *call, s8 op_precedence)
 
 sw buf_inspect_call_op_size (s_pretty *pretty, const s_call *call, s8 op_precedence)
 {
-  (void) call;
-  (void) op_precedence;
-  return -1;
+  s_ident ident;
+  s_tag *left;
+  bool op;
+  bool paren = false;
+  s8 precedence;
+  sw r;
+  sw result = 0;
+  s_tag *right;
+  left = &call->arguments->tag;
+  assert(list_next(call->arguments));
+  right = &list_next(call->arguments)->tag;
+  if (left->type == TAG_CALL) {
+    if (! operator_find(&left->data.call.ident, &op))
+      return -1;
+    if (op &&
+        (precedence = operator_precedence(&left->data.call.ident))
+        < op_precedence) {
+      paren = true;
+      if ((r = buf_write_1_size(pretty, "(")) < 0)
+        return r;
+      result += r;
+    }
+  }
+  if ((r = buf_inspect_tag_size(pretty, left)) < 0)
+    return r;
+  result += r;
+  if (paren) {
+    if ((r = buf_write_1_size(pretty, ")")) < 0)
+      return r;
+    result += r;
+  }
+  if ((r = buf_write_1_size(pretty, " ")) < 0)
+    return r;
+  result += r;
+  if (! operator_ident(&call->ident, &ident))
+    return -1;
+  if ((r = buf_inspect_ident_size(pretty, &ident)) < 0)
+    return r;
+  result += r;
+  if ((r = buf_write_1_size(pretty, " ")) < 0)
+    return r;
+  result += r;
+  if (right->type == TAG_CALL) {
+    if (! operator_find(&right->data.call.ident, &op))
+      return -1;
+    if (op &&
+        (precedence = operator_precedence(&right->data.call.ident))
+        < op_precedence) {
+      paren = true;
+      if ((r = buf_write_1_size(pretty, "(")) < 0)
+        return r;
+      result += r;
+    }
+    else
+      paren = false;
+  }
+  if ((r = buf_inspect_tag_size(pretty, right)) < 0)
+    return r;
+  result += r;
+  if (paren) {
+    if ((r = buf_write_1_size(pretty, ")")) < 0)
+      return r;
+    result += r;
+  }
+  return result;
 }
 
 sw buf_inspect_call_op_unary (s_buf *buf, const s_call *call)
@@ -828,40 +949,40 @@ sw buf_inspect_call_size (s_pretty *pretty, const s_call *call)
   const s_sym *sym;
   if (call->ident.module == &g_sym_KC3 &&
       call->ident.sym == &g_sym_access)
-    return buf_inspect_call_access_size(call);
+    return buf_inspect_call_access_size(pretty, call);
   if (call->ident.module == &g_sym_KC3 &&
       call->ident.sym == &g_sym_if_then_else)
-    return buf_inspect_call_if_then_else_size(call);
+    return buf_inspect_call_if_then_else_size(pretty, call);
   if ((! call->ident.module ||
        call->ident.module == &g_sym_KC3) &&
       call->ident.sym == &g_sym_str &&
       call->arguments &&
       call->arguments->tag.type == TAG_LIST)
-    return buf_inspect_call_str_size(call);
+    return buf_inspect_call_str_size(pretty, call);
   if (! operator_find(&call->ident, &op))
     return -1;
   if (op) {
     if (! operator_symbol(&call->ident, &sym))
       return -1;
     if (sym == &g_sym__brackets)
-      return buf_inspect_call_brackets_size(call);
+      return buf_inspect_call_brackets_size(pretty, call);
   }
   if (call->ident.sym == &g_sym_cast)
-    return buf_inspect_cast_size(call);
+    return buf_inspect_cast_size(pretty, call);
   op_arity = op ? operator_arity(&call->ident) : 0;
   if (op_arity == 1)
-    return buf_inspect_call_op_unary_size(call);
+    return buf_inspect_call_op_unary_size(pretty, call);
   if (op_arity == 2 &&
       (op_precedence = operator_precedence(&call->ident)) > 0)
-    return buf_inspect_call_op_size(call, op_precedence);
+    return buf_inspect_call_op_size(pretty, call, op_precedence);
   if (! ident_is_special_operator(&call->ident, &b))
     return -1;
   if (b)
-    return buf_inspect_call_special_operator_size(call);
-  if ((r = buf_inspect_ident_size(&call->ident)) < 0)
+    return buf_inspect_call_special_operator_size(pretty, call);
+  if ((r = buf_inspect_ident_size(pretty, &call->ident)) < 0)
     return r;
   result += r;
-  if ((r = buf_inspect_call_args_size(call->arguments)) < 0)
+  if ((r = buf_inspect_call_args_size(pretty, call->arguments)) < 0)
     return r;
   result += r;
   return result;
@@ -930,14 +1051,14 @@ sw buf_inspect_call_special_operator_size (s_pretty *pretty, const s_call *call)
     return -1;
   }
   args = call->arguments;
-  if ((r = buf_inspect_ident_size(&call->ident)) < 0)
+  if ((r = buf_inspect_ident_size(pretty, &call->ident)) < 0)
     return r;
   result += r;
   while (args) {
     if ((r = buf_write_1_size(pretty, " ")) < 0)
       return r;
     result += r;
-    if ((r = buf_inspect_tag_size(&args->tag)) < 0)
+    if ((r = buf_inspect_tag_size(pretty, &args->tag)) < 0)
       return r;
     result += r;
     args = list_next(args);
@@ -970,7 +1091,7 @@ sw buf_inspect_call_str_size (s_pretty *pretty, const s_call *call)
     assert(! "buf_inspect_call_str_size: invalid call");
     return -1;
   }
-  return buf_inspect_str_eval_size(call->arguments->tag.data.list);
+  return buf_inspect_str_eval_size(pretty, call->arguments->tag.data.list);
 }
 
 sw buf_inspect_cast (s_buf *buf, const s_call *call)
@@ -1063,7 +1184,7 @@ sw buf_inspect_cfn_size (s_pretty *pretty, const s_cfn *cfn)
   sw r;
   sw result = 0;
   assert(cfn);
-  if ((r = buf_inspect_str_size(&cfn->name->str)) < 0)
+  if ((r = buf_inspect_str_size(pretty, &cfn->name->str)) < 0)
     return r;
   result += r;
   if ((r = buf_write_1_size(pretty, "(")) < 0)
@@ -1071,7 +1192,7 @@ sw buf_inspect_cfn_size (s_pretty *pretty, const s_cfn *cfn)
   result += r;
   arg_type = cfn->arg_types;
   while (arg_type) {
-    if ((r = buf_inspect_tag_size(&arg_type->tag)) < 0)
+    if ((r = buf_inspect_tag_size(pretty, &arg_type->tag)) < 0)
       return r;
     arg_type = list_next(arg_type);
     if (arg_type) {
@@ -1638,7 +1759,7 @@ sw buf_inspect_fn_pattern_size (s_pretty *pretty, const s_list *pattern)
     return r;
   result += r;
   while (pattern) {
-    if ((r = buf_inspect_tag_size(&pattern->tag)) < 0)
+    if ((r = buf_inspect_tag_size(pretty, &pattern->tag)) < 0)
       return r;
     result += r;
     pattern = list_next(pattern);
@@ -1720,7 +1841,7 @@ sw buf_inspect_ident_size (s_pretty *pretty, const s_ident *ident)
   sw result = 0;
   assert(ident);
   if (ident->module) {
-    if ((r = buf_inspect_sym_size(&ident->module)) < 0)
+    if ((r = buf_inspect_sym_size(pretty, &ident->module)) < 0)
       return r;
     result += r;
     if ((r = buf_write_1_size(pretty, ".")) < 0)
@@ -1765,7 +1886,7 @@ sw buf_inspect_ident_sym_reserved_size (s_pretty *pretty, const s_sym *sym)
   if ((r = buf_write_1_size(pretty, "_")) < 0)
     return r;
   size += r;
-  if ((r = buf_inspect_str_size(&sym->str)) < 0)
+  if ((r = buf_inspect_str_size(pretty, &sym->str)) < 0)
     return r;
   size += r;
   return size;
@@ -1790,7 +1911,7 @@ sw buf_inspect_integer (s_buf *buf, const s_integer *x)
   const mp_digit radix = 10;
   s32 size = 0;
   mp_int t;
-  if (mp_radix_size(&x->mp_int, radix, &size) != MP_OKAY)
+  if (mp_radix_size(pretty, &x->mp_int, radix, &size) != MP_OKAY)
     return -1;
   maxlen = size;
   if (MP_IS_ZERO(&x->mp_int))
@@ -1826,7 +1947,7 @@ sw buf_inspect_integer (s_buf *buf, const s_integer *x)
 sw buf_inspect_integer_size (s_pretty *pretty, const s_integer *x)
 {
   int size;
-  if (mp_radix_size(&x->mp_int, 10, &size) != MP_OKAY)
+  if (mp_radix_size(pretty, &x->mp_int, 10, &size) != MP_OKAY)
     return -1;
   return size - 1;
 }
@@ -1919,7 +2040,7 @@ sw buf_inspect_list_size (s_pretty *pretty, const s_list * const *list)
   result += r;
   i = *list;
   while (i) {
-    if ((r = buf_inspect_list_tag_size(&i->tag)) < 0)
+    if ((r = buf_inspect_list_tag_size(pretty, &i->tag)) < 0)
       return r;
     result += r;
     switch (i->next.type) {
@@ -1935,7 +2056,7 @@ sw buf_inspect_list_size (s_pretty *pretty, const s_list * const *list)
       if ((r = buf_write_1_size(pretty, " | ")) < 0)
         return r;
       result += r;
-      if ((r = buf_inspect_tag_size(&i->next)) < 0)
+      if ((r = buf_inspect_tag_size(pretty, &i->next)) < 0)
         return r;
       result += r;
       break;
@@ -1988,7 +2109,7 @@ sw buf_inspect_list_tag_size (s_pretty *pretty, const s_tag *tag)
       tag->data.tuple.tag[0].type == TAG_SYM) {
     sym = tag->data.tuple.tag[0].data.sym;
     if (sym_has_reserved_characters(sym)) {
-      if ((r = buf_inspect_str_size(&sym->str)) < 0)
+      if ((r = buf_inspect_str_size(pretty, &sym->str)) < 0)
         return r;
     }
     else
@@ -2076,24 +2197,26 @@ sw buf_inspect_map_size (s_pretty *pretty, const s_map *map)
       result += r;
     }
     else {
-      if ((r = buf_inspect_tag_size(map->key + i)) < 0)
+      if ((r = buf_inspect_tag_size(pretty, map->key + i)) < 0)
         return r;
       result += r;
       if ((r = buf_write_1_size(pretty, " => ")) < 0)        
         return r;
       result += r;
     }
-    if ((r = buf_inspect_tag_size(map->value + i)) < 0)
+    if ((r = buf_inspect_tag_size(pretty, map->value + i)) < 0)
       return r;
     result += r;
     i++;
     if (i < map->count) {
       if ((r = buf_write_1_size(pretty, ", ")) < 0)
-    return r;
+        return r;
+      result += r;
     }
   }
   if ((r = buf_write_1_size(pretty, "}")) < 0)
     return r;
+  result += r;
   return result;
 }
 
@@ -2131,11 +2254,13 @@ sw buf_inspect_paren_sym_size (s_pretty *pretty, const s_sym *sym)
   assert(sym);
   if ((r = buf_write_1_size(pretty, "(")) < 0)
     return r;
-  if ((r = buf_inspect_sym_size(&sym)) <= 0)
+  result += r;
+  if ((r = buf_inspect_sym_size(pretty, &sym)) <= 0)
     goto clean;
   result += r;
   if ((r = buf_write_1_size(pretty, ")")) < 0)
     return r;
+  result += r;
   r = result;
  clean:
   return r;
@@ -2175,7 +2300,10 @@ sw buf_inspect_ptag_size (s_pretty *pretty, const p_tag *ptag)
   (void) ptag;
   if ((r = buf_write_1_size(pretty, "@0x")) < 0)
     return r;
-  result += sizeof(uw) / 4;
+  result += r;
+  if ((r = buf_inspect_uw_hexadecimal_size(pretty, (uw *) ptag)) < 0)
+    return r;
+  result += r;
   return result;
 }
 
@@ -2210,20 +2338,24 @@ sw buf_inspect_ptr_free (s_buf *buf, const u_ptr_w *ptr_free)
 
 sw buf_inspect_ptr_free_size (s_pretty *pretty, const u_ptr_w *ptr)
 {
-  sw result;
+  sw result = 0;
   (void) ptr;
-  result = buf_write_1_size(pretty, "(PtrFree) 0x")) < 0)
+  if ((r = buf_write_1_size(pretty, "(PtrFree) 0x")) < 0)
     return r;
-  if ((r = buf_inspect_uw_hexadecimal_size((uw *) &ptr->p);
+  result += r;
+  if ((r = buf_inspect_uw_hexadecimal_size(pretty, (uw *) &ptr->p)) < 0)
+    return r;
+  result += r;
   return result;
 }
 
 sw buf_inspect_ptr_size (s_pretty *pretty, const u_ptr_w *ptr)
 {
-  sw result;
+  sw result = 0;
   (void) ptr;
-  result = buf_write_1_size(pretty, "(Ptr) 0x")) < 0)
+  if ((r = buf_write_1_size(pretty, "(Ptr) 0x")) < 0)
     return r;
+  result += r;
   if ((r = buf_inspect_uw_hexadecimal_size((uw *) &ptr->p);
   return result;
 }
@@ -2254,7 +2386,8 @@ sw buf_inspect_quote_size (s_pretty *pretty, const s_quote *quote)
   sw result = 0;
   if ((r = buf_write_1_size(pretty, "quote ")) < 0)
     return r;
-  if ((r = buf_inspect_tag_size(quote->tag)) < 0)
+  result += r;
+  if ((r = buf_inspect_tag_size(pretty, quote->tag)) < 0)
     return r;
   result += r;
   return result;
@@ -2288,12 +2421,13 @@ sw buf_inspect_ratio_size (s_pretty *pretty, const s_ratio *ratio)
 {
   sw r;
   sw result = 0;
-  if ((r = buf_inspect_integer_size(&ratio->numerator)) < 0)
+  if ((r = buf_inspect_integer_size(pretty, &ratio->numerator)) < 0)
     return r;
   result += r;
   if ((r = buf_write_1_size(pretty, "/")) < 0)
     return r;
-  if ((r = buf_inspect_integer_size(&ratio->denominator)) < 0)
+  result += r;
+  if ((r = buf_inspect_integer_size(pretty, &ratio->denominator)) < 0)
     return r;
   result += r;
   return result;
@@ -2553,7 +2687,7 @@ sw buf_inspect_str_reserved_size (s_pretty *pretty, const s_str *str)
   sw r;
   sw result = 0;
   s_str s;
-  r = buf_write_1_size(pretty, "\"")) < 0)
+  if ((r = buf_write_1_size(pretty, "\"")) < 0)
     return r;
   result += r;
   s = *str;
@@ -2561,18 +2695,18 @@ sw buf_inspect_str_reserved_size (s_pretty *pretty, const s_str *str)
     if ((r = str_read_character_utf8(&s, &c)) < 0)
       goto restore;
     if (r) {
-      if ((r = buf_inspect_str_character_size(&c)) <= 0)
+      if ((r = buf_inspect_str_character_size(pretty, &c)) <= 0)
         goto restore;
       result += r;
     }
     else if ((r = str_read_u8(&s, &byte)) < 0)
       goto restore;
     else if (r) {
-      r = buf_inspect_str_byte_size(&byte);
+      r = buf_inspect_str_byte_size(pretty, &byte);
       result += r;
     }
   }
-  r = buf_write_1_size(pretty, "\"")) < 0)
+  if ((r = buf_write_1_size(pretty, "\"")) < 0)
     return r;
   result += r;
   return result;
@@ -2583,14 +2717,21 @@ sw buf_inspect_str_reserved_size (s_pretty *pretty, const s_str *str)
 sw buf_inspect_str_size (s_pretty *pretty, const s_str *str)
 {
   bool b;
-  const sw quote_size = buf_write_1_size(pretty, "\"")) < 0)
-    return r;
+  const sw quote_size;
+  sw r;
+  sw result = 0;
   sw size;
   if (! str_has_reserved_characters(str, &b))
     return -1;
   if (b)
-    return buf_inspect_str_reserved_size(str);
-  size = str->size + 2 * quote_size;
+    return buf_inspect_str_reserved_size(pretty, str);
+  if ((r = buf_write_1_size(pretty, "\"")) < 0)
+    return r;
+  result += r;
+  result += str->size;
+  if ((r = buf_write_1_size(pretty, "\"")) < 0)
+    return r;
+  result += r;
   return size;
 }
 
@@ -2680,11 +2821,13 @@ sw buf_inspect_struct_size (s_pretty *pretty, const s_struct *s)
   assert(sym_is_module(s->type->module));
   if ((r = buf_write_1_size(pretty, "%")) < 0)
     return r;
+  result += r;
   if (! sym_is_module(s->type->module))
     return -1;
   result += s->type->module->str.size;
   if ((r = buf_write_1_size(pretty, "{")) < 0)
     return r;
+  result += r;
   if (s->data || s->tag) {
     while (i < s->type->map.count) {
       k = s->type->map.key + i;
@@ -2698,14 +2841,15 @@ sw buf_inspect_struct_size (s_pretty *pretty, const s_struct *s)
         return -1;
       }
       if (sym_has_reserved_characters(k->data.sym)) {
-        if ((r = buf_inspect_str_size(&k->data.sym->str)) < 0)
+        if ((r = buf_inspect_str_size(pretty, &k->data.sym->str)) < 0)
           return r;
       }
       else
         r = k->data.sym->str.size;
       result += r;
       if ((r = buf_write_1_size(pretty, ": ")) < 0)
-    return r;
+        return r;
+      result += r;
       if (s->data) {
         if (! tag_type(s->type->map.value + i, &type))
           return -1;
@@ -2721,13 +2865,17 @@ sw buf_inspect_struct_size (s_pretty *pretty, const s_struct *s)
         result += r;
       }
       i++;
-      if (i < s->type->map.count)
+      if (i < s->type->map.count) {
         if ((r = buf_write_1_size(pretty, ", ")) < 0)
+          return r;
+        result += r;
+      }
     return r;
     }
   }
   if ((r = buf_write_1_size(pretty, "}")) < 0)
     return r;
+  result += r;
   return result;
 }
 
@@ -2788,22 +2936,25 @@ sw buf_inspect_struct_type_size (s_pretty *pretty, const s_struct_type *st)
   assert(sym_is_module(st->module));
   assert(st->offset);
   assert(st->size);
-  result = buf_write_1_size(pretty, "%StructType{module: ")) < 0)
+  if ((r = buf_write_1_size(pretty, "%StructType{module: ")) < 0)
     return r;
-  if ((r = buf_inspect_sym_size(&st->module)) < 0)
+  result += r;
+  if ((r = buf_inspect_sym_size(pretty, &st->module)) < 0)
     return r;
   result += r;
   if ((r = buf_write_1_size(pretty, ", map: ")) < 0)
     return r;
-  if ((r = buf_inspect_map_size(&st->map)) < 0)
+  result += r;
+  if ((r = buf_inspect_map_size(pretty, &st->map)) < 0)
     return r;
   result += r;
   if ((r = buf_write_1_size(pretty, ", offset: ")) < 0)
     return r;
+  result += r;
   offset_array_dimension = st->map.count;
   array_init(&offset_array, &g_sym_Uw, 1, &offset_array_dimension);
   offset_array.data = st->offset;
-  if ((r = buf_inspect_array_size(&offset_array)) < 0) {
+  if ((r = buf_inspect_array_size(pretty, &offset_array)) < 0) {
     array_clean(&offset_array);
     return r;
   }
@@ -2811,11 +2962,13 @@ sw buf_inspect_struct_type_size (s_pretty *pretty, const s_struct_type *st)
   array_clean(&offset_array);
   if ((r = buf_write_1_size(pretty, ", size: ")) < 0)
     return r;
-  if ((r = buf_inspect_uw_size(&st->size)) < 0)
+  result += r;
+  if ((r = buf_inspect_uw_size(pretty, &st->size)) < 0)
     return r;
   result += r;
   if ((r = buf_write_1_size(pretty, "}")) < 0)
     return r;
+  result += r;
   return result;
 }
 
@@ -2873,13 +3026,14 @@ sw buf_inspect_sym_reserved (s_buf *buf, const s_sym *x)
 sw buf_inspect_sym_reserved_size (s_pretty *pretty, const s_sym *x)
 {
   sw r;
-  sw result;
-  result = buf_write_1_size(pretty, ":")) < 0)
+  sw result = 0;
+  if ((r = buf_write_1_size(pretty, ":")) < 0)
     return r;
-  if ((r = buf_inspect_str_size(&x->str)) < 0)
+  result += r;
+  if ((r = buf_inspect_str_size(pretty, &x->str)) < 0)
     return r;
   result += r;
-  return result; 
+  return result;
 }
 
 sw buf_inspect_tag (s_buf *buf, const s_tag *tag)
@@ -2942,53 +3096,89 @@ sw buf_inspect_tag_size (s_pretty *pretty, const s_tag *tag)
 {
   assert(tag);
   switch(tag->type) {
-  case TAG_ARRAY:    return buf_inspect_array_size(&tag->data.array);
-  case TAG_BLOCK:    return buf_inspect_block_size(&tag->data.block);
-  case TAG_BOOL:     return buf_inspect_bool_size(&tag->data.bool);
-  case TAG_CALL:     return buf_inspect_call_size(&tag->data.call);
-  case TAG_CFN:      return buf_inspect_cfn_size(&tag->data.cfn);
+  case TAG_ARRAY:
+    return buf_inspect_array_size(pretty, &tag->data.array);
+  case TAG_BLOCK:
+    return buf_inspect_block_size(pretty, &tag->data.block);
+  case TAG_BOOL:
+    return buf_inspect_bool_size(pretty, &tag->data.bool);
+  case TAG_CALL:
+    return buf_inspect_call_size(pretty, &tag->data.call);
+  case TAG_CFN:
+    return buf_inspect_cfn_size(pretty, &tag->data.cfn);
   case TAG_CHARACTER:
-    return buf_inspect_character_size(&tag->data.character);
-  case TAG_COMPLEX:  return buf_inspect_complex_size(tag->data.complex);
-  case TAG_COW:      return buf_inspect_cow_size(tag->data.cow);
-  case TAG_F32:      return buf_inspect_f32_size(&tag->data.f32);
-  case TAG_F64:      return buf_inspect_f64_size(&tag->data.f64);
-  case TAG_F128:     return buf_inspect_f128_size(&tag->data.f128);
-  case TAG_FACT:     return buf_inspect_fact_size(&tag->data.fact);
-  case TAG_FN:       return buf_inspect_fn_size(&tag->data.fn);
-  case TAG_IDENT:    return buf_inspect_ident_size(&tag->data.ident);
+    return buf_inspect_character_size(pretty, &tag->data.character);
+  case TAG_COMPLEX:
+    return buf_inspect_complex_size(tag->data.complex);
+  case TAG_COW:
+    return buf_inspect_cow_size(tag->data.cow);
+  case TAG_F32:
+    return buf_inspect_f32_size(pretty, &tag->data.f32);
+  case TAG_F64:
+    return buf_inspect_f64_size(pretty, &tag->data.f64);
+  case TAG_F128:
+    return buf_inspect_f128_size(pretty, &tag->data.f128);
+  case TAG_FACT:
+    return buf_inspect_fact_size(pretty, &tag->data.fact);
+  case TAG_FN:
+    return buf_inspect_fn_size(pretty, &tag->data.fn);
+  case TAG_IDENT:
+    return buf_inspect_ident_size(pretty, &tag->data.ident);
   case TAG_INTEGER:
-    return buf_inspect_integer_size(&tag->data.integer);
+    return buf_inspect_integer_size(pretty, &tag->data.integer);
   case TAG_LIST:
-    return buf_inspect_list_size((const s_list **) &tag->data.list);
-  case TAG_MAP:      return buf_inspect_map_size(&tag->data.map);
-  case TAG_PTAG:     return buf_inspect_ptag_size(&tag->data.ptag);
-  case TAG_PTR:      return buf_inspect_ptr_size(&tag->data.ptr);
+    return buf_inspect_list_size(pretty,
+                                 (const s_list **) &tag->data.list);
+  case TAG_MAP:
+    return buf_inspect_map_size(pretty, &tag->data.map);
+  case TAG_PTAG:
+    return buf_inspect_ptag_size(pretty, &tag->data.ptag);
+  case TAG_PTR:
+    return buf_inspect_ptr_size(pretty, &tag->data.ptr);
   case TAG_PTR_FREE:
-    return buf_inspect_ptr_free_size(&tag->data.ptr_free);
-  case TAG_QUOTE:    return buf_inspect_quote_size(&tag->data.quote);
-  case TAG_RATIO:    return buf_inspect_ratio_size(&tag->data.ratio);
-  case TAG_S8:       return buf_inspect_s8_size(&tag->data.s8);
-  case TAG_S16:      return buf_inspect_s16_size(&tag->data.s16);
-  case TAG_S32:      return buf_inspect_s32_size(&tag->data.s32);
-  case TAG_S64:      return buf_inspect_s64_size(&tag->data.s64);
-  case TAG_SW:       return buf_inspect_sw_size(&tag->data.sw);
-  case TAG_STR:      return buf_inspect_str_size(&tag->data.str);
-  case TAG_STRUCT:   return buf_inspect_struct_size(&tag->data.struct_);
+    return buf_inspect_ptr_free_size(pretty, &tag->data.ptr_free);
+  case TAG_QUOTE:
+    return buf_inspect_quote_size(pretty, &tag->data.quote);
+  case TAG_RATIO:
+    return buf_inspect_ratio_size(pretty, &tag->data.ratio);
+  case TAG_S8:
+    return buf_inspect_s8_size(pretty, &tag->data.s8);
+  case TAG_S16:
+    return buf_inspect_s16_size(pretty, &tag->data.s16);
+  case TAG_S32:
+    return buf_inspect_s32_size(pretty, &tag->data.s32);
+  case TAG_S64:
+    return buf_inspect_s64_size(pretty, &tag->data.s64);
+  case TAG_SW:
+    return buf_inspect_sw_size(pretty, &tag->data.sw);
+  case TAG_STR:
+    return buf_inspect_str_size(pretty, &tag->data.str);
+  case TAG_STRUCT:
+    return buf_inspect_struct_size(pretty, &tag->data.struct_);
   case TAG_STRUCT_TYPE:
-    return buf_inspect_struct_type_size(&tag->data.struct_type);
-  case TAG_SYM:      return buf_inspect_sym_size(&tag->data.sym);
-  case TAG_TIME:     return buf_inspect_time_size(&tag->data.time);
-  case TAG_TUPLE:    return buf_inspect_tuple_size(&tag->data.tuple);
-  case TAG_U8:       return buf_inspect_u8_size(&tag->data.u8);
-  case TAG_U16:      return buf_inspect_u16_size(&tag->data.u16);
-  case TAG_U32:      return buf_inspect_u32_size(&tag->data.u32);
-  case TAG_U64:      return buf_inspect_u64_size(&tag->data.u64);
+    return buf_inspect_struct_type_size(pretty, &tag->data.struct_type);
+  case TAG_SYM:
+    return buf_inspect_sym_size(pretty, &tag->data.sym);
+  case TAG_TIME:
+    return buf_inspect_time_size(pretty, &tag->data.time);
+  case TAG_TUPLE:
+    return buf_inspect_tuple_size(pretty, &tag->data.tuple);
+  case TAG_U8:
+    return buf_inspect_u8_size(pretty, &tag->data.u8);
+  case TAG_U16:
+    return buf_inspect_u16_size(pretty, &tag->data.u16);
+  case TAG_U32:
+    return buf_inspect_u32_size(pretty, &tag->data.u32);
+  case TAG_U64:
+    return buf_inspect_u64_size(pretty, &tag->data.u64);
   case TAG_UNQUOTE:
-    return buf_inspect_unquote_size(&tag->data.unquote);
-  case TAG_UW:       return buf_inspect_uw_size(&tag->data.uw);
-  case TAG_VAR:      return buf_inspect_var_size(tag);
-  case TAG_VOID:     return buf_inspect_void_size(NULL);
+    return buf_inspect_unquote_size(pretty, &tag->data.unquote);
+  case TAG_UW:
+    return buf_inspect_uw_size(pretty, &tag->data.uw);
+  case TAG_VAR:
+    return buf_inspect_var_size(pretty, tag);
+  case TAG_VOID:
+    return buf_inspect_void_size(pretty, NULL);
   }
   err_puts("buf_inspect_tag_size: unknown tag type");
   assert(! "buf_inspect_tag_size: unknown tag type");
@@ -3042,16 +3232,19 @@ sw buf_inspect_time_size (s_pretty *pretty, const s_time *time)
   sw result = 0;
   if ((r = buf_write_1_size(pretty, "%Time{tv_sec: ")) < 0)
     return r;
-  if ((r = buf_inspect_sw_size(&time->tv_sec)) < 0)
+  result += r;
+  if ((r = buf_inspect_sw_size(pretty, &time->tv_sec)) < 0)
     return r;
   result += r;
   if ((r = buf_write_1_size(pretty, ", tv_nsec: ")) < 0)
     return r;
-  if ((r = buf_inspect_sw_size(&time->tv_nsec)) < 0)
+  result += r;
+  if ((r = buf_inspect_sw_size(pretty, &time->tv_nsec)) < 0)
     return r;
   result += r;
   if ((r = buf_write_1_size(pretty, "}")) < 0)
     return r;
+  result += r;
   return result;
 }
 
@@ -3087,16 +3280,18 @@ sw buf_inspect_tuple_size (s_pretty *pretty, const s_tuple *tuple)
 {
   u64 i = 0;
   sw r;
-  sw result;
-  result = buf_write_1_size(pretty, "{")) < 0)
+  sw result = 0;
+  if ((r = buf_write_1_size(pretty, "{")) < 0)
     return r;
+  result += r;
   if (tuple->count) {
     while (i < tuple->count - 1) {
       if ((r = buf_inspect_tag_size(tuple->tag + i)) < 0)
         return r;
       result += r;
       if ((r = buf_write_1_size(pretty, ", ")) < 0)
-    return r;
+        return r;
+      result += r;
       i++;
     }
     if ((r = buf_inspect_tag_size(tuple->tag + i)) < 0)
@@ -3105,6 +3300,7 @@ sw buf_inspect_tuple_size (s_pretty *pretty, const s_tuple *tuple)
   }
   if ((r = buf_write_1_size(pretty, "}")) < 0)
     return r;
+  result += r;
   return result;
 }
 
@@ -3138,11 +3334,13 @@ sw buf_inspect_unquote_size (s_pretty *pretty, const s_unquote *unquote)
   sw result = 0;
   if ((r = buf_write_1_size(pretty, "unquote(")) < 0)
     return r;
-  if ((r = buf_inspect_tag_size(unquote->tag)) < 0)
+  result += r;
+  if ((r = buf_inspect_tag_size(pretty, unquote->tag)) < 0)
     return r;
   result += r;
   if ((r = buf_write_1_size(pretty, ")")) < 0)
     return r;
+  result += r;
   return result;
 }
 
@@ -3176,6 +3374,7 @@ sw buf_inspect_var_size (s_pretty *pretty, const s_tag *tag)
   result += r;
   if ((r = buf_write_1_size(pretty, " ?")) < 0)
     return r;
+  result += r;
   return result;
 }
 
diff --git a/libkc3/buf_inspect_u16_decimal.h b/libkc3/buf_inspect_u16_decimal.h
index d26361e..351c96a 100644
--- a/libkc3/buf_inspect_u16_decimal.h
+++ b/libkc3/buf_inspect_u16_decimal.h
@@ -16,7 +16,7 @@
 
 #include "types.h"
 
-sw buf_inspect_u16_decimal (s_pretty *pretty, s_buf *buf, const u16 *u);
+sw buf_inspect_u16_decimal (s_buf *buf, const u16 *u);
 sw buf_inspect_u16_decimal_size (s_pretty *pretty, const u16 *u);
 
 #endif /* LIBKC3_BUF_INSPECT_U16_decimal_H */
diff --git a/libkc3/buf_inspect_u32_decimal.h b/libkc3/buf_inspect_u32_decimal.h
index 86ff55a..ebd6b95 100644
--- a/libkc3/buf_inspect_u32_decimal.h
+++ b/libkc3/buf_inspect_u32_decimal.h
@@ -16,7 +16,7 @@
 
 #include "types.h"
 
-sw buf_inspect_u32_decimal (s_pretty *pretty, s_buf *buf, const u32 *u);
+sw buf_inspect_u32_decimal (s_buf *buf, const u32 *u);
 sw buf_inspect_u32_decimal_size (s_pretty *pretty, const u32 *u);
 
 #endif /* LIBKC3_BUF_INSPECT_U32_decimal_H */
diff --git a/libkc3/buf_inspect_u64_decimal.h b/libkc3/buf_inspect_u64_decimal.h
index 8a2bde6..4ba0fb2 100644
--- a/libkc3/buf_inspect_u64_decimal.h
+++ b/libkc3/buf_inspect_u64_decimal.h
@@ -16,7 +16,7 @@
 
 #include "types.h"
 
-sw buf_inspect_u64_decimal (s_pretty *pretty, s_buf *buf, const u64 *u);
+sw buf_inspect_u64_decimal (s_buf *buf, const u64 *u);
 sw buf_inspect_u64_decimal_size (s_pretty *pretty, const u64 *u);
 
 #endif /* LIBKC3_BUF_INSPECT_U64_decimal_H */
diff --git a/libkc3/buf_inspect_u8_decimal.h b/libkc3/buf_inspect_u8_decimal.h
index eb95ca9..e1d1bd3 100644
--- a/libkc3/buf_inspect_u8_decimal.h
+++ b/libkc3/buf_inspect_u8_decimal.h
@@ -16,7 +16,7 @@
 
 #include "types.h"
 
-sw buf_inspect_u8_decimal (s_pretty *pretty, s_buf *buf, const u8 *u);
+sw buf_inspect_u8_decimal (s_buf *buf, const u8 *u);
 sw buf_inspect_u8_decimal_size (s_pretty *pretty, const u8 *u);
 
 #endif /* LIBKC3_BUF_INSPECT_U8_decimal_H */
diff --git a/libkc3/buf_inspect_u_base.h.in b/libkc3/buf_inspect_u_base.h.in
index 61204d2..a5ec1d1 100644
--- a/libkc3/buf_inspect_u_base.h.in
+++ b/libkc3/buf_inspect_u_base.h.in
@@ -16,7 +16,7 @@
 
 #include "types.h"
 
-sw buf_inspect_u_bits$__BASE$ (s_pretty *pretty, s_buf *buf, const u_bits$ *u);
+sw buf_inspect_u_bits$__BASE$ (s_buf *buf, const u_bits$ *u);
 sw buf_inspect_u_bits$__BASE$_size (s_pretty *pretty, const u_bits$ *u);
 
 #endif /* LIBKC3_BUF_INSPECT_U_BITS$__BASE$_H */
diff --git a/libkc3/buf_inspect_uw_decimal.h b/libkc3/buf_inspect_uw_decimal.h
index aa6e148..0ac03ad 100644
--- a/libkc3/buf_inspect_uw_decimal.h
+++ b/libkc3/buf_inspect_uw_decimal.h
@@ -16,7 +16,7 @@
 
 #include "types.h"
 
-sw buf_inspect_uw_decimal (s_pretty *pretty, s_buf *buf, const uw *u);
+sw buf_inspect_uw_decimal (s_buf *buf, const uw *u);
 sw buf_inspect_uw_decimal_size (s_pretty *pretty, const uw *u);
 
 #endif /* LIBKC3_BUF_INSPECT_UW_decimal_H */