Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libraries/common/effekt.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ extern def unsafeSubstring(str: String, from: Int, to: Int) at {}: String =
extern def random() at io: Double =
js "Math.random()"
chez "(random 1.0)"
llvm """
%r = call double @c_io_random()
ret double %r
"""
vm "effekt::random()"

// References and state
Expand Down
1 change: 1 addition & 0 deletions libraries/llvm/forward-declare-c.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare %Pos @c_get_arg(i64)

declare void @c_io_println(%Pos)
declare %Pos @c_io_readln()
declare %Double @c_io_random()

declare void @hole(i8*)
declare void @duplicated_prompt()
Expand Down
8 changes: 6 additions & 2 deletions libraries/llvm/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <uv.h>
#include <string.h> // to compare flag names

// Println and Readln
// ------------------
// Println and Readln and Random
// -----------------------------

void c_io_println(String text) {
for (uint64_t j = 0; j < text.tag; ++j) {
Expand Down Expand Up @@ -34,6 +34,10 @@ String c_io_readln() {
return result;
}

double c_io_random(void) {
return (double)rand() / (double)RAND_MAX;
}

// Lib UV Bindings
// ---------------
// Ideas behind the LLVM / libuv implementation.
Expand Down