public RegisterRef?ResolveRegisterRef(string text, bool canBeEmpty) { if (string.IsNullOrWhiteSpace(text)) { if (canBeEmpty) { return(null); } throw new Exception("Empty value"); } string regex = @"^\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\[\s*([1-9][0-9]*|[0-9])\s*\]\s*$"; Match match = Regex.Match(text, regex); if (!match.Success) { throw new Exception("Invalid format. Please use the form: <registerName>[<index>]"); } string regName = match.Groups[1].Value; string offsetString = match.Groups[2].Value; Register reg = _comp.FindRegister(regName); if (reg == null) { throw new Exception("No such register: " + regName); } int offset; if (!int.TryParse(offsetString, out offset)) { throw new Exception("The index is not integer. Please use the form: <registerName>[<index>]"); } if (offset < 0) { StringBuilder sb = new StringBuilder("Cannot reference to "); sb.Append(text); throw new Exception(sb.ToString()); } if (offset >= reg.Width) { StringBuilder sb = new StringBuilder("Cannot reference to "); sb.Append(text); sb.Append(" - the register has only ").Append(reg.Width) .Append(" qubits, numbered from 0 to ").Append(reg.Width - 1); throw new Exception(sb.ToString()); } RegisterRef toReturn = new RegisterRef() { Register = reg, Offset = offset }; return(toReturn); }
public RegisterRef? ResolveRegisterRef(string text, bool canBeEmpty) { if (string.IsNullOrWhiteSpace(text)) { if (canBeEmpty) { return null; } throw new Exception("Empty value"); } string regex = @"^\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\[\s*([1-9][0-9]*|[0-9])\s*\]\s*$"; Match match = Regex.Match(text, regex); if (!match.Success) { throw new Exception("Invalid format. Please use the form: <registerName>[<index>]"); } string regName = match.Groups[1].Value; string offsetString = match.Groups[2].Value; Register reg = _comp.FindRegister(regName); if (reg == null) { throw new Exception("No such register: " + regName); } int offset; if (!int.TryParse(offsetString, out offset)) { throw new Exception("The index is not integer. Please use the form: <registerName>[<index>]"); } if (offset < 0) { StringBuilder sb = new StringBuilder("Cannot reference to "); sb.Append(text); throw new Exception(sb.ToString()); } if (offset >= reg.Width) { StringBuilder sb = new StringBuilder("Cannot reference to "); sb.Append(text); sb.Append(" - the register has only ").Append(reg.Width) .Append(" qubits, numbered from 0 to ").Append(reg.Width - 1); throw new Exception(sb.ToString()); } RegisterRef toReturn = new RegisterRef() { Register = reg, Offset = offset }; return toReturn; }