Commit eaa311733131ca40c55105bbb61df179cdbde71c

Thomas de Grivel 2023-07-23T17:12:30

wip

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
diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 9841e39..1e4b2bb 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -21,13 +21,36 @@ sw buf_inspect_array_data_rec (s_buf *buf, const s_array *array,
                                uw dimension, uw *address,
                                f_buf_inspect inspect, u8 **data);
 sw buf_inspect_array_data_size (const s_array *array);
-sw buf_inspect_array_data_size_rec (s_buf *buf, const s_array *array,
-                               uw dimension, uw *address,
-                               f_buf_inspect inspect, u8 **data);
+sw buf_inspect_array_data_size_rec (const s_array *array,
+                                    uw dimension, uw *address,
+                                    f_buf_inspect_size inspect,
+                                    u8 **data);
 sw buf_inspect_array_type (s_buf *buf, const s_array *array);
 sw buf_inspect_array_type_size (const s_array *array);
 sw buf_inspect_tag_type (s_buf *buf, e_tag_type type);
 
+sw buf_inspect_array (s_buf *buf, const s_array *array)
+{
+  sw r;
+  sw result = 0;
+  assert(buf);
+  assert(array);
+  if ((r = buf_inspect_array_type(buf, array)) <= 0)
+    goto clean;
+  result += r;
+  if ((r = buf_write_1(buf, " ")) < 0)
+    goto clean;
+  result += r;
+  if ((r = buf_inspect_array_data(buf, array)) < 0) {
+    warnx("buf_inspect_array: buf_inspect_array_data");
+    goto clean;
+  }
+  result += r;
+  r = result;
+ clean:
+  return r;
+}
+
 sw buf_inspect_array_data (s_buf *buf, const s_array *array)
 {
   uw *address;
@@ -41,6 +64,7 @@ sw buf_inspect_array_data (s_buf *buf, const s_array *array)
   data = array->data;
   r = buf_inspect_array_data_rec(buf, array, 0, address,
                                  inspect, &data);
+  free(address);
   return r;
 }
 
@@ -82,54 +106,113 @@ sw buf_inspect_array_data_rec (s_buf *buf, const s_array *array,
   return r;
 }
 
-sw buf_inspect_array_type (s_buf *buf, const s_array *array)
+sw buf_inspect_array_data_size (const s_array *array)
+{
+  uw *address;
+  u8 *data;
+  f_buf_inspect_size inspect;
+  sw r;
+  assert(array);
+  address = calloc(array->dimension, sizeof(uw));
+  inspect = tag_type_to_buf_inspect_size(array->type);
+  data = array->data;
+  r = buf_inspect_array_data_size_rec(array, 0, address,
+                                      inspect, &data);
+  free(address);
+  return r;
+}
+
+sw buf_inspect_array_data_size_rec (const s_array *array,
+                                    uw dimension, uw *address,
+                                    f_buf_inspect_size inspect, u8 **data)
+{
+  sw r;
+  sw result = 0;
+  r = strlen("{");
+  result += r;
+  address[dimension] = 0;
+  while (1) {
+    if (dimension == array->dimension - 1) {
+      if ((r = inspect(*data)) <= 0)
+        goto clean;
+      result += r;
+      *data += array->dimensions[dimension].item_size;
+    }
+    else {
+      if ((r = buf_inspect_array_data_size_rec(array, dimension + 1,
+                                               address, inspect,
+                                               data)) <= 0)
+        goto clean;
+      result += r;
+    }
+    address[dimension]++;
+    if (address[dimension] == array->dimensions[dimension].count)
+      break;
+    r = strlen(", ");
+    result += r;
+  }
+  r = strlen("}");
+  result += r;
+  r = result;
+ clean:
+  return r;
+}
+
+sw buf_inspect_array_size (const s_array *array)
 {
   sw r;
   sw result = 0;
-  assert(buf);
   assert(array);
-  if ((r = buf_write_1(buf, "(")) <= 0)
+  if ((r = buf_inspect_array_type_size(array)) <= 0)
     goto clean;
   result += r;
-  if ((r = buf_inspect_tag_type(buf, array->type)) <= 0)
-    goto clean;
+  r = strlen(" ");
   result += r;
-  if ((r = buf_write_1(buf, ")")) <= 0)
+  if ((r = buf_inspect_array_data_size(array)) <= 0) {
+    warnx("buf_inspect_array_size: buf_inspect_array_data");
     goto clean;
+  }
   result += r;
   r = result;
  clean:
   return r;
 }
 
