Commit fbfd58a3ebee3856ee7ed8d920814b3009a3a58e

Thomas de Grivel 2024-09-16T14:42:49

wip cow

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
diff --git a/.ikc3_history b/.ikc3_history
index 1db7ffa..ca49c99 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,17 +1,3 @@
-a = ? <- 1 ; 2
-a
-a = ? <- 1 ; 2
-a
-(a = ? <- 1) ; 2
-a
-a = ? <- 1 ; 2
-quote a = ? <- 1 ; 2
-to_lisp(quote a = ? <- 1 ; 2)
-operator(:";")
-KC3.Operator.find(:";")
-a = ?
-a
-a <- 1
 a
 a = 1
 a
@@ -97,3 +83,17 @@ Str.ftime(Time.now(), "%a, %b %d %G %T %Z")
 cow 1
 cow(1)
 cow 1
+cow 1 + 1
+quote cow 1 + 1
+a = cow 1 + 1
+a
+a + 1
+a <- 3
+a = cow 1 + 1
+a
+a = cow 1 + 1
+a
+a = cow 1 + 1
+a
+a = cow 1 + 1
+a
diff --git a/ikc3/.ikc3_history b/ikc3/.ikc3_history
index f76e1d9..ca49c99 100644
--- a/ikc3/.ikc3_history
+++ b/ikc3/.ikc3_history
@@ -1,100 +1,99 @@
-op = ?
-Facts.with(Facts.env_facts(), quote [[KC3, :operator, ^ op], [^ op, :symbol_value, value]], fn (fact) { puts(op); 1 })
-op = ?
-Facts.with(Facts.env_facts(), quote [[KC3, :operator, ^ op], [^ op, :symbol_value, value]], fn (fact) { puts(op); 1 })
-op = ?
-op <- 1
-op
-op <- ?
-Facts.with(Facts.env_facts(), quote [[KC3, :operator, ^ op], [^ op, :symbol_value, value]], fn (fact) { puts(op); 1 })
-Facts.with(Facts.env_facts(), quote [[KC3, :operator, op], [^ op, :symbol_value, value]], fn (fact) { puts(op); 1 })
-Facts.with(Facts.env_facts(), quote [[KC3, :operator, op], [op, :symbol_value, value]], fn (fact) { puts(op); 1 })
-Facts.with(Facts.env_facts(), quote [[KC3, :operator, unquote(op)], [unquote(op), :symbol_value, value]], fn (fact) { puts(op); 1 })
-op = ?
-Facts.with(Facts.env_facts(), quote [[KC3, :operator, unquote(op)], [unquote(op), :symbol_value, value]], fn (fact) { puts(op); 1 })
-op
-Facts.with(Facts.env_facts(), quote [[KC3, :operator, op = ?], [op, :symbol_value, value]], fn (fact) { puts(op); 1 })
-Facts.with(Facts.env_facts(), quote [[KC3, :operator, op], [^ op, :symbol_value, value]], fn (fact) { puts(op); 1 })
-Facts.with(Facts.env_facts(), quote [[KC3, :operator, op]], fn (fact) { puts(op); 1 })
-Facts.with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) { puts(fact.object); 1 })
-?
-a = ?
-?
-a
-a = ?
-a
-a = ?
-a
-a <- 1
-a
-(U8) (Ptr) a
-(Tag) (Ptr) a
-(Ptr) a
-(Tag) (Ptr) a
-a = ?
-a
-(Ptr) a
-a = ?
 a
-a <- 1
+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 })
+to_lisp(quote a = ? <- 1 ; 2)
 a = ?
