diff --git a/hy/core.hy b/hy/core.hy index 0406b8e03e..1b6e5327c9 100644 --- a/hy/core.hy +++ b/hy/core.hy @@ -3,10 +3,16 @@ (import [reader [read-str]]) (import [printer [pr-str]]) +(defn sequential? [a] + (or (instance? tuple a) (instance? list a))) + (defn equal [a b] - (if (and (coll? a) (coll? b) (= (len a) (len b))) + (if (and (sequential? a) (sequential? b) (= (len a) (len b))) (every? (fn [[a b]] (equal a b)) (zip a b)) + (and (instance? dict a) (instance? dict b) (= (.keys a) (.keys b))) + (every? (fn [k] (and (equal (get a k) (get b k)))) a) + (= (type a) (type b)) (= a b) @@ -41,11 +47,12 @@ "rest" (fn [a] (if (none? a) (,) (tuple (rest a)))) "empty?" empty? "count" (fn [a] (if (none? a) 0 (len a))) + "apply" (fn [f &rest a] (apply f (+ (list (butlast a)) (list (last a))))) + "map" (fn [f a] (tuple (map f a))) "atom" (fn [a] (Atom a)) "atom?" (fn [a] (instance? Atom a)) "deref" (fn [a] a.val) "reset!" (fn [a b] (do (setv a.val b) b)) - "swap!" (fn [a f &rest args] (do (setv a.val (apply f (+ (, a.val) args))) - a.val)) + "swap!" (fn [a f &rest xs] (do (setv a.val (apply f (+ (, a.val) xs))) a.val)) }) diff --git a/hy/printer.hy b/hy/printer.hy index 1e63484810..55809fc195 100644 --- a/hy/printer.hy +++ b/hy/printer.hy @@ -1,5 +1,5 @@ (import [hy.models [HyInteger :as Int HyKeyword :as Keyword - HyString :as Str HySymbol :as Sym HyDict :as Map]]) + HyString :as Str HySymbol :as Sym]]) (import [mal_types [Atom]]) (defn escape [s] @@ -18,6 +18,8 @@ (= t Str) (if _r (+ "\"" (escape obj) "\"") obj) (= t tuple) (+ "(" (.join " " (map (fn [x] (pr-str x _r)) obj)) ")") (= t list) (+ "[" (.join " " (map (fn [x] (pr-str x _r)) obj)) "]") - (= t Map) (+ "{" (.join " " (map (fn [x] (pr-str x _r)) obj)) "}") + (= t dict) (+ "{" (.join " " (map (fn [k] (+ (pr-str k _r) " " + (pr-str (get obj k) _r))) + obj)) "}") (instance? Atom obj) (+ "(atom " (pr-str obj.val _r) ")") True (str obj)))) diff --git a/hy/reader.hy b/hy/reader.hy index 55010dc5b5..41abec29d3 100644 --- a/hy/reader.hy +++ b/hy/reader.hy @@ -1,5 +1,5 @@ (import [hy.models [HyInteger :as Int HyKeyword :as Keyword - HyString :as Str HySymbol :as Sym HyDict :as Map]] + HyString :as Str HySymbol :as Sym]] [re]) (defclass Blank [Exception]) @@ -82,7 +82,7 @@ (= "[" token) (read-seq rdr "[" "]") (= "}" token) (raise (Exception "unexpected '}'")) - (= "{" token) (Map (read-seq rdr "{" "}")) + (= "{" token) (dict (partition (read-seq rdr "{" "}") 2)) True (read-atom rdr))) diff --git a/hy/step2_eval.hy b/hy/step2_eval.hy index 2b7fd9a431..d6b407b52c 100755 --- a/hy/step2_eval.hy +++ b/hy/step2_eval.hy @@ -1,6 +1,5 @@ #!/usr/bin/env hy -(import [hy.models [HyDict :as Map]]) (import sys traceback) (import [reader [read-str Blank]]) (import [printer [pr-str]]) @@ -14,7 +13,9 @@ (if (symbol? ast) (if (.has_key env ast) (get env ast) (raise (Exception (+ ast " not found")))) - (instance? Map ast) (Map (map (fn [x] (EVAL x env)) ast)) + (instance? dict ast) (dict (map (fn [k] + [(EVAL k env) (EVAL (get ast k) env)]) + ast)) (instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast)) (instance? list ast) (list (map (fn [x] (EVAL x env)) ast)) True ast)) diff --git a/hy/step3_env.hy b/hy/step3_env.hy index 31393cef53..95dc3fbbb2 100755 --- a/hy/step3_env.hy +++ b/hy/step3_env.hy @@ -1,6 +1,6 @@ #!/usr/bin/env hy -(import [hy.models [HyDict :as Map HySymbol :as Sym]]) +(import [hy.models [HySymbol :as Sym]]) (import sys traceback) (import [reader [read-str Blank]]) (import [printer [pr-str]]) @@ -15,7 +15,9 @@ ;;(print "eval-ast:" ast (type ast)) (if (symbol? ast) (env-get env ast) - (instance? Map ast) (Map (map (fn [x] (EVAL x env)) ast)) + (instance? dict ast) (dict (map (fn [k] + [(EVAL k env) (EVAL (get ast k) env)]) + ast)) (instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast)) (instance? list ast) (list (map (fn [x] (EVAL x env)) ast)) True ast)) diff --git a/hy/step4_if_fn_do.hy b/hy/step4_if_fn_do.hy index 2c8db51871..bf9c6ba737 100755 --- a/hy/step4_if_fn_do.hy +++ b/hy/step4_if_fn_do.hy @@ -1,6 +1,6 @@ #!/usr/bin/env hy -(import [hy.models [HyDict :as Map HySymbol :as Sym]]) +(import [hy.models [HySymbol :as Sym]]) (import sys traceback) (import [reader [read-str Blank]]) (import [printer [pr-str]]) @@ -16,7 +16,9 @@ ;;(print "eval-ast:" ast (type ast)) (if (symbol? ast) (env-get env ast) - (instance? Map ast) (Map (map (fn [x] (EVAL x env)) ast)) + (instance? dict ast) (dict (map (fn [k] + [(EVAL k env) (EVAL (get ast k) env)]) + ast)) (instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast)) (instance? list ast) (list (map (fn [x] (EVAL x env)) ast)) True ast)) diff --git a/hy/step5_tco.hy b/hy/step5_tco.hy index ebc58fa6b6..6b4ee8fafa 100755 --- a/hy/step5_tco.hy +++ b/hy/step5_tco.hy @@ -1,6 +1,6 @@ #!/usr/bin/env hy -(import [hy.models [HyDict :as Map HySymbol :as Sym]]) +(import [hy.models [HySymbol :as Sym]]) (import sys traceback) (import [reader [read-str Blank]]) (import [printer [pr-str]]) @@ -16,7 +16,9 @@ ;;(print "eval-ast:" ast (type ast)) (if (symbol? ast) (env-get env ast) - (instance? Map ast) (Map (map (fn [x] (EVAL x env)) ast)) + (instance? dict ast) (dict (map (fn [k] + [(EVAL k env) (EVAL (get ast k) env)]) + ast)) (instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast)) (instance? list ast) (list (map (fn [x] (EVAL x env)) ast)) True ast)) diff --git a/hy/step6_file.hy b/hy/step6_file.hy index 735842cc5b..680a347928 100755 --- a/hy/step6_file.hy +++ b/hy/step6_file.hy @@ -1,6 +1,6 @@ #!/usr/bin/env hy -(import [hy.models [HyDict :as Map HyString :as Str HySymbol :as Sym]]) +(import [hy.models [HyString :as Str HySymbol :as Sym]]) (import sys traceback) (import [reader [read-str Blank]]) (import [printer [pr-str]]) @@ -16,7 +16,9 @@ ;;(print "eval-ast:" ast (type ast)) (if (symbol? ast) (env-get env ast) - (instance? Map ast) (Map (map (fn [x] (EVAL x env)) ast)) + (instance? dict ast) (dict (map (fn [k] + [(EVAL k env) (EVAL (get ast k) env)]) + ast)) (instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast)) (instance? list ast) (list (map (fn [x] (EVAL x env)) ast)) True ast)) diff --git a/hy/step7_quote.hy b/hy/step7_quote.hy index 74150467f8..17e6db7f1d 100755 --- a/hy/step7_quote.hy +++ b/hy/step7_quote.hy @@ -1,6 +1,6 @@ #!/usr/bin/env hy -(import [hy.models [HyDict :as Map HyString :as Str HySymbol :as Sym]]) +(import [hy.models [HyString :as Str HySymbol :as Sym]]) (import sys traceback) (import [reader [read-str Blank]]) (import [printer [pr-str]]) @@ -13,7 +13,7 @@ ;; eval (defn pair? [x] - (and (coll? x) (> (len x) 0))) + (and (core.sequential? x) (> (len x) 0))) (defn QUASIQUOTE [ast] (if @@ -34,7 +34,9 @@ ;;(print "eval-ast:" ast (type ast)) (if (symbol? ast) (env-get env ast) - (instance? Map ast) (Map (map (fn [x] (EVAL x env)) ast)) + (instance? dict ast) (dict (map (fn [k] + [(EVAL k env) (EVAL (get ast k) env)]) + ast)) (instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast)) (instance? list ast) (list (map (fn [x] (EVAL x env)) ast)) True ast)) diff --git a/hy/step8_macros.hy b/hy/step8_macros.hy index 5de0cc6254..740c751b80 100755 --- a/hy/step8_macros.hy +++ b/hy/step8_macros.hy @@ -1,6 +1,6 @@ #!/usr/bin/env hy -(import [hy.models [HyDict :as Map HyString :as Str HySymbol :as Sym]]) +(import [hy.models [HyString :as Str HySymbol :as Sym]]) (import sys traceback) (import [reader [read-str Blank]]) (import [printer [pr-str]]) @@ -13,7 +13,7 @@ ;; eval (defn pair? [x] - (and (coll? x) (> (len x) 0))) + (and (core.sequential? x) (> (len x) 0))) (defn QUASIQUOTE [ast] (if @@ -50,7 +50,9 @@ ;;(print "eval-ast:" ast (type ast)) (if (symbol? ast) (env-get env ast) - (instance? Map ast) (Map (map (fn [x] (EVAL x env)) ast)) + (instance? dict ast) (dict (map (fn [k] + [(EVAL k env) (EVAL (get ast k) env)]) + ast)) (instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast)) (instance? list ast) (list (map (fn [x] (EVAL x env)) ast)) True ast))