-sw buf_inspect_array (s_buf *buf, const s_array *array)
+sw buf_inspect_array_type (s_buf *buf, const s_array *array)
 {
   sw r;
   sw result = 0;
   assert(buf);
   assert(array);
-  if ((r = buf_inspect_array_type(buf, array)) <= 0)
+  if ((r = buf_write_1(buf, "(")) <= 0)
     goto clean;
   result += r;
-  if ((r = buf_write_1(buf, " ")) < 0)
+  if ((r = buf_inspect_tag_type(buf, array->type)) <= 0)
     goto clean;
   result += r;
-  if ((r = buf_inspect_array_data(buf, array)) < 0) {
-    warnx("buf_inspect_array: buf_inspect_array_data");
+  if ((r = buf_write_1(buf, ")")) <= 0)
     goto clean;
-  }
   result += r;
   r = result;
  clean:
   return r;
-
 }
 
-sw buf_inspect_array_size (const s_array *a)
+sw buf_inspect_array_type_size (const s_array *array)
 {
-  assert(a);
-  (void) a;
-  return -1;
+  sw r;
+  sw result = 0;
+  assert(array);
+  r = strlen("(");
+  result += r;
+  if ((r = buf_inspect_tag_type_size(array->type)) <= 0)
+    goto clean;
+  result += r;
+  r = strlen(")");
+  result += r;
+  r = result;
+ clean:
+  return r;
 }
 
 sw buf_inspect_bool (s_buf *buf, const bool *b)
@@ -1372,6 +1455,69 @@ sw buf_inspect_tag_type (s_buf *buf, e_tag_type type)
   return -1;
 }
 
+sw buf_inspect_tag_type_size (e_tag_type type)
+{
+  switch(type) {
+  case TAG_VOID:
+    return strlen("void");
+  case TAG_ARRAY:
+    return strlen("array");
+  case TAG_BOOL:
+    return strlen("bool");
+  case TAG_CALL:
+  case TAG_CALL_FN:
+  case TAG_CALL_MACRO:
+    return strlen("call");    
+  case TAG_CFN:
+    return strlen("cfn");
+  case TAG_CHARACTER:
+    return strlen("character");
+  case TAG_F32:
+    return strlen("f32");
+  case TAG_F64:
+    return strlen("f64");
+  case TAG_FN:
+    return strlen("fn");
+  case TAG_IDENT:
+    return strlen("ident");
+  case TAG_INTEGER:
+    return strlen("integer");
+  case TAG_LIST:
+    return strlen("list");
+  case TAG_PTAG:
+    return strlen("ptag");
+  case TAG_QUOTE:
+    return strlen("quote");
+  case TAG_S8:
+    return strlen("s8");
+  case TAG_S16:
+    return strlen("s16");
+  case TAG_S32:
+    return strlen("s32");
+  case TAG_S64:
+    return strlen("s64");
+  case TAG_STR:
+    return strlen("str");
+  case TAG_SYM:
+    return strlen("sym");
+  case TAG_TUPLE:
+    return strlen("tuple");
+  case TAG_U8:
+    return strlen("u8");
+  case TAG_U16:
+    return strlen("u16");
+  case TAG_U32:
+    return strlen("u32");
+  case TAG_U64:
+    return strlen("u64");
+  case TAG_VAR:
+    return strlen("var");
+  }
+  assert(! "buf_inspect_tag_type: unknown tag type");
+  errx(1, "buf_inspect_tag_type: unknown tag type");
+  return -1;
+}
+
 sw buf_inspect_tuple (s_buf *buf, const s_tuple *tuple)
 {
   u64 i = 0;
diff --git a/libc3/buf_inspect.h b/libc3/buf_inspect.h
index 612eef3..de5ee62 100644
--- a/libc3/buf_inspect.h
+++ b/libc3/buf_inspect.h
@@ -110,6 +110,8 @@ sw buf_inspect_sym_reserved_size (const s_sym *sym);
 sw buf_inspect_sym_size (const s_sym *sym);
 sw buf_inspect_tag (s_buf *buf, const s_tag *tag);
 sw buf_inspect_tag_size (const s_tag *tag);
+sw buf_inspect_tag_type (s_buf *buf, e_tag_type type);
+sw buf_inspect_tag_type_size (e_tag_type type);
 sw buf_inspect_tuple (s_buf *buf, const s_tuple *tuple);
 sw buf_inspect_tuple_size (const s_tuple *tuple);
 BUF_INSPECT_U_PROTOTYPES(8);
diff --git a/libc3/tag.c b/libc3/tag.c
index 0f84662..33ff5ac 100644
--- a/libc3/tag.c
+++ b/libc3/tag.c
@@ -2147,6 +2147,68 @@ f_buf_inspect tag_type_to_buf_inspect (e_tag_type type)
   return NULL;
 }
 
