Skip to content

Commit

Permalink
mal impl: stop returning a value from env-set
Browse files Browse the repository at this point in the history
This improves readability because the return value was only a trick to
shorten `def!` and `defmacro!` and was ignored everywhere else.

As a side effect, this probably improves efficiency, as `let*` is the
most frequent caller.
  • Loading branch information
asarhaddon authored and kanaka committed Aug 7, 2024
1 parent af2279d commit 2304fea
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 13 deletions.
5 changes: 2 additions & 3 deletions impls/mal/env.mal
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
(get @e ks)
(throw (str "'" ks "' not found"))))))

;; The return value must be ignored.
(def! env-set (fn* [env k v]
(do
(swap! env assoc (str k) v)
v)))
(swap! env assoc (str k) v)))
5 changes: 4 additions & 1 deletion impls/mal/step3_env.mal
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
(let* [a0 (first ast)]
(cond
(= 'def! a0)
(env-set env (nth ast 1) (EVAL (nth ast 2) env))
(let* [val (EVAL (nth ast 2) env)]
(do
(env-set env (nth ast 1) val)
val))

(= 'let* a0)
(LET (new-env env) (nth ast 1) (nth ast 2))
Expand Down
5 changes: 4 additions & 1 deletion impls/mal/step4_if_fn_do.mal
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
(let* [a0 (first ast)]
(cond
(= 'def! a0)
(env-set env (nth ast 1) (EVAL (nth ast 2) env))
(let* [val (EVAL (nth ast 2) env)]
(do
(env-set env (nth ast 1) val)
val))

(= 'let* a0)
(LET (new-env env) (nth ast 1) (nth ast 2))
Expand Down
5 changes: 4 additions & 1 deletion impls/mal/step6_file.mal
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
(let* [a0 (first ast)]
(cond
(= 'def! a0)
(env-set env (nth ast 1) (EVAL (nth ast 2) env))
(let* [val (EVAL (nth ast 2) env)]
(do
(env-set env (nth ast 1) val)
val))

(= 'let* a0)
(LET (new-env env) (nth ast 1) (nth ast 2))
Expand Down
5 changes: 4 additions & 1 deletion impls/mal/step7_quote.mal
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
(let* [a0 (first ast)]
(cond
(= 'def! a0)
(env-set env (nth ast 1) (EVAL (nth ast 2) env))
(let* [val (EVAL (nth ast 2) env)]
(do
(env-set env (nth ast 1) val)
val))

(= 'let* a0)
(LET (new-env env) (nth ast 1) (nth ast 2))
Expand Down
10 changes: 8 additions & 2 deletions impls/mal/step8_macros.mal
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
(let* [a0 (first ast)]
(cond
(= 'def! a0)
(env-set env (nth ast 1) (EVAL (nth ast 2) env))
(let* [val (EVAL (nth ast 2) env)]
(do
(env-set env (nth ast 1) val)
val))

(= 'let* a0)
(LET (new-env env) (nth ast 1) (nth ast 2))
Expand All @@ -69,7 +72,10 @@
(EVAL (QUASIQUOTE (nth ast 1)) env)

(= 'defmacro! a0)
(env-set env (nth ast 1) (defmacro! _ (EVAL (nth ast 2) env)))
(let* [fun (defmacro! _ (EVAL (nth ast 2) env))]
(do
(env-set env (nth ast 1) fun)
fun))

(= 'do a0)
(nth (map (fn* [exp] (EVAL exp env)) (rest ast)) (- (count ast) 2))
Expand Down
10 changes: 8 additions & 2 deletions impls/mal/step9_try.mal
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
(let* [a0 (first ast)]
(cond
(= 'def! a0)
(env-set env (nth ast 1) (EVAL (nth ast 2) env))
(let* [val (EVAL (nth ast 2) env)]
(do
(env-set env (nth ast 1) val)
val))

(= 'let* a0)
(LET (new-env env) (nth ast 1) (nth ast 2))
Expand All @@ -69,7 +72,10 @@
(EVAL (QUASIQUOTE (nth ast 1)) env)

(= 'defmacro! a0)
(env-set env (nth ast 1) (defmacro! _ (EVAL (nth ast 2) env)))
(let* [fun (defmacro! _ (EVAL (nth ast 2) env))]
(do
(env-set env (nth ast 1) fun)
fun))

(= 'try* a0)
(if (< (count ast) 3)
Expand Down
10 changes: 8 additions & 2 deletions impls/mal/stepA_mal.mal
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
(let* [a0 (first ast)]
(cond
(= 'def! a0)
(env-set env (nth ast 1) (EVAL (nth ast 2) env))
(let* [val (EVAL (nth ast 2) env)]
(do
(env-set env (nth ast 1) val)
val))

(= 'let* a0)
(LET (new-env env) (nth ast 1) (nth ast 2))
Expand All @@ -69,7 +72,10 @@
(EVAL (QUASIQUOTE (nth ast 1)) env)

(= 'defmacro! a0)
(env-set env (nth ast 1) (defmacro! _ (EVAL (nth ast 2) env)))
(let* [fun (defmacro! _ (EVAL (nth ast 2) env))]
(do
(env-set env (nth ast 1) fun)
fun))

(= 'try* a0)
(if (< (count ast) 3)
Expand Down

0 comments on commit 2304fea

Please sign in to comment.