From e8f52c24f49d68a235d0a82f4df31f5562ed1274 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Thu, 21 Sep 2017 23:32:15 -0500 Subject: [PATCH] Hy: sync indenting. Define entry with defmain. --- hy/Makefile | 21 ++++++ hy/step0_repl.hy | 14 ++-- hy/step1_read_print.hy | 21 +++--- hy/step2_eval.hy | 43 +++++++------ hy/step3_env.hy | 65 ++++++++++--------- hy/step4_if_fn_do.hy | 107 ++++++++++++++++--------------- hy/step5_tco.hy | 120 +++++++++++++++++----------------- hy/step6_file.hy | 130 +++++++++++++++++++------------------ hy/step7_quote.hy | 142 +++++++++++++++++++++-------------------- hy/step8_macros.hy | 44 +++++++------ hy/step9_try.hy | 46 ++++++------- hy/stepA_mal.hy | 48 +++++++------- 12 files changed, 429 insertions(+), 372 deletions(-) create mode 100644 hy/Makefile diff --git a/hy/Makefile b/hy/Makefile new file mode 100644 index 0000000000..2e6a5d7b64 --- /dev/null +++ b/hy/Makefile @@ -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]" diff --git a/hy/step0_repl.hy b/hy/step0_repl.hy index 9596f056f6..d651bbf0ef 100755 --- a/hy/step0_repl.hy +++ b/hy/step0_repl.hy @@ -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))))) diff --git a/hy/step1_read_print.hy b/hy/step1_read_print.hy index dcd384214c..179b09f884 100755 --- a/hy/step1_read_print.hy +++ b/hy/step1_read_print.hy @@ -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)))))))) diff --git a/hy/step2_eval.hy b/hy/step2_eval.hy index d6b407b52c..6eb4b0f324 100755 --- a/hy/step2_eval.hy +++ b/hy/step2_eval.hy @@ -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 {'+ + '- - '* * @@ -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)))))))) diff --git a/hy/step3_env.hy b/hy/step3_env.hy index 95dc3fbbb2..2e46c3cfc3 100755 --- a/hy/step3_env.hy +++ b/hy/step3_env.hy @@ -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 {'+ + '- - '* * @@ -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)))))))) diff --git a/hy/step4_if_fn_do.hy b/hy/step4_if_fn_do.hy index bf9c6ba737..0fd04c442b 100755 --- a/hy/step4_if_fn_do.hy +++ b/hy/step4_if_fn_do.hy @@ -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] @@ -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)))))))) diff --git a/hy/step5_tco.hy b/hy/step5_tco.hy index 6b4ee8fafa..8e6ec056c1 100755 --- a/hy/step5_tco.hy +++ b/hy/step5_tco.hy @@ -29,58 +29,59 @@ (while True (if (not (instance? tuple ast)) (setv res (eval-ast ast env)) - + ;; apply list - (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)) - (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 + ;; indented to match later steps (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)) + (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)) (continue)) ;; TCO - (setv res (apply f args))))))) + + (= (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))))))) (break)) res) @@ -100,12 +101,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)))))))) diff --git a/hy/step6_file.hy b/hy/step6_file.hy index 680a347928..8bd4d696d0 100755 --- a/hy/step6_file.hy +++ b/hy/step6_file.hy @@ -29,58 +29,59 @@ (while True (if (not (instance? tuple ast)) (setv res (eval-ast ast env)) - + ;; apply list - (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)) - (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 + ;; indented to match later steps (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)) + (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)) (continue)) ;; TCO - (setv res (apply f args))))))) + + (= (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))))))) (break)) res) @@ -97,22 +98,25 @@ (for [k core.ns] (env-set repl-env (Sym k) (get core.ns k))) (env-set repl-env (Sym "eval") (fn [ast] (EVAL ast repl-env))) -(env-set repl-env (Sym "*ARGV*") (tuple (map Str (rest (rest sys.argv))))) +(env-set repl-env (Sym "*ARGV*") (, )) ;; core.mal: defined using the language itself (REP "(def! not (fn* [a] (if a false true)))") (REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))") -(when (>= (len sys.argv) 2) - (REP (+ "(load-file \"" (get sys.argv 1) "\")")) - (sys.exit 0)) - -(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] + (if (>= (len args) 2) + (do + (env-set repl-env (Sym "*ARGV*") (tuple (map Str (rest (rest args))))) + (REP (+ "(load-file \"" (get args 1) "\")"))) + (do + (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)))))))))) diff --git a/hy/step7_quote.hy b/hy/step7_quote.hy index 17e6db7f1d..532e970c53 100755 --- a/hy/step7_quote.hy +++ b/hy/step7_quote.hy @@ -47,64 +47,65 @@ (while True (if (not (instance? tuple ast)) (setv res (eval-ast ast env)) - + ;; apply list - (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 "quote") a0) - (setv res a1) - - (= (Sym "quasiquote") a0) - (do (setv ast (QUASIQUOTE a1)) (continue)) ;; TCO - - (= (Sym "do") a0) - (do (eval-ast (list (butlast (rest ast))) env) - (setv ast (last ast)) - (continue)) ;; TCO - - (= (Sym "if") a0) + ;; indented to match later steps (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)) + (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 "quote") a0) + (setv res a1) + + (= (Sym "quasiquote") a0) + (do (setv ast (QUASIQUOTE a1)) (continue)) ;; TCO + + (= (Sym "do") a0) + (do (eval-ast (list (butlast (rest ast))) env) + (setv ast (last ast)) (continue)) ;; TCO - (setv res (apply f args))))))) + + (= (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))))))) (break)) res) @@ -121,22 +122,25 @@ (for [k core.ns] (env-set repl-env (Sym k) (get core.ns k))) (env-set repl-env (Sym "eval") (fn [ast] (EVAL ast repl-env))) -(env-set repl-env (Sym "*ARGV*") (tuple (map Str (rest (rest sys.argv))))) +(env-set repl-env (Sym "*ARGV*") (, )) ;; core.mal: defined using the language itself (REP "(def! not (fn* [a] (if a false true)))") (REP "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))") -(when (>= (len sys.argv) 2) - (REP (+ "(load-file \"" (get sys.argv 1) "\")")) - (sys.exit 0)) - -(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] + (if (>= (len args) 2) + (do + (env-set repl-env (Sym "*ARGV*") (tuple (map Str (rest (rest args))))) + (REP (+ "(load-file \"" (get args 1) "\")"))) + (do + (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)))))))))) diff --git a/hy/step8_macros.hy b/hy/step8_macros.hy index 740c751b80..d0574ccda9 100755 --- a/hy/step8_macros.hy +++ b/hy/step8_macros.hy @@ -63,7 +63,7 @@ (while True (if (not (instance? tuple ast)) (setv res (eval-ast ast env)) - + ;; apply list (do (setv ast (macroexpand ast env)) @@ -75,10 +75,10 @@ (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)) @@ -92,7 +92,7 @@ (= (Sym "quasiquote") a0) (do (setv ast (QUASIQUOTE a1)) (continue)) ;; TCO - + (= (Sym "defmacro!") a0) (do (setv func (EVAL a2 env) func.macro True) @@ -105,7 +105,7 @@ (do (eval-ast (list (butlast (rest ast))) env) (setv ast (last ast)) (continue)) ;; TCO - + (= (Sym "if") a0) (do (setv cond (EVAL a1 env)) @@ -115,7 +115,7 @@ (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 [])))) @@ -150,7 +150,7 @@ (for [k core.ns] (env-set repl-env (Sym k) (get core.ns k))) (env-set repl-env (Sym "eval") (fn [ast] (EVAL ast repl-env))) -(env-set repl-env (Sym "*ARGV*") (tuple (map Str (rest (rest sys.argv))))) +(env-set repl-env (Sym "*ARGV*") (, )) ;; core.mal: defined using the language itself (REP "(def! not (fn* [a] (if a false true)))") @@ -158,17 +158,19 @@ (REP "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))") (REP "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))") - -(when (>= (len sys.argv) 2) - (REP (+ "(load-file \"" (get sys.argv 1) "\")")) - (sys.exit 0)) - -(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] + (if (>= (len args) 2) + (do + (env-set repl-env (Sym "*ARGV*") (tuple (map Str (rest (rest args))))) + (REP (+ "(load-file \"" (get args 1) "\")"))) + (do + (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)))))))))) diff --git a/hy/step9_try.hy b/hy/step9_try.hy index 2e2f4edd87..ae5f936f93 100755 --- a/hy/step9_try.hy +++ b/hy/step9_try.hy @@ -64,7 +64,7 @@ (while True (if (not (instance? tuple ast)) (setv res (eval-ast ast env)) - + ;; apply list (do (setv ast (macroexpand ast env)) @@ -76,10 +76,10 @@ (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)) @@ -93,7 +93,7 @@ (= (Sym "quasiquote") a0) (do (setv ast (QUASIQUOTE a1)) (continue)) ;; TCO - + (= (Sym "defmacro!") a0) (do (setv func (EVAL a2 env) func.macro True) @@ -114,12 +114,12 @@ (EVAL (nth a2 2) (env-new env [(nth a2 1)] [exc])))) (EVAL a1 env))) - + (= (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)) @@ -129,7 +129,7 @@ (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 [])))) @@ -164,7 +164,7 @@ (for [k core.ns] (env-set repl-env (Sym k) (get core.ns k))) (env-set repl-env (Sym "eval") (fn [ast] (EVAL ast repl-env))) -(env-set repl-env (Sym "*ARGV*") (tuple (map Str (rest (rest sys.argv))))) +(env-set repl-env (Sym "*ARGV*") (, )) ;; core.mal: defined using the language itself (REP "(def! not (fn* [a] (if a false true)))") @@ -172,17 +172,19 @@ (REP "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))") (REP "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))") - -(when (>= (len sys.argv) 2) - (REP (+ "(load-file \"" (get sys.argv 1) "\")")) - (sys.exit 0)) - -(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] + (if (>= (len args) 2) + (do + (env-set repl-env (Sym "*ARGV*") (tuple (map Str (rest (rest args))))) + (REP (+ "(load-file \"" (get args 1) "\")"))) + (do + (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)))))))))) diff --git a/hy/stepA_mal.hy b/hy/stepA_mal.hy index 080e6b4f3d..5d9a9fb4fe 100755 --- a/hy/stepA_mal.hy +++ b/hy/stepA_mal.hy @@ -64,7 +64,7 @@ (while True (if (not (instance? tuple ast)) (setv res (eval-ast ast env)) - + ;; apply list (do (setv ast (macroexpand ast env)) @@ -76,10 +76,10 @@ (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)) @@ -93,7 +93,7 @@ (= (Sym "quasiquote") a0) (do (setv ast (QUASIQUOTE a1)) (continue)) ;; TCO - + (= (Sym "defmacro!") a0) (do (setv func (EVAL a2 env) func.macro True) @@ -114,12 +114,12 @@ (EVAL (nth a2 2) (env-new env [(nth a2 1)] [exc])))) (EVAL a1 env))) - + (= (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)) @@ -129,7 +129,7 @@ (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 [])))) @@ -164,7 +164,7 @@ (for [k core.ns] (env-set repl-env (Sym k) (get core.ns k))) (env-set repl-env (Sym "eval") (fn [ast] (EVAL ast repl-env))) -(env-set repl-env (Sym "*ARGV*") (tuple (map Str (rest (rest sys.argv))))) +(env-set repl-env (Sym "*ARGV*") (, )) ;; core.mal: defined using the language itself (REP "(def! *host-language* \"Hy\")") @@ -174,18 +174,20 @@ (REP "(def! gensym (fn* [] (symbol (str \"G__\" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))") (REP "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))") - -(when (>= (len sys.argv) 2) - (REP (+ "(load-file \"" (get sys.argv 1) "\")")) - (sys.exit 0)) - -(REP "(println (str \"Mal [\" *host-language* \"]\"))") -(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] + (if (>= (len args) 2) + (do + (env-set repl-env (Sym "*ARGV*") (tuple (map Str (rest (rest args))))) + (REP (+ "(load-file \"" (get args 1) "\")"))) + (do + (REP "(println (str \"Mal [\" *host-language* \"]\"))") + (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))))))))))