diff --git a/stages/lisp0/lisp.asm b/stages/lisp0/lisp.asm index 7fbd565..6c616e9 100644 --- a/stages/lisp0/lisp.asm +++ b/stages/lisp0/lisp.asm @@ -387,6 +387,19 @@ p_eq: lea rsi, [rel atoms] cmovz rax, [rsi + ATOM_TRUE * 8] ; if no pair is unequal, return "t", else return nil ret + +make_num: + push rdi + mov rdi, 16 + mov rsi, 8 + call alloc + mov dword [rax], 1 ; refcount = 1 + pop rdi + mov qword [rax + 8], rdi ; value = number + mov rdi, rax + mov rsi, OBJ_NUM + call obj_set_tag + ret ;; sum list $rdi in env $rsi p_add: @@ -394,14 +407,8 @@ p_add: mov qword [rsp + 8], rsi ; env call eval_list mov qword [rsp], rax ; evaled list - mov rdi, 16 - mov rsi, 8 - call alloc - mov rdi, rax - mov dword [rdi], 1 ; refcount = 1 - mov qword [rdi + 8], 0 ; value = 0 - mov rsi, OBJ_NUM - call obj_set_tag + mov rdi, 0 + call make_num mov rcx, rax ; acc mov rdi, qword [rsp] ; $rdi = evaled list mov rsi, qword [rsp + 8] ; $rsi = env @@ -415,14 +422,8 @@ p_sub: mov qword [rsp + 8], rsi ; env call eval_list mov qword [rsp], rax ; evaled list - mov rdi, 16 - mov rsi, 8 - call alloc - mov rdi, rax - mov dword [rdi], 1 ; refcount = 1 - mov qword [rdi + 8], 0 ; value = 0 - mov rsi, OBJ_NUM - call obj_set_tag + mov rdi, 0 + call make_num mov rcx, rax ; acc mov rdi, qword [rsp] ; $rdi = evaled list mov rsi, qword [rsp + 8] ; $rsi = env @@ -436,14 +437,8 @@ p_mul: mov qword [rsp + 8], rsi ; env call eval_list mov qword [rsp], rax ; evaled list - mov rdi, 16 - mov rsi, 8 - call alloc - mov rdi, rax - mov dword [rdi], 1 ; refcount = 1 - mov qword [rdi + 8], 1 ; value = 1 - mov rsi, OBJ_NUM - call obj_set_tag + mov rdi, 1 + call make_num mov rcx, rax ; acc mov rdi, qword [rsp] ; $rdi = evaled list mov rsi, qword [rsp + 8] ; $rsi = env @@ -479,17 +474,13 @@ p_rem: p_car: call eval_list mov rdi, rax - call get_car - mov rdi, rax - call get_car + call get_caar ret p_cdr: call eval_list mov rdi, rax - call get_car - mov rdi, rax - call get_cdr + call get_cadr ret p_not: @@ -608,9 +599,7 @@ p_let_star_inner: mov qword [rsp], rdi ; acc-env mov qword [rsp + 8], rsi ; (var val) mov rdi, rsi - call get_cdr - mov rdi, rax - call get_car + call get_cdar mov rdi, rax mov rsi, qword [rsp] ; acc-env call eval @@ -630,9 +619,7 @@ p_let: sub rsp, 24 call get_car mov qword [rsp], rax ; bindings - call get_cdr - mov rdi, rax - call get_car + call get_cdar mov qword [rsp + 8], rax ; body mov qword [rsp + 16], rsi ; in_env mov rdi, qword [rsp] ; $rdi = bindings @@ -650,9 +637,7 @@ p_let_star: sub rsp, 24 call get_car mov qword [rsp], rax ; bindings - call get_cdr - mov rdi, rax - call get_car + call get_cdar mov qword [rsp + 8], rax ; body mov qword [rsp + 16], rsi ; in_env mov rdi, qword [rsp] ; $rdi = bindings @@ -700,6 +685,8 @@ p_str_concat: p_str_append: ;; return the nth element of a string p_str_nth: +;; sets the nth element of a string to a char +p_set_nth: ;; returns a new string that is a substring from a a+b of the input string ;; (substr "hello world" 0 5) -> "hello" p_str_substr: