-
Notifications
You must be signed in to change notification settings - Fork 1
/
createParser.js
61 lines (46 loc) · 1.92 KB
/
createParser.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
function createParser(){
//really think about how much you have messed up by representing quantificators with A and E
//like really consider how dumb you are
//fix it
const opOrder = ['>', '+', '*', '$', '%', '-']; //$ => A; % => E //so dumb, but I dont see a lazier way to do it
const operators = {
binary_A : ['>', '+', '*'] , //AoB
binary_B : ['$', '%'] , //oAB
unary : ['-'] , //oA
}
const parse = function(parseString){
while(parseString[0] == '(' && parseString[parseString.length-1] == ')'){
parseString = parseString.slice(1, -1);
}
//bad time and operation complexity
for(let j = 0; j < opOrder.length; j ++){
const operator = opOrder[j];
let openBracketCount = 0;
for(let i = 0; i < parseString.length; i++){
let activeChar = parseString[i];
//counting if inside open brackets
if(activeChar == '('){
openBracketCount += 1;
continue;
} else if(activeChar == ')'){
openBracketCount -= 1;
continue;
}
//never evaluate stuff inside brackets (skip)
if(openBracketCount != 0){continue;}
if(activeChar == operator){
if(operators.binary_A.includes(operator)){
return new Expression(activeChar, parse(parseString.slice(0, i)), parse(parseString.slice(i+1, parseString.length)));
} else if (operators.binary_B.includes(operator)){
return new Expression({'$':'A', '%':'E'}[activeChar], parseString[i+1], parse(parseString.slice(i+2, parseString.length)))
} else if (operators.unary.includes(operator)){
return new Expression(activeChar, parse(parseString.slice(i+1, parseString.length))); //this is a weird way to write it as when calling upon negation, it should only ever be possivle that i = 0
}
console.log('createParser.js has some problems with the list constants');
}
}
}
return new Expression('0', ...parseString);
}
return parse;
}