Skip to content

Commit

Permalink
Fix list and repl output
Browse files Browse the repository at this point in the history
* (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.
  • Loading branch information
bendudson committed Dec 15, 2017
1 parent dfefe35 commit ccd0810
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
12 changes: 7 additions & 5 deletions nasm/core.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
28 changes: 24 additions & 4 deletions nasm/stepA_mal.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
;;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit ccd0810

Please sign in to comment.