Skip to content

Commit 4df76e3

Browse files
committed
Changed comment syntax to /* ... */ and added new sequence generator
Small fixes and changed comment syntax to /* ... */. Also added new generator for inventory sequence.
1 parent b06e81e commit 4df76e3

File tree

7 files changed

+94
-37
lines changed

7 files changed

+94
-37
lines changed

lib/enumerables.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,29 @@ def dress(e=Float::INFINITY)
433433
(0..e).lazy.collect {|n| A001316(n) }
434434
end
435435

436+
# Dress sequence https://oeis.org/A001316
436437
def A001316(n)
437438
return n+1 if n <= 1
438439
A001316(n/2) << n%2
439440
end
440441

442+
def inventory(e=Float::INFINITY)
443+
(0..e).lazy.collect {|n| A342585(n) }
444+
end
445+
446+
# Inventory sequence: http://oeis.org/A342585
447+
def A342585(n)
448+
values = []
449+
current_val = 0
450+
new_val = 0
451+
n.times do |i|
452+
new_val = values.count(current_val)
453+
values.append(new_val)
454+
current_val = new_val == 0 ? 0 : current_val+=1
455+
end
456+
new_val
457+
end
458+
441459
# https://oeis.org/A030101
442460
def binrev(base=2)
443461
Enumerator.new do |y|

lib/parser/generative.treetop

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,17 @@ grammar GenerativeSyntax
113113
end
114114
end
115115

116+
# Suffle and take random or pick random
116117
if p and p.text_value != ""
117-
set = set.shuffle if p.text_value[0]=="~"
118-
set = p.text_value[0]=="~" ? set.take(p.text_value[1..].to_i) : set.pick(p.text_value[1..].to_i) if p.text_value.length>1
118+
if p.text_value[0]=="~"
119+
set = set.shuffle
120+
set = set.take(p.text_value[1..].to_i) if p.text_value.length>1
121+
else
122+
set = p.text_value.length>1 ? set.pick(p.text_value[1..].to_i) : set.pick(1)
123+
end
119124
end
120125

126+
# Operations
121127
if !o.empty? then
122128
o.elements.each do |op|
123129
if op.methods.include?(:value) then
@@ -447,6 +453,7 @@ grammar GenerativeSyntax
447453
}
448454
end
449455

