Styles of Interpretation

Congratulations, you've written your first compiler!

Now that we've worked through some details, let's think about why we might want to write a compiler for a little language like this. Suppose we've got a bunch of music coded up in MUS format and we want to get the songs playing on our webpages. What can we do?

Interpret MUS Directly

One way to proceed would be to write a JavaScript function playMUS that takes a MUS song as input and plays the notes. This would be a handy function. We could put the script that defines playMUS on our page and then paste in a MUS file directly into our page. We could also write some code to download MUS files from a server and play them. We could even let the user write MUS code in a form and then click a button to play the song.

Precompile MUS to NOTE, interpret NOTE

We could run our lovely compile function on our MUS songs to get a collection of NOTE songs. That doesn't get us all the way to sounds. So we also implement a NOTE interpreter playNOTE. This function takes a NOTE program as input and plays the notes. The advantage here is that the playNOTE interpreter is simpler and more efficient than playMUS.

Online compile MUS to NOTE, interpret NOTE

In this scenario we include our compile function on our webpage and also include a NOTE interpreter playNOTE. We get all the advantages of the pure interpreter approach. We can also pre-compile MUS programs and use the playNOTE interpreter to be more efficient if we want.

Assuming you have compile and playNOTE implemented in JavaScript, implement playMUS in JavaScript.


You're probably over thinking this one. The answer is really simple.

Prev Next