Skip to content

Commit

Permalink
Provide labels, macros, and variables in assembly result
Browse files Browse the repository at this point in the history
  • Loading branch information
TollyH committed Jun 2, 2024
1 parent dd04b2e commit 4fbbafd
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ public readonly record struct AssemblyResult
ulong EntryPoint,
AAPFeatures UsedExtensions,
FilePosition[] AssembledLines,
int AssembledFiles
int AssembledFiles,
Dictionary<string, ulong> Labels,
HashSet<string> AllVariables,
HashSet<string> AllMacros
);

/// <summary>
Expand Down Expand Up @@ -177,6 +180,10 @@ public readonly struct AssemblyPosition(Stack<ImportStackFrame> importStack, Sta
// Files that begin with an %ASM_ONCE directive (i.e. won't throw a circular import error)
private readonly HashSet<string> completeAsmOnceFiles = new();

// Collections for every macro and variable defined throughout the program, including deleted ones
private readonly HashSet<string> allMacros = new();
private readonly HashSet<string> allVariables = new();

/// <param name="baseFilePath">
/// The path to the file being assembled.
/// This is used only for presentation to the user, it need not be accurate or even valid.
Expand Down Expand Up @@ -367,6 +374,7 @@ public void SetAssemblerVariable(string name, ulong value)
}

assemblerVariables[name] = value;
_ = allVariables.Add(name);
}

/// <summary>
Expand All @@ -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.
/// </remarks>
/// <exception cref="SyntaxError">Thrown if the given name is invalid.</exception>
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))
Expand All @@ -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))
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand Down

0 comments on commit 4fbbafd

Please sign in to comment.