alda-lang / alda

A music programming language for musicians. :notes:
https://alda.io
Eclipse Public License 2.0
5.61k stars 288 forks source link

ASTNode Generation from []ScoreUpdate's #471

Closed Scowluga closed 1 year ago

Scowluga commented 1 year ago

Background

Currently for MusicXML import, we are able to go from MusicXML to Alda []ScoreUpdate. The following 3 PRs combined will introduce an Alda code generator to complete the import process:

  1. https://github.com/alda-lang/alda/pull/471
  2. https://github.com/alda-lang/alda/pull/474
  3. https://github.com/alda-lang/alda/pull/475

This PR is the first step of the process, introducing []ScoreUpdate -> ASTNode conversion via the following function in parser/gen.go:

func GenerateASTFromScoreUpdates(updates []model.ScoreUpdate) (ASTNode, error)

Notes

gen.go

The generation is mostly straightforward, with a few points to note:

  1. As commented, AldaSourceContext is ignored in generation. I think this is a very fine choice.
  2. Processing parts and voices is a little non-trivial. I tried to do it in the cleanest way, but essentially the generator must "reconstruct" an ASTNode because ASTNode.Updates "flattens" this information in ScoreUpdates.

PartUpdate to LispListNode

We only handle conversion for PartUpdate's that are introduced by the MusicXML importer. We also add KeySignature.String() so that we can generate the correct literal for the corresponding LispStringNode of a KeySignatureSet.

test_helper.go

I've slightly refactored/generalized parser's tests in parseTestCase in test_helper.go:

  1. Now providing actual ASTNode or []ScoreUpdate is optional. These are tested if provided. Otherwise, we just test that everything works and that the generated ASTNode is equal the parsed ASTNode.
  2. We now always test applying ScoreUpdates to a Score (with an opt out). I also changed a couple test cases so that they could be applied to a score. Let me know if we want it changed.
  3. examples_test.go which tests the example files now uses parseTestCase.

e2e

We've talked about a complete refactor of testing to spin-off an e2e testing module downstream of both parser and model. I think the changes we have now are "good enough" that this change is not quite worth it for now, given my timeline. I've left it as a TODO.