math-expression-evaluator.js 18 KB

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