public override double VisitIdentifierExpression(ExpressionGrammarParser.IdentifierExpressionContext context)
        {
            var    result  = context.GetText();
            string letters = "";
            int    i       = 0;

            while (Char.IsLetter(result[i]))
            {
                letters += result[i];
                i++;
            }
            int numbers = Convert.ToInt32(result.Substring(i));

            // get DataGridView
            var dgv = Program.form1.getDataGridView();

            // update variable dependencies - send selected cells to the ones that they use
            int currentRow = Program.form1.CurrentRow;                    // get row of cell for which we calculate the expression
            int currentCol = Program.form1.CurrentColumn;                 // get col of cell for which we calculate the expression
            //  MessageBox.Show("Current row for which we calculate the expression:" + dgv.Columns[currentCol].Name + dgv.Rows[currentRow].HeaderCell.Value);
            MyCell cell = (MyCell)dgv.Rows[currentRow].Cells[currentCol]; // get cell
            // get cell name in Excel terms
            string colName = dgv.Columns[cell.ColumnIndex].HeaderText;
            string rowName = cell.RowIndex.ToString();

            // MessageBox.Show("TRY to add variable dependency:" + (colName + rowName) + " to " + (letters + numbers.ToString()));
            //check to prevent adding redundant cells to hashsets (A-B-C type of connection mustn't create A-C link)
            if (((MyCell)(dgv.Rows[currentRow].Cells[currentCol])).Expression.Contains(letters + numbers))
            {
                //    MessageBox.Show("added variable dependency:" + (colName + rowName) + " to " + (letters + numbers.ToString()));
                // add it as a cell to visit while checking recursion
                ((MyCell)(dgv.Rows[numbers].Cells[letters])).Variables.Add(colName + rowName);
            }
            try
            {
                /*if (((letters + numbers.ToString()) != (colName + rowName))
                 *  && !(RecurChecker.Check((MyCell)dgv.Rows[numbers].Cells[letters], letters + numbers, new HashSet<string>()))
                 * )
                 */
                if (!(RecurChecker.Check((MyCell)dgv.Rows[numbers].Cells[letters], letters + numbers, new HashSet <string>())))
                {
                    //     MessageBox.Show("Recur checker OK");
                    //     MessageBox.Show("Expression: " + ((MyCell)dgv.Rows[numbers].Cells[letters]).Expression);
                    Program.form1.CurrentRow = numbers;
                    int tmp;
                    Program.form1.getColNameToColIndex().TryGetValue(letters, out tmp);
                    Program.form1.CurrentColumn = tmp;
                    return(Convert.ToDouble(Calculator.Evaluate(((MyCell)dgv.Rows[numbers].Cells[letters]).Expression)));
                }
                else
                {
                    throw new Exception("Cycles detected!");
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Cycles!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(0);
            }
        }
示例#2
0
        public static bool Check(MyCell currentCell, string currentVar, HashSet <string> vars)
        {
            try
            {
                /*
                 * MessageBox.Show("inside recur checker");
                 * MessageBox.Show("current var:" + currentVar);
                 * string variablesInVars = "";
                 * foreach (string var in vars)
                 * {
                 *  variablesInVars += var + " ";
                 * }
                 * MessageBox.Show(variablesInVars);
                 */

                HashSet <string> tmp = new HashSet <string>(vars);
                tmp.IntersectWith(currentCell.Variables);
                if (tmp.Count != 0)
                {
                    return(true);
                }
                vars.Add(currentVar);
                HashSet <string> cellsToGo = currentCell.Variables;
                bool             f         = false;
                foreach (string cellName in cellsToGo)
                {
                    string letters = "";
                    int    i       = 0;
                    while (Char.IsLetter(cellName[i]))
                    {
                        letters += cellName[i];
                        i++;
                    }
                    int numbers = Convert.ToInt32(cellName.Substring(i));
                    if (!f)
                    {
                        f = f || Check((MyCell)Program.form1.getDataGridView().Rows[numbers].Cells[letters], cellName, vars);
                    }
                }
                return(f);
            }
            catch (StackOverflowException e)
            {
                MessageBox.Show("Stack overflow! Recursion use I see indeed.");
                return(true);
            }
        }