Skip to content

Instantly share code, notes, and snippets.

@mstum
Created December 10, 2012 06:37
Show Gist options
  • Save mstum/4248838 to your computer and use it in GitHub Desktop.
Save mstum/4248838 to your computer and use it in GitHub Desktop.
Resolving Labels
private static Dictionary<string, int> ResolveLabels(IList<Token> tokens)
{
var result = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase);
var currentPosition = 0;
foreach (var token in tokens)
{
var commandToken = token as CommandToken;
if (commandToken != null)
{
try
{
var opcode = Opcodes.GetByName(commandToken.Command);
currentPosition += opcode.Size;
}
catch (IllegalOpcodeException ex)
{
throw new CompilerException(ex.Message);
}
}
else
{
var labelToken = token as LabelDeclarationToken;
if (labelToken != null)
{
if (result.ContainsKey(labelToken.Label))
{
throw new CompilerException("Label redefined: " + labelToken.Label);
}
result[labelToken.Label] = currentPosition;
}
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment