Skip to content

Commit

Permalink
Hy: use dict instead of HyDict
Browse files Browse the repository at this point in the history
  • Loading branch information
kanaka committed Sep 22, 2017
1 parent 3cc7b87 commit 081c322
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 23 deletions.
13 changes: 10 additions & 3 deletions hy/core.hy
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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))
})
6 changes: 4 additions & 2 deletions hy/printer.hy
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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))))
4 changes: 2 additions & 2 deletions hy/reader.hy
Original file line number Diff line number Diff line change
@@ -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])
Expand Down Expand Up @@ -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)))

Expand Down
5 changes: 3 additions & 2 deletions hy/step2_eval.hy
Original file line number Diff line number Diff line change
@@ -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]])
Expand All @@ -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))
Expand Down
6 changes: 4 additions & 2 deletions hy/step3_env.hy
Original file line number Diff line number Diff line change
@@ -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]])
Expand All @@ -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))
Expand Down
6 changes: 4 additions & 2 deletions hy/step4_if_fn_do.hy
Original file line number Diff line number Diff line change
@@ -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]])
Expand All @@ -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))
Expand Down
6 changes: 4 additions & 2 deletions hy/step5_tco.hy
Original file line number Diff line number Diff line change
@@ -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]])
Expand All @@ -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))
Expand Down
6 changes: 4 additions & 2 deletions hy/step6_file.hy
Original file line number Diff line number Diff line change
@@ -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]])
Expand All @@ -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))
Expand Down
8 changes: 5 additions & 3 deletions hy/step7_quote.hy
Original file line number Diff line number Diff line change
@@ -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]])
Expand All @@ -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
Expand All @@ -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))
Expand Down
8 changes: 5 additions & 3 deletions hy/step8_macros.hy
Original file line number Diff line number Diff line change
@@ -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]])
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 081c322

Please sign in to comment.