nodes.js 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080
  1. /**
  2. * Copyright (c) 2009-2013 Jeremy Ashkenas
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use,
  8. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following
  11. * conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. define(function(require, exports, module) {
  27. // Generated by CoffeeScript 1.6.3
  28. var Access, Arr, Assign, Base, Block, Call, Class, Closure, Code, CodeFragment, Comment, Existence, Extends, For, IDENTIFIER, IDENTIFIER_STR, IS_STRING, If, In, Index, LEVEL_ACCESS, LEVEL_COND, LEVEL_LIST, LEVEL_OP, LEVEL_PAREN, LEVEL_TOP, Literal, METHOD_DEF, NEGATE, NO, Obj, Op, Param, Parens, RESERVED, Range, Return, SIMPLENUM, STRICT_PROSCRIBED, Scope, Slice, Splat, Switch, TAB, THIS, Throw, Try, UTILITIES, Value, While, YES, addLocationDataFn, compact, del, ends, extend, flatten, fragmentsToText, last, locationDataToString, merge, multident, some, starts, throwSyntaxError, unfoldSoak, utility, _ref, _ref1, _ref2, _ref3,
  29. __hasProp = {}.hasOwnProperty,
  30. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  31. __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
  32. __slice = [].slice;
  33. Error.stackTraceLimit = Infinity;
  34. Scope = require('./scope').Scope;
  35. _ref = require('./lexer'), RESERVED = _ref.RESERVED, STRICT_PROSCRIBED = _ref.STRICT_PROSCRIBED;
  36. _ref1 = require('./helpers'), compact = _ref1.compact, flatten = _ref1.flatten, extend = _ref1.extend, merge = _ref1.merge, del = _ref1.del, starts = _ref1.starts, ends = _ref1.ends, last = _ref1.last, some = _ref1.some, addLocationDataFn = _ref1.addLocationDataFn, locationDataToString = _ref1.locationDataToString, throwSyntaxError = _ref1.throwSyntaxError;
  37. exports.extend = extend;
  38. exports.addLocationDataFn = addLocationDataFn;
  39. YES = function() {
  40. return true;
  41. };
  42. NO = function() {
  43. return false;
  44. };
  45. THIS = function() {
  46. return this;
  47. };
  48. NEGATE = function() {
  49. this.negated = !this.negated;
  50. return this;
  51. };
  52. exports.CodeFragment = CodeFragment = (function() {
  53. function CodeFragment(parent, code) {
  54. var _ref2;
  55. this.code = "" + code;
  56. this.locationData = parent != null ? parent.locationData : void 0;
  57. this.type = (parent != null ? (_ref2 = parent.constructor) != null ? _ref2.name : void 0 : void 0) || 'unknown';
  58. }
  59. CodeFragment.prototype.toString = function() {
  60. return "" + this.code + (this.locationData ? ": " + locationDataToString(this.locationData) : '');
  61. };
  62. return CodeFragment;
  63. })();
  64. fragmentsToText = function(fragments) {
  65. var fragment;
  66. return ((function() {
  67. var _i, _len, _results;
  68. _results = [];
  69. for (_i = 0, _len = fragments.length; _i < _len; _i++) {
  70. fragment = fragments[_i];
  71. _results.push(fragment.code);
  72. }
  73. return _results;
  74. })()).join('');
  75. };
  76. exports.Base = Base = (function() {
  77. function Base() {}
  78. Base.prototype.compile = function(o, lvl) {
  79. return fragmentsToText(this.compileToFragments(o, lvl));
  80. };
  81. Base.prototype.compileToFragments = function(o, lvl) {
  82. var node;
  83. o = extend({}, o);
  84. if (lvl) {
  85. o.level = lvl;
  86. }
  87. node = this.unfoldSoak(o) || this;
  88. node.tab = o.indent;
  89. if (o.level === LEVEL_TOP || !node.isStatement(o)) {
  90. return node.compileNode(o);
  91. } else {
  92. return node.compileClosure(o);
  93. }
  94. };
  95. Base.prototype.compileClosure = function(o) {
  96. var jumpNode;
  97. if (jumpNode = this.jumps()) {
  98. jumpNode.error('cannot use a pure statement in an expression');
  99. }
  100. o.sharedScope = true;
  101. return Closure.wrap(this).compileNode(o);
  102. };
  103. Base.prototype.cache = function(o, level, reused) {
  104. var ref, sub;
  105. if (!this.isComplex()) {
  106. ref = level ? this.compileToFragments(o, level) : this;
  107. return [ref, ref];
  108. } else {
  109. ref = new Literal(reused || o.scope.freeVariable('ref'));
  110. sub = new Assign(ref, this);
  111. if (level) {
  112. return [sub.compileToFragments(o, level), [this.makeCode(ref.value)]];
  113. } else {
  114. return [sub, ref];
  115. }
  116. }
  117. };
  118. Base.prototype.cacheToCodeFragments = function(cacheValues) {
  119. return [fragmentsToText(cacheValues[0]), fragmentsToText(cacheValues[1])];
  120. };
  121. Base.prototype.makeReturn = function(res) {
  122. var me;
  123. me = this.unwrapAll();
  124. if (res) {
  125. return new Call(new Literal("" + res + ".push"), [me]);
  126. } else {
  127. return new Return(me);
  128. }
  129. };
  130. Base.prototype.contains = function(pred) {
  131. var node;
  132. node = void 0;
  133. this.traverseChildren(false, function(n) {
  134. if (pred(n)) {
  135. node = n;
  136. return false;
  137. }
  138. });
  139. return node;
  140. };
  141. Base.prototype.lastNonComment = function(list) {
  142. var i;
  143. i = list.length;
  144. while (i--) {
  145. if (!(list[i] instanceof Comment)) {
  146. return list[i];
  147. }
  148. }
  149. return null;
  150. };
  151. Base.prototype.toString = function(idt, name) {
  152. var tree;
  153. if (idt == null) {
  154. idt = '';
  155. }
  156. if (name == null) {
  157. name = this.constructor.name;
  158. }
  159. tree = '\n' + idt + name;
  160. if (this.soak) {
  161. tree += '?';
  162. }
  163. this.eachChild(function(node) {
  164. return tree += node.toString(idt + TAB);
  165. });
  166. return tree;
  167. };
  168. Base.prototype.eachChild = function(func) {
  169. var attr, child, _i, _j, _len, _len1, _ref2, _ref3;
  170. if (!this.children) {
  171. return this;
  172. }
  173. _ref2 = this.children;
  174. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  175. attr = _ref2[_i];
  176. if (this[attr]) {
  177. _ref3 = flatten([this[attr]]);
  178. for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) {
  179. child = _ref3[_j];
  180. if (func(child) === false) {
  181. return this;
  182. }
  183. }
  184. }
  185. }
  186. return this;
  187. };
  188. Base.prototype.traverseChildren = function(crossScope, func) {
  189. return this.eachChild(function(child) {
  190. var recur;
  191. recur = func(child);
  192. if (recur !== false) {
  193. return child.traverseChildren(crossScope, func);
  194. }
  195. });
  196. };
  197. Base.prototype.invert = function() {
  198. return new Op('!', this);
  199. };
  200. Base.prototype.unwrapAll = function() {
  201. var node;
  202. node = this;
  203. while (node !== (node = node.unwrap())) {
  204. continue;
  205. }
  206. return node;
  207. };
  208. Base.prototype.children = [];
  209. Base.prototype.isStatement = NO;
  210. Base.prototype.jumps = NO;
  211. Base.prototype.isComplex = YES;
  212. Base.prototype.isChainable = NO;
  213. Base.prototype.isAssignable = NO;
  214. Base.prototype.unwrap = THIS;
  215. Base.prototype.unfoldSoak = NO;
  216. Base.prototype.assigns = NO;
  217. Base.prototype.updateLocationDataIfMissing = function(locationData) {
  218. if (this.locationData) {
  219. return this;
  220. }
  221. this.locationData = locationData;
  222. return this.eachChild(function(child) {
  223. return child.updateLocationDataIfMissing(locationData);
  224. });
  225. };
  226. Base.prototype.error = function(message) {
  227. return throwSyntaxError(message, this.locationData);
  228. };
  229. Base.prototype.makeCode = function(code) {
  230. return new CodeFragment(this, code);
  231. };
  232. Base.prototype.wrapInBraces = function(fragments) {
  233. return [].concat(this.makeCode('('), fragments, this.makeCode(')'));
  234. };
  235. Base.prototype.joinFragmentArrays = function(fragmentsList, joinStr) {
  236. var answer, fragments, i, _i, _len;
  237. answer = [];
  238. for (i = _i = 0, _len = fragmentsList.length; _i < _len; i = ++_i) {
  239. fragments = fragmentsList[i];
  240. if (i) {
  241. answer.push(this.makeCode(joinStr));
  242. }
  243. answer = answer.concat(fragments);
  244. }
  245. return answer;
  246. };
  247. return Base;
  248. })();
  249. exports.Block = Block = (function(_super) {
  250. __extends(Block, _super);
  251. function Block(nodes) {
  252. this.expressions = compact(flatten(nodes || []));
  253. }
  254. Block.prototype.children = ['expressions'];
  255. Block.prototype.push = function(node) {
  256. this.expressions.push(node);
  257. return this;
  258. };
  259. Block.prototype.pop = function() {
  260. return this.expressions.pop();
  261. };
  262. Block.prototype.unshift = function(node) {
  263. this.expressions.unshift(node);
  264. return this;
  265. };
  266. Block.prototype.unwrap = function() {
  267. if (this.expressions.length === 1) {
  268. return this.expressions[0];
  269. } else {
  270. return this;
  271. }
  272. };
  273. Block.prototype.isEmpty = function() {
  274. return !this.expressions.length;
  275. };
  276. Block.prototype.isStatement = function(o) {
  277. var exp, _i, _len, _ref2;
  278. _ref2 = this.expressions;
  279. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  280. exp = _ref2[_i];
  281. if (exp.isStatement(o)) {
  282. return true;
  283. }
  284. }
  285. return false;
  286. };
  287. Block.prototype.jumps = function(o) {
  288. var exp, _i, _len, _ref2;
  289. _ref2 = this.expressions;
  290. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  291. exp = _ref2[_i];
  292. if (exp.jumps(o)) {
  293. return exp;
  294. }
  295. }
  296. };
  297. Block.prototype.makeReturn = function(res) {
  298. var expr, len;
  299. len = this.expressions.length;
  300. while (len--) {
  301. expr = this.expressions[len];
  302. if (!(expr instanceof Comment)) {
  303. this.expressions[len] = expr.makeReturn(res);
  304. if (expr instanceof Return && !expr.expression) {
  305. this.expressions.splice(len, 1);
  306. }
  307. break;
  308. }
  309. }
  310. return this;
  311. };
  312. Block.prototype.compileToFragments = function(o, level) {
  313. if (o == null) {
  314. o = {};
  315. }
  316. if (o.scope) {
  317. return Block.__super__.compileToFragments.call(this, o, level);
  318. } else {
  319. return this.compileRoot(o);
  320. }
  321. };
  322. Block.prototype.compileNode = function(o) {
  323. var answer, compiledNodes, fragments, index, node, top, _i, _len, _ref2;
  324. this.tab = o.indent;
  325. top = o.level === LEVEL_TOP;
  326. compiledNodes = [];
  327. _ref2 = this.expressions;
  328. for (index = _i = 0, _len = _ref2.length; _i < _len; index = ++_i) {
  329. node = _ref2[index];
  330. node = node.unwrapAll();
  331. node = node.unfoldSoak(o) || node;
  332. if (node instanceof Block) {
  333. compiledNodes.push(node.compileNode(o));
  334. } else if (top) {
  335. node.front = true;
  336. fragments = node.compileToFragments(o);
  337. if (!node.isStatement(o)) {
  338. fragments.unshift(this.makeCode("" + this.tab));
  339. fragments.push(this.makeCode(";"));
  340. }
  341. compiledNodes.push(fragments);
  342. } else {
  343. compiledNodes.push(node.compileToFragments(o, LEVEL_LIST));
  344. }
  345. }
  346. if (top) {
  347. if (this.spaced) {
  348. return [].concat(this.joinFragmentArrays(compiledNodes, '\n\n'), this.makeCode("\n"));
  349. } else {
  350. return this.joinFragmentArrays(compiledNodes, '\n');
  351. }
  352. }
  353. if (compiledNodes.length) {
  354. answer = this.joinFragmentArrays(compiledNodes, ', ');
  355. } else {
  356. answer = [this.makeCode("void 0")];
  357. }
  358. if (compiledNodes.length > 1 && o.level >= LEVEL_LIST) {
  359. return this.wrapInBraces(answer);
  360. } else {
  361. return answer;
  362. }
  363. };
  364. Block.prototype.compileRoot = function(o) {
  365. var exp, fragments, i, name, prelude, preludeExps, rest, _i, _len, _ref2;
  366. o.indent = o.bare ? '' : TAB;
  367. o.level = LEVEL_TOP;
  368. this.spaced = true;
  369. o.scope = new Scope(null, this, null);
  370. _ref2 = o.locals || [];
  371. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  372. name = _ref2[_i];
  373. o.scope.parameter(name);
  374. }
  375. prelude = [];
  376. if (!o.bare) {
  377. preludeExps = (function() {
  378. var _j, _len1, _ref3, _results;
  379. _ref3 = this.expressions;
  380. _results = [];
  381. for (i = _j = 0, _len1 = _ref3.length; _j < _len1; i = ++_j) {
  382. exp = _ref3[i];
  383. if (!(exp.unwrap() instanceof Comment)) {
  384. break;
  385. }
  386. _results.push(exp);
  387. }
  388. return _results;
  389. }).call(this);
  390. rest = this.expressions.slice(preludeExps.length);
  391. this.expressions = preludeExps;
  392. if (preludeExps.length) {
  393. prelude = this.compileNode(merge(o, {
  394. indent: ''
  395. }));
  396. prelude.push(this.makeCode("\n"));
  397. }
  398. this.expressions = rest;
  399. }
  400. fragments = this.compileWithDeclarations(o);
  401. if (o.bare) {
  402. return fragments;
  403. }
  404. return [].concat(prelude, this.makeCode("(function() {\n"), fragments, this.makeCode("\n}).call(this);\n"));
  405. };
  406. Block.prototype.compileWithDeclarations = function(o) {
  407. var assigns, declars, exp, fragments, i, post, rest, scope, spaced, _i, _len, _ref2, _ref3, _ref4;
  408. fragments = [];
  409. post = [];
  410. _ref2 = this.expressions;
  411. for (i = _i = 0, _len = _ref2.length; _i < _len; i = ++_i) {
  412. exp = _ref2[i];
  413. exp = exp.unwrap();
  414. if (!(exp instanceof Comment || exp instanceof Literal)) {
  415. break;
  416. }
  417. }
  418. o = merge(o, {
  419. level: LEVEL_TOP
  420. });
  421. if (i) {
  422. rest = this.expressions.splice(i, 9e9);
  423. _ref3 = [this.spaced, false], spaced = _ref3[0], this.spaced = _ref3[1];
  424. _ref4 = [this.compileNode(o), spaced], fragments = _ref4[0], this.spaced = _ref4[1];
  425. this.expressions = rest;
  426. }
  427. post = this.compileNode(o);
  428. scope = o.scope;
  429. if (scope.expressions === this) {
  430. declars = o.scope.hasDeclarations();
  431. assigns = scope.hasAssignments;
  432. if (declars || assigns) {
  433. if (i) {
  434. fragments.push(this.makeCode('\n'));
  435. }
  436. fragments.push(this.makeCode("" + this.tab + "var "));
  437. if (declars) {
  438. fragments.push(this.makeCode(scope.declaredVariables().join(', ')));
  439. }
  440. if (assigns) {
  441. if (declars) {
  442. fragments.push(this.makeCode(",\n" + (this.tab + TAB)));
  443. }
  444. fragments.push(this.makeCode(scope.assignedVariables().join(",\n" + (this.tab + TAB))));
  445. }
  446. fragments.push(this.makeCode(";\n" + (this.spaced ? '\n' : '')));
  447. } else if (fragments.length && post.length) {
  448. fragments.push(this.makeCode("\n"));
  449. }
  450. }
  451. return fragments.concat(post);
  452. };
  453. Block.wrap = function(nodes) {
  454. if (nodes.length === 1 && nodes[0] instanceof Block) {
  455. return nodes[0];
  456. }
  457. return new Block(nodes);
  458. };
  459. return Block;
  460. })(Base);
  461. exports.Literal = Literal = (function(_super) {
  462. __extends(Literal, _super);
  463. function Literal(value) {
  464. this.value = value;
  465. }
  466. Literal.prototype.makeReturn = function() {
  467. if (this.isStatement()) {
  468. return this;
  469. } else {
  470. return Literal.__super__.makeReturn.apply(this, arguments);
  471. }
  472. };
  473. Literal.prototype.isAssignable = function() {
  474. return IDENTIFIER.test(this.value);
  475. };
  476. Literal.prototype.isStatement = function() {
  477. var _ref2;
  478. return (_ref2 = this.value) === 'break' || _ref2 === 'continue' || _ref2 === 'debugger';
  479. };
  480. Literal.prototype.isComplex = NO;
  481. Literal.prototype.assigns = function(name) {
  482. return name === this.value;
  483. };
  484. Literal.prototype.jumps = function(o) {
  485. if (this.value === 'break' && !((o != null ? o.loop : void 0) || (o != null ? o.block : void 0))) {
  486. return this;
  487. }
  488. if (this.value === 'continue' && !(o != null ? o.loop : void 0)) {
  489. return this;
  490. }
  491. };
  492. Literal.prototype.compileNode = function(o) {
  493. var answer, code, _ref2;
  494. code = this.value === 'this' ? ((_ref2 = o.scope.method) != null ? _ref2.bound : void 0) ? o.scope.method.context : this.value : this.value.reserved ? "\"" + this.value + "\"" : this.value;
  495. answer = this.isStatement() ? "" + this.tab + code + ";" : code;
  496. return [this.makeCode(answer)];
  497. };
  498. Literal.prototype.toString = function() {
  499. return ' "' + this.value + '"';
  500. };
  501. return Literal;
  502. })(Base);
  503. exports.Undefined = (function(_super) {
  504. __extends(Undefined, _super);
  505. function Undefined() {
  506. _ref2 = Undefined.__super__.constructor.apply(this, arguments);
  507. return _ref2;
  508. }
  509. Undefined.prototype.isAssignable = NO;
  510. Undefined.prototype.isComplex = NO;
  511. Undefined.prototype.compileNode = function(o) {
  512. return [this.makeCode(o.level >= LEVEL_ACCESS ? '(void 0)' : 'void 0')];
  513. };
  514. return Undefined;
  515. })(Base);
  516. exports.Null = (function(_super) {
  517. __extends(Null, _super);
  518. function Null() {
  519. _ref3 = Null.__super__.constructor.apply(this, arguments);
  520. return _ref3;
  521. }
  522. Null.prototype.isAssignable = NO;
  523. Null.prototype.isComplex = NO;
  524. Null.prototype.compileNode = function() {
  525. return [this.makeCode("null")];
  526. };
  527. return Null;
  528. })(Base);
  529. exports.Bool = (function(_super) {
  530. __extends(Bool, _super);
  531. Bool.prototype.isAssignable = NO;
  532. Bool.prototype.isComplex = NO;
  533. Bool.prototype.compileNode = function() {
  534. return [this.makeCode(this.val)];
  535. };
  536. function Bool(val) {
  537. this.val = val;
  538. }
  539. return Bool;
  540. })(Base);
  541. exports.Return = Return = (function(_super) {
  542. __extends(Return, _super);
  543. function Return(expr) {
  544. if (expr && !expr.unwrap().isUndefined) {
  545. this.expression = expr;
  546. }
  547. }
  548. Return.prototype.children = ['expression'];
  549. Return.prototype.isStatement = YES;
  550. Return.prototype.makeReturn = THIS;
  551. Return.prototype.jumps = THIS;
  552. Return.prototype.compileToFragments = function(o, level) {
  553. var expr, _ref4;
  554. expr = (_ref4 = this.expression) != null ? _ref4.makeReturn() : void 0;
  555. if (expr && !(expr instanceof Return)) {
  556. return expr.compileToFragments(o, level);
  557. } else {
  558. return Return.__super__.compileToFragments.call(this, o, level);
  559. }
  560. };
  561. Return.prototype.compileNode = function(o) {
  562. var answer;
  563. answer = [];
  564. answer.push(this.makeCode(this.tab + ("return" + (this.expression ? " " : ""))));
  565. if (this.expression) {
  566. answer = answer.concat(this.expression.compileToFragments(o, LEVEL_PAREN));
  567. }
  568. answer.push(this.makeCode(";"));
  569. return answer;
  570. };
  571. return Return;
  572. })(Base);
  573. exports.Value = Value = (function(_super) {
  574. __extends(Value, _super);
  575. function Value(base, props, tag) {
  576. if (!props && base instanceof Value) {
  577. return base;
  578. }
  579. this.base = base;
  580. this.properties = props || [];
  581. if (tag) {
  582. this[tag] = true;
  583. }
  584. return this;
  585. }
  586. Value.prototype.children = ['base', 'properties'];
  587. Value.prototype.add = function(props) {
  588. this.properties = this.properties.concat(props);
  589. return this;
  590. };
  591. Value.prototype.hasProperties = function() {
  592. return !!this.properties.length;
  593. };
  594. Value.prototype.isArray = function() {
  595. return !this.properties.length && this.base instanceof Arr;
  596. };
  597. Value.prototype.isComplex = function() {
  598. return this.hasProperties() || this.base.isComplex();
  599. };
  600. Value.prototype.isAssignable = function() {
  601. return this.hasProperties() || this.base.isAssignable();
  602. };
  603. Value.prototype.isSimpleNumber = function() {
  604. return this.base instanceof Literal && SIMPLENUM.test(this.base.value);
  605. };
  606. Value.prototype.isString = function() {
  607. return this.base instanceof Literal && IS_STRING.test(this.base.value);
  608. };
  609. Value.prototype.isAtomic = function() {
  610. var node, _i, _len, _ref4;
  611. _ref4 = this.properties.concat(this.base);
  612. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  613. node = _ref4[_i];
  614. if (node.soak || node instanceof Call) {
  615. return false;
  616. }
  617. }
  618. return true;
  619. };
  620. Value.prototype.isStatement = function(o) {
  621. return !this.properties.length && this.base.isStatement(o);
  622. };
  623. Value.prototype.assigns = function(name) {
  624. return !this.properties.length && this.base.assigns(name);
  625. };
  626. Value.prototype.jumps = function(o) {
  627. return !this.properties.length && this.base.jumps(o);
  628. };
  629. Value.prototype.isObject = function(onlyGenerated) {
  630. if (this.properties.length) {
  631. return false;
  632. }
  633. return (this.base instanceof Obj) && (!onlyGenerated || this.base.generated);
  634. };
  635. Value.prototype.isSplice = function() {
  636. return last(this.properties) instanceof Slice;
  637. };
  638. Value.prototype.unwrap = function() {
  639. if (this.properties.length) {
  640. return this;
  641. } else {
  642. return this.base;
  643. }
  644. };
  645. Value.prototype.cacheReference = function(o) {
  646. var base, bref, name, nref;
  647. name = last(this.properties);
  648. if (this.properties.length < 2 && !this.base.isComplex() && !(name != null ? name.isComplex() : void 0)) {
  649. return [this, this];
  650. }
  651. base = new Value(this.base, this.properties.slice(0, -1));
  652. if (base.isComplex()) {
  653. bref = new Literal(o.scope.freeVariable('base'));
  654. base = new Value(new Parens(new Assign(bref, base)));
  655. }
  656. if (!name) {
  657. return [base, bref];
  658. }
  659. if (name.isComplex()) {
  660. nref = new Literal(o.scope.freeVariable('name'));
  661. name = new Index(new Assign(nref, name.index));
  662. nref = new Index(nref);
  663. }
  664. return [base.add(name), new Value(bref || base.base, [nref || name])];
  665. };
  666. Value.prototype.compileNode = function(o) {
  667. var fragments, prop, props, _i, _len;
  668. this.base.front = this.front;
  669. props = this.properties;
  670. fragments = this.base.compileToFragments(o, (props.length ? LEVEL_ACCESS : null));
  671. if ((this.base instanceof Parens || props.length) && SIMPLENUM.test(fragmentsToText(fragments))) {
  672. fragments.push(this.makeCode('.'));
  673. }
  674. for (_i = 0, _len = props.length; _i < _len; _i++) {
  675. prop = props[_i];
  676. fragments.push.apply(fragments, prop.compileToFragments(o));
  677. }
  678. return fragments;
  679. };
  680. Value.prototype.unfoldSoak = function(o) {
  681. var _this = this;
  682. return this.unfoldedSoak != null ? this.unfoldedSoak : this.unfoldedSoak = (function() {
  683. var fst, i, ifn, prop, ref, snd, _i, _len, _ref4, _ref5;
  684. if (ifn = _this.base.unfoldSoak(o)) {
  685. (_ref4 = ifn.body.properties).push.apply(_ref4, _this.properties);
  686. return ifn;
  687. }
  688. _ref5 = _this.properties;
  689. for (i = _i = 0, _len = _ref5.length; _i < _len; i = ++_i) {
  690. prop = _ref5[i];
  691. if (!prop.soak) {
  692. continue;
  693. }
  694. prop.soak = false;
  695. fst = new Value(_this.base, _this.properties.slice(0, i));
  696. snd = new Value(_this.base, _this.properties.slice(i));
  697. if (fst.isComplex()) {
  698. ref = new Literal(o.scope.freeVariable('ref'));
  699. fst = new Parens(new Assign(ref, fst));
  700. snd.base = ref;
  701. }
  702. return new If(new Existence(fst), snd, {
  703. soak: true
  704. });
  705. }
  706. return false;
  707. })();
  708. };
  709. return Value;
  710. })(Base);
  711. exports.Comment = Comment = (function(_super) {
  712. __extends(Comment, _super);
  713. function Comment(comment) {
  714. this.comment = comment;
  715. }
  716. Comment.prototype.isStatement = YES;
  717. Comment.prototype.makeReturn = THIS;
  718. Comment.prototype.compileNode = function(o, level) {
  719. var code;
  720. code = "/*" + (multident(this.comment, this.tab)) + (__indexOf.call(this.comment, '\n') >= 0 ? "\n" + this.tab : '') + "*/";
  721. if ((level || o.level) === LEVEL_TOP) {
  722. code = o.indent + code;
  723. }
  724. return [this.makeCode("\n"), this.makeCode(code)];
  725. };
  726. return Comment;
  727. })(Base);
  728. exports.Call = Call = (function(_super) {
  729. __extends(Call, _super);
  730. function Call(variable, args, soak) {
  731. this.args = args != null ? args : [];
  732. this.soak = soak;
  733. this.isNew = false;
  734. this.isSuper = variable === 'super';
  735. this.variable = this.isSuper ? null : variable;
  736. }
  737. Call.prototype.children = ['variable', 'args'];
  738. Call.prototype.newInstance = function() {
  739. var base, _ref4;
  740. base = ((_ref4 = this.variable) != null ? _ref4.base : void 0) || this.variable;
  741. if (base instanceof Call && !base.isNew) {
  742. base.newInstance();
  743. } else {
  744. this.isNew = true;
  745. }
  746. return this;
  747. };
  748. Call.prototype.superReference = function(o) {
  749. var accesses, method;
  750. method = o.scope.namedMethod();
  751. if (method != null ? method.klass : void 0) {
  752. accesses = [new Access(new Literal('__super__'))];
  753. if (method["static"]) {
  754. accesses.push(new Access(new Literal('constructor')));
  755. }
  756. accesses.push(new Access(new Literal(method.name)));
  757. return (new Value(new Literal(method.klass), accesses)).compile(o);
  758. } else if (method != null ? method.ctor : void 0) {
  759. return "" + method.name + ".__super__.constructor";
  760. } else {
  761. return this.error('cannot call super outside of an instance method.');
  762. }
  763. };
  764. Call.prototype.superThis = function(o) {
  765. var method;
  766. method = o.scope.method;
  767. return (method && !method.klass && method.context) || "this";
  768. };
  769. Call.prototype.unfoldSoak = function(o) {
  770. var call, ifn, left, list, rite, _i, _len, _ref4, _ref5;
  771. if (this.soak) {
  772. if (this.variable) {
  773. if (ifn = unfoldSoak(o, this, 'variable')) {
  774. return ifn;
  775. }
  776. _ref4 = new Value(this.variable).cacheReference(o), left = _ref4[0], rite = _ref4[1];
  777. } else {
  778. left = new Literal(this.superReference(o));
  779. rite = new Value(left);
  780. }
  781. rite = new Call(rite, this.args);
  782. rite.isNew = this.isNew;
  783. left = new Literal("typeof " + (left.compile(o)) + " === \"function\"");
  784. return new If(left, new Value(rite), {
  785. soak: true
  786. });
  787. }
  788. call = this;
  789. list = [];
  790. while (true) {
  791. if (call.variable instanceof Call) {
  792. list.push(call);
  793. call = call.variable;
  794. continue;
  795. }
  796. if (!(call.variable instanceof Value)) {
  797. break;
  798. }
  799. list.push(call);
  800. if (!((call = call.variable.base) instanceof Call)) {
  801. break;
  802. }
  803. }
  804. _ref5 = list.reverse();
  805. for (_i = 0, _len = _ref5.length; _i < _len; _i++) {
  806. call = _ref5[_i];
  807. if (ifn) {
  808. if (call.variable instanceof Call) {
  809. call.variable = ifn;
  810. } else {
  811. call.variable.base = ifn;
  812. }
  813. }
  814. ifn = unfoldSoak(o, call, 'variable');
  815. }
  816. return ifn;
  817. };
  818. Call.prototype.compileNode = function(o) {
  819. var arg, argIndex, compiledArgs, compiledArray, fragments, preface, _i, _len, _ref4, _ref5;
  820. if ((_ref4 = this.variable) != null) {
  821. _ref4.front = this.front;
  822. }
  823. compiledArray = Splat.compileSplattedArray(o, this.args, true);
  824. if (compiledArray.length) {
  825. return this.compileSplat(o, compiledArray);
  826. }
  827. compiledArgs = [];
  828. _ref5 = this.args;
  829. for (argIndex = _i = 0, _len = _ref5.length; _i < _len; argIndex = ++_i) {
  830. arg = _ref5[argIndex];
  831. if (argIndex) {
  832. compiledArgs.push(this.makeCode(", "));
  833. }
  834. compiledArgs.push.apply(compiledArgs, arg.compileToFragments(o, LEVEL_LIST));
  835. }
  836. fragments = [];
  837. if (this.isSuper) {
  838. preface = this.superReference(o) + (".call(" + (this.superThis(o)));
  839. if (compiledArgs.length) {
  840. preface += ", ";
  841. }
  842. fragments.push(this.makeCode(preface));
  843. } else {
  844. if (this.isNew) {
  845. fragments.push(this.makeCode('new '));
  846. }
  847. fragments.push.apply(fragments, this.variable.compileToFragments(o, LEVEL_ACCESS));
  848. fragments.push(this.makeCode("("));
  849. }
  850. fragments.push.apply(fragments, compiledArgs);
  851. fragments.push(this.makeCode(")"));
  852. return fragments;
  853. };
  854. Call.prototype.compileSplat = function(o, splatArgs) {
  855. var answer, base, fun, idt, name, ref;
  856. if (this.isSuper) {
  857. return [].concat(this.makeCode("" + (this.superReference(o)) + ".apply(" + (this.superThis(o)) + ", "), splatArgs, this.makeCode(")"));
  858. }
  859. if (this.isNew) {
  860. idt = this.tab + TAB;
  861. return [].concat(this.makeCode("(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return Object(result) === result ? result : child;\n" + this.tab + "})("), this.variable.compileToFragments(o, LEVEL_LIST), this.makeCode(", "), splatArgs, this.makeCode(", function(){})"));
  862. }
  863. answer = [];
  864. base = new Value(this.variable);
  865. if ((name = base.properties.pop()) && base.isComplex()) {
  866. ref = o.scope.freeVariable('ref');
  867. answer = answer.concat(this.makeCode("(" + ref + " = "), base.compileToFragments(o, LEVEL_LIST), this.makeCode(")"), name.compileToFragments(o));
  868. } else {
  869. fun = base.compileToFragments(o, LEVEL_ACCESS);
  870. if (SIMPLENUM.test(fragmentsToText(fun))) {
  871. fun = this.wrapInBraces(fun);
  872. }
  873. if (name) {
  874. ref = fragmentsToText(fun);
  875. fun.push.apply(fun, name.compileToFragments(o));
  876. } else {
  877. ref = 'null';
  878. }
  879. answer = answer.concat(fun);
  880. }
  881. return answer = answer.concat(this.makeCode(".apply(" + ref + ", "), splatArgs, this.makeCode(")"));
  882. };
  883. return Call;
  884. })(Base);
  885. exports.Extends = Extends = (function(_super) {
  886. __extends(Extends, _super);
  887. function Extends(child, parent) {
  888. this.child = child;
  889. this.parent = parent;
  890. }
  891. Extends.prototype.children = ['child', 'parent'];
  892. Extends.prototype.compileToFragments = function(o) {
  893. return new Call(new Value(new Literal(utility('extends'))), [this.child, this.parent]).compileToFragments(o);
  894. };
  895. return Extends;
  896. })(Base);
  897. exports.Access = Access = (function(_super) {
  898. __extends(Access, _super);
  899. function Access(name, tag) {
  900. this.name = name;
  901. this.name.asKey = true;
  902. this.soak = tag === 'soak';
  903. }
  904. Access.prototype.children = ['name'];
  905. Access.prototype.compileToFragments = function(o) {
  906. var name;
  907. name = this.name.compileToFragments(o);
  908. if (IDENTIFIER.test(fragmentsToText(name))) {
  909. name.unshift(this.makeCode("."));
  910. } else {
  911. name.unshift(this.makeCode("["));
  912. name.push(this.makeCode("]"));
  913. }
  914. return name;
  915. };
  916. Access.prototype.isComplex = NO;
  917. return Access;
  918. })(Base);
  919. exports.Index = Index = (function(_super) {
  920. __extends(Index, _super);
  921. function Index(index) {
  922. this.index = index;
  923. }
  924. Index.prototype.children = ['index'];
  925. Index.prototype.compileToFragments = function(o) {
  926. return [].concat(this.makeCode("["), this.index.compileToFragments(o, LEVEL_PAREN), this.makeCode("]"));
  927. };
  928. Index.prototype.isComplex = function() {
  929. return this.index.isComplex();
  930. };
  931. return Index;
  932. })(Base);
  933. exports.Range = Range = (function(_super) {
  934. __extends(Range, _super);
  935. Range.prototype.children = ['from', 'to'];
  936. function Range(from, to, tag) {
  937. this.from = from;
  938. this.to = to;
  939. this.exclusive = tag === 'exclusive';
  940. this.equals = this.exclusive ? '' : '=';
  941. }
  942. Range.prototype.compileVariables = function(o) {
  943. var step, _ref4, _ref5, _ref6, _ref7;
  944. o = merge(o, {
  945. top: true
  946. });
  947. _ref4 = this.cacheToCodeFragments(this.from.cache(o, LEVEL_LIST)), this.fromC = _ref4[0], this.fromVar = _ref4[1];
  948. _ref5 = this.cacheToCodeFragments(this.to.cache(o, LEVEL_LIST)), this.toC = _ref5[0], this.toVar = _ref5[1];
  949. if (step = del(o, 'step')) {
  950. _ref6 = this.cacheToCodeFragments(step.cache(o, LEVEL_LIST)), this.step = _ref6[0], this.stepVar = _ref6[1];
  951. }
  952. _ref7 = [this.fromVar.match(SIMPLENUM), this.toVar.match(SIMPLENUM)], this.fromNum = _ref7[0], this.toNum = _ref7[1];
  953. if (this.stepVar) {
  954. return this.stepNum = this.stepVar.match(SIMPLENUM);
  955. }
  956. };
  957. Range.prototype.compileNode = function(o) {
  958. var cond, condPart, from, gt, idx, idxName, known, lt, namedIndex, stepPart, to, varPart, _ref4, _ref5;
  959. if (!this.fromVar) {
  960. this.compileVariables(o);
  961. }
  962. if (!o.index) {
  963. return this.compileArray(o);
  964. }
  965. known = this.fromNum && this.toNum;
  966. idx = del(o, 'index');
  967. idxName = del(o, 'name');
  968. namedIndex = idxName && idxName !== idx;
  969. varPart = "" + idx + " = " + this.fromC;
  970. if (this.toC !== this.toVar) {
  971. varPart += ", " + this.toC;
  972. }
  973. if (this.step !== this.stepVar) {
  974. varPart += ", " + this.step;
  975. }
  976. _ref4 = ["" + idx + " <" + this.equals, "" + idx + " >" + this.equals], lt = _ref4[0], gt = _ref4[1];
  977. condPart = this.stepNum ? +this.stepNum > 0 ? "" + lt + " " + this.toVar : "" + gt + " " + this.toVar : known ? ((_ref5 = [+this.fromNum, +this.toNum], from = _ref5[0], to = _ref5[1], _ref5), from <= to ? "" + lt + " " + to : "" + gt + " " + to) : (cond = this.stepVar ? "" + this.stepVar + " > 0" : "" + this.fromVar + " <= " + this.toVar, "" + cond + " ? " + lt + " " + this.toVar + " : " + gt + " " + this.toVar);
  978. stepPart = this.stepVar ? "" + idx + " += " + this.stepVar : known ? namedIndex ? from <= to ? "++" + idx : "--" + idx : from <= to ? "" + idx + "++" : "" + idx + "--" : namedIndex ? "" + cond + " ? ++" + idx + " : --" + idx : "" + cond + " ? " + idx + "++ : " + idx + "--";
  979. if (namedIndex) {
  980. varPart = "" + idxName + " = " + varPart;
  981. }
  982. if (namedIndex) {
  983. stepPart = "" + idxName + " = " + stepPart;
  984. }
  985. return [this.makeCode("" + varPart + "; " + condPart + "; " + stepPart)];
  986. };
  987. Range.prototype.compileArray = function(o) {
  988. var args, body, cond, hasArgs, i, idt, post, pre, range, result, vars, _i, _ref4, _ref5, _results;
  989. if (this.fromNum && this.toNum && Math.abs(this.fromNum - this.toNum) <= 20) {
  990. range = (function() {
  991. _results = [];
  992. for (var _i = _ref4 = +this.fromNum, _ref5 = +this.toNum; _ref4 <= _ref5 ? _i <= _ref5 : _i >= _ref5; _ref4 <= _ref5 ? _i++ : _i--){ _results.push(_i); }
  993. return _results;
  994. }).apply(this);
  995. if (this.exclusive) {
  996. range.pop();
  997. }
  998. return [this.makeCode("[" + (range.join(', ')) + "]")];
  999. }
  1000. idt = this.tab + TAB;
  1001. i = o.scope.freeVariable('i');
  1002. result = o.scope.freeVariable('results');
  1003. pre = "\n" + idt + result + " = [];";
  1004. if (this.fromNum && this.toNum) {
  1005. o.index = i;
  1006. body = fragmentsToText(this.compileNode(o));
  1007. } else {
  1008. vars = ("" + i + " = " + this.fromC) + (this.toC !== this.toVar ? ", " + this.toC : '');
  1009. cond = "" + this.fromVar + " <= " + this.toVar;
  1010. body = "var " + vars + "; " + cond + " ? " + i + " <" + this.equals + " " + this.toVar + " : " + i + " >" + this.equals + " " + this.toVar + "; " + cond + " ? " + i + "++ : " + i + "--";
  1011. }
  1012. post = "{ " + result + ".push(" + i + "); }\n" + idt + "return " + result + ";\n" + o.indent;
  1013. hasArgs = function(node) {
  1014. return node != null ? node.contains(function(n) {
  1015. return n instanceof Literal && n.value === 'arguments' && !n.asKey;
  1016. }) : void 0;
  1017. };
  1018. if (hasArgs(this.from) || hasArgs(this.to)) {
  1019. args = ', arguments';
  1020. }
  1021. return [this.makeCode("(function() {" + pre + "\n" + idt + "for (" + body + ")" + post + "}).apply(this" + (args != null ? args : '') + ")")];
  1022. };
  1023. return Range;
  1024. })(Base);
  1025. exports.Slice = Slice = (function(_super) {
  1026. __extends(Slice, _super);
  1027. Slice.prototype.children = ['range'];
  1028. function Slice(range) {
  1029. this.range = range;
  1030. Slice.__super__.constructor.call(this);
  1031. }
  1032. Slice.prototype.compileNode = function(o) {
  1033. var compiled, compiledText, from, fromCompiled, to, toStr, _ref4;
  1034. _ref4 = this.range, to = _ref4.to, from = _ref4.from;
  1035. fromCompiled = from && from.compileToFragments(o, LEVEL_PAREN) || [this.makeCode('0')];
  1036. if (to) {
  1037. compiled = to.compileToFragments(o, LEVEL_PAREN);
  1038. compiledText = fragmentsToText(compiled);
  1039. if (!(!this.range.exclusive && +compiledText === -1)) {
  1040. toStr = ', ' + (this.range.exclusive ? compiledText : SIMPLENUM.test(compiledText) ? "" + (+compiledText + 1) : (compiled = to.compileToFragments(o, LEVEL_ACCESS), "+" + (fragmentsToText(compiled)) + " + 1 || 9e9"));
  1041. }
  1042. }
  1043. return [this.makeCode(".slice(" + (fragmentsToText(fromCompiled)) + (toStr || '') + ")")];
  1044. };
  1045. return Slice;
  1046. })(Base);
  1047. exports.Obj = Obj = (function(_super) {
  1048. __extends(Obj, _super);
  1049. function Obj(props, generated) {
  1050. this.generated = generated != null ? generated : false;
  1051. this.objects = this.properties = props || [];
  1052. }
  1053. Obj.prototype.children = ['properties'];
  1054. Obj.prototype.compileNode = function(o) {
  1055. var answer, i, idt, indent, join, lastNoncom, node, prop, props, _i, _j, _len, _len1;
  1056. props = this.properties;
  1057. if (!props.length) {
  1058. return [this.makeCode(this.front ? '({})' : '{}')];
  1059. }
  1060. if (this.generated) {
  1061. for (_i = 0, _len = props.length; _i < _len; _i++) {
  1062. node = props[_i];
  1063. if (node instanceof Value) {
  1064. node.error('cannot have an implicit value in an implicit object');
  1065. }
  1066. }
  1067. }
  1068. idt = o.indent += TAB;
  1069. lastNoncom = this.lastNonComment(this.properties);
  1070. answer = [];
  1071. for (i = _j = 0, _len1 = props.length; _j < _len1; i = ++_j) {
  1072. prop = props[i];
  1073. join = i === props.length - 1 ? '' : prop === lastNoncom || prop instanceof Comment ? '\n' : ',\n';
  1074. indent = prop instanceof Comment ? '' : idt;
  1075. if (prop instanceof Assign && prop.variable instanceof Value && prop.variable.hasProperties()) {
  1076. prop.variable.error('Invalid object key');
  1077. }
  1078. if (prop instanceof Value && prop["this"]) {
  1079. prop = new Assign(prop.properties[0].name, prop, 'object');
  1080. }
  1081. if (!(prop instanceof Comment)) {
  1082. if (!(prop instanceof Assign)) {
  1083. prop = new Assign(prop, prop, 'object');
  1084. }
  1085. (prop.variable.base || prop.variable).asKey = true;
  1086. }
  1087. if (indent) {
  1088. answer.push(this.makeCode(indent));
  1089. }
  1090. answer.push.apply(answer, prop.compileToFragments(o, LEVEL_TOP));
  1091. if (join) {
  1092. answer.push(this.makeCode(join));
  1093. }
  1094. }
  1095. answer.unshift(this.makeCode("{" + (props.length && '\n')));
  1096. answer.push(this.makeCode("" + (props.length && '\n' + this.tab) + "}"));
  1097. if (this.front) {
  1098. return this.wrapInBraces(answer);
  1099. } else {
  1100. return answer;
  1101. }
  1102. };
  1103. Obj.prototype.assigns = function(name) {
  1104. var prop, _i, _len, _ref4;
  1105. _ref4 = this.properties;
  1106. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  1107. prop = _ref4[_i];
  1108. if (prop.assigns(name)) {
  1109. return true;
  1110. }
  1111. }
  1112. return false;
  1113. };
  1114. return Obj;
  1115. })(Base);
  1116. exports.Arr = Arr = (function(_super) {
  1117. __extends(Arr, _super);
  1118. function Arr(objs) {
  1119. this.objects = objs || [];
  1120. }
  1121. Arr.prototype.children = ['objects'];
  1122. Arr.prototype.compileNode = function(o) {
  1123. var answer, compiledObjs, fragments, index, obj, _i, _len;
  1124. if (!this.objects.length) {
  1125. return [this.makeCode('[]')];
  1126. }
  1127. o.indent += TAB;
  1128. answer = Splat.compileSplattedArray(o, this.objects);
  1129. if (answer.length) {
  1130. return answer;
  1131. }
  1132. answer = [];
  1133. compiledObjs = (function() {
  1134. var _i, _len, _ref4, _results;
  1135. _ref4 = this.objects;
  1136. _results = [];
  1137. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  1138. obj = _ref4[_i];
  1139. _results.push(obj.compileToFragments(o, LEVEL_LIST));
  1140. }
  1141. return _results;
  1142. }).call(this);
  1143. for (index = _i = 0, _len = compiledObjs.length; _i < _len; index = ++_i) {
  1144. fragments = compiledObjs[index];
  1145. if (index) {
  1146. answer.push(this.makeCode(", "));
  1147. }
  1148. answer.push.apply(answer, fragments);
  1149. }
  1150. if (fragmentsToText(answer).indexOf('\n') >= 0) {
  1151. answer.unshift(this.makeCode("[\n" + o.indent));
  1152. answer.push(this.makeCode("\n" + this.tab + "]"));
  1153. } else {
  1154. answer.unshift(this.makeCode("["));
  1155. answer.push(this.makeCode("]"));
  1156. }
  1157. return answer;
  1158. };
  1159. Arr.prototype.assigns = function(name) {
  1160. var obj, _i, _len, _ref4;
  1161. _ref4 = this.objects;
  1162. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  1163. obj = _ref4[_i];
  1164. if (obj.assigns(name)) {
  1165. return true;
  1166. }
  1167. }
  1168. return false;
  1169. };
  1170. return Arr;
  1171. })(Base);
  1172. exports.Class = Class = (function(_super) {
  1173. __extends(Class, _super);
  1174. function Class(variable, parent, body) {
  1175. this.variable = variable;
  1176. this.parent = parent;
  1177. this.body = body != null ? body : new Block;
  1178. this.boundFuncs = [];
  1179. this.body.classBody = true;
  1180. }
  1181. Class.prototype.children = ['variable', 'parent', 'body'];
  1182. Class.prototype.determineName = function() {
  1183. var decl, tail;
  1184. if (!this.variable) {
  1185. return null;
  1186. }
  1187. decl = (tail = last(this.variable.properties)) ? tail instanceof Access && tail.name.value : this.variable.base.value;
  1188. if (__indexOf.call(STRICT_PROSCRIBED, decl) >= 0) {
  1189. this.variable.error("class variable name may not be " + decl);
  1190. }
  1191. return decl && (decl = IDENTIFIER.test(decl) && decl);
  1192. };
  1193. Class.prototype.setContext = function(name) {
  1194. return this.body.traverseChildren(false, function(node) {
  1195. if (node.classBody) {
  1196. return false;
  1197. }
  1198. if (node instanceof Literal && node.value === 'this') {
  1199. return node.value = name;
  1200. } else if (node instanceof Code) {
  1201. node.klass = name;
  1202. if (node.bound) {
  1203. return node.context = name;
  1204. }
  1205. }
  1206. });
  1207. };
  1208. Class.prototype.addBoundFunctions = function(o) {
  1209. var bvar, lhs, _i, _len, _ref4;
  1210. _ref4 = this.boundFuncs;
  1211. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  1212. bvar = _ref4[_i];
  1213. lhs = (new Value(new Literal("this"), [new Access(bvar)])).compile(o);
  1214. this.ctor.body.unshift(new Literal("" + lhs + " = " + (utility('bind')) + "(" + lhs + ", this)"));
  1215. }
  1216. };
  1217. Class.prototype.addProperties = function(node, name, o) {
  1218. var assign, base, exprs, func, props;
  1219. props = node.base.properties.slice(0);
  1220. exprs = (function() {
  1221. var _results;
  1222. _results = [];
  1223. while (assign = props.shift()) {
  1224. if (assign instanceof Assign) {
  1225. base = assign.variable.base;
  1226. delete assign.context;
  1227. func = assign.value;
  1228. if (base.value === 'constructor') {
  1229. if (this.ctor) {
  1230. assign.error('cannot define more than one constructor in a class');
  1231. }
  1232. if (func.bound) {
  1233. assign.error('cannot define a constructor as a bound function');
  1234. }
  1235. if (func instanceof Code) {
  1236. assign = this.ctor = func;
  1237. } else {
  1238. this.externalCtor = o.scope.freeVariable('class');
  1239. assign = new Assign(new Literal(this.externalCtor), func);
  1240. }
  1241. } else {
  1242. if (assign.variable["this"]) {
  1243. func["static"] = true;
  1244. if (func.bound) {
  1245. func.context = name;
  1246. }
  1247. } else {
  1248. assign.variable = new Value(new Literal(name), [new Access(new Literal('prototype')), new Access(base)]);
  1249. if (func instanceof Code && func.bound) {
  1250. this.boundFuncs.push(base);
  1251. func.bound = false;
  1252. }
  1253. }
  1254. }
  1255. }
  1256. _results.push(assign);
  1257. }
  1258. return _results;
  1259. }).call(this);
  1260. return compact(exprs);
  1261. };
  1262. Class.prototype.walkBody = function(name, o) {
  1263. var _this = this;
  1264. return this.traverseChildren(false, function(child) {
  1265. var cont, exps, i, node, _i, _len, _ref4;
  1266. cont = true;
  1267. if (child instanceof Class) {
  1268. return false;
  1269. }
  1270. if (child instanceof Block) {
  1271. _ref4 = exps = child.expressions;
  1272. for (i = _i = 0, _len = _ref4.length; _i < _len; i = ++_i) {
  1273. node = _ref4[i];
  1274. if (node instanceof Value && node.isObject(true)) {
  1275. cont = false;
  1276. exps[i] = _this.addProperties(node, name, o);
  1277. }
  1278. }
  1279. child.expressions = exps = flatten(exps);
  1280. }
  1281. return cont && !(child instanceof Class);
  1282. });
  1283. };
  1284. Class.prototype.hoistDirectivePrologue = function() {
  1285. var expressions, index, node;
  1286. index = 0;
  1287. expressions = this.body.expressions;
  1288. while ((node = expressions[index]) && node instanceof Comment || node instanceof Value && node.isString()) {
  1289. ++index;
  1290. }
  1291. return this.directives = expressions.splice(0, index);
  1292. };
  1293. Class.prototype.ensureConstructor = function(name, o) {
  1294. var missing, ref, superCall;
  1295. missing = !this.ctor;
  1296. this.ctor || (this.ctor = new Code);
  1297. this.ctor.ctor = this.ctor.name = name;
  1298. this.ctor.klass = null;
  1299. this.ctor.noReturn = true;
  1300. if (missing) {
  1301. if (this.parent) {
  1302. superCall = new Literal("" + name + ".__super__.constructor.apply(this, arguments)");
  1303. }
  1304. if (this.externalCtor) {
  1305. superCall = new Literal("" + this.externalCtor + ".apply(this, arguments)");
  1306. }
  1307. if (superCall) {
  1308. ref = new Literal(o.scope.freeVariable('ref'));
  1309. this.ctor.body.unshift(new Assign(ref, superCall));
  1310. }
  1311. this.addBoundFunctions(o);
  1312. if (superCall) {
  1313. this.ctor.body.push(ref);
  1314. this.ctor.body.makeReturn();
  1315. }
  1316. return this.body.expressions.unshift(this.ctor);
  1317. } else {
  1318. return this.addBoundFunctions(o);
  1319. }
  1320. };
  1321. Class.prototype.compileNode = function(o) {
  1322. var call, decl, klass, lname, name, params, _ref4;
  1323. decl = this.determineName();
  1324. name = decl || '_Class';
  1325. if (name.reserved) {
  1326. name = "_" + name;
  1327. }
  1328. lname = new Literal(name);
  1329. this.hoistDirectivePrologue();
  1330. this.setContext(name);
  1331. this.walkBody(name, o);
  1332. this.ensureConstructor(name, o);
  1333. this.body.spaced = true;
  1334. if (!(this.ctor instanceof Code)) {
  1335. this.body.expressions.unshift(this.ctor);
  1336. }
  1337. this.body.expressions.push(lname);
  1338. (_ref4 = this.body.expressions).unshift.apply(_ref4, this.directives);
  1339. call = Closure.wrap(this.body);
  1340. if (this.parent) {
  1341. this.superClass = new Literal(o.scope.freeVariable('super', false));
  1342. this.body.expressions.unshift(new Extends(lname, this.superClass));
  1343. call.args.push(this.parent);
  1344. params = call.variable.params || call.variable.base.params;
  1345. params.push(new Param(this.superClass));
  1346. }
  1347. klass = new Parens(call, true);
  1348. if (this.variable) {
  1349. klass = new Assign(this.variable, klass);
  1350. }
  1351. return klass.compileToFragments(o);
  1352. };
  1353. return Class;
  1354. })(Base);
  1355. exports.Assign = Assign = (function(_super) {
  1356. __extends(Assign, _super);
  1357. function Assign(variable, value, context, options) {
  1358. var forbidden, name, _ref4;
  1359. this.variable = variable;
  1360. this.value = value;
  1361. this.context = context;
  1362. this.param = options && options.param;
  1363. this.subpattern = options && options.subpattern;
  1364. forbidden = (_ref4 = (name = this.variable.unwrapAll().value), __indexOf.call(STRICT_PROSCRIBED, _ref4) >= 0);
  1365. if (forbidden && this.context !== 'object') {
  1366. this.variable.error("variable name may not be \"" + name + "\"");
  1367. }
  1368. }
  1369. Assign.prototype.children = ['variable', 'value'];
  1370. Assign.prototype.isStatement = function(o) {
  1371. return (o != null ? o.level : void 0) === LEVEL_TOP && (this.context != null) && __indexOf.call(this.context, "?") >= 0;
  1372. };
  1373. Assign.prototype.assigns = function(name) {
  1374. return this[this.context === 'object' ? 'value' : 'variable'].assigns(name);
  1375. };
  1376. Assign.prototype.unfoldSoak = function(o) {
  1377. return unfoldSoak(o, this, 'variable');
  1378. };
  1379. Assign.prototype.compileNode = function(o) {
  1380. var answer, compiledName, isValue, match, name, val, varBase, _ref4, _ref5, _ref6, _ref7;
  1381. if (isValue = this.variable instanceof Value) {
  1382. if (this.variable.isArray() || this.variable.isObject()) {
  1383. return this.compilePatternMatch(o);
  1384. }
  1385. if (this.variable.isSplice()) {
  1386. return this.compileSplice(o);
  1387. }
  1388. if ((_ref4 = this.context) === '||=' || _ref4 === '&&=' || _ref4 === '?=') {
  1389. return this.compileConditional(o);
  1390. }
  1391. }
  1392. compiledName = this.variable.compileToFragments(o, LEVEL_LIST);
  1393. name = fragmentsToText(compiledName);
  1394. if (!this.context) {
  1395. varBase = this.variable.unwrapAll();
  1396. if (!varBase.isAssignable()) {
  1397. this.variable.error("\"" + (this.variable.compile(o)) + "\" cannot be assigned");
  1398. }
  1399. if (!(typeof varBase.hasProperties === "function" ? varBase.hasProperties() : void 0)) {
  1400. if (this.param) {
  1401. o.scope.add(name, 'var');
  1402. } else {
  1403. o.scope.find(name);
  1404. }
  1405. }
  1406. }
  1407. if (this.value instanceof Code && (match = METHOD_DEF.exec(name))) {
  1408. if (match[1]) {
  1409. this.value.klass = match[1];
  1410. }
  1411. this.value.name = (_ref5 = (_ref6 = (_ref7 = match[2]) != null ? _ref7 : match[3]) != null ? _ref6 : match[4]) != null ? _ref5 : match[5];
  1412. }
  1413. val = this.value.compileToFragments(o, LEVEL_LIST);
  1414. if (this.context === 'object') {
  1415. return compiledName.concat(this.makeCode(": "), val);
  1416. }
  1417. answer = compiledName.concat(this.makeCode(" " + (this.context || '=') + " "), val);
  1418. if (o.level <= LEVEL_LIST) {
  1419. return answer;
  1420. } else {
  1421. return this.wrapInBraces(answer);
  1422. }
  1423. };
  1424. Assign.prototype.compilePatternMatch = function(o) {
  1425. var acc, assigns, code, fragments, i, idx, isObject, ivar, name, obj, objects, olen, ref, rest, splat, top, val, value, vvar, vvarText, _i, _len, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;
  1426. top = o.level === LEVEL_TOP;
  1427. value = this.value;
  1428. objects = this.variable.base.objects;
  1429. if (!(olen = objects.length)) {
  1430. code = value.compileToFragments(o);
  1431. if (o.level >= LEVEL_OP) {
  1432. return this.wrapInBraces(code);
  1433. } else {
  1434. return code;
  1435. }
  1436. }
  1437. isObject = this.variable.isObject();
  1438. if (top && olen === 1 && !((obj = objects[0]) instanceof Splat)) {
  1439. if (obj instanceof Assign) {
  1440. _ref4 = obj, (_ref5 = _ref4.variable, idx = _ref5.base), obj = _ref4.value;
  1441. } else {
  1442. idx = isObject ? obj["this"] ? obj.properties[0].name : obj : new Literal(0);
  1443. }
  1444. acc = IDENTIFIER.test(idx.unwrap().value || 0);
  1445. value = new Value(value);
  1446. value.properties.push(new (acc ? Access : Index)(idx));
  1447. if (_ref6 = obj.unwrap().value, __indexOf.call(RESERVED, _ref6) >= 0) {
  1448. obj.error("assignment to a reserved word: " + (obj.compile(o)));
  1449. }
  1450. return new Assign(obj, value, null, {
  1451. param: this.param
  1452. }).compileToFragments(o, LEVEL_TOP);
  1453. }
  1454. vvar = value.compileToFragments(o, LEVEL_LIST);
  1455. vvarText = fragmentsToText(vvar);
  1456. assigns = [];
  1457. splat = false;
  1458. if (!IDENTIFIER.test(vvarText) || this.variable.assigns(vvarText)) {
  1459. assigns.push([this.makeCode("" + (ref = o.scope.freeVariable('ref')) + " = ")].concat(__slice.call(vvar)));
  1460. vvar = [this.makeCode(ref)];
  1461. vvarText = ref;
  1462. }
  1463. for (i = _i = 0, _len = objects.length; _i < _len; i = ++_i) {
  1464. obj = objects[i];
  1465. idx = i;
  1466. if (isObject) {
  1467. if (obj instanceof Assign) {
  1468. _ref7 = obj, (_ref8 = _ref7.variable, idx = _ref8.base), obj = _ref7.value;
  1469. } else {
  1470. if (obj.base instanceof Parens) {
  1471. _ref9 = new Value(obj.unwrapAll()).cacheReference(o), obj = _ref9[0], idx = _ref9[1];
  1472. } else {
  1473. idx = obj["this"] ? obj.properties[0].name : obj;
  1474. }
  1475. }
  1476. }
  1477. if (!splat && obj instanceof Splat) {
  1478. name = obj.name.unwrap().value;
  1479. obj = obj.unwrap();
  1480. val = "" + olen + " <= " + vvarText + ".length ? " + (utility('slice')) + ".call(" + vvarText + ", " + i;
  1481. if (rest = olen - i - 1) {
  1482. ivar = o.scope.freeVariable('i');
  1483. val += ", " + ivar + " = " + vvarText + ".length - " + rest + ") : (" + ivar + " = " + i + ", [])";
  1484. } else {
  1485. val += ") : []";
  1486. }
  1487. val = new Literal(val);
  1488. splat = "" + ivar + "++";
  1489. } else {
  1490. name = obj.unwrap().value;
  1491. if (obj instanceof Splat) {
  1492. obj.error("multiple splats are disallowed in an assignment");
  1493. }
  1494. if (typeof idx === 'number') {
  1495. idx = new Literal(splat || idx);
  1496. acc = false;
  1497. } else {
  1498. acc = isObject && IDENTIFIER.test(idx.unwrap().value || 0);
  1499. }
  1500. val = new Value(new Literal(vvarText), [new (acc ? Access : Index)(idx)]);
  1501. }
  1502. if ((name != null) && __indexOf.call(RESERVED, name) >= 0) {
  1503. obj.error("assignment to a reserved word: " + (obj.compile(o)));
  1504. }
  1505. assigns.push(new Assign(obj, val, null, {
  1506. param: this.param,
  1507. subpattern: true
  1508. }).compileToFragments(o, LEVEL_LIST));
  1509. }
  1510. if (!(top || this.subpattern)) {
  1511. assigns.push(vvar);
  1512. }
  1513. fragments = this.joinFragmentArrays(assigns, ', ');
  1514. if (o.level < LEVEL_LIST) {
  1515. return fragments;
  1516. } else {
  1517. return this.wrapInBraces(fragments);
  1518. }
  1519. };
  1520. Assign.prototype.compileConditional = function(o) {
  1521. var left, right, _ref4;
  1522. _ref4 = this.variable.cacheReference(o), left = _ref4[0], right = _ref4[1];
  1523. if (!left.properties.length && left.base instanceof Literal && left.base.value !== "this" && !o.scope.check(left.base.value)) {
  1524. this.variable.error("the variable \"" + left.base.value + "\" can't be assigned with " + this.context + " because it has not been declared before");
  1525. }
  1526. if (__indexOf.call(this.context, "?") >= 0) {
  1527. o.isExistentialEquals = true;
  1528. }
  1529. return new Op(this.context.slice(0, -1), left, new Assign(right, this.value, '=')).compileToFragments(o);
  1530. };
  1531. Assign.prototype.compileSplice = function(o) {
  1532. var answer, exclusive, from, fromDecl, fromRef, name, to, valDef, valRef, _ref4, _ref5, _ref6;
  1533. _ref4 = this.variable.properties.pop().range, from = _ref4.from, to = _ref4.to, exclusive = _ref4.exclusive;
  1534. name = this.variable.compile(o);
  1535. if (from) {
  1536. _ref5 = this.cacheToCodeFragments(from.cache(o, LEVEL_OP)), fromDecl = _ref5[0], fromRef = _ref5[1];
  1537. } else {
  1538. fromDecl = fromRef = '0';
  1539. }
  1540. if (to) {
  1541. if ((from != null ? from.isSimpleNumber() : void 0) && to.isSimpleNumber()) {
  1542. to = +to.compile(o) - +fromRef;
  1543. if (!exclusive) {
  1544. to += 1;
  1545. }
  1546. } else {
  1547. to = to.compile(o, LEVEL_ACCESS) + ' - ' + fromRef;
  1548. if (!exclusive) {
  1549. to += ' + 1';
  1550. }
  1551. }
  1552. } else {
  1553. to = "9e9";
  1554. }
  1555. _ref6 = this.value.cache(o, LEVEL_LIST), valDef = _ref6[0], valRef = _ref6[1];
  1556. answer = [].concat(this.makeCode("[].splice.apply(" + name + ", [" + fromDecl + ", " + to + "].concat("), valDef, this.makeCode(")), "), valRef);
  1557. if (o.level > LEVEL_TOP) {
  1558. return this.wrapInBraces(answer);
  1559. } else {
  1560. return answer;
  1561. }
  1562. };
  1563. return Assign;
  1564. })(Base);
  1565. exports.Code = Code = (function(_super) {
  1566. __extends(Code, _super);
  1567. function Code(params, body, tag) {
  1568. this.params = params || [];
  1569. this.body = body || new Block;
  1570. this.bound = tag === 'boundfunc';
  1571. if (this.bound) {
  1572. this.context = '_this';
  1573. }
  1574. }
  1575. Code.prototype.children = ['params', 'body'];
  1576. Code.prototype.isStatement = function() {
  1577. return !!this.ctor;
  1578. };
  1579. Code.prototype.jumps = NO;
  1580. Code.prototype.compileNode = function(o) {
  1581. var answer, code, exprs, i, idt, lit, p, param, params, ref, splats, uniqs, val, wasEmpty, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref4, _ref5, _ref6, _ref7, _ref8;
  1582. o.scope = new Scope(o.scope, this.body, this);
  1583. o.scope.shared = del(o, 'sharedScope');
  1584. o.indent += TAB;
  1585. delete o.bare;
  1586. delete o.isExistentialEquals;
  1587. params = [];
  1588. exprs = [];
  1589. this.eachParamName(function(name) {
  1590. if (!o.scope.check(name)) {
  1591. return o.scope.parameter(name);
  1592. }
  1593. });
  1594. _ref4 = this.params;
  1595. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  1596. param = _ref4[_i];
  1597. if (!param.splat) {
  1598. continue;
  1599. }
  1600. _ref5 = this.params;
  1601. for (_j = 0, _len1 = _ref5.length; _j < _len1; _j++) {
  1602. p = _ref5[_j].name;
  1603. if (p["this"]) {
  1604. p = p.properties[0].name;
  1605. }
  1606. if (p.value) {
  1607. o.scope.add(p.value, 'var', true);
  1608. }
  1609. }
  1610. splats = new Assign(new Value(new Arr((function() {
  1611. var _k, _len2, _ref6, _results;
  1612. _ref6 = this.params;
  1613. _results = [];
  1614. for (_k = 0, _len2 = _ref6.length; _k < _len2; _k++) {
  1615. p = _ref6[_k];
  1616. _results.push(p.asReference(o));
  1617. }
  1618. return _results;
  1619. }).call(this))), new Value(new Literal('arguments')));
  1620. break;
  1621. }
  1622. _ref6 = this.params;
  1623. for (_k = 0, _len2 = _ref6.length; _k < _len2; _k++) {
  1624. param = _ref6[_k];
  1625. if (param.isComplex()) {
  1626. val = ref = param.asReference(o);
  1627. if (param.value) {
  1628. val = new Op('?', ref, param.value);
  1629. }
  1630. exprs.push(new Assign(new Value(param.name), val, '=', {
  1631. param: true
  1632. }));
  1633. } else {
  1634. ref = param;
  1635. if (param.value) {
  1636. lit = new Literal(ref.name.value + ' == null');
  1637. val = new Assign(new Value(param.name), param.value, '=');
  1638. exprs.push(new If(lit, val));
  1639. }
  1640. }
  1641. if (!splats) {
  1642. params.push(ref);
  1643. }
  1644. }
  1645. wasEmpty = this.body.isEmpty();
  1646. if (splats) {
  1647. exprs.unshift(splats);
  1648. }
  1649. if (exprs.length) {
  1650. (_ref7 = this.body.expressions).unshift.apply(_ref7, exprs);
  1651. }
  1652. for (i = _l = 0, _len3 = params.length; _l < _len3; i = ++_l) {
  1653. p = params[i];
  1654. params[i] = p.compileToFragments(o);
  1655. o.scope.parameter(fragmentsToText(params[i]));
  1656. }
  1657. uniqs = [];
  1658. this.eachParamName(function(name, node) {
  1659. if (__indexOf.call(uniqs, name) >= 0) {
  1660. node.error("multiple parameters named '" + name + "'");
  1661. }
  1662. return uniqs.push(name);
  1663. });
  1664. if (!(wasEmpty || this.noReturn)) {
  1665. this.body.makeReturn();
  1666. }
  1667. if (this.bound) {
  1668. if ((_ref8 = o.scope.parent.method) != null ? _ref8.bound : void 0) {
  1669. this.bound = this.context = o.scope.parent.method.context;
  1670. } else if (!this["static"]) {
  1671. o.scope.parent.assign('_this', 'this');
  1672. }
  1673. }
  1674. idt = o.indent;
  1675. code = 'function';
  1676. if (this.ctor) {
  1677. code += ' ' + this.name;
  1678. }
  1679. code += '(';
  1680. answer = [this.makeCode(code)];
  1681. for (i = _m = 0, _len4 = params.length; _m < _len4; i = ++_m) {
  1682. p = params[i];
  1683. if (i) {
  1684. answer.push(this.makeCode(", "));
  1685. }
  1686. answer.push.apply(answer, p);
  1687. }
  1688. answer.push(this.makeCode(') {'));
  1689. if (!this.body.isEmpty()) {
  1690. answer = answer.concat(this.makeCode("\n"), this.body.compileWithDeclarations(o), this.makeCode("\n" + this.tab));
  1691. }
  1692. answer.push(this.makeCode('}'));
  1693. if (this.ctor) {
  1694. return [this.makeCode(this.tab)].concat(__slice.call(answer));
  1695. }
  1696. if (this.front || (o.level >= LEVEL_ACCESS)) {
  1697. return this.wrapInBraces(answer);
  1698. } else {
  1699. return answer;
  1700. }
  1701. };
  1702. Code.prototype.eachParamName = function(iterator) {
  1703. var param, _i, _len, _ref4, _results;
  1704. _ref4 = this.params;
  1705. _results = [];
  1706. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  1707. param = _ref4[_i];
  1708. _results.push(param.eachName(iterator));
  1709. }
  1710. return _results;
  1711. };
  1712. Code.prototype.traverseChildren = function(crossScope, func) {
  1713. if (crossScope) {
  1714. return Code.__super__.traverseChildren.call(this, crossScope, func);
  1715. }
  1716. };
  1717. return Code;
  1718. })(Base);
  1719. exports.Param = Param = (function(_super) {
  1720. __extends(Param, _super);
  1721. function Param(name, value, splat) {
  1722. var _ref4;
  1723. this.name = name;
  1724. this.value = value;
  1725. this.splat = splat;
  1726. if (_ref4 = (name = this.name.unwrapAll().value), __indexOf.call(STRICT_PROSCRIBED, _ref4) >= 0) {
  1727. this.name.error("parameter name \"" + name + "\" is not allowed");
  1728. }
  1729. }
  1730. Param.prototype.children = ['name', 'value'];
  1731. Param.prototype.compileToFragments = function(o) {
  1732. return this.name.compileToFragments(o, LEVEL_LIST);
  1733. };
  1734. Param.prototype.asReference = function(o) {
  1735. var node;
  1736. if (this.reference) {
  1737. return this.reference;
  1738. }
  1739. node = this.name;
  1740. if (node["this"]) {
  1741. node = node.properties[0].name;
  1742. if (node.value.reserved) {
  1743. node = new Literal(o.scope.freeVariable(node.value));
  1744. }
  1745. } else if (node.isComplex()) {
  1746. node = new Literal(o.scope.freeVariable('arg'));
  1747. }
  1748. node = new Value(node);
  1749. if (this.splat) {
  1750. node = new Splat(node);
  1751. }
  1752. return this.reference = node;
  1753. };
  1754. Param.prototype.isComplex = function() {
  1755. return this.name.isComplex();
  1756. };
  1757. Param.prototype.eachName = function(iterator, name) {
  1758. var atParam, node, obj, _i, _len, _ref4;
  1759. if (name == null) {
  1760. name = this.name;
  1761. }
  1762. atParam = function(obj) {
  1763. var node;
  1764. node = obj.properties[0].name;
  1765. if (!node.value.reserved) {
  1766. return iterator(node.value, node);
  1767. }
  1768. };
  1769. if (name instanceof Literal) {
  1770. return iterator(name.value, name);
  1771. }
  1772. if (name instanceof Value) {
  1773. return atParam(name);
  1774. }
  1775. _ref4 = name.objects;
  1776. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  1777. obj = _ref4[_i];
  1778. if (obj instanceof Assign) {
  1779. this.eachName(iterator, obj.value.unwrap());
  1780. } else if (obj instanceof Splat) {
  1781. node = obj.name.unwrap();
  1782. iterator(node.value, node);
  1783. } else if (obj instanceof Value) {
  1784. if (obj.isArray() || obj.isObject()) {
  1785. this.eachName(iterator, obj.base);
  1786. } else if (obj["this"]) {
  1787. atParam(obj);
  1788. } else {
  1789. iterator(obj.base.value, obj.base);
  1790. }
  1791. } else {
  1792. obj.error("illegal parameter " + (obj.compile()));
  1793. }
  1794. }
  1795. };
  1796. return Param;
  1797. })(Base);
  1798. exports.Splat = Splat = (function(_super) {
  1799. __extends(Splat, _super);
  1800. Splat.prototype.children = ['name'];
  1801. Splat.prototype.isAssignable = YES;
  1802. function Splat(name) {
  1803. this.name = name.compile ? name : new Literal(name);
  1804. }
  1805. Splat.prototype.assigns = function(name) {
  1806. return this.name.assigns(name);
  1807. };
  1808. Splat.prototype.compileToFragments = function(o) {
  1809. return this.name.compileToFragments(o);
  1810. };
  1811. Splat.prototype.unwrap = function() {
  1812. return this.name;
  1813. };
  1814. Splat.compileSplattedArray = function(o, list, apply) {
  1815. var args, base, compiledNode, concatPart, fragments, i, index, node, _i, _len;
  1816. index = -1;
  1817. while ((node = list[++index]) && !(node instanceof Splat)) {
  1818. continue;
  1819. }
  1820. if (index >= list.length) {
  1821. return [];
  1822. }
  1823. if (list.length === 1) {
  1824. node = list[0];
  1825. fragments = node.compileToFragments(o, LEVEL_LIST);
  1826. if (apply) {
  1827. return fragments;
  1828. }
  1829. return [].concat(node.makeCode("" + (utility('slice')) + ".call("), fragments, node.makeCode(")"));
  1830. }
  1831. args = list.slice(index);
  1832. for (i = _i = 0, _len = args.length; _i < _len; i = ++_i) {
  1833. node = args[i];
  1834. compiledNode = node.compileToFragments(o, LEVEL_LIST);
  1835. args[i] = node instanceof Splat ? [].concat(node.makeCode("" + (utility('slice')) + ".call("), compiledNode, node.makeCode(")")) : [].concat(node.makeCode("["), compiledNode, node.makeCode("]"));
  1836. }
  1837. if (index === 0) {
  1838. node = list[0];
  1839. concatPart = node.joinFragmentArrays(args.slice(1), ', ');
  1840. return args[0].concat(node.makeCode(".concat("), concatPart, node.makeCode(")"));
  1841. }
  1842. base = (function() {
  1843. var _j, _len1, _ref4, _results;
  1844. _ref4 = list.slice(0, index);
  1845. _results = [];
  1846. for (_j = 0, _len1 = _ref4.length; _j < _len1; _j++) {
  1847. node = _ref4[_j];
  1848. _results.push(node.compileToFragments(o, LEVEL_LIST));
  1849. }
  1850. return _results;
  1851. })();
  1852. base = list[0].joinFragmentArrays(base, ', ');
  1853. concatPart = list[index].joinFragmentArrays(args, ', ');
  1854. return [].concat(list[0].makeCode("["), base, list[index].makeCode("].concat("), concatPart, (last(list)).makeCode(")"));
  1855. };
  1856. return Splat;
  1857. })(Base);
  1858. exports.While = While = (function(_super) {
  1859. __extends(While, _super);
  1860. function While(condition, options) {
  1861. this.condition = (options != null ? options.invert : void 0) ? condition.invert() : condition;
  1862. this.guard = options != null ? options.guard : void 0;
  1863. }
  1864. While.prototype.children = ['condition', 'guard', 'body'];
  1865. While.prototype.isStatement = YES;
  1866. While.prototype.makeReturn = function(res) {
  1867. if (res) {
  1868. return While.__super__.makeReturn.apply(this, arguments);
  1869. } else {
  1870. this.returns = !this.jumps({
  1871. loop: true
  1872. });
  1873. return this;
  1874. }
  1875. };
  1876. While.prototype.addBody = function(body) {
  1877. this.body = body;
  1878. return this;
  1879. };
  1880. While.prototype.jumps = function() {
  1881. var expressions, node, _i, _len;
  1882. expressions = this.body.expressions;
  1883. if (!expressions.length) {
  1884. return false;
  1885. }
  1886. for (_i = 0, _len = expressions.length; _i < _len; _i++) {
  1887. node = expressions[_i];
  1888. if (node.jumps({
  1889. loop: true
  1890. })) {
  1891. return node;
  1892. }
  1893. }
  1894. return false;
  1895. };
  1896. While.prototype.compileNode = function(o) {
  1897. var answer, body, rvar, set;
  1898. o.indent += TAB;
  1899. set = '';
  1900. body = this.body;
  1901. if (body.isEmpty()) {
  1902. body = this.makeCode('');
  1903. } else {
  1904. if (this.returns) {
  1905. body.makeReturn(rvar = o.scope.freeVariable('results'));
  1906. set = "" + this.tab + rvar + " = [];\n";
  1907. }
  1908. if (this.guard) {
  1909. if (body.expressions.length > 1) {
  1910. body.expressions.unshift(new If((new Parens(this.guard)).invert(), new Literal("continue")));
  1911. } else {
  1912. if (this.guard) {
  1913. body = Block.wrap([new If(this.guard, body)]);
  1914. }
  1915. }
  1916. }
  1917. body = [].concat(this.makeCode("\n"), body.compileToFragments(o, LEVEL_TOP), this.makeCode("\n" + this.tab));
  1918. }
  1919. answer = [].concat(this.makeCode(set + this.tab + "while ("), this.condition.compileToFragments(o, LEVEL_PAREN), this.makeCode(") {"), body, this.makeCode("}"));
  1920. if (this.returns) {
  1921. answer.push(this.makeCode("\n" + this.tab + "return " + rvar + ";"));
  1922. }
  1923. return answer;
  1924. };
  1925. return While;
  1926. })(Base);
  1927. exports.Op = Op = (function(_super) {
  1928. var CONVERSIONS, INVERSIONS;
  1929. __extends(Op, _super);
  1930. function Op(op, first, second, flip) {
  1931. if (op === 'in') {
  1932. return new In(first, second);
  1933. }
  1934. if (op === 'do') {
  1935. return this.generateDo(first);
  1936. }
  1937. if (op === 'new') {
  1938. if (first instanceof Call && !first["do"] && !first.isNew) {
  1939. return first.newInstance();
  1940. }
  1941. if (first instanceof Code && first.bound || first["do"]) {
  1942. first = new Parens(first);
  1943. }
  1944. }
  1945. this.operator = CONVERSIONS[op] || op;
  1946. this.first = first;
  1947. this.second = second;
  1948. this.flip = !!flip;
  1949. return this;
  1950. }
  1951. CONVERSIONS = {
  1952. '==': '===',
  1953. '!=': '!==',
  1954. 'of': 'in'
  1955. };
  1956. INVERSIONS = {
  1957. '!==': '===',
  1958. '===': '!=='
  1959. };
  1960. Op.prototype.children = ['first', 'second'];
  1961. Op.prototype.isSimpleNumber = NO;
  1962. Op.prototype.isUnary = function() {
  1963. return !this.second;
  1964. };
  1965. Op.prototype.isComplex = function() {
  1966. var _ref4;
  1967. return !(this.isUnary() && ((_ref4 = this.operator) === '+' || _ref4 === '-')) || this.first.isComplex();
  1968. };
  1969. Op.prototype.isChainable = function() {
  1970. var _ref4;
  1971. return (_ref4 = this.operator) === '<' || _ref4 === '>' || _ref4 === '>=' || _ref4 === '<=' || _ref4 === '===' || _ref4 === '!==';
  1972. };
  1973. Op.prototype.invert = function() {
  1974. var allInvertable, curr, fst, op, _ref4;
  1975. if (this.isChainable() && this.first.isChainable()) {
  1976. allInvertable = true;
  1977. curr = this;
  1978. while (curr && curr.operator) {
  1979. allInvertable && (allInvertable = curr.operator in INVERSIONS);
  1980. curr = curr.first;
  1981. }
  1982. if (!allInvertable) {
  1983. return new Parens(this).invert();
  1984. }
  1985. curr = this;
  1986. while (curr && curr.operator) {
  1987. curr.invert = !curr.invert;
  1988. curr.operator = INVERSIONS[curr.operator];
  1989. curr = curr.first;
  1990. }
  1991. return this;
  1992. } else if (op = INVERSIONS[this.operator]) {
  1993. this.operator = op;
  1994. if (this.first.unwrap() instanceof Op) {
  1995. this.first.invert();
  1996. }
  1997. return this;
  1998. } else if (this.second) {
  1999. return new Parens(this).invert();
  2000. } else if (this.operator === '!' && (fst = this.first.unwrap()) instanceof Op && ((_ref4 = fst.operator) === '!' || _ref4 === 'in' || _ref4 === 'instanceof')) {
  2001. return fst;
  2002. } else {
  2003. return new Op('!', this);
  2004. }
  2005. };
  2006. Op.prototype.unfoldSoak = function(o) {
  2007. var _ref4;
  2008. return ((_ref4 = this.operator) === '++' || _ref4 === '--' || _ref4 === 'delete') && unfoldSoak(o, this, 'first');
  2009. };
  2010. Op.prototype.generateDo = function(exp) {
  2011. var call, func, param, passedParams, ref, _i, _len, _ref4;
  2012. passedParams = [];
  2013. func = exp instanceof Assign && (ref = exp.value.unwrap()) instanceof Code ? ref : exp;
  2014. _ref4 = func.params || [];
  2015. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  2016. param = _ref4[_i];
  2017. if (param.value) {
  2018. passedParams.push(param.value);
  2019. delete param.value;
  2020. } else {
  2021. passedParams.push(param);
  2022. }
  2023. }
  2024. call = new Call(exp, passedParams);
  2025. call["do"] = true;
  2026. return call;
  2027. };
  2028. Op.prototype.compileNode = function(o) {
  2029. var answer, isChain, _ref4, _ref5;
  2030. isChain = this.isChainable() && this.first.isChainable();
  2031. if (!isChain) {
  2032. this.first.front = this.front;
  2033. }
  2034. if (this.operator === 'delete' && o.scope.check(this.first.unwrapAll().value)) {
  2035. this.error('delete operand may not be argument or var');
  2036. }
  2037. if (((_ref4 = this.operator) === '--' || _ref4 === '++') && (_ref5 = this.first.unwrapAll().value, __indexOf.call(STRICT_PROSCRIBED, _ref5) >= 0)) {
  2038. this.error("cannot increment/decrement \"" + (this.first.unwrapAll().value) + "\"");
  2039. }
  2040. if (this.isUnary()) {
  2041. return this.compileUnary(o);
  2042. }
  2043. if (isChain) {
  2044. return this.compileChain(o);
  2045. }
  2046. if (this.operator === '?') {
  2047. return this.compileExistence(o);
  2048. }
  2049. answer = [].concat(this.first.compileToFragments(o, LEVEL_OP), this.makeCode(' ' + this.operator + ' '), this.second.compileToFragments(o, LEVEL_OP));
  2050. if (o.level <= LEVEL_OP) {
  2051. return answer;
  2052. } else {
  2053. return this.wrapInBraces(answer);
  2054. }
  2055. };
  2056. Op.prototype.compileChain = function(o) {
  2057. var fragments, fst, shared, _ref4;
  2058. _ref4 = this.first.second.cache(o), this.first.second = _ref4[0], shared = _ref4[1];
  2059. fst = this.first.compileToFragments(o, LEVEL_OP);
  2060. fragments = fst.concat(this.makeCode(" " + (this.invert ? '&&' : '||') + " "), shared.compileToFragments(o), this.makeCode(" " + this.operator + " "), this.second.compileToFragments(o, LEVEL_OP));
  2061. return this.wrapInBraces(fragments);
  2062. };
  2063. Op.prototype.compileExistence = function(o) {
  2064. var fst, ref;
  2065. if (!o.isExistentialEquals && this.first.isComplex()) {
  2066. ref = new Literal(o.scope.freeVariable('ref'));
  2067. fst = new Parens(new Assign(ref, this.first));
  2068. } else {
  2069. fst = this.first;
  2070. ref = fst;
  2071. }
  2072. return new If(new Existence(fst), ref, {
  2073. type: 'if'
  2074. }).addElse(this.second).compileToFragments(o);
  2075. };
  2076. Op.prototype.compileUnary = function(o) {
  2077. var op, parts, plusMinus;
  2078. parts = [];
  2079. op = this.operator;
  2080. parts.push([this.makeCode(op)]);
  2081. if (op === '!' && this.first instanceof Existence) {
  2082. this.first.negated = !this.first.negated;
  2083. return this.first.compileToFragments(o);
  2084. }
  2085. if (o.level >= LEVEL_ACCESS) {
  2086. return (new Parens(this)).compileToFragments(o);
  2087. }
  2088. plusMinus = op === '+' || op === '-';
  2089. if ((op === 'new' || op === 'typeof' || op === 'delete') || plusMinus && this.first instanceof Op && this.first.operator === op) {
  2090. parts.push([this.makeCode(' ')]);
  2091. }
  2092. if ((plusMinus && this.first instanceof Op) || (op === 'new' && this.first.isStatement(o))) {
  2093. this.first = new Parens(this.first);
  2094. }
  2095. parts.push(this.first.compileToFragments(o, LEVEL_OP));
  2096. if (this.flip) {
  2097. parts.reverse();
  2098. }
  2099. return this.joinFragmentArrays(parts, '');
  2100. };
  2101. Op.prototype.toString = function(idt) {
  2102. return Op.__super__.toString.call(this, idt, this.constructor.name + ' ' + this.operator);
  2103. };
  2104. return Op;
  2105. })(Base);
  2106. exports.In = In = (function(_super) {
  2107. __extends(In, _super);
  2108. function In(object, array) {
  2109. this.object = object;
  2110. this.array = array;
  2111. }
  2112. In.prototype.children = ['object', 'array'];
  2113. In.prototype.invert = NEGATE;
  2114. In.prototype.compileNode = function(o) {
  2115. var hasSplat, obj, _i, _len, _ref4;
  2116. if (this.array instanceof Value && this.array.isArray()) {
  2117. _ref4 = this.array.base.objects;
  2118. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  2119. obj = _ref4[_i];
  2120. if (!(obj instanceof Splat)) {
  2121. continue;
  2122. }
  2123. hasSplat = true;
  2124. break;
  2125. }
  2126. if (!hasSplat) {
  2127. return this.compileOrTest(o);
  2128. }
  2129. }
  2130. return this.compileLoopTest(o);
  2131. };
  2132. In.prototype.compileOrTest = function(o) {
  2133. var cmp, cnj, i, item, ref, sub, tests, _i, _len, _ref4, _ref5, _ref6;
  2134. if (this.array.base.objects.length === 0) {
  2135. return [this.makeCode("" + (!!this.negated))];
  2136. }
  2137. _ref4 = this.object.cache(o, LEVEL_OP), sub = _ref4[0], ref = _ref4[1];
  2138. _ref5 = this.negated ? [' !== ', ' && '] : [' === ', ' || '], cmp = _ref5[0], cnj = _ref5[1];
  2139. tests = [];
  2140. _ref6 = this.array.base.objects;
  2141. for (i = _i = 0, _len = _ref6.length; _i < _len; i = ++_i) {
  2142. item = _ref6[i];
  2143. if (i) {
  2144. tests.push(this.makeCode(cnj));
  2145. }
  2146. tests = tests.concat((i ? ref : sub), this.makeCode(cmp), item.compileToFragments(o, LEVEL_ACCESS));
  2147. }
  2148. if (o.level < LEVEL_OP) {
  2149. return tests;
  2150. } else {
  2151. return this.wrapInBraces(tests);
  2152. }
  2153. };
  2154. In.prototype.compileLoopTest = function(o) {
  2155. var fragments, ref, sub, _ref4;
  2156. _ref4 = this.object.cache(o, LEVEL_LIST), sub = _ref4[0], ref = _ref4[1];
  2157. fragments = [].concat(this.makeCode(utility('indexOf') + ".call("), this.array.compileToFragments(o, LEVEL_LIST), this.makeCode(", "), ref, this.makeCode(") " + (this.negated ? '< 0' : '>= 0')));
  2158. if (fragmentsToText(sub) === fragmentsToText(ref)) {
  2159. return fragments;
  2160. }
  2161. fragments = sub.concat(this.makeCode(', '), fragments);
  2162. if (o.level < LEVEL_LIST) {
  2163. return fragments;
  2164. } else {
  2165. return this.wrapInBraces(fragments);
  2166. }
  2167. };
  2168. In.prototype.toString = function(idt) {
  2169. return In.__super__.toString.call(this, idt, this.constructor.name + (this.negated ? '!' : ''));
  2170. };
  2171. return In;
  2172. })(Base);
  2173. exports.Try = Try = (function(_super) {
  2174. __extends(Try, _super);
  2175. function Try(attempt, errorVariable, recovery, ensure) {
  2176. this.attempt = attempt;
  2177. this.errorVariable = errorVariable;
  2178. this.recovery = recovery;
  2179. this.ensure = ensure;
  2180. }
  2181. Try.prototype.children = ['attempt', 'recovery', 'ensure'];
  2182. Try.prototype.isStatement = YES;
  2183. Try.prototype.jumps = function(o) {
  2184. var _ref4;
  2185. return this.attempt.jumps(o) || ((_ref4 = this.recovery) != null ? _ref4.jumps(o) : void 0);
  2186. };
  2187. Try.prototype.makeReturn = function(res) {
  2188. if (this.attempt) {
  2189. this.attempt = this.attempt.makeReturn(res);
  2190. }
  2191. if (this.recovery) {
  2192. this.recovery = this.recovery.makeReturn(res);
  2193. }
  2194. return this;
  2195. };
  2196. Try.prototype.compileNode = function(o) {
  2197. var catchPart, ensurePart, placeholder, tryPart;
  2198. o.indent += TAB;
  2199. tryPart = this.attempt.compileToFragments(o, LEVEL_TOP);
  2200. catchPart = this.recovery ? (placeholder = new Literal('_error'), this.errorVariable ? this.recovery.unshift(new Assign(this.errorVariable, placeholder)) : void 0, [].concat(this.makeCode(" catch ("), placeholder.compileToFragments(o), this.makeCode(") {\n"), this.recovery.compileToFragments(o, LEVEL_TOP), this.makeCode("\n" + this.tab + "}"))) : !(this.ensure || this.recovery) ? [this.makeCode(' catch (_error) {}')] : [];
  2201. ensurePart = this.ensure ? [].concat(this.makeCode(" finally {\n"), this.ensure.compileToFragments(o, LEVEL_TOP), this.makeCode("\n" + this.tab + "}")) : [];
  2202. return [].concat(this.makeCode("" + this.tab + "try {\n"), tryPart, this.makeCode("\n" + this.tab + "}"), catchPart, ensurePart);
  2203. };
  2204. return Try;
  2205. })(Base);
  2206. exports.Throw = Throw = (function(_super) {
  2207. __extends(Throw, _super);
  2208. function Throw(expression) {
  2209. this.expression = expression;
  2210. }
  2211. Throw.prototype.children = ['expression'];
  2212. Throw.prototype.isStatement = YES;
  2213. Throw.prototype.jumps = NO;
  2214. Throw.prototype.makeReturn = THIS;
  2215. Throw.prototype.compileNode = function(o) {
  2216. return [].concat(this.makeCode(this.tab + "throw "), this.expression.compileToFragments(o), this.makeCode(";"));
  2217. };
  2218. return Throw;
  2219. })(Base);
  2220. exports.Existence = Existence = (function(_super) {
  2221. __extends(Existence, _super);
  2222. function Existence(expression) {
  2223. this.expression = expression;
  2224. }
  2225. Existence.prototype.children = ['expression'];
  2226. Existence.prototype.invert = NEGATE;
  2227. Existence.prototype.compileNode = function(o) {
  2228. var cmp, cnj, code, _ref4;
  2229. this.expression.front = this.front;
  2230. code = this.expression.compile(o, LEVEL_OP);
  2231. if (IDENTIFIER.test(code) && !o.scope.check(code)) {
  2232. _ref4 = this.negated ? ['===', '||'] : ['!==', '&&'], cmp = _ref4[0], cnj = _ref4[1];
  2233. code = "typeof " + code + " " + cmp + " \"undefined\" " + cnj + " " + code + " " + cmp + " null";
  2234. } else {
  2235. code = "" + code + " " + (this.negated ? '==' : '!=') + " null";
  2236. }
  2237. return [this.makeCode(o.level <= LEVEL_COND ? code : "(" + code + ")")];
  2238. };
  2239. return Existence;
  2240. })(Base);
  2241. exports.Parens = Parens = (function(_super) {
  2242. __extends(Parens, _super);
  2243. function Parens(body) {
  2244. this.body = body;
  2245. }
  2246. Parens.prototype.children = ['body'];
  2247. Parens.prototype.unwrap = function() {
  2248. return this.body;
  2249. };
  2250. Parens.prototype.isComplex = function() {
  2251. return this.body.isComplex();
  2252. };
  2253. Parens.prototype.compileNode = function(o) {
  2254. var bare, expr, fragments;
  2255. expr = this.body.unwrap();
  2256. if (expr instanceof Value && expr.isAtomic()) {
  2257. expr.front = this.front;
  2258. return expr.compileToFragments(o);
  2259. }
  2260. fragments = expr.compileToFragments(o, LEVEL_PAREN);
  2261. bare = o.level < LEVEL_OP && (expr instanceof Op || expr instanceof Call || (expr instanceof For && expr.returns));
  2262. if (bare) {
  2263. return fragments;
  2264. } else {
  2265. return this.wrapInBraces(fragments);
  2266. }
  2267. };
  2268. return Parens;
  2269. })(Base);
  2270. exports.For = For = (function(_super) {
  2271. __extends(For, _super);
  2272. function For(body, source) {
  2273. var _ref4;
  2274. this.source = source.source, this.guard = source.guard, this.step = source.step, this.name = source.name, this.index = source.index;
  2275. this.body = Block.wrap([body]);
  2276. this.own = !!source.own;
  2277. this.object = !!source.object;
  2278. if (this.object) {
  2279. _ref4 = [this.index, this.name], this.name = _ref4[0], this.index = _ref4[1];
  2280. }
  2281. if (this.index instanceof Value) {
  2282. this.index.error('index cannot be a pattern matching expression');
  2283. }
  2284. this.range = this.source instanceof Value && this.source.base instanceof Range && !this.source.properties.length;
  2285. this.pattern = this.name instanceof Value;
  2286. if (this.range && this.index) {
  2287. this.index.error('indexes do not apply to range loops');
  2288. }
  2289. if (this.range && this.pattern) {
  2290. this.name.error('cannot pattern match over range loops');
  2291. }
  2292. if (this.own && !this.object) {
  2293. this.index.error('cannot use own with for-in');
  2294. }
  2295. this.returns = false;
  2296. }
  2297. For.prototype.children = ['body', 'source', 'guard', 'step'];
  2298. For.prototype.compileNode = function(o) {
  2299. var body, bodyFragments, compare, compareDown, declare, declareDown, defPart, defPartFragments, down, forPartFragments, guardPart, idt1, increment, index, ivar, kvar, kvarAssign, lastJumps, lvar, name, namePart, ref, resultPart, returnResult, rvar, scope, source, step, stepNum, stepVar, svar, varPart, _ref4, _ref5;
  2300. body = Block.wrap([this.body]);
  2301. lastJumps = (_ref4 = last(body.expressions)) != null ? _ref4.jumps() : void 0;
  2302. if (lastJumps && lastJumps instanceof Return) {
  2303. this.returns = false;
  2304. }
  2305. source = this.range ? this.source.base : this.source;
  2306. scope = o.scope;
  2307. name = this.name && (this.name.compile(o, LEVEL_LIST));
  2308. index = this.index && (this.index.compile(o, LEVEL_LIST));
  2309. if (name && !this.pattern) {
  2310. scope.find(name);
  2311. }
  2312. if (index) {
  2313. scope.find(index);
  2314. }
  2315. if (this.returns) {
  2316. rvar = scope.freeVariable('results');
  2317. }
  2318. ivar = (this.object && index) || scope.freeVariable('i');
  2319. kvar = (this.range && name) || index || ivar;
  2320. kvarAssign = kvar !== ivar ? "" + kvar + " = " : "";
  2321. if (this.step && !this.range) {
  2322. _ref5 = this.cacheToCodeFragments(this.step.cache(o, LEVEL_LIST)), step = _ref5[0], stepVar = _ref5[1];
  2323. stepNum = stepVar.match(SIMPLENUM);
  2324. }
  2325. if (this.pattern) {
  2326. name = ivar;
  2327. }
  2328. varPart = '';
  2329. guardPart = '';
  2330. defPart = '';
  2331. idt1 = this.tab + TAB;
  2332. if (this.range) {
  2333. forPartFragments = source.compileToFragments(merge(o, {
  2334. index: ivar,
  2335. name: name,
  2336. step: this.step
  2337. }));
  2338. } else {
  2339. svar = this.source.compile(o, LEVEL_LIST);
  2340. if ((name || this.own) && !IDENTIFIER.test(svar)) {
  2341. defPart += "" + this.tab + (ref = scope.freeVariable('ref')) + " = " + svar + ";\n";
  2342. svar = ref;
  2343. }
  2344. if (name && !this.pattern) {
  2345. namePart = "" + name + " = " + svar + "[" + kvar + "]";
  2346. }
  2347. if (!this.object) {
  2348. if (step !== stepVar) {
  2349. defPart += "" + this.tab + step + ";\n";
  2350. }
  2351. if (!(this.step && stepNum && (down = +stepNum < 0))) {
  2352. lvar = scope.freeVariable('len');
  2353. }
  2354. declare = "" + kvarAssign + ivar + " = 0, " + lvar + " = " + svar + ".length";
  2355. declareDown = "" + kvarAssign + ivar + " = " + svar + ".length - 1";
  2356. compare = "" + ivar + " < " + lvar;
  2357. compareDown = "" + ivar + " >= 0";
  2358. if (this.step) {
  2359. if (stepNum) {
  2360. if (down) {
  2361. compare = compareDown;
  2362. declare = declareDown;
  2363. }
  2364. } else {
  2365. compare = "" + stepVar + " > 0 ? " + compare + " : " + compareDown;
  2366. declare = "(" + stepVar + " > 0 ? (" + declare + ") : " + declareDown + ")";
  2367. }
  2368. increment = "" + ivar + " += " + stepVar;
  2369. } else {
  2370. increment = "" + (kvar !== ivar ? "++" + ivar : "" + ivar + "++");
  2371. }
  2372. forPartFragments = [this.makeCode("" + declare + "; " + compare + "; " + kvarAssign + increment)];
  2373. }
  2374. }
  2375. if (this.returns) {
  2376. resultPart = "" + this.tab + rvar + " = [];\n";
  2377. returnResult = "\n" + this.tab + "return " + rvar + ";";
  2378. body.makeReturn(rvar);
  2379. }
  2380. if (this.guard) {
  2381. if (body.expressions.length > 1) {
  2382. body.expressions.unshift(new If((new Parens(this.guard)).invert(), new Literal("continue")));
  2383. } else {
  2384. if (this.guard) {
  2385. body = Block.wrap([new If(this.guard, body)]);
  2386. }
  2387. }
  2388. }
  2389. if (this.pattern) {
  2390. body.expressions.unshift(new Assign(this.name, new Literal("" + svar + "[" + kvar + "]")));
  2391. }
  2392. defPartFragments = [].concat(this.makeCode(defPart), this.pluckDirectCall(o, body));
  2393. if (namePart) {
  2394. varPart = "\n" + idt1 + namePart + ";";
  2395. }
  2396. if (this.object) {
  2397. forPartFragments = [this.makeCode("" + kvar + " in " + svar)];
  2398. if (this.own) {
  2399. guardPart = "\n" + idt1 + "if (!" + (utility('hasProp')) + ".call(" + svar + ", " + kvar + ")) continue;";
  2400. }
  2401. }
  2402. bodyFragments = body.compileToFragments(merge(o, {
  2403. indent: idt1
  2404. }), LEVEL_TOP);
  2405. if (bodyFragments && (bodyFragments.length > 0)) {
  2406. bodyFragments = [].concat(this.makeCode("\n"), bodyFragments, this.makeCode("\n"));
  2407. }
  2408. return [].concat(defPartFragments, this.makeCode("" + (resultPart || '') + this.tab + "for ("), forPartFragments, this.makeCode(") {" + guardPart + varPart), bodyFragments, this.makeCode("" + this.tab + "}" + (returnResult || '')));
  2409. };
  2410. For.prototype.pluckDirectCall = function(o, body) {
  2411. var base, defs, expr, fn, idx, ref, val, _i, _len, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9;
  2412. defs = [];
  2413. _ref4 = body.expressions;
  2414. for (idx = _i = 0, _len = _ref4.length; _i < _len; idx = ++_i) {
  2415. expr = _ref4[idx];
  2416. expr = expr.unwrapAll();
  2417. if (!(expr instanceof Call)) {
  2418. continue;
  2419. }
  2420. val = expr.variable.unwrapAll();
  2421. if (!((val instanceof Code) || (val instanceof Value && ((_ref5 = val.base) != null ? _ref5.unwrapAll() : void 0) instanceof Code && val.properties.length === 1 && ((_ref6 = (_ref7 = val.properties[0].name) != null ? _ref7.value : void 0) === 'call' || _ref6 === 'apply')))) {
  2422. continue;
  2423. }
  2424. fn = ((_ref8 = val.base) != null ? _ref8.unwrapAll() : void 0) || val;
  2425. ref = new Literal(o.scope.freeVariable('fn'));
  2426. base = new Value(ref);
  2427. if (val.base) {
  2428. _ref9 = [base, val], val.base = _ref9[0], base = _ref9[1];
  2429. }
  2430. body.expressions[idx] = new Call(base, expr.args);
  2431. defs = defs.concat(this.makeCode(this.tab), new Assign(ref, fn).compileToFragments(o, LEVEL_TOP), this.makeCode(';\n'));
  2432. }
  2433. return defs;
  2434. };
  2435. return For;
  2436. })(While);
  2437. exports.Switch = Switch = (function(_super) {
  2438. __extends(Switch, _super);
  2439. function Switch(subject, cases, otherwise) {
  2440. this.subject = subject;
  2441. this.cases = cases;
  2442. this.otherwise = otherwise;
  2443. }
  2444. Switch.prototype.children = ['subject', 'cases', 'otherwise'];
  2445. Switch.prototype.isStatement = YES;
  2446. Switch.prototype.jumps = function(o) {
  2447. var block, conds, _i, _len, _ref4, _ref5, _ref6;
  2448. if (o == null) {
  2449. o = {
  2450. block: true
  2451. };
  2452. }
  2453. _ref4 = this.cases;
  2454. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  2455. _ref5 = _ref4[_i], conds = _ref5[0], block = _ref5[1];
  2456. if (block.jumps(o)) {
  2457. return block;
  2458. }
  2459. }
  2460. return (_ref6 = this.otherwise) != null ? _ref6.jumps(o) : void 0;
  2461. };
  2462. Switch.prototype.makeReturn = function(res) {
  2463. var pair, _i, _len, _ref4, _ref5;
  2464. _ref4 = this.cases;
  2465. for (_i = 0, _len = _ref4.length; _i < _len; _i++) {
  2466. pair = _ref4[_i];
  2467. pair[1].makeReturn(res);
  2468. }
  2469. if (res) {
  2470. this.otherwise || (this.otherwise = new Block([new Literal('void 0')]));
  2471. }
  2472. if ((_ref5 = this.otherwise) != null) {
  2473. _ref5.makeReturn(res);
  2474. }
  2475. return this;
  2476. };
  2477. Switch.prototype.compileNode = function(o) {
  2478. var block, body, cond, conditions, expr, fragments, i, idt1, idt2, _i, _j, _len, _len1, _ref4, _ref5, _ref6;
  2479. idt1 = o.indent + TAB;
  2480. idt2 = o.indent = idt1 + TAB;
  2481. fragments = [].concat(this.makeCode(this.tab + "switch ("), (this.subject ? this.subject.compileToFragments(o, LEVEL_PAREN) : this.makeCode("false")), this.makeCode(") {\n"));
  2482. _ref4 = this.cases;
  2483. for (i = _i = 0, _len = _ref4.length; _i < _len; i = ++_i) {
  2484. _ref5 = _ref4[i], conditions = _ref5[0], block = _ref5[1];
  2485. _ref6 = flatten([conditions]);
  2486. for (_j = 0, _len1 = _ref6.length; _j < _len1; _j++) {
  2487. cond = _ref6[_j];
  2488. if (!this.subject) {
  2489. cond = cond.invert();
  2490. }
  2491. fragments = fragments.concat(this.makeCode(idt1 + "case "), cond.compileToFragments(o, LEVEL_PAREN), this.makeCode(":\n"));
  2492. }
  2493. if ((body = block.compileToFragments(o, LEVEL_TOP)).length > 0) {
  2494. fragments = fragments.concat(body, this.makeCode('\n'));
  2495. }
  2496. if (i === this.cases.length - 1 && !this.otherwise) {
  2497. break;
  2498. }
  2499. expr = this.lastNonComment(block.expressions);
  2500. if (expr instanceof Return || (expr instanceof Literal && expr.jumps() && expr.value !== 'debugger')) {
  2501. continue;
  2502. }
  2503. fragments.push(cond.makeCode(idt2 + 'break;\n'));
  2504. }
  2505. if (this.otherwise && this.otherwise.expressions.length) {
  2506. fragments.push.apply(fragments, [this.makeCode(idt1 + "default:\n")].concat(__slice.call(this.otherwise.compileToFragments(o, LEVEL_TOP)), [this.makeCode("\n")]));
  2507. }
  2508. fragments.push(this.makeCode(this.tab + '}'));
  2509. return fragments;
  2510. };
  2511. return Switch;
  2512. })(Base);
  2513. exports.If = If = (function(_super) {
  2514. __extends(If, _super);
  2515. function If(condition, body, options) {
  2516. this.body = body;
  2517. if (options == null) {
  2518. options = {};
  2519. }
  2520. this.condition = options.type === 'unless' ? condition.invert() : condition;
  2521. this.elseBody = null;
  2522. this.isChain = false;
  2523. this.soak = options.soak;
  2524. }
  2525. If.prototype.children = ['condition', 'body', 'elseBody'];
  2526. If.prototype.bodyNode = function() {
  2527. var _ref4;
  2528. return (_ref4 = this.body) != null ? _ref4.unwrap() : void 0;
  2529. };
  2530. If.prototype.elseBodyNode = function() {
  2531. var _ref4;
  2532. return (_ref4 = this.elseBody) != null ? _ref4.unwrap() : void 0;
  2533. };
  2534. If.prototype.addElse = function(elseBody) {
  2535. if (this.isChain) {
  2536. this.elseBodyNode().addElse(elseBody);
  2537. } else {
  2538. this.isChain = elseBody instanceof If;
  2539. this.elseBody = this.ensureBlock(elseBody);
  2540. this.elseBody.updateLocationDataIfMissing(elseBody.locationData);
  2541. }
  2542. return this;
  2543. };
  2544. If.prototype.isStatement = function(o) {
  2545. var _ref4;
  2546. return (o != null ? o.level : void 0) === LEVEL_TOP || this.bodyNode().isStatement(o) || ((_ref4 = this.elseBodyNode()) != null ? _ref4.isStatement(o) : void 0);
  2547. };
  2548. If.prototype.jumps = function(o) {
  2549. var _ref4;
  2550. return this.body.jumps(o) || ((_ref4 = this.elseBody) != null ? _ref4.jumps(o) : void 0);
  2551. };
  2552. If.prototype.compileNode = function(o) {
  2553. if (this.isStatement(o)) {
  2554. return this.compileStatement(o);
  2555. } else {
  2556. return this.compileExpression(o);
  2557. }
  2558. };
  2559. If.prototype.makeReturn = function(res) {
  2560. if (res) {
  2561. this.elseBody || (this.elseBody = new Block([new Literal('void 0')]));
  2562. }
  2563. this.body && (this.body = new Block([this.body.makeReturn(res)]));
  2564. this.elseBody && (this.elseBody = new Block([this.elseBody.makeReturn(res)]));
  2565. return this;
  2566. };
  2567. If.prototype.ensureBlock = function(node) {
  2568. if (node instanceof Block) {
  2569. return node;
  2570. } else {
  2571. return new Block([node]);
  2572. }
  2573. };
  2574. If.prototype.compileStatement = function(o) {
  2575. var answer, body, child, cond, exeq, ifPart, indent;
  2576. child = del(o, 'chainChild');
  2577. exeq = del(o, 'isExistentialEquals');
  2578. if (exeq) {
  2579. return new If(this.condition.invert(), this.elseBodyNode(), {
  2580. type: 'if'
  2581. }).compileToFragments(o);
  2582. }
  2583. indent = o.indent + TAB;
  2584. cond = this.condition.compileToFragments(o, LEVEL_PAREN);
  2585. body = this.ensureBlock(this.body).compileToFragments(merge(o, {
  2586. indent: indent
  2587. }));
  2588. ifPart = [].concat(this.makeCode("if ("), cond, this.makeCode(") {\n"), body, this.makeCode("\n" + this.tab + "}"));
  2589. if (!child) {
  2590. ifPart.unshift(this.makeCode(this.tab));
  2591. }
  2592. if (!this.elseBody) {
  2593. return ifPart;
  2594. }
  2595. answer = ifPart.concat(this.makeCode(' else '));
  2596. if (this.isChain) {
  2597. o.chainChild = true;
  2598. answer = answer.concat(this.elseBody.unwrap().compileToFragments(o, LEVEL_TOP));
  2599. } else {
  2600. answer = answer.concat(this.makeCode("{\n"), this.elseBody.compileToFragments(merge(o, {
  2601. indent: indent
  2602. }), LEVEL_TOP), this.makeCode("\n" + this.tab + "}"));
  2603. }
  2604. return answer;
  2605. };
  2606. If.prototype.compileExpression = function(o) {
  2607. var alt, body, cond, fragments;
  2608. cond = this.condition.compileToFragments(o, LEVEL_COND);
  2609. body = this.bodyNode().compileToFragments(o, LEVEL_LIST);
  2610. alt = this.elseBodyNode() ? this.elseBodyNode().compileToFragments(o, LEVEL_LIST) : [this.makeCode('void 0')];
  2611. fragments = cond.concat(this.makeCode(" ? "), body, this.makeCode(" : "), alt);
  2612. if (o.level >= LEVEL_COND) {
  2613. return this.wrapInBraces(fragments);
  2614. } else {
  2615. return fragments;
  2616. }
  2617. };
  2618. If.prototype.unfoldSoak = function() {
  2619. return this.soak && this;
  2620. };
  2621. return If;
  2622. })(Base);
  2623. Closure = {
  2624. wrap: function(expressions, statement, noReturn) {
  2625. var args, argumentsNode, call, func, meth;
  2626. if (expressions.jumps()) {
  2627. return expressions;
  2628. }
  2629. func = new Code([], Block.wrap([expressions]));
  2630. args = [];
  2631. argumentsNode = expressions.contains(this.isLiteralArguments);
  2632. if (argumentsNode && expressions.classBody) {
  2633. argumentsNode.error("Class bodies shouldn't reference arguments");
  2634. }
  2635. if (argumentsNode || expressions.contains(this.isLiteralThis)) {
  2636. meth = new Literal(argumentsNode ? 'apply' : 'call');
  2637. args = [new Literal('this')];
  2638. if (argumentsNode) {
  2639. args.push(new Literal('arguments'));
  2640. }
  2641. func = new Value(func, [new Access(meth)]);
  2642. }
  2643. func.noReturn = noReturn;
  2644. call = new Call(func, args);
  2645. if (statement) {
  2646. return Block.wrap([call]);
  2647. } else {
  2648. return call;
  2649. }
  2650. },
  2651. isLiteralArguments: function(node) {
  2652. return node instanceof Literal && node.value === 'arguments' && !node.asKey;
  2653. },
  2654. isLiteralThis: function(node) {
  2655. return (node instanceof Literal && node.value === 'this' && !node.asKey) || (node instanceof Code && node.bound) || (node instanceof Call && node.isSuper);
  2656. }
  2657. };
  2658. unfoldSoak = function(o, parent, name) {
  2659. var ifn;
  2660. if (!(ifn = parent[name].unfoldSoak(o))) {
  2661. return;
  2662. }
  2663. parent[name] = ifn.body;
  2664. ifn.body = new Value(parent);
  2665. return ifn;
  2666. };
  2667. UTILITIES = {
  2668. "extends": function() {
  2669. return "function(child, parent) { for (var key in parent) { if (" + (utility('hasProp')) + ".call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }";
  2670. },
  2671. bind: function() {
  2672. return 'function(fn, me){ return function(){ return fn.apply(me, arguments); }; }';
  2673. },
  2674. indexOf: function() {
  2675. return "[].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }";
  2676. },
  2677. hasProp: function() {
  2678. return '{}.hasOwnProperty';
  2679. },
  2680. slice: function() {
  2681. return '[].slice';
  2682. }
  2683. };
  2684. LEVEL_TOP = 1;
  2685. LEVEL_PAREN = 2;
  2686. LEVEL_LIST = 3;
  2687. LEVEL_COND = 4;
  2688. LEVEL_OP = 5;
  2689. LEVEL_ACCESS = 6;
  2690. TAB = ' ';
  2691. IDENTIFIER_STR = "[$A-Za-z_\\x7f-\\uffff][$\\w\\x7f-\\uffff]*";
  2692. IDENTIFIER = RegExp("^" + IDENTIFIER_STR + "$");
  2693. SIMPLENUM = /^[+-]?\d+$/;
  2694. METHOD_DEF = RegExp("^(?:(" + IDENTIFIER_STR + ")\\.prototype(?:\\.(" + IDENTIFIER_STR + ")|\\[(\"(?:[^\\\\\"\\r\\n]|\\\\.)*\"|'(?:[^\\\\'\\r\\n]|\\\\.)*')\\]|\\[(0x[\\da-fA-F]+|\\d*\\.?\\d+(?:[eE][+-]?\\d+)?)\\]))|(" + IDENTIFIER_STR + ")$");
  2695. IS_STRING = /^['"]/;
  2696. utility = function(name) {
  2697. var ref;
  2698. ref = "__" + name;
  2699. Scope.root.assign(ref, UTILITIES[name]());
  2700. return ref;
  2701. };
  2702. multident = function(code, tab) {
  2703. code = code.replace(/\n/g, '$&' + tab);
  2704. return code.replace(/\s+$/, '');
  2705. };
  2706. });