private void Parse()
{
var t = tokens[pos];
while (t.Type != TokenType.EndOfFile)
{
if (t.Type == TokenType.Label)
{
if (labels.ContainsKey(t.Value))
throw new AssemblerException(string.Format("Duplicate label '{0}' on line {1}.", t.Value, t.Line));
labels.Add(t.Value, new Label(t.Value, instructions.Count));
pos++;
}
else if (t.Type == TokenType.Keyword)
{
instructions.Add(ParseInstruction());
}
else if (t.Type == TokenType.Word && new List<string> { "db", "dw", "rb" }.Contains(t.Value.ToLower()))
{
string type = t.Value.ToLower();
var dataLine = t.Line;
t = tokens[++pos];
var data = new DataInstruction();
while (t.Type == TokenType.String || t.Type == TokenType.Number)
{
if (t.Type == TokenType.String)
{
data.Add(t.Value);
}
else
{
switch (type)
{
case "db":
{
byte value;
if (!byte.TryParse(t.Value, out value))
throw new AssemblerException(string.Format("Value out of range on line {0}.", t.Line));
data.Add(value);
break;
}
case "dw":
{
short value;
if (!short.TryParse(t.Value, out value))
throw new AssemblerException(string.Format("Value out of range on line {0}.", t.Line));
data.Add(value);
break;
}
case "rb":
{
short value;
if (!short.TryParse(t.Value, out value) || value < 0)
throw new AssemblerException(String.Format("Value out of range on line {0}.", t.Line));
while (value-- > 0)
data.Add(0);
break;
}
}
}
pos++;
if (tokens[pos].Type == TokenType.Comma)
{
t = tokens[++pos];
continue;
}
break;
}
if (!data.HasData)
throw new AssemblerException(string.Format("Empty data directive on line {0}.", dataLine));
instructions.Add(data);
}
else
{
throw new AssemblerException(string.Format("Unexpected {0} on line {1}.", t.Value, t.Line));
}
t = tokens[pos];
}
}