Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getFullByRegExp(reg) {
- let res = "";
- while (ind < expr.length && reg.test(expr[ind])) {
- res += expr[ind];
- ind++;
- }
- return res;
- }
- function skipWhiteSpaces() {
- while (ind < expr.length && /\s/.test(expr[ind])) {
- ind++;
- }
- }
- function getOperation() {
- skipWhiteSpaces();
- switch (expr[ind]) {
- case '-':
- case '+':
- case '/':
- case '*':
- return expr[ind];
- default :
- // ошибка
- }
- }
- function getOperand() {
- let curStack = [];
- skipWhiteSpaces();
- switch (expr[ind]) {
- case '(':
- ind++;
- let symb = getOperation();
- curStack.push(symb);
- ind++;
- let op1 = getOperand();
- ind++;
- let op2 = getOperand();
- for (let i = 0; i < op2.length; i++){
- curStack.push(op2[i]);
- }
- for (let i = 0; i < op1.length; i++){
- curStack.push(op1[i]);
- }
- break;
- case ')':
- ind++;
- break;
- default :
- let st = [];
- if (/[A-Za-z]/.test(expr[ind])) {
- st.push(getFullByRegExp(/[A-Za-z]/));
- return st;
- } else if (/\d/.test(expr[ind])) {
- st.push(parseInt(getFullByRegExp(/\d/)));
- return st;
- } else {
- // error
- }
- }
- return curStack;
- }
- const parsePrefix = function (str) {
- ind = 0;
- expr = str;
- skipWhiteSpaces();
- switch (expr[ind]) {
- case '(':
- ind++;
- let symb = getOperation();
- stack.push(symb);
- ind++;
- let op1 = getOperand();
- ind++;
- let op2 = getOperand();
- for (let i = 0; i < op2.length; i++){
- stack.push(op2[i]);
- }
- for (let i = 0; i < op1.length; i++){
- stack.push(op1[i]);
- }
- break;
- }
- let res = "";
- while (stack.length > 0) {
- res += stack.pop();
- res += " ";
- }
- // return parse(res);
- return res;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement