Commit d602f366c09451f82b57805daad04936d116d759

Thomas de Grivel 2024-03-05T10:52:42

wip complex

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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
diff --git a/.ic3_history b/.ic3_history
index 8a760a8..bb0aca1 100644
--- a/.ic3_history
+++ b/.ic3_history
@@ -1,4 +1,3 @@
-name = quote n
 quote "Hello, " + (unquote name) + " !"
 m = macro (name) { quote "Hello, " + (unquote name) + " !" }
 n = "123"
@@ -97,3 +96,4 @@ sqrt(4)
 sqrt(9)
 sqrt(16)
 sqrt(-1)
+1 + 2 +i 3
diff --git a/lib/c3/0.1/c3.facts b/lib/c3/0.1/c3.facts
index 1e5e4b9..7a03ba2 100644
--- a/lib/c3/0.1/c3.facts
+++ b/lib/c3/0.1/c3.facts
@@ -71,6 +71,13 @@ replace {C3.operator_add, :arity, 2}
 replace {C3.operator_add, :cfn, cfn Tag "tag_add" (Tag, Tag, Result)}
 replace {C3.operator_add, :operator_precedence, 10}
 replace {C3.operator_add, :operator_associativity, :left}
+add {C3, :operator, C3.operator_addi}
+replace {C3.operator_addi, :is_a, :operator}
+replace {C3.operator_addi, :symbol, :+i}
+replace {C3.operator_addi, :arity, 2}
+replace {C3.operator_addi, :cfn, cfn Tag "tag_addi" (Tag, Tag, Result)}
+replace {C3.operator_addi, :operator_precedence, 10}
+replace {C3.operator_addi, :operator_associativity, :left}
 add {C3, :operator, C3.operator_sub}
 replace {C3.operator_sub, :is_a, :operator}
 replace {C3.operator_sub, :symbol, :-}
diff --git a/libc3/bool.c b/libc3/bool.c
index 4cc9f8e..f4c07b4 100644
--- a/libc3/bool.c
+++ b/libc3/bool.c
@@ -27,8 +27,8 @@ bool * bool_init_cast (bool *b, const s_tag *tag)
   switch (tag->type) {
   case TAG_BOOL:      *b = tag->data.bool;                     return b;
   case TAG_CHARACTER: *b = (bool) tag->data.character;         return b;
-    //case TAG_COMPLEX:   *b = ! complex_is_zero(tag->data.complex);
-    //                                                         return b;
+  case TAG_COMPLEX:   *b = ! complex_is_zero(tag->data.complex);
+                                                               return b;
   case TAG_F32:       *b = (bool) tag->data.f32;               return b;
   case TAG_F64:       *b = (bool) tag->data.f64;               return b;
   case TAG_F128:      *b = (bool) tag->data.f128;              return b;
diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index df98d91..92173e9 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -779,22 +779,19 @@ sw buf_inspect_character_size (const character *c)
   return result;
 }
 
