babashka-sci-dev

Crispin 2022-03-06T08:41:40.826309Z

can sci take some code, expand all the macros in it, but not "run" it? As in only the evaluations needed to expand the macros are done, and then you have access to the AST before execution...

Crispin 2022-03-06T08:41:57.189669Z

or is the macroexpansion not a seperate step, and is tied up with evaluation?

borkdude 2022-03-06T10:07:51.353629Z

@retrogradeorbit I guess you could do something like this:

user=> (sci/eval-string "(defmacro dude [x] `[~x ~x]) (clojure.walk/macroexpand-all '(dude 1))")
[1 1]

borkdude 2022-03-06T10:08:06.797649Z

What problem are you trying to solve?

Crispin 2022-03-06T13:59:35.826889Z

Im hatching a massive hack

Crispin 2022-03-06T14:05:13.258209Z

not even sure it will work...

Crispin 2022-03-06T14:08:16.318019Z

Ive been experimenting with different ways to generate wasm

Crispin 2022-03-06T14:08:53.339959Z

but I don't like them

Crispin 2022-03-06T14:09:17.993139Z

last time I used wasm I used C++ and C++ is never enjoyable.

Crispin 2022-03-06T14:09:58.762309Z

and theres this: https://github.com/helins/wasm.cljc

Crispin 2022-03-06T15:22:35.342719Z

is this madness? https://gist.github.com/retrogradeorbit/063f2874eb6155dfe781db9acf1cecad

borkdude 2022-03-06T15:27:05.149519Z

@retrogradeorbit What problem are you trying to solve?

borkdude 2022-03-06T15:27:16.743669Z

(I'll just keep asking the question ;))

borkdude 2022-03-06T15:28:16.512719Z

Oh I see now, a C generator

borkdude 2022-03-06T15:28:28.627439Z

Sorry, I didn't read the title so I couldn't tell if this was C or JavaScript

borkdude 2022-03-06T15:31:11.124929Z

It reminds me of this: https://github.com/dundalek/liz It parses Clojure (using edamame) and generates zig code

👀 1
Crispin 2022-03-06T15:32:06.159079Z

Last time I was making wasm I was using C++ and then I got to the point where I wanted to implement something like this

Crispin 2022-03-06T15:32:55.949529Z

a moving 3x3 window over a 2D array, outputing a central value based on the pattern

Crispin 2022-03-06T15:33:13.348489Z

and this is quite straight forwards with pattern matching

Crispin 2022-03-06T15:33:18.857479Z

but C++ doesnt have that

Crispin 2022-03-06T15:33:26.680359Z

and it quickly descended into hell

Crispin 2022-03-06T15:33:58.739929Z

and my C++ friend when I asked him just said, "nah you're stuffed. write it all out by hand"

Crispin 2022-03-06T15:34:18.013619Z

my kingdom for a proper macro system

Crispin 2022-03-06T15:34:41.928289Z

If I can generate C, I can implement a pattern matcher as a macro

Crispin 2022-03-06T15:35:06.732299Z

I will check out this liz... see how its doing its magic

borkdude 2022-03-06T15:36:02.117229Z

why not generate the code once using a macro or whatever templating thing and then inline that code

borkdude 2022-03-06T15:36:18.969429Z

seems like a lower hanging fruit than implementing a Clojure -> C transpiler

Crispin 2022-03-06T15:37:06.874049Z

yeah so thats an option... but then it gets unwieldy

Crispin 2022-03-06T15:37:24.551469Z

as you have half C++ files, half generated snippets.. that get mashed together

Crispin 2022-03-06T15:37:52.096609Z

implementing a Clojure -> C transpiler does sound like madness, yes

borkdude 2022-03-06T15:38:15.128289Z

this is effectively what liz is, transpiling to C shouldn't be much harder perhaps

Crispin 2022-03-06T15:38:17.194339Z

would it even be possible for have core.match emit the C code

Crispin 2022-03-06T15:38:30.904029Z

yeah, the aim is to not implement a lisp in C

Crispin 2022-03-06T15:38:52.706259Z

just to write C using s-exp

Crispin 2022-03-06T15:39:02.660949Z

and then, some macros over the top

borkdude 2022-03-06T15:39:15.615829Z

that is effectively what liz is ;)

Crispin 2022-03-06T15:39:26.608529Z

Im gonna read some liz source

Crispin 2022-03-06T15:40:37.041139Z

you know, its not crazy huge

Crispin 2022-03-06T15:40:47.853179Z

its quite small

Crispin 2022-03-06T15:42:05.390779Z

you know what, I should try using liz

Crispin 2022-03-06T15:42:17.480939Z

see what its like to use liz to generate wasm

borkdude 2022-03-06T15:43:14.315539Z

yeah, let me know how that goes :)

borkdude 2022-03-06T15:45:21.394419Z

Maybe using Common Lisp for this would also make sense, given that you can get quite close performance to C using it and there may be WASM bindings for it

borkdude 2022-03-06T15:46:33.968219Z

but generating C would be most optimal I guess

Crispin 2022-03-06T15:51:30.067609Z

one approach is to build up wasm from the ground up. wasm has a format wast that is s-expr. It's spec is very lispy already. You would deal directly in the wasm types and map clj straight to wasm ast. This is somewhat like helins/wasm.cljc does

Crispin 2022-03-06T15:51:46.792979Z

the other is to transpile to another supported lang, and then compile that down to wasm

Crispin 2022-03-06T15:52:24.803499Z

the first is probably more elegant, but the advantage of the second is the existing toolchains (like emscripten) work magic on the C code

Crispin 2022-03-06T15:53:24.976119Z

you've got your advanced optimiser already done, and whats really great is you can bring across your stdlib malloc/free heap allocator into the wasm code to manage the wasm "linear memory"

Crispin 2022-03-06T15:53:58.061239Z

ie, you can malloc and free in your wasm code, then access that data from the JS side. Its shared.

Crispin 2022-03-06T15:54:41.643529Z

and if you decompile the C generated wasm, you can see the entire libc heap allocator expressed inside the wasm

Crispin 2022-03-06T15:55:27.487269Z

so theres all this work that just kind of comes for free with transpiling first

Crispin 2022-03-06T15:55:43.121079Z

zig looks good though!

Crispin 2022-03-06T15:56:07.782889Z

and if liz supports me writing macros... that can solve my problem

borkdude 2022-03-06T16:17:11.406389Z

liz doesn't support macros I think :/

borkdude 2022-03-06T16:17:46.729609Z

at one point liz listed SCI as an option to execute macros during transpilation, not sure if Jakub explored that option, but don't see it mentioned anymore