public static int ParseParameter(Command.ParameterType t, PreParsedLine l, out ParsedType foundType, Dictionary <string, Alias> aliases, Dictionary <string, int> labels = null) { int x = 0; foundType = ParsedType.Constant; switch (t) { case Command.ParameterType.Label: if (labels != null && !labels.TryGetValue(l.Parameter.Value, out x)) { throw new ArgumentException("Unable to find label '" + l.Parameter + "' of line '" + l.InputLine + "'"); } foundType = ParsedType.Label; break; case Command.ParameterType.Constant: if (!int.TryParse(l.Parameter.Value, out x)) { throw new ArgumentException("Unable to parse parameter '" + l.Parameter + "' of line '" + l.InputLine + "'"); } break; case Command.ParameterType.StackDelta: x = ParseNonNegative(l); foundType = ParsedType.StackDelta; break; case Command.ParameterType.StackAddress: x = ParseNonNegative(l); foundType = ParsedType.Address; break; case Command.ParameterType.Address: foundType = ParsedType.Address; if (!int.TryParse(l.Parameter.Value, out x)) { Alias alias; if (aliases.TryGetValue(l.Parameter.Value, out alias)) { x = alias.Address; foundType = ParsedType.Alias; } else { throw new ArgumentException("Unable to parse address parameter '" + l.Parameter + "' of line '" + l.InputLine + "'. Parameter is no valid integer, or known alias"); } } if (x < 0 || x >= State.MemorySize) { throw new ArgumentException("Address parameter '" + l.Parameter + "' of line '" + l.InputLine + "' exceeds valid address range"); } break; } return(x); }
private static void Register(string name, Action <State, int> action, Command.ParameterType t) { commands.Add(name, new Command(name, action, t)); }