Commit ba40083674cc17da3fe537edf2ba0d5aa606f9ca

Thomas de Grivel 2023-10-19T13:04:03

wip operators

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
diff --git a/lib/c3/0.1/c3.facts b/lib/c3/0.1/c3.facts
index 41ff274..30dc597 100644
--- a/lib/c3/0.1/c3.facts
+++ b/lib/c3/0.1/c3.facts
@@ -3,111 +3,111 @@
 replace {C3, :is_a, :module}
 replace {C3, :operator, C3.operator00}
 replace {C3.operator00, :is_a, :operator}
-replace {C3.operator00, :symbol, _"()"}
+replace {C3.operator00, :symbol, :"()"}
 replace {C3.operator00, :arity, 1}
 replace {C3.operator00, :cfn, cfn Tag "tag_paren" (Tag, Result)}
 replace {C3.operator00, :operator_precedence, 1}
 replace {C3.operator00, :operator_associativity, :left}
 add {C3, :operator, C3.operator01}
 replace {C3.operator01, :is_a, :operator}
-replace {C3.operator01, :symbol, _"[]"}
+replace {C3.operator01, :symbol, :"[]"}
 replace {C3.operator01, :arity, 2}
 replace {C3.operator01, :cfn, cfn Tag "tag_brackets" (Tag, Tag, Result)}
 replace {C3.operator01, :operator_precedence, 1}
 replace {C3.operator01, :operator_associativity, :left}
 add {C3, :operator, C3.operator02}
 replace {C3.operator02, :is_a, :operator}
-replace {C3.operator02, :symbol, !}
+replace {C3.operator02, :symbol, :!}
 replace {C3.operator02, :arity, 1}
 replace {C3.operator02, :cfn, cfn :bool "tag_not" (Tag)}
 replace {C3.operator02, :operator_precedence, 2}
 replace {C3.operator02, :operator_associativity, :right}
 add {C3, :operator, C3.operator03}
 replace {C3.operator03, :is_a, :operator}
-replace {C3.operator03, :symbol, ~}
+replace {C3.operator03, :symbol, :~}
 replace {C3.operator03, :arity, 1}
 replace {C3.operator03, :cfn, cfn Tag "tag_bnot" (Tag, Result)}
 replace {C3.operator03, :operator_precedence, 2}
 replace {C3.operator03, :operator_associativity, :right}
 add {C3, :operator, C3.operator04}
 replace {C3.operator04, :is_a, :operator}
-replace {C3.operator04, :symbol, -}
+replace {C3.operator04, :symbol, :-}
 replace {C3.operator04, :arity, 1}
 replace {C3.operator04, :cfn, cfn Tag "tag_neg" (Tag, Result)}
 replace {C3.operator04, :operator_precedence, 2}
 replace {C3.operator04, :operator_associativity, :right}
 add {C3, :operator, C3.operator05}
 replace {C3.operator05, :is_a, :operator}
-replace {C3.operator05, :symbol, *}
+replace {C3.operator05, :symbol, :*}
 replace {C3.operator05, :arity, 2}
 replace {C3.operator05, :cfn, cfn Tag "tag_mul" (Tag, Tag, Result)}
 replace {C3.operator05, :operator_precedence, 3}
 replace {C3.operator05, :operator_associativity, :left}
 add {C3, :operator, C3.operator06}
 replace {C3.operator06, :is_a, :operator}
-replace {C3.operator06, :symbol, /}
+replace {C3.operator06, :symbol, :/}
 replace {C3.operator06, :arity, 2}
 replace {C3.operator06, :cfn, cfn Tag "tag_div" (Tag, Tag, Result)}
 replace {C3.operator06, :operator_precedence, 3}
 replace {C3.operator06, :operator_associativity, :left}
 add {C3, :operator, C3.operator07}
 replace {C3.operator07, :is_a, :operator}
-replace {C3.operator07, :symbol, mod}
+replace {C3.operator07, :symbol, :mod}
 replace {C3.operator07, :arity, 2}
 replace {C3.operator07, :cfn, cfn Tag "tag_mod" (Tag, Tag, Result)}
 replace {C3.operator07, :operator_precedence, 3}
 replace {C3.operator07, :operator_associativity, :left}
 add {C3, :operator, C3.operator08}
 replace {C3.operator08, :is_a, :operator}
