math-expression-evaluator.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /** math-expression-evaluator version 1.2.17
  2. Dated:2017-05-05 */
  3. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mexp = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  4. var Mexp=require('./postfix_evaluator.js');
  5. Mexp.prototype.formulaEval = function () {
  6. "use strict";
  7. var stack=[],pop1,pop2,pop3;
  8. var disp=[];
  9. var temp='';
  10. var arr=this.value;
  11. for(var i=0;i<arr.length;i++){
  12. if(arr[i].type===1||arr[i].type===3){
  13. disp.push({value:arr[i].type===3?arr[i].show:arr[i].value,type:1});
  14. }
  15. else if(arr[i].type===13){
  16. disp.push({value:arr[i].show,type:1});
  17. }
  18. else if(arr[i].type===0){
  19. disp[disp.length-1]={value:arr[i].show+(arr[i].show!="-"?"(":"")+disp[disp.length-1].value+(arr[i].show!="-"?")":""),type:0};
  20. }
  21. else if(arr[i].type===7){
  22. disp[disp.length-1]={value:(disp[disp.length-1].type!=1?"(":"")+disp[disp.length-1].value+(disp[disp.length-1].type!=1?")":"")+arr[i].show,type:7};
  23. }
  24. else if(arr[i].type===10){
  25. pop1=disp.pop();
  26. pop2=disp.pop();
  27. if(arr[i].show==='P'||arr[i].show==='C')disp.push({value:"<sup>"+pop2.value+"</sup>"+arr[i].show+"<sub>"+pop1.value+"</sub>",type:10});
  28. else disp.push({value:(pop2.type!=1?"(":"")+pop2.value+(pop2.type!=1?")":"")+"<sup>"+pop1.value+"</sup>",type:1});
  29. }
  30. else if(arr[i].type===2||arr[i].type===9){
  31. pop1=disp.pop();
  32. pop2=disp.pop();
  33. disp.push({value:(pop2.type!=1?"(":"")+pop2.value+(pop2.type!=1?")":"")+arr[i].show+(pop1.type!=1?"(":"")+pop1.value+(pop1.type!=1?")":""),type:arr[i].type});
  34. }
  35. else if(arr[i].type===12){
  36. pop1=disp.pop();
  37. pop2=disp.pop();
  38. pop3=disp.pop();
  39. disp.push({value:arr[i].show+"("+pop3.value+","+pop2.value+","+pop1.value+")",type:12});
  40. }
  41. }
  42. return disp[0].value;
  43. };
  44. module.exports=Mexp;
  45. },{"./postfix_evaluator.js":5}],2:[function(require,module,exports){
  46. var Mexp = require('./math_function.js')
  47. function inc (arr, val) {
  48. for (var i = 0; i < arr.length; i++) {
  49. arr[i] += val
  50. }
  51. return arr
  52. }
  53. var token = ['sin', 'cos', 'tan', 'pi', '(', ')', 'P', 'C',
  54. 'asin', 'acos', 'atan', '7', '8', '9', 'int',
  55. 'cosh', 'acosh', 'ln', '^', 'root', '4', '5', '6', '/', '!',
  56. 'tanh', 'atanh', 'Mod', '1', '2', '3', '*',
  57. 'sinh', 'asinh', 'e', 'log', '0', '.', '+', '-', ',', 'Sigma', 'n', 'Pi', 'pow']
  58. var show = ['sin', 'cos', 'tan', '&pi;', '(', ')', 'P', 'C',
  59. 'asin', 'acos', 'atan', '7', '8', '9', 'Int',
  60. 'cosh', 'acosh', ' ln', '^', 'root', '4', '5', '6', '&divide;', '!',
  61. 'tanh', 'atanh', ' Mod ', '1', '2', '3', '&times;',
  62. 'sinh', 'asinh', 'e', ' log', '0', '.', '+', '-', ',', '&Sigma;', 'n', '&Pi;', 'pow']
  63. var eva = [Mexp.math.sin, Mexp.math.cos, Mexp.math.tan, 'PI', '(', ')', Mexp.math.P, Mexp.math.C,
  64. Mexp.math.asin, Mexp.math.acos, Mexp.math.atan, '7', '8', '9', Math.floor,
  65. Mexp.math.cosh, Mexp.math.acosh, Math.log, Math.pow, Math.sqrt, '4', '5', '6', Mexp.math.div, Mexp.math.fact,
  66. Mexp.math.tanh, Mexp.math.atanh, Mexp.math.mod, '1', '2', '3', Mexp.math.mul,
  67. Mexp.math.sinh, Mexp.math.asinh, 'E', Mexp.math.log, '0', '.', Mexp.math.add, Mexp.math.sub, ',', Mexp.math.sigma, 'n', Mexp.math.Pi, Math.pow]
  68. var preced = {
  69. 0: 11,
  70. 1: 0,
  71. 2: 3,
  72. 3: 0,
  73. 4: 0,
  74. 5: 0,
  75. 6: 0,
  76. 7: 11,
  77. 8: 11,
  78. 9: 1,
  79. 10: 10,
  80. 11: 0,
  81. 12: 11,
  82. 13: 0
  83. }
  84. var type = [0, 0, 0, 3, 4, 5, 10, 10,
  85. 0, 0, 0, 1, 1, 1, 0,
  86. 0, 0, 0, 10, 0, 1, 1, 1, 2, 7,
  87. 0, 0, 2, 1, 1, 1, 2,
  88. 0, 0, 3, 0, 1, 6, 9, 9, 11, 12, 13, 12, 8]
  89. /*
  90. 0 : function with syntax function_name(Maths_exp)
  91. 1 : numbers
  92. 2 : binary operators like * / Mod left associate and same precedence
  93. 3 : Math constant values like e,pi,Cruncher ans
  94. 4 : opening bracket
  95. 5 : closing bracket
  96. 6 : decimal
  97. 7 : function with syntax (Math_exp)function_name
  98. 8: function with syntax function_name(Math_exp1,Math_exp2)
  99. 9 : binary operator like +,-
  100. 10: binary operator like P C or ^
  101. 11: ,
  102. 12: function with , seperated three parameters
  103. 13: variable of Sigma function
  104. */
  105. var type0 = {
  106. 0: true,
  107. 1: true,
  108. 3: true,
  109. 4: true,
  110. 6: true,
  111. 8: true,
  112. 9: true,
  113. 12: true,
  114. 13: true
  115. } // type2:true,type4:true,type9:true,type11:true,type21:true,type22
  116. var type1 = {
  117. 0: true,
  118. 1: true,
  119. 2: true,
  120. 3: true,
  121. 4: true,
  122. 5: true,
  123. 6: true,
  124. 7: true,
  125. 8: true,
  126. 9: true,
  127. 10: true,
  128. 11: true,
  129. 12: true,
  130. 13: true
  131. } // type3:true,type5:true,type7:true,type23
  132. var type1Asterick = {
  133. 0: true,
  134. 3: true,
  135. 4: true,
  136. 8: true,
  137. 12: true,
  138. 13: true
  139. }
  140. var empty = {}
  141. var type3Asterick = {
  142. 0: true,
  143. 1: true,
  144. 3: true,
  145. 4: true,
  146. 6: true,
  147. 8: true,
  148. 12: true,
  149. 13: true
  150. } // type_5:true,type_7:true,type_23
  151. var type6 = {
  152. 1: true
  153. }
  154. var newAr = [
  155. [],
  156. ['1', '2', '3', '7', '8', '9', '4', '5', '6', '+', '-', '*', '/', '(', ')', '^', '!', 'P', 'C', 'e', '0', '.', ',', 'n'],
  157. ['pi', 'ln', 'Pi'],
  158. ['sin', 'cos', 'tan', 'Del', 'int', 'Mod', 'log', 'pow'],
  159. ['asin', 'acos', 'atan', 'cosh', 'root', 'tanh', 'sinh'],
  160. ['acosh', 'atanh', 'asinh', 'Sigma']
  161. ]
  162. function match (str1, str2, i, x) {
  163. for (var f = 0; f < x; f++) {
  164. if (str1[i + f] !== str2[f]) {
  165. return false
  166. }
  167. }
  168. return true
  169. }
  170. Mexp.addToken = function (tokens) {
  171. for (var i = 0; i < tokens.length; i++) {
  172. var x = tokens[i].token.length
  173. var temp = -1
  174. // newAr is a specially designed data structure in which 1D array at location one of 2d array has all string with length 1 2 with 2 and so on
  175. if (x < newAr.length) { // match to check if token is really huge and not existing
  176. // if not checked it will break in next line as undefined index
  177. for (var y = 0; y < newAr[x].length; y++) {
  178. if (tokens[i].token === newAr[x][y]) {
  179. temp = token.indexOf(newAr[x][y])
  180. break
  181. }
  182. }
  183. }
  184. if (temp === -1) {
  185. token.push(tokens[i].token)
  186. type.push(tokens[i].type)
  187. if (newAr.length <= tokens[i].token.length) {
  188. newAr[tokens[i].token.length] = []
  189. }
  190. newAr[tokens[i].token.length].push(tokens[i].token)
  191. eva.push(tokens[i].value)
  192. show.push(tokens[i].show)
  193. } else {
  194. token[temp] = tokens[i].token
  195. type[temp] = tokens[i].type
  196. eva[temp] = tokens[i].value
  197. show[temp] = tokens[i].show
  198. }
  199. }
  200. }
  201. Mexp.lex = function (inp, tokens) {
  202. 'use strict'
  203. var changeSignObj = {
  204. value: Mexp.math.changeSign,
  205. type: 0,
  206. pre: 21,
  207. show: '-'
  208. }
  209. var closingParObj = {
  210. value: ')',
  211. show: ')',
  212. type: 5,
  213. pre: 0
  214. }
  215. var openingParObj = {
  216. value: '(',
  217. type: 4,
  218. pre: 0,
  219. show: '('
  220. }
  221. var str = [openingParObj]
  222. var ptc = [] // Parenthesis to close at the beginning is after one token
  223. var inpStr = inp
  224. var key
  225. var pcounter = 0
  226. var allowed = type0
  227. var bracToClose = 0
  228. var asterick = empty
  229. var prevKey = ''
  230. var i, x, y
  231. if (typeof tokens !== 'undefined') {
  232. Mexp.addToken(tokens)
  233. }
  234. var obj = {}
  235. for (i = 0; i < inpStr.length; i++) {
  236. if (inpStr[i] === ' ') {
  237. continue
  238. }
  239. key = ''
  240. for (x = (inpStr.length - i > (newAr.length - 2) ? newAr.length - 1 : inpStr.length - i); x > 0; x--) {
  241. for (y = 0; y < newAr[x].length; y++) {
  242. if (match(inpStr, newAr[x][y], i, x)) {
  243. key = newAr[x][y]
  244. y = newAr[x].length
  245. x = 0
  246. }
  247. }
  248. }
  249. i += key.length - 1
  250. if (key === '') {
  251. throw (new Mexp.Exception('Can\'t understand after ' + inpStr.slice(i)))
  252. }
  253. var index = token.indexOf(key)
  254. var cToken = key
  255. var cType = type[index]
  256. var cEv = eva[index]
  257. var cPre = preced[cType]
  258. var cShow = show[index]
  259. var pre = str[str.length - 1]
  260. var j
  261. for (j = ptc.length; j--;) { // loop over ptc
  262. if (ptc[j] === 0) {
  263. if ([0, 2, 3, 5, 9, 11, 12, 13].indexOf(cType) !== -1) {
  264. if (allowed[cType] !== true) {
  265. throw (new Mexp.Exception(key + ' is not allowed after ' + prevKey))
  266. }
  267. str.push(closingParObj)
  268. allowed = type1
  269. asterick = type3Asterick
  270. inc(ptc, -1).pop()
  271. }
  272. } else break
  273. }
  274. if (allowed[cType] !== true) {
  275. throw (new Mexp.Exception(key + ' is not allowed after ' + prevKey))
  276. }
  277. if (asterick[cType] === true) {
  278. cType = 2
  279. cEv = Mexp.math.mul
  280. cShow = '&times;'
  281. cPre = 3
  282. i = i - key.length
  283. }
  284. obj = {
  285. value: cEv,
  286. type: cType,
  287. pre: cPre,
  288. show: cShow
  289. }
  290. if (cType === 0) {
  291. allowed = type0
  292. asterick = empty
  293. inc(ptc, 2).push(2)
  294. str.push(obj)
  295. str.push(openingParObj)
  296. } else if (cType === 1) {
  297. if (pre.type === 1) {
  298. pre.value += cEv
  299. inc(ptc, 1)
  300. } else {
  301. str.push(obj)
  302. }
  303. allowed = type1
  304. asterick = type1Asterick
  305. } else if (cType === 2) {
  306. allowed = type0
  307. asterick = empty
  308. inc(ptc, 2)
  309. str.push(obj)
  310. } else if (cType === 3) { // constant
  311. str.push(obj)
  312. allowed = type1
  313. asterick = type3Asterick
  314. } else if (cType === 4) {
  315. pcounter += ptc.length
  316. ptc = []
  317. bracToClose++
  318. allowed = type0
  319. asterick = empty
  320. str.push(obj)
  321. } else if (cType === 5) {
  322. if (!bracToClose) {
  323. throw (new Mexp.Exception('Closing parenthesis are more than opening one, wait What!!!'))
  324. }
  325. while (pcounter--) { // loop over ptc
  326. str.push(closingParObj)
  327. }
  328. pcounter = 0
  329. bracToClose--
  330. allowed = type1
  331. asterick = type3Asterick
  332. str.push(obj)
  333. } else if (cType === 6) {
  334. if (pre.hasDec) {
  335. throw (new Mexp.Exception('Two decimals are not allowed in one number'))
  336. }
  337. if (pre.type !== 1) {
  338. pre = {
  339. value: 0,
  340. type: 1,
  341. pre: 0
  342. } // pre needs to be changed as it will the last value now to be safe in later code
  343. str.push(pre)
  344. inc(ptc, -1)
  345. }
  346. allowed = type6
  347. inc(ptc, 1)
  348. asterick = empty
  349. pre.value += cEv
  350. pre.hasDec = true
  351. } else if (cType === 7) {
  352. allowed = type1
  353. asterick = type3Asterick
  354. inc(ptc, 1)
  355. str.push(obj)
  356. }
  357. if (cType === 8) {
  358. allowed = type0
  359. asterick = empty
  360. inc(ptc, 4).push(4)
  361. str.push(obj)
  362. str.push(openingParObj)
  363. } else if (cType === 9) {
  364. if (pre.type === 9) {
  365. if (pre.value === Mexp.math.add) {
  366. pre.value = cEv
  367. pre.show = cShow
  368. inc(ptc, 1)
  369. } else if (pre.value === Mexp.math.sub && cShow === '-') {
  370. pre.value = Mexp.math.add
  371. pre.show = '+'
  372. inc(ptc, 1)
  373. }
  374. } else if (pre.type !== 5 && pre.type !== 7 && pre.type !== 1 && pre.type !== 3 && pre.type !== 13) { // changesign only when negative is found
  375. if (cToken === '-') { // do nothing for + token
  376. // don't add with the above if statement as that will run the else statement of parent if on Ctoken +
  377. allowed = type0
  378. asterick = empty
  379. inc(ptc, 2).push(2)
  380. str.push(changeSignObj)
  381. str.push(openingParObj)
  382. }
  383. } else {
  384. str.push(obj)
  385. inc(ptc, 2)
  386. }
  387. allowed = type0
  388. asterick = empty
  389. } else if (cType === 10) {
  390. allowed = type0
  391. asterick = empty
  392. inc(ptc, 2)
  393. str.push(obj)
  394. } else if (cType === 11) {
  395. allowed = type0
  396. asterick = empty
  397. str.push(obj)
  398. } else if (cType === 12) {
  399. allowed = type0
  400. asterick = empty
  401. inc(ptc, 6).push(6)
  402. str.push(obj)
  403. str.push(openingParObj)
  404. } else if (cType === 13) {
  405. allowed = type1
  406. asterick = type3Asterick
  407. str.push(obj)
  408. }
  409. inc(ptc, -1)
  410. prevKey = key
  411. }
  412. for (j = ptc.length; j--;) { // loop over ptc
  413. if (ptc[j] === 0) {
  414. str.push(closingParObj)
  415. inc(ptc, -1).pop()
  416. } else break // if it is not zero so before ptc also cant be zero
  417. }
  418. if (allowed[5] !== true) {
  419. throw (new Mexp.Exception('complete the expression'))
  420. }
  421. while (bracToClose--) {
  422. str.push(closingParObj)
  423. }
  424. str.push(closingParObj)
  425. // console.log(str);
  426. return new Mexp(str)
  427. }
  428. module.exports = Mexp
  429. },{"./math_function.js":3}],3:[function(require,module,exports){
  430. var Mexp = function (parsed) {
  431. this.value = parsed
  432. }
  433. Mexp.math = {
  434. isDegree: true, // mode of calculator
  435. acos: function (x) {
  436. return (Mexp.math.isDegree ? 180 / Math.PI * Math.acos(x) : Math.acos(x))
  437. },
  438. add: function (a, b) {
  439. return a + b
  440. },
  441. asin: function (x) {
  442. return (Mexp.math.isDegree ? 180 / Math.PI * Math.asin(x) : Math.asin(x))
  443. },
  444. atan: function (x) {
  445. return (Mexp.math.isDegree ? 180 / Math.PI * Math.atan(x) : Math.atan(x))
  446. },
  447. acosh: function (x) {
  448. return Math.log(x + Math.sqrt(x * x - 1))
  449. },
  450. asinh: function (x) {
  451. return Math.log(x + Math.sqrt(x * x + 1))
  452. },
  453. atanh: function (x) {
  454. return Math.log((1 + x) / (1 - x))
  455. },
  456. C: function (n, r) {
  457. var pro = 1
  458. var other = n - r
  459. var choice = r
  460. if (choice < other) {
  461. choice = other
  462. other = r
  463. }
  464. for (var i = choice + 1; i <= n; i++) {
  465. pro *= i
  466. }
  467. return pro / Mexp.math.fact(other)
  468. },
  469. changeSign: function (x) {
  470. return -x
  471. },
  472. cos: function (x) {
  473. if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
  474. return Math.cos(x)
  475. },
  476. cosh: function (x) {
  477. return (Math.pow(Math.E, x) + Math.pow(Math.E, -1 * x)) / 2
  478. },
  479. div: function (a, b) {
  480. return a / b
  481. },
  482. fact: function (n) {
  483. if (n % 1 !== 0) return 'NaN'
  484. var pro = 1
  485. for (var i = 2; i <= n; i++) {
  486. pro *= i
  487. }
  488. return pro
  489. },
  490. inverse: function (x) {
  491. return 1 / x
  492. },
  493. log: function (i) {
  494. return Math.log(i) / Math.log(10)
  495. },
  496. mod: function (a, b) {
  497. return a % b
  498. },
  499. mul: function (a, b) {
  500. return a * b
  501. },
  502. P: function (n, r) {
  503. var pro = 1
  504. for (var i = Math.floor(n) - Math.floor(r) + 1; i <= Math.floor(n); i++) {
  505. pro *= i
  506. }
  507. return pro
  508. },
  509. Pi: function (low, high, ex) {
  510. var pro = 1
  511. for (var i = low; i <= high; i++) {
  512. pro *= Number(ex.postfixEval({
  513. n: i
  514. }))
  515. }
  516. return pro
  517. },
  518. pow10x: function (e) {
  519. var x = 1
  520. while (e--) {
  521. x *= 10
  522. }
  523. return x
  524. },
  525. sigma: function (low, high, ex) {
  526. var sum = 0
  527. for (var i = low; i <= high; i++) {
  528. sum += Number(ex.postfixEval({
  529. n: i
  530. }))
  531. }
  532. return sum
  533. },
  534. sin: function (x) {
  535. if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
  536. return Math.sin(x)
  537. },
  538. sinh: function (x) {
  539. return (Math.pow(Math.E, x) - Math.pow(Math.E, -1 * x)) / 2
  540. },
  541. sub: function (a, b) {
  542. return a - b
  543. },
  544. tan: function (x) {
  545. if (Mexp.math.isDegree) x = Mexp.math.toRadian(x)
  546. return Math.tan(x)
  547. },
  548. tanh: function (x) {
  549. return Mexp.sinha(x) / Mexp.cosha(x)
  550. },
  551. toRadian: function (x) {
  552. return x * Math.PI / 180
  553. }
  554. }
  555. Mexp.Exception = function (message) {
  556. this.message = message
  557. }
  558. module.exports = Mexp
  559. },{}],4:[function(require,module,exports){
  560. var Mexp=require('./lexer.js');
  561. Mexp.prototype.toPostfix = function () {
  562. 'use strict';
  563. var post=[],elem,popped,prep,pre,ele;
  564. var stack=[{value:"(",type:4,pre:0}];
  565. var arr=this.value;
  566. for (var i=1; i < arr.length; i++) {
  567. if(arr[i].type===1||arr[i].type===3||arr[i].type===13){ //if token is number,constant,or n(which is also a special constant in our case)
  568. if(arr[i].type===1)
  569. arr[i].value=Number(arr[i].value);
  570. post.push(arr[i]);
  571. }
  572. else if(arr[i].type===4){
  573. stack.push(arr[i]);
  574. }
  575. else if(arr[i].type===5){
  576. while((popped=stack.pop()).type!==4){
  577. post.push(popped);
  578. }
  579. }
  580. else if(arr[i].type===11){
  581. while((popped=stack.pop()).type!==4){
  582. post.push(popped);
  583. }
  584. stack.push(popped);
  585. }
  586. else {
  587. elem=arr[i];
  588. pre=elem.pre;
  589. ele=stack[stack.length-1];
  590. prep=ele.pre;
  591. var flag=ele.value=='Math.pow'&&elem.value=='Math.pow';
  592. if(pre>prep)stack.push(elem);
  593. else {
  594. while(prep>=pre&&!flag||flag&&pre<prep){
  595. popped=stack.pop();
  596. ele=stack[stack.length-1];
  597. post.push(popped);
  598. prep=ele.pre;
  599. flag=elem.value=='Math.pow'&&ele.value=='Math.pow';
  600. }
  601. stack.push(elem);
  602. }
  603. }
  604. }
  605. return new Mexp(post);
  606. };
  607. module.exports=Mexp;
  608. },{"./lexer.js":2}],5:[function(require,module,exports){
  609. var Mexp=require('./postfix.js');
  610. Mexp.prototype.postfixEval = function (UserDefined) {
  611. 'use strict';
  612. UserDefined=UserDefined||{};
  613. UserDefined.PI=Math.PI;
  614. UserDefined.E=Math.E;
  615. var stack=[],pop1,pop2,pop3;
  616. var disp=[];
  617. var temp='';
  618. var arr=this.value;
  619. var bool=(typeof UserDefined.n!=="undefined");
  620. for(var i=0;i<arr.length;i++){
  621. if(arr[i].type===1){
  622. stack.push({value:arr[i].value,type:1});
  623. }
  624. else if(arr[i].type===3){
  625. stack.push({value:UserDefined[arr[i].value],type:1});
  626. }
  627. else if(arr[i].type===0){
  628. if(typeof stack[stack.length-1].type==="undefined"){
  629. stack[stack.length-1].value.push(arr[i]);
  630. }
  631. else stack[stack.length-1].value=arr[i].value(stack[stack.length-1].value);
  632. }
  633. else if(arr[i].type===7){
  634. if(typeof stack[stack.length-1].type==="undefined"){
  635. stack[stack.length-1].value.push(arr[i]);
  636. }
  637. else stack[stack.length-1].value=arr[i].value(stack[stack.length-1].value);
  638. }
  639. else if(arr[i].type===8){
  640. pop1=stack.pop();
  641. pop2=stack.pop();
  642. stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});
  643. }
  644. else if(arr[i].type===10){
  645. pop1=stack.pop();
  646. pop2=stack.pop();
  647. if(typeof pop2.type==="undefined"){
  648. pop2.value=pop2.concat(pop1);
  649. pop2.value.push(arr[i]);
  650. stack.push(pop2);
  651. }
  652. else if (typeof pop1.type==="undefined") {
  653. pop1.unshift(pop2);
  654. pop1.push(arr[i]);
  655. stack.push(pop1);
  656. }
  657. else{
  658. stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});
  659. }
  660. }
  661. else if(arr[i].type===2||arr[i].type===9){
  662. pop1=stack.pop();
  663. pop2=stack.pop();
  664. if(typeof pop2.type==="undefined"){
  665. console.log(pop2);
  666. pop2=pop2.concat(pop1);
  667. pop2.push(arr[i]);
  668. stack.push(pop2);
  669. }
  670. else if (typeof pop1.type==="undefined") {
  671. pop1.unshift(pop2);
  672. pop1.push(arr[i]);
  673. stack.push(pop1);
  674. }
  675. else{
  676. stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});
  677. }
  678. }
  679. else if(arr[i].type===12){
  680. pop1=stack.pop();
  681. if (typeof pop1.type!=="undefined") {
  682. pop1=[pop1];
  683. }
  684. pop2=stack.pop();
  685. pop3=stack.pop();
  686. stack.push({type:1,value:arr[i].value(pop3.value,pop2.value,new Mexp(pop1))});
  687. }
  688. else if(arr[i].type===13){
  689. if(bool){
  690. stack.push({value:UserDefined[arr[i].value],type:3});
  691. }
  692. else stack.push([arr[i]]);
  693. }
  694. }
  695. if (stack.length>1) {
  696. throw(new Mexp.exception("Uncaught Syntax error"));
  697. }
  698. return stack[0].value>1000000000000000?"Infinity":parseFloat(stack[0].value.toFixed(15));
  699. };
  700. Mexp.eval=function(str,tokens,obj){
  701. if (typeof tokens==="undefined") {
  702. return this.lex(str).toPostfix().postfixEval();
  703. }
  704. else if (typeof obj==="undefined") {
  705. if (typeof tokens.length!=="undefined")
  706. return this.lex(str,tokens).toPostfix().postfixEval();
  707. else
  708. return this.lex(str).toPostfix().postfixEval(tokens);
  709. }
  710. else
  711. return this.lex(str,tokens).toPostfix().postfixEval(obj);
  712. };
  713. module.exports=Mexp;
  714. },{"./postfix.js":4}]},{},[1])(1)
  715. });