Skip to content

Commit

Permalink
scope v2
Browse files Browse the repository at this point in the history
  • Loading branch information
hochang committed Oct 17, 2012
1 parent 976bdc9 commit ac9bfbd
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 14 deletions.
1 change: 1 addition & 0 deletions modules/compiler/lib/compiler.js
Expand Up @@ -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);
})
Expand Down
51 changes: 51 additions & 0 deletions modules/compiler/lib/peg/ql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions modules/compiler/pegs/ql.peg
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions modules/engine/lib/engine.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion modules/engine/lib/engine/ifelse.js
Expand Up @@ -17,6 +17,7 @@
'use strict';

var _ = require('underscore'),
logic = require('./logic.js'),
assert = require('assert');

exports.exec = function(opts, statement, parentEvent, cb) {
Expand All @@ -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;
Expand Down
17 changes: 12 additions & 5 deletions modules/engine/lib/engine/logic.js
Expand Up @@ -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){
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion modules/engine/lib/engine/trycatch.js
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions modules/engine/test/scope-test.js
Expand Up @@ -19,7 +19,6 @@
var Engine = require('../lib/engine');

var engine = new Engine();

module.exports['if'] = function(test) {
var context, q;
context = {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ac9bfbd

Please sign in to comment.