PageRenderTime 75ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/coffee-script/nodes.js

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