Skip to content

Commit 6c7be69

Browse files
committed
WIP basilisp port
1 parent 681fb64 commit 6c7be69

25 files changed

+651
-260
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ clojure.data.json*
2323
*.iml
2424

2525
obj/
26+
27+
/py/
28+
**__pycache__**

.vscode/settings.json

+21-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@
6060
":shadow"
6161
]
6262
}
63-
}
63+
},
64+
{
65+
"name": "basilisp",
66+
"projectType": "deps.edn",
67+
"projectRootPath": [
68+
"."
69+
],
70+
"customJackInCommandLine": "./py/bin/basilisp nrepl-server",
71+
"jackInEnv": {
72+
"PYTHONPATH": "dev:src:test"
73+
},
74+
"nReplPortFile": [
75+
".nrepl-port",
76+
],
77+
"menuSelections": {
78+
"cljAliases": [
79+
":dev",
80+
":cider"
81+
]
82+
}
83+
},
6484
]
6585
}

src/examples/data.cljc

+27-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
(:require #?(:clj [clojure.java.io :as io])
33
#?(:org.babashka/nbb [clojure.core]
44
:default [examples.hacker-news :as hn])
5-
[clojure.pprint :as pp]
5+
#?(:lpy [portal.runtime]
6+
:default [clojure.pprint :as pp])
67
[examples.macros :refer [read-file]]
78
[portal.colors :as c]
89
[portal.viewer :as v])
@@ -13,7 +14,9 @@
1314
:org.babashka/nbb (:import)
1415
:cljs (:import [goog.math Long])
1516
:cljr (:import [System DateTime Guid Uri]
16-
[System.IO File])))
17+
[System.IO File])
18+
:lpy (:import [math :as Math]
19+
[datetime :as datetime])))
1720

1821
#?(:clj
1922
(defn slurp-bytes [x]
@@ -56,17 +59,23 @@
5659
::uuid (random-uuid)
5760
::date (js/Date.)
5861
::bigint (js/BigInt "42")
59-
::js-array #js [0 1 2 3 4]
60-
::js-object #js {:hello "world"}}
62+
::js-array (clj->js [0 1 2 3 4])
63+
::js-object (clj->js {:hello "world"})}
6164
:cljs
6265
{::long (.fromString Long "4611681620380904123")
6366
::promise (js/Promise.resolve 123)
6467
::url (js/URL. "https://github.com/djblue/portal")
6568
::uuid (random-uuid)
6669
::date (js/Date.)
6770
::bigint (js/BigInt "42")
68-
::js-array #js [0 1 2 3 4]
69-
::js-object #js {:hello "world"}}))
71+
::js-array (clj->js [0 1 2 3 4])
72+
::js-object (clj->js {:hello "world"})}
73+
:lpy
74+
{::promise (promise)
75+
::uuid (random-uuid)
76+
::date (datetime.datetime/now)
77+
::py-list (python/list [0 1 2 3 4])
78+
::py-dict (python/dict {"hello" "world"})}))
7079

7180
(def platform-collections
7281
#?(:bb nil
@@ -107,11 +116,15 @@
107116

108117
(def clojure-data
109118
{::regex #"hello-world"
110-
::sorted-map (sorted-map-by gt 3 "c" 2 "b" 1 "a")
111-
::sorted-set (sorted-set-by gt 3 2 1)
119+
::sorted-map #?(:lpy nil
120+
:default (sorted-map-by gt 3 "c" 2 "b" 1 "a"))
121+
::sorted-set #?(:lpy nil
122+
:default
123+
(sorted-set-by gt 3 2 1))
112124
::var #'portal.colors/themes
113125
::with-meta (with-meta 'with-meta {:hello :world})
114-
::tagged (tagged-literal 'my/tag ["hello, world"])
126+
::tagged #?(:lpy nil
127+
:default (tagged-literal 'my/tag ["hello, world"]))
115128
{:example/settings 'complex-key} :hello-world
116129
::atom (atom ::hello)
117130
::function println
@@ -244,9 +257,11 @@
244257
::different-value ::new-key}]))
245258

246259
(def diff-text-data
247-
(v/diff-text
248-
[(with-out-str (pp/pprint (first diff-data)))
249-
(with-out-str (pp/pprint (second diff-data)))]))
260+
#?(:lpy nil
261+
:default
262+
(v/diff-text
263+
[(with-out-str (pp/pprint (first diff-data)))
264+
(with-out-str (pp/pprint (second diff-data)))])))
250265

