Fork me on GitHub
#sci
<
2022-10-12
>
Joshua Suskalo19:10:24

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?

teodorlu20:10:00

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

Joshua Suskalo20:10:49

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.

Joshua Suskalo20:10:52

Which is a value I need

Joshua Suskalo20:10:31

I don't see anything about macroexpanding in the api markdown file either

phronmophobic20:10:37

Is there any reason you can't submit the form as (clojure.walk/macroexpand-all (quote ~form)) to get what you need?

phronmophobic20:10:16

or even:

{:expanded (clojure.walk/macroexpand-all (quote ~form))
  :result ~form}

Joshua Suskalo20:10:46

So you mean like read in the expression, wrap it in a macroexpand all, quote it, and then eval that?

Joshua Suskalo20:10:09

I guess I could potentially do that.

Joshua Suskalo20:10:24

is that macroexpand all provided by sci?

Joshua Suskalo20:10:30

I don't think I can provide it in cljs

phronmophobic20:10:51

I tried it in bb

Joshua Suskalo20:10:05

I think this would need nbb

phronmophobic20:10:06

I'm not sure if that means it also applies to sci in cljs

Joshua Suskalo20:10:38

I'll give it a shot a little later

phronmophobic20:10:49

I don't have nbb handy, but should be easy to test

Joshua Suskalo20:10:02

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.

teodorlu20:10:32

(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?

borkdude20:10:36

macroexpand is available inside SCI already

1
Joshua Suskalo20:10:07

Yeah, it's available from inside, my question was about doing macroexpand instead of eval-form

Joshua Suskalo20:10:27

Which yeah, looks like can be done by just wrapping macroexpand and quote before evaling

borkdude20:10:41

yeah, that's how I'd do it

borkdude20:10:04

> I don't have nbb handy npx nbb is all you need :)

teodorlu20:10:28

(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))

Joshua Suskalo20:10:52

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

Joshua Suskalo20:10:18

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

teodorlu20:10:14

I'm still not grasping exactly what you're hoping to achieve :)

borkdude20:10:39

you could also do a walk on the code before you feed it into sci

👍 1
Joshua Suskalo20:10:05

That's what I'd initially thought to do but I need to macroexpand first

Joshua Suskalo20:10:22

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.

borkdude20:10:29

ok, so you do first a macroexpand inside SCI, then walk that expression and then eval that expression in SCI :)

clojure-spin 2
Joshua Suskalo20:10:56

yup, that's the conclusion we came to! Glad that's confirmed to work. 🙂

Joshua Suskalo20:10:50

It seems like I'd be able to do most of this, but I'm not quite sure with a separate macroexpand step