Permalink
Browse files

scope v2

  • Loading branch information...
1 parent 976bdc9 commit ac9bfbd893a8b4d84622ec3c528aa82460975ad5 hochang committed Oct 17, 2012
@@ -332,6 +332,7 @@ function walk(line, symbols) {
walk(tryline, symbols);
});
_.each(line.catchClause, function(currentcatch){
+ walk(currentcatch.condition, symbols);
_.each(currentcatch.lines, function(catchline){
walk(catchline, symbols);
})

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -89,6 +89,47 @@
return -1;
}
+ function countLogicVars(condition){
+ var fallback, ret;
+ if (condition.fallback){
+ fallback = countLogicVars(condition.fallback);
+ }else{
+ fallback = [];
+ }
+ switch(condition.logic){
+ case 'and':
+ ret = _.all(condition.values, function(onecond){
+ return countLogicVars(onecond);
+ });
+ case 'not':
+ ret = !countLogicVars(condition.values);
+ default://normal
+ ret = [condition.values];
+ }
+ return ret.concat(fallback);
+ }
+
+ function findThrows(tryClause){
+ if(!tryClause){
+ return [];
+ }
+ var errMap = [];
+ for (var i = 0; i < tryClause.length; i++){
+ var thistry = tryClause[i];
+ if(thistry.type === 'throw'){
+ errMap.push(thistry.err);
+ }else if(thistry.type === 'if'){
+ errMap = errMap.concat(findThrows(thistry.if));
+ errMap = errMap.concat(findThrows(thistry.else));
+ }else if (thistry.type === 'try'){
+ errMap = errMap.concat(findThrows(thistry.tryClause));
+ errMap = errMap.concat(findThrows(thistry.catchClause));
+ errMap = errMap.concat(findThrows(thistry.finallyClause));
+ }
+ }
+ return errMap;
+ }
+
// Split join statements in to main and a joiner. The main statement is the independent
// and the joiner depends on the outcome of the main statement. In this process, we split
// the columns in the columns clause across the main and joiner, and merge them at runtime
@@ -418,6 +459,16 @@ ElseClause = 'else' insig '{' insig larr:TryCrlf* insig'}' {
}
TryClause = 'try' insig '{' insig tryClause:TryCrlf+ insig '}' catchClause:CatchClause+ finallyClause:FinallyClause? {
+ var errMap = findThrows(tryClause);
+ for (var i = 0; i < catchClause.length; i++){
+ var catchvars = countLogicVars(catchClause[i].condition);
+ for (var j = 0; j < catchvars.length; j++){
+ var errVar = catchvars[j];
+ if(errMap.indexOf(errVar) == -1){
+ throw new this.SyntaxError("Line " + line + " exception variable "+errVar+" is not thrown inside the try statement.");
+ }
+ }
+ }
return {
id : id++,
line : line,
@@ -397,10 +397,6 @@ Engine.prototype.execute = function() {
if(statement.scope && execState[statement.scope.id].state === eventTypes.STATEMENT_WAITING) {
sweep(statement.scope);
}
- if(statement.type === 'logic'){
- //sweep if's condition
- console.log('hi')
- }
if(statement.rhs) {
_.each(statement.rhs.dependsOn, function(dependency) {
if(execState[dependency.id].state === eventTypes.STATEMENT_WAITING) {
@@ -464,7 +460,8 @@ Engine.prototype.execute = function() {
}
}
else if(results === null || results === undefined
- || results.body === null || results.body === undefined){
+ || results.body === null || results.body === undefined
+ || todo.type === 'logic' && results === false){
var fallback = statement.rhs ? statement.rhs.fallback : statement.fallback;
if(fallback) {
fallback.fbhold = false;
@@ -17,6 +17,7 @@
'use strict';
var _ = require('underscore'),
+ logic = require('./logic.js'),
assert = require('assert');
exports.exec = function(opts, statement, parentEvent, cb) {
@@ -32,7 +33,7 @@ exports.exec = function(opts, statement, parentEvent, cb) {
line: statement.line
},
cb: cb}),
- mycondition = true;//logic.exec(statement.condition, context),
+ mycondition = logic.findResult(statement.condition),
toskip, toexec;
if(mycondition) {
toskip = statement.else;
@@ -24,7 +24,18 @@ exports.exec = function(opts, statement, parentEvent, cb) {
line: statement.line
},
cb: cb});
- logicTx.cb(statement.result);
+ logicTx.cb(null, statement.result);
+}
+
+exports.findResult = function(statement) {
+ var sofar = statement;
+ while(sofar){
+ if(sofar.result){
+ return sofar.result;
+ }
+ sofar = sofar.fallback;
+ }
+ return false;
}
function calculate(condition, context){
@@ -33,10 +44,6 @@ function calculate(condition, context){
assert.ok(condition.values, 'Condition is in unexpected form. Need to have values field.');
assert.ok(context, 'context is missing.')
switch(condition.logic){
- case 'or':
- return _.any(condition.values, function(onecond){
- return calculate(onecond, context);
- });
case 'and':
return _.all(condition.values, function(onecond){
return calculate(onecond, context);
@@ -35,7 +35,7 @@ exports.exec = function(opts, statement, parentEvent, cb) {
},
cb: cb})
var results = _.map(statement.catchClause, function(onecatch){
- return logic.exec(onecatch.condition, context);
+ return logic.findResult(onecatch.condition);
});
tryTx.cb(null, results);
@@ -19,7 +19,6 @@
var Engine = require('../lib/engine');
var engine = new Engine();
-
module.exports['if'] = function(test) {
var context, q;
context = {
@@ -173,7 +172,8 @@ module.exports['trycatch-nested'] = function(test) {
catch (hello){\n\
a =select * from foo}\n\
catch (world){\n\
- c = select * from empty}\
+ c = select * from empty}\n\
+ throw(abc)\n\
}catch(abc){}\n\
return c || a || b';
engine.exec({script: q, context: context, cb: function(err, result) {

0 comments on commit ac9bfbd

Please sign in to comment.