-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPNEvaluator.js
156 lines (154 loc) · 6.15 KB
/
PNEvaluator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* PNEvaluator - Evaluate a postfix notation math expression
* @author Adileo Barone <[email protected]>
*
* @example
* //returns: 4
* new PNEvaluator('2 x +', function(errs, output){
* if(errs.length > 0){
* console.log(errs)
* }else{
* console.log(output)
* }
* }, {x:2})
* @example <caption>Available math functions</caption>
{
'abs': function(x) {return Math.abs(x)},
'cos': function(x) {return Math.cos(x)},
'exp': function(x) {return Math.exp(x)},
'log': function(x) {return Math.log(x)},
'sin': function(x) {return Math.sin(x)},
'pow': function(x,y) {return Math.pow(x,y)},
'tan': function(x) {return Math.tan(x)},
'acos': function(x) {return Math.acos(x)},
'asin': function(x) {return Math.asin(x)},
'atan': function(x) {return Math.atan(x)},
'cbrt': function(x) {return Math.cbrt(x)},
'ceil': function(x) {return Math.ceil(x)},
'cosh': function(x) {return Math.cosh(x)},
'imul': function(x,y) {return Math.imul(x,y)},
'log2': function(x) {return Math.log2(x)},
'sign': function(x) {return Math.sign(x)},
'sinh': function(x) {return Math.sinh(x)},
'sqrt': function(x) {return Math.sqrt(x)},
'tanh': function(x) {return Math.tanh(x)},
'acosh': function(x) {return Math.acosh(x)},
'asinh': function(x) {return Math.asinh(x)},
'atan2': function(x) {return Math.atan2(x)},
'atanh': function(x) {return Math.atanh(x)},
'clz32': function(x) {return Math.clz32(x)},
'expm1': function(x) {return Math.expm1(x)},
'floor': function(x) {return Math.floor(x)},
'log10': function(x) {return Math.log10(x)},
'log1p': function(x) {return Math.log1p(x)},
'round': function(x) {return Math.round(x)},
'trunc': function(x) {return Math.trunc(x)},
'fround': function(x) {return Math.fround(x)},
'random': function() {return Math.random()},
'max': function(x,y) {if(x>y){return x;}else{return y;}},
'min': function(x,y) {if(x<y){return x;}else{return y;}},
'/': function(x,y) {return x/y},
'*': function(x,y) {return x*y},
'+': function(x,y) {return x+y},
'-': function(x,y) {return x-y},
'^': function(x,y) {return Math.pow(x,y)},
'%': function(x,y) {return x % y},
}
* @constructor
* @param {string} string - math expression specified in postfix notation
* @param {PNEvaluator~evaluateCallback} callback - called when expression is evaluated
* @param {PNEvaluator~Variables} variables - values of variables in the expression
* @param {PNEvaluator~Options} options - configs
*
*/
function PNEvaluator(string, callback, variables = {}, options = null) {
this.variables = variables
this.funcs = {
'abs': function(x) {return Math.abs(x)},
'cos': function(x) {return Math.cos(x)},
'exp': function(x) {return Math.exp(x)},
'log': function(x) {return Math.log(x)},
'sin': function(x) {return Math.sin(x)},
'pow': function(x,y) {return Math.pow(x,y)},
'tan': function(x) {return Math.tan(x)},
'acos': function(x) {return Math.acos(x)},
'asin': function(x) {return Math.asin(x)},
'atan': function(x) {return Math.atan(x)},
'cbrt': function(x) {return Math.cbrt(x)},
'ceil': function(x) {return Math.ceil(x)},
'cosh': function(x) {return Math.cosh(x)},
'imul': function(x,y) {return Math.imul(x,y)},
'log2': function(x) {return Math.log2(x)},
'sign': function(x) {return Math.sign(x)},
'sinh': function(x) {return Math.sinh(x)},
'sqrt': function(x) {return Math.sqrt(x)},
'tanh': function(x) {return Math.tanh(x)},
'acosh': function(x) {return Math.acosh(x)},
'asinh': function(x) {return Math.asinh(x)},
'atan2': function(x) {return Math.atan2(x)},
'atanh': function(x) {return Math.atanh(x)},
'clz32': function(x) {return Math.clz32(x)},
'expm1': function(x) {return Math.expm1(x)},
'floor': function(x) {return Math.floor(x)},
'log10': function(x) {return Math.log10(x)},
'log1p': function(x) {return Math.log1p(x)},
'round': function(x) {return Math.round(x)},
'trunc': function(x) {return Math.trunc(x)},
'fround': function(x) {return Math.fround(x)},
'random': function() {return Math.random()},
'max': function(x,y) {if(x>y){return x;}else{return y;}},
'min': function(x,y) {if(x<y){return x;}else{return y;}},
'/': function(x,y) {return x/y},
'*': function(x,y) {return x*y},
'+': function(x,y) {return x+y},
'-': function(x,y) {return x-y},
'^': function(x,y) {return Math.pow(x,y)},
'%': function(x,y) {return x % y},
}
this.stack = []
this.errors = []
this.debug = false
if(options){
if(options.debug && options.debug == true){
this.debug = true
}
}
var s = string.split(' ')
for (t in s) {
var tok = s[t]
if(this.debug) console.log(this.stack)
if (this.funcs.hasOwnProperty(tok)) {
//It's a function token
var n_args = this.funcs[tok].length
var args = []
for (var i = 0; i < this.funcs[tok].length; i++){
args.push(this.stack.pop())
}
args = args.reverse()
var res = this.funcs[tok].apply(this, args)
this.stack.push(res)
} else if (tok.match(/[a-zA-Z]{1}[0-9]*/)) {
//It's a variable token
if(this.variables.hasOwnProperty(tok)){
this.stack.push(this.variables[tok])
}else{
if(this.debug) console.log('unknown variable value')
this.errors.push('Unknown variable value')
}
} else if (parseFloat(tok)) {
var n = parseFloat(tok)
this.stack.push(n)
} else {
if(this.debug) console.log('not recognized')
this.errors.push('Item not recognized')
}
}
if(this.debug) console.log(this.stack)
if(this.stack.length != 1){
this.errors.push('Wrong expression')
callback(this.errors)
}else{
callback(this.errors, this.stack)
}
}
module.exports = PNEvaluator;