251266
(def string-data
252267
(v/for

src/examples/hacker_news.cljc

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
(ns examples.hacker-news
22
(:require #?(:clj [portal.sync :as a]
33
:cljs [portal.async :as a]
4-
:cljr [portal.sync :as a])
4+
:cljr [portal.sync :as a]
5+
:lpy [portal.sync :as a])
56
#?(:cljs [examples.fetch :refer [fetch]])
6-
[clojure.core.protocols :refer [nav]]
77
#?(:clj [portal.runtime.json :as json]
8-
:cljr [portal.runtime.json :as json])))
8+
:cljr [portal.runtime.json :as json]
9+
:lpy [portal.runtime.json :as json])))
910

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

@@ -38,19 +39,22 @@
3839
#?(:clj (-> url slurp json/read)
3940
:cljs (-> (fetch url)
4041
(.then #(js->clj (.parse js/JSON %) :keywordize-keys true)))
41-
:cljr (-> url (slurp :enc "utf8") json/read)))
42+
:cljr (-> url (slurp :enc "utf8") json/read)
43+
:lpy (-> url slurp json/read)))
4244

4345
(defn as-url [s]
4446
#?(:clj (java.net.URL. s)
4547
:cljs (js/URL. s)
46-
:cljr (System.Uri. s)))
48+
:cljr (System.Uri. s)
49+
:lpy s))
4750

4851
(defn as-date [^long timestamp]
4952
#?(:clj (java.util.Date. timestamp)
5053
:cljs (js/Date. timestamp)
5154
:cljr (.DateTime
5255
(System.DateTimeOffset/FromUnixTimeMilliseconds
53-
timestamp))))
56+
timestamp))
57+
:lpy timestamp))
5458

5559
(declare nav-hn)
5660
(declare nav-item)
@@ -82,32 +86,32 @@
8286

8387
(contains? item :kids)
8488
(update :kids vary-meta assoc
85-
`nav #'nav-item
89+
'clojure.core.protocols/nav #'nav-item
8690
:portal.viewer/default :portal.viewer/inspector)
8791

8892
(contains? item :submitted)
8993
(update :submitted vary-meta assoc
90-
`nav #'nav-item
94+
'clojure.core.protocols/nav #'nav-item
9195
:portal.viewer/default :portal.viewer/inspector)))))
9296

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

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

