Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hy: merge eval_ast/macroexpand into EVAL. Add DEBUG-EVAL. #683

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions impls/hy/step2_eval.hy
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,34 @@
(read-str str))

;; eval
(defn eval-ast [ast env]
(if
(symbol? ast) (if (.has_key env ast) (get env ast)
(raise (Exception (+ ast " not found"))))
(instance? dict ast) (dict (map (fn [k]
[k (EVAL (get ast k) env)])
ast))
(instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast))
(instance? list ast) (list (map (fn [x] (EVAL x env)) ast))
True ast))

(defn EVAL [ast env]
;; indented to match later steps
(if (not (instance? tuple ast))
(eval-ast ast env)
(if
(symbol? ast)
(if (.has_key env ast) (get env ast)
(raise (Exception (+ ast " not found"))))

;; apply list
(if
(empty? ast)
ast
(instance? dict ast)
(dict (map (fn [k]
[k (EVAL (get ast k) env)])
ast))

(instance? list ast)
(list (map (fn [x] (EVAL x env)) ast))

(not (instance? tuple ast))
ast

(empty? ast)
ast

;; apply list
;; apply
(do
(setv el (eval-ast ast env)
(setv el (list (map (fn [x] (EVAL x env)) ast))
f (first el)
args (list (rest el)))
(apply f args)))))
(apply f args))))

;; print
(defn PRINT [exp]
Expand Down
43 changes: 24 additions & 19 deletions impls/hy/step3_env.hy
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,42 @@
(import sys traceback)
(import [reader [read-str Blank]])
(import [printer [pr-str]])
(import [env [env-new env-get env-set]])
(import [env [env-new env-get env-set env-find]])

;; read
(defn READ [str]
(read-str str))

;; eval
(defn eval-ast [ast env]
;;(print "eval-ast:" ast (type ast))
(if
(symbol? ast) (env-get env ast)
(instance? dict ast) (dict (map (fn [k]
[k (EVAL (get ast k) env)])
ast))
(instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast))
(instance? list ast) (list (map (fn [x] (EVAL x env)) ast))
True ast))

(defn EVAL [ast env]
;;(print "EVAL:" ast (type ast))
;; indented to match later steps
(if (not (instance? tuple ast))
(eval-ast ast env)
(setv [dbgevalenv] [(env-find env (Sym "DEBUG-EVAL"))])
(if dbgevalenv
(do (setv [dbgevalsym] [(env-get dbgevalenv (Sym "DEBUG-EVAL"))])
(if (not (none? dbgevalsym))
(print "EVAL:" (pr-str ast True)))))
(if
(symbol? ast)
(env-get env ast)

(instance? dict ast)
(dict (map (fn [k]
[k (EVAL (get ast k) env)])
ast))

(instance? list ast)
(list (map (fn [x] (EVAL x env)) ast))

(not (instance? tuple ast))
ast

(empty? ast)
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))

Expand All @@ -47,7 +52,7 @@

;; apply
(do
(setv el (eval-ast ast env)
(setv el (list (map (fn [x] (EVAL x env)) ast))
f (first el)
args (list (rest el)))
(apply f args))))))
Expand Down
45 changes: 25 additions & 20 deletions impls/hy/step4_if_fn_do.hy
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,43 @@
(import [mal_types [MalException]])
(import [reader [read-str Blank]])
(import [printer [pr-str]])
(import [env [env-new env-get env-set]])
(import [env [env-new env-get env-set env-find]])
(import core)

;; read
(defn READ [str]
(read-str str))

;; eval
(defn eval-ast [ast env]
;;(print "eval-ast:" ast (type ast))
(if
(symbol? ast) (env-get env ast)
(instance? dict ast) (dict (map (fn [k]
[k (EVAL (get ast k) env)])
ast))
(instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast))
(instance? list ast) (list (map (fn [x] (EVAL x env)) ast))
True ast))

(defn EVAL [ast env]
;;(print "EVAL:" ast (type ast))
;; indented to match later steps
(if (not (instance? tuple ast))
(eval-ast ast env)
(setv [dbgevalenv] [(env-find env (Sym "DEBUG-EVAL"))])
(if dbgevalenv
(do (setv [dbgevalsym] [(env-get dbgevalenv (Sym "DEBUG-EVAL"))])
(if (not (none? dbgevalsym))
(print "EVAL:" (pr-str ast True)))))
(if
(symbol? ast)
(env-get env ast)

(instance? dict ast)
(dict (map (fn [k]
[k (EVAL (get ast k) env)])
ast))

(instance? list ast)
(list (map (fn [x] (EVAL x env)) ast))

(not (instance? tuple ast))
ast

(empty? ast)
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))

Expand All @@ -48,7 +53,7 @@
(EVAL a2 env))

