janet-lang / jpm

Janet Project Manager
MIT License
65 stars 21 forks source link

Add cgen loader and possibility to embed janet in the cgen template #43

Closed pepe closed 2 years ago

pepe commented 2 years ago

This PR shows the possibility of using Janet loaders to import the cgen file and render the C source file from the template.

It also adds a new top directive, $, which will compile and execute code and print the result into the output file. Implementation restricts the evaluation to one form, but that would be an easy fix, and maybe it is the better way?

pepe commented 2 years ago

I tried to also add non-top form Janet reverted it, as I am using print-ir which works only for the top forms.

pepe commented 2 years ago

After some more time spent with this feature, I guess it could be working this way. So I guess I can polish it in the coming days with:

If you think it makes sense at all.

bakpakin commented 2 years ago

Interesting, thanks for the work here!

The emit-janet form looks really flexible and useful for codegen - as for the loader, that is an interesting idea - is the idea to be able to use a .cgen file like a C template, taking advantage of the new $ directive? If that is the case, perhaps it would make sense if the render function could take arguments besides where to write to.

bakpakin commented 2 years ago

Merging for now

pepe commented 2 years ago

Thank you very much. I have not yet used cgen as a template, yet it is very easy to add. I will attempt it tomorrow as i am in transit today.

I have two examples of code usage with loader https://git.sr.ht/~pepe/shriek/tree/master/item/chriek.cgen and https://git.sr.ht/~pepe/jfzy/tree/cgen/item/fzy-reduced.cgen

I must say that even as I am just starting to scratch the surface of possibilities I am very happy with the outcomes.

And as always the work and ideas are mostly yours i am just following your path.