public void CalculateAllCells(ElectronicTableCell updated) { foreach (DataGridViewRow r in Rows) { foreach (ElectronicTableCell c in r.Cells) { c.IsReevaluated = false; c.CurrentlyCalculating = false; } } foreach (var d in updated.Dependecies) { d.Depended.Remove(updated); } updated.Dependecies.Clear(); if (string.IsNullOrWhiteSpace(updated.Expression)) { ClearDependecies(updated); return; } try { CalculateCellExpression(updated); } catch { throw; // fix cell loop handling } }
public override int VisitCell([NotNull] GrammarParser.CellContext context) { try { string cellRef = context.GetText(); int column = 0; while (65 <= (int)cellRef[column] && (int)cellRef[column] <= 90) { column++; } int colNum = ColumnSys26.Sys26ToNum(cellRef.Substring(0, column)) - 1; int rowNum = int.Parse(cellRef.Substring(column)) - 1; ElectronicTableCell cell = CalculatingCell.Table.Cell(rowNum, colNum); if (cell.CurrentlyCalculating) { var ex = new Exception(); ex.Data.Add("Type", "cell loop"); throw ex; } cell.Depended.Add(CalculatingCell); CalculatingCell.Dependecies.Add(cell); cell.CurrentlyCalculating = true; int ans = cell.Count(); cell.CurrentlyCalculating = false; Console.WriteLine($"Value of {cellRef} is {ans}"); return(ans); } catch { throw; } }
void ClearDependecies(ElectronicTableCell cur) { cur.Value = cur.Expression = ""; foreach (var d in cur.Depended) { ClearDependecies(d); } cur.Depended.Clear(); cur.Dependecies.Clear(); }
public ParsingVisitor(ElectronicTableCell calculating) : base() { CalculatingCell = calculating; Console.WriteLine($"CalculatingCell: {calculating}"); Console.WriteLine("Dependecies:"); foreach (var d in calculating.Dependecies) { Console.WriteLine(d); } Console.WriteLine("Depended:"); foreach (var d in calculating.Depended) { Console.WriteLine(d); } }
void CalculateCellExpression(ElectronicTableCell cell) { try { var inputStream = new AntlrInputStream(cell.Expression); var lexer = new GrammarLexer(inputStream); var commonTokenStream = new CommonTokenStream(lexer); var parser = new GrammarParser(commonTokenStream); parser.RemoveErrorListeners(); parser.AddErrorListener(new ParsingErrorListener()); var expr = parser.rule(); cell.Value = (new ParsingVisitor(cell)).Visit(expr); } catch { throw; } cell.IsReevaluated = true; foreach (var dep in cell.Depended) { CalculateCellExpression(dep); } }
void AddTable(ElectronicTable table) { if (table == null) { Table = new ElectronicTable(20, 10) { Location = new Point(40, MainPanel.Bottom + 20), Size = new Size(ClientSize.Width - 80, ClientSize.Height - MainPanel.Bottom - 80), }; } else { Table.SuspendLayout(); Controls.Remove(Table); table.Location = new Point(30, MainPanel.Bottom + 30); table.Size = new Size(ClientSize.Width - 60, ClientSize.Height - MainPanel.Bottom - 60); Table = table; } Table.CellEnter += (s, e) => { ElectronicTableCell selected = Table.Cell(e.RowIndex, e.ColumnIndex) as ElectronicTableCell; ExpressionTextBox.Text = Table.Cell(e.RowIndex, e.ColumnIndex).Expression; }; Table.CellBeginEdit += (s, e) => { try { Table.CurCell.Value = ExpressionTextBox.Text = Table.Cell(e.RowIndex, e.ColumnIndex).Expression; } catch (IndexOutOfRangeException) { } }; Table.CellEndEdit += (s, e) => { ElectronicTableCell changed = Table.Cell(e.RowIndex, e.ColumnIndex); string oldExpr = changed.Expression; changed.Expression = (string)changed.Value; try { Table.CalculateAllCells(changed); } catch (Exception ex) { if (ex.Data.Contains("Type")) { MessageBox.Show($"Wrong expression: {ex.Data["Type"]}", "Error"); } else { MessageBox.Show($"Wrong expression", "Error"); } changed.Expression = oldExpr; Table.CalculateAllCells(changed); } TableChanged = true; Text = "*Table"; }; Controls.Add(Table); Table.ResumeLayout(); }