Commit effa72e006ece089387e2fadbaa27c09436d1805

Thomas de Grivel 2024-08-22T02:33:51

facts_with_tags and variables

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
diff --git a/.ikc3_history b/.ikc3_history
index bc800e4..e64971d 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,17 +1,3 @@
-a = ?
-a <- 1
-a
-(Ptr) a
-a <- 1
-a <- 2
-type(a)
-a = ?
-a <- 1
-a
-a = ?
-a <- 1
-a
-a = ?
 a <- 1
 a = ?
 a <- 1
@@ -97,3 +83,17 @@ a <- 1
 a
 (Ptr) 0x1801350e830
 (Tag) (Ptr) 0x1801350e830
+Facts.with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) { puts(fact.object); 1 })
+Facts.with(Facts.env_facts(), [[KC3, :operator, ?]], fn (fact) { puts(fact.object); 1 })
+(Ptr) 0xf24fc9ce000
+(Tag) (Ptr) 0xf24fc9ce000
+?
+Facts.with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) { puts(fact.object); 1 })
+Facts.with(Facts.env_facts(), [[KC3, :operator, ?]], fn (fact) { puts(fact.object); 1 })
+Facts.with(Facts.env_facts(), quote [[KC3, :operator, op]], fn (fact) { puts(fact.object); 1 })
+op
+op = ?
+Facts.with(Facts.env_facts(), quote [[KC3, :operator, op]], fn (fact) { puts(fact.object); 1 })
+op
+Facts.with(Facts.env_facts(), quote [[KC3, :operator, op]], fn (fact) { puts(fact.object); 1 })
+Facts.with(Facts.env_facts(), quote [[KC3, :operator, op = (Ident) ?]], fn (fact) { puts(fact.object); 1 })
diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index a36cb3d..97edddb 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -3746,21 +3746,26 @@ sw buf_inspect_var (s_buf *buf, const s_tag *tag)
   assert(tag->type == TAG_VAR);
   assert(tag->data.var.type);
   if (tag->data.var.type == &g_sym_Tag) {
-    if ((r = buf_write_1(buf, "?0x")) < 0)
+    if ((r = buf_write_1(buf, "?")) < 0)
       return r;
   }
   else {
     if ((r = buf_inspect_paren_sym(buf, tag->data.var.type)) < 0)
       return r;
     result += r;
-    if ((r = buf_write_1(buf, " ?0x")) < 0)
+    if ((r = buf_write_1(buf, " ?")) < 0)
+      return r;
+    result += r;
+  }
+  if (tag->data.var.ptr) {
+    if ((r = buf_write_1(buf, "0x")) < 0)
+      return r;
+    result += r;
+    if ((r = buf_inspect_uw_hexadecimal
+         (buf, (uw *) &tag->data.var.ptr)) < 0)
       return r;
     result += r;
   }
-  if ((r = buf_inspect_uw_hexadecimal
-       (buf, (uw *) &tag->data.var.ptr)) < 0)
-    return r;
-  result += r;
   return result;
 }
 
@@ -3785,10 +3790,15 @@ sw buf_inspect_var_size (s_pretty *pretty, const s_tag *tag)
       return r;
     result += r;
   }
-  if ((r = buf_inspect_uw_hexadecimal_size
-       (pretty, (uw *) &tag->data.var.ptr)) < 0)
-    return r;
-  result += r;
+  if (tag->data.var.ptr) {
+    if ((r = buf_write_1_size(pretty, "0x")) < 0)
+      return r;
+    result += r;
+    if ((r = buf_inspect_uw_hexadecimal_size
+         (pretty, (uw *) &tag->data.var.ptr)) < 0)
+      return r;
+    result += r;
+  }
   return result;
 }
 
diff --git a/libkc3/buf_parse.c b/libkc3/buf_parse.c
index 9119676..330a017 100644
--- a/libkc3/buf_parse.c
+++ b/libkc3/buf_parse.c
@@ -20,6 +20,7 @@
 #include "buf.h"
 #include "buf_inspect.h"
 #include "buf_parse.h"
+#include "buf_parse_uw.h"
 #include "buf_save.h"
 #include "call.h"
 #include "cfn.h"
