Syntax
This chapter describes how a Sonata source file is read. The normative definition is the
language specification (Documentation/spec/sonata-language.md) and its grammar
(Documentation/spec/grammar.ebnf); this chapter is the practical version.
Files, items and compositions
A source file is UTF-8 text with the extension .sonata. At the top level it contains
items: imports, constants, functions, patterns, instrument definitions and compositions.
import std.music
const root = D3
fn fifth_above(p: Pitch) -> Pitch {
return p + 7st
}
pattern two_notes(p: Pitch) {
play p duration 1/8
play fifth_above(p) duration 1/8
}
composition "Items" {
instrument harp: Harp {
two_notes(root)
two_notes(root + 12st)
}
}A file without a composition is a module: something other files import. When a file has
several compositions the first one is compiled, unless another is asked for by its title
(sonata check file.sonata --composition "Title").
Comments
// A line comment runs to the end of the line.
/* A block comment can span lines
/* and can be nested, */
which makes it safe to comment out code that already contains comments. */
composition "Comments" {
instrument piano: GrandPiano {
play C4 // comments can follow a statement
}
}Statements and lines
One statement per line. A semicolon separates statements written on the same line:
composition "Lines" {
tempo 100bpm; signature 3/4
instrument piano: GrandPiano {
play C4; play E4; play G4
}
}An expression continues on the next line only when the current line ends with an operator, a comma or an open bracket:
composition "Continuation" {
instrument piano: GrandPiano {
let notes = [C4, E4,
G4, C5]
let step = 1/8 +
1/16
for n in notes {
play n duration step
}
}
}A line that starts with -, ( or [ always begins a new statement, so two statements
are never merged by accident.
Blocks
Braces group statements: the body of a composition, of a track, of a loop. A block opens a scope: a variable declared inside is not visible outside.
Names
Identifiers are made of ASCII letters, digits and _ and do not start with a digit. Two kinds
of words can never be names:
- Reserved words such as
play,tempo,instrument,out. The compiler says so when you try:'out' is a reserved word and cannot be used as a variable name(P1010). - Note and pitch-class shapes.
A4,BbandCare always literals.a4,AmpandCmajare ordinary names.
Built-in function names (min, chord, on, …) cannot be redefined either (S2002).
composition "Reserved" {
instrument piano: GrandPiano {
let out = C4
play out
}
}Literals
| Kind | Examples |
|---|---|
| Integer and decimal | 42, 48_000, 0.75 |
| Exact fraction | 1/4, 3/8 — the division of two integers is exact, never truncated |
| With a unit | 120bpm, 440Hz, 2kHz, -8dB, 5ms, 2s, 7st, 25cents, 60%, 3bars, 1beat, 90deg |
| Note | C4, F#3, Bb2, C-1 — letter, optional #, ##, b, bb, octave −1 to 9 |
| Pitch class | D, F#, Bb — used by key |
| String | "text" with the escapes \n, \t, \", \\, \u{1D11E} |
| Boolean | true, false, on, off |
| List | [C4, E4, G4], [1/8, 1/8, 1/4] |
| Range | 0..4 is 0, 1, 2, 3; 0..=4 includes 4 |
The unit is glued to the number: 120bpm, not 120 bpm. A minus sign is an operator, so
-8dB is the negation of 8dB.
Expressions
composition "Expressions" {
instrument piano: GrandPiano {
let notes = [C4, E4, G4, B4]
let top = notes[len(notes) - 1] // indexing; notes[-1] also means the last
let loud = if top > A4 then 0.9 else 0.6
play notes.first duration 1/4 velocity loud
play top + 12st duration 1/4 velocity loud - 0.2
chord notes + 12st duration 1/2
}
}Operators, from the lowest precedence to the highest: or, and, not, comparisons
(== != < <= > >=), ranges (.., ..=), + -, * / %, unary -, then indexing, member
access (.len, .first, .last, .midi, .octave, .hz) and calls. What each operator
means for each type is in Types and units.
Canonical formatting
sonata fmt file.sonata (or Composition › Format Source, ⇧⌘I) rewrites a file in the
canonical style: four spaces per level, one statement per line, comments kept. Formatting
never changes what a program means and running it twice changes nothing. A file that does not
parse is left untouched.