456+
# Deprecated. Still works but consider using (1 2 3)? or (1 2 (3 3))~4 instead.
450457
rule array
451458
'[' first:(set_items) rest:(',' i:set_items)+ ']'
452459
{
@@ -556,7 +563,7 @@ grammar GenerativeSyntax
556563

557564
# Characters ignored by generative parsing and parsed using ziffers parser
558565
rule ignore
559-
v:(reset / slide / arpeggio / ignore_function / ignore_comment / ignore_params)
566+
v:(ignore_comment / reset / slide / arpeggio / ignore_function / ignore_params)
560567
{
561568
def value
562569
v.value.to_s
@@ -614,10 +621,28 @@ grammar GenerativeSyntax
614621
end
615622

616623
rule ignore_comment
617-
'<!' [a-zA-Z0-9_\-\s\n\r\|#+\-*]+ '>'
624+
comment:(multi_line_comment / one_line_comment)
618625
{
619626
def value
620-
text_value
627+
comment.value
628+
end
629+
}
630+
end
631+
632+
rule multi_line_comment
633+
'/*' ((s [*] s) / [a-zA-Z0-9_\-\|#+\-] / s)+ '*/'
634+
{
635+
def value
636+
nil
637+
end
638+
}
639+
end
640+
641+
rule one_line_comment
642+
'//' ([^\n]*)
643+
{
644+
def value
645+
nil
621646
end
622647
}
623648
end
@@ -1261,13 +1286,13 @@ end
12611286
end
12621287

12631288
rule zchar
1264-
s:([mklpdcwyhnqaefsxtgujz]+)
1265-
{
1266-
def value
1267-
sum = s.text_value.split('').inject(0){|sum,x| sum + Thread.current[:default_durs][x.to_sym]}
1268-
sum.to_f
1269-
end
1270-
}
1289+
s:([mklpdcwyhnqaefsxtgujz]+)
1290+
{
1291+
def value
1292+
sum = s.text_value.split('').inject(0){|sum,x| sum + Thread.current[:default_durs][x.to_sym]}
1293+
sum.to_f
1294+
end
1295+
}
12711296
end
12721297

12731298
end

lib/parser/zgrammar.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def parse_ziffers(text, opts, shared)
165165
zlog text
166166
zlog opts
167167
zlog shared
168-
raise "Invalid syntax after: "+parse_failure(zparser.failure_reason)+ ". Check parentheses!"
168+
raise "Invalid syntax after: "+parse_failure(zparser.failure_reason)
169169
end
170170

171171
ziffers = ZiffArray.new(result.value)
@@ -235,7 +235,7 @@ def parse_generative(text, opts={}, shared={}, return_shared_opts=false)
235235
zlog text
236236
zlog opts
237237
zlog shared
238-
raise "Invalid syntax after: "+parse_failure(rparser.failure_reason)+ ". Check parentheses!"
238+
raise "Invalid syntax after: "+parse_failure(rparser.failure_reason)
239239
end
240240
return_shared_opts ? [result.value,Thread.current[:tshared]] : result.value
241241
end
@@ -257,7 +257,7 @@ def parse_params(text,opts={})
257257
zlog lparser.failure_line
258258
zlog lparser.failure_column
259259
zlog text
260-
raise "Invalid syntax after: "+parse_failure(lparser.failure_reason)+ ". Check parentheses!"
260+
raise "Invalid syntax after: "+parse_failure(lparser.failure_reason)
261261
end
262262

263263
result.value
@@ -276,7 +276,7 @@ def unroll_repeats(text)
276276
zlog repeatparser.failure_line
277277
zlog repeatparser.failure_column
278278
zlog text
279-
raise "Invalid syntax after: "+parse_failure(repeatparser.failure_reason)+ ". Check parentheses!"
279+
raise "Invalid syntax after: "+parse_failure(repeatparser.failure_reason)
280280
end
281281
result.value
282282
end

lib/parser/ziffers.treetop

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
grammar Ziffers
22

33
rule line
4-
(s / comment / opt_list / continue / bar / relative_duration / multi_duration / multi_scale / arpeggio / chords / pitch_classes / control / multiuse / use / multi_escaped_opts / fixed_octave / numeric_octave / call / eval / rest / slide / reset / skip)*
4+
(comment / s / opt_list / continue / bar / relative_duration / multi_duration / multi_scale / arpeggio / chords / pitch_classes / control / multiuse / use / multi_escaped_opts / fixed_octave / numeric_octave / call / eval / rest / slide / reset)*
55
{
66
def value
77
e = elements.collect {|v| v.value }.flatten.compact
@@ -168,7 +168,25 @@ grammar Ziffers
168168
end
169169

170170
rule comment
171-
'<!' [a-zA-Z0-9_\-\s\n\r\|#+\-*]+ '>'
171+
comment:(multi_line_comment / one_line_comment)
172+
{
173+
def value
174+
comment.value
175+
end
176+
}
177+
end
178+
179+
rule multi_line_comment
180+
'/*' ((s [*] s) / [a-zA-Z0-9_\-\|#+\-] / s)+ '*/'
181+
{
182+
def value
183+
nil
184+
end
185+
}
186+
end
187+
188+
rule one_line_comment
189+
'//' ([^\n]*)
172190
{
173191
def value
174192
nil
@@ -577,20 +595,9 @@ end
577595
}
578596
end
579597

580-
rule skip
581-
'# '
582-
{
583-
def value
584-
Thread.current[:topts][:skip] = Thread.current[:topts][:skip] ? false : true
585-
nil
586-
end
587-
}
588-
end
589-
590598
rule bar
591599
'|' {
592600
def value
593-
Thread.current[:topts].except!(:skip)
594601
Thread.current[:topts][:octave] = 0
595602
Thread.current[:topts][:duration] = Thread.current[:topts_orig][:duration] ? Thread.current[:topts_orig][:duration] : 0.25
596603
Thread.current[:topts][:measure] = !Thread.current[:topts][:measure] ? 0 : Thread.current[:topts][:measure] += 1

test/run_wiki_test_multi.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
Ziffers.debug
44

55
def test_play
6-
6+
77
# Multistaff
8-
8+
99
a = parse_rows "
1010
/ synth: :pretty_bell
1111
[:q 2 2 2 4 q. 4 e 3 h 2 q 2 1 2 4 q. 2 e 1 h 0 :] [: 4 e 5 4 3 2 h 3 e 4 3 2 1 h 2 e 3 2 1 0 q. 1 e _ 4 h 4 q ^ 0 1 2 3 h 2 1 :]
@@ -14,7 +14,7 @@ def test_play
1414
[: q 0 0 2 2 1 _ 4 h ^ 0 q 0 _ 4 ^ 0 _ 2 4 4 h ^ 0 :] [: h 0 q _ 5 ^ 0 h 1 q _ 4 6 h ^ 0 q _ 3 5 6 r 4 r 2 4 r 3 h 0 4 :] / octave: -1
1515
"
1616
assert_equal(a[:rows].length,4)
17-
17+
1818
a = parse_rows "
1919
/ synth: :kalimba
2020
h H X / X: :bd_808, H: {sample: :drum_cowbell, amp: 0.025}
@@ -24,7 +24,7 @@ def test_play
2424
h0 q 0 _ 6 h 5 4 5 3 q 5 6 h ^0 4 ^ 0 0 1 1 w. _4 h 0 h. _ 3 q 3 h 1 q 2 3 h 4 5 4 ^0 4 5 3 4 w. ^ 0
2525
"
2626
assert_equal(a[:rows].length,5)
27-
27+
2828
a = parse_rows "
2929
/ synth: :piano
3030
|[: q 2 2 1 0 | h _ 6 5 | q 2 2 #3 #4 | h 5 #4 | q 5 ^ 0 q. _ 6 e 5 | w 5 :] | \
@@ -43,7 +43,7 @@ def test_play
4343
| q __ 5 #4 5 5 | h __ 4 5 | q __ 5 5 4 0 | q __ 0 0 h 1 | h __ 2 e 5 6 q ^ 0 | e __ 3 4 q 5 1 3 | w ___ 5 |
4444
"
4545
assert_equal(a[:rows].length,4)
46-
46+
4747
end
4848

4949
def test_methods

test/run_wiki_tests.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,17 @@ def test_play
274274

275275
# Comments
276276

277-
a = zparse "| 2 3 |q <! This is a comment > 2 | e 4 2 |"
277+
a = zparse "| 2 3 |q /* This is a comment */ 2 | e 4 2 |"
278278
assert_equal(a.pcs,[2,3,2,4,2])
279-
a = zparse "|q 2 3 <! | q 1 2 > | e 4 2 |"
279+
a = zparse "|q 2 3 /* | q 1 2 */ | e 4 2 |"
280280
assert_equal(a.pcs,[2,3,4,2])
281+
a = zparse "|q 2 3 /* This is | q 1
282+
2 comment */ | e 4 2 |"
283+
assert_equal(a.pcs,[2,3,4,2])
284+
a = zparse "|q 2 3 // | q 1 2 | e 4 2 |
285+
5 6"
286+
assert_equal(a.pcs,[2,3,5,6])
287+
281288

282289
end
283290

ziffers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ def zplay_multi_line(input, opts={}, shared={},merge_opts={})
708708
def parse_rows(input)
709709
lines = input.split("\n").to_a.filter {|v| !v.strip.empty? }
710710
lines = lines.filter {|n| !n.start_with? "//" }
711-
parameters = lines.map {|l| l.split("/") }
711+
parameters = lines.map {|l| l.split("/ ") }
712712
rows = parameters.map{|p| p[0]}.filter {|v| !v.strip.empty? }.map {|l| l.split("|").filter {|v| !v.strip.empty? } }
713713
rows_length = rows.map(&:length).max
714714
rows = rows.map {|v| v.length<rows_length ? v+Array.new(rows_length-v.length){ nil } : v }

0 commit comments

Comments
 (0)