<auto> improvements

if the last thing is a label, make sure to include an end command for it
if the last thing isn't an end command (or goto, etc) make sure to include an end command for it
This commit is contained in:
haven1433
2023-01-22 21:25:31 -06:00
parent 9622784b1c
commit 2b8c35c1d5
3 changed files with 27 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
using HavenSoft.HexManiac.Core.ViewModels.DataFormats;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace HavenSoft.HexManiac.Core.Models.Code {
public class LabelLibrary {
@@ -54,6 +55,7 @@ namespace HavenSoft.HexManiac.Core.Models.Code {
script.Add(endCommand);
}
unresolvedLabels.Clear();
if (labels.Values.Any(address => address == scriptStart + script.Count)) script.Add(endCommand);
}
}
}

View File

@@ -306,6 +306,8 @@ namespace HavenSoft.HexManiac.Core.Models.Code {
var labels = ExtractLocalLabels(model, start, lines);
bool lastCommandIsEndCommand = false;
for (var i = 0; i < lines.Length; i++) {
var line = lines[i].Trim();
if (line.EndsWith(":")) continue; // label, not code. Don't parse.
@@ -424,10 +426,13 @@ namespace HavenSoft.HexManiac.Core.Models.Code {
pointerOffset += arg.Length(model, currentSize + pointerOffset);
}
lastCommandIsEndCommand = command.IsEndingCommand;
break;
}
}
if (!lastCommandIsEndCommand) result.Add(endToken);
// any labels that were used but not included, stick them on the end of the script
labels.ResolveUnresolvedLabels(start, result, endToken);
@@ -436,7 +441,6 @@ namespace HavenSoft.HexManiac.Core.Models.Code {
deferred.WriteData(result, start);
}
if (result.Count == 0) result.Add(endToken); // end
return result.ToArray();
}

View File

@@ -597,6 +597,26 @@ You said no!
Assert.Equal(14, Model.ReadPointer(8));
}
[Fact]
public void Script_EndsInLabel_AutoIncludeEndCommand() {
EventScript = "if1 = <go1>;end;go1:";
Assert.Equal(7, Model.ReadPointer(2));
Assert.Equal(2, Model[7]);
}
[Fact]
public void Script_Unfinished_EndsWithEndCommand() {
EventScript = "nop";
Assert.Equal(2, Model[1]);
}
[Fact]
public void Script_EndsWithGoto_DoesNotIncludeClosingEnd() {
EventScript = "goto <100>";
Assert.Equal(0xFF, Model[5]);
}
// TODO test that we get an error (not an exception) if we do auto on an unformatted pointer
}
}