Get base/prelude system working
This commit is contained in:
parent
3370fbc13e
commit
792ce12617
61
prelude.ld
Normal file
61
prelude.ld
Normal file
|
@ -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
|
|
@ -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)
|
||||
(-> ast prettify-ast println)
|
||||
|
||||
result
|
||||
)
|
3
src/ludus/prelude.clj
Normal file
3
src/ludus/prelude.clj
Normal file
|
@ -0,0 +1,3 @@
|
|||
(ns ludus.prelude)
|
||||
|
||||
(def prelude (slurp "prelude.ld"))
|
|
@ -1,6 +0,0 @@
|
|||
(ns ludus.prelude)
|
||||
|
||||
(def prelude "
|
||||
ns prelude {
|
||||
}
|
||||
")
|
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue
Block a user