forked from synthia-synth/synthia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.y
349 lines (323 loc) · 6.43 KB
/
lang.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
%{
package main
import (
"log"
"strconv"
"bytes"
"unicode/utf8"
)
var stm = false
var line int = 1
%}
%union {
stream *astStream
str string
instructions []instruction
inst instruction
expr expression
expressions []expression
integer int
instumentDef instrument
headers []header
headerField header
note *noteExpression
notes []*noteExpression
}
%token '(' ')' '{' '}' '[' ']' '.' '=' ';'
%token STREAM
%token <str> LABEL
%token <integer> NUM
%token <expr> TIMING
%token <note> NOTE
%type <stream> strm
%type <instructions> inst_list
%type <inst> inst
%type <expr> expr chord
%type <expressions> expr_list
%type <instumentDef> definition
%type <headers> top head_list
%type <headerField> head
%type <note> note_el
%type <notes> note_list
%%
top: head_list
{ ast = AST($1) }
head_list:
head ';'
{
$$ = []header{$1}
}
| head_list head ';'
{
$$ = append($1, $2)
}
head:
strm {
stm = true
$$ = $1
}
| LABEL '(' expr_list ')'
{
stm = true
$$ = &functionCall{label: $1, arguments: $3}
}
| LABEL '=' definition
{
stm = true
$$ = &instrumentInstance{label: $1, inst: $3}
}
strm:
STREAM LABEL '{' inst_list '}'
{
$$ = &astStream{label: $2, instructions: $4}
}
inst_list:
inst ';'
{
$$ = []instruction{$1}
}
| inst_list inst ';'
{
$$ = append($1, $2)
}
inst:
LABEL '.' LABEL '(' expr_list ')'
{
stm = true
$$ = &methodCall{obj: &object{label: $1}, method: $3, arguments: $5}
}
| LABEL '(' expr_list ')'
{
stm = true
$$ = &functionCall{label: $1, arguments: $3}
}
| LABEL '=' definition
{
stm = true
$$ = &instrumentInstance{label: $1, inst: $3}
}
definition:
LABEL '.' LABEL
{
i, err := instrumentLookup($1, $3)
if err != nil {
log.Fatalf("Instrument lookup with (%s,%s) failed: %s\n", $1, $3, err)
}
$$ = i
}
expr_list:
expr
{
$$ = []expression{$1}
}
| expr_list ',' expr
{
$$ = append($1, $3)
}
expr:
note_el { $$ = $1 }
| NUM { $$ = intExp($1) }
| TIMING { $$ = $1 }
| chord { $$ = $1 }
chord:
'(' note_list ')' { $$ = &chordExpression{notes: $2} }
note_list:
note_el { $$ = []*noteExpression{$1} }
| note_list ',' note_el { $$ = append($1, $3) }
note_el:
NOTE
{
$$ = $1
}
%%
// The parser expects the lexer to return 0 on EOF. Give it a name
// for clarity.
const eof = 0
// The parser uses the type <prefix>Lex as a lexer. It must provide
// the methods Lex(*<prefix>SymType) int and Error(string).
type langLex struct {
line []byte
peek rune
}
// The parser calls this method to get each new token. This
// implementation returns operators and NUM.
func (x *langLex) Lex(yylval *langSymType) int {
for {
c := x.next()
switch c {
case eof:
return eof
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return x.num(c, yylval)
case '+', '-', '*', '/', '(', ')', ';', '[', ']', '.', '{', '}', '=', ',':
return int(c)
// Recognize Unicode multiplication and division
// symbols, returning what the parser expects.
case '×':
return '*'
case '÷':
return '/'
case '\n':
if stm {
x.peek = ';'
stm = false
}
line++
case ' ', '\t', '\r':
default:
return x.label(c, yylval)
}
}
}
// Lex a number.
func (x *langLex) num(c rune, yylval *langSymType) int {
add := func(b *bytes.Buffer, c rune) {
if _, err := b.WriteRune(c); err != nil {
log.Fatalf("WriteRune: %s", err)
}
}
var b bytes.Buffer
add(&b, c)
L: for {
c = x.next()
switch c {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
add(&b, c)
default:
break L
}
}
if c != eof {
x.peek = c
}
var err error
yylval.integer, err = strconv.Atoi(b.String())
if err != nil {
log.Fatalf("bad number %q, error: %s", b.String(), err)
return eof
}
return NUM
}
func (x *langLex) label(c rune, yylval *langSymType) int {
add := func(b *bytes.Buffer, c rune) {
if _, err := b.WriteRune(c); err != nil {
log.Fatalf("WriteRune: %s", err)
}
}
var b bytes.Buffer
add(&b, c)
L: for {
c = x.next()
switch c {
case ' ', '.', '\t', '+', '-', '*', '/', '(', ')', '\n', '[', ']', '{', '}', '=', ',', eof:
break L
default:
add(&b, c)
}
}
if c != eof {
x.peek = c
}
yylval.str = b.String()
timing, isTiming := timingLookup[yylval.str]
if isTiming {
modifier := NormalLength
c = x.next()
if c == '.' {
modifier = Dotted
} else {
x.peek = c
}
yylval.expr = &timingExpression{ timing: timing, modifier: modifier}
return TIMING
}
note, isNote := noteLookup[yylval.str]
if isNote {
c = x.next()
if c != '['{
log.Fatalf("Invalid Note %s with no octave. Should be %s[n] where n is a single-digit integer\n", yylval.str, yylval.str)
}
c = x.next()
var octave int
switch c {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
var err error
octave, err = strconv.Atoi(string(c))
if err != nil {
log.Fatal("Critical Error: Integer is not an Integer!\n")
}
default:
log.Fatalf("Invalid Note %s with no octave. Should be %s[n] where n is a single-digit integer\n", yylval.str, yylval.str)
}
c = x.next()
if c != ']'{
log.Fatalf("Invalid Note %s with no octave. Should be %s[n] where n is a single-digit integer\n", yylval.str, yylval.str)
}
acc := AccidentalNatural
c = x.next()
if c == '.' {
var a bytes.Buffer
LA: for {
c = x.next()
switch c {
case ' ', '.', '\t', '+', '-', '*', '/', '(', ')', '\n', '[', ']', '{', '}', ',', '=', eof:
break LA
default:
add(&a, c)
}
}
ac, isAccidental := accidentalLookup[a.String()]
if !isAccidental {
log.Fatalf("%s is not a valid accidental\n", a.String())
}
if c != eof {
x.peek = c
}
acc = ac
} else {
x.peek = c
}
yylval.note = ¬eExpression{ note: note, octave: octave, accidental: acc }
return NOTE
}
if b.String() == "stream" {
return STREAM
}
return LABEL
}
// Return the next rune for the lexer.
func (x *langLex) next() rune {
if x.peek != eof {
r := x.peek
x.peek = eof
return r
}
if len(x.line) == 0 {
return eof
}
c, size := utf8.DecodeRune(x.line)
x.line = x.line[size:]
if c == utf8.RuneError && size == 1 {
log.Print("invalid utf8")
return x.next()
}
return c
}
// The parser calls this method on a parse error.
func (x *langLex) Error(s string) {
c := x.next()
var cString string
switch c {
case ' ':
cString = "space"
case '\n':
cString = "\\n"
case '\t':
cString = "\\t"
case eof:
cString = "EOF"
default:
cString = string(c)
}
log.Printf("parse error on line %d before character %s: %s", line, cString, s)
}