Repository Public
1<!--2ORIX Sonata3File: Documentation/manual/syntax.md4Author: Simon-Pierre Boucher5Contact: contact@spboucher.ai6-->7# Syntax89This chapter describes how a Sonata source file is read. The normative definition is the10language specification (`Documentation/spec/sonata-language.md`) and its grammar11(`Documentation/spec/grammar.ebnf`); this chapter is the practical version.1213## Files, items and compositions1415A source file is UTF-8 text with the extension `.sonata`. At the top level it contains16*items*: imports, constants, functions, patterns, instrument definitions and compositions.1718```sonata19import std.music2021const root = D32223fn fifth_above(p: Pitch) -> Pitch {24 return p + 7st25}2627pattern two_notes(p: Pitch) {28 play p duration 1/829 play fifth_above(p) duration 1/830}3132composition "Items" {33 instrument harp: Harp {34 two_notes(root)35 two_notes(root + 12st)36 }37}38```3940A file without a composition is a *module*: something other files import. When a file has41several compositions the first one is compiled, unless another is asked for by its title42(`sonata check file.sonata --composition "Title"`).4344## Comments4546```sonata47// A line comment runs to the end of the line.48/* A block comment can span lines49 /* and can be nested, */50 which makes it safe to comment out code that already contains comments. */51composition "Comments" {52 instrument piano: GrandPiano {53 play C4 // comments can follow a statement54 }55}56```5758## Statements and lines5960One statement per line. A semicolon separates statements written on the same line:6162```sonata63composition "Lines" {64 tempo 100bpm; signature 3/465 instrument piano: GrandPiano {66 play C4; play E4; play G467 }68}69```7071An expression continues on the next line only when the current line ends with an operator, a72comma or an open bracket:7374```sonata75composition "Continuation" {76 instrument piano: GrandPiano {77 let notes = [C4, E4,78 G4, C5]79 let step = 1/8 +80 1/1681 for n in notes {82 play n duration step83 }84 }85}86```8788A line that *starts* with `-`, `(` or `[` always begins a new statement, so two statements89are never merged by accident.9091## Blocks9293Braces group statements: the body of a composition, of a track, of a loop. A block opens a94scope: a variable declared inside is not visible outside.9596## Names9798Identifiers are made of ASCII letters, digits and `_` and do not start with a digit. Two kinds99of words can never be names:100101* **Reserved words** such as `play`, `tempo`, `instrument`, `out`. The compiler says so when102 you try: `'out' is a reserved word and cannot be used as a variable name` (P1010).103* **Note and pitch-class shapes.** `A4`, `Bb` and `C` are always literals. `a4`, `Amp` and104 `Cmaj` are ordinary names.105106Built-in function names (`min`, `chord`, `on`, …) cannot be redefined either (S2002).107108```sonata,error=P1010109composition "Reserved" {110 instrument piano: GrandPiano {111 let out = C4112 play out113 }114}115```116117## Literals118119| Kind | Examples |120|---|---|121| Integer and decimal | `42`, `48_000`, `0.75` |122| Exact fraction | `1/4`, `3/8` — the division of two integers is exact, never truncated |123| With a unit | `120bpm`, `440Hz`, `2kHz`, `-8dB`, `5ms`, `2s`, `7st`, `25cents`, `60%`, `3bars`, `1beat`, `90deg` |124| Note | `C4`, `F#3`, `Bb2`, `C-1` — letter, optional `#`, `##`, `b`, `bb`, octave −1 to 9 |125| Pitch class | `D`, `F#`, `Bb` — used by `key` |126| String | `"text"` with the escapes `\n`, `\t`, `\"`, `\\`, `\u{1D11E}` |127| Boolean | `true`, `false`, `on`, `off` |128| List | `[C4, E4, G4]`, `[1/8, 1/8, 1/4]` |129| Range | `0..4` is 0, 1, 2, 3; `0..=4` includes 4 |130131The unit is glued to the number: `120bpm`, not `120 bpm`. A minus sign is an operator, so132`-8dB` is the negation of `8dB`.133134## Expressions135136```sonata137composition "Expressions" {138 instrument piano: GrandPiano {139 let notes = [C4, E4, G4, B4]140 let top = notes[len(notes) - 1] // indexing; notes[-1] also means the last141 let loud = if top > A4 then 0.9 else 0.6142 play notes.first duration 1/4 velocity loud143 play top + 12st duration 1/4 velocity loud - 0.2144 chord notes + 12st duration 1/2145 }146}147```148149Operators, from the lowest precedence to the highest: `or`, `and`, `not`, comparisons150(`== != < <= > >=`), ranges (`..`, `..=`), `+ -`, `* / %`, unary `-`, then indexing, member151access (`.len`, `.first`, `.last`, `.midi`, `.octave`, `.hz`) and calls. What each operator152means for each type is in [Types and units](types-and-units.md).153154## Canonical formatting155156`sonata fmt file.sonata` (or **Composition › Format Source**, ⇧⌘I) rewrites a file in the157canonical style: four spaces per level, one statement per line, comments kept. Formatting158never changes what a program means and running it twice changes nothing. A file that does not159parse is left untouched.160