Skip to content

Commit

Permalink
Hy: simplify main eval loop. Dockerfile cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
kanaka committed Sep 22, 2017
1 parent bf794d1 commit 1872f73
Show file tree
Hide file tree
Showing 10 changed files with 485 additions and 475 deletions.
4 changes: 2 additions & 2 deletions hy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ WORKDIR /mal
##########################################################

# Hy
RUN apt-get -y install python-pip
RUN pip install hy && \
RUN apt-get -y install python-pip && \
pip install hy && \
mkdir /.cache && \
chmod uog+rwx /.cache
26 changes: 13 additions & 13 deletions hy/step2_eval.hy
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
True ast))

(defn EVAL [ast env]
(if (not (instance? tuple ast))
(eval-ast ast env)
;; indented to match later steps
(if (not (instance? tuple ast))
(eval-ast ast env)

;; apply list
;; indented to match later steps
(if
(empty? ast)
ast
;; apply list
(if
(empty? ast)
ast

;; apply
(do
(setv el (eval-ast ast env)
f (first el)
args (rest el))
(apply f args)))))
;; apply
(do
(setv el (eval-ast ast env)
f (first el)
args (list (rest el)))
(apply f args)))))

;; print
(defn PRINT [exp]
Expand Down
48 changes: 24 additions & 24 deletions hy/step3_env.hy
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,34 @@
True ast))

(defn EVAL [ast env]
;;(print "EVAL:" ast (type ast) (instance? tuple ast))
(if (not (instance? tuple ast))
(eval-ast ast env)
;;(print "EVAL:" ast (type ast))
;; indented to match later steps
(if (not (instance? tuple ast))
(eval-ast ast env)

;; apply list
;; indented to match later steps
(do
(setv [a0 a1 a2] [(nth ast 0) (nth ast 1) (nth ast 2)])
(if
(none? a0)
ast
;; apply list
(do
(setv [a0 a1 a2] [(nth ast 0) (nth ast 1) (nth ast 2)])
(if
(none? a0)
ast

(= (Sym "def!") a0)
(env-set env a1 (EVAL a2 env))
(= (Sym "def!") a0)
(env-set env a1 (EVAL a2 env))

(= (Sym "let*") a0)
(do
(setv env (env-new env))
(for [[b e] (partition a1 2)]
(env-set env b (EVAL e env)))
(EVAL a2 env))
(= (Sym "let*") a0)
(do
(setv env (env-new env))
(for [[b e] (partition a1 2)]
(env-set env b (EVAL e env)))
(EVAL a2 env))

;; apply
(do
(setv el (eval-ast ast env)
f (first el)
args (rest el))
(apply f args))))))
;; apply
(do
(setv el (eval-ast ast env)
f (first el)
args (list (rest el)))
(apply f args))))))

;; print
(defn PRINT [exp]
Expand Down
90 changes: 45 additions & 45 deletions hy/step4_if_fn_do.hy
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,51 @@
True ast))

(defn EVAL [ast env]
;;(print "EVAL:" ast (type ast) (instance? tuple ast))
(if (not (instance? tuple ast))
(eval-ast ast env)

;; apply list
;; indented to match later steps
(do
(setv [a0 a1 a2] [(nth ast 0) (nth ast 1) (nth ast 2)])
(if
(none? a0)
ast

(= (Sym "def!") a0)
(env-set env a1 (EVAL a2 env))

(= (Sym "let*") a0)
(do
(setv env (env-new env))
(for [[b e] (partition a1 2)]
(env-set env b (EVAL e env)))
(EVAL a2 env))

(= (Sym "do") a0)
(last (eval-ast (list (rest ast)) env))

(= (Sym "if") a0)
(do
(setv cond (EVAL a1 env))
(if (or (none? cond) (and (instance? bool cond)
(= cond False)))
(if (> (len ast) 2)
(EVAL (nth ast 3) env)
None)
(EVAL a2 env)))

(= (Sym "fn*") a0)
(fn [&rest args]
(EVAL a2 (env-new env a1 (or args []))))

;; apply
(do
(setv el (eval-ast ast env)
f (first el)
args (list (rest el)))
(apply f args))))))
;;(print "EVAL:" ast (type ast))
;; indented to match later steps
(if (not (instance? tuple ast))
(eval-ast ast env)

;; apply list
(do
(setv [a0 a1 a2] [(nth ast 0) (nth ast 1) (nth ast 2)])
(if
(none? a0)
ast

(= (Sym "def!") a0)
(env-set env a1 (EVAL a2 env))

(= (Sym "let*") a0)
(do
(setv env (env-new env))
(for [[b e] (partition a1 2)]
(env-set env b (EVAL e env)))
(EVAL a2 env))

(= (Sym "do") a0)
(last (eval-ast (list (rest ast)) env))

(= (Sym "if") a0)
(do
(setv cond (EVAL a1 env))
(if (or (none? cond) (and (instance? bool cond)
(= cond False)))
(if (> (len ast) 2)
(EVAL (nth ast 3) env)
None)
(EVAL a2 env)))

(= (Sym "fn*") a0)
(fn [&rest args]
(EVAL a2 (env-new env a1 (or args []))))

;; apply
(do
(setv el (eval-ast ast env)
f (first el)
args (list (rest el)))
(apply f args))))))

