Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #6 from amasad/master

Minimize stack trace needed
  • Loading branch information...
commit 2db606595ad815d8f0224b033d286db1ac78ddf6 2 parents d50ca41 + 8341101
@nasser authored
Showing with 18 additions and 7 deletions.
  1. +18 −7 public/qlb/qlb.js
View
25 public/qlb/qlb.js
@@ -39,7 +39,7 @@ Qlb.eval = function(exp, env) {
return rest
} else if(first == "إفعل") {
- rest = rest.map(function(e) { return Qlb.eval(e, env) });
+ rest = run(rest, env);
return rest[rest.length - 1];
} else if(first == "إذا") {
@@ -75,7 +75,8 @@ Qlb.eval = function(exp, env) {
}
} else {
- var exps = exp.map(function(p) { return Qlb.eval(p, env) })
+ var exps = run(exp, env);
+
if(typeof exps[0] == "function")
// first element evaluates to a function
return exps.shift().apply(this, exps)
@@ -86,23 +87,33 @@ Qlb.eval = function(exp, env) {
} else
// return last element
return exps[exps.length - 1];
+
}
}
};
-Qlb.execute = function(code) {
+
+function run(ast, env) {
try {
- var ast = Qlb.parser.parse(code);
var val;
+ var ret = [];
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) {
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);
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.