-/*
 sw buf_inspect_complex (s_buf *buf, const s_complex *c)
 {
-  s_struct s;
   sw r;
   sw result = 0;
   s_buf_save save;
   buf_save_init(buf, &save);
-  if (! struct_init_with_data(&s, &g_sym_Complex, false, c)) {
-    r = -2;
-    goto clean;
-  }
-  if ((r = buf_inspect_struct(buf, c)) <= 0)
+  if ((r = buf_inspect_tag(buf, &c->x)) < 0)
     goto restore;
   result += r;
-  if ((
+  if ((r = buf_write_1(buf, " +i ")) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_inspect_tag(buf, &c->y)) < 0)
     goto restore;
   result += r;
   r = result;
@@ -803,21 +800,22 @@ sw buf_inspect_complex (s_buf *buf, const s_complex *c)
   buf_save_restore_wpos(buf, &save);
  clean:
   buf_save_clean(buf, &save);
-  return result;
+  return r;
 }
 
-sw buf_inspect_complex_size (const complex *c)
+sw buf_inspect_complex_size (const s_complex *c)
 {
   sw r;
   sw result = 0;
-  result += strlen("'");
-  if ((r = buf_inspect_str_complex_size(c)) <= 0)
+  if ((r = buf_inspect_tag_size(&c->x)) < 0)
+    return r;
+  result += r;
+  result += strlen(" +i ");
+  if ((r = buf_inspect_tag_size(&c->y)) < 0)
     return r;
   result += r;
-  result += strlen("'");
   return result;
 }
-*/
 
 sw buf_inspect_f32 (s_buf *buf, const f32 *f)
 {
@@ -2302,6 +2300,7 @@ sw buf_inspect_tag (s_buf *buf, const s_tag *tag)
   case TAG_CFN:     return buf_inspect_cfn(buf, &tag->data.cfn);
   case TAG_CHARACTER:
     return buf_inspect_character(buf, &tag->data.character);
+  case TAG_COMPLEX: return buf_inspect_complex(buf, tag->data.complex);
   case TAG_F32:     return buf_inspect_f32(buf, &tag->data.f32);
   case TAG_F64:     return buf_inspect_f64(buf, &tag->data.f64);
   case TAG_F128:    return buf_inspect_f128(buf, &tag->data.f128);
@@ -2354,6 +2353,7 @@ sw buf_inspect_tag_size (const s_tag *tag)
   case TAG_CFN:      return buf_inspect_cfn_size(&tag->data.cfn);
   case TAG_CHARACTER:
     return buf_inspect_character_size(&tag->data.character);
+  case TAG_COMPLEX:  return buf_inspect_complex_size(tag->data.complex);
   case TAG_F32:      return buf_inspect_f32_size(&tag->data.f32);
   case TAG_F64:      return buf_inspect_f64_size(&tag->data.f64);
   case TAG_F128:     return buf_inspect_f128_size(&tag->data.f128);
diff --git a/libc3/compare.c b/libc3/compare.c
index 56b0ba3..f74479f 100644
--- a/libc3/compare.c
+++ b/libc3/compare.c
@@ -13,9 +13,11 @@
 #include "assert.h"
 #include <string.h>
 #include "compare.h"
+#include "complex.h"
 #include "data.h"
 #include "integer.h"
 #include "list.h"
+#include "ratio.h"
 #include "tag.h"
 
 #define COMPARE_DEF(type)                       \
@@ -117,6 +119,35 @@ s8 compare_cfn (const s_cfn *a, const s_cfn *b)
 }
 
 COMPARE_DEF(character)
+
+s8 compare_complex (const s_complex *a, const s_complex *b)
+{
+  s_tag aa = {0};
+  s_tag ax2 = {0};
+  s_tag ay2 = {0};
+  s_tag bb = {0};
+  s_tag bx2 = {0};
+  s_tag by2 = {0};
+  s8 r;
+  assert(a);
+  assert(b);
+  if (! tag_mul(&a->x, &a->x, &ax2) ||
+      ! tag_mul(&a->y, &a->y, &ay2) ||
+      ! tag_add(&ax2, &ay2, &aa) ||
+      ! tag_mul(&b->x, &b->x, &bx2) ||
+      ! tag_mul(&b->y, &b->y, &by2) ||
+      ! tag_add(&bx2, &by2, &bb))
+    abort();
+  r = compare_tag(&aa, &bb);
+  tag_clean(&bb);
+  tag_clean(&by2);
+  tag_clean(&bx2);
+  tag_clean(&aa);
+  tag_clean(&ay2);
+  tag_clean(&ax2);
+  return r;
+}
+
 COMPARE_DEF(f32)
 COMPARE_DEF(f64)
 COMPARE_DEF(f128)
