Skip to content

Commit

Permalink
Repeats work again, and now with endings!
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed May 1, 2013
1 parent 44064cd commit f46a8df
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions lib/ABC/Utils.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ package ABC::Utils {
my $context = ABC::Context.new($key, $meter, $length); my $context = ABC::Context.new($key, $meter, $length);
my @elements = $tune.music; my @elements = $tune.music;


my $repeat_position = 0; my $repeat-position = 0;
my $repeat_context = ABC::Context.new($context); my $repeat-context = ABC::Context.new($context);
my $in-repeat = False; my $repeat-count = 1;
my $in-nth-repeat = False;
my $i = 0; my $i = 0;
gather while ($i < @elements) { gather while ($i < @elements) {
given @elements[$i].key { given @elements[$i].key {
Expand All @@ -141,24 +142,38 @@ package ABC::Utils {
$stem, $stem,
$stem.is-tie); $stem.is-tie);
} }
when "nth_repeat" {
$in-nth-repeat = True;
if $repeat-count !(elem) @elements[$i].value {
# this is an ending for some other repeat, skip it
$i++ while ($i < @elements
&& !(@elements[$i].key eq "barline"
&& @elements[$i].value eq ":|" | ":|:"));
}
}
when "end_nth_repeat" {
$in-nth-repeat = False;
}
when "barline" { when "barline" {
given @elements[$i].value { given @elements[$i].value {
when ":|" | ":|:" { when ":|" | ":|:" {
if !$in-repeat { if $in-nth-repeat || $repeat-count == 1 {
$context = ABC::Context.new($repeat_context); $context = ABC::Context.new($repeat-context);
$i = $repeat_position; $i = $repeat-position;
$in-repeat = True; $repeat-count++;
} else { } else {
$in-repeat = False;
# treat :| as :|: because it is sometimes used as such by mistake # treat :| as :|: because it is sometimes used as such by mistake
$repeat_context = ABC::Context.new($context); $repeat-context = ABC::Context.new($context);
$repeat_position = $i; $repeat-position = $i;
$repeat-count = 1;
} }
$in-nth-repeat = False;
} }
when "|:" { when "|:" {
$repeat_context = ABC::Context.new($context); $repeat-context = ABC::Context.new($context);
$repeat_position = $i; $repeat-position = $i;
$in-repeat = False; $repeat-count = 1;
$in-nth-repeat = False;
} }
} }
$context.bar-line; $context.bar-line;
Expand Down

0 comments on commit f46a8df

Please sign in to comment.