From 4fbbafd8010cc59bb68324f6726f196be35925a5 Mon Sep 17 00:00:00 2001 From: TollyH Date: Sun, 2 Jun 2024 17:44:57 +0100 Subject: [PATCH] Provide labels, macros, and variables in assembly result --- Assembler.cs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/Assembler.cs b/Assembler.cs index 1a29a31..f70226f 100644 --- a/Assembler.cs +++ b/Assembler.cs @@ -21,7 +21,10 @@ public readonly record struct AssemblyResult ulong EntryPoint, AAPFeatures UsedExtensions, FilePosition[] AssembledLines, - int AssembledFiles + int AssembledFiles, + Dictionary Labels, + HashSet AllVariables, + HashSet AllMacros ); /// @@ -177,6 +180,10 @@ public readonly struct AssemblyPosition(Stack importStack, Sta // Files that begin with an %ASM_ONCE directive (i.e. won't throw a circular import error) private readonly HashSet completeAsmOnceFiles = new(); + // Collections for every macro and variable defined throughout the program, including deleted ones + private readonly HashSet allMacros = new(); + private readonly HashSet allVariables = new(); + /// /// The path to the file being assembled. /// This is used only for presentation to the user, it need not be accurate or even valid. @@ -367,6 +374,7 @@ public void SetAssemblerVariable(string name, ulong value) } assemblerVariables[name] = value; + _ = allVariables.Add(name); } /// @@ -376,11 +384,15 @@ public void SetAssemblerVariable(string name, ulong value) /// This method will perform validation on the name of the macro and remove a multi-line macro with the same name if it exists. /// /// Thrown if the given name is invalid. - public void SetSingleLineMacro(string name, string replacement) + public void SetSingleLineMacro(string name, string replacement, bool automatic = false) { ValidateMacroName(name); singleLineMacros[name] = replacement; + if (!automatic) + { + _ = allMacros.Add(name); + } singleLineMacroNames.Add(name); singleLineMacroNames = singleLineMacroNames.OrderByDescending(n => n.Length).Distinct().ToList(); if (multiLineMacros.Remove(name)) @@ -401,6 +413,7 @@ public void SetMultiLineMacro(string name, string[] replacement) ValidateMacroName(name); multiLineMacros[name] = replacement; + _ = allMacros.Add(name); multiLineMacroNames.Add(name); multiLineMacroNames = multiLineMacroNames.OrderByDescending(n => n.Length).Distinct().ToList(); if (singleLineMacros.Remove(name)) @@ -454,7 +467,8 @@ public AssemblyResult GetAssemblyResult(bool finalize) #if ASSEMBLER_WARNINGS warnings.ToArray(), #endif - entryPoint, usedExtensions, processedLines.ToArray(), timesSeenFile.Count); + entryPoint, usedExtensions, processedLines.ToArray(), timesSeenFile.Count, + labels, allVariables, allMacros); } /// @@ -2664,9 +2678,9 @@ private bool RunConditionalCheck(string mnemonic, string[] operands) private void SetFileMacros() { - SetSingleLineMacro("#FILE_PATH", currentFilePosition.File.EscapeCharacters()); - SetSingleLineMacro("#FILE_NAME", Path.GetFileName(currentFilePosition.File).EscapeCharacters()); - SetSingleLineMacro("#FOLDER_PATH", (Path.GetDirectoryName(currentFilePosition.File) ?? "").EscapeCharacters()); + SetSingleLineMacro("#FILE_PATH", currentFilePosition.File.EscapeCharacters(), true); + SetSingleLineMacro("#FILE_NAME", Path.GetFileName(currentFilePosition.File).EscapeCharacters(), true); + SetSingleLineMacro("#FOLDER_PATH", (Path.GetDirectoryName(currentFilePosition.File) ?? "").EscapeCharacters(), true); } ///