Skip to content

Commit

Permalink
WIP basilisp port
Browse files Browse the repository at this point in the history
  • Loading branch information
djblue committed Oct 31, 2024
1 parent 681fb64 commit 7c296f2
Show file tree
Hide file tree
Showing 26 changed files with 662 additions and 264 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ clojure.data.json*
*.iml

obj/

/py/
**__pycache__**
22 changes: 21 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@
":shadow"
]
}
}
},
{
"name": "basilisp",
"projectType": "deps.edn",
"projectRootPath": [
"."
],
"customJackInCommandLine": "./py/bin/basilisp nrepl-server",
"jackInEnv": {
"PYTHONPATH": "dev:src:test"
},
"nReplPortFile": [
".nrepl-port",
],
"menuSelections": {
"cljAliases": [
":dev",
":cider"
]
}
},
]
}
40 changes: 28 additions & 12 deletions src/examples/data.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require #?(:clj [clojure.java.io :as io])
#?(:org.babashka/nbb [clojure.core]
:default [examples.hacker-news :as hn])
[clojure.pprint :as pp]
#?(:lpy [portal.runtime]
:default [clojure.pprint :as pp])
[examples.macros :refer [read-file]]
[portal.colors :as c]
[portal.viewer :as v])
Expand All @@ -13,7 +14,9 @@
:org.babashka/nbb (:import)
:cljs (:import [goog.math Long])
:cljr (:import [System DateTime Guid Uri]
[System.IO File])))
[System.IO File])
:lpy (:import [math :as Math]
[datetime :as datetime])))

#?(:clj
(defn slurp-bytes [x]
Expand Down Expand Up @@ -56,17 +59,24 @@
::uuid (random-uuid)
::date (js/Date.)
::bigint (js/BigInt "42")
::js-array #js [0 1 2 3 4]
::js-object #js {:hello "world"}}
::js-array (clj->js [0 1 2 3 4])
::js-object (clj->js {:hello "world"})}
:cljs
{::long (.fromString Long "4611681620380904123")
::promise (js/Promise.resolve 123)
::url (js/URL. "https://github.com/djblue/portal")
::uuid (random-uuid)
::date (js/Date.)
::bigint (js/BigInt "42")
::js-array #js [0 1 2 3 4]
::js-object #js {:hello "world"}}))
::js-array (clj->js [0 1 2 3 4])
::js-object (clj->js {:hello "world"})}
:lpy
{::fractions 22/7
::promise (promise)
::uuid (random-uuid)
::date (datetime.datetime/now)
::py-list (python/list [0 1 2 3 4])
::py-dict (python/dict {"hello" "world"})}))

(def platform-collections
#?(:bb nil
Expand Down Expand Up @@ -107,11 +117,15 @@

(def clojure-data
{::regex #"hello-world"
::sorted-map (sorted-map-by gt 3 "c" 2 "b" 1 "a")
::sorted-set (sorted-set-by gt 3 2 1)
::sorted-map #?(:lpy nil
:default (sorted-map-by gt 3 "c" 2 "b" 1 "a"))
::sorted-set #?(:lpy nil
:default
(sorted-set-by gt 3 2 1))
::var #'portal.colors/themes
::with-meta (with-meta 'with-meta {:hello :world})
::tagged (tagged-literal 'my/tag ["hello, world"])
::tagged #?(:lpy nil
:default (tagged-literal 'my/tag ["hello, world"]))
{:example/settings 'complex-key} :hello-world
::atom (atom ::hello)
::function println
Expand Down Expand Up @@ -244,9 +258,11 @@
::different-value ::new-key}]))

(def diff-text-data
(v/diff-text
[(with-out-str (pp/pprint (first diff-data)))
(with-out-str (pp/pprint (second diff-data)))]))
#?(:lpy nil
:default
(v/diff-text
[(with-out-str (pp/pprint (first diff-data)))
(with-out-str (pp/pprint (second diff-data)))])))

(def string-data
(v/for
Expand Down
28 changes: 16 additions & 12 deletions src/examples/hacker_news.cljc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
(ns examples.hacker-news
(:require #?(:clj [portal.sync :as a]
:cljs [portal.async :as a]
:cljr [portal.sync :as a])
:cljr [portal.sync :as a]
:lpy [portal.sync :as a])
#?(:cljs [examples.fetch :refer [fetch]])
[clojure.core.protocols :refer [nav]]
#?(:clj [portal.runtime.json :as json]
:cljr [portal.runtime.json :as json])))
:cljr [portal.runtime.json :as json]
:lpy [portal.runtime.json :as json])))

