Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

scope v2 partial

  • Loading branch information...
commit 976bdc9496e2ea563b4dd6ad258f17ba898a6168 1 parent f82b2f3
hochang authored
View
42 modules/compiler/lib/compiler.js
@@ -119,7 +119,6 @@ function plan(compiled) {
creates[line.id.toString()] = line;
}
else if (line.type === 'if') {
- //divscope(line.condition);
divescope(line.if, line);
if (line.else){
divescope(line.else, line);
@@ -305,12 +304,8 @@ function walk(line, symbols) {
}
break;
case 'if':
- dependency = symbols[line.condition];
- if(dependency) {
- addDep(line, line.dependsOn, dependency, symbols);
- walk(dependency, symbols);
- }
-
+ addDep(line, line.dependsOn, line.condition, symbols);
+ walk(line.condition, symbols);
_.each(line.if, function(ifline){
walk(ifline, symbols);
});
@@ -320,13 +315,24 @@ function walk(line, symbols) {
})
}
break;
+ case 'logic':
+ var condDepends = logicVars(line, symbols);
+ _.each(condDepends, function(dependency){
+ addDep(line, line.dependsOn, dependency, symbols);
+ walk(dependency, symbols);
+ });
+ if(line.fallback) {
+ walk(line.fallback, symbols);
+ }
+
+ break;
case 'try':
_.each(line.dependsOn, function(tryline){
addListener(tryline, line);
walk(tryline, symbols);
});
- _.each(line.catchClause, function(currentcatch, k){
- _.each(currentcatch, function(catchline){
+ _.each(line.catchClause, function(currentcatch){
+ _.each(currentcatch.lines, function(catchline){
walk(catchline, symbols);
})
});
@@ -573,3 +579,21 @@ function introspectWhere(line, symbols, parent) {
}
return line;
}
+
+// find all variables that appears in a logic condition.
+function logicVars(condition, symbols){
+ if(_.isString(condition.values)){
+ var conditionDep = symbols[condition.values];
+ if (conditionDep) {
+ return [conditionDep];
+ }else{
+ return [];
+ }
+ }
+ else if (!_.isArray(condition.values)){
+ return logicVars(condition.values, symbols);
+ }
+ return _.reduce(condition.values, function(memo, val){
+ return memo.concat(logicVars(val, symbols));
+ }, [])
+}
View
68 modules/compiler/lib/peg/ql.js
@@ -486,10 +486,10 @@ module.exports = (function(){
result5 = parse_insig();
if (result5 !== null) {
result6 = [];
- result7 = parse_ClauseCrlf();
+ result7 = parse_TryCrlf();
while (result7 !== null) {
result6.push(result7);
- result7 = parse_ClauseCrlf();
+ result7 = parse_TryCrlf();
}
if (result6 !== null) {
result7 = parse_insig();
@@ -603,10 +603,10 @@ module.exports = (function(){
result3 = parse_insig();
if (result3 !== null) {
result4 = [];
- result5 = parse_ClauseCrlf();
+ result5 = parse_TryCrlf();
while (result5 !== null) {
result4.push(result5);
- result5 = parse_ClauseCrlf();
+ result5 = parse_TryCrlf();
}
if (result4 !== null) {
result5 = parse_insig();
@@ -1206,25 +1206,22 @@ module.exports = (function(){
}
function parse_OrPhrase() {
- var result0, result1, result2, result3;
+ var result0, result1, result2;
var pos0, pos1;
pos0 = clone(pos);
pos1 = clone(pos);
result0 = parse_AndPhrase();
+ if (result0 === null) {
+ result0 = parse_NotPhrase();
+ if (result0 === null) {
+ result0 = parse_NormalPhrase();
+ }
+ }
if (result0 !== null) {
result1 = parse_insig();
if (result1 !== null) {
- result3 = parse_OrTail();
- if (result3 !== null) {
- result2 = [];
- while (result3 !== null) {
- result2.push(result3);
- result3 = parse_OrTail();
- }
- } else {
- result2 = null;
- }
+ result2 = parse_OrTail();
if (result2 !== null) {
result0 = [result0, result1, result2];
} else {
@@ -1241,10 +1238,10 @@ module.exports = (function(){
}
if (result0 !== null) {
result0 = (function(offset, line, column, s1, sn) {
- return {
- logic: 'or',
- values: [s1].concat(sn)
+ if(sn){
+ s1.fallback = sn;
}
+ return s1;
})(pos0.offset, pos0.line, pos0.column, result0[0], result0[2]);
}
if (result0 === null) {
@@ -1304,25 +1301,19 @@ module.exports = (function(){
}
function parse_AndPhrase() {
- var result0, result1, result2, result3;
+ var result0, result1, result2;
var pos0, pos1;
pos0 = clone(pos);
pos1 = clone(pos);
result0 = parse_NotPhrase();
+ if (result0 === null) {
+ result0 = parse_NormalPhrase();
+ }
if (result0 !== null) {
result1 = parse_insig();
if (result1 !== null) {
- result3 = parse_AndTail();
- if (result3 !== null) {
- result2 = [];
- while (result3 !== null) {
- result2.push(result3);
- result3 = parse_AndTail();
- }
- } else {
- result2 = null;
- }
+ result2 = parse_AndTail();
if (result2 !== null) {
result0 = [result0, result1, result2];
} else {
@@ -1340,7 +1331,10 @@ module.exports = (function(){
if (result0 !== null) {
result0 = (function(offset, line, column, s1, sn) {
return {
+ type: 'logic',
logic: 'and',
+ id : id++,
+ line : line,
values: [s1].concat(sn)
}
})(pos0.offset, pos0.line, pos0.column, result0[0], result0[2]);
@@ -1352,7 +1346,7 @@ module.exports = (function(){
}
function parse_AndTail() {
- var result0, result1, result2;
+ var result0, result1, result2, result3;
var pos0, pos1;
pos0 = clone(pos);
@@ -1371,7 +1365,13 @@ module.exports = (function(){
if (result1 !== null) {
result2 = parse_LogicStatement();
if (result2 !== null) {
- result0 = [result0, result1, result2];
+ result3 = parse_insig();
+ if (result3 !== null) {
+ result0 = [result0, result1, result2, result3];
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
} else {
result0 = null;
pos = clone(pos1);
@@ -1431,7 +1431,10 @@ module.exports = (function(){
if (result0 !== null) {
result0 = (function(offset, line, column, s) {
return {
+ type: 'logic',
logic : 'not',
+ id : id++,
+ line : line,
values : s
}
})(pos0.offset, pos0.line, pos0.column, result0[1]);
@@ -1451,7 +1454,10 @@ module.exports = (function(){
if (result0 !== null) {
result0 = (function(offset, line, column, w) {
return {
+ type: 'logic',
logic: 'normal',
+ id : id++,
+ line : line,
values: w
}
})(pos0.offset, pos0.line, pos0.column, result0);
View
25 modules/compiler/pegs/ql.peg
@@ -403,7 +403,7 @@ ClauseCrlf = sp s:(pClause / Line) crlf* {
pClause = IfClause / TryClause;
-IfClause = 'if' insig condition:LogicParen insig '{' insig iflines:ClauseCrlf* insig '}' insig elselines:ElseClause?{
+IfClause = 'if' insig condition:LogicParen insig '{' insig iflines:TryCrlf* insig '}' insig elselines:ElseClause?{
return {
id : id++,
line : line,
@@ -413,7 +413,7 @@ IfClause = 'if' insig condition:LogicParen insig '{' insig iflines:ClauseCrlf* i
else : elselines
}
}
-ElseClause = 'else' insig '{' insig larr:ClauseCrlf* insig'}' {
+ElseClause = 'else' insig '{' insig larr:TryCrlf* insig'}' {
return larr;
}
@@ -463,35 +463,44 @@ LogicParen = '(' insig p:LogicPhrase insig ')' insig {
}
LogicPhrase = OrPhrase / AndPhrase / NotPhrase / NormalPhrase;
-OrPhrase = s1:AndPhrase insig sn:OrTail+{
- return {
- logic: 'or',
- values: [s1].concat(sn)
+OrPhrase = s1:(AndPhrase / NotPhrase / NormalPhrase) insig sn:OrTail{
+ if(sn){
+ s1.fallback = sn;
}
+ return s1;
}
OrTail = '||' insig s:LogicStatement insig{
return s;
}
-AndPhrase = s1:NotPhrase insig sn:AndTail+{
+AndPhrase = s1:(NotPhrase / NormalPhrase) insig sn:AndTail{
return {
+ type: 'logic',
logic: 'and',
+ id : id++,
+ line : line,
values: [s1].concat(sn)
}
}
-AndTail = '&&' insig s:LogicStatement {
+AndTail = '&&' insig s:LogicStatement insig{
return s;
}
NotPhrase = '!' s:(LogicParen / NotPhrase / NormalPhrase) {
return {
+ type: 'logic',
logic : 'not',
+ id : id++,
+ line : line,
values : s
}
}
NormalPhrase = w:Word {
return {
+ type: 'logic',
logic: 'normal',
+ id : id++,
+ line : line,
values: w
}
}
View
8 modules/compiler/test/scope-test.js
@@ -27,11 +27,15 @@ module.exports = {
}\n\
finally {select * from bbb}";
var statement = compiler.compile(q);
-
+ test.equals(statement.rhs.dependsOn.length, 2);
+ test.equals(statement.rhs.catchClause.length, 1);
+ test.equals(statement.rhs.catchClause[0].condition.values, 'asdf');
+ test.equals(statement.rhs.catchClause[0].condition.logic, 'normal');
+ test.equals(statement.rhs.finallyClause.length, 1);
test.done();
},
'if else': function(test) {
- var q = "if (awef) {e = select * from f} else {g = select * from h}\n\
+ var q = "if (awef || wef && !jlk) {e = select * from f} else {g = select * from h}\n\
return e || g";
var statement = compiler.compile(q);
test.equals(statement.rhs.dependsOn.length, 1);
View
27 modules/engine/lib/engine.js
@@ -34,6 +34,7 @@ var configLoader = require('./engine/config.js'),
update = require('./engine/update.js'),
ifelse = require('./engine/ifelse.js'),
trycatch = require('./engine/trycatch.js'),
+ logic = require('./engine/logic.js'),
_util = require('./engine/util.js'),
jsonfill = require('./engine/jsonfill.js'),
eventTypes = require('./engine/event-types.js'),
@@ -292,19 +293,20 @@ Engine.prototype.execute = function() {
function scopeInit(statement) {
switch(statement.type){
case 'if':
- _.each(statement.ifClause, function(line){
+ init(statement.condition);
+ _.each(statement.if, function(line){
init(line);
});
- _.each(statement.elseClause, function(line){
+ _.each(statement.else, function(line){
init(line);
});
break;
case 'try':
- _.each(statement.tryClause, function(line){
+ _.each(statement.dependsOn, function(line){
init(line);
});
- _.each(statement.catchClause, function(currentcatch, k){
- _.each(currentcatch, function(line){
+ _.each(statement.catchClause, function(currentcatch){
+ _.each(currentcatch.lines, function(line){
init(line);
});
});
@@ -368,9 +370,13 @@ Engine.prototype.execute = function() {
var catchzip = _.zip(arg, statement.catchClause)
_.each(catchzip, function(mycatch) {
if(mycatch[0]){
- sweep(mycatch[1]);
+ _.each(mycatch[1].lines, function(line){
+ sweep(line);
+ });
}else{
- skipVar(mycatch[1]);
+ _.each(mycatch[1].lines, function(line){
+ skipVar(line);
+ });
}
});
break;
@@ -391,6 +397,10 @@ 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) {
@@ -720,6 +730,9 @@ function _execOne(opts, statement, parentEvent, cb) {
case 'throw':
trycatch.throw(opts, statement, parentEvent, cb);
break;
+ case 'logic':
+ logic.exec(opts, statement, parentEvent, cb);
+ break;
}
}
View
3  modules/engine/lib/engine/ifelse.js
@@ -17,7 +17,6 @@
'use strict';
var _ = require('underscore'),
- logic = require('./logic.js'),
assert = require('assert');
exports.exec = function(opts, statement, parentEvent, cb) {
@@ -33,7 +32,7 @@ exports.exec = function(opts, statement, parentEvent, cb) {
line: statement.line
},
cb: cb}),
- mycondition = logic.exec(statement.condition, context),
+ mycondition = true;//logic.exec(statement.condition, context),
toskip, toexec;
if(mycondition) {
toskip = statement.else;
View
19 modules/engine/lib/engine/logic.js
@@ -15,8 +15,19 @@
*/
var _ = require('underscore'),
assert = require('assert');
+exports.exec = function(opts, statement, parentEvent, cb) {
+ statement.result = calculate(statement, opts.context);
+ logicTx = opts.logEmitter.beginEvent({
+ parent: parentEvent,
+ type: 'logic',
+ message: {
+ line: statement.line
+ },
+ cb: cb});
+ logicTx.cb(statement.result);
+}
-exports.exec = function(condition, context){
+function calculate(condition, context){
assert.ok(condition, 'Condition is missing.');
assert.ok(condition.logic, 'Condition is in unexpected form. Need to have logic field.');
assert.ok(condition.values, 'Condition is in unexpected form. Need to have values field.');
@@ -24,14 +35,14 @@ exports.exec = function(condition, context){
switch(condition.logic){
case 'or':
return _.any(condition.values, function(onecond){
- return exec(onecond, context);
+ return calculate(onecond, context);
});
case 'and':
return _.all(condition.values, function(onecond){
- return exec(onecond, context);
+ return calculate(onecond, context);
});
case 'not':
- return !exec(condition.values, context);
+ return !calculate(condition.values, context);
default://normal
return !!context[condition.values];
}
View
191 modules/engine/test/scope-test.js
@@ -20,67 +20,70 @@ var Engine = require('../lib/engine');
var engine = new Engine();
-module.exports = {
- 'trycatch' : function(test) {
- var context, q;
- context = {
- foo : {
- 'hello' : 'Hello',
- 'world' : 'World'
- },
- bar : {
- 'chocolate' : 'Chocolate',
- 'milk' : 'Milk'
- },
- cond : 1
- };
- q = 'try {\n\
+module.exports['if'] = function(test) {
+ var context, q;
+ context = {
+ foo : {
+ 'hello' : 'Hello',
+ 'world' : 'World'
+ },
+ bar : {
+ 'chocolate' : 'Chocolate',
+ 'milk' : 'Milk'
+ },
+ zero : null
+ };
+ q = 'mycond = select * from foo\n\
+ empty = select * from zero\n\
+ if(empty || !!mycond){a = select * from foo}\n\
+ else {\n\
+ b = select * from bar}\n\
+ return "{a}" || "{b}"';
+ engine.exec({script: q, context: context, cb: function(err, result) {
+ if(err) {
+ test.fail('got error: ' + err.stack);
+ test.done();
+ }
+ else {
+ test.deepEqual(result.body, context.foo);
+ test.done();
+ }
+ }});
+};
+
+module.exports['trycatch'] = function(test) {
+ var context, q;
+ context = {
+ foo : {
+ 'hello' : 'Hello',
+ 'world' : 'World'
+ },
+ bar : {
+ 'chocolate' : 'Chocolate',
+ 'milk' : 'Milk'
+ }
+ };
+ q = 'try {\n\
b = select * from foo;\n\
throw (hello)}\n\
catch (hello){\n\
a =select * from foo}\n\
return a || b';
- engine.exec({script: q, context: context, cb: function(err, result) {
- if(err) {
- test.fail('got error: ' + err.stack);
- test.done();
- }
- else {
- test.deepEqual(result.body, context.foo);
- test.done();
- }
- }});
- },
- 'if': function(test) {
- var context, q;
- context = {
- foo : {
- 'hello' : 'Hello',
- 'world' : 'World'
- },
- bar : {
- 'chocolate' : 'Chocolate',
- 'milk' : 'Milk'
- },
- cond : 1
- };
- q = 'mycond = select * from cond\n\
- if(mycond){a = select * from foo}\n\
- else {\n\
- b = select * from bar}\n\
- return "{a}" || "{b}"';
- engine.exec({script: q, context: context, cb: function(err, result) {
- if(err) {
- test.fail('got error: ' + err.stack);
- test.done();
- }
- else {
- test.deepEqual(result.body, context.foo);
- test.done();
- }
- }});
- },
- 'else': function(test) {
+ engine.exec({script: q, context: context, cb: function(err, result) {
+ if(err) {
+ test.fail('got error: ' + err.stack);
+ test.done();
+ }
+ else {
+ test.deepEqual(result.body, context.foo);
+ test.done();
+ }
+ }});
+};
+
+
+
+module.exports['else'] = function(test) {
var context, q;
context = {
foo : {
@@ -108,5 +111,79 @@ module.exports = {
test.done();
}
}});
- }
+ };
+
+module.exports['trycatch-with-if'] = function(test) {
+ var context, q;
+ context = {
+ foo : {
+ 'hello' : 'Hello',
+ 'world' : 'World'
+ },
+ bar : {
+ 'chocolate' : 'Chocolate',
+ 'milk' : 'Milk'
+ },
+ empty : null,
+ cond : 1
+ };
+ q = 'try {\n\
+ b = select * from foo;\n\
+ throw (hello)\n\
+ if(d){\n\
+ throw (world)}\n\
+ }\n\
+ catch (hello){\n\
+ a =select * from foo}\n\
+ catch (world){\n\
+ c = select * from empty}\n\
+ return c || a || b';
+ engine.exec({script: q, context: context, cb: function(err, result) {
+ if(err) {
+ test.fail('got error: ' + err.stack);
+ test.done();
+ }
+ else {
+ test.deepEqual(result.body, context.foo);
+ test.done();
+ }
+ }});
+};
+
+module.exports['trycatch-nested'] = function(test) {
+ var context, q;
+ context = {
+ foo : {
+ 'hello' : 'Hello',
+ 'world' : 'World'
+ },
+ bar : {
+ 'chocolate' : 'Chocolate',
+ 'milk' : 'Milk'
+ },
+ empty : null,
+ cond : 1
+ };
+ q = 'try{try {\n\
+ b = select * from foo;\n\
+ throw (hello)\n\
+ if(d){\n\
+ throw (world)}\n\
+ }\n\
+ catch (hello){\n\
+ a =select * from foo}\n\
+ catch (world){\n\
+ c = select * from empty}\
+ }catch(abc){}\n\
+ return c || a || b';
+ engine.exec({script: q, context: context, cb: function(err, result) {
+ if(err) {
+ test.fail('got error: ' + err.stack);
+ test.done();
+ }
+ else {
+ test.deepEqual(result.body, context.foo);
+ test.done();
+ }
+ }});
};
Please sign in to comment.
Something went wrong with that request. Please try again.