+f_buf_inspect_size tag_type_to_buf_inspect_size (e_tag_type type)
+{
+  switch (type) {
+  case TAG_VOID:
+    return (f_buf_inspect_size) buf_inspect_void_size;
+  case TAG_ARRAY:
+  case TAG_BOOL:
+    return (f_buf_inspect_size) buf_inspect_bool_size;
+  case TAG_CALL:
+  case TAG_CALL_FN:
+  case TAG_CALL_MACRO:
+    return (f_buf_inspect_size) buf_inspect_call_size;
+  case TAG_CFN:
+    return (f_buf_inspect_size) buf_inspect_cfn_size;
+  case TAG_CHARACTER:
+    return (f_buf_inspect_size) buf_inspect_character_size;
+  case TAG_F32:
+    return (f_buf_inspect_size) buf_inspect_f32_size;
+  case TAG_F64:
+    return (f_buf_inspect_size) buf_inspect_f64_size;
+  case TAG_FN:
+    return (f_buf_inspect_size) buf_inspect_fn_size;
+  case TAG_IDENT:
+    return (f_buf_inspect_size) buf_inspect_ident_size;
+  case TAG_INTEGER:
+    return (f_buf_inspect_size) buf_inspect_integer_size;
+  case TAG_S64:
+    return (f_buf_inspect_size) buf_inspect_s64_size;
+  case TAG_S32:
+    return (f_buf_inspect_size) buf_inspect_s32_size;
+  case TAG_S16:
+    return (f_buf_inspect_size) buf_inspect_s16_size;
+  case TAG_S8:
+    return (f_buf_inspect_size) buf_inspect_s8_size;
+  case TAG_U8:
+    return (f_buf_inspect_size) buf_inspect_u8_size;
+  case TAG_U16:
+    return (f_buf_inspect_size) buf_inspect_u16_size;
+  case TAG_U32:
+    return (f_buf_inspect_size) buf_inspect_u32_size;
+  case TAG_U64:
+    return (f_buf_inspect_size) buf_inspect_u64_size;
+  case TAG_LIST:
+    return (f_buf_inspect_size) buf_inspect_list_size;
+  case TAG_PTAG:
+    return (f_buf_inspect_size) buf_inspect_ptag_size;
+  case TAG_QUOTE:
+    return (f_buf_inspect_size) buf_inspect_quote_size;
+  case TAG_STR:
+    return (f_buf_inspect_size) buf_inspect_str_size;
+  case TAG_SYM:
+    return (f_buf_inspect_size) buf_inspect_sym_size;
+  case TAG_TUPLE:
+    return (f_buf_inspect_size) buf_inspect_tuple_size;
+  case TAG_VAR:
+    return (f_buf_inspect_size) buf_inspect_var_size;
+  }
+  assert(! "tag_type_to_buf_inspect_size: unknown tag type");
+  errx(1, "tag_type_to_buf_inspect_size: unknown tag type");
+  return NULL;
+}
+
 f_buf_parse tag_type_to_buf_parse (e_tag_type type)
 {
   switch (type) {
diff --git a/libc3/tag.h b/libc3/tag.h
index e69f99a..140459d 100644
--- a/libc3/tag.h
+++ b/libc3/tag.h
@@ -89,22 +89,23 @@ s_tag * tag_new_var ();
 void tag_delete (s_tag *tag);
 
 /* Observers */
-u64           tag_hash_u64 (const s_tag *tag);
-uw            tag_hash_uw (const s_tag *tag);
-s_str *       tag_inspect (const s_tag *tag, s_str *dest);
-e_bool        tag_is_bound_var (const s_tag *tag);
-e_bool        tag_is_number (const s_tag *tag);
-e_bool        tag_is_unbound_var (const s_tag *tag);
-s8            tag_number_compare (const s_tag *a, const s_tag *b);
-sw            tag_size (const s_tag *tag);
-void *        tag_to_ffi_pointer (s_tag *tag, const s_sym *type);
-ffi_type      tag_to_ffi_type(const s_tag *tag);
-void *        tag_to_pointer (s_tag *tag, e_tag_type type);
-sw            tag_type_size (e_tag_type type);
-f_buf_inspect tag_type_to_buf_inspect (e_tag_type type);
-f_buf_parse   tag_type_to_buf_parse (e_tag_type type);
-s8 *          tag_type_to_string (e_tag_type type);
-const s_sym * tag_type_to_sym (e_tag_type tag_type);
+u64                tag_hash_u64 (const s_tag *tag);
+uw                 tag_hash_uw (const s_tag *tag);
+s_str *            tag_inspect (const s_tag *tag, s_str *dest);
+e_bool             tag_is_bound_var (const s_tag *tag);
+e_bool             tag_is_number (const s_tag *tag);
+e_bool             tag_is_unbound_var (const s_tag *tag);
+s8                 tag_number_compare (const s_tag *a, const s_tag *b);
+sw                 tag_size (const s_tag *tag);
+void *             tag_to_ffi_pointer (s_tag *tag, const s_sym *type);
+ffi_type           tag_to_ffi_type(const s_tag *tag);
+void *             tag_to_pointer (s_tag *tag, e_tag_type type);
+sw                 tag_type_size (e_tag_type type);
+f_buf_inspect      tag_type_to_buf_inspect (e_tag_type type);
+f_buf_inspect_size tag_type_to_buf_inspect_size (e_tag_type type);
+f_buf_parse        tag_type_to_buf_parse (e_tag_type type);
+s8 *               tag_type_to_string (e_tag_type type);
+const s_sym *      tag_type_to_sym (e_tag_type tag_type);
 
 /* Modifiers */
 s_tag *  tag_1 (s_tag *tag, const s8 *p);
diff --git a/libc3/types.h b/libc3/types.h
index 3e8cc4a..67dae59 100644
--- a/libc3/types.h
+++ b/libc3/types.h
@@ -506,5 +506,6 @@ struct facts_with_cursor {
 
 /* Functions */
 typedef sw (* f_buf_inspect) (s_buf *buf, const void *x);
+typedef sw (* f_buf_inspect_size) (const void *x);
 
 #endif /* TYPES_H */
diff --git a/test/buf_inspect_test.c b/test/buf_inspect_test.c
index 9ff0098..35bb106 100644
--- a/test/buf_inspect_test.c
+++ b/test/buf_inspect_test.c
@@ -28,9 +28,9 @@
     buf_init(&buf_result, false, sizeof(b), b);                        \
     TEST_EQ(buf_inspect_array_size(&tmp), strlen(expected));           \
     TEST_EQ(buf_inspect_array(&buf_result, &tmp), strlen(expected));   \
-    array_clean(&tmp);                                                 \
     TEST_EQ(buf_result.wpos, strlen(expected));                        \
     TEST_STRNCMP(buf_result.ptr.ps8, (expected), buf_result.wpos);     \
+    array_clean(&tmp);                                                 \
     buf_clean(&buf_result);                                            \
   } while (0)