From 792ce12617fd2870cd04e67ba88299f79ffec9a3 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Fri, 1 Dec 2023 12:38:33 -0500 Subject: [PATCH] Get base/prelude system working --- prelude.ld | 61 ++++++++++++++++++++++++++++++++++++++ src/ludus/interpreter.cljc | 19 +++++++----- src/ludus/prelude.clj | 3 ++ src/ludus/prelude.cljc | 6 ---- src/ludus/repl.clj | 8 ++--- 5 files changed, 79 insertions(+), 18 deletions(-) create mode 100644 prelude.ld create mode 100644 src/ludus/prelude.clj delete mode 100644 src/ludus/prelude.cljc diff --git a/prelude.ld b/prelude.ld new file mode 100644 index 0000000..5d72cc9 --- /dev/null +++ b/prelude.ld @@ -0,0 +1,61 @@ +& this file, uniquely, gets `base` loaded. See src/ludus/base.cljc for exports + +fn first { + "Returns the first element of a list or tuple." + (xs as :list) -> extern (:first, xs) + (xs as :tuple) -> extern (:second, xs) +} + +fn nth { + "Returns the element at index n of a list or tuple. Zero-indexed: the first element is at index 0." + (xs as :list, n as :number) -> extern (:nth, xs, n) + (xs as :tuple, n as :number) -> extern (:nth, xs, extern (:inc, n)) +} + +fn count { + "Returns the number of elements in a list or tuple." + (xs as :list) -> extern (count, xs) + (xs as :tuple) -> extern (:dec, extern (:count, xs)) +} + +fn clist { + "Takes a tuple, and returns it as a list." + (xs as :tuple) -> extern (:into, [], extern (:rest, xs)) +} + +fn fold { + "Folds a collection." + (f as :function, xs as :list) -> extern (:reduce, f, xs) + (f as :function, root, xs as :list) -> extern (:reduce, f, root, xs) +} + +fn conj { + "Adds an element to a list. Short for conjoin." + () -> [] + (xs as :list) -> xs + (xs as :list, x) -> extern (:conj, xs, x) + & (xs, x, ...ys) -> extern (:conj, xs, x, ...ys) +} + +fn ludus/add { + "Adds numbers." + () -> 0 + (x as :number) -> x + (x as :number, y as :number) -> add (x, y) + (x as :number, y as :number, ...zs) -> fold (ludus/add, add (x, y), zs) +} + +ns math { + :add ludus/add +} + +print ("From Prelude, hello.") + +ns prelude { + conj + clist +} + +print (prelude) + +prelude \ No newline at end of file diff --git a/src/ludus/interpreter.cljc b/src/ludus/interpreter.cljc index cb259db..5f3dfbe 100644 --- a/src/ludus/interpreter.cljc +++ b/src/ludus/interpreter.cljc @@ -7,6 +7,7 @@ [ludus.base :as base] [ludus.prelude :as prelude] [ludus.data :as data] + [ludus.show :as show] ;;[ludus.loader :as loader] [clojure.pprint :as pp] [clojure.set] @@ -18,6 +19,9 @@ ;; that's for later, tho (defn- ludus-resolve [key ctx-vol] (let [ctx @ctx-vol] + (println "Resolving " key) + (println "Current context: " (keys ctx)) + ;(println "Parent context: " (keys (if (::parent ctx) (deref (::parent ctx)) {}))) (if (contains? ctx key) (get ctx key) (if (contains? ctx ::parent) @@ -420,7 +424,7 @@ (get map kw)))))) (defn- call-fn [lfn args ctx] - (println "Calling function " (:name lfn)) + ; (println "Calling function " (:name lfn)) (cond (= ::data/partial (first args)) {::data/type ::data/clj @@ -873,6 +877,7 @@ interpreted (interpret-ast parsed base-ctx) namespace (dissoc interpreted ::data/type ::data/name ::data/struct) context (ns->ctx namespace)] + ;(println context) context)) ;; TODO: update this to use new parser pipeline & new AST representation @@ -938,19 +943,17 @@ )) ;; repl -(do +(comment - (def source "fn foo { \"This is a docstring\"; (:foo) -> :foo; (:bar) -> :bar; (_) -> :something_else } - :doc (foo) + (def source " ") (def tokens (-> source scanner/scan :tokens)) (def ast (p/apply-parser g/script tokens)) - (def result (interpret-safe source ast {})) + ;(def result (interpret-safe source ast {})) - (-> ast prettify-ast pp/pprint) - - result + (-> ast prettify-ast println) + ) \ No newline at end of file diff --git a/src/ludus/prelude.clj b/src/ludus/prelude.clj new file mode 100644 index 0000000..2747865 --- /dev/null +++ b/src/ludus/prelude.clj @@ -0,0 +1,3 @@ +(ns ludus.prelude) + +(def prelude (slurp "prelude.ld")) \ No newline at end of file diff --git a/src/ludus/prelude.cljc b/src/ludus/prelude.cljc deleted file mode 100644 index 1fbfb2d..0000000 --- a/src/ludus/prelude.cljc +++ /dev/null @@ -1,6 +0,0 @@ -(ns ludus.prelude) - -(def prelude " -ns prelude { -} -") \ No newline at end of file diff --git a/src/ludus/repl.clj b/src/ludus/repl.clj index e7029be..75c253e 100644 --- a/src/ludus/repl.clj +++ b/src/ludus/repl.clj @@ -22,7 +22,7 @@ (println "\nGoodbye!") (System/exit 0)) -(def base-ctx (merge base/base ;process/process +(def repl-ctx (merge interpreter/ludus-prelude {::repl true "repl" {::data/struct true @@ -35,12 +35,12 @@ :body (fn ([] (let [session @current-session] - (swap! session #(assoc % :ctx (volatile! base-ctx))) + (swap! session #(assoc % :ctx (volatile! repl-ctx))) :ok)) ([name] (if-let [session (get @sessions name)] (do - (swap! session #(assoc % :ctx (volatile! base-ctx))) + (swap! session #(assoc % :ctx (volatile! repl-ctx))) :ok) (do (println "No session named" name) @@ -74,7 +74,7 @@ (defn- new-session [name] (let [session (atom {:name name - :ctx (volatile! base-ctx) + :ctx (volatile! repl-ctx) :history []})] (swap! sessions #(assoc % name session)) session))