101105
(def stories
102106
(with-meta
103107
#{:topstories :newstories :beststories
104108
:askstories :showstories :jobstories}
105-
{`nav #'nav-hn
109+
{'clojure.core.protocols/nav #'nav-hn
106110
:portal.viewer/default :portal.viewer/inspector}))
107111

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

112116
(defn nav-hn [coll k v]
113117
(cond

src/examples/macros.cljc

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

44
#?(:clj (defmacro read-file [file-name] (slurp file-name))
55
:cljs (defn read-file [_file-name] ::missing)
6-
:cljr (defmacro read-file [file-name] (slurp file-name :enc "utf8")))
6+
:cljr (defmacro read-file [file-name] (slurp file-name :enc "utf8"))
7+
:lpy (defn read-file [file-name] (slurp file-name)))

src/portal/api.cljc

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
[portal.runtime.jvm.commands])
44
#?(:clj [portal.runtime.jvm.launcher :as l]
55
:cljs [portal.runtime.node.launcher :as l]
6-
:cljr [portal.runtime.clr.launcher :as l])
6+
:cljr [portal.runtime.clr.launcher :as l]
7+
:lpy [portal.runtime.py.launcher :as l])
78
#?(:clj [portal.sync :as a]
89
:cljs [portal.async :as a]
9-
:cljr [portal.sync :as a])
10+
:cljr [portal.sync :as a]
11+
:lpy [portal.sync :as a])
1012
#?(:clj [clojure.java.io :as io]
1113
:cljs [portal.resources :as io])
1214
[clojure.set :as set]

src/portal/runtime.cljc

+34-20
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
(:refer-clojure :exclude [read])
33
(:require #?(:clj [portal.sync :as a]
44
:cljr [portal.sync :as a]
5-
:cljs [portal.async :as a])
5+
:cljs [portal.async :as a]
6+
:lpy [portal.sync :as a])
67
#?(:joyride [portal.runtime.datafy :refer [datafy nav]]
78
:org.babashka/nbb [portal.runtime.datafy :refer [datafy nav]]
9+
:lpy [portal.runtime.datafy :refer [datafy nav]]
810
:default [clojure.datafy :refer [datafy nav]])
911
#?(:joyride [cljs.pprint :as pprint]
1012
:default [clojure.pprint :as pprint])
@@ -15,6 +17,7 @@
1517

1618
#?(:joyride nil
1719
:org.babashka/nbb nil
20+
:lpy nil
1821
:default
1922
(defmethod pprint/simple-dispatch tagged-type [value]
2023
(if (not= (:tag value) "remote")
@@ -74,7 +77,7 @@
7477
(defn- hashable? [value]
7578
(try
7679
(and (hash value) true)
77-
(catch #?(:clj Exception :cljr Exception :cljs :default) _
80+
(catch #?(:clj Exception :cljr Exception :cljs :default :lpy Exception) _
7881
false)))
7982

8083
#?(:bb (def clojure.lang.Range (type (range 1.0))))
@@ -96,7 +99,13 @@
9699
(try (with-meta value {}) true
97100
(catch :default _e false)))
98101

99-
:cljs (implements? IMeta value)))
102+
:cljs (implements? IMeta value)
103+
104+
:lpy
105+
(try (with-meta value {}) true
106+
(catch Exception _e false))))
107+
108+
#?(:lpy (defn- sorted? [_] false))
100109

101110
(defn- hash+ [x]
102111
(cond
@@ -138,7 +147,8 @@
138147
:cljr (instance? clojure.lang.Atom o)
139148
:joyride (= Atom (type o))
140149
:org.babashka/nbb (= Atom (type o))
141-
:cljs (satisfies? cljs.core/IAtom o)))
150+
:cljs (satisfies? cljs.core/IAtom o)
151+
:lpy (instance? basilisp.lang.atom/Atom o)))
142152

143153
(defn- notify [session-id a]
144154
(when-let [request @request]
@@ -212,7 +222,7 @@
212222

213223
(defn- to-object [buffer value tag rep]
214224
(if-not *session*
215-
(cson/-to-json
225+
(cson/to-json*
216226
(with-meta
217227
(cson/tagged-value "remote" (pr-str value))
218228
(meta value))
@@ -235,9 +245,9 @@
235245
:clj
236246
(extend-type java.util.Collection
237247
cson/ToJson
238-
(-to-json [value buffer]
248+
(to-json* [value buffer]
239249
(if-let [id (value->id? value)]
240-
(cson/-to-json (cson/tagged-value "ref" id) buffer)
250+
(cson/to-json* (cson/tagged-value "ref" id) buffer)
241251
(cson/tagged-coll
242252
buffer
243253
(cond
@@ -251,9 +261,9 @@
251261
:clj
252262
(extend-type java.util.Map
253263
cson/ToJson
254-
(-to-json [value buffer]
264+
(to-json* [value buffer]
255265
(if-let [id (value->id? value)]
256-
(cson/-to-json (cson/tagged-value "ref" id) buffer)
266+
(cson/to-json* (cson/tagged-value "ref" id) buffer)
257267
(cson/tagged-map
258268
buffer
259269
"{"
@@ -264,15 +274,16 @@
264274

265275
(extend-type #?(:clj Object
266276
:cljr Object
267-
:cljs default)
277+
:cljs default
278+
:lpy python/object)
268279
cson/ToJson
269-
(-to-json [value buffer]
280+
(to-json* [value buffer]
270281
(to-object buffer value :object nil)))
271282

272283
(defn- has? [m k]
273284
(try
274285
(k m)
275-
(catch #?(:clj Exception :cljr Exception :cljs :default) _e)))
286+
(catch #?(:clj Exception :cljr Exception :lpy Exception :cljs :default) _e nil)))
276287

277288
(defn- no-cache [value]
278289
(or (not (coll? value))
@@ -341,21 +352,24 @@
341352

342353
#_{:clj-kondo/ignore [:unused-private-var]}
343354
(defn- runtime []
344-
#?(:portal :portal :bb :bb :clj :clj :joyride :joyride :org.babashka/nbb :nbb :cljs :cljs :cljr :cljr))
355+
#?(:portal :portal :bb :bb :clj :clj :joyride :joyride :org.babashka/nbb :nbb :cljs :cljs :cljr :cljr :lpy :py))
345356

346357
(defn- error->data [e]
347358
#?(:clj (assoc (Throwable->map e) :runtime (runtime))
348359
:cljr (assoc (Throwable->map e) :runtime (runtime))
349-
:cljs e))
360+
:default e))
350361

351362
(defn update-value [new-value]
352363
(try
353364
(realize-value! new-value)
354365
(swap! tap-list conj new-value)
355-
(catch #?(:clj Exception :cljr Exception :cljs :default) e
366+
(catch #?(:clj Exception :cljr Exception :lpy Exception :cljs :default) e
356367
(swap! tap-list conj
357368
(error->data
358-
(ex-info "Failed to receive value." {:value-type (type new-value)} e))))))
369+
#?(:lpy
370+
(ex-info "Failed to receive value." {:value-type (type new-value)})
371+
:default
372+
(ex-info "Failed to receive value." {:value-type (type new-value)} e)))))))
359373

360374
(def ^:private runtime-keymap (atom ^::no-cache {}))
361375

@@ -372,6 +386,7 @@
372386
#?(:bb "bb"
373387
:clj "jvm"
374388
:cljr "clr"
389+
:lpy "py"
375390
:joyride "joyride"
376391
:org.babashka/nbb "nbb"
377392
:cljs (cond
@@ -431,7 +446,7 @@
431446
(cond-> out
432447
(predicate v)
433448
(assoc name result))
434-
(catch #?(:clj Exception :cljr Exception :cljs :default) _ex out))
449+
(catch #?(:cljs :default :default Exception) _ex out))
435450
(assoc out name result)))))
436451
{}
437452
@registry)
@@ -453,7 +468,7 @@
453468
(a/try
454469
(a/let [return (binding [*session* session] (apply f args))]
455470
(done (assoc (source-info f) :return return)))
456-
(catch #?(:clj Exception :cljr Exception :cljs js/Error) e
471+
(catch #?(:clj Exception :cljr Exception :cljs js/Error :default Exception) e
457472
(done (assoc
458473
(source-info f)
459474
:error
@@ -462,8 +477,7 @@
462477
{::function f
463478
::args args
464479
::found? (some? f)
465-
::data (ex-data e)}
466-
e)
480+
::data (ex-data e)})
467481
datafy
468482
(assoc :runtime (runtime)))))))))
469483

0 commit comments

Comments
 (0)