Commit 043b0d8b70461f69758221946fa35d7632c1a006

Thomas de Grivel 2023-03-15T10:26:19

cfn_link

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
diff --git a/lib/c3/0.1/c3.facts b/lib/c3/0.1/c3.facts
index f9c992b..3231808 100644
--- a/lib/c3/0.1/c3.facts
+++ b/lib/c3/0.1/c3.facts
@@ -9,19 +9,19 @@
 {C3, :symbol, C3.*}
 {C3, :symbol, C3./}
 {C3, :symbol, C3.first}
-{C3.+, :cfn, cfn :tag "tag_add" (:tag, :tag)}
+{C3.+, :cfn, cfn :tag "tag_add" (:tag, :tag, :tag)}
 {C3.+, :is_a, :operator}
 {C3.+, :operator_precedence, 1}
 {C3.+, :operator_associativity, :left}
-{C3.-, :cfn, cfn :tag "tag_sub" (:tag, :tag)}
+{C3.-, :cfn, cfn :tag "tag_sub" (:tag, :tag, :tag)}
 {C3.-, :is_a, :operator}
 {C3.-, :operator_precedence, 1}
 {C3.-, :operator_associativity, :left}
-{C3.*, :cfn, cfn :tag "tag_mul" (:tag, :tag)}
+{C3.*, :cfn, cfn :tag "tag_mul" (:tag, :tag, :tag)}
 {C3.*, :is_a, :operator}
 {C3.*, :operator_precedence, 2}
 {C3.*, :operator_associativity, :left}
-{C3./, :cfn, cfn :tag "tag_div" (:tag, :tag)}
+{C3./, :cfn, cfn :tag "tag_div" (:tag, :tag, :tag)}
 {C3./, :is_a, :operator}
 {C3./, :operator_precedence, 2}
 {C3./, :operator_associativity, :left}
diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index 05d9360..197ee95 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -317,6 +317,7 @@ sw buf_parse_cfn (s_buf *buf, s_cfn *dest)
     goto restore;
   result += r;
   tmp.arity = list_length(tmp.arg_types);
+  cfn_link(&tmp);
   *dest = tmp;
   r = result;
   goto clean;
diff --git a/libc3/cfn.c b/libc3/cfn.c
index 4780568..d26c9aa 100644
--- a/libc3/cfn.c
+++ b/libc3/cfn.c
@@ -12,6 +12,7 @@
  * THIS SOFTWARE.
  */
 #include <assert.h>
+#include <dlfcn.h>
 #include <err.h>
 #include <stdlib.h>
 #include <string.h>
@@ -87,6 +88,14 @@ s_cfn * cfn_init (s_cfn *cfn)
   return cfn;
 }
 
+s_cfn * cfn_link (s_cfn *cfn)
+{
+  assert(cfn);
+  if (! (cfn->p = (void (*) (void)) dlsym(RTLD_DEFAULT, cfn->name.ptr.ps8)))
+    errx(1, "cfn_link: %s: %s", cfn->name.ptr.ps8, dlerror());
+  return cfn;
+}
+
 s_cfn * cfn_set_type (s_cfn *cfn, s_list *arg_type,
                       const s_sym *result_type)
 {
diff --git a/libc3/cfn.h b/libc3/cfn.h
index 2ddf28a..3aa3f7e 100644
--- a/libc3/cfn.h
+++ b/libc3/cfn.h
@@ -24,4 +24,7 @@ void    cfn_clean (s_cfn *cfn);
 s_tag * cfn_apply (s_cfn *cfn, s_list *args, s_tag *dest);
 s_cfn * cfn_copy (const s_cfn *cfn, s_cfn *dest);
 
+/* modifiers */
+s_cfn * cfn_link (s_cfn *cfn);
+
 #endif /* CFN_H */
diff --git a/libc3/tag.c b/libc3/tag.c
index 57c8045..9300d1d 100644
--- a/libc3/tag.c
+++ b/libc3/tag.c
@@ -27,6 +27,271 @@ s_tag * tag_1 (s_tag *tag, const s8 *p)
   return tag_init_1(tag, p);
 }
 