-replace {C3.operator08, :symbol, +}
+replace {C3.operator08, :symbol, :+}
 replace {C3.operator08, :arity, 2}
 replace {C3.operator08, :cfn, cfn Tag "tag_add" (Tag, Tag, Result)}
 replace {C3.operator08, :operator_precedence, 4}
 replace {C3.operator08, :operator_associativity, :left}
 add {C3, :operator, C3.operator09}
 replace {C3.operator09, :is_a, :operator}
-replace {C3.operator09, :symbol, -}
+replace {C3.operator09, :symbol, :-}
 replace {C3.operator09, :arity, 2}
 replace {C3.operator09, :cfn, cfn Tag "tag_sub" (Tag, Tag, Result)}
 replace {C3.operator09, :operator_precedence, 4}
 replace {C3.operator09, :operator_associativity, :left}
 add {C3, :operator, C3.operator10}
 replace {C3.operator10, :is_a, :operator}
-replace {C3.operator10, :symbol, <<}
+replace {C3.operator10, :symbol, :<<}
 replace {C3.operator10, :arity, 2}
 replace {C3.operator10, :cfn, cfn Tag "tag_shift_left" (Tag, Tag, Result)}
 replace {C3.operator10, :operator_precedence, 5}
 replace {C3.operator10, :operator_associativity, :left}
 add {C3, :operator, C3.operator11}
 replace {C3.operator11, :is_a, :operator}
-replace {C3.operator11, :symbol, >>}
+replace {C3.operator11, :symbol, :>>}
 replace {C3.operator11, :arity, 2}
 replace {C3.operator11, :cfn, cfn Tag "tag_shift_right" (Tag, Tag, Result)}
 replace {C3.operator11, :operator_precedence, 5}
 replace {C3.operator11, :operator_associativity, :left}
 add {C3, :operator, C3.operator12}
 replace {C3.operator12, :is_a, :operator}
-replace {C3.operator12, :symbol, <}
+replace {C3.operator12, :symbol, :<}
 replace {C3.operator12, :arity, 2}
 replace {C3.operator12, :cfn, cfn :bool "tag_lt" (Tag, Tag)}
 replace {C3.operator12, :operator_precedence, 6}
 replace {C3.operator12, :operator_associativity, :left}
 add {C3, :operator, C3.operator13}
-replace {C3.operator13, :symbol, <=}
+replace {C3.operator13, :symbol, :<=}
 replace {C3.operator13, :is_a, :operator}
 replace {C3.operator13, :arity, 2}
 replace {C3.operator13, :cfn, cfn :bool "tag_lte" (Tag, Tag)}
 replace {C3.operator13, :operator_precedence, 6}
 replace {C3.operator13, :operator_associativity, :left}
 add {C3, :operator, C3.operator14}
-replace {C3.operator14, :symbol, >}
+replace {C3.operator14, :symbol, :>}
 replace {C3.operator14, :is_a, :operator}
 replace {C3.operator14, :arity, 2}
 replace {C3.operator14, :cfn, cfn :bool "tag_gt" (Tag, Tag)}
 replace {C3.operator14, :operator_precedence, 6}
 replace {C3.operator14, :operator_associativity, :left}
 add {C3, :operator, C3.operator15}
-replace {C3.operator15, :symbol, >=}
+replace {C3.operator15, :symbol, :>=}
 replace {C3.operator15, :is_a, :operator}
 replace {C3.operator15, :arity, 2}
 replace {C3.operator15, :cfn, cfn :bool "tag_gte" (Tag, Tag)}
@@ -115,49 +115,49 @@ replace {C3.operator15, :operator_precedence, 6}
 replace {C3.operator15, :operator_associativity, :left}
 add {C3, :operator, C3.operator16}
 replace {C3.operator16, :is_a, :operator}
-replace {C3.operator16, :symbol, ==}
+replace {C3.operator16, :symbol, :==}
 replace {C3.operator16, :arity, 2}
 replace {C3.operator16, :cfn, cfn :bool "tag_eq" (Tag, Tag)}
 replace {C3.operator16, :operator_precedence, 7}
 replace {C3.operator16, :operator_associativity, :left}
 add {C3, :operator, C3.operator17}
 replace {C3.operator17, :is_a, :operator}