;; print
(defn PRINT [exp]
Expand Down
110 changes: 56 additions & 54 deletions hy/step5_tco.hy
Original file line number Diff line number Diff line change
Expand Up @@ -24,64 +24,66 @@
True ast))

(defn EVAL [ast env]
;;(print "EVAL:" ast (type ast) (instance? tuple ast))
;;(print "EVAL:" ast (type ast))
;; indented to match later steps
(setv res None)
(while True
(if (not (instance? tuple ast))
(setv res (eval-ast ast env))

;; apply list
;; indented to match later steps
(do
(setv [a0 a1 a2] [(nth ast 0) (nth ast 1) (nth ast 2)])
(if
(none? a0)
(setv res ast)

(= (Sym "def!") a0)
(setv res (env-set env a1 (EVAL a2 env)))

(= (Sym "let*") a0)
(do
(setv env (env-new env))
(for [[b e] (partition a1 2)]
(env-set env b (EVAL e env)))
(setv ast a2)
(continue)) ;; TCO

(= (Sym "do") a0)
(do (eval-ast (list (butlast (rest ast))) env)
(setv ast (last ast))
(setv res
(if (not (instance? tuple ast))
(eval-ast ast env)

;; apply list
(do
(setv [a0 a1 a2] [(nth ast 0) (nth ast 1) (nth ast 2)])
(if
(none? a0)
ast

(= (Sym "def!") a0)
(env-set env a1 (EVAL a2 env))

(= (Sym "let*") a0)
(do
(setv env (env-new env))
(for [[b e] (partition a1 2)]
(env-set env b (EVAL e env)))
(setv ast a2)
(continue)) ;; TCO

(= (Sym "if") a0)
(do
(setv cond (EVAL a1 env))
(if (or (none? cond) (and (instance? bool cond)
(= cond False)))
(if (> (len ast) 2)
(do (setv ast (nth ast 3)) (continue)) ;; TCO
(setv res None))
(do (setv ast a2) (continue)))) ;; TCO

(= (Sym "fn*") a0)
(setv func (fn [&rest args]
(EVAL a2 (env-new env a1 (or args []))))
func.ast a2
func.env env
func.params a1
res func)

;; apply
(do
(setv el (eval-ast ast env)
f (first el)
args (list (rest el)))
(if (hasattr f "ast")
(do (setv ast f.ast
env (env-new f.env f.params args))
(continue)) ;; TCO
(setv res (apply f args)))))))
(= (Sym "do") a0)
(do (eval-ast (list (butlast (rest ast))) env)
(setv ast (last ast))
(continue)) ;; TCO

(= (Sym "if") a0)
(do
(setv cond (EVAL a1 env))
(if (or (none? cond) (and (instance? bool cond)
(= cond False)))
(if (> (len ast) 2)
(do (setv ast (nth ast 3)) (continue)) ;; TCO
None)
(do (setv ast a2) (continue)))) ;; TCO

(= (Sym "fn*") a0)
(do
(setv func (fn [&rest args]
(EVAL a2 (env-new env a1 (or args []))))
func.ast a2
func.env env
func.params a1)
func)

;; apply
(do
(setv el (eval-ast ast env)
f (first el)
args (list (rest el)))
(if (hasattr f "ast")
(do (setv ast f.ast
env (env-new f.env f.params args))
(continue)) ;; TCO
(apply f args)))))))
(break))
res)

Expand Down
Loading

0 comments on commit 1872f73

Please sign in to comment.