3 (import [hy.models [HySymbol :as Sym]])
5 (import [mal_types [MalException]])
6 (import [reader [read-str Blank]])
7 (import [printer [pr-str]])
8 (import [env [env-new env-get env-set]])
16 (defn eval-ast [ast env]
17 ;;(print "eval-ast:" ast (type ast))
19 (symbol? ast) (env-get env ast)
20 (instance? dict ast) (dict (map (fn [k]
21 [(EVAL k env) (EVAL (get ast k) env)])
23 (instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast))
24 (instance? list ast) (list (map (fn [x] (EVAL x env)) ast))
28 ;;(print "EVAL:" ast (type ast))
29 ;; indented to match later steps
30 (if (not (instance? tuple ast))
35 (setv [a0 a1 a2] [(nth ast 0) (nth ast 1) (nth ast 2)])
41 (env-set env a1 (EVAL a2 env))
45 (setv env (env-new env))
46 (for [[b e] (partition a1 2)]
47 (env-set env b (EVAL e env)))
51 (last (eval-ast (list (rest ast)) env))
55 (setv cond (EVAL a1 env))
56 (if (or (none? cond) (and (instance? bool cond)
59 (EVAL (nth ast 3) env)
65 (EVAL a2 (env-new env a1 (or args []))))
69 (setv el (eval-ast ast env)
71 args (list (rest el)))
79 (def repl-env (env-new))
81 (PRINT (EVAL (READ str) repl-env)))
83 ;; core.hy: defined using Hy
85 (env-set repl-env (Sym k) (get core.ns k)))
87 ;; core.mal: defined using the language itself
88 (REP "(def! not (fn* [a] (if a false true)))")
91 ;; indented to match later steps
94 (do (setv line (raw_input "user> "))
95 (if (= "" line) (continue))
97 (except [EOFError] (break))
100 (setv msg (.rstrip (.join "" (apply traceback.format_exception
102 (if (instance? MalException e)
103 (setv msg (+ (.rstrip msg) ": " (pr-str e.val True))))