Does using sci from cljs work the same way as from clj?
I'm trying to determine if it fits the requirements I have for a project where I need a tiny lisp interpreter.
More or less I need to be able to evaluate small lisp expressions with some custom functions injected, but I also need to be able to walk it after macroexpansion and find any usages of what's effectively a new special form prop and extract that information before evaluation.
Is that reasonably within reach with sci?
To the best of my understanding, I think that should be possible. At least, I don't know of any reason why it's not possible. A specific example would help :) Here's the sci README on macros: https://github.com/babashka/sci#macros
Yeah, I was reading through this and I can see macros are supported, but I haven't been able to tell if there's any way to get the non-evaluated macroexpanded expression.
Which is a value I need
I don't see anything about macroexpanding in the api markdown file either
Is there any reason you can't submit the form as (clojure.walk/macroexpand-all (quote ~form)) to get what you need?
or even:
{:expanded (clojure.walk/macroexpand-all (quote ~form))
:result ~form}So you mean like read in the expression, wrap it in a macroexpand all, quote it, and then eval that?
yea
I guess I could potentially do that.
is that macroexpand all provided by sci?
I don't think I can provide it in cljs
I tried it in bb
I think this would need nbb
I'm not sure if that means it also applies to sci in cljs
I'll give it a shot a little later
I don't have nbb handy, but should be easy to test
Yeah, I'll test it out. If that works, then I think I can use sci as-is. If not, then I might need to look at the inner workings and think about a PR to add a macroexpand to the API.
(require '[sci.core :as sci])
(let [ctx (sci/init {})]
(sci/eval-form ctx '(do (defmacro lol [y] y)
(def x 123)
{:x-stuff (* x (lol x))
:macro-stuff (macroexpand (quote (lol :abc)))})))
;; => {:x-stuff 15129, :macro-stuff :abc}
Something like this?yea
macroexpand is available inside SCI already
Yeah, it's available from inside, my question was about doing macroexpand instead of eval-form
Which yeah, looks like can be done by just wrapping macroexpand and quote before evaling
yeah, that's how I'd do it
> I don't have nbb handy
npx nbb is all you need :)
(defn macroexpand-in-sci [code]
(let [ctx (sci/init {})]
(sci/eval-form ctx
`(macroexpand (quote ~code)))))
(macroexpand-in-sci '(when true
:abc))
;; => (if true (do :abc))My issue here is I'm basically writing a bunch of tiny separate sci expressions which need to be able to refer to stuff that's provided outside of the sci context by way of this prop special function but I have to be able to find usages of prop outside the context of sci
although putting some more thought into that I might even need to mess with that a little more because of the potential for constructing symbols at runtime to pass to prop
I'm still not grasping exactly what you're hoping to achieve :)
you could also do a walk on the code before you feed it into sci
That's what I'd initially thought to do but I need to macroexpand first
This is just one tiny part of a very complex whole that I'm trying to work out a spec for and determine exactly what components need what features. ATM it's looking like sci can do what I need from an interpreter though.
ok, so you do first a macroexpand inside SCI, then walk that expression and then eval that expression in SCI :)
yup, that's the conclusion we came to! Glad that's confirmed to work. 🙂
It seems like I'd be able to do most of this, but I'm not quite sure with a separate macroexpand step