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 PRIMITIVES.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ Note that functions have their names mangled a little bit ("-" is converted to "
* `int`
* Convert the given float, or character, to an integer.
* Anything else becomes zero.
* `mkdir`
* Create the named directory. **NOTE**: Mode is fixed at 0755, and parent directories must exist.
* `newline`
* Print a newline.
* `not`
Expand All @@ -129,6 +131,8 @@ Note that functions have their names mangled a little bit ("-" is converted to "
* Print the given string.
* `random`
* Return a random integer between zero and N.
* `rmdir`
* Remove the named directory.
* `setnth`
* Update the Nth item in the given list with the specified value.
* The list is updated in-place.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ It should be noted that we prepend a standard library of functions to all user p
* File I/O operations:
* `fopen`, `fclose`, `fread`, and `fwrite`.
* Filesystem primitive:
* `dir?`, `entries`, `exists?`, `file?`, `stat`, `unlink` and `which`.
* `dir?`, `entries`, `exists?`, `file?`, `mkdir`, `rmdir`, `stat`, `unlink` and `which`.
* Comparison operations:
* `=`, `<`, `<=`, `>=`, `>`, and `!` to invert a result.
* Special forms
Expand Down
43 changes: 43 additions & 0 deletions compiler/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ SYS_close equ 3
SYS_stat equ 4
SYS_lseek equ 8
SYS_exit equ 60
SYS_mkdir equ 83
SYS_rmdir equ 84
SYS_unlink equ 87
SYS_getdents64 equ 217
SYS_getrandom equ 318
Expand Down Expand Up @@ -1423,6 +1425,27 @@ FUNC fn_int
ret


;; Make a directory, mode is fixed
FUNC fn_mkdir
mov rbx, rdi
GET_TAG_BITS rbx
cmp rbx, TAG_ID_STRING
jne type_error

UNTAG_REG rdi
mov rax, SYS_mkdir
mov rsi, 0o755 ; mode in octal
syscall
test rax,rax
js .failure
TAG_INTEGER_REG rax
ret
.failure:
xor rax,rax
TAG_NIL_REG rax
ret


;; A nil becomes 1, anything else becomes nil.
FUNC fn_not
mov rax, rdi
Expand Down Expand Up @@ -1535,6 +1558,26 @@ FUNC fn_random



;; Remove a directory, mode is fixed
FUNC fn_rmdir
mov rbx, rdi
GET_TAG_BITS rbx
cmp rbx, TAG_ID_STRING
jne type_error

UNTAG_REG rdi
mov rax, SYS_rmdir
syscall
test rax,rax
js .failure
TAG_INTEGER_REG rax
ret
.failure:
xor rax,rax
TAG_NIL_REG rax
ret


;; Update the Nth list item, leaving everything else as-is.
FUNC fn_setnth
mov rbx,rdi ; arg1: list
Expand Down
6 changes: 6 additions & 0 deletions test/mkdir-rmdir.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(defun main ()
(mkdir "foo")
(println "Does foo/ exist:" (dir? "foo"))
(rmdir "foo")
(println "Does foo/ exist:" (dir? "foo"))
)
2 changes: 2 additions & 0 deletions test/mkdir-rmdir.test.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Does foo/ exist:1
Does foo/ exist:<nil>
Loading