Commit 4cb0bb7d94a54d857454fb156905f3f208ff8113

Thomas de Grivel 2024-03-04T12:34:55

operations on ratios

diff --git a/.ic3_history b/.ic3_history
index fa38750..7e0499f 100644
--- a/.ic3_history
+++ b/.ic3_history
@@ -1,24 +1,3 @@
-dlopen("libc3/window/sdl2/.libs/libc3_window_sdl2.so")
-%GL.Object{}
-name = "Plop"
-m = macro (name) { quote "Hello, " + name + " !" }
-m(name)
-m(^ name)
-name = "Plop"
-m = macro (name) { quote "Hello, " + unquote name + " !" }
-name = "Plop"
-m = macro (name) { quote "Hello, " + unquote name + " !" }
-m(name)
-m("Plop")
-m(^ name)
-m = macro (name) { quote "Hello, " + unquote name + " !" }
-name = "Plop"
-^ name = plop
-plop
-name
-name = "Plop"
-m = macro (name) { quote "Hello, " + unquote name + " !" }
-m(name)
 m(^name)
 m(^ name)
 m = macro (name) { quote "Hello, " + unquote name + " !" }
@@ -97,3 +76,24 @@ else
   3 + 3
   4 + 4
 end
+if_then_else true 1 2
+if_then_else
+féls
+false
+if true && true
+  1 + 1
+  2 + 2
+else
+  3 + 3
+  4 + 4
+end
+1/2 + 1/3
+1/2 + 2/3
+3/2 + 1/3
+3/2 + 2/3
+4/2 + 2/3
+4/2 / 2/3
+1/2 / 2/3
+1/2 * 2/3
+1/2 - 2/3
+1/2 / -2/3
diff --git a/libc3/ratio.c b/libc3/ratio.c
index 27fcde9..a6956d1 100644
--- a/libc3/ratio.c
+++ b/libc3/ratio.c
@@ -246,14 +246,20 @@ bool ratio_is_zero (const s_ratio *r)
 s_ratio * ratio_mul (const s_ratio *a, const s_ratio *b,
                            s_ratio *dest)
 {
+  s_ratio tmp = {0};
   assert(a);
   assert(b);
   assert(dest);
   assert(integer_is_positive(&a->denominator));
   assert(integer_is_positive(&b->denominator));
-  integer_mul(&a->numerator, &b->numerator, &tmp.numerator);
-  integer_mul(&a->denominator, &b->denominator,
-          &tmp.denominator);
+  if (! integer_mul(&a->numerator, &b->numerator, &tmp.numerator))
+    return NULL;
+  if (! integer_mul(&a->denominator, &b->denominator,
+                    &tmp.denominator)) {
+    integer_clean(&tmp.numerator);
+    return NULL;
+  }
+  *dest = tmp;
   return dest;
 }
 
diff --git a/libc3/tag_add.c b/libc3/tag_add.c
index 1d26e2e..e320851 100644
--- a/libc3/tag_add.c
+++ b/libc3/tag_add.c
@@ -166,8 +166,10 @@ s_tag * tag_add (const s_tag *a, const s_tag *b, s_tag *dest)
   case TAG_RATIO:
     switch (b->type) {
     case TAG_RATIO:
+      if (! ratio_add(&a->data.ratio, &b->data.ratio,
+                      &dest->data.ratio))
+        return NULL;
       dest->type = TAG_RATIO;
-      ratio_add(&a->data.ratio, &b->data.ratio, &dest->data.ratio);
       return dest;
     default:
       goto ko;
diff --git a/libc3/tag_div.c b/libc3/tag_div.c
index 5d8dda9..e4c84b8 100644
--- a/libc3/tag_div.c
+++ b/libc3/tag_div.c
@@ -13,6 +13,7 @@
 #include "assert.h"
 #include <math.h>
 #include "integer.h"
+#include "ratio.h"
 #include "tag.h"
 
 s_tag * tag_div (const s_tag *a, const s_tag *b, s_tag *dest)
@@ -143,6 +144,17 @@ s_tag * tag_div (const s_tag *a, const s_tag *b, s_tag *dest)
     default:
       goto ko;
     }
+  case TAG_RATIO:
+    switch (b->type) {
+    case TAG_RATIO:
+      if (! ratio_div(&a->data.ratio, &b->data.ratio,
+                      &dest->data.ratio))
+        return NULL;
+      dest->type = TAG_RATIO;
+      return dest;
+    default:
+      goto ko;
+    }
   case TAG_S8:
     switch (b->type) {
     case TAG_F32:
diff --git a/libc3/tag_mul.c b/libc3/tag_mul.c
index 2097fc4..dd435b2 100644
--- a/libc3/tag_mul.c
+++ b/libc3/tag_mul.c
@@ -13,6 +13,7 @@
 #include "assert.h"
 #include <math.h>
 #include "integer.h"
+#include "ratio.h"
 #include "tag.h"
 
 s_tag * tag_mul (const s_tag *a, const s_tag *b, s_tag *dest)
@@ -143,6 +144,17 @@ s_tag * tag_mul (const s_tag *a, const s_tag *b, s_tag *dest)
     default:
       goto ko;
     }
+  case TAG_RATIO:
+    switch (b->type) {
+    case TAG_RATIO:
+      if (! ratio_mul(&a->data.ratio, &b->data.ratio,
+                      &dest->data.ratio))
+        return NULL;
+      dest->type = TAG_RATIO;
+      return dest;
+    default:
+      goto ko;
+    }
   case TAG_S8:
     switch (b->type) {
     case TAG_F32:
diff --git a/libc3/tag_sub.c b/libc3/tag_sub.c
index 5057909..c5b0484 100644
--- a/libc3/tag_sub.c
+++ b/libc3/tag_sub.c
@@ -13,6 +13,7 @@
 #include "assert.h"
 #include <math.h>
 #include "integer.h"
+#include "ratio.h"
 #include "tag.h"
 
 s_tag * tag_sub (const s_tag *a, const s_tag *b, s_tag *dest)
@@ -143,6 +144,17 @@ s_tag * tag_sub (const s_tag *a, const s_tag *b, s_tag *dest)
     default:
       goto ko;
     }
+  case TAG_RATIO:
+    switch (b->type) {
+    case TAG_RATIO:
+      if (! ratio_sub(&a->data.ratio, &b->data.ratio,
+                      &dest->data.ratio))
+        return NULL;
+      dest->type = TAG_RATIO;
+      return dest;
+    default:
+      goto ko;
+    }
   case TAG_S8:
     switch (b->type) {
     case TAG_F32: