3d8dfe187b086c02fbaabd35938c8094d38ce8ce0ef006a409df99de4fdc6910758cd78da0f33de8ce527c3ad19578729ea6296de1c412d0f98e79448b87f0 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. var Mexp = require('./postfix.js')
  2. Mexp.prototype.postfixEval = function (UserDefined) {
  3. 'use strict'
  4. UserDefined = UserDefined || {}
  5. UserDefined.PI = Math.PI
  6. UserDefined.E = Math.E
  7. var stack = [],
  8. pop1,
  9. pop2,
  10. pop3
  11. var disp = []
  12. var temp = ''
  13. var arr = this.value
  14. var bool = typeof UserDefined.n !== 'undefined'
  15. for (var i = 0; i < arr.length; i++) {
  16. if (arr[i].type === 1) {
  17. stack.push({ value: arr[i].value, type: 1 })
  18. } else if (arr[i].type === 3) {
  19. stack.push({ value: UserDefined[arr[i].value], type: 1 })
  20. } else if (arr[i].type === 0) {
  21. if (typeof stack[stack.length - 1].type === 'undefined') {
  22. stack[stack.length - 1].value.push(arr[i])
  23. } else stack[stack.length - 1].value = arr[i].value(stack[stack.length - 1].value)
  24. } else if (arr[i].type === 7) {
  25. if (typeof stack[stack.length - 1].type === 'undefined') {
  26. stack[stack.length - 1].value.push(arr[i])
  27. } else stack[stack.length - 1].value = arr[i].value(stack[stack.length - 1].value)
  28. } else if (arr[i].type === 8) {
  29. var popped = []
  30. for (var x = 0; x < arr[i].numberOfArguments; x++) {
  31. popped.push(stack.pop().value)
  32. }
  33. stack.push({ type: 1, value: arr[i].value.apply(arr[i], popped.reverse()) })
  34. } else if (arr[i].type === 10) {
  35. pop1 = stack.pop()
  36. pop2 = stack.pop()
  37. if (typeof pop2.type === 'undefined') {
  38. pop2.value = pop2.concat(pop1)
  39. pop2.value.push(arr[i])
  40. stack.push(pop2)
  41. } else if (typeof pop1.type === 'undefined') {
  42. pop1.unshift(pop2)
  43. pop1.push(arr[i])
  44. stack.push(pop1)
  45. } else {
  46. stack.push({ type: 1, value: arr[i].value(pop2.value, pop1.value) })
  47. }
  48. } else if (arr[i].type === 2 || arr[i].type === 9) {
  49. pop1 = stack.pop()
  50. pop2 = stack.pop()
  51. if (typeof pop2.type === 'undefined') {
  52. pop2 = pop2.concat(pop1)
  53. pop2.push(arr[i])
  54. stack.push(pop2)
  55. } else if (typeof pop1.type === 'undefined') {
  56. pop1.unshift(pop2)
  57. pop1.push(arr[i])
  58. stack.push(pop1)
  59. } else {
  60. stack.push({ type: 1, value: arr[i].value(pop2.value, pop1.value) })
  61. }
  62. } else if (arr[i].type === 12) {
  63. pop1 = stack.pop()
  64. if (typeof pop1.type !== 'undefined') {
  65. pop1 = [pop1]
  66. }
  67. pop2 = stack.pop()
  68. pop3 = stack.pop()
  69. stack.push({ type: 1, value: arr[i].value(pop3.value, pop2.value, new Mexp(pop1)) })
  70. } else if (arr[i].type === 13) {
  71. if (bool) {
  72. stack.push({ value: UserDefined[arr[i].value], type: 3 })
  73. } else stack.push([arr[i]])
  74. }
  75. }
  76. if (stack.length > 1) {
  77. throw new Mexp.Exception('Uncaught Syntax error')
  78. }
  79. return stack[0].value > 1000000000000000 ? 'Infinity' : parseFloat(stack[0].value.toFixed(15))
  80. }
  81. Mexp.eval = function (str, tokens, obj) {
  82. if (typeof tokens === 'undefined') {
  83. return this.lex(str).toPostfix().postfixEval()
  84. } else if (typeof obj === 'undefined') {
  85. if (typeof tokens.length !== 'undefined') return this.lex(str, tokens).toPostfix().postfixEval()
  86. else return this.lex(str).toPostfix().postfixEval(tokens)
  87. } else return this.lex(str, tokens).toPostfix().postfixEval(obj)
  88. }
  89. module.exports = Mexp