-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.js
284 lines (266 loc) · 7.24 KB
/
Parser.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
function Parser(exp){
var variableValues = {};
var expression;
var iElement;
//Constants
var CONSTANTS ={};
CONSTANTS["pi"] = Math.PI;
CONSTANTS["e"] = Math.E;
var RESERVED_NAMES = new Array ( //Operations
"sin",
"cos",
"tan",
"log",
"min",
"max",
"abs",
//constants
"pi",
"e"
)
Parser(exp);
/**
*Creates the object with the expression to be evaluated
*@param exp The expression to be evaluated.
*/
function Parser(exp){
if(typeof(exp) == "string"){
expression = exp;
}else{
throw "String expected for expression, but "+typeof(exp)+" received.";
}
}
/**
*Set a variable value, if the variable already exists, changes its value, else creates the variable with the passed value.
*Similar to variable = value.
*
*@param variable Variable name (type: string)
*@param value Variable value (type: int or float)
*/
this.setVariable = function(variable, value){
if(typeof(variable) != "string"){
throw "String expected for variable, but "+typeof(exp)+" received.";
}
if(!__verifyVariable(variable)){
throw "Invalid characters in variable name.";
}
if(__isReservedName(variable)){
throw "This name is a function or a constant.";
}
if(typeof(value) == "number"){
throw "Invalid value.";
}
variableValues[variable] = value.toString();
return true;
}
/**
*Changes the expression to be evaluated
*@param newExp The new expression(type: string)
*/
this.changeExpression = function(newExp){
if(typeof(newExp) == "string"){
expression = newExp;
}else{
throw "String expected for expression, but "+typeof(newExp)+" received.";
}
}
this.evaluate = function(){
var exp = __replaceValues();
exp = exp.replace(/\s/g, "");
if(/^[^0-9\^\-\+\*\\\(\)]$/g.test(exp)){
throw "Some illegal characters or unset variables in the expression.";
}
return __expressionResult(exp);
}
//DEBUG PROPOSE
/**
*Gives the variable value.
*@param variable The variable name.
*/
this.getValue = function(variable){
if(!(variable in variableValues)){
console.log("variável inválida");
}else{
console.log(variable + " = " + variableValues[variable]);
}
}
/**
*Verify if the variable exists.
*@param variable The variable name.
*/
this.variableExists = function(variable){
return variable in variableValues;
}
//PRIVATE METHODS
/**
*Verify if the variable respect name rules
*@param name The name to be verified.
*/
function __verifyVariable(name){
var rule = new RegExp("^[A-Za-z_][0-9A-Za-z_]*$");
return rule.test(name);
}
/**
*Verify if this name is used for some operations
*@param name The name to be verified.
*/
function __isReservedName(name){
return RESERVED_NAMES.indexOf(name) > -1
}
/**
*Replace variables with their respective values
*/
function __replaceValues(){
var result = expression;
//substitute variable values
for(var key in variableValues){
var regex = new RegExp("\\b"+key+"\\b", "g");
result = result.replace(regex, variableValues[key]);
}
//substitute constant values
for(var key in CONSTANTS){
var regex = new RegExp("\\b"+key+"\\b", "g");
result = result.replace(regex, CONSTANTS[key]);
}
return result;
}
/**
*Compile and get the value of the expression
*@param exp The expression to be calculated.
*/
function __expressionResult(exp){
iElement = 0;
return __expression(exp);
}
//PARSER RULES
/*
number = {"0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"|"."}
factor = number | "(" expression ")" | wordInFunctionsArray"(" factor ["," factor] ")"
element = factor [{"^" factor}]
component = element [{("*" | "/") element}]
expression = component [{("+" | "-") component}]
*/
function __expression(exp){
var result = 0;
result = __component(exp);
while(iElement < exp.length){
if(exp[iElement] == "+"){
iElement++;
result += __component(exp);
}else if(exp[iElement] == "-"){
iElement++;
result -= __component(exp);
}else if(exp[iElement] == ")" || exp[iElement] == ","){
break;
}
}
return result;
}
function __component(exp){
var result = 0;
result = __element(exp);
while((exp[iElement] == '*' || exp[iElement] == '/') && (iElement < exp.length)){
if(exp[iElement] == "*"){
iElement++;
result *= __element(exp);
} else if(exp[iElement] == "/"){
iElement++;
result /= __element(exp);
}
}
return result;
}
function __element(exp){
var result = 0;
result = __factor(exp);
while (exp[iElement] == '^' && iElement < exp.length){
if(exp[iElement] == "^"){
iElement++;
result = Math.pow(result,__factor(exp));
}
}
return result;
}
function __factor(exp){
var result = 0;
var functions = new Array("sin", "cos", "tan","log","max","min","abs");
if(exp[iElement] == "("){
iElement++;
result = __expression(exp);
if(exp[iElement] != ")") throw "Missing ')' in column "+iElement.toString()+".";
iElement++;
}else if(functions.indexOf(exp[iElement]+exp[iElement+1]+exp[iElement+2]) > -1 && exp[iElement+3] == "("){
//function verify
var f = exp[iElement]+exp[iElement+1]+exp[iElement+2];
iElement += 4;
var num1, num2;
var verifyNum2 = false;
num1 = __expression(exp);
if(exp[iElement] == ","){
if(parseFloat(num1) != parseFloat(num1)) throw "Expected a number in column "+iElement-1+".";
iElement++;
verifyNum2 = true;
num2 = __expression(exp);
}
if(exp[iElement] != ")") throw "Missing ')' in column "+iElement.toString()+".";
iElement++;
if(verifyNum2){
if(parseFloat(num2) != parseFloat(num2)) throw "Expected a number in column "+iElement-2+".";
}else{
if(parseFloat(num1) != parseFloat(num1)) throw "Expected a number in column "+iElement-2+".";
}
if(f == "sin"){
result = Math.sin(num1);
}else if(f == "cos"){
result = Math.cos(num1);
}else if(f == "tan"){
result = Math.tan(num1);
}else if(f == "log"){
result = Math.log(num1)/Math.log(num2);
}else if(f == "min"){
result = Math.min(num1,num2);
}else if(f == "max"){
result = Math.max(num1,num2);
}else if(f == "abs"){
result = Math.abs(num1);
}
}else {
result = __number(exp);
}
return result;
}
function __number(exp){
var values = new Array("0","1","2","3","4","5","6","7","8","9",".");
var s = "";
var pointCount = 0;
if((values.indexOf(exp[iElement-1]) > -1 || exp[iElement-1] == ')' || exp[iElement-1] == '(') && (exp[iElement] == '-' || exp[iElement] == '+')){
s += exp[iElement++];
}
while(values.indexOf(exp[iElement]) > -1 && iElement < exp.length){
if(exp[iElement] == "."){
if(pointCount == 1) throw "Invalid element in column "+iElement.toString()+".";
pointCount++;
}
s+= exp[iElement++];
}
if(parseFloat(s) != parseFloat(s)) throw "Invalid element in column "+iElement.toString()+".";
return parseFloat(s)
}
function __verifyFunction(f, a, b){
if(f == "sin"){
return Math.sin(a);
}else if(f == "cos"){
return Math.cos(a);
}else if(f == "tan"){
return Math.tan(a);
}else if(f == "log"){
return Math.log(a)/Math.log(b);
}else if(f == "min"){
return Math.min(a,b);
}else if(f == "max"){
return Math.max(a,b);
}else if(f == "abs"){
return Math.abs(a);
}
}
}