]> git.lizzy.rs Git - uwu-nolambda.git/commitdiff
Implement nolamda:random module
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 1 Jan 2022 14:28:29 +0000 (15:28 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 1 Jan 2022 14:28:29 +0000 (15:28 +0100)
Makefile
README.md
random.c [new file with mode: 0644]
test.uwu

index bdf5dedd6d9cd1205b50317fcd3ace4e0f483b78..5459cb5a84568e8f68ae1a156660cb58c701cd0e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-all: flow.so io.so fs.so os.so
+all: flow.so io.so fs.so os.so random.so
 
 uwu_include_path=../uwulang/
 
index 8dbdb0a4a6498b1115388574534cc2acbcd0fad1..a2e93a733894541cece234c096ada5921ef36a79 100644 (file)
--- a/README.md
+++ b/README.md
@@ -17,7 +17,7 @@ make uwu_include_path=/path/to/uwulang/repo
 
 ## Modules
 
-The following modules are implemented: `nolambda:flow`, `nolambda:io`, `nolambda:fs`, `nolambda:os`. The modules `nolambda:global`, and `nolambda:random` are ToDo.
+Note: The module `nolambda:global` is not implemented yet.
 
 ### `nolambda:flow`
 
@@ -54,6 +54,6 @@ Note: all file paths are relative to the _directory the program was started from
 
 ### `nolambda:random`
 
-- `nolambda:random:random`: Returns a pseudorandom integer between $0 (integer) and $1 (integer). The upper bound is exclusive, the lower bound inclusive. Causes an error if the range is bigger than `nolambda:random:max`.
-- `nolambda:random:max`: Returns RAND_MAX, which is usually 32767.
+- `nolambda:random:random`: Returns a pseudorandom integer between $0 (integer) and $1 (integer). The range is inclusive on both boundse. Causes an error if the range is bigger than `nolambda:random:max` or empty.
+- `nolambda:random:max`: Returns RAND_MAX.
 - `nolambda:random:seed`: Sets the random seed to $0 (integer) and returns `:nil:nil`.
diff --git a/random.c b/random.c
new file mode 100644 (file)
index 0000000..dea81aa
--- /dev/null
+++ b/random.c
@@ -0,0 +1,53 @@
+#include <stdlib.h>
+#include "api/vm.h"
+#include "api/util.h"
+#include "api/int.h"
+#include "api/nil.h"
+#include "common/err.h"
+
+UwUVMValue uwu_random(UwUVMArgs *args)
+{
+       uwuutil_require_exact("nolambda:random:random", args, 2);
+
+       UwUVMValue value0 = uwuvm_get_arg(args, 0);
+
+       if (value0.type != &uwuint_type)
+               error("type error: nolamda:random:random requires an integer as $0\n");
+
+       UwUVMValue value1 = uwuvm_get_arg(args, 1);
+
+       if (value1.type != &uwuint_type)
+               error("type error: nolamda:random:random requires an integer as $1\n");
+
+       long min = uwuint_get(value0);
+       long max = uwuint_get(value1) + 1;
+
+       long range = max - min;
+
+       if (range < 0)
+               error("type error: range passed to nolambda:random:random is empty\n");
+
+       if (range > RAND_MAX)
+               error("type error: range passed to nolambda:random:random is bigger than nolambda:random:max");
+
+       return uwuint_create(min + rand() % range);
+}
+
+UwUVMValue uwu_max(UwUVMArgs *args)
+{
+       uwuutil_require_exact("nolambda:random:max", args, 0);
+       return uwuint_create(RAND_MAX);
+}
+
+UwUVMValue uwu_seed(UwUVMArgs *args)
+{
+       uwuutil_require_exact("nolambda:random:seed", args, 1);
+
+       UwUVMValue value = uwuvm_get_arg(args, 0);
+
+       if (value.type != &uwuint_type)
+               error("type error: nolamda:random:seed requires an integer as $0\n");
+
+       srand(uwuint_get(value) % RAND_MAX);
+       return uwunil_create();
+}
index 8e7d32b52455ddce7ee3afb5697b93f9c3916320..6182a05cc3c75bb1878f905c0c5727c7f9e50117 100644 (file)
--- a/test.uwu
+++ b/test.uwu
@@ -8,11 +8,6 @@ main flow:linear(
        )),
 
        io:print(:nil:nil),
-       io:print("--- QUINE ---"),
-       io:print(fs:read("test.uwu")),
-       io:print("--- END OF QUINE ---"),
-       io:print(:nil:nil),
-
        fs:write("test", "hello world"),
        :bool:if(fs:exists("test"),
                io:print("successfully wrote file"),
@@ -27,11 +22,31 @@ main flow:linear(
                io:print("successfully removed file")
        ),
 
+       io:print(:nil:nil),
        io:print(os:time),
        os:sleep(500),
        io:print(os:time),
 
+       io:print(:nil:nil),
        os:execute("echo hello world"),
 
+       io:print(:nil:nil),
+       io:print(:str:cat(
+               "Unseeded dice: ",
+               random:random(1, 6)
+       )),
+
+       random:seed(os:time),
+       io:print(:str:cat(
+               "Seeded dice: ",
+               random:random(1, 6)
+       )),
+
+       io:print(:str:cat(
+               "RAND_MAX = ",
+               random:max
+       )),
+
+       io:print(:nil:nil),
        "success"
 )