/// <summary> /// Finds the nearest matching label with the same name. /// </summary> /// <param name="name">The label to search for.</param> /// <param name="line">Search for matches near this line number.</param> /// <param name="log">A log to write warnings/errors to.</param> /// <returns>Returns True if found.</returns> public bool GetNearestLabel(string name, int line, out Label closestMatch) { List<Label> ids; closestMatch = null; bool found = labelDic.TryGetValue(name, out ids); if (found) { // if only one match lets shortcut and take only that one if (ids.Count == 1) closestMatch = ids[0]; else { int closestDistance = int.MaxValue; foreach (Label l in ids) { int thisDistance = Math.Abs(line - l.lineNum); if (thisDistance <= closestDistance) { closestDistance = thisDistance; closestMatch = l; } } } } return found; }
/// <summary> /// Adds a label to the label dictionary. /// </summary> /// <param name="newLabel">The new label to add to the Labels container.</param> /// <param name="log">Location to write errors and warnings.</param> public void AddLabel(Label newLabel, Log log) { List<Label> ids; if (labelDic.TryGetValue(newLabel.labelName, out ids)) { log.Warning("The label, {0}, already exists. References will use the nearest label based on line number.", newLabel.labelName); ids.Add(newLabel); } else { ids = new List<Label>(); ids.Add(newLabel); labelDic.Add(newLabel.labelName, ids); } }
/// <summary> /// Adds a label to the label dictionary. /// </summary> /// <param name="name">The name of the label. (i.e "myLabel" in myLabel:)</param> /// <param name="firstStmt">The next statement following this label.</param> /// <param name="log">Location to write errors and warnings.</param> public void AddLabel(string name, int lineNum, Stmt firstStmt, Log log) { Label newLabel = new Label { labelName = name, lineNum = lineNum, firstStmt = firstStmt }; AddLabel(newLabel, log); }