pogo
Version:
A readable, DSL friendly programming language that compiles to JavaScript
82 lines • 4.31 kB
JavaScript
(function() {
var self = this;
var asyncControl;
asyncControl = require("../asyncControl");
module.exports = function(terms) {
var self = this;
var forExpressionTerm, forExpression;
forExpressionTerm = terms.term({
constructor: function(init, test, incr, stmts) {
var self = this;
self.isFor = true;
self.initialization = init;
self.test = test;
self.increment = incr;
self.indexVariable = init.target;
self.statements = stmts;
return self.statements = self._scopedBody();
},
_scopedBody: function() {
var self = this;
var containsReturn, forResultVariable, rewrittenStatements, loopStatements;
containsReturn = false;
forResultVariable = self.cg.generatedVariable([ "for", "result" ]);
rewrittenStatements = self.statements.rewrite({
rewrite: function(term) {
if (term.isReturn) {
containsReturn = true;
return terms.subStatements([ self.cg.definition(forResultVariable, term.expression, {
assignment: true
}), self.cg.returnStatement(self.cg.boolean(true)) ]);
}
},
limit: function(term, gen1_options) {
var path;
path = gen1_options !== void 0 && Object.prototype.hasOwnProperty.call(gen1_options, "path") && gen1_options.path !== void 0 ? gen1_options.path : path;
return term.isClosure;
}
}).serialiseAllStatements();
if (containsReturn) {
loopStatements = [];
loopStatements.push(self.cg.definition(forResultVariable, self.cg.nil()));
loopStatements.push(self.cg.ifExpression([ {
condition: self.cg.subExpression(self.cg.functionCall(self.cg.block([ self.indexVariable ], rewrittenStatements, {
returnLastStatement: false
}), [ self.indexVariable ])),
body: self.cg.statements([ self.cg.returnStatement(forResultVariable) ])
} ]));
return self.cg.asyncStatements(loopStatements);
} else {
return self.statements;
}
},
generate: function(scope) {
var self = this;
return self.code("for(", self.initialization.generate(scope), ";", self.test.generate(scope), ";", self.increment.generate(scope), "){", self.statements.generateStatements(scope), "}");
},
generateStatement: function() {
var self = this;
var args = Array.prototype.slice.call(arguments, 0, arguments.length);
var gen2_o;
gen2_o = self;
return gen2_o.generate.apply(gen2_o, args);
},
rewriteResultTermInto: function(returnTerm) {
var self = this;
return void 0;
}
});
return forExpression = function(init, test, incr, body) {
var initStatements, testStatements, incrStatements, asyncForFunction;
initStatements = terms.asyncStatements([ init ]);
testStatements = terms.asyncStatements([ test ]);
incrStatements = terms.asyncStatements([ incr ]);
if (initStatements.returnsPromise || testStatements.returnsPromise || (incrStatements.returnsPromise || body.returnsPromise)) {
asyncForFunction = terms.moduleConstants.defineAs([ "async", "for" ], terms.javascript(asyncControl.for.toString()));
return terms.scope([ init, terms.resolve(terms.functionCall(asyncForFunction, [ terms.closure([], testStatements), terms.closure([], incrStatements), terms.closure([], body) ]).alreadyPromise()) ]);
} else {
return forExpressionTerm(init, test, incr, body);
}
};
};
}).call(this);