diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/core.py | 6 | ||||
| -rw-r--r-- | python/step3_env.py | 9 | ||||
| -rw-r--r-- | python/step4_if_fn_do.py | 7 | ||||
| -rw-r--r-- | python/step5_tco.py | 7 | ||||
| -rw-r--r-- | python/step6_file.py | 12 | ||||
| -rw-r--r-- | python/step7_quote.py | 12 | ||||
| -rw-r--r-- | python/step8_macros.py | 14 | ||||
| -rw-r--r-- | python/step9_interop.py | 14 | ||||
| -rw-r--r-- | python/stepA_more.py | 15 |
9 files changed, 41 insertions, 55 deletions
diff --git a/python/core.py b/python/core.py index 20ac793..54737aa 100644 --- a/python/core.py +++ b/python/core.py @@ -3,6 +3,8 @@ from itertools import chain import mal_types as types from mal_types import List, Vector +import mal_readline +import reader import printer # Errors/Exceptions @@ -112,10 +114,14 @@ ns = { 'false?': types._false_Q, 'symbol': types._symbol, 'symbol?': types._symbol_Q, + 'pr-str': pr_str, 'str': do_str, 'prn': prn, 'println': println, + 'readline': lambda prompt: mal_readline.readline(prompt), + 'read-string': reader.read_str, + 'slurp': lambda file: open(file).read(), '<': lambda a,b: a<b, '<=': lambda a,b: a<=b, '>': lambda a,b: a>b, diff --git a/python/step3_env.py b/python/step3_env.py index f684274..372adc4 100644 --- a/python/step3_env.py +++ b/python/step3_env.py @@ -57,12 +57,11 @@ def PRINT(exp): repl_env = Env() def REP(str): return PRINT(EVAL(READ(str), repl_env)) -def _ref(k,v): repl_env.set(k, v) -_ref('+', lambda a,b: a+b) -_ref('-', lambda a,b: a-b) -_ref('*', lambda a,b: a*b) -_ref('/', lambda a,b: int(a/b)) +repl_env.set('+', lambda a,b: a+b) +repl_env.set('-', lambda a,b: a-b) +repl_env.set('*', lambda a,b: a*b) +repl_env.set('/', lambda a,b: int(a/b)) while True: try: diff --git a/python/step4_if_fn_do.py b/python/step4_if_fn_do.py index d9910f6..a06d10b 100644 --- a/python/step4_if_fn_do.py +++ b/python/step4_if_fn_do.py @@ -72,12 +72,11 @@ def PRINT(exp): repl_env = Env() def REP(str): return PRINT(EVAL(READ(str), repl_env)) -def _ref(k,v): repl_env.set(k, v) -# Import types functions -for name, val in core.ns.items(): _ref(name, val) +# core.py: defined using python +for k, v in core.ns.items(): repl_env.set(k, v) -# Defined using the language itself +# core.mal: defined using the language itself REP("(def! not (fn* (a) (if a false true)))") while True: diff --git a/python/step5_tco.py b/python/step5_tco.py index 633cbd8..7982073 100644 --- a/python/step5_tco.py +++ b/python/step5_tco.py @@ -79,12 +79,11 @@ def PRINT(exp): repl_env = Env() def REP(str): return PRINT(EVAL(READ(str), repl_env)) -def _ref(k,v): repl_env.set(k, v) -# Import types functions -for name, val in core.ns.items(): _ref(name, val) +# core.py: defined using python +for k, v in core.ns.items(): repl_env.set(k, v) -# Defined using the language itself +# core.mal: defined using the language itself REP("(def! not (fn* (a) (if a false true)))") while True: diff --git a/python/step6_file.py b/python/step6_file.py index 1a992cc..ba2d355 100644 --- a/python/step6_file.py +++ b/python/step6_file.py @@ -79,16 +79,12 @@ def PRINT(exp): repl_env = Env() def REP(str): return PRINT(EVAL(READ(str), repl_env)) -def _ref(k,v): repl_env.set(k, v) -# Import types functions -for name, val in core.ns.items(): _ref(name, val) +# core.py: defined using python +for k, v in core.ns.items(): repl_env.set(k, v) +repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) -_ref('read-string', reader.read_str) -_ref('eval', lambda ast: EVAL(ast, repl_env)) -_ref('slurp', lambda file: open(file).read()) - -# Defined using the language itself +# 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) \")\")))))") diff --git a/python/step7_quote.py b/python/step7_quote.py index cae3c99..aefa421 100644 --- a/python/step7_quote.py +++ b/python/step7_quote.py @@ -101,16 +101,12 @@ def PRINT(exp): repl_env = Env() def REP(str): return PRINT(EVAL(READ(str), repl_env)) -def _ref(k,v): repl_env.set(k, v) -# Import types functions -for name, val in core.ns.items(): _ref(name, val) +# core.py: defined using python +for k, v in core.ns.items(): repl_env.set(k, v) +repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) -_ref('read-string', reader.read_str) -_ref('eval', lambda ast: EVAL(ast, repl_env)) -_ref('slurp', lambda file: open(file).read()) - -# Defined using the language itself +# 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) \")\")))))") diff --git a/python/step8_macros.py b/python/step8_macros.py index adb4546..90aedf3 100644 --- a/python/step8_macros.py +++ b/python/step8_macros.py @@ -121,18 +121,16 @@ def PRINT(exp): repl_env = Env() def REP(str): return PRINT(EVAL(READ(str), repl_env)) -def _ref(k,v): repl_env.set(k, v) -# Import types functions -for name, val in core.ns.items(): _ref(name, val) +# core.py: defined using python +for k, v in core.ns.items(): repl_env.set(k, v) +repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) -_ref('read-string', reader.read_str) -_ref('eval', lambda ast: EVAL(ast, repl_env)) -_ref('slurp', lambda file: open(file).read()) - -# Defined using the language itself +# 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) \")\")))))") +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))))))))") if len(sys.argv) >= 2: REP('(load-file "' + sys.argv[1] + '")') diff --git a/python/step9_interop.py b/python/step9_interop.py index 1d8e55a..eae7837 100644 --- a/python/step9_interop.py +++ b/python/step9_interop.py @@ -130,18 +130,16 @@ def PRINT(exp): repl_env = Env() def REP(str): return PRINT(EVAL(READ(str), repl_env)) -def _ref(k,v): repl_env.set(k, v) -# Import types functions -for name, val in core.ns.items(): _ref(name, val) +# core.py: defined using python +for k, v in core.ns.items(): repl_env.set(k, v) +repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) -_ref('read-string', reader.read_str) -_ref('eval', lambda ast: EVAL(ast, repl_env)) -_ref('slurp', lambda file: open(file).read()) - -# Defined using the language itself +# 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) \")\")))))") +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))))))))") if len(sys.argv) >= 2: REP('(load-file "' + sys.argv[1] + '")') diff --git a/python/stepA_more.py b/python/stepA_more.py index dccce8e..fa79ec3 100644 --- a/python/stepA_more.py +++ b/python/stepA_more.py @@ -144,21 +144,16 @@ def PRINT(exp): repl_env = Env() def REP(str): return PRINT(EVAL(READ(str), repl_env)) -def _ref(k,v): repl_env.set(k, v) -# Import types functions -for name, val in core.ns.items(): _ref(name, val) +# core.py: defined using python +for k, v in core.ns.items(): repl_env.set(k, v) +repl_env.set('eval', lambda ast: EVAL(ast, repl_env)) -_ref('readline', lambda prompt: mal_readline.readline(prompt)) -_ref('read-string', reader.read_str) -_ref('eval', lambda ast: EVAL(ast, repl_env)) -_ref('slurp', lambda file: open(file).read()) - -# Defined using the language itself +# 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) \")\")))))") 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))))))))") -REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))") if len(sys.argv) >= 2: REP('(load-file "' + sys.argv[1] + '")') |