-a <- void
-a
-%KC3.Operator{symbol_value: void}
-%KC3.Operator{symbol_value: 1}
-%KC3.Operator{symbol_value: void}
-1; 2; 3
-1; 2
-1 ; 2
-a = 1; 2
-a = ?; 2
-a
-a = ?; 2
-a
-a = 1; 2
-a
-a = ?
-b = (Var) 0x9ee46e3ef10
-? <- 1
-a = ? <- 1
-a
-a = ? <- 1 ; b = a
-b
-a
-a = ? <- 1 ; 2
-a
-a = ? <- 1 ; 2
-a
-(a = ? <- 1) ; 2
-a
-a = ? <- 1 ; 2
-a
-a = ? <- 1 ; 2
-a
-a = ? <- 1 ; 2
-a
-(a = ? <- 1) ; 2
-a
-a = ? <- 1 ; 2
+Facts.with_tags(Facts.env_facts(), KC3, :operator, a, fn (fact) { puts(a); 1 })
+to_lisp(quote a = ? <- 1 ; 2)
+to_lisp(quote a = 1 ; 2)
+to_lisp(quote a = ? <- 1 ; 2)
+q
+exit
+to_lisp(quote a = ? <- 1 ; 2)
+quote if true do if true do %KC3.Operator{} end end
+quote if true do if true do unquote(%KC3.Operator{}) end end
+quote if true do if true do %KC3.Operator{sym: :a} end end
+to_lisp(quote a = ? <- 1 ; 2)
+quote if true do if true do %KC3.Operator{symbol_value: void} end end
+quote if true do if true do %KC3.Operator{sym: :-, symbol_value: void} end end
+to_lisp(quote a = ? <- 1 ; 2)
 quote a = ? <- 1 ; 2
-KC3.Operator.find(:xxx)
-KC3.Operator.find(:+)
 quote 1 + 20 / 3 * 4 - 5
+quote 1 + 20 / 3 * 4
+quote 1 + 20 / 3 * 4 - 5
+1 + 1
+1 + 1000000000000000000000000000000000000000000000000000000
+(1 + 1000000000000000000000000000000000000000000000000000000) * 2
+sqrt(1 + 1000000000000000000000000000000000000000000000000000000)
+List.reverse([1, 2, 3, 4])
+List.reverse
+List.reverse([1, 2, 3, 4])
+List.map([1, 2, 3, 4], fn (x) { x * 2 })
+List.has?([1, 2, 3, 4], 1)
+List.has?([1, 2, 3, 4], 0)
+quote if true do if true do %KC3.Operator{} end end
+quote if true do if true do %KC3.Operator{sym: :-} end end
+quote if true do if true do %KC3.Operator{sym: :-, symbol_value: void} end end
+quote if true do if true do %KC3.Operator{sym: :-, symbol_value: void} else 0 end else 1 end
+a = ?
+b = ?
+a = ? ; void
+Facts.with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) { puts(fact.object); 1 })
+EKC3.html_escape("plop <1>")
+Facts.first_with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) { fact.object })
+first_operator = Facts.first_with_tags(Facts.env_facts(), KC3, :operator, ?, fn (fact) { fact.object })
+first_operator
+List.append([1, 2, 3], 4)
+HTTPd.routes
+a = :dir
+file = %{type: :dir}
+file.type
+file.type == :dir
+file = %{type: :dir, items = [%{type: file, items: []}]}
+file = %{type: :dir, items: [%{type: file, items: []}]}
+file = %{type: :dir, items: [%{type: :file, items: []}]}
+file = "plop.md"
+Str.starts_with?(file, ".") ||                                     
+(Sw) 4 > 0
+q
+List.sort([1, 2, 3, 4])
+List.sort([4, 3, 2, 1])
+List.sort(["1. a", "2. bc", "3. cde", "4. fghi"])
+List.sort(["1. abcd", "2. efg", "3. hi", "4. j"])
 
