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
3 changes: 3 additions & 0 deletions PRIMITIVES.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ added to them too.
* Returns file information as a list (TYPE SIZE MODE), or nil on failure.
* `stdlib`
* Return the contents of our slisp stdlib at runtime.
* `strat`
* Read the byte at offset N from the given string.
* This is a dangerous function, with no bounds-checking, which works around the fact `explode` and `substr` get foiled by embedded NULL characters.
* `strcat`
* Join two strings together and return them.
* `strcmp`
Expand Down
34 changes: 34 additions & 0 deletions compiler/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2770,6 +2770,40 @@ FUNC fn_sys_stdlib
TAG_STRING_REG rax
ret

;; Return the given byte from the string, as an integer.
;;
;; This function is dangerous as it has no bound checking, we could add
;; it since we know the size of all allocations made with alloc..
;;
;; For the moment it is a workaround for the fact our strings can't have
;; embedded null-characters - if they do "explode" and "substr" don't work
;; any longer.
;;
;; TODO: Add bound-checking.
;;
FUNC fn_sys_strat
mov rbx, rdi ; type-check arg1
GET_TAG_BITS rbx
cmp rbx, TAG_ID_STRING
jne type_error
mov rbx, rsi ; type-check arg2
GET_TAG_BITS rbx
cmp rbx, TAG_ID_INTEGER
jne type_error

UNTAG_REG rdi
UNTAG_REG rsi

; RDI points to the string, add the RSI/offset to it
add rdi, rsi

; Load and return the character/byte as an integer.
xor rax, rax
mov al, byte [rdi]
TAG_INTEGER_REG rax
ret


;; join the two given strings and return a new one.
FUNC fn_sys_strcat
mov rbx, rdi
Expand Down
1 change: 1 addition & 0 deletions inception.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@
(register-builtin "sys_sqrt" (lambda (args) (sys_sqrt (car args))))
(register-builtin "sys_stat" (lambda (args) (sys_stat (car args))))
(register-builtin "sys_stdlib" (lambda (args) (sys_stdlib)))
(register-builtin "sys_strat" (lambda (args) (sys_strat (car args) (cadr args))))
(register-builtin "sys_strcat" (lambda (args) (sys_strcat (car args) (cadr args))))
(register-builtin "sys_strcmp" (lambda (args) (sys_strcmp (car args) (cadr args))))
(register-builtin "sys_strdup" (lambda (args) (sys_strdup (car args))))
Expand Down
6 changes: 6 additions & 0 deletions stdlib.slisp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ This returns a list of the form (TYPE SIZE MODE)."
"Return the contents of the stdlib.slisp standard-library."
(sys_stdlib))

(defun strat(str n)
"Return the integer value of the character at offset N for the given string.

This ignores embedded NULLs which would otherwise break substr and explode."
(sys_strat str n))

(defun strcat(a b)
"Return the result of concatenating the two specified strings into one."
(sys_strcat a b))
Expand Down
Loading