Skip to content

Commit

Permalink
Hy: sync indenting. Define entry with defmain.
Browse files Browse the repository at this point in the history
  • Loading branch information
kanaka committed Sep 22, 2017
1 parent f382a61 commit e8f52c2
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 372 deletions.
21 changes: 21 additions & 0 deletions hy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
SOURCES_BASE = mal_types.hy reader.hy printer.hy
SOURCES_LISP = env.hy core.hy stepA_mal.hy
SOURCES = $(SOURCES_BASE) $(SOURCES_LISP)

all: mal.hy

mal.hy: stepA_mal.hy
cp $< $@

clean:
rm -f mal.hy *.pyc

#.PHONY: stats tests $(TESTS)
.PHONY: stats

stats: $(SOURCES)
@wc $^
@printf "%5s %5s %5s %s\n" `grep -E "^[[:space:]]*;|^[[:space:]]*$$" $^ | wc` "[comments/blanks]"
stats-lisp: $(SOURCES_LISP)
@wc $^
@printf "%5s %5s %5s %s\n" `grep -E "^[[:space:]]*;|^[[:space:]]*$$" $^ | wc` "[comments/blanks]"
14 changes: 8 additions & 6 deletions hy/step0_repl.hy
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
(defn REP [str]
(PRINT (EVAL (READ str) {})))

(while True
(try
(do (setv line (raw_input "user> "))
(if (= "" line) (continue))
(print (REP line)))
(except [EOFError] (break))))
(defmain [&rest args]
;; indented to match later steps
(while True
(try
(do (setv line (raw_input "user> "))
(if (= "" line) (continue))
(print (REP line)))
(except [EOFError] (break)))))
21 changes: 12 additions & 9 deletions hy/step1_read_print.hy
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
(defn REP [str]
(PRINT (EVAL (READ str) {})))

(while True
(try
(do (setv line (raw_input "user> "))
(if (= "" line) (continue))
(print (REP line)))
(except [EOFError] (break))
(except [Blank])
(except []
(print (.join "" (apply traceback.format_exception (.exc_info sys)))))))
(defmain [&rest args]
;; indented to match later steps
(while True
(try
(do (setv line (raw_input "user> "))
(if (= "" line) (continue))
(print (REP line)))
(except [EOFError] (break))
(except [Blank])
(except []
(print (.join "" (apply traceback.format_exception
(.exc_info sys))))))))
43 changes: 25 additions & 18 deletions hy/step2_eval.hy
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,26 @@

(defn EVAL [ast env]
(if (not (instance? tuple ast))
(eval-ast ast env)
(eval-ast ast env)

(if
(empty? ast) ast
(do
(setv el (eval-ast ast env)
f (first el)
args (rest el))
(apply f args)))))
;; apply list
;; indented to match later steps
(if
(empty? ast)
ast

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

;; print
(defn PRINT [exp]
(pr-str exp True))

;; repl

(def repl-env {'+ +
'- -
'* *
Expand All @@ -46,12 +50,15 @@
(defn REP [str]
(PRINT (EVAL (READ str) repl-env)))

(while True
(try
(do (setv line (raw_input "user> "))
(if (= "" line) (continue))
(print (REP line)))
(except [EOFError] (break))
(except [Blank])
(except []
(print (.join "" (apply traceback.format_exception (.exc_info sys)))))))
(defmain [&rest args]
;; indented to match later steps
(while True
(try
(do (setv line (raw_input "user> "))
(if (= "" line) (continue))
(print (REP line)))
(except [EOFError] (break))
(except [Blank])
(except []
(print (.join "" (apply traceback.format_exception
(.exc_info sys))))))))
65 changes: 34 additions & 31 deletions hy/step3_env.hy
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,38 @@
(defn EVAL [ast env]
;;(print "EVAL:" ast (type ast) (instance? tuple ast))
(if (not (instance? tuple ast))
(eval-ast ast env)
(eval-ast ast env)

;; apply list
(do
(setv [a0 a1 a2] [(nth ast 0) (nth ast 1) (nth ast 2)])
(if
(none? a0)
ast
;; 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 "def!") a0)
(env-set env a1 (EVAL a2 env))

(= (Sym "let*") a0)
(do
(setv let-env (env-new env))
(for [[b e] (partition a1 2)]
(env-set let-env b (EVAL e let-env)))
(EVAL a2 let-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 (rest el))
(apply f args))))))

;; print
(defn PRINT [exp]
(pr-str exp True))

;; repl

(def repl-env {'+ +
'- -
'* *
Expand All @@ -65,12 +65,15 @@
(defn REP [str]
(PRINT (EVAL (READ str) repl-env)))

(while True
(try
(do (setv line (raw_input "user> "))
(if (= "" line) (continue))
(print (REP line)))
(except [EOFError] (break))
(except [Blank])
(except []
(print (.join "" (apply traceback.format_exception (.exc_info sys)))))))
(defmain [&rest args]
;; indented to match later steps
(while True
(try
(do (setv line (raw_input "user> "))
(if (= "" line) (continue))
(print (REP line)))
(except [EOFError] (break))
(except [Blank])
(except []
(print (.join "" (apply traceback.format_exception
(.exc_info sys))))))))
107 changes: 55 additions & 52 deletions hy/step4_if_fn_do.hy
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,49 @@
(defn EVAL [ast env]
;;(print "EVAL:" ast (type ast) (instance? tuple ast))
(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 let-env (env-new env))
(for [[b e] (partition a1 2)]
(env-set let-env b (EVAL e let-env)))
(EVAL a2 let-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)))
;;(print "f:" f "args:" args)
(apply f args))))))
(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
(defn PRINT [exp]
Expand All @@ -86,12 +86,15 @@
;; core.mal: defined using the language itself
(REP "(def! not (fn* [a] (if a false true)))")

(while True
(try
(do (setv line (raw_input "user> "))
(if (= "" line) (continue))
(print (REP line)))
(except [EOFError] (break))
(except [Blank])
(except []
(print (.join "" (apply traceback.format_exception (.exc_info sys)))))))
(defmain [&rest args]
;; indented to match later steps
(while True
(try
(do (setv line (raw_input "user> "))
(if (= "" line) (continue))
(print (REP line)))
(except [EOFError] (break))
(except [Blank])
(except []
(print (.join "" (apply traceback.format_exception
(.exc_info sys))))))))
Loading

0 comments on commit e8f52c2

Please sign in to comment.