diff --git a/src/labels/process.rs b/src/labels/process.rs index 960b522..dc4bdd0 100644 --- a/src/labels/process.rs +++ b/src/labels/process.rs @@ -43,7 +43,8 @@ pub fn process_labels(board: &mut Board) { /// Resolve ".local" labels to "name.local" form. fn resolve_local_labels(stats: &mut [ParsedStat]) { for stat in stats.iter_mut() { - let mut section = CompactString::const_new(""); + let mut section_counter = 0; + let mut section = CompactString::const_new("$0"); for chunk in stat.iter_mut() { match chunk { Chunk::Label { @@ -57,12 +58,13 @@ fn resolve_local_labels(stats: &mut [ParsedStat]) { if label.name.is_empty() { // Expand :.local to :name.local assert!(label.local.is_some()); - label.name = section.clone() + label.name = section.clone(); } else if label.local.is_none() { // Interpret label :name as start of new section. // Only label definitions do this; label references have no effect. if !*is_ref { - section = label.name.clone(); + section_counter += 1; + section = format!("{}${}", &label.name, section_counter).into(); } } } diff --git a/src/labels/snapshots/zasm__labels__process__test__local_labels.snap b/src/labels/snapshots/zasm__labels__process__test__local_labels.snap index af0fea6..b852d96 100644 --- a/src/labels/snapshots/zasm__labels__process__test__local_labels.snap +++ b/src/labels/snapshots/zasm__labels__process__test__local_labels.snap @@ -18,3 +18,38 @@ expression: board_to_text(board) ' :stop #walk i +--- +'Using locals before any globals are defined +:_loop1 +#take gems 1 _break +#_loop1 +:_break +--- +'Multiple sections with the same name +#end +:touch +Trying to sell you some ammo... +#take gems 10 _skip +#give ammo 10 +:_skip +#zap touch +#end +:touch +Trying to sell you some torches... +#take gems 15 _skip0 +#give torches 5 +:_skip0 +#restore touch +--- +'Multiple locals with the same name +#end +:repeat3 +:_z +:_z +:_z +#take gems 1 _skip1 +#give score 1 +:_skip1 +#zap _z +#_z +:_z diff --git a/tests/labels/local.txt b/tests/labels/local.txt index 141a81f..2e7130c 100644 --- a/tests/labels/local.txt +++ b/tests/labels/local.txt @@ -14,3 +14,39 @@ ' :stop #walk i +--- +'Using locals before any globals are defined +:.loop +#take gems 1 .break +#.loop +:.break +--- +'Multiple sections with the same name +#end +:touch +Trying to sell you some ammo... +#take gems 10 .skip +#give ammo 10 +:.skip +#zap touch +#end +:touch +Trying to sell you some torches... +#take gems 15 .skip +#give torches 5 +:.skip +#restore touch +--- +'Multiple locals with the same name +#end +:repeat3 +:.z +:.z +:.z +#take gems 1 .skip +#give score 1 +:.skip +#zap .z +#.z +:.z +