1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 10:37:58 +03:00

Fix list and repl output

* (list) wasn't always incrementing reference counts when needed

* The value from eval of a file shouldn't be printed, so defined
  a small routine read_eval which doesn't print the final value.

Self-hosting now runs to step8, where two tests of the -> macro fail.
This commit is contained in:
Ben Dudson 2017-12-15 23:56:49 +00:00
parent dfefe35bd9
commit ccd081039e
2 changed files with 31 additions and 9 deletions

View File

@ -797,10 +797,6 @@ core_list:
call incref_object
mov rax, rsi
ret
.not_seq:
load_static core_list_not_seq
jmp core_throw_str
;; Convert arguments into a vector
core_vector:
@ -2432,8 +2428,14 @@ core_apply:
mov cl, al
and al, container_mask
cmp al, container_list
je .run
jne .last_convert_to_list
; Already a list, just increment reference count
mov rsi, r9
call incref_object
jmp .run
.last_convert_to_list:
; Convert vector to list by copying first element
call alloc_cons

View File

@ -80,7 +80,7 @@ section .data
;
;; Startup string. This is evaluated on startup
static mal_startup_string, db "(do (def! not (fn* (a) (if a false true))) (def! load-file (fn* (f) (eval (read-string (str ",34,"(do",34," (slurp f) ",34,")",34," ))))) (defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ",34,"odd number of forms to cond",34,")) (cons 'cond (rest (rest xs))))))) (def! *gensym-counter* (atom 0)) (def! gensym (fn* [] (symbol (str ",34,"G__",34," (swap! *gensym-counter* (fn* [x] (+ 1 x))))))) (defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs))))))))) (def! *host-language* ",34,"nasm",34,") )"
static mal_startup_string, db "(do (def! not (fn* (a) (if a false true))) (def! load-file (fn* (f) (eval (read-string (str ",34,"(do",34," (slurp f) ",34,")",34," ))))) (defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw ",34,"odd number of forms to cond",34,")) (cons 'cond (rest (rest xs))))))) (def! *gensym-counter* (atom 0)) (def! gensym (fn* [] (symbol (str ",34,"G__",34," (swap! *gensym-counter* (fn* [x] (+ 1 x))))))) (defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs))))))))) (def! *host-language* ",34,"nasm",34,") (def! conj nil) (def! seq nil) )"
;; Command to run, appending the name of the script to run
static run_script_string, db "(load-file ",34
@ -2471,6 +2471,24 @@ macroexpand:
.done:
pop r15
ret
;; Read and eval
read_eval:
; -------------
; Read
call read_str
; -------------
; Eval
mov rsi, rax ; Form to evaluate
mov rdi, [repl_env] ; Environment
xchg rsi, rdi
call incref_object ; Environment increment refs
xchg rsi, rdi ; since it will be decremented by eval
jmp eval ; This releases Env and Form/AST
;; Read-Eval-Print in sequence
;;
@ -2577,7 +2595,9 @@ _start:
push rax
mov rsi, rax
call rep_seq
call read_eval ; no print ('nil')
mov rsi, rax
call release_object ; Release result of eval
; Release the input string
pop rsi
@ -2697,7 +2717,7 @@ run_script:
mov cl, ')'
call string_append_char ; closing brace
; Read-Eval-Print "(load-file <file>)"
call rep_seq
; Read-Eval "(load-file <file>)"
call read_eval
jmp quit