(def root "https://hacker-news.firebaseio.com/v0")

Expand Down Expand Up @@ -38,19 +39,22 @@
#?(:clj (-> url slurp json/read)
:cljs (-> (fetch url)
(.then #(js->clj (.parse js/JSON %) :keywordize-keys true)))
:cljr (-> url (slurp :enc "utf8") json/read)))
:cljr (-> url (slurp :enc "utf8") json/read)
:lpy (-> url slurp json/read)))

(defn as-url [s]
#?(:clj (java.net.URL. s)
:cljs (js/URL. s)
:cljr (System.Uri. s)))
:cljr (System.Uri. s)
:lpy s))

(defn as-date [^long timestamp]
#?(:clj (java.util.Date. timestamp)
:cljs (js/Date. timestamp)
:cljr (.DateTime
(System.DateTimeOffset/FromUnixTimeMilliseconds
timestamp))))
timestamp))
:lpy timestamp))

(declare nav-hn)
(declare nav-item)
Expand Down Expand Up @@ -82,32 +86,32 @@

(contains? item :kids)
(update :kids vary-meta assoc
`nav #'nav-item
'clojure.core.protocols/nav #'nav-item
:portal.viewer/default :portal.viewer/inspector)

(contains? item :submitted)
(update :submitted vary-meta assoc
`nav #'nav-item
'clojure.core.protocols/nav #'nav-item
:portal.viewer/default :portal.viewer/inspector)))))

(defn fetch-user [user]
(a/let [res (fetch-hn (str "/user/" user ".json"))]
(vary-meta res assoc `nav #'nav-hn)))
(vary-meta res assoc 'clojure.core.protocols/nav #'nav-hn)))

(defn nav-item [_coll _k v]
(a/let [res (fetch-hn (str "/item/" v ".json"))]
(vary-meta res assoc `nav #'nav-hn)))
(vary-meta res assoc 'clojure.core.protocols/nav #'nav-hn)))

(def stories
(with-meta
#{:topstories :newstories :beststories
:askstories :showstories :jobstories}
{`nav #'nav-hn
{'clojure.core.protocols/nav #'nav-hn
:portal.viewer/default :portal.viewer/inspector}))

(defn fetch-stories [type]
(a/let [res (fetch-hn (str "/" (name type) ".json"))]
(with-meta (take 15 res) (merge (meta res) {`nav #'nav-item}))))
(with-meta (take 15 res) (merge (meta res) {'clojure.core.protocols/nav #'nav-item}))))

(defn nav-hn [coll k v]
(cond
Expand Down
3 changes: 2 additions & 1 deletion src/examples/macros.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

#?(:clj (defmacro read-file [file-name] (slurp file-name))
:cljs (defn read-file [_file-name] ::missing)
:cljr (defmacro read-file [file-name] (slurp file-name :enc "utf8")))
:cljr (defmacro read-file [file-name] (slurp file-name :enc "utf8"))
:lpy (defn read-file [file-name] (slurp file-name)))
6 changes: 4 additions & 2 deletions src/portal/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
[portal.runtime.jvm.commands])
#?(:clj [portal.runtime.jvm.launcher :as l]
:cljs [portal.runtime.node.launcher :as l]
:cljr [portal.runtime.clr.launcher :as l])
:cljr [portal.runtime.clr.launcher :as l]
:lpy [portal.runtime.py.launcher :as l])
#?(:clj [portal.sync :as a]
:cljs [portal.async :as a]
:cljr [portal.sync :as a])
:cljr [portal.sync :as a]
:lpy [portal.sync :as a])
#?(:clj [clojure.java.io :as io]
:cljs [portal.resources :as io])
[clojure.set :as set]
Expand Down
6 changes: 4 additions & 2 deletions src/portal/client/py.lpy
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
(ns portal.client.py
(:require [basilisp.json :as json])
(:require [basilisp.json :as json]
[portal.runtime.cson :as cson])
(:import [urllib.request :as request]))

(defn- serialize [encoding value]
(.encode
(try
(case encoding
:json (json/write-str value)
:json (json/write-str value)
:cson (cson/write value)
:edn (binding [*print-meta* true]
(pr-str value)))
(catch Exception ex
Expand Down
Loading

0 comments on commit 7c296f2

Please sign in to comment.