mirror of
https://github.com/kanaka/mal.git
synced 2024-09-21 10:37:58 +03:00
print_readably flag, str and println functions
Additional flag to pr_str in RDI. If zero, returns string without escaping special characters. Added str and println functions, using most of the same code as pr-str and prn Only one test now failing for step 4.
This commit is contained in:
parent
2eba19559b
commit
17d45192b9
@ -28,6 +28,8 @@ section .data
|
|||||||
|
|
||||||
static core_pr_str_symbol, db "pr-str"
|
static core_pr_str_symbol, db "pr-str"
|
||||||
static core_prn_symbol, db "prn"
|
static core_prn_symbol, db "prn"
|
||||||
|
static core_str_symbol, db "str"
|
||||||
|
static core_println_symbol, db "println"
|
||||||
|
|
||||||
;; Strings
|
;; Strings
|
||||||
|
|
||||||
@ -85,6 +87,8 @@ core_environment:
|
|||||||
|
|
||||||
core_env_native core_pr_str_symbol, core_pr_str
|
core_env_native core_pr_str_symbol, core_pr_str
|
||||||
core_env_native core_prn_symbol, core_prn
|
core_env_native core_prn_symbol, core_prn
|
||||||
|
core_env_native core_str_symbol, core_str
|
||||||
|
core_env_native core_println_symbol, core_println
|
||||||
|
|
||||||
; -----------------
|
; -----------------
|
||||||
; Put the environment in RAX
|
; Put the environment in RAX
|
||||||
@ -435,7 +439,11 @@ core_list:
|
|||||||
;; Convert arguments to a readable string, separated by a space
|
;; Convert arguments to a readable string, separated by a space
|
||||||
;;
|
;;
|
||||||
core_pr_str:
|
core_pr_str:
|
||||||
|
mov rdi, 1 ; print_readably
|
||||||
|
jmp core_str_functions
|
||||||
|
core_str:
|
||||||
|
xor rdi, rdi
|
||||||
|
core_str_functions:
|
||||||
mov al, BYTE [rsi]
|
mov al, BYTE [rsi]
|
||||||
mov ah, al
|
mov ah, al
|
||||||
and ah, content_mask
|
and ah, content_mask
|
||||||
@ -503,12 +511,16 @@ core_pr_str:
|
|||||||
; More inputs
|
; More inputs
|
||||||
mov rsi, [rsi + Cons.cdr] ; pointer
|
mov rsi, [rsi + Cons.cdr] ; pointer
|
||||||
|
|
||||||
|
cmp rdi, 0 ; print_readably
|
||||||
|
je .end_append_char ; No separator if not printing readably
|
||||||
|
|
||||||
; Add separator
|
; Add separator
|
||||||
push rsi
|
push rsi
|
||||||
mov rsi, r8
|
mov rsi, r8
|
||||||
mov cl, ' '
|
mov cl, ' '
|
||||||
call string_append_char
|
call string_append_char
|
||||||
pop rsi
|
pop rsi
|
||||||
|
.end_append_char:
|
||||||
|
|
||||||
; Get the type in ah for comparison at start of loop
|
; Get the type in ah for comparison at start of loop
|
||||||
mov al, BYTE [rsi]
|
mov al, BYTE [rsi]
|
||||||
@ -527,8 +539,11 @@ core_pr_str:
|
|||||||
|
|
||||||
;; Print arguments readably, return nil
|
;; Print arguments readably, return nil
|
||||||
core_prn:
|
core_prn:
|
||||||
; Convert to string
|
|
||||||
call core_pr_str
|
call core_pr_str
|
||||||
|
jmp core_prn_functions
|
||||||
|
core_println:
|
||||||
|
call core_str
|
||||||
|
core_prn_functions:
|
||||||
mov rsi, rax
|
mov rsi, rax
|
||||||
|
|
||||||
; Put newline at the end
|
; Put newline at the end
|
||||||
|
@ -17,6 +17,7 @@ section .data
|
|||||||
section .text
|
section .text
|
||||||
|
|
||||||
;; Input: Address of object in RSI
|
;; Input: Address of object in RSI
|
||||||
|
;; print_readably in RDI. Set to zero for false
|
||||||
;;
|
;;
|
||||||
;; Output: Address of string in RAX
|
;; Output: Address of string in RAX
|
||||||
;;
|
;;
|
||||||
@ -42,6 +43,11 @@ pr_str:
|
|||||||
; ---------------------------
|
; ---------------------------
|
||||||
; Handle string
|
; Handle string
|
||||||
|
|
||||||
|
cmp rdi, 0
|
||||||
|
je .string_not_readable
|
||||||
|
|
||||||
|
; printing readably, so escape characters
|
||||||
|
|
||||||
call string_new ; Output string in rax
|
call string_new ; Output string in rax
|
||||||
|
|
||||||
mov r12, rax
|
mov r12, rax
|
||||||
@ -102,6 +108,12 @@ pr_str:
|
|||||||
|
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
.string_not_readable:
|
||||||
|
; Just return the string
|
||||||
|
call incref_object
|
||||||
|
mov rax, rsi
|
||||||
|
ret
|
||||||
|
|
||||||
; ----------------------------
|
; ----------------------------
|
||||||
.not_string:
|
.not_string:
|
||||||
; Now test the container type (value, list, map, vector)
|
; Now test the container type (value, list, map, vector)
|
||||||
|
@ -184,6 +184,7 @@ error_throw:
|
|||||||
; Print the object in RSI then quit
|
; Print the object in RSI then quit
|
||||||
cmp rsi, 0
|
cmp rsi, 0
|
||||||
je .done ; nothing to print
|
je .done ; nothing to print
|
||||||
|
mov rdi, 1 ; print_readably
|
||||||
call pr_str
|
call pr_str
|
||||||
mov rsi, rax
|
mov rsi, rax
|
||||||
call print_string
|
call print_string
|
||||||
@ -1455,6 +1456,7 @@ _start:
|
|||||||
|
|
||||||
; Put into pr_str
|
; Put into pr_str
|
||||||
mov rsi, rax
|
mov rsi, rax
|
||||||
|
mov rdi, 1 ; print_readably
|
||||||
call pr_str
|
call pr_str
|
||||||
push rax ; Save output string
|
push rax ; Save output string
|
||||||
|
|
||||||
@ -1488,6 +1490,7 @@ _start:
|
|||||||
; Check if an object was thrown
|
; Check if an object was thrown
|
||||||
cmp rsi, 0
|
cmp rsi, 0
|
||||||
je .catch_done_print ; nothing to print
|
je .catch_done_print ; nothing to print
|
||||||
|
mov rdi, 1
|
||||||
call pr_str
|
call pr_str
|
||||||
mov rsi, rax
|
mov rsi, rax
|
||||||
call print_string
|
call print_string
|
||||||
|
Loading…
Reference in New Issue
Block a user