def compile(self): if (self.orelse == []): return helper.format( ''' var _i, _len, _ref, %(target)s; _ref = %(iter)s for (_i = 0; _len = _ref.__len__(); _i < _len; _i++) { %(target)s = _ref[_i]; %(body)s } ''', self ) else: return helper.format( ''' var _i, _len, _ref, %(target)s; _ref = %(iter)s if (_ref.__len__() > 0) { for (_i = 0; _len = _ref.__len__(); _i < _len; _i++) { %(target)s = _ref[_i]; %(body)s if (_i >= _len - 1) { %(orelse)s } } } else { %(orelse)s } ''', self )
def compile(self): keyElement = str(self.key) valueElement = str(self.value.compile()) iter = self.generators[0].iter isMultiTargeted = hasattr(self.generators[0].target, 'elts') is True if isMultiTargeted: ''' Multi-target for cases such as: print {x: y + x for x, y in [('hello', 5), ('world', 2)]} x, y are the targets print {x: y + z for x, y, z in [(5, 2, 7), (8, 9, 4)]} x, y, z are targets ''' targets = self.generators[0].target.elts numTargets = len(targets) for i in range(numTargets): mappedTarget = "x[%d]" % i keyElement = keyElement.replace(targets[i], mappedTarget) valueElement = valueElement.replace(targets[i], mappedTarget) return helper.format( ''' (function(){ var obj = {}; var translated = %(iter)s.map(function(x){ return [%(keyElement)s, %(valueElement)s]; }); for(var i = 0; i < translated.length; i++){ var key = translated[i][0]; obj[key] = translated[i][1]; } return obj; })() ''', self ) else: ''' Single target for cases such as: k = 4 print {x: x + k for x in [5, 67, 2]} x is the single target ''' target = self.generators[0].target return helper.format( ''' (function(){ var obj = {}; var translated = %(iter)s.map(function(%(target)s){ return [%(keyElement)s,%(valueElement)s]; }); for(var i = 0; i < translated.length; i++){ var key = translated[i][0]; obj[key] = translated[i][1]; } return obj; })() ''', self )
def compile(self): return helper.format( ''' function(%(args)s) { return %(body)s; } ''', self )
def compile(self): return helper.format( ''' (function(){ %(body)s }).call(this) ''', self )
def compile(self): return helper.format( ''' var %(name)s = function(%(args)s) { %(body)s } ''', self )
def compile(self): compiled = helper.format( ''' if (%(test)s) { %(body)s } ''', self ) # **Consideration** abstract list.isEmpty() ? if len(self.orelse) != 0: compiled = compiled + '\n' + helper.format( ''' else { %(orelse)s } ''', self ) return compiled
def compile(self): return helper.format( ''' try { %(body)s } finally { %(finalbody)s } ''', self )
def compile(self): if (self.bases == []): return helper.format( ''' var %(name)s; %(name)s = (function(){ %(body)s return %(name)s; })(); ''', self )
def compile(self): return helper.format( ''' if(%(test)s){', while(true){', %(body)s', if(!%(test)s){', %(orelse)s', break;', }', }' }', else{', %(orelse)s', }' ''', self )