]> git.lizzy.rs Git - uwu-lang.git/blobdiff - std/int.c
uwuint: use long instead of int to prevent YEAR2038 problem in nolambda time library
[uwu-lang.git] / std / int.c
index 48c808b18fcec473d78b7b16608ddc6cb40ad6c3..be314dc98cddd69153ecedabc86df3086c596908 100644 (file)
--- a/std/int.c
+++ b/std/int.c
@@ -1,9 +1,10 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include "../src/err.h"
-#include "../api/vm.h"
-#include "../api/int.h"
-#include "../api/bool.h"
+#include "common/err.h"
+#include "api/vm.h"
+#include "api/int.h"
+#include "api/bool.h"
+#include "api/util.h"
 
 typedef enum
 {
@@ -15,23 +16,22 @@ typedef enum
        BOP_EQU,
 } BinaryOP;
 
-static int binary(const char *fnname, UwUVMArgs *args, BinaryOP op)
+static long binary(const char *fnname, UwUVMArgs *args, BinaryOP op)
 {
-       if (args->num != 2)
-               error("error: %s requires exactly 2 arguments\n", fnname);
+       uwuutil_require_exact(fnname, args, 2);
 
        UwUVMValue value0 = uwuvm_get_arg(args, 0);
 
-       if (value0.type != VT_INT)
-               error("error: %s requires an integer as $0\n", fnname);
+       if (value0.type != &uwuint_type)
+               error("type error: %s requires an integer as $0\n", fnname);
 
        UwUVMValue value1 = uwuvm_get_arg(args, 1);
 
-       if (value1.type != VT_INT)
-               error("error: %s requires an integer as $1\n", fnname);
+       if (value1.type != &uwuint_type)
+               error("type error: %s requires an integer as $1\n", fnname);
 
-       int a = value0.value.int_value;
-       int b = value1.value.int_value;
+       long a = uwuint_get(value0);
+       long b = uwuint_get(value1);
 
        switch (op) {
                case BOP_SUB: return a - b;
@@ -41,6 +41,8 @@ static int binary(const char *fnname, UwUVMArgs *args, BinaryOP op)
                case BOP_GRT: return a > b;
                case BOP_EQU: return a == b;
        }
+
+       return 0;
 }
 
 typedef enum
@@ -50,17 +52,17 @@ typedef enum
        ROP_EQU,
 } ReduceOP;
 
-static int reduce(const char *fnname, UwUVMArgs *args, ReduceOP op, int result)
+static long reduce(const char *fnname, UwUVMArgs *args, ReduceOP op, long result)
 {
-       int first;
+       long first;
 
        for (size_t i = 0; i < args->num; i++) {
                UwUVMValue value = uwuvm_get_arg(args, i);
 
-               if (value.type != VT_INT)
-                       error("error: %s only accepts integers as arguments (invalid argument: $%lu)\n", fnname, i);
+               if (value.type != &uwuint_type)
+                       error("type error: %s only accepts integers as arguments (invalid argument: $%lu)\n", fnname, i);
 
-               int this = value.value.int_value;
+               long this = uwuint_get(value);
 
                switch (op) {
                        case ROP_ADD: result += this; break;
@@ -80,7 +82,7 @@ static int reduce(const char *fnname, UwUVMArgs *args, ReduceOP op, int result)
 
 UwUVMValue uwu_add(UwUVMArgs *args)
 {
-       return uwuint_create(reduce(":int:mul", args, ROP_ADD, 0));
+       return uwuint_create(reduce(":int:add", args, ROP_ADD, 0));
 }
 
 UwUVMValue uwu_sub(UwUVMArgs *args)
@@ -115,20 +117,11 @@ UwUVMValue uwu_greater(UwUVMArgs *args)
 
 UwUVMValue uwu_equal(UwUVMArgs *args)
 {
-       if (args->num < 2)
-               error("error: :int:equal requires at least 2 arguments\n");
-
+       uwuutil_require_min(":int:equal", args, 2);
        return uwubool_create(reduce(":int:equal", args, ROP_EQU, 1) == 1);
 }
 
 UwUVMValue uwu_is(UwUVMArgs *args)
 {
-       if (args->num < 1)
-               error("error: :int:is requires at least 1 argument\n");
-
-       for (size_t i = 0; i < args->num; i++)
-               if (uwuvm_get_arg(args, i).type != VT_INT)
-                       return uwubool_create(false);
-
-       return uwubool_create(true);
+       return uwuutil_is_type(":int:is", args, &uwuint_type);
 }