+1/3
+1/3 * 2
+2/6 * 2
+double = fn (x) { x * 2 }
+double(4)
+double(21)
+List.map([1, 2, 3, 4], double)
+List.reverse([1, 2, 3, 4])
+Time.now
+Time.now()
+Str.ftime(Time.now(), "%a")
+Str.ftim
+Str.ftime
+Str.ftime(Time.now(), "%a")
+Str.ftime(Time.now(), "%a, %d")
+Str.ftime(Time.now(), "%a, %G")
+Str.ftime(Time.now(), "%a, %m %d %G")
+Str.ftime(Time.now(), "%a, %b %d %G")
+Str.ftime(Time.now(), "%a, %b %d %G %T %Z")
+cow 1
+cow(1)
+cow 1
+cow 1 + 1
+quote cow 1 + 1
+a = cow 1 + 1
+a
+a + 1
+a <- 3
+a = cow 1 + 1
+a
+a = cow 1 + 1
+a
+a = cow 1 + 1
+a
+a = cow 1 + 1
+a
diff --git a/lib/kc3/0.1/kc3.facts b/lib/kc3/0.1/kc3.facts
index e5046ee..2afba6f 100644
--- a/lib/kc3/0.1/kc3.facts
+++ b/lib/kc3/0.1/kc3.facts
@@ -194,14 +194,14 @@ add {KC3, :operator, KC3.operator_assign}
 replace {KC3.operator_assign, :is_a, :operator}
 replace {KC3.operator_assign, :sym, :<-}
 replace {KC3.operator_assign, :arity, 2}
-replace {KC3.operator_assign, :symbol_value, cfn Tag "var_assign" (Var, Tag, Result)}
+replace {KC3.operator_assign, :symbol_value, cfn Tag "tag_assign" (Tag, Tag, Result)}
 replace {KC3.operator_assign, :operator_precedence, 3}
 replace {KC3.operator_assign, :operator_associativity, :left}
 add {KC3, :operator, KC3.operator_assign_2}
 replace {KC3.operator_assign_2, :is_a, :operator}
 replace {KC3.operator_assign_2, :sym, :←}
 replace {KC3.operator_assign_2, :arity, 2}
-replace {KC3.operator_assign_2, :symbol_value, cfn Tag "var_assign" (Var, Tag, Result)}
+replace {KC3.operator_assign_2, :symbol_value, cfn Tag "tag_assign" (Tag, Tag, Result)}
 replace {KC3.operator_assign_2, :operator_precedence, 3}
 replace {KC3.operator_assign_2, :operator_associativity, :left}
 add {KC3, :operator, KC3.operator_equal}
diff --git a/libkc3/buf_parse.c b/libkc3/buf_parse.c
index b708373..67fb444 100644
--- a/libkc3/buf_parse.c
+++ b/libkc3/buf_parse.c
@@ -1364,10 +1364,10 @@ sw buf_parse_cow (s_buf *buf, s_cow *cow)
       goto restore;
     result += r;
   }
-  if ((r = buf_read_1(buf, "cow ")) <= 0)
+  if ((r = buf_read_1(buf, "cow")) <= 0)
     goto restore;
   result += r;
-  if ((r = buf_ignore_spaces(buf)) < 0)
+  if ((r = buf_ignore_spaces(buf)) <= 0)
     goto restore;
   result += r;
   if (! cow_init(&tmp, type))
