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: 2 additions & 2 deletions PRIMITIVES.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ added to them too.
* Print the specified integer.
* `printstr`
* Print the given string.
* `random`
* Return a random integer between zero and N.
* `split`
* Split a string by the given character, and return a list of "(before after)". Return nil if the character isn't found.
* `split-all`
Expand Down Expand Up @@ -301,6 +299,8 @@ The implementation of these primitives can be found in the file [stdlib.slisp](s
* Print "anything".
* `println`
* Print "anything" by invoking `print`, then outputting a newline.
* `random`
* Return a random integer between zero and N-1.
* `range`
* Return a list of numbers between the given start/end, using the specified step-size.
* `read-line`
Expand Down
29 changes: 0 additions & 29 deletions compiler/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ SYS_wait4 equ 61
SYS_getdents64 equ 217
SYS_clock_gettime equ 228
SYS_prlimit64 equ 302
SYS_getrandom equ 318

;; (entries)
O_RDONLY equ 0
Expand Down Expand Up @@ -2395,34 +2394,6 @@ FUNC fn_sys_putc
ret



;; Generate a random number 0-N
FUNC fn_sys_random
mov rbx, rdi ; type-check arg1
GET_TAG_BITS rbx
cmp rbx, TAG_ID_INTEGER
jne type_error

UNTAG_REG rdi
push rdi ; push the limit

mov rax, SYS_getrandom
lea rdi, [random_buffer] ; random value location
mov rsi, 4 ; number of bytes
xor rdx, rdx ; flags = 0
syscall

pop rcx ; limit the value
mov eax, [random_buffer]
xor edx, edx
div ecx

mov rax, rdx
TAG_INTEGER_REG rax
ret



;; system helper
FUNC fn_sys_run
mov rbx, rdi ; type check
Expand Down
1 change: 0 additions & 1 deletion inception.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@
(register-builtin "sys_packages" (lambda (args) (sys_packages )))
(register-builtin "sys_plus" (lambda (args) (sys_plus (car args) (cadr args))))
(register-builtin "sys_putc" (lambda (args) (sys_putc (car args))))
(register-builtin "sys_random" (lambda (args) (sys_random (car args))))
(register-builtin "sys_run" (lambda (args) (sys_run (car args) (cadr args))))
(register-builtin "sys_split" (lambda (args) (sys_split (car args) (cadr args))))
(register-builtin "sys_sqrt" (lambda (args) (sys_sqrt (car args))))
Expand Down
13 changes: 9 additions & 4 deletions stdlib.slisp
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ It is assumed the filehandle came from fopen, but nil is also accepted."
"Write the single specified character to STDOUT."
(sys_putc c))

(defun random (n)
"Return a random number 0-N."
(sys_random n))

;; NOP: This is a special form in our compiler
;; However if we make this then we can pretend
;; we load packages.
Expand Down Expand Up @@ -1035,6 +1031,15 @@ Uses (environment) to get all environmental variables, and filters."
(getenv-loop name (cdr xs))))
nil))


(defconst SYS_getrandom 318)

(defun random (n)
"Return a random number between zero and N-1, (both zero and N-1 are valid return values.)"
(let ((buf "skx/slisp"))
(syscall SYS_getrandom buf 8)
(% (abs (u64 buf 0)) n)))

(defun read-line ()
"Read and return a line of text from STDIN"
(let ((text "")
Expand Down
Loading