+s_tag * tag_add (const s_tag *a, const s_tag *b, s_tag *dest)
+{
+  assert(a);
+  assert(b);
+  assert(dest);
+  switch (a->type.type) {
+  case TAG_F32:
+    switch (b->type.type) {
+    case TAG_F32:
+      return tag_init_f32(dest, a->data.f32 + b->data.f32);
+    case TAG_F64:
+      return tag_init_f64(dest, a->data.f32 + b->data.f64);
+    case TAG_S8:
+      return tag_init_f32(dest, a->data.f32 + b->data.s8);
+    case TAG_S16:
+      return tag_init_f32(dest, a->data.f32 + b->data.s16);
+    case TAG_S32:
+      return tag_init_f32(dest, a->data.f32 + b->data.s32);
+    case TAG_S64:
+      return tag_init_f32(dest, a->data.f32 + b->data.s64);
+    case TAG_U8:
+      return tag_init_f32(dest, a->data.f32 + b->data.u8);
+    case TAG_U16:
+      return tag_init_f32(dest, a->data.f32 + b->data.u16);
+    case TAG_U32:
+      return tag_init_f32(dest, a->data.f32 + b->data.u32);
+    case TAG_U64:
+      return tag_init_f32(dest, a->data.f32 + b->data.u64);
+    default:
+      goto ko;
+  }
+  case TAG_F64:
+    switch (b->type.type) {
+    case TAG_F32:
+      return tag_init_f64(dest, a->data.f64 + b->data.f32);
+    case TAG_F64:
+      return tag_init_f64(dest, a->data.f64 + b->data.f64);
+    case TAG_S8:
+      return tag_init_f64(dest, a->data.f64 + b->data.s8);
+    case TAG_S16:
+      return tag_init_f64(dest, a->data.f64 + b->data.s16);
+    case TAG_S32:
+      return tag_init_f64(dest, a->data.f64 + b->data.s32);
+    case TAG_S64:
+      return tag_init_f64(dest, a->data.f64 + b->data.s64);
+    case TAG_U8:
+      return tag_init_f64(dest, a->data.f64 + b->data.u8);
+    case TAG_U16:
+      return tag_init_f64(dest, a->data.f64 + b->data.u16);
+    case TAG_U32:
+      return tag_init_f64(dest, a->data.f64 + b->data.u32);
+    case TAG_U64:
+      return tag_init_f64(dest, a->data.f64 + b->data.u64);
+    default:
+      goto ko;
+  }
+  case TAG_S8:
+    switch (b->type.type) {
+    case TAG_F32:
+      return tag_init_f32(dest, a->data.s8 + b->data.f32);
+    case TAG_F64:
+      return tag_init_f64(dest, a->data.s8 + b->data.f64);
+    case TAG_S8:
+      return tag_init_s8(dest, a->data.s8 + b->data.s8);
+    case TAG_S16:
+      return tag_init_s16(dest, a->data.s8 + b->data.s16);
+    case TAG_S32:
+      return tag_init_s32(dest, a->data.s8 + b->data.s32);
+    case TAG_S64:
+      return tag_init_s64(dest, a->data.s8 + b->data.s64);
+    case TAG_U8:
+      return tag_init_s8(dest, a->data.s8 + b->data.u8);
+    case TAG_U16:
+      return tag_init_s16(dest, a->data.s8 + b->data.u16);
+    case TAG_U32:
+      return tag_init_s32(dest, a->data.s8 + b->data.u32);
+    case TAG_U64:
+      return tag_init_s64(dest, a->data.s8 + b->data.u64);
+    default:
+      goto ko;
+  }
+  case TAG_S16:
+    switch (b->type.type) {
+    case TAG_F32:
+      return tag_init_f32(dest, a->data.s16 + b->data.f32);
+    case TAG_F64:
+      return tag_init_f64(dest, a->data.s16 + b->data.f64);
+    case TAG_S8:
+      return tag_init_s16(dest, a->data.s16 + b->data.s8);
+    case TAG_S16:
+      return tag_init_s16(dest, a->data.s16 + b->data.s16);
+    case TAG_S32:
+      return tag_init_s32(dest, a->data.s16 + b->data.s32);
+    case TAG_S64:
+      return tag_init_s64(dest, a->data.s16 + b->data.s64);
+    case TAG_U8:
+      return tag_init_s16(dest, a->data.s16 + b->data.u8);
+    case TAG_U16:
+      return tag_init_s16(dest, a->data.s16 + b->data.u16);
+    case TAG_U32:
+      return tag_init_s32(dest, a->data.s16 + b->data.u32);
+    case TAG_U64:
+      return tag_init_s64(dest, a->data.s16 + b->data.u64);
+    default:
+      goto ko;
+  }
+  case TAG_S32:
+    switch (b->type.type) {
+    case TAG_F32:
+      return tag_init_f32(dest, a->data.s32 + b->data.f32);
+    case TAG_F64:
+      return tag_init_f64(dest, a->data.s32 + b->data.f64);
+    case TAG_S8:
+      return tag_init_s32(dest, a->data.s32 + b->data.s8);
+    case TAG_S16:
+      return tag_init_s32(dest, a->data.s32 + b->data.s16);
+    case TAG_S32:
+      return tag_init_s32(dest, a->data.s32 + b->data.s32);
+    case TAG_S64:
+      return tag_init_s64(dest, a->data.s32 + b->data.s64);
+    case TAG_U8:
+      return tag_init_s32(dest, a->data.s32 + b->data.u8);
+    case TAG_U16:
+      return tag_init_s32(dest, a->data.s32 + b->data.u16);
+    case TAG_U32:
+      return tag_init_s32(dest, a->data.s32 + b->data.u32);
+    case TAG_U64:
+      return tag_init_s64(dest, a->data.s32 + b->data.u64);
+    default:
+      goto ko;
+  }
+  case TAG_S64:
+    switch (b->type.type) {
+    case TAG_F32:
+      return tag_init_f32(dest, a->data.s64 + b->data.f32);
+    case TAG_F64:
+      return tag_init_f64(dest, a->data.s64 + b->data.f64);
+    case TAG_S8:
+      return tag_init_s32(dest, a->data.s64 + b->data.s8);
+    case TAG_S16:
+      return tag_init_s32(dest, a->data.s64 + b->data.s16);
+    case TAG_S32:
+      return tag_init_s32(dest, a->data.s64 + b->data.s32);
+    case TAG_S64:
+      return tag_init_s64(dest, a->data.s64 + b->data.s64);
+    case TAG_U8:
+      return tag_init_s32(dest, a->data.s64 + b->data.u8);
+    case TAG_U16:
+      return tag_init_s32(dest, a->data.s64 + b->data.u16);
+    case TAG_U32:
+      return tag_init_s32(dest, a->data.s64 + b->data.u32);
+    case TAG_U64:
+      return tag_init_s64(dest, a->data.s64 + b->data.u64);
+    default:
+      goto ko;
+  }
+  case TAG_U8:
+    switch (b->type.type) {
+    case TAG_F32:
+      return tag_init_f32(dest, a->data.u8 + b->data.f32);
+    case TAG_F64:
+      return tag_init_f64(dest, a->data.u8 + b->data.f64);
+    case TAG_S8:
+      return tag_init_s8(dest, a->data.u8 + b->data.s8);
+    case TAG_S16:
+      return tag_init_s16(dest, a->data.u8 + b->data.s16);
+    case TAG_S32:
+      return tag_init_s32(dest, a->data.u8 + b->data.s32);
+    case TAG_S64:
+      return tag_init_s64(dest, a->data.u8 + b->data.s64);
+    case TAG_U8:
+      return tag_init_u8(dest, a->data.u8 + b->data.u8);
+    case TAG_U16:
+      return tag_init_u16(dest, a->data.u8 + b->data.u16);
+    case TAG_U32:
+      return tag_init_u32(dest, a->data.u8 + b->data.u32);
+    case TAG_U64:
+      return tag_init_u64(dest, a->data.u8 + b->data.u64);
+    default:
+      goto ko;
+  }
+  case TAG_U16:
+    switch (b->type.type) {
+    case TAG_F32:
+      return tag_init_f32(dest, a->data.u16 + b->data.f32);
+    case TAG_F64:
+      return tag_init_f64(dest, a->data.u16 + b->data.f64);
+    case TAG_S8:
+      return tag_init_s16(dest, a->data.u16 + b->data.s8);
+    case TAG_S16:
+      return tag_init_s16(dest, a->data.u16 + b->data.s16);
+    case TAG_S32:
+      return tag_init_s32(dest, a->data.u16 + b->data.s32);
+    case TAG_S64:
+      return tag_init_s64(dest, a->data.u16 + b->data.s64);
+    case TAG_U8:
+      return tag_init_u16(dest, a->data.u16 + b->data.u8);
+    case TAG_U16:
+      return tag_init_u16(dest, a->data.u16 + b->data.u16);
+    case TAG_U32:
+      return tag_init_u32(dest, a->data.u16 + b->data.u32);
+    case TAG_U64:
+      return tag_init_u64(dest, a->data.u16 + b->data.u64);
+    default:
+      goto ko;
+  }
+  case TAG_U32:
+    switch (b->type.type) {
+    case TAG_F32:
+      return tag_init_f32(dest, a->data.u32 + b->data.f32);
+    case TAG_F64:
+      return tag_init_f64(dest, a->data.u32 + b->data.f64);
+    case TAG_S8:
+      return tag_init_s32(dest, a->data.u32 + b->data.s8);
+    case TAG_S16:
+      return tag_init_s32(dest, a->data.u32 + b->data.s16);
+    case TAG_S32:
+      return tag_init_s32(dest, a->data.u32 + b->data.s32);
+    case TAG_S64:
+      return tag_init_s64(dest, a->data.u32 + b->data.s64);
+    case TAG_U8:
+      return tag_init_u32(dest, a->data.u32 + b->data.u8);
+    case TAG_U16:
+      return tag_init_u32(dest, a->data.u32 + b->data.u16);
+    case TAG_U32:
+      return tag_init_u32(dest, a->data.u32 + b->data.u32);
+    case TAG_U64:
+      return tag_init_u64(dest, a->data.u32 + b->data.u64);
+    default:
+      goto ko;
+  }
+  case TAG_U64:
+    switch (b->type.type) {
+    case TAG_F32:
+      return tag_init_f32(dest, a->data.u64 + b->data.f32);
+    case TAG_F64:
+      return tag_init_f64(dest, a->data.u64 + b->data.f64);
+    case TAG_S8:
+      return tag_init_s64(dest, a->data.u64 + b->data.s8);
+    case TAG_S16:
+      return tag_init_s64(dest, a->data.u64 + b->data.s16);
+    case TAG_S32:
+      return tag_init_s64(dest, a->data.u64 + b->data.s32);
+    case TAG_S64:
+      return tag_init_s64(dest, a->data.u64 + b->data.s64);
+    case TAG_U8:
+      return tag_init_u64(dest, a->data.u64 + b->data.u8);
+    case TAG_U16:
+      return tag_init_u64(dest, a->data.u64 + b->data.u16);
+    case TAG_U32:
+      return tag_init_u64(dest, a->data.u64 + b->data.u32);
+    case TAG_U64:
+      return tag_init_u64(dest, a->data.u64 + b->data.u64);
+    default:
+      goto ko;
+    }
+  default:
+    goto ko;
+  }
+ ko:
+  errx(1, "cannot add %s to %s",
+       tag_type_to_string(a->type.type),
+       tag_type_to_string(b->type.type));
+}
+
 s_tag * tag_bool (s_tag *tag, bool b)
 {
   assert(tag);
@@ -759,6 +1024,39 @@ ffi_type tag_to_ffi_type(const s_tag *tag)
   return ffi_type_void;
 }
 
