Implement `compile.cell`
compile.module takes in a "program" as string, parses with parseModule, and outputs a define function to create a runtime module.
compile.cell should take in a "cell" as a string, parse with parseCell, but I'm not sure what it should output.
The output should work for:
- Defining new variables -
module.define - Re-defining variables -
module.redefine importvariables -module.import- Maybe deriving, e.g.
import {...} with {...}? But this probably goes withimportabove -module.derive - Maybe deleting variables? But this doesnt really make sense (since you write code to delete a variable - maybe -
variable.delete
A very rough guess could be:
const define = compile.module(`
a = 1
b = 2
c = 3
`)
const module = runtime.module(define)
await module.value("a") // 1
await module.value("b") // 2
await module.value("c") // 3
let {define, redefine} = compile.cell(`a = b`)
redefine(module)
await module.value("a") // should now be 2
let {define} = compile.cell(`d = c`)
define(module)
await module.value("d") // should be 4 (including redefine example from above)
But idk how import cells could work like this :/
compile.cell could also take in a module as a parameter - for example:
const define = compile.module(`a = 1
b = 2
c= a + b`)
const module = runtime.module(define)
compile.cell(`a = 4`, module)
But I don't like this too much, since 1) how do you distinguish from define/redefine/imports, and 2) I want it to have a similar signature as compile.module (take in a string, return a function you can use to define).
@bryangingechen @davertron I just merged in #12 that implements compile.cell (v0.4.0), feel free to take a look at the new API and comment here about what you think! The README has a new section about how it works, and there's some live examples of it in this notebook under "Redefining cells".
I'm afraid the define/redefine might get confusing, and how define requires an observer to work properly (but redefine doesnt need it). I'm not 100% sure what the usecase for compile.cell would be, but I'd love to hear your thoughts if you have any!
@asg017 Awesome! I'll try and take a look at this in the next couple of days and see if I can work it in.
Just to give you some info about what the use case I have for compile.cell: I've built my own observablehq clone. I did this for a couple of reasons: 1) For fun, to learn more about how it works, and 2) Because I wanted a self-hosted version of this at my company that we could use and have access to our data (there's no way my company would be comfortable having a public tool like the real observablehq having access to our data). I also wanted to be able to modify the stdlib to be able to add helper methods for fetching data from our data sources etc.
Anyway, so I've basically used the runtime and your compiler to make my own version of observablehq, which means that I have an editor where people can create their own notebooks etc., which means I need to be able to define cells and redefine them on the fly as the user edits their notebook.
FWIW, this means I'm also very interested in your work to be able to compile notebooks into a module similar to how observablehq's api does it, because I'll want to allow people to import cells from my own notebooks etc. I was planning on trying to implement that myself at some point, so maybe that's something I could contribute back here instead (or use if someone else is already working on it).
But again, thanks for your awesome work on this, it's super cool and has been a fun learning experience!
@bryangingechen @davertron I just merged in #12 that implements
compile.cell(v0.4.0), feel free to take a look at the new API and comment here about what you think! The README has a new section about how it works, and there's some live examples of it in this notebook under "Redefining cells".
this is nice! for those who want to try compile.notebook I forked your original notebook & added my bits for loading notebook metadata to render it in an iframe. See Compiling Notebook section here ;)
https://observablehq.com/@randomfractals/an-unofficial-observablehq-compiler