Commit ecd36f071a5d5f49052004e2e0d72d56b0e131d9

Thomas de Grivel 2024-08-10T18:38:03

pretty printer: tests ok

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
diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index adf6e4c..961842a 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -263,7 +263,7 @@ sw buf_inspect_block (s_buf *buf, const s_block *block)
       return buf_write_1(buf, "do end");
   }
   pretty_save = buf->pretty;
-  pretty_indent(&buf->pretty, 2);
+  pretty_indent(&buf->pretty, PRETTY_INDENT);
   if (block->short_form) {
     if ((r = buf_write_1(buf, "{ ")) < 0)
       return r;
@@ -391,7 +391,7 @@ sw buf_inspect_block_size (s_pretty *pretty, const s_block *block)
       return buf_write_1_size(pretty, "do end");
   }
   pretty_save = *pretty;
-  pretty_indent(pretty, 2);
+  pretty_indent(pretty, PRETTY_INDENT);
   if (block->short_form) {    
     if ((r = buf_write_1_size(pretty, "{ ")) < 0)
       return r; 
@@ -669,6 +669,7 @@ sw buf_inspect_call_if_then_else (s_buf *buf, const s_call *call)
   s_tag *condition;
   s_tag *else_;
   uw i;
+  s_pretty pretty_save;
   sw r;
   sw result = 0;
   s_tag *then;
@@ -684,13 +685,15 @@ sw buf_inspect_call_if_then_else (s_buf *buf, const s_call *call)
     return r;
   result += r;
   then = &list_next(call->arguments)->tag;
+  pretty_save = buf->pretty;
   if (then->type == TAG_BLOCK) {
     if ((r = buf_write_1(buf, " do")) < 0)
       return r;
     result += r;
+    pretty_indent(&buf->pretty, PRETTY_INDENT);
     i = 0;
     while (i < then->data.block.count) {
-      if ((r = buf_write_1(buf, "\n  ")) < 0)
+      if ((r = buf_write_1(buf, "\n")) < 0)
         return r;
       result += r;
       if ((r = buf_inspect_tag(buf, then->data.block.tag + i)) < 0)
@@ -702,11 +705,14 @@ sw buf_inspect_call_if_then_else (s_buf *buf, const s_call *call)
     if ((r = buf_inspect_tag(buf, then)) < 0)
       return r;
   result += r;
+  buf->pretty = pretty_save;
   else_ = &list_next(list_next(call->arguments))->tag;
   if (else_->type != TAG_VOID) {
     if ((r = buf_write_1(buf, "\nelse")) < 0)
       return r;
     result += r;
+    pretty_save = buf->pretty;
+    pretty_indent(&buf->pretty, PRETTY_INDENT);
     if (then->type == TAG_BLOCK) {
       if ((r = buf_inspect_block_inner(buf, &else_->data.block)) < 0)
         return r;
@@ -716,6 +722,7 @@ sw buf_inspect_call_if_then_else (s_buf *buf, const s_call *call)
         return r;
     result += r;
   }
+  buf->pretty = pretty_save;
   if ((r = buf_write_1(buf, "\nend")) < 0)
     return r;
   result += r;
@@ -748,7 +755,7 @@ sw buf_inspect_call_if_then_else_size (s_pretty *pretty, const s_call *call)
     if ((r = buf_write_1_size(pretty, " do")) < 0)
       return r;
     result += r;
-    pretty_indent(pretty, 2);
+    pretty_indent(pretty, PRETTY_INDENT);
     i = 0;
     while (i < then->data.block.count) {
       if ((r = buf_write_1_size(pretty, "\n")) < 0)
@@ -760,7 +767,7 @@ sw buf_inspect_call_if_then_else_size (s_pretty *pretty, const s_call *call)
     }
   }
   else {
-    pretty_indent(pretty, 2);
+    pretty_indent(pretty, PRETTY_INDENT);
     if ((r = buf_inspect_tag_size(pretty, then)) < 0)
       return r;
   }
@@ -2148,11 +2155,11 @@ sw buf_inspect_integer (s_buf *buf, const s_integer *x)
   const mp_digit radix = 10;
   s32 size = 0;
   mp_int t;
+  if (MP_IS_ZERO(&x->mp_int))
+    return buf_write_u8(buf, '0');
   if (mp_radix_size(&x->mp_int, radix, &size) != MP_OKAY)
     return -1;
   maxlen = size;
-  if (MP_IS_ZERO(&x->mp_int))
-    return buf_write_u8(buf, '0');
   if (mp_init_copy(&t, &x->mp_int) != MP_OKAY)
     return -1;
   if (t.sign == MP_NEG) {
@@ -2184,15 +2191,13 @@ sw buf_inspect_integer (s_buf *buf, const s_integer *x)
 sw buf_inspect_integer_size (s_pretty *pretty, const s_integer *x)
 {
   const mp_digit radix = 10;
-  sw result = 0;
   s32 size = 0;
   (void) pretty;
-  if (mp_radix_size(&x->mp_int, radix, &size) != MP_OKAY)
-    return -1;
-  result = size;
   if (MP_IS_ZERO(&x->mp_int))
     return 1;
-  return result;
+  if (mp_radix_size(&x->mp_int, radix, &size) != MP_OKAY)
+    return -1;
+  return size - 1;
 }
 
 sw buf_inspect_list (s_buf *buf, const s_list * const *x)
@@ -3027,7 +3032,7 @@ sw buf_inspect_struct (s_buf *buf, const s_struct *s)
     assert(! "buf_inspect_struct: sym_is_module(s->type->module)");
     return -1;
   }
-  if ((r = buf_write_str(buf, &s->type->module->str)) < 0)
+  if ((r = buf_write_str_memcpy(buf, &s->type->module->str)) < 0)
     return r;
   result += r;
   if ((r = buf_write_1(buf, "{")) < 0)
@@ -3050,7 +3055,7 @@ sw buf_inspect_struct (s_buf *buf, const s_struct *s)
           return r;
       }
       else
-        if ((r = buf_write_str(buf, &k->data.sym->str)) < 0)
+        if ((r = buf_write_str_memcpy(buf, &k->data.sym->str)) < 0)
           return r;
       result += r;
       if ((r = buf_write_1(buf, ": ")) < 0)
@@ -3145,7 +3150,6 @@ sw buf_inspect_struct_size (s_pretty *pretty, const s_struct *s)
           return r;
         result += r;
       }
-    return r;
     }
   }
   if ((r = buf_write_1_size(pretty, "}")) < 0)
