@kevin.van.rooijen it doesn’t, but I think for this kind of simple transformation it’s really better to write your own function that walks the structure instead of using sci for this
👍
@kevin.van.rooijen I think you can adapt this function from sci to create your own transpiler:
(defn analyze
[ctx expr]
;; (prn "ana" expr)
(let [ret (cond (constant? expr) expr ;; constants do not carry metadata
(symbol? expr) (let [v (resolve-symbol ctx expr false)]
(cond (constant? v) v
;; (fn? v) (utils/vary-meta* v dissoc :sci.impl/op)
(vars/var? v) (if (:const (meta v))
@v (types/->EvalVar v))
:else (merge-meta v (meta expr))))
:else
(merge-meta
(cond
(map? expr)
(-> (zipmap (analyze-children ctx (keys expr))
(analyze-children ctx (vals expr)))
mark-eval)
(or (vector? expr) (set? expr))
(-> (into (empty expr) (analyze-children ctx expr))
mark-eval)
(and (seq? expr) (seq expr))
(analyze-call ctx expr)
:else expr)
(select-keys (meta expr)
[:line :column :tag])))]
;; (prn "ana" expr '-> ret 'm-> (meta ret))
ret))
You can also keep track of the depth, etc
Currently at work so I can't look at this in depth. But I think you're suggesting to extract sci code and use that instead of using sci itself?
Yes. Just walk your structure like in the above snippet. If it's a map, use zipmap, if it's a seq, treat it like a function call, etc.
I'll take a look at Sci and see how it does it, always good to read other people's code. I already have a working implementation, I was mostly just tripping over the macro part. Last night I was able to figure out a decent solution though.
Currently my transpiler supports all the language features of the target language. I now need to implement more clojure specific features. Which is quite a challenge for such a limited target language (but very educational)
Is there a Sci variation of the backquote? When working with Sci macros the ` doesn’t work as desired. E.g. for unknown keywords it will use the current clojure namespace which is not the current Sci namespace
I’m thinking of writing another macro for this if this doesn’t exist
Can you make an example? Could be a bug, backquote should work as in Clojure.
No it is not a bug as far as i know. It’s more of a usability issue
Like the following doesn’t work intuitively
(def my-macro ^:sci/macro true
(fn [_ _ form]
`(some-other-ns-function form)
))
Yeah, that's not something sci can do anything about, it's evaluated outside of it
yeah I was hoping to find some way of dealing with this, but the current-ns is not available either
My current efforts to write a macro for this are not very succesful yet. I was thing of having a macro where you pass the desired ns as an argument
Something like this
(defn sci-backquote* [current-ns form]
(clojure.walk/postwalk (fn [x]
(if (symbol? x)
(if (namespace x)
x
(let [n (name x)]
(if (clojure.string/ends-with? x "#")
x
(if (resolve (symbol "clojure.core" n))
x
(symbol current-ns n)))))
x))
form))
(defmacro sci-backquote [ns & form]
`(quote (do ~@(sci-backquote* (str ns) form))))
It’s not working 🙂
Maybe just use fully qualified symbols in your macros? That's how I tend to do it
^:sci/macro true
should just be ^:sci/macro
btwah good catch 🙂 thanks
yeah I use fully qualified symbols now, but it becomes a bit tedious. It’s ok for now
Not much too complain otherwise. Sci is working really well 🙂
Cool! I do want to look at that iterate-max thing soon, but first I'm working on getting a proper stacktrace, which is a bit harder than I thought :)
and now it's ff-ing hot...
haha yeah brain is frying
btw I think spire also has some logic around this same problem
for macros
ah i’ll have a look