@@ -4517,6 +4518,7 @@ sw buf_parse_unquote (s_buf *buf, s_unquote *dest)
 sw buf_parse_var (s_buf *buf, s_var *dest)
 {
   character c;
+  uw i;
   sw r;
   sw result = 0;
   s_buf_save save;
@@ -4536,11 +4538,18 @@ sw buf_parse_var (s_buf *buf, s_var *dest)
   if ((r = buf_read_1(buf, "?")) <= 0)
     goto restore;
   result += r;
+  if ((r = buf_read_1(buf, "0x")) < 0)
+    goto ok;
+  if (r > 0) {
+    if ((r = buf_parse_uw_hexadecimal(buf, &i)) < 0)
+      goto restore;
+  }
   if (buf_peek_character_utf8(buf, &c) > 0 &&
       ! ident_character_is_reserved(c)) {
     r = 0;
     goto restore;
   }
+ ok:
   *dest = tmp;
   r = result;
   goto clean;
diff --git a/libkc3/buf_parse_u.c.in b/libkc3/buf_parse_u.c.in
index edacd21..9be4d27 100644
--- a/libkc3/buf_parse_u.c.in
+++ b/libkc3/buf_parse_u.c.in
@@ -155,6 +155,11 @@ sw buf_parse_u_bits$_base (s_buf *buf, const s_str *base,
   return r;
 }
 
+sw buf_parse_u_bits$_hexadecimal (s_buf *buf, u_bits$ *dest)
+{
+  return buf_parse_u_bits$_base(buf, &g_kc3_base_hexadecimal, dest);
+}
+
 s_tag * buf_parse_tag_u_bits$ (s_buf *buf, s_tag *dest)
 {
   s_tag tmp = {0};
diff --git a/libkc3/buf_parse_u.h.in b/libkc3/buf_parse_u.h.in
index fee8d12..ae3877d 100644
--- a/libkc3/buf_parse_u.h.in
+++ b/libkc3/buf_parse_u.h.in
@@ -19,6 +19,7 @@
 sw buf_parse_u_bits$ (s_buf *buf, u_bits$ *dest);
 sw buf_parse_u_bits$_base (s_buf *buf, const s_str *base,
                            u_bits$ *dest);
+sw buf_parse_u_bits$_hexadecimal (s_buf *buf, u_bits$ *dest);
 s_tag * buf_parse_tag_u_bits$ (s_buf *buf, s_tag *dest);
 s_tag * buf_parse_tag_u_bits$_base (s_buf *buf, const s_str *base,
                                     s_tag *dest);
diff --git a/libkc3/buf_parse_u16.c b/libkc3/buf_parse_u16.c
index fce9b62..a5b2291 100644
--- a/libkc3/buf_parse_u16.c
+++ b/libkc3/buf_parse_u16.c
@@ -155,6 +155,11 @@ sw buf_parse_u16_base (s_buf *buf, const s_str *base,
   return r;
 }
 
+sw buf_parse_u16_hexadecimal (s_buf *buf, u16 *dest)
+{
+  return buf_parse_u16_base(buf, &g_kc3_base_hexadecimal, dest);
+}
+
 s_tag * buf_parse_tag_u16 (s_buf *buf, s_tag *dest)
 {
   s_tag tmp = {0};
diff --git a/libkc3/buf_parse_u16.h b/libkc3/buf_parse_u16.h
index 4ab68da..3a11e19 100644
--- a/libkc3/buf_parse_u16.h
+++ b/libkc3/buf_parse_u16.h
@@ -19,6 +19,7 @@
 sw buf_parse_u16 (s_buf *buf, u16 *dest);
 sw buf_parse_u16_base (s_buf *buf, const s_str *base,
                            u16 *dest);
+sw buf_parse_u16_hexadecimal (s_buf *buf, u16 *dest);
 s_tag * buf_parse_tag_u16 (s_buf *buf, s_tag *dest);
 s_tag * buf_parse_tag_u16_base (s_buf *buf, const s_str *base,
                                     s_tag *dest);
diff --git a/libkc3/buf_parse_u32.c b/libkc3/buf_parse_u32.c
index d3e771b..111135c 100644
--- a/libkc3/buf_parse_u32.c
+++ b/libkc3/buf_parse_u32.c
@@ -155,6 +155,11 @@ sw buf_parse_u32_base (s_buf *buf, const s_str *base,
   return r;
 }
 
+sw buf_parse_u32_hexadecimal (s_buf *buf, u32 *dest)
+{
+  return buf_parse_u32_base(buf, &g_kc3_base_hexadecimal, dest);
+}
+
 s_tag * buf_parse_tag_u32 (s_buf *buf, s_tag *dest)
 {
   s_tag tmp = {0};
diff --git a/libkc3/buf_parse_u32.h b/libkc3/buf_parse_u32.h
index 8fbc6c8..e798007 100644
--- a/libkc3/buf_parse_u32.h
+++ b/libkc3/buf_parse_u32.h
@@ -19,6 +19,7 @@
 sw buf_parse_u32 (s_buf *buf, u32 *dest);
 sw buf_parse_u32_base (s_buf *buf, const s_str *base,
                            u32 *dest);
+sw buf_parse_u32_hexadecimal (s_buf *buf, u32 *dest);
 s_tag * buf_parse_tag_u32 (s_buf *buf, s_tag *dest);
 s_tag * buf_parse_tag_u32_base (s_buf *buf, const s_str *base,
                                     s_tag *dest);
diff --git a/libkc3/buf_parse_u64.c b/libkc3/buf_parse_u64.c
index c27df5d..a852a87 100644
--- a/libkc3/buf_parse_u64.c
+++ b/libkc3/buf_parse_u64.c
@@ -155,6 +155,11 @@ sw buf_parse_u64_base (s_buf *buf, const s_str *base,
   return r;
 }
 
+sw buf_parse_u64_hexadecimal (s_buf *buf, u64 *dest)
+{
+  return buf_parse_u64_base(buf, &g_kc3_base_hexadecimal, dest);
+}
+
 s_tag * buf_parse_tag_u64 (s_buf *buf, s_tag *dest)
 {
   s_tag tmp = {0};
diff --git a/libkc3/buf_parse_u64.h b/libkc3/buf_parse_u64.h
index 3c5468e..7e71d1c 100644
--- a/libkc3/buf_parse_u64.h
+++ b/libkc3/buf_parse_u64.h
@@ -19,6 +19,7 @@
 sw buf_parse_u64 (s_buf *buf, u64 *dest);
 sw buf_parse_u64_base (s_buf *buf, const s_str *base,
                            u64 *dest);
+sw buf_parse_u64_hexadecimal (s_buf *buf, u64 *dest);
 s_tag * buf_parse_tag_u64 (s_buf *buf, s_tag *dest);
 s_tag * buf_parse_tag_u64_base (s_buf *buf, const s_str *base,
                                     s_tag *dest);
diff --git a/libkc3/buf_parse_u8.c b/libkc3/buf_parse_u8.c
index c339e05..316e0c1 100644
--- a/libkc3/buf_parse_u8.c
+++ b/libkc3/buf_parse_u8.c
@@ -155,6 +155,11 @@ sw buf_parse_u8_base (s_buf *buf, const s_str *base,
   return r;
 }
 
+sw buf_parse_u8_hexadecimal (s_buf *buf, u8 *dest)
+{
+  return buf_parse_u8_base(buf, &g_kc3_base_hexadecimal, dest);
+}
+
 s_tag * buf_parse_tag_u8 (s_buf *buf, s_tag *dest)
 {
   s_tag tmp = {0};
diff --git a/libkc3/buf_parse_u8.h b/libkc3/buf_parse_u8.h
index fd23a59..882081e 100644
--- a/libkc3/buf_parse_u8.h
+++ b/libkc3/buf_parse_u8.h
@@ -19,6 +19,7 @@
 sw buf_parse_u8 (s_buf *buf, u8 *dest);
 sw buf_parse_u8_base (s_buf *buf, const s_str *base,
                            u8 *dest);
+sw buf_parse_u8_hexadecimal (s_buf *buf, u8 *dest);
 s_tag * buf_parse_tag_u8 (s_buf *buf, s_tag *dest);
 s_tag * buf_parse_tag_u8_base (s_buf *buf, const s_str *base,
                                     s_tag *dest);
diff --git a/libkc3/buf_parse_uw.c b/libkc3/buf_parse_uw.c
index 43d9b9a..c123fa7 100644
--- a/libkc3/buf_parse_uw.c
+++ b/libkc3/buf_parse_uw.c
@@ -155,6 +155,11 @@ sw buf_parse_uw_base (s_buf *buf, const s_str *base,
   return r;
 }
 
+sw buf_parse_uw_hexadecimal (s_buf *buf, uw *dest)
+{
+  return buf_parse_uw_base(buf, &g_kc3_base_hexadecimal, dest);
+}
+
 s_tag * buf_parse_tag_uw (s_buf *buf, s_tag *dest)
 {
   s_tag tmp = {0};
diff --git a/libkc3/buf_parse_uw.h b/libkc3/buf_parse_uw.h
index c1c0b7c..c298fa5 100644
--- a/libkc3/buf_parse_uw.h
+++ b/libkc3/buf_parse_uw.h
@@ -19,6 +19,7 @@
 sw buf_parse_uw (s_buf *buf, uw *dest);
 sw buf_parse_uw_base (s_buf *buf, const s_str *base,
                            uw *dest);
+sw buf_parse_uw_hexadecimal (s_buf *buf, uw *dest);
 s_tag * buf_parse_tag_uw (s_buf *buf, s_tag *dest);
 s_tag * buf_parse_tag_uw_base (s_buf *buf, const s_str *base,
                                     s_tag *dest);
diff --git a/libkc3/env.c b/libkc3/env.c
index 78d2e84..ddfb65e 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -2058,6 +2058,7 @@ s_tag * env_facts_with (s_env *env, s_facts *facts, s_list **spec,
     if (! facts_with_cursor_next(&cursor, &fact))
       goto clean;
     if (! fact) {
+      err_puts("env_facts_with: ok");
       goto ok;
     }
     tag_clean(&tmp);
@@ -2124,7 +2125,7 @@ s_facts_with_cursor * env_facts_with_list (s_env *env, s_facts *facts,
     tmp_tail = &(*tmp_tail)->next.data.list;
     spec_i = list_next(spec_i);
   }
-  if (true) {
+  if (false) {
     err_write_1("env_facts_with_list: spec = ");
     err_inspect_list((const s_list * const *) &tmp);
     err_write_1("\n");
@@ -2134,7 +2135,7 @@ s_facts_with_cursor * env_facts_with_list (s_env *env, s_facts *facts,
     assert(! "env_facts_with_list: facts_spec_new_list");
     goto ko;
   }
-  if (true) {
+  if (false) {
     err_write_1("env_facts_with_list: spec = ");
     err_inspect_facts_spec(facts_spec);
     err_write_1("\n");
diff --git a/libkc3/facts_with.c b/libkc3/facts_with.c
index 2b54df8..76a48a7 100644
--- a/libkc3/facts_with.c
+++ b/libkc3/facts_with.c
@@ -27,37 +27,34 @@ s_facts_with_cursor * facts_with (s_facts *facts,
                                   p_facts_spec spec)
 {
   uw facts_count;
+  s_facts_with_cursor tmp = {0};
   assert(facts);
   assert(cursor);
   assert(spec);
   facts_count = facts_spec_count_facts(spec);
-  cursor->facts = facts;
-  cursor->facts_count = facts_count;
+  tmp.facts = facts;
+  tmp.facts_count = facts_count;
   if (facts_count > 0) {
-    cursor->levels = alloc(facts_count *
-                           sizeof(s_facts_with_cursor_level));
-    if (! cursor->levels)
+    tmp.levels = alloc(facts_count *
+                       sizeof(s_facts_with_cursor_level));
+    if (! tmp.levels)
       return NULL;
-    cursor->spec = facts_spec_new_expand(spec);
-    /*
-    buf_inspect_facts_spec(&g_c3_env.err, spec);
-    buf_write_1(&g_c3_env.err, "\n");
-    buf_inspect_facts_spec(&g_c3_env.err, cursor->spec);
-    buf_write_1(&g_c3_env.err, "\n");
-    buf_flush(&g_c3_env.err);
-    */
-    /* facts_spec_sort(cursor->spec); */
+    tmp.spec = facts_spec_new_expand(spec);
+    if (false) {
+      err_write_1("facts_with: spec = ");
+      err_inspect_facts_spec(spec);
+      err_write_1("\nfacts_with: tmp.spec = ");
+      err_inspect_facts_spec(tmp.spec);
+      err_write_1("\n");
+    }
+    /* facts_spec_sort(tmp.spec); */
   }
-  else {
-    cursor->levels = NULL;
-    cursor->spec = NULL;
-  }
-  cursor->level = 0;
-  if (pthread_mutex_init(&cursor->mutex, NULL)) {
+  if (pthread_mutex_init(&tmp.mutex, NULL)) {
     err_puts("facts_with: pthread_mutex_init");
     assert(! "facts_with: pthread_mutex_init");
     return NULL;
   }
+  *cursor = tmp;
   return cursor;
 }
 
diff --git a/test/ikc3/facts_with_tags.kc3 b/test/ikc3/facts_with_tags.kc3
index 9ebc9cd..082c42f 100644
--- a/test/ikc3/facts_with_tags.kc3
+++ b/test/ikc3/facts_with_tags.kc3
@@ -1,8 +1,38 @@
 quote Facts.with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) {
   puts(fact.object)
-  void
+  1
 })
 Facts.with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) {
   puts(fact.object)
-  void
+  1
+})
+quote Facts.with_tags(Facts.env_facts(), KC3, :operator, op = ?, fn (fact) {
+  puts(op)
+  2
+})
+Facts.with_tags(Facts.env_facts(), KC3, :operator, op = ?, fn (fact) {
+  puts(op)
+  2
+})
+quote op = ?
+op = ?
+quote Facts.with_tags(Facts.env_facts(), KC3, :operator, op, fn (fact) {
+  puts(op)
+  3
+})
+Facts.with_tags(Facts.env_facts(), KC3, :operator, op, fn (fact) {
+  puts(op)
+  3
+})
+quote type(op)
+type(op)
+quote op = quote KC3.operator_eq
+op = quote KC3.operator_eq
+quote Facts.with_tags(Facts.env_facts(), KC3, :operator, op, fn (fact) {
+  puts(op)
+  4
+})
+Facts.with_tags(Facts.env_facts(), KC3, :operator, op, fn (fact) {
+  puts(op)
+  4
 })
diff --git a/test/ikc3/facts_with_tags.out.expected b/test/ikc3/facts_with_tags.out.expected
index 269d066..644156a 100644
--- a/test/ikc3/facts_with_tags.out.expected
+++ b/test/ikc3/facts_with_tags.out.expected
@@ -1,4 +1,4 @@
-Facts.with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) { puts(fact.object); void })
+Facts.with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) { puts(fact.object); 1 })
 operator_eq
 operator_gt
 operator_lt
@@ -21,10 +21,82 @@ operator_bnot
 operator_bxor
 operator_equal
 operator_paren
+operator_assign
 operator_not_eq
 operator_require
 operator_brackets
 operator_defstruct
 operator_shift_left
 operator_shift_right
-void
+1
+Facts.with_tags(Facts.env_facts(), KC3, :operator, op = ?, fn (fact) { puts(op); 2 })
+operator_eq
+operator_gt
+operator_lt
+operator_or
+operator_add
+operator_and
+operator_bor
+operator_div
+operator_gte
+operator_lte
+operator_mod
+operator_mul
+operator_neg
+operator_not
+operator_pin
+operator_sub
+operator_addi
+operator_band
+operator_bnot
+operator_bxor
+operator_equal
+operator_paren
+operator_assign
+operator_not_eq
+operator_require
+operator_brackets
+operator_defstruct
+operator_shift_left
+operator_shift_right
+2
+op = ?
+?0x
+Facts.with_tags(Facts.env_facts(), KC3, :operator, op, fn (fact) { puts(op); 3 })
+operator_eq
+operator_gt
+operator_lt
+operator_or
+operator_add
+operator_and
+operator_bor
+operator_div
+operator_gte
+operator_lte
+operator_mod
+operator_mul
+operator_neg
+operator_not
+operator_pin
+operator_sub
+operator_addi
+operator_band
+operator_bnot
+operator_bxor
+operator_equal
+operator_paren
+operator_assign
+operator_not_eq
+operator_require
+operator_brackets
+operator_defstruct
+operator_shift_left
+operator_shift_right
+3
+type(op)
+Var
+op = quote operator_eq
+operator_eq
+Facts.with_tags(Facts.env_facts(), KC3, :operator, op, fn (fact) { puts(op); 4 })
+operator_eq
+4