(= (Sym "do") a0)
(last (eval-ast (list (rest ast)) env))
(last (list (map (fn [x] (EVAL x env)) (list (rest ast)))))

(= (Sym "if") a0)
(do
Expand All @@ -66,7 +71,7 @@

;; apply
(do
(setv el (eval-ast ast env)
(setv el (list (map (fn [x] (EVAL x env)) ast))
f (first el)
args (list (rest el)))
(apply f args))))))
Expand Down
47 changes: 26 additions & 21 deletions impls/hy/step5_tco.hy
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,45 @@
(import [mal_types [MalException]])
(import [reader [read-str Blank]])
(import [printer [pr-str]])
(import [env [env-new env-get env-set]])
(import [env [env-new env-get env-set env-find]])
(import core)

;; read
(defn READ [str]
(read-str str))

;; eval
(defn eval-ast [ast env]
;;(print "eval-ast:" ast (type ast))
(if
(symbol? ast) (env-get env ast)
(instance? dict ast) (dict (map (fn [k]
[k (EVAL (get ast k) env)])
ast))
(instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast))
(instance? list ast) (list (map (fn [x] (EVAL x env)) ast))
True ast))

(defn EVAL [ast env]
;;(print "EVAL:" ast (type ast))
;; indented to match later steps
(setv res None)
(while True
(setv [dbgevalenv] [(env-find env (Sym "DEBUG-EVAL"))])
(if dbgevalenv
(do (setv [dbgevalsym] [(env-get dbgevalenv (Sym "DEBUG-EVAL"))])
(if (not (none? dbgevalsym))
(print "EVAL:" (pr-str ast True)))))
(setv res
(if (not (instance? tuple ast))
(eval-ast ast env)
(if
(symbol? ast)
(env-get env ast)

(instance? dict ast)
(dict (map (fn [k]
[k (EVAL (get ast k) env)])
ast))

(instance? list ast)
(list (map (fn [x] (EVAL x env)) ast))

(not (instance? tuple ast))
ast

(empty? ast)
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))

Expand All @@ -52,7 +56,8 @@
(continue)) ;; TCO

(= (Sym "do") a0)
(do (eval-ast (list (butlast (rest ast))) env)
(do (list (map (fn [x] (EVAL x env))
(list (butlast (rest ast)))))
(setv ast (last ast))
(continue)) ;; TCO

Expand All @@ -77,7 +82,7 @@

;; apply
(do
(setv el (eval-ast ast env)
(setv el (list (map (fn [x] (EVAL x env)) ast))
f (first el)
args (list (rest el)))
(if (hasattr f "ast")
Expand Down
47 changes: 26 additions & 21 deletions impls/hy/step6_file.hy
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,45 @@
(import [mal_types [MalException]])
(import [reader [read-str Blank]])
(import [printer [pr-str]])
(import [env [env-new env-get env-set]])
(import [env [env-new env-get env-set env-find]])
(import core)

;; read
(defn READ [str]
(read-str str))

;; eval
(defn eval-ast [ast env]
;;(print "eval-ast:" ast (type ast))
(if
(symbol? ast) (env-get env ast)
(instance? dict ast) (dict (map (fn [k]
[k (EVAL (get ast k) env)])
ast))
(instance? tuple ast) (tuple (map (fn [x] (EVAL x env)) ast))
(instance? list ast) (list (map (fn [x] (EVAL x env)) ast))
True ast))

(defn EVAL [ast env]
;;(print "EVAL:" ast (type ast) (instance? tuple ast))
;; indented to match later steps
(setv res None)
(while True
(setv [dbgevalenv] [(env-find env (Sym "DEBUG-EVAL"))])
(if dbgevalenv
(do (setv [dbgevalsym] [(env-get dbgevalenv (Sym "DEBUG-EVAL"))])
(if (not (none? dbgevalsym))
(print "EVAL:" (pr-str ast True)))))
(setv res
(if (not (instance? tuple ast))
(eval-ast ast env)
(if
(symbol? ast)
(env-get env ast)

(instance? dict ast)
(dict (map (fn [k]
[k (EVAL (get ast k) env)])
ast))

(instance? list ast)
(list (map (fn [x] (EVAL x env)) ast))

(not (instance? tuple ast))
ast

(empty? ast)
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))

Expand All @@ -52,7 +56,8 @@
(continue)) ;; TCO

(= (Sym "do") a0)
(do (eval-ast (list (butlast (rest ast))) env)
(do (list (map (fn [x] (EVAL x env))
(list (butlast (rest ast)))))
(setv ast (last ast))
(continue)) ;; TCO

Expand All @@ -77,7 +82,7 @@

;; apply
(do
(setv el (eval-ast ast env)
(setv el (list (map (fn [x] (EVAL x env)) ast))
f (first el)
args (list (rest el)))
(if (hasattr f "ast")
Expand Down
Loading
Loading