@@ -512,14 +543,26 @@ s8 compare_tag (const s_tag *a, const s_tag *b) {
       b == TAG_FIRST)
     return 1;
   switch (a->type) {
+  case TAG_COMPLEX:
+    switch (b->type) {
+    case TAG_COMPLEX:
+      return compare_complex(a->data.complex, b->data.complex);
+    default:
+      break;
+    }
+    break;
   case TAG_F32:
     switch (b->type) {
+    case TAG_COMPLEX:
+      return compare_f32(a->data.f32, complex_to_f32(b->data.complex));
     case TAG_F32: return compare_f32(a->data.f32, b->data.f32);
     case TAG_F64: return compare_f64((f64) a->data.f32, b->data.f64);
     case TAG_F128:
       return compare_f128((f128) a->data.f32, b->data.f128);
     case TAG_INTEGER:
       return compare_f32(a->data.f32, integer_to_f32(&b->data.integer));
+    case TAG_RATIO:
+      return compare_f32(a->data.f32, ratio_to_f32(&b->data.ratio));
     case TAG_S8:  return compare_f32(a->data.f32, (f32) b->data.s8);
     case TAG_S16: return compare_f32(a->data.f32, (f32) b->data.s16);
     case TAG_S32: return compare_f32(a->data.f32, (f32) b->data.s32);
@@ -536,6 +579,8 @@ s8 compare_tag (const s_tag *a, const s_tag *b) {
     break;
   case TAG_F64:
     switch (b->type) {
+    case TAG_COMPLEX:
+      return compare_f64(a->data.f64, complex_to_f64(b->data.complex));
     case TAG_F32: return compare_f64(a->data.f64, b->data.f32);
     case TAG_F64: return compare_f64((f64) a->data.f64, b->data.f64);
     case TAG_F128:
@@ -621,6 +666,14 @@ s8 compare_tag (const s_tag *a, const s_tag *b) {
       break;
     }
     break;
+  case TAG_RATIO:
+    switch (b->type) {
+    case TAG_RATIO:
+      return compare_ratio(&a->data.ratio, &b->data.ratio);
+    default:
+      break;
+    }
+    break;
   case TAG_S8:
     switch (b->type) {
     case TAG_F32: return compare_f32((f32) a->data.s8, b->data.f32);
@@ -929,8 +982,6 @@ s8 compare_tag (const s_tag *a, const s_tag *b) {
                                           b->data.ptr_free.p);
   case TAG_QUOTE:      return compare_quote(&a->data.quote,
                                             &b->data.quote);
-  case TAG_RATIO:      return compare_ratio(&a->data.ratio,
-                                           &b->data.ratio);
   case TAG_STR:        return compare_str(&a->data.str, &b->data.str);
   case TAG_STRUCT:     return compare_struct(&a->data.struct_,
                                              &b->data.struct_);
@@ -943,10 +994,12 @@ s8 compare_tag (const s_tag *a, const s_tag *b) {
   case TAG_UNQUOTE:    return compare_unquote(&a->data.unquote,
                                               &b->data.unquote);
   case TAG_VAR:        return compare_ptr(a, b);
+  case TAG_COMPLEX:
   case TAG_F32:
   case TAG_F64:
   case TAG_F128:
   case TAG_INTEGER:
+  case TAG_RATIO:
   case TAG_S8:
   case TAG_S16:
   case TAG_S32:
diff --git a/libc3/compare.h b/libc3/compare.h
index 5237925..f891d7b 100644
--- a/libc3/compare.h
+++ b/libc3/compare.h
@@ -24,6 +24,7 @@ s8 compare_bool (bool a, bool b);
 s8 compare_call (const s_call *a, const s_call *b);
 s8 compare_cfn (const s_cfn *a, const s_cfn *b);
 COMPARE_PROTOTYPE(character);
+s8 compare_complex (const s_complex *a, const s_complex *b);
 COMPARE_PROTOTYPE(f32);
 COMPARE_PROTOTYPE(f64);
 COMPARE_PROTOTYPE(f128);
diff --git a/libc3/complex.c b/libc3/complex.c
index c8f624f..b58acb4 100644
--- a/libc3/complex.c
+++ b/libc3/complex.c
@@ -36,11 +36,18 @@ void complex_clean (s_complex *c)
   tag_clean(&c->y);
 }
 
+void complex_delete (s_complex *c)
+{
+  assert(c);
+  complex_clean(c);
+  free(c);
+}
+
 s_complex * complex_init (s_complex *c)
 {
   assert(c);
-  tag_init_f64(&c->x, 0);
-  tag_init_f64(&c->y, 0);
+  tag_init_u8(&c->x, 0);
+  tag_init_u8(&c->y, 0);
   return c;
 }
 
diff --git a/libc3/complex.h b/libc3/complex.h
index 0b1179c..2f2a22f 100644
--- a/libc3/complex.h
+++ b/libc3/complex.h
@@ -39,6 +39,7 @@ s_complex * complex_div (const s_complex *a, const s_complex *b,
                          s_complex *dest);
 
 /* Constructors, call complex_delete after use. */
+void        complex_delete (s_complex *c);
 s_complex * complex_new (void);
 s_complex * complex_new_copy (const s_complex *a);
 
diff --git a/libc3/env.c b/libc3/env.c
index cc1c989..509c758 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -286,6 +286,29 @@ bool env_eval_call_resolve (s_env *env, s_call *call)
   return call_get(call, &env->facts);
 }
 
+bool env_eval_complex (s_env *env, const s_complex *c, s_tag *dest)
+{
+  s_complex *tmp = NULL;
+  assert(env);
+  assert(c);
+  assert(dest);
+  tmp = alloc(sizeof(s_complex));
+  if (! tmp)
+    return false;
+  if (! env_eval_tag(env, &c->x, &tmp->x)) {
+    free(tmp);
+    return false;
+  }
+  if (! env_eval_tag(env, &c->y, &tmp->y)) {
+    tag_clean(&tmp->x);
+    free(tmp);
+    return false;
+  }
+  dest->type = TAG_COMPLEX;
+  dest->data.complex = tmp;
+  return true;
+}
+
 bool env_eval_equal_list (s_env *env, bool macro, const s_list *a,
                           const s_list *b, s_list **dest)
 {
@@ -442,6 +465,7 @@ bool env_eval_equal_tag (s_env *env, bool macro, const s_tag *a,
     return true;
   }
   switch (a->type) {
+  case TAG_COMPLEX:
   case TAG_F32:
   case TAG_F64:
   case TAG_F128:
@@ -458,6 +482,7 @@ bool env_eval_equal_tag (s_env *env, bool macro, const s_tag *a,
   case TAG_U64:
   case TAG_UW:
     switch (b->type) {
+    case TAG_COMPLEX:
     case TAG_F32:
     case TAG_F64:
     case TAG_F128:
@@ -958,6 +983,7 @@ bool env_eval_quote_tag (s_env *env, const s_tag *tag, s_tag *dest)
   case TAG_BOOL:
   case TAG_CFN:
   case TAG_CHARACTER:
+  case TAG_COMPLEX:
   case TAG_F32:
   case TAG_F64:
   case TAG_F128:
@@ -1132,6 +1158,8 @@ bool env_eval_tag (s_env *env, const s_tag *tag, s_tag *dest)
     return env_eval_block(env, &tag->data.block, dest);
   case TAG_CALL:
     return env_eval_call(env, &tag->data.call, dest);
+  case TAG_COMPLEX:
+    return env_eval_complex(env, tag->data.complex, dest);
   case TAG_IDENT:
     return env_eval_ident(env, &tag->data.ident, dest);
   case TAG_LIST:
diff --git a/libc3/env.h b/libc3/env.h
index 49b5497..0f22ced 100644
--- a/libc3/env.h
+++ b/libc3/env.h
@@ -42,6 +42,8 @@ bool          env_eval_call_cfn (s_env *env, const s_call *call,
 bool          env_eval_call_fn (s_env *env, const s_call *call,
                                 s_tag *dest);
 bool          env_eval_call_resolve (s_env *env, s_call *call);
+bool          env_eval_complex (s_env *env, const s_complex *c,
+                                s_tag *dest);
 bool          env_eval_equal_block (s_env *env, bool macro,
                                     const s_block *a, const s_block *b,
                                     s_block *dest);
diff --git a/libc3/hash.c b/libc3/hash.c
index 786d47a..c6531c8 100644
--- a/libc3/hash.c
+++ b/libc3/hash.c
@@ -162,6 +162,19 @@ bool hash_update_cfn (t_hash *hash, const s_cfn *cfn)
 
 HASH_UPDATE_DEF(char)
 HASH_UPDATE_DEF(character)
+
+bool hash_update_complex (t_hash *hash, const s_complex *c)
+{
+  const s8 type[] = "complex";
+  assert(hash);
+  assert(c);
+  if (! hash_update(hash, type, sizeof(type)) ||
+      ! hash_update_tag(hash, &c->x) ||
+      ! hash_update_tag(hash, &c->y))
+    return false;
+  return true;
+}
+
 HASH_UPDATE_DEF(f32)
 HASH_UPDATE_DEF(f64)
 HASH_UPDATE_DEF(f128)
@@ -338,8 +351,8 @@ bool hash_update_ratio (t_hash *hash, const s_ratio *ratio)
   assert(hash);
   assert(ratio);
   if (! hash_update(hash, type, sizeof(type)) ||
-  ! hash_update(hash, &ratio->numerator, sizeof(ratio->numerator)) ||
-  ! hash_update(hash, &ratio->denominator, sizeof(ratio->denominator)))
+      ! hash_update_integer(hash, &ratio->numerator) ||
+      ! hash_update_integer(hash, &ratio->denominator))
     return false;
   return true;
 }
@@ -441,52 +454,52 @@ bool hash_update_tag (t_hash *hash, const s_tag *tag)
   if (! hash_update_u8(hash, &tag_type))
     return false;
   switch (tag->type) {
-  case TAG_ARRAY: return hash_update_array(hash, &tag->data.array);
-  case TAG_BLOCK: return hash_update_block(hash, &tag->data.block);
-  case TAG_BOOL:  return hash_update_bool(hash, &tag->data.bool);
-  case TAG_CALL:  return hash_update_call(hash, &tag->data.call);
-  case TAG_CFN:   return hash_update_cfn(hash, &tag->data.cfn);
+  case TAG_ARRAY:   return hash_update_array(hash, &tag->data.array);
+  case TAG_BLOCK:   return hash_update_block(hash, &tag->data.block);
+  case TAG_BOOL:    return hash_update_bool(hash, &tag->data.bool);
+  case TAG_CALL:    return hash_update_call(hash, &tag->data.call);
+  case TAG_CFN:     return hash_update_cfn(hash, &tag->data.cfn);
   case TAG_CHARACTER:
     return hash_update_character(hash, &tag->data.character);
-  case TAG_F32:   return hash_update_f32(hash, &tag->data.f32);
-  case TAG_F64:   return hash_update_f64(hash, &tag->data.f64);
-  case TAG_F128:  return hash_update_f128(hash, &tag->data.f128);
-  case TAG_FACT:  return hash_update_fact(hash, &tag->data.fact);
-  case TAG_FN:    return hash_update_fn(hash, &tag->data.fn);
-  case TAG_IDENT: return hash_update_ident(hash, &tag->data.ident);
+  case TAG_COMPLEX: return hash_update_complex(hash, tag->data.complex);
+  case TAG_F32:     return hash_update_f32(hash, &tag->data.f32);
+  case TAG_F64:     return hash_update_f64(hash, &tag->data.f64);
+  case TAG_F128:    return hash_update_f128(hash, &tag->data.f128);
+  case TAG_FACT:    return hash_update_fact(hash, &tag->data.fact);
+  case TAG_FN:      return hash_update_fn(hash, &tag->data.fn);
+  case TAG_IDENT:   return hash_update_ident(hash, &tag->data.ident);
   case TAG_INTEGER:
     return hash_update_integer(hash, &tag->data.integer);
   case TAG_LIST:
     return hash_update_list(hash, (const s_list * const *)
                             &tag->data.list);
-  case TAG_MAP:   return hash_update_map(hash, &tag->data.map);
-  case TAG_PTAG:  return hash_update_ptag(hash, &tag->data.ptag);
-  case TAG_PTR:   return hash_update_ptr(hash, &tag->data.ptr);
+  case TAG_MAP:     return hash_update_map(hash, &tag->data.map);
+  case TAG_PTAG:    return hash_update_ptag(hash, &tag->data.ptag);
+  case TAG_PTR:     return hash_update_ptr(hash, &tag->data.ptr);
   case TAG_PTR_FREE:
     return hash_update_ptr_free(hash, &tag->data.ptr_free);
-  case TAG_QUOTE: return hash_update_quote(hash, &tag->data.quote);
-  case TAG_RATIO: return hash_update_ratio(hash, &tag->data.ratio);
-  case TAG_S8:    return hash_update_s8(hash, &tag->data.s8);
-  case TAG_S16:   return hash_update_s16(hash, &tag->data.s16);
-  case TAG_S32:   return hash_update_s32(hash, &tag->data.s32);
-  case TAG_S64:   return hash_update_s64(hash, &tag->data.s64);
-  case TAG_SW:    return hash_update_sw(hash, &tag->data.sw);
-  case TAG_STR:   return hash_update_str(hash, &tag->data.str);
-  case TAG_STRUCT:
-    return hash_update_struct(hash, &tag->data.struct_);
+  case TAG_QUOTE:   return hash_update_quote(hash, &tag->data.quote);
+  case TAG_RATIO:   return hash_update_ratio(hash, &tag->data.ratio);
+  case TAG_S8:      return hash_update_s8(hash, &tag->data.s8);
+  case TAG_S16:     return hash_update_s16(hash, &tag->data.s16);
+  case TAG_S32:     return hash_update_s32(hash, &tag->data.s32);
+  case TAG_S64:     return hash_update_s64(hash, &tag->data.s64);
+  case TAG_SW:      return hash_update_sw(hash, &tag->data.sw);
+  case TAG_STR:     return hash_update_str(hash, &tag->data.str);
+  case TAG_STRUCT:  return hash_update_struct(hash, &tag->data.struct_);
   case TAG_STRUCT_TYPE:
     return hash_update_struct_type(hash, &tag->data.struct_type);
-  case TAG_SYM:   return hash_update_sym(hash, &tag->data.sym);
-  case TAG_TUPLE: return hash_update_tuple(hash, &tag->data.tuple);
-  case TAG_U8:    return hash_update_u8(hash, &tag->data.u8);
-  case TAG_U16:   return hash_update_u16(hash, &tag->data.u16);
-  case TAG_U32:   return hash_update_u32(hash, &tag->data.u32);
-  case TAG_U64:   return hash_update_u64(hash, &tag->data.u64);
+  case TAG_SYM:     return hash_update_sym(hash, &tag->data.sym);
+  case TAG_TUPLE:   return hash_update_tuple(hash, &tag->data.tuple);
+  case TAG_U8:      return hash_update_u8(hash, &tag->data.u8);
+  case TAG_U16:     return hash_update_u16(hash, &tag->data.u16);
+  case TAG_U32:     return hash_update_u32(hash, &tag->data.u32);
+  case TAG_U64:     return hash_update_u64(hash, &tag->data.u64);
   case TAG_UNQUOTE:
     return hash_update_unquote(hash, &tag->data.unquote);
-  case TAG_UW:    return hash_update_uw(hash, &tag->data.uw);
-  case TAG_VAR:   return hash_update_var(hash, NULL);
-  case TAG_VOID:  return hash_update_void(hash, NULL);
+  case TAG_UW:      return hash_update_uw(hash, &tag->data.uw);
+  case TAG_VAR:     return hash_update_var(hash, NULL);
+  case TAG_VOID:    return hash_update_void(hash, NULL);
   }
   err_puts("hash_update_tag: unknown tag type");
   assert(! "hash_update_tag: unknown tag type");
diff --git a/libc3/hash.h b/libc3/hash.h
index e4871a4..86b8232 100644
--- a/libc3/hash.h
+++ b/libc3/hash.h
@@ -31,6 +31,7 @@ bool hash_update_call (t_hash *hash, const s_call *call);
 bool hash_update_cfn (t_hash *hash, const s_cfn *cfn);
 HASH_UPDATE_PROTOTYPE(char);
 HASH_UPDATE_PROTOTYPE(character);
+bool hash_update_complex (t_hash *hash, const s_complex *c);
 HASH_UPDATE_PROTOTYPE(f32);
 HASH_UPDATE_PROTOTYPE(f64);
 HASH_UPDATE_PROTOTYPE(f128);
diff --git a/libc3/tag.c b/libc3/tag.c
index c21d38f..65eaadd 100644
--- a/libc3/tag.c
+++ b/libc3/tag.c
@@ -21,6 +21,7 @@
 #include "call.h"
 #include "cfn.h"
 #include "compare.h"
+#include "complex.h"
 #include "env.h"
 #include "fn.h"
 #include "frame.h"
@@ -143,6 +144,7 @@ void tag_clean (s_tag *tag)
   case TAG_BLOCK:       block_clean(&tag->data.block);     break;
   case TAG_CALL:        call_clean(&tag->data.call);       break;
   case TAG_CFN:         cfn_clean(&tag->data.cfn);         break;
+  case TAG_COMPLEX:     complex_delete(tag->data.complex); break;
   case TAG_FN:          fn_clean(&tag->data.fn);           break;
   case TAG_INTEGER:     integer_clean(&tag->data.integer); break;
   case TAG_LIST:        list_delete_all(tag->data.list);   break;
@@ -322,6 +324,7 @@ s_tag * tag_init_copy (s_tag *tag, const s_tag *src)
     tag_init_var(tag);
     break;
   case TAG_VOID:
+    tag_init_void(tag);
     break;
   case TAG_ARRAY:
     array_init_copy(&tag->data.array, &src->data.array);
@@ -335,6 +338,9 @@ s_tag * tag_init_copy (s_tag *tag, const s_tag *src)
   case TAG_CFN:
     cfn_init_copy(&tag->data.cfn, &src->data.cfn);
     break;
+  case TAG_COMPLEX:
+    tag->data.complex = complex_new_copy(src->data.complex);
+    break;
   case TAG_FN:
     fn_init_copy(&tag->data.fn, &src->data.fn);
     break;
@@ -464,18 +470,51 @@ bool tag_is_number (const s_tag *tag)
 {
   assert(tag);
   switch (tag->type) {
+  case TAG_VOID:
+  case TAG_ARRAY:
+  case TAG_BLOCK:
+  case TAG_BOOL:
+  case TAG_CALL:
+  case TAG_CFN:
+  case TAG_CHARACTER:
+  case TAG_FACT:
+  case TAG_FN:
+  case TAG_LIST:
+  case TAG_MAP:
+  case TAG_PTAG:
+  case TAG_PTR:
+  case TAG_PTR_FREE:
+  case TAG_QUOTE:
+  case TAG_STR:
+  case TAG_STRUCT:
+  case TAG_STRUCT_TYPE:
+  case TAG_SYM:
+  case TAG_TUPLE:
+  case TAG_UNQUOTE:
+  case TAG_VAR:
+  case TAG_IDENT:
+    return false;
+  case TAG_COMPLEX:
+  case TAG_F32:
+  case TAG_F64:
+  case TAG_F128:
   case TAG_INTEGER:
-  case TAG_S8:
-  case TAG_S16:
-  case TAG_S32:
+  case TAG_RATIO:
+  case TAG_SW:
   case TAG_S64:
+  case TAG_S32:
+  case TAG_S16:
+  case TAG_S8:
   case TAG_U8:
   case TAG_U16:
   case TAG_U32:
   case TAG_U64:
+  case TAG_UW:
     return true;
-  default: ;
   }
+  err_puts("tag_is_number: invalid tag type");
+  assert(! "tag_is_number: invalid tag type");
+  abort();
   return false;
 }
 
diff --git a/libc3/types.h b/libc3/types.h
index c119f8a..fc9aac3 100644
--- a/libc3/types.h
+++ b/libc3/types.h
@@ -96,7 +96,7 @@ typedef enum {
   TAG_CALL,
   TAG_CFN,
   TAG_CHARACTER,
-  //TAG_COMPLEX,
+  TAG_COMPLEX,
   TAG_F32,
   TAG_F64,
   TAG_F128,