kanaka / mal

mal - Make a Lisp
Other
10k stars 2.53k forks source link

Should `eval` use the top-level environment? #652

Closed jcguu95 closed 5 months ago

jcguu95 commented 5 months ago

Thanks for creating mal!

I'm playing with your python implementation. I'm a bit confused with the following behavior.

(do 
  (def! x 0) 
  (let* (x 10000) 
    (eval 'x))) ; => 0

In the python implementation stepA, eval always refers to the global environment repl_env. The source code is the following.

repl_env.set(types._symbol('eval'), lambda ast: EVAL(ast, repl_env))

QUESTION Is this intended behavior? In both emacs-lisp and common-lisp, the equivalent code yields 100.

(progn 
  (defvar x) 
  (setf x 0) 
  (let ((x 10000)) 
    (eval 'x))) 
; => 10000
jcguu95 commented 5 months ago

Sorry, I overlooked. It's specified in one test tests/step6_file.mal.

;; Checking that eval does not use local environments.
(def! a 1)
;=>1
(let* (a 2) (eval (read-string "a")))
;=>1

I wonder why this design though.