<commit_before>/**
 * This file is part of the NovaeCalc project.
 *
 * It is permitted to use, redistribute and/or modify this software
 * under the terms of the BSD License
 *
 * @author <NAME> <<EMAIL>>
 * @copyright (c) 2015 <NAME>, @felixmaier
 *
 * You may not change or remove these lines
 *
 */

"use strict";

  /**
   * Run the interpreter
   *
   * @method eval
   * @static
   */
  NOVAE.eval = function() {

    var cells = null;
    var usedCellSheet = null;

    if (arguments[0]) {
      cells = NOVAE.Cells.Used[arguments[0]];
      usedCellSheet = NOVAE.Cells.Used[arguments[0]];
    } else {
      cells = NOVAE.Cells.Used[NOVAE.CurrentSheet];
      usedCellSheet = NOVAE.Cells.Used[NOVAE.CurrentSheet];
    }

    var formulas = [];

    var result = 0;

    for (var ii in cells) {
      for (var kk in cells[ii]) {
        /** Cell has a formula */
        if (cells[ii][kk].Formula && cells[ii][kk].Formula.length) {
          /** The formula behind the variable assignment */
          formulas.push({
            name: kk,
            /** Validate formula, add the parent cell value before the formula to emulate a variable assignment */
            value: cells[ii][kk].Formula.substr(0, 0) + kk + cells[ii][kk].Formula.substr(0)
          });
        }
      }
    }

    if (formulas && formulas.length) {
      /** Process all found formulas */
      for (var ii = 0; ii < formulas.length; ++ii) {

        /** Receive the result */
        var result = this.evaluateFormula(formulas[ii].value);

        var name = formulas[ii].name;
        var letter = name.match(NOVAE.REGEX.numbers).join("");
        var number = ~~(name.match(NOVAE.REGEX.letters).join(""));

        /** Update used cell stack content */
        usedCellSheet[letter][formulas[ii].name].Content = result;

        /** Display the result, if cell is in view */
        var jumps = NOVAE.$.getCell({ letter: NOVAE.$.alphaToNumber(letter), number: number });
        if (jumps >= 0) NOVAE.DOM.Output.children[jumps].innerHTML = result;

      }
    }

  };

  /**
   * Recursive evaluate a formula
   * Consider natural order recalculation, means recalculate each dependent cell
   * before calculate the real result
   *
   * @method evaluateFormula
   * @static
   */
  NOVAE.evaluateFormula = function(formula) { 

    var interpret = ENGEL.interpret(formula);

    var name = interpret.Variables.shift();

    /** Natural order recalculation */
    for (var ii = 0; ii < interpret.Variables.length; ++ii) {
      var letter = interpret.Variables[ii].match(NOVAE.REGEX.numbers).join("");
      if (NOVAE.Cells.Used[NOVAE.CurrentSheet][letter] &&
          NOVAE.Cells.Used[NOVAE.CurrentSheet][letter][interpret.Variables[ii]] &&
          NOVAE.Cells.Used[NOVAE.CurrentSheet][letter][interpret.Variables[ii]].Formula) {
        NOVAE.evaluateFormula(interpret.Variables[ii] + NOVAE.Cells.Used[NOVAE.CurrentSheet][letter][interpret.Variables[ii]].Formula);<commit_msg>Detect circular references in formulas<commit_after>/**
 * This file is part of the NovaeCalc project.
 *
 * It is permitted to use, redistribute and/or modify this software
 * under the terms of the BSD License
 *
 * @author <NAME> <<EMAIL>>
 * @copyright (c) 2015 <NAME>, @felixmaier
 *
 * You may not change or remove these lines
 *
 */

"use strict";

  /**
   * Run the interpreter
   *
   * @method eval
   * @static
   */
  NOVAE.eval = function() {

    var cells = null;
    var usedCellSheet = null;

    if (arguments[0]) {
      cells = NOVAE.Cells.Used[arguments[0]];
      usedCellSheet = NOVAE.Cells.Used[arguments[0]];
    } else {
      cells = NOVAE.Cells.Used[NOVAE.CurrentSheet];
      usedCellSheet = NOVAE.Cells.Used[NOVAE.CurrentSheet];
    }

    var formulas = [];

    var result = 0;

    for (var ii in cells) {
      for (var kk in cells[ii]) {
        /** Cell has a formula */
        if (cells[ii][kk].Formula && cells[ii][kk].Formula.length) {
          /** The formula behind the variable assignment */
          formulas.push({
            name: kk,
            /** Validate formula, add the parent cell value before the formula to emulate a variable assignment */
            value: cells[ii][kk].Formula.substr(0, 0) + kk + cells[ii][kk].Formula.substr(0)
          });
        }
      }
    }

    if (formulas && formulas.length) {
      /** Process all found formulas */
      for (var ii = 0; ii < formulas.length; ++ii) {

        /** Receive the result */
        var result = this.evaluateFormula(formulas[ii].value);

        var name = formulas[ii].name;
        var letter = name.match(NOVAE.REGEX.numbers).join("");
        var number = ~~(name.match(NOVAE.REGEX.letters).join(""));

        /** Update used cell stack content */
        usedCellSheet[letter][formulas[ii].name].Content = result;

        /** Display the result, if cell is in view */
        var jumps = NOVAE.$.getCell({ letter: NOVAE.$.alphaToNumber(letter), number: number });
        if (jumps >= 0) NOVAE.DOM.Output.children[jumps].innerHTML = result;

      }
    }

  };

  /**
   * Recursive evaluate a formula
   * Consider natural order recalculation, means recalculate each dependent cell
   * before calculate the real result
   *
   * @method evaluateFormula
   * @static
   */
  NOVAE.evaluateFormula = function(formula) {

    var index = arguments[1] || undefined;

    var interpret = ENGEL.interpret(formula);

    var name = interpret.Variables.shift();

    if (index && index.length) {
      if (index[0] === name) {

        var muiButton = "mui-btn mui-btn-primary mui-btn-lg alertButton";
        var title = "<h2>Circular Reference detected!</h2><h3>It seems like you have a circular reference in " + name + ".</h3>";
        var buttons = "<button class='"+muiButton+" alertOk' name='ok'>Ok</button>";
        NOVAE_UI.Modal(title, buttons, function(submit) {});
        return ("CircularReference");
      }
    } else {
      index = [name];
    }

    /** Natural order recalculation */
    for (var ii = 0; ii < interpret.Variables.length; ++ii) {
      var letter = interpret.Variables[ii].match(NOVAE.REGEX.numbers).join("");
      if (NOVAE.Cells.Used[NOVAE.CurrentSheet][letter] &&
          NOVAE.Cells.Used[NOVAE.CurrentSheet][letter][interpret.Variables[ii]] &&
          NOVAE.Cells.Used[NOVAE.CurrentSheet][letter][interpret.Variables[ii]].Formula) {
          var result = NOVAE.evaluateFormula(interpret.Variables[ii] + NOVAE.Cells.Used[NOVAE.CurrentSheet][letter][interpret.Variables[ii]].Formula, index || interpret.Variables);
          /** Break calculation */
          if (result === "CircularReference") return ("CircularReference");
{'fraction_non_alphanumeric': 0.14144292785360732, 'fraction_numerical': 0.0050997450127493625, 'mean_word_length': 2.655701754385965, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7205609', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2012, 'n_words': 507}