-replace {C3.operator17, :symbol, !=}
+replace {C3.operator17, :symbol, :!=}
 replace {C3.operator17, :arity, 2}
 replace {C3.operator17, :cfn, cfn :bool "tag_not_eq" (Tag, Tag)}
 replace {C3.operator17, :operator_precedence, 7}
 replace {C3.operator17, :operator_associativity, :left}
 add {C3, :operator, C3.operator18}
 replace {C3.operator18, :is_a, :operator}
-replace {C3.operator18, :symbol, &}
+replace {C3.operator18, :symbol, :&}
 replace {C3.operator18, :arity, 2}
 replace {C3.operator18, :cfn, cfn :bool "tag_band" (Tag, Tag)}
 replace {C3.operator18, :operator_precedence, 8}
 replace {C3.operator18, :operator_associativity, :left}
 add {C3, :operator, C3.operator19}
 replace {C3.operator19, :is_a, :operator}
-replace {C3.operator19, :symbol, ^}
+replace {C3.operator19, :symbol, :^}
 replace {C3.operator19, :arity, 2}
 replace {C3.operator19, :cfn, cfn :bool "tag_bxor" (Tag, Tag)}
 replace {C3.operator19, :operator_precedence, 9}
 replace {C3.operator19, :operator_associativity, :left}
 add {C3, :operator, C3.operator20}
 replace {C3.operator20, :is_a, :operator}
-replace {C3.operator20, :symbol, |}
+replace {C3.operator20, :symbol, :|}
 replace {C3.operator20, :arity, 2}
 replace {C3.operator20, :cfn, cfn :bool "tag_bor" (Tag, Tag)}
 replace {C3.operator20, :operator_precedence, 10}
 replace {C3.operator20, :operator_associativity, :left}
 add {C3, :operator, C3.operator21}
 replace {C3.operator21, :is_a, :operator}
-replace {C3.operator21, :symbol, &&}
+replace {C3.operator21, :symbol, :&&}
 replace {C3.operator21, :arity, 2}
 replace {C3.operator21, :cfn, cfn :bool "tag_and" (Tag, Tag)}
 replace {C3.operator21, :operator_precedence, 11}
 replace {C3.operator21, :operator_associativity, :left}
 add {C3, :operator, C3.operator22}
 replace {C3.operator22, :is_a, :operator}
-replace {C3.operator22, :symbol, ||}
+replace {C3.operator22, :symbol, :||}
 replace {C3.operator22, :arity, 2}
 replace {C3.operator22, :cfn, cfn :bool "tag_and" (Tag, Tag)}
 replace {C3.operator22, :operator_precedence, 12}
@@ -165,7 +165,7 @@ replace {C3.operator22, :operator_associativity, :left}
 add {C3, :operator, C3.operator23}
 replace {C3.operator23, :is_a, :operator}
 add {C3.operator23, :is_a, :special_operator}
-replace {C3.operator23, :symbol, =}
+replace {C3.operator23, :symbol, :=}
 replace {C3.operator23, :arity, 2}
 replace {C3.operator23, :cfn, cfn Tag "tag_equal" (Tag, Tag, Result)}
 replace {C3.operator23, :operator_precedence, 13}
diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 09b49d6..1d4f9aa 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -723,7 +723,7 @@ sw buf_inspect_fact_size (const s_fact *fact)
   return result;
 }
 
-sw buf_inspect_fact_spec (s_buf *buf, p_facts_spec spec)
+sw buf_inspect_facts_spec (s_buf *buf, p_facts_spec spec)
 {
   sw r;
   sw result = 0;
@@ -743,6 +743,12 @@ sw buf_inspect_fact_spec (s_buf *buf, p_facts_spec spec)
         return r;
       result += r;
     }
+    if ((r = buf_write_1(buf, ", ")) < 0)
+      return r;
+    result += r;
+    if ((r = buf_inspect_tag(buf, spec[1])) < 0)
+      return r;
+    result += r;
     if ((r = buf_write_1(buf, "}")) < 0)
       return r;
     result += r;
diff --git a/libc3/buf_inspect.h b/libc3/buf_inspect.h
index 87abb32..846a103 100644
--- a/libc3/buf_inspect.h
+++ b/libc3/buf_inspect.h
@@ -78,7 +78,7 @@ sw buf_inspect_f64 (s_buf *buf, const f64 *x);
 sw buf_inspect_f64_size (const f64 *x);
 sw buf_inspect_fact (s_buf *buf, const s_fact *fact);
 sw buf_inspect_fact_size (const s_fact *fact);
