diff --git a/mal/core.mal b/mal/core.mal index 87649951..aa9137cd 100644 --- a/mal/core.mal +++ b/mal/core.mal @@ -1,26 +1,12 @@ -(def! _macro_magic - :_I_tell_ya_this_is_a_MAL_macro_mark_my_words) - -(def! _macro_wrap (fn* [f] - [_macro_magic f])) - -(def! _macro_unwrap (fn* [x] - (if (vector? x) - (if (= (first x) _macro_magic) - (nth x 1))))) - -(def! _macro? (fn* [x] - (if (_macro_unwrap x) - true +(def! _map? (fn* [x] + (if (map? x) + (not (= (keys x) '(:__MAL_MACRO__))) false))) -(def! false_on_macro (fn* [f] - (fn* [x] - (if (_macro_unwrap x) - false - (f x))))) -(def! _sequential? (false_on_macro sequential?)) -(def! _vector? (false_on_macro vector?)) +(def! _macro? (fn* [x] + (if (map? x) + (= (keys x) '(:__MAL_MACRO__)) + false))) (def! core_ns [['= =] @@ -57,9 +43,9 @@ ['list list] ['list? list?] ['vector vector] - ['vector? _vector?] + ['vector? vector?] ['hash-map hash-map] - ['map? map?] + ['map? _map?] ['assoc assoc] ['dissoc dissoc] ['get get] @@ -67,7 +53,7 @@ ['keys keys] ['vals vals] - ['sequential? _sequential?] + ['sequential? sequential?] ['cons cons] ['concat concat] ['nth nth] diff --git a/mal/step8_macros.mal b/mal/step8_macros.mal index 4d9c5092..0018d4c7 100644 --- a/mal/step8_macros.mal +++ b/mal/step8_macros.mal @@ -29,7 +29,9 @@ (let* [a0 (first ast)] (if (symbol? a0) (if (env-find env a0) - (_macro_unwrap (env-get env a0)))))))) + (let* [m (env-get env a0)] + (if (_macro? m) + (get m :__MAL_MACRO__))))))))) (def! MACROEXPAND (fn* [ast env] (let* [m (is-macro-call ast env)] @@ -44,9 +46,9 @@ (list? ast) (map (fn* [exp] (EVAL exp env)) ast) - (_vector? ast) (apply vector (map (fn* [exp] (EVAL exp env)) ast)) + (vector? ast) (apply vector (map (fn* [exp] (EVAL exp env)) ast)) - (map? ast) (apply hash-map + (_map? ast) (apply hash-map (apply concat (map (fn* [k] [k (EVAL (get ast k) env)]) (keys ast)))) @@ -85,7 +87,7 @@ (EVAL (QUASIQUOTE (nth ast 1)) env) (= 'defmacro! a0) - (env-set env (nth ast 1) (_macro_wrap (EVAL (nth ast 2) env))) + (env-set env (nth ast 1) {:__MAL_MACRO__ (EVAL (nth ast 2) env)}) (= 'macroexpand a0) (MACROEXPAND (nth ast 1) env) @@ -109,8 +111,7 @@ ;; print -(def! PRINT (fn* [x] - (pr-str (let* [m (_macro_unwrap x)] (if m m x))))) +(def! PRINT pr-str) ;; repl (def! repl-env (new-env)) diff --git a/mal/step9_try.mal b/mal/step9_try.mal index b744f1a0..676e93e7 100644 --- a/mal/step9_try.mal +++ b/mal/step9_try.mal @@ -29,7 +29,9 @@ (let* [a0 (first ast)] (if (symbol? a0) (if (env-find env a0) - (_macro_unwrap (env-get env a0)))))))) + (let* [m (env-get env a0)] + (if (_macro? m) + (get m :__MAL_MACRO__))))))))) (def! MACROEXPAND (fn* [ast env] (let* [m (is-macro-call ast env)] @@ -44,9 +46,9 @@ (list? ast) (map (fn* [exp] (EVAL exp env)) ast) - (_vector? ast) (apply vector (map (fn* [exp] (EVAL exp env)) ast)) + (vector? ast) (apply vector (map (fn* [exp] (EVAL exp env)) ast)) - (map? ast) (apply hash-map + (_map? ast) (apply hash-map (apply concat (map (fn* [k] [k (EVAL (get ast k) env)]) (keys ast)))) @@ -85,7 +87,7 @@ (EVAL (QUASIQUOTE (nth ast 1)) env) (= 'defmacro! a0) - (env-set env (nth ast 1) (_macro_wrap (EVAL (nth ast 2) env))) + (env-set env (nth ast 1) {:__MAL_MACRO__ (EVAL (nth ast 2) env)}) (= 'macroexpand a0) (MACROEXPAND (nth ast 1) env) @@ -118,8 +120,7 @@ ;; print -(def! PRINT (fn* [x] - (pr-str (let* [m (_macro_unwrap x)] (if m m x))))) +(def! PRINT pr-str) ;; repl (def! repl-env (new-env)) diff --git a/mal/stepA_mal.mal b/mal/stepA_mal.mal index ad75a70f..26feecf2 100644 --- a/mal/stepA_mal.mal +++ b/mal/stepA_mal.mal @@ -29,7 +29,9 @@ (let* [a0 (first ast)] (if (symbol? a0) (if (env-find env a0) - (_macro_unwrap (env-get env a0)))))))) + (let* [m (env-get env a0)] + (if (_macro? m) + (get m :__MAL_MACRO__))))))))) (def! MACROEXPAND (fn* [ast env] (let* [m (is-macro-call ast env)] @@ -44,9 +46,9 @@ (list? ast) (map (fn* [exp] (EVAL exp env)) ast) - (_vector? ast) (apply vector (map (fn* [exp] (EVAL exp env)) ast)) + (vector? ast) (apply vector (map (fn* [exp] (EVAL exp env)) ast)) - (map? ast) (apply hash-map + (_map? ast) (apply hash-map (apply concat (map (fn* [k] [k (EVAL (get ast k) env)]) (keys ast)))) @@ -85,7 +87,7 @@ (EVAL (QUASIQUOTE (nth ast 1)) env) (= 'defmacro! a0) - (env-set env (nth ast 1) (_macro_wrap (EVAL (nth ast 2) env))) + (env-set env (nth ast 1) {:__MAL_MACRO__ (EVAL (nth ast 2) env)}) (= 'macroexpand a0) (MACROEXPAND (nth ast 1) env) @@ -118,8 +120,7 @@ ;; print -(def! PRINT (fn* [x] - (pr-str (let* [m (_macro_unwrap x)] (if m m x))))) +(def! PRINT pr-str) ;; repl (def! repl-env (new-env)) diff --git a/tests/step9_try.mal b/tests/step9_try.mal index 9e39ba16..5f8c606b 100644 --- a/tests/step9_try.mal +++ b/tests/step9_try.mal @@ -177,6 +177,7 @@ (map? :abc) ;=>false + ;; ;; Testing hash-maps (hash-map "a" 1) @@ -377,3 +378,5 @@ (= [] {}) ;=>false +(map? cond) +;=>false diff --git a/tests/stepA_mal.mal b/tests/stepA_mal.mal index 9ea1cb5f..6056315b 100644 --- a/tests/stepA_mal.mal +++ b/tests/stepA_mal.mal @@ -163,6 +163,8 @@ ;=>false (macro? :+) ;=>false +(macro? {}) +;=>false ;;