Created
December 10, 2012 06:37
-
-
Save mstum/4248838 to your computer and use it in GitHub Desktop.
Resolving Labels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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