nodes.js 108 KB

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