-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseMathExpression.js
70 lines (67 loc) · 2.33 KB
/
parseMathExpression.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
function startParseMathExpression(arr) {
var i = 0;
var expr, oper1;
if (isTypeDefWord(arr[i].type)) {
oper1 = arr[i + 1];
expr = parseMathExpression(arr.slice(i + 3));
} else if (arr[i].type == 'IDENTIFIER') {
oper1 = arr[i];
expr = parseMathExpression(arr.slice(i + 2));
} else if (arr[i].type == '--') { // || arr[i + 1].type == 'INC'
return {
type: '--',
oper1: arr[i + 1]
}
} else if (arr[i + 1].type == '--' || arr[i + 2].type == '++') {
return
}
return {
type: ':=',
oper1: oper1,
oper2: expr
};
}
function parseMathExpression(arr) {
var signPos = getNextSignWithoutBracesPos(arr, ['==', '&&']);
if (signPos != null) {
var arr1 = arr.slice(0, signPos);
var arr2 = arr.slice(signPos + 1);
var node = {
type: arr[signPos].type,
oper2: parseMathExpression(arr1),
oper3: parseMathExpression(arr2)
}
return node;
} else {
signPos = getNextSignWithoutBracesPos(arr, ['+', '-']);
if (signPos != null) {
var arr1 = arr.slice(0, signPos);
var arr2 = arr.slice(signPos + 1);
var node = {
type: arr[signPos].type,
oper2: parseMathExpression(arr1),
oper3: parseMathExpression(arr2)
}
return node;
} else {
signPos = getNextSignWithoutBracesPos(arr, ['*', '/']);
if (signPos != null) {
var arr1 = arr.slice(0, signPos);
var arr2 = arr.slice(signPos + 1);
var node = {
type: arr[signPos].type,
oper2: parseMathExpression(arr1),
oper3: parseMathExpression(arr2)
}
return node;
} else {
if (arr[0].type == '(') {
var rightParPos = arr.length - 1;
return parseMathExpression(arr.slice(1, arr.length - 1));
} else if (arr[0].type == 'STRING' || arr[0].type == 'DOUBLE' || arr[0].type == 'INTEGER' || arr[0].type == 'BOOLEAN' || arr[0].type == 'IDENTIFIER') {
return arr[0];
}
}
}
}
}