Skip to content

Commit

Permalink
Merge pull request #6 from amasad/master
Browse files Browse the repository at this point in the history
Minimize stack trace needed
  • Loading branch information
nasser committed Jan 17, 2013
2 parents d50ca41 + 8341101 commit 2db6065
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions public/qlb/qlb.js
Expand Up @@ -39,7 +39,7 @@ Qlb.eval = function(exp, env) {
return rest return rest


} else if(first == "إفعل") { } else if(first == "إفعل") {
rest = rest.map(function(e) { return Qlb.eval(e, env) }); rest = run(rest, env);
return rest[rest.length - 1]; return rest[rest.length - 1];


} else if(first == "إذا") { } else if(first == "إذا") {
Expand Down Expand Up @@ -75,7 +75,8 @@ Qlb.eval = function(exp, env) {
} }


} else { } else {
var exps = exp.map(function(p) { return Qlb.eval(p, env) }) var exps = run(exp, env);

if(typeof exps[0] == "function") if(typeof exps[0] == "function")
// first element evaluates to a function // first element evaluates to a function
return exps.shift().apply(this, exps) return exps.shift().apply(this, exps)
Expand All @@ -86,23 +87,33 @@ Qlb.eval = function(exp, env) {
} else } else
// return last element // return last element
return exps[exps.length - 1]; return exps[exps.length - 1];



} }
} }
}; };


Qlb.execute = function(code) {
function run(ast, env) {
try { try {
var ast = Qlb.parser.parse(code);
var val; var val;
var ret = [];
for (var i = 0; i < ast.length; i++) { for (var i = 0; i < ast.length; i++) {
val = Qlb.eval(ast[i], Qlb.globalEnvironment); val = Qlb.eval(ast[i], env);
ret.push(val);
}; };
return val; return ret;

} catch(e) { } catch(e) {
Qlb.handleUncaughtException(e); Qlb.handleUncaughtException(e);
}
}


Qlb.execute = function(code) {
try {
var ast = Qlb.parser.parse(code);
return run(ast, Qlb.globalEnvironment).pop();
} catch (e) {
Qlb.handleUncaughtException(e);
} }
}; };


Expand Down

0 comments on commit 2db6065

Please sign in to comment.