sci

https://github.com/babashka/SCI - also see #babashka and #nbb
Markus Agwin 2021-05-31T13:40:21.078800Z

In attempting to create a Scittle plugin, (sci/copy-var some-macro sci-created-ns) (where some-macro is a symbol created by :refer-macros) results in an "Unable to resolve var" error. Just to be sure: it's not possible to expose macros of an arbitrary cljs-library in a Scittle plugin, right?

borkdude 2021-05-31T13:47:19.079300Z

To be sure, just re-implement the macro in terms in CLJS as a normal function

borkdude 2021-05-31T13:47:39.079600Z

and then use :sci/macro as metadata on that function to expose it to sci

borkdude 2021-05-31T13:48:20.080500Z

(defn my-macro [_form _env x y & zs] `(do ...))
(with-meta my-macro {:sci/macro true})

🙏 1
Markus Agwin 2021-06-01T08:48:31.080900Z

I cannot get this to work:

(ns scittle.makro-plugin
  (:require
   [sci.core :as sci]
   [scittle.core :as scittle]))

(defn add-low-fn [_env _form x y & zs] `(str "__" ~x ~y ~zs))
(def add-low-makro (with-meta add-low-fn {:sci/macro true}))

(def rns (sci/create-ns 'makro-plugin.core nil))

(def makro-plugin-namespace
  {'add-low-fn (sci/copy-var add-low-fn rns)
   'add-low-makro (sci/copy-var add-low-makro rns)})

(scittle/register-plugin!
  ::makro-plugin
  {:namespaces {'makro-plugin.core makro-plugin-namespace}})
add-low-makro behaves the same way as add-low-fn . Do I need to add something in order to SCI recognise a macro?

borkdude 2021-06-01T08:53:17.081200Z

you should not use copy-var with add-low-makro

borkdude 2021-06-01T08:53:24.081400Z

as it only copies the var metadata to the sci var

borkdude 2021-06-01T08:56:49.081900Z

@markus.agwin So:

scittle.makro-plugin=> (defn add-low-fn [_env _form x y & zs] `(str "__" ~x ~y ~@zs))
#'scittle.makro-plugin/add-low-fn
scittle.makro-plugin=> (sci/eval-string "(add-low-makro 1 2 3 4)" {:bindings {'add-low-makro (with-meta add-low-fn {:sci/macro true})}})
"__1234"

borkdude 2021-06-01T08:59:17.082300Z

This also works:

scittle.makro-plugin=> (defn ^:macro  add-low-fn [_env _form x y & zs] `(str "__" ~x ~y ~@zs))
#'scittle.makro-plugin/add-low-fn
scittle.makro-plugin=> (sci/eval-string "(add-low-makro 1 2 3 4)" {:bindings {'add-low-makro (sci/copy-var add-low-fn rns)}})
"__1234"

Markus Agwin 2021-06-01T09:28:55.082500Z

Great! For the record, this works now:

(ns scittle.makro-plugin
  (:require
   [sci.core :as sci]
   [scittle.core :as scittle]))

(defn add-low-fn [_env _form x y] `(str "_" ~x ~y))
(defn ^:macro add-low-makro [_env _form x y] (add-low-fn _env _form x y))

(def rns (sci/create-ns 'makro-plugin.core nil))

(def makro-plugin-namespace
  {'add-low-fn (sci/copy-var add-low-fn rns)
   'add-low-makro (sci/copy-var add-low-makro rns)})

(scittle/register-plugin!
  ::makro-plugin
  {:namespaces {'makro-plugin.core makro-plugin-namespace}})

borkdude 2021-06-01T09:30:20.082700Z

:thumbsup:

Markus Agwin 2021-06-01T13:45:42.082900Z

@borkdude is env meant to be empty when I print it via

(defn add-low-fn [form env x y] 
`(str "_ form: " ~(str form) " env: " ~(str env) ~x ~y))
? (at least I got the form<->env order right this time)

borkdude 2021-06-01T13:47:52.083100Z

it depends where you call it from

borkdude 2021-06-01T13:48:00.083300Z

it contains the locals

borkdude 2021-06-01T13:48:26.083500Z

these arguments are called &amp;form and &amp;env in normal Clojure macros

Markus Agwin 2021-06-01T14:00:05.083800Z

But in this specific Scittle example I cannot improve my code to (for example) have the name of some namespace appear via (:name (:ns env)), right?

borkdude 2021-06-01T14:01:55.084Z

Maybe this example helps? https://gist.github.com/borkdude/c97da85da67c7bcc5671765aef5a89ad

Markus Agwin 2021-06-01T14:19:44.084200Z

that helps, thank you!