/* Copyright (C) 2014 Michigan State University Board of Trustees The JavaScript code in this page is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License (GNU GPL) as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. The code is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU GPL for more details. As additional permission under GNU GPL version 3 section 7, you may distribute non-source (e.g., minimized or compacted) forms of that code without the copy of the GNU GPL normally required by section 4, provided you include this license notice and a URL through which recipients can access the Corresponding Source. */ "use strict"; /** * Equation parser * @constructor * @param {boolean} [implicit_operators] - assume hidden multiplication and unit operators in some cases (unlike maxima) * @param {boolean} [unit_mode] - handle only numerical expressions with units (no variable) * @param {Array.} [constants] - array of constant names for unit mode */ function Parser(implicit_operators, unit_mode, constants) { if (typeof implicit_operators == "undefined") this.implicit_operators = false; else this.implicit_operators = implicit_operators; if (typeof unit_mode == "undefined") this.unit_mode = false; else this.unit_mode = unit_mode; if (typeof constants == "undefined" || constants == null) this.constants = []; else this.constants = constants; this.defs = new Definitions(); this.defs.define(); this.operators = this.defs.operators; this.oph = {}; // operator hash table for (var i=0; i= this.tokens.length) { this.current_token = null; return; } this.current_token = this.tokens[this.token_nr]; this.token_nr += 1; }; /** * Adds hidden multiplication and unit operators to the token stream */ Parser.prototype.addHiddenOperators = function() { var multiplication = this.defs.findOperator("*"); var unit_operator = this.defs.findOperator("`"); var in_units = false; // we check if we are already in the units to avoid adding two ` operators inside var in_exp = false; for (var i=0; i i + 2 && this.tokens[i + 2].type == Token.NAME))); if (units) { var test_token, index_test; if (next_token.type == Token.NAME) { test_token = next_token; index_test = i + 1; } else { // for instance for "2 (m/s)" index_test = i + 2; test_token = this.tokens[index_test]; } for (var j=0; j index_test + 1 && this.tokens[index_test + 1].value == "(") { var known_functions = ["pow", "sqrt", "abs", "exp", "factorial", "diff", "integrate", "sum", "product", "limit", "binomial", "matrix", "ln", "log", "log10", "mod", "signum", "ceiling", "floor", "sin", "cos", "tan", "asin", "acos", "atan", "atan2", "sinh", "cosh", "tanh", "asinh", "acosh", "atanh"]; for (var j=0; j