-sw buf_inspect_fact_spec (s_buf *buf, p_facts_spec spec);
+sw buf_inspect_facts_spec (s_buf *buf, p_facts_spec spec);
 sw buf_inspect_fn (s_buf *buf, const s_fn *fn);
 sw buf_inspect_fn_algo (s_buf *buf, const s_list *algo);
 sw buf_inspect_fn_algo_size (const s_list *algo);
diff --git a/libc3/env.c b/libc3/env.c
index ec8d1e9..fcf9c82 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -791,6 +791,7 @@ s_ident * env_operator_call_ident (s_env *env, const s_ident *op,
 bool env_operator_find (s_env *env, const s_ident *op, u8 arity)
 {
   s_facts_with_cursor cursor;
+  p_facts_spec spec;
   s_tag tag_arity;
   s_tag tag_arity_u8;
   s_tag tag_is_a;
@@ -814,22 +815,23 @@ bool env_operator_find (s_env *env, const s_ident *op, u8 arity)
   tag_init_var(&tag_operator_var);
   tag_init_1(  &tag_symbol, ":symbol");
   tag_init_sym(&tag_sym, tmp.sym);
-  facts_with(&env->facts, &cursor, (t_facts_spec) {
-      &tag_module_name, &tag_is_a, &tag_module,
-      &tag_operator, &tag_operator_var, NULL,
-      &tag_operator_var, &tag_is_a, &tag_operator,
-      &tag_symbol, &tag_sym,
-      &tag_arity, &tag_arity_u8,
-      NULL, NULL });
+  spec = (p_facts_spec) & (t_facts_spec) {
+    &tag_module_name, &tag_is_a, &tag_module,
+    &tag_operator, &tag_operator_var, NULL,
+    &tag_operator_var, &tag_is_a, &tag_operator,
+    &tag_symbol, &tag_sym,
+    &tag_arity, &tag_arity_u8,
+    NULL, NULL };
+  facts_with(&env->facts, &cursor, spec);
   if (facts_with_cursor_next(&cursor)) {
     facts_with_cursor_clean(&cursor);
     return true;
   }
-  if (false)
-    warnx("operator %s not found in module %s",
-          tmp.sym->str.ptr.ps8,
-          tmp.module->str.ptr.ps8);
-  facts_with_cursor_clean(&cursor);
+#ifdef DEBUG_OPERATOR_FIND
+  warnx("env_operator_find: operator %s not found in module %s",
+        tmp.sym->str.ptr.ps8,
+        tmp.module->str.ptr.ps8);
+#endif
   return false;
 }
 
@@ -875,9 +877,10 @@ bool env_operator_is_right_associative (s_env *env, const s_ident *op)
     facts_with_cursor_clean(&cursor);
     return false;
   }
-  warnx("operator %s not found in module %s",
-        op->sym->str.ptr.ps8,
-        op->module->str.ptr.ps8);
+  warnx("env_operator_is_right_associative: "
+        "operator %s not found in module %s",
+        tmp.sym->str.ptr.ps8,
+        tmp.module->str.ptr.ps8);
   facts_with_cursor_clean(&cursor);
   return false;
 }
@@ -920,6 +923,11 @@ s8 env_operator_precedence (s_env *env, const s_ident *op)
       tag_operator_precedence_var.type == TAG_U8) {
     r = tag_operator_precedence_var.data.u8;
   }
+  else
+    warnx("env_operator_precedence: "
+          "operator %s not found in module %s",
+          tmp.sym->str.ptr.ps8,
+          tmp.module->str.ptr.ps8);
   facts_with_cursor_clean(&cursor);
   return r;
 }