@@ -4112,6 +4112,7 @@ sw buf_parse_tag_primary_4 (s_buf *buf, s_tag *dest)
   switch (c) {
   case '(':
     if ((r = buf_parse_tag_var(buf, dest)) ||
+        (r = buf_parse_tag_cow(buf, dest)) ||
         (r = buf_parse_tag_number(buf, dest)) ||
         (r = buf_parse_tag_array(buf, dest)) ||
         (r = buf_parse_tag_cast(buf, dest)) ||
diff --git a/libkc3/cow.c b/libkc3/cow.c
index d8b8272..5e581a3 100644
--- a/libkc3/cow.c
+++ b/libkc3/cow.c
@@ -22,6 +22,17 @@
 #include "sym.h"
 #include "tag.h"
 
+s_tag * cow_assign (s_cow *cow, const s_tag *value, s_tag *dest)
+{
+  if (! tag_init_copy(cow_read_write(cow), value))
+    return NULL;
+  if (! cow_freeze(cow))
+    return NULL;
+  dest->type = TAG_COW;
+  dest->data.cow = cow;
+  return dest;
+}
+
 void cow_clean (s_cow *cow)
 {
   assert(cow);
diff --git a/libkc3/cow.h b/libkc3/cow.h
index b0baad6..914d3a4 100644
--- a/libkc3/cow.h
+++ b/libkc3/cow.h
@@ -31,7 +31,7 @@
 /* Stack-allocation compatible functions. Call cow_clean after use. */
 void    cow_clean (s_cow *cow);
 s_cow * cow_init (s_cow *cow, const s_sym *type);
-s_cow * cow_init_1 (s_cow *cow, const char *utf8);
+/* s_cow * cow_init_1 (s_cow *cow, const char *utf8); */
 s_cow * cow_init_cast (s_cow *cow, const s_sym * const *type,
                        const s_tag *tag);
 s_cow * cow_init_copy (s_cow *cow, const s_cow *src);
@@ -41,7 +41,7 @@ s_cow * cow_init_tag_copy (s_cow *cow, const s_sym *type,
 /* Heap-allocation functions. Call cow_delete after use. */
 void    cow_delete (s_cow *cow);
 s_cow * cow_new (const s_sym *type);
-s_cow * cow_new_1 (const char *utf8);
+/* s_cow * cow_new_1 (const char *utf8); */
 s_cow * cow_new_cast (const s_sym * const *type, const s_tag *tag);
 s_cow * cow_new_copy (const s_cow *src);
 s_cow * cow_new_tag_copy (const s_sym *type, const s_tag *src);
@@ -53,6 +53,7 @@ s_tag *       cow_read_write (s_cow *cow);
 const s_tag * cow_resolve (const s_cow *cow);
 
 /* Operators. */
+s_tag * cow_assign (s_cow *cow, const s_tag *value, s_tag *dest);
 s_cow * cow_freeze (s_cow *cow);
 s_cow * cow_freeze_copy (s_cow *cow, const s_tag *src);
 s_cow * cow_thaw (s_cow *cow);
diff --git a/libkc3/tag.c b/libkc3/tag.c
index e0059a7..d13300a 100644
--- a/libkc3/tag.c
+++ b/libkc3/tag.c
@@ -78,6 +78,49 @@ s8 tag_arity (const s_tag *tag)
   return -1;
 }
 
+s_tag * tag_assign (const s_tag *tag, const s_tag *value, s_tag *dest)
+{
+  assert(tag);
+  assert(value);
+  assert(dest);
+  switch (tag->type) {
+  case TAG_VAR:
+    return var_assign(&tag->data.var, value, dest);
+  case TAG_COW:
+    return cow_assign(tag->data.cow, value, dest);
+  default:
+    break;
+  }
+  err_write_1("tag_assign: cannot assign to ");
+  err_write_1(tag_type_to_string(tag->type));
+  err_write_1("\n");
+  assert(! "tag_assign: cannot assign to this tag type");
+  return NULL;
+}
+
+s_tag * tag_brackets (const s_tag *tag, const s_tag *address,
+                      s_tag *dest)
+{
+  assert(tag);
+  assert(address);
+  assert(dest);
+  switch (tag->type) {
+  case TAG_ARRAY:
+    switch (address->type) {
+    case TAG_ARRAY:
+      return array_data_tag(&tag->data.array, &address->data.array,
+                            dest);
+    default:
+      break;
+    }
+  default:
+    break;
+  }
+  err_puts("tag_brackets: invalid arguments");
+  assert(! "tag_brackets: invalid arguments");
+  return NULL;
+}
+
 s_tag * tag_cast_integer_to_s8 (s_tag *tag)
 {
   s8 i;
@@ -200,29 +243,6 @@ void tag_clean (s_tag *tag)
   }
 }
 
-s_tag * tag_brackets (const s_tag *tag, const s_tag *address,
-                      s_tag *dest)
-{
-  assert(tag);
-  assert(address);
-  assert(dest);
-  switch (tag->type) {
-  case TAG_ARRAY:
-    switch (address->type) {
-    case TAG_ARRAY:
-      return array_data_tag(&tag->data.array, &address->data.array,
-                            dest);
-    default:
-      break;
-    }
-  default:
-    break;
-  }
-  err_puts("tag_brackets: invalid arguments");
-  assert(! "tag_brackets: invalid arguments");
-  return NULL;
-}
-
 void tag_delete (s_tag *tag)
 {
   tag_clean(tag);
diff --git a/libkc3/tag.h b/libkc3/tag.h
index c18fcd7..aaaa71a 100644
--- a/libkc3/tag.h
+++ b/libkc3/tag.h
@@ -81,12 +81,14 @@ bool    tag_to_pointer (s_tag *tag, const s_sym *type, void **dest);
 /* KC3 operators. */
 s_tag * tag_add (const s_tag *a, const s_tag *b, s_tag *dest);
 bool *  tag_and (const s_tag *a, const s_tag *b, bool *dest);
+s_tag * tag_assign (const s_tag *tag, const s_tag *value, s_tag *dest);
 s_tag * tag_band (const s_tag *a, const s_tag *b, s_tag *dest);
 s_tag * tag_bnot (const s_tag *tag, s_tag *dest);
 s_tag * tag_bor (const s_tag *a, const s_tag *b, s_tag *dest);
 s_tag * tag_brackets (const s_tag *tag, const s_tag *address,
                       s_tag *dest);
 s_tag * tag_bxor (const s_tag *a, const s_tag *b, s_tag *dest);
+s_tag * tag_cow (const s_tag *value, s_tag *dest);
 s_tag * tag_div (const s_tag *a, const s_tag *b, s_tag *dest);
 bool *  tag_lt (const s_tag *a, const s_tag *b, bool *dest);
 bool *  tag_lte (const s_tag *a, const s_tag *b, bool *dest);
diff --git a/libkc3/var.c b/libkc3/var.c
index d742c96..d4ee907 100644
--- a/libkc3/var.c
+++ b/libkc3/var.c
@@ -19,7 +19,7 @@
 #include "tag.h"
 #include "var.h"
 
-s_tag * var_assign (s_var *var, const s_tag *value, s_tag *dest)
+s_tag * var_assign (const s_var *var, const s_tag *value, s_tag *dest)
 {
   assert(var);
   assert(value);
@@ -119,7 +119,7 @@ s_var * var_init_copy (s_var *var, const s_var *src)
   return var;
 }
 
-bool * var_is_unbound (s_var *var, bool *dest)
+bool * var_is_unbound (const s_var *var, bool *dest)
 {
   assert(var);
   assert(dest);
@@ -127,7 +127,7 @@ bool * var_is_unbound (s_var *var, bool *dest)
   return dest;
 }
 
-s_var * var_reset (s_var *var)
+const s_var * var_reset (const s_var *var)
 {
   s_tag tmp = {0};
   assert(var);
@@ -139,7 +139,7 @@ s_var * var_reset (s_var *var)
   return var;
 }
 
-s_var * var_set (s_var *var, const s_tag *value)
+const s_var * var_set (const s_var *var, const s_tag *value)
 {
   const s_sym *value_type;
   assert(var);
diff --git a/libkc3/var.h b/libkc3/var.h
index 95ff67e..2124582 100644
--- a/libkc3/var.h
+++ b/libkc3/var.h
@@ -23,10 +23,12 @@ s_var * var_init_cast (s_var *tag, const s_sym * const *type,
 s_var * var_init_copy (s_var *tag, const s_var *src);
 
 /* Observers. */
-bool * var_is_unbound (s_var *var, bool *dest);
+bool *  var_is_unbound (const s_var *var, bool *dest);
 
 /* Operators. */
-s_var * var_reset (s_var *var);
-s_var * var_set (s_var *var, const s_tag *value);
+s_tag *       var_assign (const s_var *var, const s_tag *value,
+                          s_tag *dest);
+const s_var * var_reset (const s_var *var);
+const s_var * var_set (const s_var *var, const s_tag *value);
 
 #endif /* LIBKC3_VAR_H */