LingDong- / psvg

Programmable Scalable Vector Graphics -- drawings that draw themselves
https://psvg.netlify.app/
301 stars 15 forks source link

Implementing a Full Interpreter? #19

Open LingDong- opened 3 years ago

LingDong- commented 3 years ago

This issue is for discussing the proposal of implementing a full interpreter, to replace the current transpiler to JS. (see #18 )

Some things that I can think of:

Pros:

Cons:

Please feel free to add to this list!

antfu commented 3 years ago

I am working on a Rust implementation of PSVG as my practice of learning it (it was painful, but I starting liking some of the ways of doing things it offers). So far I have implemented the basic interpreter which supports numeric operators and some built-in functions. It's now able to compile koch.psvg and sierpinski.psvg which should cover your points on not relying on JS while being relatively smaller and faster (need to do benchmark later).

Not sure if you are interested in this approach (or will it higher the contribution barrier?), just offering an idea here. It's currently private and I will let you know when I feel more confident about it :P

LingDong- commented 3 years ago

@antfu

Cool! I'm down to give Rust a shot. @0918nobita I saw a thumbs up so perhaps you're thinking the same? ;)

Looks like we're at a real "crossroads" here (even the number of options match that of a crossroad):

What do you guys think?

[1] Great for users, double effort for us. Every time we want to add a feature we need to do it twice?

[2] Then maybe we can use acorn/astring to improve the TypeScript transpiler? Sounds like this might be the best option to me?

[3] Theoretically we can target WebAssembly so node/browser can still use PSVG. Seems like all modern devices are ready for wasm (but how many users of the library will feel ready?). Also, how big is the binary size compared to JS? Also it seems that wasm always need to be asynchronously loaded, which is pretty annoying most of the time.

antfu commented 3 years ago

Yeah, maybe don't take the Rust thing too seriously? I guess it would be much easier to use TypeScript the write the interpreter than Rust (at least to me) also not sure how much perf can Rust gain in return. I am not sure about its future but I might probably continue doing it as I am learning before moving on.

Also mention that if we only keep the syntax a very small subset of JavaScript or PSVG script (numeric operators + functions are actually quite good enough to me), we can probably handwrite them quite easily, and then there might be not needed for introducing heavy parsers like acorn/astring.

LingDong- commented 3 years ago

Aha ok! That makes sense. Sounds like we should proceed with the TypeScript interpreter plan then.

I'm still curious what the implementation will look like in Rust, I bet there's a ton of Option<Rc<RefCell<Box<...>>>> :P

antfu commented 3 years ago

Option<Rc<RefCell<Box<...>>>>

...Yeah exactly. But I found Result<Ok, Err> quite handy in terms of error handling.

Sent you the access invitation if you want to check out :P https://github.com/antfu-sponsors/psvg-rust

0918nobita commented 3 years ago

I'm also in favor of proceeding with the implementation of the interpreter in TypeScript. I have started to implement it in my repository.

By the way, there are many parts of the standard library, such as error handling, that I feel are cumbersome and redundant (compared to that of Rust). If there is a library that enhances the standard library, such as fp-ts (includes Either<A, B> type, which is used like Result<Ok, Err> type), the implementation will be a little easier. (My thinking is greatly influenced by functional programming languages such as F#, OCaml, and Haskell.)

LingDong- commented 3 years ago

Cool! I think fp-ts looks good. (Maybe later if we end up only using a couple features from fp-ts, we can implement those ourselves to make things more compact). Good luck with the implementation! I'm happy to help too.