diff --git a/libc3/facts.c b/libc3/facts.c
index 115d5a8..1916662 100644
--- a/libc3/facts.c
+++ b/libc3/facts.c
@@ -76,6 +76,7 @@ s_fact * facts_add_tags (s_facts *facts, const s_tag *subject,
 
 void facts_clean (s_facts *facts)
 {
+  facts_dump_file(facts, "debug.facts");
   if (facts->log)
     facts_close(facts);
   skiplist_delete__fact(facts->index_osp);
diff --git a/libc3/facts_with.c b/libc3/facts_with.c
index a7c163e..30c50eb 100644
--- a/libc3/facts_with.c
+++ b/libc3/facts_with.c
@@ -13,6 +13,9 @@
 #include <assert.h>
 #include <err.h>
 #include <stdlib.h>
+#include "buf.h"
+#include "buf_inspect.h"
+#include "env.h"
 #include "facts_cursor.h"
 #include "facts_spec.h"
 #include "facts_with.h"
@@ -33,6 +36,13 @@ s_facts_with_cursor * facts_with (s_facts *facts,
     cursor->levels = calloc(facts_count,
                             sizeof(s_facts_with_cursor_level));
     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); */
   }
   else {
diff --git a/libc3/facts_with_cursor.c b/libc3/facts_with_cursor.c
index 24abd7c..38c9b4c 100644
--- a/libc3/facts_with_cursor.c
+++ b/libc3/facts_with_cursor.c
@@ -51,9 +51,9 @@ s_fact * facts_with_cursor_next (s_facts_with_cursor *cursor)
     level = &cursor->levels[cursor->facts_count - 1];
 #ifdef DEBUG_FACTS
     buf_write_1(&g_c3_env.err, "[debug] cursor->level=");
-    buf_inspect_u64(&g_c3_env.err, cursor->level);
+    buf_inspect_uw(&g_c3_env.err, &cursor->level);
     buf_write_1(&g_c3_env.err, " level->spec=");
-    buf_inspect_fact_spec(&g_c3_env.err, level->spec);
+    buf_inspect_facts_spec(&g_c3_env.err, level->spec);
     buf_write_1(&g_c3_env.err, " ");
     buf_inspect_fact(&g_c3_env.err, level->fact);
 #endif
@@ -92,9 +92,9 @@ s_fact * facts_with_cursor_next (s_facts_with_cursor *cursor)
     }
 #ifdef DEBUG_FACTS
     buf_write_1(&g_c3_env.err, "[debug] cursor->level=");
-    buf_inspect_u64(&g_c3_env.err, cursor->level);
+    buf_inspect_uw(&g_c3_env.err, &cursor->level);
     buf_write_1(&g_c3_env.err, " level->spec=");
-    buf_inspect_fact_spec(&g_c3_env.err, level->spec);
+    buf_inspect_facts_spec(&g_c3_env.err, level->spec);
     buf_write_1(&g_c3_env.err, " ");
     buf_inspect_fact(&g_c3_env.err, level->fact);
 #endif
diff --git a/libc3/sym.c b/libc3/sym.c
index f5adfde..b225a80 100644
--- a/libc3/sym.c
+++ b/libc3/sym.c
@@ -318,9 +318,7 @@ bool sym_to_tag_type (const s_sym *sym, e_tag_type *dest)
     *dest = TAG_TUPLE;
     return true;
   }
-  /*
+  warnx("sym_to_tag_type: unknown type: %s", sym->str.ptr.ps8);
   assert(! "sym_to_tag_type: unknown type");
-  errx(1, "sym_to_tag_type: unknown type: %s", sym->str.ptr.ps8);
-  */
   return false;
 }
diff --git a/libc3/tag.c b/libc3/tag.c
index dda268f..4294e50 100644
--- a/libc3/tag.c
+++ b/libc3/tag.c
@@ -4784,8 +4784,7 @@ bool tag_is_number (const s_tag *tag)
 
 bool tag_is_unbound_var (const s_tag *tag)
 {
-  return (tag &&
-          tag->type == TAG_VAR);
+  return (tag && tag->type == TAG_VAR);
 }
 
 s_tag * tag_list (s_tag *tag, s_list *x)
diff --git a/libc3/types.h b/libc3/types.h
index 9cabd76..2fadc4b 100644
--- a/libc3/types.h
+++ b/libc3/types.h
@@ -77,7 +77,6 @@ typedef enum {
   TAG_F64,
   TAG_FACT,
   TAG_FN,
-  TAG_IDENT,
   TAG_INTEGER,
   TAG_SW,
   TAG_S64,
@@ -95,7 +94,8 @@ typedef enum {
   TAG_STR,
   TAG_SYM,
   TAG_TUPLE,
-  TAG_VAR
+  TAG_VAR,
+  TAG_IDENT
 } e_tag_type;
 
 /* structs */
@@ -506,9 +506,9 @@ struct facts_with_cursor_level {
 struct facts_with_cursor {
   s_facts *facts;
   s_binding *bindings;
-  size_t facts_count;
+  uw facts_count;
   s_facts_with_cursor_level *levels;
-  size_t level;
+  uw level;
   p_facts_spec spec;
   pthread_mutex_t mutex;
 };