diff options
Diffstat (limited to 'std')
-rw-r--r-- | std/bool.c | 13 | ||||
-rw-r--r-- | std/int.c | 16 | ||||
-rw-r--r-- | std/nil.c | 6 | ||||
-rw-r--r-- | std/ref.c | 21 | ||||
-rw-r--r-- | std/str.c | 6 |
5 files changed, 42 insertions, 20 deletions
@@ -2,8 +2,10 @@ #include <stdlib.h> #include "../src/err.h" #include "../api/vm.h" +#include "../api/util.h" #include "../api/bool.h" + static inline bool get_bool_arg(UwUVMArgs *args, size_t i) { return uwubool_get(uwuvm_get_arg(args, i)); @@ -14,7 +16,7 @@ UwUVMValue uwu_if(UwUVMArgs *args) if (args->num != 3) error("error: :bool:if requires exactly 3 arguments\n"); - return uwuvm_copy_value(get_bool_arg(args, 0) + return uwuvm_clone_value(get_bool_arg(args, 0) ? uwuvm_get_arg(args, 1) : uwuvm_get_arg(args, 2) ); @@ -84,12 +86,5 @@ UwUVMValue uwu_false(UwUVMArgs *args) UwUVMValue uwu_is(UwUVMArgs *args) { - if (args->num < 1) - error("error: :bool:is requires at least 1 argument\n"); - - for (size_t i = 0; i < args->num; i++) - if (uwuvm_get_arg(args, i).type != &uwubool_type) - return uwubool_create(false); - - return uwubool_create(true); + return uwuutil_is_type(":bool:is", args, &uwubool_type); } @@ -4,6 +4,7 @@ #include "../api/vm.h" #include "../api/int.h" #include "../api/bool.h" +#include "../api/util.h" typedef enum { @@ -30,8 +31,8 @@ static int binary(const char *fnname, UwUVMArgs *args, BinaryOP op) if (value1.type != &uwuint_type) error("error: %s requires an integer as $1\n", fnname); - int a = *(int *) value0.data; - int b = *(int *) value1.data; + int a = uwuint_get(value0); + int b = uwuint_get(value1); switch (op) { case BOP_SUB: return a - b; @@ -60,7 +61,7 @@ static int reduce(const char *fnname, UwUVMArgs *args, ReduceOP op, int result) if (value.type != &uwuint_type) error("error: %s only accepts integers as arguments (invalid argument: $%lu)\n", fnname, i); - int this = *(int *) value.data; + int this = uwuint_get(value); switch (op) { case ROP_ADD: result += this; break; @@ -123,12 +124,5 @@ UwUVMValue uwu_equal(UwUVMArgs *args) 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 != &uwuint_type) - return uwubool_create(false); - - return uwubool_create(true); + return uwuutil_is_type(":int:is", args, &uwuint_type); } @@ -1,5 +1,6 @@ #include "../src/err.h" #include "../api/nil.h" +#include "../api/util.h" UwUVMValue uwu_nil(UwUVMArgs *args) { @@ -8,3 +9,8 @@ UwUVMValue uwu_nil(UwUVMArgs *args) return uwunil_create(); } + +UwUVMValue uwu_is(UwUVMArgs *args) +{ + return uwuutil_is_type(":nil:is", args, &uwunil_type); +} diff --git a/std/ref.c b/std/ref.c new file mode 100644 index 0000000..f82bb50 --- /dev/null +++ b/std/ref.c @@ -0,0 +1,21 @@ +#include "../src/err.h" +#include "../api/ref.h" +#include "../api/util.h" + +UwUVMValue uwu_call(UwUVMArgs *args) +{ + if (args->num < 1) + error(":ref:call requires at least one argument\n"); + + UwUVMValue value = uwuvm_get_arg(args, 0); + + if (value.type != &uwuref_type) + error(":ref:call requires a function reference as $0\n"); + + return uwuvm_call_function(value.data, args->num - 1, &args->unevaluated[1], args->super); +} + +UwUVMValue uwu_is(UwUVMArgs *args) +{ + return uwuutil_is_type(":ref:is", args, &uwuref_type); +} @@ -2,6 +2,7 @@ #include <stdlib.h> #include "../api/vm.h" #include "../api/str.h" +#include "../api/util.h" UwUVMValue uwu_cat(UwUVMArgs *args) { @@ -28,3 +29,8 @@ UwUVMValue uwu_cat(UwUVMArgs *args) return uwustr_create(result); } + +UwUVMValue uwu_is(UwUVMArgs *args) +{ + return uwuutil_is_type(":str:is", args, &uwustr_type); +} |