slurmulon / bach

:musical_score: Semantic music notation
http://codebach.tech
MIT License
7 stars 0 forks source link

Research pragmatic bach.json to bach conversions in CLJS #26

Open slurmulon opened 3 years ago

slurmulon commented 3 years ago

Problem

Right now the conversion to bach to bach.json is straight-forward, however the reverse conversion is not.

It's most noticeable in JS land, since only bach.track/compose is exported right now.

If you wanted changes to bach.json to be reflected in bach source code, you would need to use hacky regex patterns to perform the search and replacements in the bach source code.

If you're using a reactive web framework like Vue or React then you can be more declarative using string templates, but it still involves reflecting/re-implementing what bach.ast does already. The resulting code code is still dense and brittle, and the developer should not be expected to write it.

Solution

bach.ast, and the instaparse hiccup tree it provides, is already the lowest-level data structure in bach, and it can naturally be used to support bach.json -> bach conversions.

It would involve creating a new method in the bach core library that takes composed bach.json and then converts it bach into an AST.

This AST can then be used to re-compose the bach data.

This involves updating shadow-cljs and the bach-cljs package in general to export not only bach.ast but also any new modules or methods that are introduced with this work.

Alternative

We could achieve the same resulting by baking all of the string template and regex pattern weirdness into bach, but that solution is not particularly pleasant or speedy to write. It does, however, have better performance than converting bach.json into both bach.ast and bach, and that's why I mention it here to keep in mind.