Interpreters and Compilers

How do programs get run? How does code turn into actions? To get some practice thinking about program transformation, interpretation, and compilation we'll work out an example.

MUS is a little made-up language for representing musical notes. Programs in MUS are made up of expressions. The simplest expression is a single note. The language has a syntax, but we'll ignore it for now to avoid dealing with parsing. After being parsed, a single note looks like this.

Every note has a pitch and duration. The pitch is expressed using scientific pitch notation and the duration is specified in milliseconds.

To make melodies, MUS lets you combine two expressions together with the sequence operator to create new expressions. Here is a sequence of two notes.

The sequence operator means play the expression on the left, then play the expression on the right. The expression on the left or the right could itself be a seq or a note.

What about three notes in a row? Here are the three notes of a C major chord in a row.

Write a function prelude that takes a music expression expr as input and returns an expression that means to play a d4 note for 500 milliseconds and then play expr.

1:1

The 'seq' operator is your friend.



Next