converts scheme like arithmetic to js like arithmetic
//converts prefix addition to infix addition (+ 5 (- 7 5)) to 5 + 7 - 5
/* PrefixToInfix("(- 8 (+ 5 6)")
=> '8 - 5 + 6'
PrefixToInfix("(- 8(+ 5 6)")
=> '8 - 5 + 6'
PrefixToInfix("(* 9 (- 8 (+ 5 6)))")
=> '9 * 8 - 5 + 6 '
*/
function PrefixToInfix(line) {
var mathop = {"+":true, "-":true, "*":true, "/":true, "%":true};
var splits = line.split(/\(|\)| /);
for(var i=0;i<splits.length;i++) {
if(splits[i] === "" || splits[i] === undefined) {
splits.splice(i, 1);
}
}
for(var j=0;j<splits.length-2;j++) {
if(splits[j] in mathop && /[0-9]+/.test(splits[j+1]) && /[0-9]+/.test(splits[j+2])) {
var temp = splits[j];
splits[j] = splits[j+1];
splits[j+1] = temp;
}
else if(splits[j] in mathop && /[0-9]+/.test(splits[j+1]) && splits[j+2] in mathop){
var semp = splits[j];
splits[j] = splits[j+1];
splits[j+1] = semp;
}
}
return splits.join(" ");
}