diff --git a/libkc3/env.c b/libkc3/env.c
index f440784..c5fa1c2 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -523,6 +523,7 @@ bool env_eval_array_tag (s_env *env, const s_array *array, s_tag *dest)
   dest->data.array = tmp;
   return true;
 }
+
 bool env_eval_block (s_env *env, const s_block *block, s_tag *dest)
 {
   uw i = 0;
@@ -1758,42 +1759,39 @@ bool env_eval_quote_unquote (s_env *env, const s_unquote *unquote, s_tag *dest)
   return true;
 }
 
-bool env_eval_struct (s_env *env, const s_struct *s, s_tag *dest)
+bool env_eval_struct (s_env *env, const s_struct *s, s_struct *dest)
 {
   uw i;
-  s_struct *t;
   s_tag       tag = {0};
   const void *tag_data;
-  s_tag tmp = {0};
+  s_struct tmp = {0};
   const s_sym *type;
   const void *value;
   assert(env);
   assert(s);
   assert(dest);
-  tmp.type = TAG_STRUCT;
-  t = &tmp.data.struct_;
   if (s->data) {
-    if (! struct_init_copy(t, s))
+    if (! struct_init_copy(&tmp, s))
       return false;
     *dest = tmp;
     return true;
   }
-  t->type = s->type;
-  if (! struct_allocate(t))
+  tmp.type = s->type;
+  if (! struct_allocate(&tmp))
     return false;
   i = 0;
-  while (i < t->type->map.count) {
+  while (i < tmp.type->map.count) {
     if (s->tag) {
-      if (t->type->map.value[i].type == TAG_VAR)
-        type = t->type->map.value[i].data.var.type;
+      if (tmp.type->map.value[i].type == TAG_VAR)
+        type = tmp.type->map.value[i].data.var.type;
       else {
-        if (! tag_type(t->type->map.value + i, &type))
+        if (! tag_type(tmp.type->map.value + i, &type))
           goto ko;
       }
       if (! env_eval_tag(env, s->tag + i, &tag))
         goto ko;
-      if (t->type->map.value[i].type == TAG_VAR) {
-        if (! data_init_cast((s8 *) t->data + t->type->offset[i],
+      if (tmp.type->map.value[i].type == TAG_VAR) {
+        if (! data_init_cast((s8 *) tmp.data + tmp.type->offset[i],
                              &type, &tag))
           goto ko_init;
       }
@@ -1802,18 +1800,18 @@ bool env_eval_struct (s_env *env, const s_struct *s, s_tag *dest)
           tag_clean(&tag);
           goto ko;
         }
-        if (! data_init_copy(type, (s8 *) t->data + t->type->offset[i],
+        if (! data_init_copy(type, (s8 *) tmp.data + tmp.type->offset[i],
                              tag_data))
           goto ko_init;
       }
       tag_clean(&tag);
     }
     else {
-      if (! tag_type(t->type->map.value + i, &type))
+      if (! tag_type(tmp.type->map.value + i, &type))
         goto ko;
-      if (! tag_to_const_pointer(t->type->map.value + i, type, &value))
+      if (! tag_to_const_pointer(tmp.type->map.value + i, type, &value))
         goto ko;
-      if (! data_init_copy(type, (s8 *) t->data + t->type->offset[i],
+      if (! data_init_copy(type, (s8 *) tmp.data + tmp.type->offset[i],
                            value))
         goto ko;
     }
@@ -1825,15 +1823,23 @@ bool env_eval_struct (s_env *env, const s_struct *s, s_tag *dest)
   err_write_1("env_eval_struct: invalid type ");
   err_write_1(tag_type_to_string(tag.type));
   err_write_1(" for key ");
-  err_write_1(t->type->map.key[i].data.sym->str.ptr.pchar);
+  err_write_1(tmp.type->map.key[i].data.sym->str.ptr.pchar);
   err_write_1(", expected ");
-  err_puts(tag_type_to_string(t->type->map.value[i].type));
+  err_puts(tag_type_to_string(tmp.type->map.value[i].type));
   tag_clean(&tag);
  ko:
-  struct_clean(t);
+  struct_clean(&tmp);
   return false;
 }
 
+bool env_eval_struct_tag (s_env *env, const s_struct *s, s_tag *dest)
+{
+  if (! env_eval_struct(env, s, &dest->data.struct_))
+    return false;
+  dest->type = TAG_STRUCT;
+  return true;
+}
+
 bool env_eval_tag (s_env *env, const s_tag *tag, s_tag *dest)
 {
   assert(env);
@@ -1865,7 +1871,7 @@ bool env_eval_tag (s_env *env, const s_tag *tag, s_tag *dest)
   case TAG_QUOTE:
     return env_eval_quote(env, &tag->data.quote, dest);
   case TAG_STRUCT:
-    return env_eval_struct(env, &tag->data.struct_, dest);
+    return env_eval_struct_tag(env, &tag->data.struct_, dest);
   case TAG_TIME:
     return env_eval_time(env, &tag->data.time, dest);
   case TAG_TUPLE:
diff --git a/libkc3/env.h b/libkc3/env.h
index d6a0e90..0ad1be9 100644
--- a/libkc3/env.h
+++ b/libkc3/env.h
@@ -167,7 +167,9 @@ bool          env_eval_quote_unquote (s_env *env,
                                       const s_unquote *unquote,
                                       s_tag *dest);
 bool          env_eval_struct (s_env *env, const s_struct *s,
-                               s_tag *dest);
+                               s_struct *dest);
+bool          env_eval_struct_tag (s_env *env, const s_struct *s,
+                                   s_tag *dest);
 bool          env_eval_tag (s_env *env, const s_tag *tag,
                             s_tag *dest);
 bool          env_eval_time (s_env *env, const s_time *time,
diff --git a/libkc3/inspect.c b/libkc3/inspect.c
index 65afdaf..e3eddc8 100644
--- a/libkc3/inspect.c
+++ b/libkc3/inspect.c
@@ -27,7 +27,11 @@ s_str * inspect_array (const s_array *array, s_str *dest)
     assert(! "inspect_array: buf_inspect_array_size error");
     return NULL;
   }
-  buf_init_alloc(&tmp, size);
+  if (! buf_init_alloc(&tmp, size)) {
+    err_puts("inspect_array: buf_init alloc");
+    assert(! "inspect_array: buf_init_alloc");
+    return NULL;
+  }
   buf_inspect_array(&tmp, array);
   if (tmp.wpos != tmp.size) {
     err_puts("inspect_array: tmp.wpos != tmp.size");
@@ -237,6 +241,37 @@ s_str * inspect_str (const s_str *str, s_str *dest)
   return buf_to_str(&buf, dest);
 }
 
+s_str * inspect_struct (const s_struct *s, s_str *dest)
+{
+  s_pretty pretty = {0};
+  sw size;
+  s_buf tmp;
+  size = buf_inspect_struct_size(&pretty, s);
+  if (size < 0) {
+    err_puts("inspect_struct: buf_inspect_struct_size error");
+    assert(! "inspect_struct: buf_inspect_struct_size error");
+    return NULL;
+  }
+  if (! buf_init_alloc(&tmp, size)) {
+    err_puts("inspect_struct: buf_init alloc");
+    assert(! "inspect_struct: buf_init_alloc");
+    return NULL;
+  }
+  if (buf_inspect_struct(&tmp, s) < 0) {
+    err_puts("inspect_struct: buf_inspect_struct error");
+    assert(! "inspect_struct: buf_inspect_struct error");
+    buf_clean(&tmp);
+    return NULL;
+  }
+  if (tmp.wpos != tmp.size) {
+    err_puts("inspect_struct: tmp.wpos != tmp.size");
+    assert(! "inspect_struct: tmp.wpos != tmp.size");
+    buf_clean(&tmp);
+    return NULL;
+  }
+  return buf_to_str(&tmp, dest);
+}
+
 s_str * inspect_sym (const s_sym *sym, s_str *dest)
 {
   s_buf buf;
diff --git a/libkc3/inspect.h b/libkc3/inspect.h
index 6934c43..d2786dd 100644
--- a/libkc3/inspect.h
+++ b/libkc3/inspect.h
@@ -30,6 +30,7 @@ s_str * inspect_ident (const s_ident *ident, s_str *dest);
 s_str * inspect_list (const s_list *list, s_str *dest);
 s_str * inspect_ratio (const s_ratio *src, s_str *dest);
 s_str * inspect_str (const s_str *str, s_str *dest);
+s_str * inspect_struct (const s_struct *s, s_str *dest);
 s_str * inspect_sym (const s_sym *sym, s_str *dest);
 s_str * inspect_tag (const s_tag *tag, s_str *dest);
 s_str * inspect_tuple (const s_tuple *tuple, s_str *dest);
diff --git a/libkc3/pretty.h b/libkc3/pretty.h
index d701585..4a80faf 100644
--- a/libkc3/pretty.h
+++ b/libkc3/pretty.h
@@ -21,6 +21,8 @@
 
 #include "types.h"
 
+#define PRETTY_INDENT 2
+
 /* Operators. */
 s_pretty * pretty_indent (s_pretty *pretty, sw indent);
 
diff --git a/libkc3/str.c b/libkc3/str.c
index dcf0f5c..0b1c275 100644
--- a/libkc3/str.c
+++ b/libkc3/str.c
@@ -42,12 +42,31 @@
   {                                                                    \
     s_buf buf;                                                         \
     s_pretty pretty = {0};                                             \
+    sw r;                                                              \
     sw size;                                                           \
     size = buf_inspect_ ## name ## _size(&pretty, &x);                 \
-    if (size <= 0)                                                     \
+    if (! size)                                                        \
+      return str_init_empty(str);                                      \
+    if (size < 0) {                                                    \
+      err_puts("str_init_" # name ": buf_inspect_" # name              \
+               "_size < 0");                                           \
+      return NULL;                                                     \
+    }                                                                  \
+    if (! buf_init_alloc(&buf, size)) {                                \
+      err_puts("str_init_" # name ": buf_init_alloc");                 \
+      return NULL;                                                     \
+    }                                                                  \
+    if ((r = buf_inspect_ ## name(&buf, &x)) < 0) {                    \
+      err_puts("str_init_" # name ": buf_inspect_" # name " < 0");     \
+      buf_clean(&buf);                                                 \
       return NULL;                                                     \
-    buf_init_alloc(&buf, size);                                        \
-    if (buf_inspect_ ## name(&buf, &x) < 0) {                          \
+    }                                                                  \
+    if (r != size) {                                                   \
+      err_write_1("str_init_" # name ": buf_inspect_" # name ": ");    \
+      err_inspect_sw_decimal(&r);                                      \
+      err_write_1(" != ");                                             \
+      err_inspect_sw_decimal(&size);                                   \
+      err_write_1("\n");                                               \
       buf_clean(&buf);                                                 \
       return NULL;                                                     \
     }                                                                  \
@@ -62,9 +81,17 @@
     s_pretty pretty = {0};                                             \
     sw size;                                                           \
     size = buf_inspect_ ## type ## _decimal_size(&pretty, &x);         \
-    if (size <= 0)                                                     \
+    if (! size)                                                        \
+      return str_init_empty(str);                                      \
+    if (size < 0) {                                                    \
+      err_puts("str_init_" # type ": buf_inspect_" # type              \
+               "_size < 0");                                           \
+      return NULL;                                                     \
+    }                                                                  \
+    if (! buf_init_alloc(&buf, size)) {                                \
+      err_puts("str_init_" # type ": buf_init_alloc");                 \
       return NULL;                                                     \
-    buf_init_alloc(&buf, size);                                        \
+    }                                                                  \
     buf_inspect_ ## type ## _decimal(&buf, &x);                        \
     assert(buf.wpos == (uw) size);                                     \
     return buf_to_str(&buf, str);                                      \
@@ -310,6 +337,8 @@ s_str * str_init_cast (s_str *str, const s_sym * const *type,
     return str_init_u64(str, tag->data.u64);
   case TAG_UW:
     return str_init_uw(str, tag->data.uw);
+  case TAG_VAR:
+    return str_init_var(str, tag);
   default:
     break;
   }
@@ -501,6 +530,41 @@ DEF_STR_INIT_INT(u32)
 DEF_STR_INIT_INT(u64)
 DEF_STR_INIT_INT(uw)
 
+s_str * str_init_var (s_str *str, const s_tag *x)
+{
+  s_buf buf;
+  s_pretty pretty = {0};
+  sw r;
+  sw size;
+  size = buf_inspect_var_size(&pretty, x);
+  if (! size)
+    return str_init_empty(str);
+  if (size < 0) {
+    err_puts("str_init_var: buf_inspect_var_size < 0");
+    return NULL;
+  }
+  if (! buf_init_alloc(&buf, size)) {
+    err_puts("str_init_var: buf_init_alloc");
+    return NULL;
+  }
+  if ((r = buf_inspect_var(&buf, x)) < 0) {
+    err_puts("str_init_var: buf_inspect_var < 0");
+    buf_clean(&buf);
+    return NULL;
+  }
+  if (r != size) {
+    err_write_1("str_init_var: buf_inspect_var: ");
+    err_inspect_sw_decimal(&r);
+    err_write_1(" != ");
+    err_inspect_sw_decimal(&size);
+    err_write_1("\n");
+    buf_clean(&buf);
+    return NULL;
+  }
+  assert(buf.wpos == (uw) size);
+  return buf_to_str(&buf, str);
+}
+
 s_str * str_init_vf (s_str *str, const char *fmt, va_list ap)
 {
   int len;
diff --git a/libkc3/str.h b/libkc3/str.h
index 067a339..f7b05ce 100644
--- a/libkc3/str.h
+++ b/libkc3/str.h
@@ -72,6 +72,7 @@ PROTOTYPE_STR_INIT_INT(u16);
 PROTOTYPE_STR_INIT_INT(u32);
 PROTOTYPE_STR_INIT_INT(u64);
 PROTOTYPE_STR_INIT_INT(uw);
+PROTOTYPE_STR_INIT(var, const s_tag *);
 s_str * str_init_vf (s_str *str, const char *fmt, va_list ap);
 
 /* Constructors, call str_delete after use */
diff --git a/libkc3/struct.c b/libkc3/struct.c
index 69ef986..7e70a99 100644
--- a/libkc3/struct.c
+++ b/libkc3/struct.c
@@ -13,6 +13,8 @@
 #include <string.h>
 #include "alloc.h"
 #include "assert.h"
+#include "buf.h"
+#include "buf_parse.h"
 #include "data.h"
 #include "env.h"
 #include "list.h"
@@ -180,13 +182,32 @@ s_struct * struct_init (s_struct *s, const s_sym *module)
   return s;
 }
 
-s_struct * struct_init_1 (s_struct *s, const s8 *p)
+s_struct * struct_init_1 (s_struct *s, const char *p)
 {
+  s_buf buf;
+  uw len;
+  sw r;
+  s_struct tmp;
   assert(s);
   assert(p);
-  (void) s;
-  (void) p;
-  // FIXME
+  len = strlen(p);
+  buf_init_const(&buf, len, p);
+  buf.wpos = len;
+  r = buf_parse_struct(&buf, &tmp);
+  if (r < 0 || (uw) r != len) {
+    err_puts("struct_init_1: invalid struct");
+    assert(! "struct_init_1: invalid struct");
+    if (r > 0)
+      struct_clean(&tmp);
+    return NULL;
+  }
+  if (! env_eval_struct(&g_kc3_env, &tmp, s)) {
+    err_puts("struct_init_1: env_eval_struct");
+    assert(! "struct_init_1: env_eval_struct");
+    struct_clean(&tmp);
+    return NULL;
+  }
+  struct_clean(&tmp);
   return s;
 }
 
@@ -341,7 +362,7 @@ s_struct * struct_new (const s_sym *module)
   return s;
 }
 
-s_struct * struct_new_1 (const s8 *p)
+s_struct * struct_new_1 (const char *p)
 {
   s_struct *s;
   assert(p);
diff --git a/libkc3/struct.h b/libkc3/struct.h
index 4d747fd..7991b79 100644
--- a/libkc3/struct.h
+++ b/libkc3/struct.h
@@ -23,7 +23,7 @@
 /* Stack-allocation compatible functions, call struct_clean after use. */
 void       struct_clean (s_struct *s);
 s_struct * struct_init (s_struct *s, const s_sym *module);
-s_struct * struct_init_1 (s_struct *s, const s8 *p);
+s_struct * struct_init_1 (s_struct *s, const char *p);
 s_struct * struct_init_cast (s_struct *s, const s_sym * const *type,
                              const s_tag *tag);
 s_struct * struct_init_copy (s_struct *s, const s_struct *src);
@@ -36,7 +36,7 @@ s_struct * struct_init_with_data (s_struct *s, const s_sym *module,
 /* Heap-allocation functions, call struct_delete after use. */
 void       struct_delete (s_struct *s);
 s_struct * struct_new (const s_sym *module);
-s_struct * struct_new_1 (const s8 *p);
+s_struct * struct_new_1 (const char *p);
 s_struct * struct_new_copy (const s_struct *src);
 s_struct * struct_new_with_data (const s_sym *module, void *data);
 
diff --git a/test/ikc3/str.kc3 b/test/ikc3/str.kc3
index 2018a0b..8019691 100644
--- a/test/ikc3/str.kc3
+++ b/test/ikc3/str.kc3
@@ -50,6 +50,14 @@ quote "1 + 2 = #{1 + 2}"
 "1 + 2 = #{1 + 2}"
 quote "#{%{a: 1, b: 2}}"
 "#{%{a: 1, b: 2}}"
+quote "#{:+}"
+"#{:+}"
+quote "#{?}"
+"#{?}"
+quote "#{0}"
+"#{0}"
+quote "#{:left}"
+"#{:left}"
 quote "#{%KC3.Operator{}}"
 "#{%KC3.Operator{}}"
 quote "#{%KC3.Operator{sym: :-}}"
diff --git a/test/ikc3/str.out.expected b/test/ikc3/str.out.expected
index 1189555..f1c43f3 100644
--- a/test/ikc3/str.out.expected
+++ b/test/ikc3/str.out.expected
@@ -35,6 +35,14 @@
 "1 + 2 = 3"
 "#{%{a: 1, b: 2}}"
 "%{a: 1, b: 2}"
+"#{:+}"
+"+"
+"#{?}"
+"?"
+"#{0}"
+"0"
+"#{:left}"
+"left"
 "#{%KC3.Operator{}}"
 "%KC3.Operator{sym: :+, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}"
 "#{%KC3.Operator{sym: :-, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}}"
diff --git a/test/inspect_test.c b/test/inspect_test.c
index fbb4578..095995e 100644
--- a/test/inspect_test.c
+++ b/test/inspect_test.c
@@ -13,6 +13,7 @@
 #include <assert.h>
 #include <string.h>
 #include "../libkc3/array.h"
+#include "../libkc3/buf_inspect.h"
 #include "../libkc3/call.h"
 #include "../libkc3/fact.h"
 #include "../libkc3/ident.h"
@@ -23,6 +24,7 @@
 #include "../libkc3/str.h"
 #include "../libkc3/sym.h"
 #include "../libkc3/tag.h"
+#include "../libkc3/struct.h"
 #include "../libkc3/tuple.h"
 #include "test.h"
 
@@ -126,7 +128,7 @@
     test_context(NULL);                                                \
   } while (0)
 
-#define INSPECT_TEST_STR_1(test, expected)                               \
+#define INSPECT_TEST_STR_1(test, expected)                             \
   do {                                                                 \
     s_str result;                                                      \
     s_str str;                                                         \
@@ -138,6 +140,22 @@
     test_context(NULL);                                                \
   } while (0)
 
+#define INSPECT_TEST_STRUCT(test, expected)                            \
+  do {                                                                 \
+    s_pretty pretty = {0};                                             \
+    s_str result;                                                      \
+    s_struct struct_test;                                              \
+    assert(test);                                                      \
+    test_context("inspect_struct(" # test ") -> " # expected);         \
+    struct_init_1(&struct_test, (test));                               \
+    TEST_EQ(buf_inspect_struct_size(&pretty, &struct_test),            \
+            strlen(expected));                                         \
+    TEST_EQ(inspect_struct(&struct_test, &result), &result);           \
+    TEST_STRNCMP(result.ptr.p, (expected), result.size);               \
+    str_clean(&result);                                                \
+    test_context(NULL);                                                \
+  } while (0)
+
 #define INSPECT_TEST_SYM(test, result)                                 \
   do {                                                                 \
     const s_sym *sym;                                                  \
@@ -177,6 +195,7 @@ TEST_CASE_PROTOTYPE(inspect_ident);
 TEST_CASE_PROTOTYPE(inspect_list);
 TEST_CASE_PROTOTYPE(inspect_ratio);
 TEST_CASE_PROTOTYPE(inspect_str);
+TEST_CASE_PROTOTYPE(inspect_struct);
 TEST_CASE_PROTOTYPE(inspect_sym);
 TEST_CASE_PROTOTYPE(inspect_tuple);
 
@@ -190,6 +209,7 @@ void inspect_test (void)
   TEST_CASE_RUN(inspect_list);
   TEST_CASE_RUN(inspect_ratio);
   TEST_CASE_RUN(inspect_str);
+  TEST_CASE_RUN(inspect_struct);
   TEST_CASE_RUN(inspect_sym);
   TEST_CASE_RUN(inspect_tuple);
 }
@@ -395,6 +415,14 @@ TEST_CASE(inspect_str)
 }
 TEST_CASE_END(inspect_str)
 
+TEST_CASE(inspect_struct)
+{
+  INSPECT_TEST_STRUCT("%KC3.Operator{}", "%KC3.Operator{sym: :+, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}");
+  INSPECT_TEST_STRUCT("%KC3.Operator{sym: :-}",
+                      "%KC3.Operator{sym: :-, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}");
+}
+TEST_CASE_END(inspect_struct)
+
 TEST_CASE(inspect_sym)
 {
   INSPECT_TEST_SYM("", ":\"\"");