+s8 * tag_type_to_string (e_tag_type type)
+{
+  switch (type) {
+  case TAG_VOID: return "void";
+  case TAG_BOOL: return "bool";
+  case TAG_CALL: return "call";
+  case TAG_CALL_FN: return "call_fn";
+  case TAG_CALL_MACRO: return "call_macro";
+  case TAG_CFN: return "cfn";
+  case TAG_CHARACTER: return "character";
+  case TAG_F32: return "f32";
+  case TAG_F64: return "f64";
+  case TAG_FN: return "fn";
+  case TAG_IDENT: return "ident";
+  case TAG_INTEGER: return "integer";
+  case TAG_S64: return "s64";
+  case TAG_S32: return "s32";
+  case TAG_S16: return "s16";
+  case TAG_S8: return "s8";
+  case TAG_U8: return "u8";
+  case TAG_U16: return "u16";
+  case TAG_U32: return "u32";
+  case TAG_U64: return "u64";
+  case TAG_LIST: return "list";
+  case TAG_PTAG: return "ptag";
+  case TAG_QUOTE: return "quote";
+  case TAG_STR: return "str";
+  case TAG_SYM: return "sym";
+  case TAG_TUPLE: return "tuple";
+  case TAG_VAR: return "var";
+  }
+}
+
 s_tag * tag_u8 (s_tag *tag, u8 x)
 {
   assert(tag);
diff --git a/libc3/tag.h b/libc3/tag.h
index ed23870..e9502b1 100644
--- a/libc3/tag.h
+++ b/libc3/tag.h
@@ -97,6 +97,7 @@ 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);
 sw      tag_type_size (e_tag_type type);
+s8 *    tag_type_to_string (e_tag_type type);
 
 /* Modifiers */
 s_tag * tag_1 (s_tag *tag, const s8 *p);
@@ -136,4 +137,7 @@ s_tag * tag_var (s_tag *tag);
 s_tag * tag_void (s_tag *tag);
 ffi_type tag_to_ffi_type(const s_tag *tag);
 
+/* operators */
+s_tag * tag_add (const s_tag *a, const s_tag *b, s_tag *dest);
+
 #endif /* STR_H */