Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var parse = function (x) {
- var temp = [];
- var pushUn = function (a) {
- temp.push(a(temp.pop()));
- };
- function isNumber(n) {
- return !isNaN(parseFloat(n)) && isFinite(n);
- }
- var pushBi = function (a) {
- var r = temp.pop();
- var l = temp.pop();
- temp.push(a(l, r));
- };
- var tokens = x.split(' ');
- for (var i = 0; i < tokens.length; i++) {
- if(tokens[i] in vars){
- temp.push(variable(tokens[i]));
- }
- else if(tokens[i] in binOps){
- pushBi(binOps[tokens[i]]);
- }
- else if(tokens[i] in unOps){
- pushUn(unOps[tokens[i]]);
- }
- else if(isNumber(tokens[i])){
- temp.push(cnst(parseInt(tokens[i])));
- }
- }
- return temp.pop();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement