-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.js
80 lines (72 loc) · 1.78 KB
/
convert.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
function isalnum(input){
if(input.match(/^[a-zA-Z0-9]+$/)){
return true;
} else {
return false;
}
}
//TODO: parentheses, functions, exponents, unary operators
//dijkstra'Z shunting-yard,
// haad ik maar en tijdmachine
//infix to postfix converter
function infix_to_postfix(blocks)
{
//INPUT: blocks with nodes
//OUTPUT: array of post order nodes
postfix = [];
opstack = [];
for(var idx = 0; idx < blocks.length; idx++){
token = blocks[idx].node.value;
op = operators.indexOf(token);
if(op != -1) {
while(1) {
prev = opstack[opstack.length-1];
if(!prev || prev.value == "("){
opstack.push(blocks[idx].node);
break;
} else {
//note: < vs <= here makes a difference
//for things like 3+3*4+2.
//the result will be technically equivalent
//but not of the preferred form for tree transformations
if (op < operators.indexOf(prev.value)) {
opstack.push(blocks[idx].node);
break;
} else {
postfix.push(opstack.pop());
}
}
}
} else if(token == "("){
opstack.push( blocks[idx].node )
} else if (token == ")") {
while(opstack.length > 0)
{
val = opstack.pop();
if(val.value != "(") {
postfix.push(val)
} else {
break;
}
}
//todo check for stack error if opstack.length == 0
} else {
postfix.push(blocks[idx].node);
}
}
while(opstack.length > 0) {
postfix.push(opstack.pop());
}
return postfix;
}
var tree;
function postfix_to_tree(input)
{
nodestack = [];
tree = new mathTree();
tree.buildTree(nodestack, nodestack.length-1);
}
function p_to_i(input, output)
{
output.innerHTML = input;
}