From 2b8c35c1d55d89dc01d5decceb2f7ff10ca5262c Mon Sep 17 00:00:00 2001 From: haven1433 Date: Sun, 22 Jan 2023 21:25:31 -0600 Subject: [PATCH] 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 --- .../Models/Code/LabelLibrary.cs | 2 ++ .../Models/Code/ScriptParser.cs | 6 +++++- src/HexManiac.Tests/CodeToolTests.cs | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/HexManiac.Core/Models/Code/LabelLibrary.cs b/src/HexManiac.Core/Models/Code/LabelLibrary.cs index 27ee712a..0d2edb59 100644 --- a/src/HexManiac.Core/Models/Code/LabelLibrary.cs +++ b/src/HexManiac.Core/Models/Code/LabelLibrary.cs @@ -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); } } } diff --git a/src/HexManiac.Core/Models/Code/ScriptParser.cs b/src/HexManiac.Core/Models/Code/ScriptParser.cs index 1af4df1a..1881c277 100644 --- a/src/HexManiac.Core/Models/Code/ScriptParser.cs +++ b/src/HexManiac.Core/Models/Code/ScriptParser.cs @@ -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(); } diff --git a/src/HexManiac.Tests/CodeToolTests.cs b/src/HexManiac.Tests/CodeToolTests.cs index 54c9ffd2..9235c0a7 100644 --- a/src/HexManiac.Tests/CodeToolTests.cs +++ b/src/HexManiac.Tests/CodeToolTests.cs @@ -597,6 +597,26 @@ You said no! Assert.Equal(14, Model.ReadPointer(8)); } + [Fact] + public void Script_EndsInLabel_AutoIncludeEndCommand() { + EventScript = "if1 = ;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 } }