1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-11 00:52:44 +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:
Ben Dudson 2017-11-09 00:13:20 +00:00
parent 2eba19559b
commit 17d45192b9
3 changed files with 34 additions and 4 deletions

View File

@ -28,6 +28,8 @@ section .data
static core_pr_str_symbol, db "pr-str"
static core_prn_symbol, db "prn"
static core_str_symbol, db "str"
static core_println_symbol, db "println"
;; Strings
@ -85,6 +87,8 @@ core_environment:
core_env_native core_pr_str_symbol, core_pr_str
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
@ -435,7 +439,11 @@ core_list:
;; Convert arguments to a readable string, separated by a space
;;
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 ah, al
and ah, content_mask
@ -503,13 +511,17 @@ core_pr_str:
; More inputs
mov rsi, [rsi + Cons.cdr] ; pointer
cmp rdi, 0 ; print_readably
je .end_append_char ; No separator if not printing readably
; Add separator
push rsi
mov rsi, r8
mov cl, ' '
call string_append_char
pop rsi
.end_append_char:
; Get the type in ah for comparison at start of loop
mov al, BYTE [rsi]
mov ah, al
@ -527,8 +539,11 @@ core_pr_str:
;; Print arguments readably, return nil
core_prn:
; Convert to string
call core_pr_str
jmp core_prn_functions
core_println:
call core_str
core_prn_functions:
mov rsi, rax
; Put newline at the end

View File

@ -17,6 +17,7 @@ section .data
section .text
;; Input: Address of object in RSI
;; print_readably in RDI. Set to zero for false
;;
;; Output: Address of string in RAX
;;
@ -41,6 +42,11 @@ pr_str:
; ---------------------------
; Handle string
cmp rdi, 0
je .string_not_readable
; printing readably, so escape characters
call string_new ; Output string in rax
@ -102,6 +108,12 @@ pr_str:
ret
.string_not_readable:
; Just return the string
call incref_object
mov rax, rsi
ret
; ----------------------------
.not_string:
; Now test the container type (value, list, map, vector)

View File

@ -184,6 +184,7 @@ error_throw:
; Print the object in RSI then quit
cmp rsi, 0
je .done ; nothing to print
mov rdi, 1 ; print_readably
call pr_str
mov rsi, rax
call print_string
@ -1454,7 +1455,8 @@ _start:
push rax ; Save result
; Put into pr_str
mov rsi, rax
mov rsi, rax
mov rdi, 1 ; print_readably
call pr_str
push rax ; Save output string
@ -1488,6 +1490,7 @@ _start:
; Check if an object was thrown
cmp rsi, 0
je .catch_done_print ; nothing to print
mov rdi, 1
call pr_str
mov rsi, rax
call print_string