spade 0.0.8.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (346) hide show
  1. data/.gitignore +0 -1
  2. data/.gitmodules +4 -4
  3. data/README.md +4 -0
  4. data/bin/spade +1 -1
  5. data/lib/spade.rb +0 -51
  6. data/lib/spade/cli.rb +10 -4
  7. data/lib/spade/version.rb +1 -1
  8. data/spade.gemspec +4 -21
  9. metadata +13 -482
  10. data/Gemfile +0 -3
  11. data/Gemfile.lock +0 -40
  12. data/lib/index.js +0 -14
  13. data/lib/node/loader.js +0 -146
  14. data/lib/node/sandbox.js +0 -44
  15. data/lib/spade.js +0 -1130
  16. data/lib/spade/bundle.rb +0 -171
  17. data/lib/spade/cli/base.rb +0 -354
  18. data/lib/spade/cli/owner.rb +0 -45
  19. data/lib/spade/cli/project_generator.rb +0 -58
  20. data/lib/spade/compiler.rb +0 -34
  21. data/lib/spade/console.rb +0 -39
  22. data/lib/spade/context.rb +0 -116
  23. data/lib/spade/credentials.rb +0 -36
  24. data/lib/spade/dependency_installer.rb +0 -103
  25. data/lib/spade/environment.rb +0 -35
  26. data/lib/spade/exports.rb +0 -71
  27. data/lib/spade/installer.rb +0 -40
  28. data/lib/spade/loader.rb +0 -238
  29. data/lib/spade/local.rb +0 -46
  30. data/lib/spade/package.rb +0 -157
  31. data/lib/spade/reactor.rb +0 -159
  32. data/lib/spade/remote.rb +0 -99
  33. data/lib/spade/repository.rb +0 -18
  34. data/lib/spade/server.rb +0 -66
  35. data/lib/spade/shell.rb +0 -36
  36. data/lib/spade/templates/project/LICENSE +0 -19
  37. data/lib/spade/templates/project/README.md +0 -21
  38. data/lib/spade/templates/project/lib/main.js +0 -9
  39. data/lib/spade/templates/project/project.json +0 -31
  40. data/lib/spade/templates/project/tests/main-test.js +0 -8
  41. data/package.json +0 -27
  42. data/packages/coffee-script/.gitignore +0 -8
  43. data/packages/coffee-script/.npmignore +0 -11
  44. data/packages/coffee-script/Cakefile +0 -229
  45. data/packages/coffee-script/LICENSE +0 -22
  46. data/packages/coffee-script/README +0 -47
  47. data/packages/coffee-script/Rakefile +0 -78
  48. data/packages/coffee-script/bin/cake +0 -7
  49. data/packages/coffee-script/bin/coffee +0 -7
  50. data/packages/coffee-script/documentation/coffee/aliases.coffee +0 -11
  51. data/packages/coffee-script/documentation/coffee/array_comprehensions.coffee +0 -2
  52. data/packages/coffee-script/documentation/coffee/block_comment.coffee +0 -6
  53. data/packages/coffee-script/documentation/coffee/cake_tasks.coffee +0 -9
  54. data/packages/coffee-script/documentation/coffee/classes.coffee +0 -25
  55. data/packages/coffee-script/documentation/coffee/comparisons.coffee +0 -5
  56. data/packages/coffee-script/documentation/coffee/conditionals.coffee +0 -13
  57. data/packages/coffee-script/documentation/coffee/default_args.coffee +0 -8
  58. data/packages/coffee-script/documentation/coffee/do.coffee +0 -4
  59. data/packages/coffee-script/documentation/coffee/embedded.coffee +0 -5
  60. data/packages/coffee-script/documentation/coffee/existence.coffee +0 -10
  61. data/packages/coffee-script/documentation/coffee/expressions.coffee +0 -9
  62. data/packages/coffee-script/documentation/coffee/expressions_assignment.coffee +0 -3
  63. data/packages/coffee-script/documentation/coffee/expressions_comprehension.coffee +0 -3
  64. data/packages/coffee-script/documentation/coffee/expressions_try.coffee +0 -7
  65. data/packages/coffee-script/documentation/coffee/fat_arrow.coffee +0 -6
  66. data/packages/coffee-script/documentation/coffee/functions.coffee +0 -2
  67. data/packages/coffee-script/documentation/coffee/heredocs.coffee +0 -7
  68. data/packages/coffee-script/documentation/coffee/heregexes.coffee +0 -11
  69. data/packages/coffee-script/documentation/coffee/interpolation.coffee +0 -6
  70. data/packages/coffee-script/documentation/coffee/multiple_return_values.coffee +0 -7
  71. data/packages/coffee-script/documentation/coffee/object_comprehensions.coffee +0 -4
  72. data/packages/coffee-script/documentation/coffee/object_extraction.coffee +0 -13
  73. data/packages/coffee-script/documentation/coffee/objects_and_arrays.coffee +0 -19
  74. data/packages/coffee-script/documentation/coffee/objects_reserved.coffee +0 -5
  75. data/packages/coffee-script/documentation/coffee/overview.coffee +0 -28
  76. data/packages/coffee-script/documentation/coffee/parallel_assignment.coffee +0 -6
  77. data/packages/coffee-script/documentation/coffee/patterns_and_splats.coffee +0 -7
  78. data/packages/coffee-script/documentation/coffee/prototypes.coffee +0 -3
  79. data/packages/coffee-script/documentation/coffee/range_comprehensions.coffee +0 -2
  80. data/packages/coffee-script/documentation/coffee/scope.coffee +0 -5
  81. data/packages/coffee-script/documentation/coffee/slices.coffee +0 -7
  82. data/packages/coffee-script/documentation/coffee/soaks.coffee +0 -1
  83. data/packages/coffee-script/documentation/coffee/splats.coffee +0 -27
  84. data/packages/coffee-script/documentation/coffee/splices.coffee +0 -5
  85. data/packages/coffee-script/documentation/coffee/strings.coffee +0 -8
  86. data/packages/coffee-script/documentation/coffee/switch.coffee +0 -10
  87. data/packages/coffee-script/documentation/coffee/try.coffee +0 -8
  88. data/packages/coffee-script/documentation/coffee/while.coffee +0 -10
  89. data/packages/coffee-script/documentation/css/docs.css +0 -374
  90. data/packages/coffee-script/documentation/css/idle.css +0 -64
  91. data/packages/coffee-script/documentation/docs/browser.html +0 -25
  92. data/packages/coffee-script/documentation/docs/cake.html +0 -43
  93. data/packages/coffee-script/documentation/docs/coffee-script.html +0 -51
  94. data/packages/coffee-script/documentation/docs/command.html +0 -161
  95. data/packages/coffee-script/documentation/docs/docco.css +0 -186
  96. data/packages/coffee-script/documentation/docs/grammar.html +0 -399
  97. data/packages/coffee-script/documentation/docs/helpers.html +0 -31
  98. data/packages/coffee-script/documentation/docs/index.html +0 -3
  99. data/packages/coffee-script/documentation/docs/lexer.html +0 -490
  100. data/packages/coffee-script/documentation/docs/nodes.html +0 -1338
  101. data/packages/coffee-script/documentation/docs/optparse.html +0 -78
  102. data/packages/coffee-script/documentation/docs/repl.html +0 -24
  103. data/packages/coffee-script/documentation/docs/rewriter.html +0 -251
  104. data/packages/coffee-script/documentation/docs/scope.html +0 -54
  105. data/packages/coffee-script/documentation/docs/underscore.html +0 -295
  106. data/packages/coffee-script/documentation/images/background.png +0 -0
  107. data/packages/coffee-script/documentation/images/banding.png +0 -0
  108. data/packages/coffee-script/documentation/images/button_bg.png +0 -0
  109. data/packages/coffee-script/documentation/images/button_bg_dark.gif +0 -0
  110. data/packages/coffee-script/documentation/images/button_bg_green.gif +0 -0
  111. data/packages/coffee-script/documentation/images/favicon.ico +0 -0
  112. data/packages/coffee-script/documentation/images/logo.png +0 -0
  113. data/packages/coffee-script/documentation/images/screenshadow.png +0 -0
  114. data/packages/coffee-script/documentation/index.html.erb +0 -1607
  115. data/packages/coffee-script/documentation/js/aliases.js +0 -17
  116. data/packages/coffee-script/documentation/js/array_comprehensions.js +0 -6
  117. data/packages/coffee-script/documentation/js/block_comment.js +0 -4
  118. data/packages/coffee-script/documentation/js/cake_tasks.js +0 -10
  119. data/packages/coffee-script/documentation/js/classes.js +0 -44
  120. data/packages/coffee-script/documentation/js/comparisons.js +0 -3
  121. data/packages/coffee-script/documentation/js/conditionals.js +0 -12
  122. data/packages/coffee-script/documentation/js/default_args.js +0 -7
  123. data/packages/coffee-script/documentation/js/do.js +0 -10
  124. data/packages/coffee-script/documentation/js/embedded.js +0 -4
  125. data/packages/coffee-script/documentation/js/existence.js +0 -6
  126. data/packages/coffee-script/documentation/js/expressions.js +0 -15
  127. data/packages/coffee-script/documentation/js/expressions_assignment.js +0 -2
  128. data/packages/coffee-script/documentation/js/expressions_comprehension.js +0 -9
  129. data/packages/coffee-script/documentation/js/expressions_try.js +0 -7
  130. data/packages/coffee-script/documentation/js/fat_arrow.js +0 -9
  131. data/packages/coffee-script/documentation/js/functions.js +0 -7
  132. data/packages/coffee-script/documentation/js/heredocs.js +0 -2
  133. data/packages/coffee-script/documentation/js/heregexes.js +0 -2
  134. data/packages/coffee-script/documentation/js/interpolation.js +0 -4
  135. data/packages/coffee-script/documentation/js/multiple_return_values.js +0 -5
  136. data/packages/coffee-script/documentation/js/object_comprehensions.js +0 -15
  137. data/packages/coffee-script/documentation/js/object_extraction.js +0 -10
  138. data/packages/coffee-script/documentation/js/objects_and_arrays.js +0 -17
  139. data/packages/coffee-script/documentation/js/objects_reserved.js +0 -4
  140. data/packages/coffee-script/documentation/js/overview.js +0 -35
  141. data/packages/coffee-script/documentation/js/parallel_assignment.js +0 -4
  142. data/packages/coffee-script/documentation/js/patterns_and_splats.js +0 -4
  143. data/packages/coffee-script/documentation/js/prototypes.js +0 -3
  144. data/packages/coffee-script/documentation/js/range_comprehensions.js +0 -9
  145. data/packages/coffee-script/documentation/js/scope.js +0 -8
  146. data/packages/coffee-script/documentation/js/slices.js +0 -4
  147. data/packages/coffee-script/documentation/js/soaks.js +0 -2
  148. data/packages/coffee-script/documentation/js/splats.js +0 -15
  149. data/packages/coffee-script/documentation/js/splices.js +0 -3
  150. data/packages/coffee-script/documentation/js/strings.js +0 -2
  151. data/packages/coffee-script/documentation/js/switch.js +0 -23
  152. data/packages/coffee-script/documentation/js/try.js +0 -8
  153. data/packages/coffee-script/documentation/js/while.js +0 -18
  154. data/packages/coffee-script/documentation/vendor/jquery-1.4.2.js +0 -6240
  155. data/packages/coffee-script/examples/beautiful_code/binary_search.coffee +0 -16
  156. data/packages/coffee-script/examples/beautiful_code/quicksort_runtime.coffee +0 -13
  157. data/packages/coffee-script/examples/beautiful_code/regular_expression_matcher.coffee +0 -34
  158. data/packages/coffee-script/examples/blocks.coffee +0 -54
  159. data/packages/coffee-script/examples/code.coffee +0 -167
  160. data/packages/coffee-script/examples/computer_science/README +0 -4
  161. data/packages/coffee-script/examples/computer_science/binary_search.coffee +0 -25
  162. data/packages/coffee-script/examples/computer_science/bubble_sort.coffee +0 -11
  163. data/packages/coffee-script/examples/computer_science/linked_list.coffee +0 -108
  164. data/packages/coffee-script/examples/computer_science/luhn_algorithm.coffee +0 -36
  165. data/packages/coffee-script/examples/computer_science/merge_sort.coffee +0 -19
  166. data/packages/coffee-script/examples/computer_science/selection_sort.coffee +0 -23
  167. data/packages/coffee-script/examples/poignant.coffee +0 -181
  168. data/packages/coffee-script/examples/potion.coffee +0 -206
  169. data/packages/coffee-script/examples/underscore.coffee +0 -682
  170. data/packages/coffee-script/examples/web_server.coffee +0 -12
  171. data/packages/coffee-script/extras/EXTRAS +0 -7
  172. data/packages/coffee-script/extras/coffee-script.js +0 -8
  173. data/packages/coffee-script/extras/jsl.conf +0 -44
  174. data/packages/coffee-script/index.html +0 -2515
  175. data/packages/coffee-script/lib/browser.js +0 -52
  176. data/packages/coffee-script/lib/cake.js +0 -76
  177. data/packages/coffee-script/lib/coffee-script.js +0 -82
  178. data/packages/coffee-script/lib/command.js +0 -263
  179. data/packages/coffee-script/lib/grammar.js +0 -581
  180. data/packages/coffee-script/lib/helpers.js +0 -66
  181. data/packages/coffee-script/lib/index.js +0 -8
  182. data/packages/coffee-script/lib/lexer.js +0 -633
  183. data/packages/coffee-script/lib/nodes.js +0 -2165
  184. data/packages/coffee-script/lib/optparse.js +0 -111
  185. data/packages/coffee-script/lib/parser.js +0 -649
  186. data/packages/coffee-script/lib/repl.js +0 -42
  187. data/packages/coffee-script/lib/rewriter.js +0 -353
  188. data/packages/coffee-script/lib/scope.js +0 -120
  189. data/packages/coffee-script/lib/spade-format.js +0 -45
  190. data/packages/coffee-script/package.json +0 -26
  191. data/packages/coffee-script/src/browser.coffee +0 -43
  192. data/packages/coffee-script/src/cake.coffee +0 -69
  193. data/packages/coffee-script/src/coffee-script.coffee +0 -92
  194. data/packages/coffee-script/src/command.coffee +0 -214
  195. data/packages/coffee-script/src/grammar.coffee +0 -590
  196. data/packages/coffee-script/src/helpers.coffee +0 -56
  197. data/packages/coffee-script/src/index.coffee +0 -2
  198. data/packages/coffee-script/src/lexer.coffee +0 -653
  199. data/packages/coffee-script/src/nodes.coffee +0 -1754
  200. data/packages/coffee-script/src/optparse.coffee +0 -99
  201. data/packages/coffee-script/src/repl.coffee +0 -42
  202. data/packages/coffee-script/src/rewriter.coffee +0 -326
  203. data/packages/coffee-script/src/scope.coffee +0 -94
  204. data/packages/coffee-script/test/arguments.coffee +0 -127
  205. data/packages/coffee-script/test/assignment.coffee +0 -98
  206. data/packages/coffee-script/test/break.coffee +0 -18
  207. data/packages/coffee-script/test/comments.coffee +0 -201
  208. data/packages/coffee-script/test/conditionals.coffee +0 -181
  209. data/packages/coffee-script/test/exception_handling.coffee +0 -90
  210. data/packages/coffee-script/test/helpers.coffee +0 -96
  211. data/packages/coffee-script/test/importing.coffee +0 -18
  212. data/packages/coffee-script/test/operators.coffee +0 -225
  213. data/packages/coffee-script/test/ranges_slices_and_splices.coffee +0 -186
  214. data/packages/coffee-script/test/regular_expressions.coffee +0 -56
  215. data/packages/coffee-script/test/test.html +0 -123
  216. data/packages/coffee-script/test/test_chaining.coffee +0 -77
  217. data/packages/coffee-script/test/test_classes.coffee +0 -372
  218. data/packages/coffee-script/test/test_compilation.coffee +0 -26
  219. data/packages/coffee-script/test/test_comprehensions.coffee +0 -318
  220. data/packages/coffee-script/test/test_existence.coffee +0 -165
  221. data/packages/coffee-script/test/test_functions.coffee +0 -379
  222. data/packages/coffee-script/test/test_heredocs.coffee +0 -111
  223. data/packages/coffee-script/test/test_literals.coffee +0 -270
  224. data/packages/coffee-script/test/test_option_parser.coffee +0 -27
  225. data/packages/coffee-script/test/test_pattern_matching.coffee +0 -162
  226. data/packages/coffee-script/test/test_returns.coffee +0 -63
  227. data/packages/coffee-script/test/test_splats.coffee +0 -102
  228. data/packages/coffee-script/test/test_strings.coffee +0 -118
  229. data/packages/coffee-script/test/test_switch.coffee +0 -103
  230. data/packages/coffee-script/test/test_while.coffee +0 -71
  231. data/packages/ivory/LICENSE.txt +0 -1
  232. data/packages/ivory/README.md +0 -19
  233. data/packages/ivory/lib/buffer.js +0 -111
  234. data/packages/ivory/lib/events.js +0 -137
  235. data/packages/ivory/lib/fs.js +0 -266
  236. data/packages/ivory/lib/main.js +0 -13
  237. data/packages/ivory/lib/path.js +0 -158
  238. data/packages/ivory/lib/ruby/buffer.rb +0 -145
  239. data/packages/ivory/lib/ruby/constants.rb +0 -585
  240. data/packages/ivory/lib/ruby/events.rb +0 -32
  241. data/packages/ivory/lib/ruby/fs.rb +0 -245
  242. data/packages/ivory/lib/ruby/process.rb +0 -28
  243. data/packages/ivory/lib/stream.js +0 -115
  244. data/packages/ivory/lib/util.js +0 -414
  245. data/packages/ivory/package.json +0 -11
  246. data/packages/jquery/main.js +0 -7179
  247. data/packages/jquery/package.json +0 -10
  248. data/packages/json/lib/main.js +0 -14
  249. data/packages/json/package.json +0 -8
  250. data/packages/lproj/README.md +0 -77
  251. data/packages/lproj/examples/demo-app/en.lproj/localized.strings +0 -2
  252. data/packages/lproj/examples/demo-app/fr.lproj/localized.strings +0 -3
  253. data/packages/lproj/examples/demo-app/index.html +0 -8
  254. data/packages/lproj/examples/demo-app/lib/main.js +0 -7
  255. data/packages/lproj/examples/demo-app/package.json +0 -9
  256. data/packages/lproj/lib/main.js +0 -78
  257. data/packages/lproj/lib/strings-format.js +0 -6
  258. data/packages/lproj/package.json +0 -9
  259. data/packages/optparse/README.md +0 -161
  260. data/packages/optparse/TODO +0 -1
  261. data/packages/optparse/examples/browser-test.html +0 -75
  262. data/packages/optparse/examples/nodejs-test.js +0 -90
  263. data/packages/optparse/lib/optparse.js +0 -309
  264. data/packages/optparse/package.json +0 -13
  265. data/packages/optparse/seed.yml +0 -5
  266. data/packages/text/lib/main.js +0 -8
  267. data/packages/text/package.json +0 -9
  268. data/packages/web-file/README.md +0 -7
  269. data/packages/web-file/lib/errors.js +0 -32
  270. data/packages/web-file/lib/file-reader.js +0 -10
  271. data/packages/web-file/lib/file-system.js +0 -234
  272. data/packages/web-file/lib/file-writer.js +0 -10
  273. data/packages/web-file/lib/file.js +0 -9
  274. data/packages/web-file/lib/main.js +0 -34
  275. data/packages/web-file/lib/platform.js +0 -25
  276. data/packages/web-file/lib/ruby/file.rb +0 -252
  277. data/packages/web-file/lib/ruby/file_reader.rb +0 -69
  278. data/packages/web-file/lib/ruby/file_system.rb +0 -134
  279. data/packages/web-file/lib/ruby/file_writer.rb +0 -78
  280. data/packages/web-file/package.json +0 -12
  281. data/packages/web-typed-array/README.md +0 -7
  282. data/packages/web-typed-array/lib/array-buffer-view.js +0 -9
  283. data/packages/web-typed-array/lib/array-buffer.js +0 -7
  284. data/packages/web-typed-array/lib/main.js +0 -33
  285. data/packages/web-typed-array/lib/platform.js +0 -20
  286. data/packages/web-typed-array/lib/ruby/array_buffer.rb +0 -31
  287. data/packages/web-typed-array/lib/ruby/array_buffer_view.rb +0 -130
  288. data/packages/web-typed-array/lib/ruby/typed_array.rb +0 -133
  289. data/packages/web-typed-array/lib/typed-array.js +0 -26
  290. data/packages/web-typed-array/package.json +0 -9
  291. data/spec/cli/build_spec.rb +0 -59
  292. data/spec/cli/install_spec.rb +0 -120
  293. data/spec/cli/installed_spec.rb +0 -55
  294. data/spec/cli/list_spec.rb +0 -75
  295. data/spec/cli/login_spec.rb +0 -76
  296. data/spec/cli/new_spec.rb +0 -5
  297. data/spec/cli/owner_spec.rb +0 -115
  298. data/spec/cli/push_spec.rb +0 -74
  299. data/spec/cli/uninstall_spec.rb +0 -58
  300. data/spec/cli/unpack_spec.rb +0 -73
  301. data/spec/cli/unyank_spec.rb +0 -74
  302. data/spec/cli/update_spec.rb +0 -8
  303. data/spec/cli/yank_spec.rb +0 -74
  304. data/spec/credentials_spec.rb +0 -24
  305. data/spec/fixtures/coffee-1.0.1.pre.spd +0 -0
  306. data/spec/fixtures/core-test-0.4.3.spd +0 -0
  307. data/spec/fixtures/core-test/bin/cot +0 -3
  308. data/spec/fixtures/core-test/lib/main.js +0 -1
  309. data/spec/fixtures/core-test/resources/runner.css +0 -0
  310. data/spec/fixtures/core-test/tests/test.js +0 -1
  311. data/spec/fixtures/ivory-0.0.1.spd +0 -0
  312. data/spec/fixtures/jquery-1.4.3.spd +0 -0
  313. data/spec/fixtures/optparse-1.0.1.spd +0 -0
  314. data/spec/fixtures/package.json +0 -30
  315. data/spec/gauntlet_spec.rb +0 -27
  316. data/spec/javascript/async-test.js +0 -123
  317. data/spec/javascript/compiler/javascript.js +0 -13
  318. data/spec/javascript/compiler/ruby.js +0 -14
  319. data/spec/javascript/loader-test.js +0 -64
  320. data/spec/javascript/normalize-test.js +0 -73
  321. data/spec/javascript/packages-test.js +0 -44
  322. data/spec/javascript/relative-require-test.js +0 -72
  323. data/spec/javascript/require-test.js +0 -117
  324. data/spec/javascript/sandbox/compile.js +0 -37
  325. data/spec/javascript/sandbox/creation.js +0 -44
  326. data/spec/javascript/sandbox/format.js +0 -79
  327. data/spec/javascript/sandbox/misc.js +0 -57
  328. data/spec/javascript/sandbox/preprocessor.js +0 -81
  329. data/spec/javascript/sandbox/require.js +0 -48
  330. data/spec/javascript/sandbox/run-command.js +0 -21
  331. data/spec/javascript/spade/externs.js +0 -14
  332. data/spec/javascript/spade/load-factory.js +0 -15
  333. data/spec/javascript/spade/misc.js +0 -23
  334. data/spec/javascript/spade/ready.js +0 -12
  335. data/spec/javascript/spade/register.js +0 -13
  336. data/spec/javascript_spec.rb +0 -7
  337. data/spec/package_spec.rb +0 -267
  338. data/spec/spec_helper.rb +0 -30
  339. data/spec/support/cli.rb +0 -94
  340. data/spec/support/core_test.rb +0 -59
  341. data/spec/support/fake.rb +0 -44
  342. data/spec/support/fake_gem_server.rb +0 -66
  343. data/spec/support/fake_gemcutter.rb +0 -49
  344. data/spec/support/matchers.rb +0 -31
  345. data/spec/support/path.rb +0 -54
  346. data/test-spade.html +0 -8
@@ -1,1338 +0,0 @@
1
- <!DOCTYPE html> <html> <head> <title>nodes.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="browser.html"> browser.coffee </a> <a class="source" href="cake.html"> cake.coffee </a> <a class="source" href="coffee-script.html"> coffee-script.coffee </a> <a class="source" href="command.html"> command.coffee </a> <a class="source" href="grammar.html"> grammar.coffee </a> <a class="source" href="helpers.html"> helpers.coffee </a> <a class="source" href="index.html"> index.coffee </a> <a class="source" href="lexer.html"> lexer.coffee </a> <a class="source" href="nodes.html"> nodes.coffee </a> <a class="source" href="optparse.html"> optparse.coffee </a> <a class="source" href="repl.html"> repl.coffee </a> <a class="source" href="rewriter.html"> rewriter.coffee </a> <a class="source" href="scope.html"> scope.coffee </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> nodes.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p><code>nodes.coffee</code> contains all of the node classes for the syntax tree. Most
2
- nodes are created as the result of actions in the <a href="grammar.html">grammar</a>,
3
- but some are created by other nodes as a method of code generation. To convert
4
- the syntax tree into a string of JavaScript code, call <code>compile()</code> on the root.</p> </td> <td class="code"> <div class="highlight"><pre><span class="p">{</span><span class="nx">Scope</span><span class="p">}</span> <span class="o">=</span> <span class="nx">require</span> <span class="s1">&#39;./scope&#39;</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>Import the helpers we plan to use.</p> </td> <td class="code"> <div class="highlight"><pre><span class="p">{</span><span class="nx">compact</span><span class="p">,</span> <span class="nx">flatten</span><span class="p">,</span> <span class="nx">extend</span><span class="p">,</span> <span class="nx">merge</span><span class="p">,</span> <span class="nx">del</span><span class="p">,</span> <span class="nx">starts</span><span class="p">,</span> <span class="nx">ends</span><span class="p">,</span> <span class="nx">last</span><span class="p">}</span> <span class="o">=</span> <span class="nx">require</span> <span class="s1">&#39;./helpers&#39;</span>
5
-
6
- <span class="nv">exports.extend = </span><span class="nx">extend</span> <span class="c1"># for parser</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>Constant functions for nodes that don't need customization.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">YES = </span><span class="o">-&gt;</span> <span class="kc">yes</span>
7
- <span class="nv">NO = </span><span class="o">-&gt;</span> <span class="kc">no</span>
8
- <span class="nv">THIS = </span><span class="o">-&gt;</span> <span class="k">this</span>
9
- <span class="nv">NEGATE = </span><span class="o">-&gt;</span> <span class="vi">@negated = </span><span class="o">not</span> <span class="nx">@negated</span><span class="p">;</span> <span class="k">this</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <h3>Base</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>The <strong>Base</strong> is the abstract base class for all nodes in the syntax tree.
10
- Each subclass implements the <code>compileNode</code> method, which performs the
11
- code generation for that node. To compile a node to JavaScript,
12
- call <code>compile</code> on it, which wraps <code>compileNode</code> in some generic extra smarts,
13
- to know when the generated code needs to be wrapped up in a closure.
14
- An options hash is passed and cloned throughout, containing information about
15
- the environment from higher in the tree (such as if a returned value is
16
- being requested by the surrounding function), information about the current
17
- scope, and indentation level.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Base = </span><span class="nx">class</span> <span class="nx">Base</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Common logic for determining whether to wrap this node in a closure before
18
- compiling it, or to compile directly. We need to wrap if this node is a
19
- <em>statement</em>, and it's not a <em>pureStatement</em>, and we're not at
20
- the top level of a block (which would be unnecessary), and we haven't
21
- already been asked to return the result (because statements know how to
22
- return results).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">lvl</span><span class="p">)</span> <span class="o">-&gt;</span>
23
- <span class="nv">o = </span><span class="nx">extend</span> <span class="p">{},</span> <span class="nx">o</span>
24
- <span class="nv">o.level = </span><span class="nx">lvl</span> <span class="k">if</span> <span class="nx">lvl</span>
25
- <span class="nv">node = </span><span class="nx">@unfoldSoak</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">or</span> <span class="k">this</span>
26
- <span class="nv">node.tab = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span>
27
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">is</span> <span class="nx">LEVEL_TOP</span> <span class="o">or</span> <span class="o">not</span> <span class="nx">node</span><span class="p">.</span><span class="nx">isStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
28
- <span class="nx">node</span><span class="p">.</span><span class="nx">compileNode</span> <span class="nx">o</span>
29
- <span class="k">else</span>
30
- <span class="nx">node</span><span class="p">.</span><span class="nx">compileClosure</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>Statements converted into expressions via closure-wrapping share a scope
31
- object with their parent closure, to preserve the expected lexical scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileClosure</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
32
- <span class="k">if</span> <span class="nx">@jumps</span><span class="p">()</span>
33
- <span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">&#39;cannot use a pure statement in an expression.&#39;</span>
34
- <span class="nv">o.sharedScope = </span><span class="kc">yes</span>
35
- <span class="nx">Closure</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">compileNode</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>If the code generation wishes to use the result of a complex expression
36
- in multiple places, ensure that the expression is only ever evaluated once,
37
- by assigning it to a temporary variable. Pass a level to precompile.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">cache</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">level</span><span class="p">,</span> <span class="nx">reused</span><span class="p">)</span> <span class="o">-&gt;</span>
38
- <span class="nx">unless</span> <span class="nx">@isComplex</span><span class="p">()</span>
39
- <span class="nv">ref = </span><span class="k">if</span> <span class="nx">level</span> <span class="k">then</span> <span class="nx">@compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">level</span> <span class="k">else</span> <span class="k">this</span>
40
- <span class="p">[</span><span class="nx">ref</span><span class="p">,</span> <span class="nx">ref</span><span class="p">]</span>
41
- <span class="k">else</span>
42
- <span class="nv">ref = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">reused</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;ref&#39;</span>
43
- <span class="nv">sub = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="nx">ref</span><span class="p">,</span> <span class="k">this</span>
44
- <span class="k">if</span> <span class="nx">level</span> <span class="k">then</span> <span class="p">[</span><span class="nx">sub</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">level</span><span class="p">),</span> <span class="nx">ref</span><span class="p">.</span><span class="nx">value</span><span class="p">]</span> <span class="k">else</span> <span class="p">[</span><span class="nx">sub</span><span class="p">,</span> <span class="nx">ref</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>Compile to a source/variable pair suitable for looping.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileLoopReference</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
45
- <span class="nv">src = tmp = </span><span class="nx">@compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
46
- <span class="nx">unless</span> <span class="o">-</span><span class="kc">Infinity</span> <span class="o">&lt;</span> <span class="o">+</span><span class="nx">src</span> <span class="o">&lt;</span> <span class="kc">Infinity</span> <span class="o">or</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">src</span><span class="p">)</span> <span class="o">and</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">check</span><span class="p">(</span><span class="nx">src</span><span class="p">,</span> <span class="kc">yes</span><span class="p">)</span>
47
- <span class="nv">src = </span><span class="s2">&quot;#{ tmp = o.scope.freeVariable name } = #{src}&quot;</span>
48
- <span class="p">[</span><span class="nx">src</span><span class="p">,</span> <span class="nx">tmp</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Construct a node that returns the current node's result.
49
- Note that this is overridden for smarter behavior for
50
- many statement nodes (e.g. If, For)...</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-&gt;</span>
51
- <span class="k">new</span> <span class="nx">Return</span> <span class="k">this</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>Does this node, or any of its children, contain a node of a certain kind?
52
- Recursively traverses down the <em>children</em> of the nodes, yielding to a block
53
- and returning true when the block finds a match. <code>contains</code> does not cross
54
- scope boundaries.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">contains</span><span class="o">:</span> <span class="p">(</span><span class="nx">pred</span><span class="p">)</span> <span class="o">-&gt;</span>
55
- <span class="nv">contains = </span><span class="kc">no</span>
56
- <span class="nx">@traverseChildren</span> <span class="kc">no</span><span class="p">,</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-&gt;</span>
57
- <span class="k">if</span> <span class="nx">pred</span> <span class="nx">node</span>
58
- <span class="nv">contains = </span><span class="kc">yes</span>
59
- <span class="k">return</span> <span class="kc">no</span>
60
- <span class="nx">contains</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Is this node of a certain type, or does it contain the type?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">containsType</span><span class="o">:</span> <span class="p">(</span><span class="nx">type</span><span class="p">)</span> <span class="o">-&gt;</span>
61
- <span class="k">this</span> <span class="k">instanceof</span> <span class="nx">type</span> <span class="o">or</span> <span class="nx">@contains</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">type</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>Pull out the last non-comment node of a node list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">lastNonComment</span><span class="o">:</span> <span class="p">(</span><span class="nx">list</span><span class="p">)</span> <span class="o">-&gt;</span>
62
- <span class="nv">i = </span><span class="nx">list</span><span class="p">.</span><span class="nx">length</span>
63
- <span class="k">return</span> <span class="nx">list</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="k">while</span> <span class="nx">i</span><span class="o">--</span> <span class="k">when</span> <span class="nx">list</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Comment</span>
64
- <span class="kc">null</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p><code>toString</code> representation of the node, for inspecting the parse tree.
65
- This is what <code>coffee --nodes</code> prints out.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">toString</span><span class="o">:</span> <span class="p">(</span><span class="nv">idt = </span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="nv">name = </span><span class="nx">@constructor</span><span class="p">.</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
66
- <span class="nv">tree = </span><span class="s1">&#39;\n&#39;</span> <span class="o">+</span> <span class="nx">idt</span> <span class="o">+</span> <span class="nx">name</span>
67
- <span class="nx">tree</span> <span class="o">+=</span> <span class="s1">&#39;?&#39;</span> <span class="k">if</span> <span class="nx">@soak</span>
68
- <span class="nx">@eachChild</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nx">tree</span> <span class="o">+=</span> <span class="nx">node</span><span class="p">.</span><span class="nx">toString</span> <span class="nx">idt</span> <span class="o">+</span> <span class="nx">TAB</span>
69
- <span class="nx">tree</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>Passes each child to a function, breaking when the function returns <code>false</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">eachChild</span><span class="o">:</span> <span class="p">(</span><span class="nx">func</span><span class="p">)</span> <span class="o">-&gt;</span>
70
- <span class="k">return</span> <span class="k">this</span> <span class="nx">unless</span> <span class="nx">@children</span>
71
- <span class="k">for</span> <span class="nx">attr</span> <span class="k">in</span> <span class="nx">@children</span> <span class="k">when</span> <span class="err">@</span><span class="p">[</span><span class="nx">attr</span><span class="p">]</span>
72
- <span class="k">for</span> <span class="nx">child</span> <span class="k">in</span> <span class="nx">flatten</span> <span class="p">[</span><span class="err">@</span><span class="p">[</span><span class="nx">attr</span><span class="p">]]</span>
73
- <span class="k">return</span> <span class="k">this</span> <span class="k">if</span> <span class="nx">func</span><span class="p">(</span><span class="nx">child</span><span class="p">)</span> <span class="o">is</span> <span class="kc">false</span>
74
- <span class="k">this</span>
75
-
76
- <span class="nx">traverseChildren</span><span class="o">:</span> <span class="p">(</span><span class="nx">crossScope</span><span class="p">,</span> <span class="nx">func</span><span class="p">)</span> <span class="o">-&gt;</span>
77
- <span class="nx">@eachChild</span> <span class="p">(</span><span class="nx">child</span><span class="p">)</span> <span class="o">-&gt;</span>
78
- <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">func</span><span class="p">(</span><span class="nx">child</span><span class="p">)</span> <span class="o">is</span> <span class="kc">false</span>
79
- <span class="nx">child</span><span class="p">.</span><span class="nx">traverseChildren</span> <span class="nx">crossScope</span><span class="p">,</span> <span class="nx">func</span>
80
-
81
- <span class="nx">invert</span><span class="o">:</span> <span class="o">-&gt;</span>
82
- <span class="k">new</span> <span class="nx">Op</span> <span class="s1">&#39;!&#39;</span><span class="p">,</span> <span class="k">this</span>
83
-
84
- <span class="nx">unwrapAll</span><span class="o">:</span> <span class="o">-&gt;</span>
85
- <span class="nv">node = </span><span class="k">this</span>
86
- <span class="k">continue</span> <span class="nx">until</span> <span class="nx">node</span> <span class="o">is</span> <span class="nv">node = </span><span class="nx">node</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span>
87
- <span class="nx">node</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Default implementations of the common node properties and methods. Nodes
88
- will override these with custom logic, if needed.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">children</span><span class="o">:</span> <span class="p">[]</span>
89
-
90
- <span class="nx">isStatement</span> <span class="o">:</span> <span class="nx">NO</span>
91
- <span class="nx">jumps</span> <span class="o">:</span> <span class="nx">NO</span>
92
- <span class="nx">isComplex</span> <span class="o">:</span> <span class="nx">YES</span>
93
- <span class="nx">isChainable</span> <span class="o">:</span> <span class="nx">NO</span>
94
- <span class="nx">isAssignable</span> <span class="o">:</span> <span class="nx">NO</span>
95
-
96
- <span class="nx">unwrap</span> <span class="o">:</span> <span class="nx">THIS</span>
97
- <span class="nx">unfoldSoak</span> <span class="o">:</span> <span class="nx">NO</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>Is this node used to assign a certain variable?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">assigns</span><span class="o">:</span> <span class="nx">NO</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <h3>Expressions</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">&#182;</a> </div> <p>The expressions body is the list of expressions that forms the body of an
98
- indented block of code -- the implementation of a function, a clause in an
99
- <code>if</code>, <code>switch</code>, or <code>try</code>, and so on...</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Expressions = </span><span class="nx">class</span> <span class="nx">Expressions</span> <span class="k">extends</span> <span class="nx">Base</span>
100
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">nodes</span><span class="p">)</span> <span class="o">-&gt;</span>
101
- <span class="vi">@expressions = </span><span class="nx">compact</span> <span class="nx">flatten</span> <span class="nx">nodes</span> <span class="o">or</span> <span class="p">[]</span>
102
-
103
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;expressions&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">&#182;</a> </div> <p>Tack an expression on to the end of this expression list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">push</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-&gt;</span>
104
- <span class="nx">@expressions</span><span class="p">.</span><span class="nx">push</span> <span class="nx">node</span>
105
- <span class="k">this</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">&#182;</a> </div> <p>Remove and return the last expression of this expression list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">pop</span><span class="o">:</span> <span class="o">-&gt;</span>
106
- <span class="nx">@expressions</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">&#182;</a> </div> <p>Add an expression at the beginning of this expression list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">unshift</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-&gt;</span>
107
- <span class="nx">@expressions</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">node</span>
108
- <span class="k">this</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">&#182;</a> </div> <p>If this Expressions consists of just a single node, unwrap it by pulling
109
- it back out.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">unwrap</span><span class="o">:</span> <span class="o">-&gt;</span>
110
- <span class="k">if</span> <span class="nx">@expressions</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">1</span> <span class="k">then</span> <span class="nx">@expressions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">else</span> <span class="k">this</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">&#182;</a> </div> <p>Is this an empty block of code?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">isEmpty</span><span class="o">:</span> <span class="o">-&gt;</span>
111
- <span class="o">not</span> <span class="nx">@expressions</span><span class="p">.</span><span class="nx">length</span>
112
-
113
- <span class="nx">isStatement</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
114
- <span class="k">for</span> <span class="nx">exp</span> <span class="k">in</span> <span class="nx">@expressions</span> <span class="k">when</span> <span class="nx">exp</span><span class="p">.</span><span class="nx">isStatement</span> <span class="nx">o</span>
115
- <span class="k">return</span> <span class="kc">yes</span>
116
- <span class="kc">no</span>
117
-
118
- <span class="nx">jumps</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
119
- <span class="k">for</span> <span class="nx">exp</span> <span class="k">in</span> <span class="nx">@expressions</span>
120
- <span class="k">return</span> <span class="nx">exp</span> <span class="k">if</span> <span class="nx">exp</span><span class="p">.</span><span class="nx">jumps</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">&#182;</a> </div> <p>An Expressions node does not return its entire body, rather it
121
- ensures that the final expression is returned.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-&gt;</span>
122
- <span class="nv">len = </span><span class="nx">@expressions</span><span class="p">.</span><span class="nx">length</span>
123
- <span class="k">while</span> <span class="nx">len</span><span class="o">--</span>
124
- <span class="nv">expr = </span><span class="nx">@expressions</span><span class="p">[</span><span class="nx">len</span><span class="p">]</span>
125
- <span class="k">if</span> <span class="nx">expr</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Comment</span>
126
- <span class="nx">@expressions</span><span class="p">[</span><span class="nx">len</span><span class="p">]</span> <span class="o">=</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span>
127
- <span class="k">break</span>
128
- <span class="k">this</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">&#182;</a> </div> <p>An <strong>Expressions</strong> is the only node that can serve as the root.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nv">o = </span><span class="p">{},</span> <span class="nx">level</span><span class="p">)</span> <span class="o">-&gt;</span>
129
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span> <span class="k">then</span> <span class="k">super</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">level</span> <span class="k">else</span> <span class="nx">@compileRoot</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">&#182;</a> </div> <p>Compile all expressions within the <strong>Expressions</strong> body. If we need to
130
- return the result, and it's an expression, simply return it. If it's a
131
- statement, ask the statement to do so.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
132
- <span class="vi">@tab = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span>
133
- <span class="nv">top = </span><span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">is</span> <span class="nx">LEVEL_TOP</span>
134
- <span class="nv">codes = </span><span class="p">[]</span>
135
- <span class="k">for</span> <span class="nx">node</span> <span class="k">in</span> <span class="nx">@expressions</span>
136
- <span class="nv">node = </span><span class="nx">node</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()</span>
137
- <span class="nv">node = </span><span class="p">(</span><span class="nx">node</span><span class="p">.</span><span class="nx">unfoldSoak</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">or</span> <span class="nx">node</span><span class="p">)</span>
138
- <span class="k">if</span> <span class="nx">top</span>
139
- <span class="nv">node.front = </span><span class="kc">true</span>
140
- <span class="nv">code = </span><span class="nx">node</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
141
- <span class="nx">codes</span><span class="p">.</span><span class="nx">push</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">isStatement</span> <span class="nx">o</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="nx">@tab</span> <span class="o">+</span> <span class="nx">code</span> <span class="o">+</span> <span class="s1">&#39;;&#39;</span>
142
- <span class="k">else</span>
143
- <span class="nx">codes</span><span class="p">.</span><span class="nx">push</span> <span class="nx">node</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
144
- <span class="k">return</span> <span class="nx">codes</span><span class="p">.</span><span class="nx">join</span> <span class="s1">&#39;\n&#39;</span> <span class="k">if</span> <span class="nx">top</span>
145
- <span class="nv">code = </span><span class="nx">codes</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span> <span class="o">or</span> <span class="s1">&#39;void 0&#39;</span>
146
- <span class="k">if</span> <span class="nx">codes</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="o">and</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&gt;=</span> <span class="nx">LEVEL_LIST</span> <span class="k">then</span> <span class="s2">&quot;(#{code})&quot;</span> <span class="k">else</span> <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">&#182;</a> </div> <p>If we happen to be the top-level <strong>Expressions</strong>, wrap everything in
147
- a safety closure, unless requested not to.
148
- It would be better not to generate them in the first place, but for now,
149
- clean up obvious double-parentheses.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileRoot</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
150
- <span class="nv">o.indent = </span><span class="vi">@tab = </span><span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">bare</span> <span class="k">then</span> <span class="s1">&#39;&#39;</span> <span class="k">else</span> <span class="nx">TAB</span>
151
- <span class="nv">o.scope = </span><span class="k">new</span> <span class="nx">Scope</span> <span class="kc">null</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="kc">null</span>
152
- <span class="nv">o.level = </span><span class="nx">LEVEL_TOP</span>
153
- <span class="nv">code = </span><span class="nx">@compileWithDeclarations</span> <span class="nx">o</span>
154
- <span class="nv">code = </span><span class="nx">code</span><span class="p">.</span><span class="nx">replace</span> <span class="nx">TRAILING_WHITESPACE</span><span class="p">,</span> <span class="s1">&#39;&#39;</span>
155
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">bare</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">&quot;(function() {\n#{code}\n}).call(this);\n&quot;</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">&#182;</a> </div> <p>Compile the expressions body for the contents of a function, with
156
- declarations of all inner variables pushed up to the top.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileWithDeclarations</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
157
- <span class="nv">code = post = </span><span class="s1">&#39;&#39;</span>
158
- <span class="k">for</span> <span class="nx">exp</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">@expressions</span>
159
- <span class="nv">exp = </span><span class="nx">exp</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span>
160
- <span class="k">break</span> <span class="nx">unless</span> <span class="nx">exp</span> <span class="k">instanceof</span> <span class="nx">Comment</span> <span class="o">or</span> <span class="nx">exp</span> <span class="k">instanceof</span> <span class="nx">Literal</span>
161
- <span class="nv">o = </span><span class="nx">merge</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">level</span><span class="o">:</span> <span class="nx">LEVEL_TOP</span><span class="p">)</span>
162
- <span class="k">if</span> <span class="nx">i</span>
163
- <span class="nv">rest = </span><span class="nx">@expressions</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">@expressions</span><span class="p">.</span><span class="nx">length</span>
164
- <span class="nv">code = </span><span class="nx">@compileNode</span> <span class="nx">o</span>
165
- <span class="vi">@expressions = </span><span class="nx">rest</span>
166
- <span class="nv">post = </span><span class="nx">@compileNode</span> <span class="nx">o</span>
167
- <span class="p">{</span><span class="nx">scope</span><span class="p">}</span> <span class="o">=</span> <span class="nx">o</span>
168
- <span class="k">if</span> <span class="nx">scope</span><span class="p">.</span><span class="nx">expressions</span> <span class="o">is</span> <span class="k">this</span>
169
- <span class="k">if</span> <span class="o">not</span> <span class="nx">o</span><span class="p">.</span><span class="nx">globals</span> <span class="o">and</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">hasDeclarations</span><span class="p">()</span>
170
- <span class="nx">code</span> <span class="o">+=</span> <span class="s2">&quot;#{@tab}var #{ scope.declaredVariables().join(&#39;, &#39;) };\n&quot;</span>
171
- <span class="k">if</span> <span class="nx">scope</span><span class="p">.</span><span class="nx">hasAssignments</span>
172
- <span class="nx">code</span> <span class="o">+=</span> <span class="s2">&quot;#{@tab}var #{ multident scope.assignedVariables().join(&#39;, &#39;), @tab };\n&quot;</span>
173
- <span class="nx">code</span> <span class="o">+</span> <span class="nx">post</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">&#182;</a> </div> <p>Wrap up the given nodes as an <strong>Expressions</strong>, unless it already happens
174
- to be one.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@wrap</span><span class="o">:</span> <span class="p">(</span><span class="nx">nodes</span><span class="p">)</span> <span class="o">-&gt;</span>
175
- <span class="k">return</span> <span class="nx">nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="nx">nodes</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">1</span> <span class="o">and</span> <span class="nx">nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nx">Expressions</span>
176
- <span class="k">new</span> <span class="nx">Expressions</span> <span class="nx">nodes</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">&#182;</a> </div> <h3>Literal</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">&#182;</a> </div> <p>Literals are static values that can be passed through directly into
177
- JavaScript without translation, such as: strings, numbers,
178
- <code>true</code>, <code>false</code>, <code>null</code>...</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Literal = </span><span class="nx">class</span> <span class="nx">Literal</span> <span class="k">extends</span> <span class="nx">Base</span>
179
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@value</span><span class="p">)</span> <span class="o">-&gt;</span>
180
-
181
- <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-&gt;</span>
182
- <span class="k">if</span> <span class="nx">@isStatement</span><span class="p">()</span> <span class="k">then</span> <span class="k">this</span> <span class="k">else</span> <span class="k">new</span> <span class="nx">Return</span> <span class="k">this</span>
183
-
184
- <span class="nx">isAssignable</span><span class="o">:</span> <span class="o">-&gt;</span>
185
- <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span> <span class="nx">@value</span>
186
-
187
- <span class="nx">isStatement</span><span class="o">:</span> <span class="o">-&gt;</span>
188
- <span class="nx">@value</span> <span class="k">in</span> <span class="p">[</span><span class="s1">&#39;break&#39;</span><span class="p">,</span> <span class="s1">&#39;continue&#39;</span><span class="p">,</span> <span class="s1">&#39;debugger&#39;</span><span class="p">]</span>
189
-
190
- <span class="nx">isComplex</span><span class="o">:</span> <span class="nx">NO</span>
191
-
192
- <span class="nx">assigns</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
193
- <span class="nx">name</span> <span class="o">is</span> <span class="nx">@value</span>
194
-
195
- <span class="nx">jumps</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
196
- <span class="k">return</span> <span class="kc">no</span> <span class="nx">unless</span> <span class="nx">@isStatement</span><span class="p">()</span>
197
- <span class="k">if</span> <span class="o">not</span> <span class="p">(</span><span class="nx">o</span> <span class="o">and</span> <span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nx">loop</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">block</span> <span class="o">and</span> <span class="p">(</span><span class="nx">@value</span> <span class="o">isnt</span> <span class="s1">&#39;continue&#39;</span><span class="p">)))</span> <span class="k">then</span> <span class="k">this</span> <span class="k">else</span> <span class="kc">no</span>
198
-
199
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
200
- <span class="nv">code = </span><span class="k">if</span> <span class="nx">@value</span><span class="p">.</span><span class="nx">reserved</span> <span class="k">then</span> <span class="s2">&quot;\&quot;#{@value}\&quot;&quot;</span> <span class="k">else</span> <span class="nx">@value</span>
201
- <span class="k">if</span> <span class="nx">@isStatement</span><span class="p">()</span> <span class="k">then</span> <span class="s2">&quot;#{@tab}#{code};&quot;</span> <span class="k">else</span> <span class="nx">code</span>
202
-
203
- <span class="nx">toString</span><span class="o">:</span> <span class="o">-&gt;</span>
204
- <span class="s1">&#39; &quot;&#39;</span> <span class="o">+</span> <span class="nx">@value</span> <span class="o">+</span> <span class="s1">&#39;&quot;&#39;</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">&#182;</a> </div> <h3>Return</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">&#182;</a> </div> <p>A <code>return</code> is a <em>pureStatement</em> -- wrapping it in a closure wouldn't
205
- make sense.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Return = </span><span class="nx">class</span> <span class="nx">Return</span> <span class="k">extends</span> <span class="nx">Base</span>
206
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@expression</span><span class="p">)</span> <span class="o">-&gt;</span>
207
-
208
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;expression&#39;</span><span class="p">]</span>
209
-
210
- <span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
211
- <span class="nx">makeReturn</span><span class="o">:</span> <span class="nx">THIS</span>
212
- <span class="nx">jumps</span><span class="o">:</span> <span class="nx">THIS</span>
213
-
214
- <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">level</span><span class="p">)</span> <span class="o">-&gt;</span>
215
- <span class="nv">expr = </span><span class="nx">@expression</span><span class="o">?</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span>
216
- <span class="k">if</span> <span class="nx">expr</span> <span class="o">and</span> <span class="nx">expr</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Return</span> <span class="k">then</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">level</span> <span class="k">else</span> <span class="k">super</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">level</span>
217
-
218
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
219
- <span class="nx">@tab</span> <span class="o">+</span> <span class="s2">&quot;return#{ if @expression then &#39; &#39; + @expression.compile(o, LEVEL_PAREN) else &#39;&#39; };&quot;</span></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">&#182;</a> </div> <h3>Value</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">&#182;</a> </div> <p>A value, variable or literal or parenthesized, indexed or dotted into,
220
- or vanilla.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Value = </span><span class="nx">class</span> <span class="nx">Value</span> <span class="k">extends</span> <span class="nx">Base</span>
221
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">base</span><span class="p">,</span> <span class="nx">props</span><span class="p">,</span> <span class="nx">tag</span><span class="p">)</span> <span class="o">-&gt;</span>
222
- <span class="k">return</span> <span class="nx">base</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">props</span> <span class="o">and</span> <span class="nx">base</span> <span class="k">instanceof</span> <span class="nx">Value</span>
223
- <span class="vi">@base = </span><span class="nx">base</span>
224
- <span class="vi">@properties = </span><span class="nx">props</span> <span class="o">or</span> <span class="p">[]</span>
225
- <span class="err">@</span><span class="p">[</span><span class="nx">tag</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">tag</span>
226
- <span class="k">return</span> <span class="k">this</span>
227
-
228
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;base&#39;</span><span class="p">,</span> <span class="s1">&#39;properties&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">&#182;</a> </div> <p>Add a property access to the list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">push</span><span class="o">:</span> <span class="p">(</span><span class="nx">prop</span><span class="p">)</span> <span class="o">-&gt;</span>
229
- <span class="nx">@properties</span><span class="p">.</span><span class="nx">push</span> <span class="nx">prop</span>
230
- <span class="k">this</span>
231
-
232
- <span class="nx">hasProperties</span><span class="o">:</span> <span class="o">-&gt;</span>
233
- <span class="o">!!</span><span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">&#182;</a> </div> <p>Some boolean checks for the benefit of other nodes.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">isArray</span> <span class="o">:</span> <span class="o">-&gt;</span> <span class="o">not</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">and</span> <span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">Arr</span>
234
- <span class="nx">isComplex</span> <span class="o">:</span> <span class="o">-&gt;</span> <span class="nx">@hasProperties</span><span class="p">()</span> <span class="o">or</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
235
- <span class="nx">isAssignable</span> <span class="o">:</span> <span class="o">-&gt;</span> <span class="nx">@hasProperties</span><span class="p">()</span> <span class="o">or</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">isAssignable</span><span class="p">()</span>
236
- <span class="nx">isSimpleNumber</span> <span class="o">:</span> <span class="o">-&gt;</span> <span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">Literal</span> <span class="o">and</span> <span class="nx">SIMPLENUM</span><span class="p">.</span><span class="nx">test</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">value</span>
237
- <span class="nx">isAtomic</span> <span class="o">:</span> <span class="o">-&gt;</span>
238
- <span class="k">for</span> <span class="nx">node</span> <span class="k">in</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">@base</span>
239
- <span class="k">return</span> <span class="kc">no</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">soak</span> <span class="o">or</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Call</span>
240
- <span class="kc">yes</span>
241
-
242
- <span class="nx">isStatement</span> <span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="o">not</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">and</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">isStatement</span> <span class="nx">o</span>
243
- <span class="nx">assigns</span> <span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="o">not</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">and</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">assigns</span> <span class="nx">name</span>
244
- <span class="nx">jumps</span> <span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="o">not</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">and</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">jumps</span> <span class="nx">o</span>
245
-
246
- <span class="nx">isObject</span><span class="o">:</span> <span class="p">(</span><span class="nx">onlyGenerated</span><span class="p">)</span> <span class="o">-&gt;</span>
247
- <span class="k">return</span> <span class="kc">no</span> <span class="k">if</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span>
248
- <span class="p">(</span><span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">Obj</span><span class="p">)</span> <span class="o">and</span> <span class="p">(</span><span class="o">not</span> <span class="nx">onlyGenerated</span> <span class="o">or</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">generated</span><span class="p">)</span>
249
-
250
- <span class="nx">isSplice</span><span class="o">:</span> <span class="o">-&gt;</span>
251
- <span class="nx">last</span><span class="p">(</span><span class="nx">@properties</span><span class="p">)</span> <span class="k">instanceof</span> <span class="nx">Slice</span>
252
-
253
- <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-&gt;</span>
254
- <span class="k">if</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="k">then</span> <span class="k">super</span><span class="p">()</span> <span class="k">else</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-39">&#182;</a> </div> <p>The value can be unwrapped as its inner node, if there are no attached
255
- properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">unwrap</span><span class="o">:</span> <span class="o">-&gt;</span>
256
- <span class="k">if</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="k">then</span> <span class="k">this</span> <span class="k">else</span> <span class="nx">@base</span></pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-40">&#182;</a> </div> <p>A reference has base part (<code>this</code> value) and name part.
257
- We cache them separately for compiling complex expressions.
258
- <code>a()[b()] ?= c</code> -> <code>(_base = a())[_name = b()] ? _base[_name] = c</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">cacheReference</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
259
- <span class="nv">name = </span><span class="nx">last</span> <span class="nx">@properties</span>
260
- <span class="k">if</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">&lt;</span> <span class="mi">2</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">name</span><span class="o">?</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
261
- <span class="k">return</span> <span class="p">[</span><span class="k">this</span><span class="p">,</span> <span class="k">this</span><span class="p">]</span> <span class="c1"># `a` `a.b`</span>
262
- <span class="nv">base = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">@base</span><span class="p">,</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">slice</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span>
263
- <span class="k">if</span> <span class="nx">base</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span> <span class="c1"># `a().b`</span>
264
- <span class="nv">bref = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;base&#39;</span>
265
- <span class="nv">base = </span><span class="k">new</span> <span class="nx">Value</span> <span class="k">new</span> <span class="nx">Parens</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">bref</span><span class="p">,</span> <span class="nx">base</span>
266
- <span class="k">return</span> <span class="p">[</span><span class="nx">base</span><span class="p">,</span> <span class="nx">bref</span><span class="p">]</span> <span class="nx">unless</span> <span class="nx">name</span> <span class="c1"># `a()`</span>
267
- <span class="k">if</span> <span class="nx">name</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span> <span class="c1"># `a[b()]`</span>
268
- <span class="nv">nref = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;name&#39;</span>
269
- <span class="nv">name = </span><span class="k">new</span> <span class="nx">Index</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">nref</span><span class="p">,</span> <span class="nx">name</span><span class="p">.</span><span class="nx">index</span>
270
- <span class="nv">nref = </span><span class="k">new</span> <span class="nx">Index</span> <span class="nx">nref</span>
271
- <span class="p">[</span><span class="nx">base</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">name</span><span class="p">),</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">bref</span> <span class="o">or</span> <span class="nx">base</span><span class="p">.</span><span class="nx">base</span><span class="p">,</span> <span class="p">[</span><span class="nx">nref</span> <span class="o">or</span> <span class="nx">name</span><span class="p">])]</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-41">&#182;</a> </div> <p>We compile a value to JavaScript by compiling and joining each property.
272
- Things get much more interesting if the chain of properties has <em>soak</em>
273
- operators <code>?.</code> interspersed. Then we have to take care not to accidentally
274
- evaluate anything twice when building the soak chain.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
275
- <span class="vi">@base.front = </span><span class="nx">@front</span>
276
- <span class="nv">props = </span><span class="nx">@properties</span>
277
- <span class="nv">code = </span><span class="nx">@base</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="k">if</span> <span class="nx">props</span><span class="p">.</span><span class="nx">length</span> <span class="k">then</span> <span class="nx">LEVEL_ACCESS</span> <span class="k">else</span> <span class="kc">null</span>
278
- <span class="nv">code = </span><span class="s2">&quot;(#{code})&quot;</span> <span class="k">if</span> <span class="nx">props</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nx">Access</span> <span class="o">and</span> <span class="nx">@isSimpleNumber</span><span class="p">()</span>
279
- <span class="nx">code</span> <span class="o">+=</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span> <span class="k">for</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">props</span>
280
- <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-42">&#182;</a> </div> <p>Unfold a soak into an <code>If</code>: <code>a?.b</code> -> <code>a.b if a?</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">unfoldSoak</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
281
- <span class="k">if</span> <span class="nv">ifn = </span><span class="nx">@base</span><span class="p">.</span><span class="nx">unfoldSoak</span> <span class="nx">o</span>
282
- <span class="nb">Array</span><span class="o">::</span><span class="nx">push</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">ifn</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">properties</span><span class="p">,</span> <span class="nx">@properties</span>
283
- <span class="k">return</span> <span class="nx">ifn</span>
284
- <span class="k">for</span> <span class="nx">prop</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">@properties</span> <span class="k">when</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">soak</span>
285
- <span class="nv">prop.soak = </span><span class="kc">off</span>
286
- <span class="nv">fst = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">@base</span><span class="p">,</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">slice</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">i</span>
287
- <span class="nv">snd = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">@base</span><span class="p">,</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">slice</span> <span class="nx">i</span>
288
- <span class="k">if</span> <span class="nx">fst</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
289
- <span class="nv">ref = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;ref&#39;</span>
290
- <span class="nv">fst = </span><span class="k">new</span> <span class="nx">Parens</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">ref</span><span class="p">,</span> <span class="nx">fst</span>
291
- <span class="nv">snd.base = </span><span class="nx">ref</span>
292
- <span class="k">return</span> <span class="k">new</span> <span class="nx">If</span> <span class="k">new</span> <span class="nx">Existence</span><span class="p">(</span><span class="nx">fst</span><span class="p">),</span> <span class="nx">snd</span><span class="p">,</span> <span class="nx">soak</span><span class="o">:</span> <span class="kc">on</span>
293
- <span class="kc">null</span></pre></div> </td> </tr> <tr id="section-43"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-43">&#182;</a> </div> <h3>Comment</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-44"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-44">&#182;</a> </div> <p>CoffeeScript passes through block comments as JavaScript block comments
294
- at the same position.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Comment = </span><span class="nx">class</span> <span class="nx">Comment</span> <span class="k">extends</span> <span class="nx">Base</span>
295
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@comment</span><span class="p">)</span> <span class="o">-&gt;</span>
296
-
297
- <span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
298
- <span class="nx">makeReturn</span><span class="o">:</span> <span class="nx">THIS</span>
299
-
300
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">level</span><span class="p">)</span> <span class="o">-&gt;</span>
301
- <span class="nv">code = </span><span class="s1">&#39;/*&#39;</span> <span class="o">+</span> <span class="nx">multident</span><span class="p">(</span><span class="nx">@comment</span><span class="p">,</span> <span class="nx">@tab</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;*/&#39;</span>
302
- <span class="nv">code = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+</span> <span class="nx">code</span> <span class="k">if</span> <span class="p">(</span><span class="nx">level</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span><span class="p">)</span> <span class="o">is</span> <span class="nx">LEVEL_TOP</span>
303
- <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-45"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-45">&#182;</a> </div> <h3>Call</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-46"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-46">&#182;</a> </div> <p>Node for a function invocation. Takes care of converting <code>super()</code> calls into
304
- calls against the prototype's function of the same name.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Call = </span><span class="nx">class</span> <span class="nx">Call</span> <span class="k">extends</span> <span class="nx">Base</span>
305
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">variable</span><span class="p">,</span> <span class="vi">@args = </span><span class="p">[],</span> <span class="nx">@soak</span><span class="p">)</span> <span class="o">-&gt;</span>
306
- <span class="vi">@isNew = </span><span class="kc">false</span>
307
- <span class="vi">@isSuper = </span><span class="nx">variable</span> <span class="o">is</span> <span class="s1">&#39;super&#39;</span>
308
- <span class="vi">@variable = </span><span class="k">if</span> <span class="nx">@isSuper</span> <span class="k">then</span> <span class="kc">null</span> <span class="k">else</span> <span class="nx">variable</span>
309
-
310
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;variable&#39;</span><span class="p">,</span> <span class="s1">&#39;args&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-47"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-47">&#182;</a> </div> <p>Tag this invocation as creating a new instance.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">newInstance</span><span class="o">:</span> <span class="o">-&gt;</span>
311
- <span class="nv">base = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">base</span> <span class="o">or</span> <span class="nx">@variable</span>
312
- <span class="k">if</span> <span class="nx">base</span> <span class="k">instanceof</span> <span class="nx">Call</span>
313
- <span class="nx">base</span><span class="p">.</span><span class="nx">newInstance</span><span class="p">()</span>
314
- <span class="k">else</span>
315
- <span class="vi">@isNew = </span><span class="kc">true</span>
316
- <span class="k">this</span></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-48">&#182;</a> </div> <p>Grab the reference to the superclass's implementation of the current
317
- method.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">superReference</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
318
- <span class="p">{</span><span class="nx">method</span><span class="p">}</span> <span class="o">=</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span>
319
- <span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">&#39;cannot call super outside of a function.&#39;</span> <span class="nx">unless</span> <span class="nx">method</span>
320
- <span class="p">{</span><span class="nx">name</span><span class="p">}</span> <span class="o">=</span> <span class="nx">method</span>
321
- <span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">&#39;cannot call super on an anonymous function.&#39;</span> <span class="nx">unless</span> <span class="nx">name</span>
322
- <span class="k">if</span> <span class="nx">method</span><span class="p">.</span><span class="nx">klass</span>
323
- <span class="s2">&quot;#{method.klass}.__super__.#{name}&quot;</span>
324
- <span class="k">else</span>
325
- <span class="s2">&quot;#{name}.__super__.constructor&quot;</span></pre></div> </td> </tr> <tr id="section-49"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-49">&#182;</a> </div> <p>Soaked chained invocations unfold into if/else ternary structures.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">unfoldSoak</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
326
- <span class="k">if</span> <span class="nx">@soak</span>
327
- <span class="k">if</span> <span class="nx">@variable</span>
328
- <span class="k">return</span> <span class="nx">ifn</span> <span class="k">if</span> <span class="nv">ifn = </span><span class="nx">unfoldSoak</span> <span class="nx">o</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="s1">&#39;variable&#39;</span>
329
- <span class="p">[</span><span class="nx">left</span><span class="p">,</span> <span class="nx">rite</span><span class="p">]</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">@variable</span><span class="p">).</span><span class="nx">cacheReference</span> <span class="nx">o</span>
330
- <span class="k">else</span>
331
- <span class="nv">left = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">@superReference</span> <span class="nx">o</span>
332
- <span class="nv">rite = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">left</span>
333
- <span class="nv">rite = </span><span class="k">new</span> <span class="nx">Call</span> <span class="nx">rite</span><span class="p">,</span> <span class="nx">@args</span>
334
- <span class="nv">rite.isNew = </span><span class="nx">@isNew</span>
335
- <span class="nv">left = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="s2">&quot;typeof #{ left.compile o } === \&quot;function\&quot;&quot;</span>
336
- <span class="k">return</span> <span class="k">new</span> <span class="nx">If</span> <span class="nx">left</span><span class="p">,</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">rite</span><span class="p">),</span> <span class="nx">soak</span><span class="o">:</span> <span class="kc">yes</span>
337
- <span class="nv">call = </span><span class="k">this</span>
338
- <span class="nv">list = </span><span class="p">[]</span>
339
- <span class="nx">loop</span>
340
- <span class="k">if</span> <span class="nx">call</span><span class="p">.</span><span class="nx">variable</span> <span class="k">instanceof</span> <span class="nx">Call</span>
341
- <span class="nx">list</span><span class="p">.</span><span class="nx">push</span> <span class="nx">call</span>
342
- <span class="nv">call = </span><span class="nx">call</span><span class="p">.</span><span class="nx">variable</span>
343
- <span class="k">continue</span>
344
- <span class="k">break</span> <span class="nx">unless</span> <span class="nx">call</span><span class="p">.</span><span class="nx">variable</span> <span class="k">instanceof</span> <span class="nx">Value</span>
345
- <span class="nx">list</span><span class="p">.</span><span class="nx">push</span> <span class="nx">call</span>
346
- <span class="k">break</span> <span class="nx">unless</span> <span class="p">(</span><span class="nv">call = </span><span class="nx">call</span><span class="p">.</span><span class="nx">variable</span><span class="p">.</span><span class="nx">base</span><span class="p">)</span> <span class="k">instanceof</span> <span class="nx">Call</span>
347
- <span class="k">for</span> <span class="nx">call</span> <span class="k">in</span> <span class="nx">list</span><span class="p">.</span><span class="nx">reverse</span><span class="p">()</span>
348
- <span class="k">if</span> <span class="nx">ifn</span>
349
- <span class="k">if</span> <span class="nx">call</span><span class="p">.</span><span class="nx">variable</span> <span class="k">instanceof</span> <span class="nx">Call</span>
350
- <span class="nv">call.variable = </span><span class="nx">ifn</span>
351
- <span class="k">else</span>
352
- <span class="nv">call.variable.base = </span><span class="nx">ifn</span>
353
- <span class="nv">ifn = </span><span class="nx">unfoldSoak</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">call</span><span class="p">,</span> <span class="s1">&#39;variable&#39;</span>
354
- <span class="nx">ifn</span></pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">&#182;</a> </div> <p>Compile a vanilla function call.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
355
- <span class="nx">@variable</span><span class="o">?</span><span class="p">.</span><span class="nv">front = </span><span class="nx">@front</span>
356
- <span class="k">if</span> <span class="nv">code = </span><span class="nx">Splat</span><span class="p">.</span><span class="nx">compileSplattedArray</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">@args</span><span class="p">,</span> <span class="kc">true</span>
357
- <span class="k">return</span> <span class="nx">@compileSplat</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">code</span>
358
- <span class="nv">args = </span><span class="p">(</span><span class="nx">arg</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span> <span class="k">for</span> <span class="nx">arg</span> <span class="k">in</span> <span class="nx">@args</span><span class="p">).</span><span class="nx">join</span> <span class="s1">&#39;, &#39;</span>
359
- <span class="k">if</span> <span class="nx">@isSuper</span>
360
- <span class="nx">@superReference</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;.call(this#{ args and &#39;, &#39; + args })&quot;</span>
361
- <span class="k">else</span>
362
- <span class="p">(</span><span class="k">if</span> <span class="nx">@isNew</span> <span class="k">then</span> <span class="s1">&#39;new &#39;</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_ACCESS</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;(#{args})&quot;</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">&#182;</a> </div> <p><code>super()</code> is converted into a call against the superclass's implementation
363
- of the current function.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileSuper</span><span class="o">:</span> <span class="p">(</span><span class="nx">args</span><span class="p">,</span> <span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
364
- <span class="s2">&quot;#{@superReference(o)}.call(this#{ if args.length then &#39;, &#39; else &#39;&#39; }#{args})&quot;</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-52">&#182;</a> </div> <p>If you call a function with a splat, it's converted into a JavaScript
365
- <code>.apply()</code> call to allow an array of arguments to be passed.
366
- If it's a constructor, then things get real tricky. We have to inject an
367
- inner constructor in order to be able to pass the varargs.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileSplat</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">splatArgs</span><span class="p">)</span> <span class="o">-&gt;</span>
368
- <span class="k">return</span> <span class="s2">&quot;#{ @superReference o }.apply(this, #{splatArgs})&quot;</span> <span class="k">if</span> <span class="nx">@isSuper</span>
369
- <span class="k">if</span> <span class="nx">@isNew</span>
370
- <span class="nv">idt = </span><span class="nx">@tab</span> <span class="o">+</span> <span class="nx">TAB</span>
371
- <span class="k">return</span> <span class="s2">&quot;&quot;&quot;</span>
372
- <span class="s2"> (function(func, args, ctor) {</span>
373
- <span class="s2"> #{idt}ctor.prototype = func.prototype;</span>
374
- <span class="s2"> #{idt}var child = new ctor, result = func.apply(child, args);</span>
375
- <span class="s2"> #{idt}return typeof result === &quot;</span><span class="nx">object</span><span class="s2">&quot; ? result : child;</span>
376
- <span class="s2"> #{@tab}})(#{ @variable.compile o, LEVEL_LIST }, #{splatArgs}, function() {})</span>
377
- <span class="s2"> &quot;&quot;&quot;</span>
378
- <span class="nv">base = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">@variable</span>
379
- <span class="k">if</span> <span class="p">(</span><span class="nv">name = </span><span class="nx">base</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">pop</span><span class="p">())</span> <span class="o">and</span> <span class="nx">base</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
380
- <span class="nv">ref = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;ref&#39;</span>
381
- <span class="nv">fun = </span><span class="s2">&quot;(#{ref} = #{ base.compile o, LEVEL_LIST })#{ name.compile o }&quot;</span>
382
- <span class="k">else</span>
383
- <span class="nv">fun = </span><span class="nx">base</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_ACCESS</span>
384
- <span class="k">if</span> <span class="nx">name</span>
385
- <span class="nv">ref = </span><span class="nx">fun</span>
386
- <span class="nx">fun</span> <span class="o">+=</span> <span class="nx">name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
387
- <span class="k">else</span>
388
- <span class="nv">ref = </span><span class="s1">&#39;null&#39;</span>
389
- <span class="s2">&quot;#{fun}.apply(#{ref}, #{splatArgs})&quot;</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-53">&#182;</a> </div> <h3>Extends</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-54">&#182;</a> </div> <p>Node to extend an object's prototype with an ancestor object.
390
- After <code>goog.inherits</code> from the
391
- <a href="http://closure-library.googlecode.com/svn/docs/closureGoogBase.js.html">Closure Library</a>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Extends = </span><span class="nx">class</span> <span class="nx">Extends</span> <span class="k">extends</span> <span class="nx">Base</span>
392
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@child</span><span class="p">,</span> <span class="nx">@parent</span><span class="p">)</span> <span class="o">-&gt;</span>
393
-
394
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;child&#39;</span><span class="p">,</span> <span class="s1">&#39;parent&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-55">&#182;</a> </div> <p>Hooks one constructor into another's prototype chain.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
395
- <span class="nx">utility</span> <span class="s1">&#39;hasProp&#39;</span>
396
- <span class="k">new</span> <span class="nx">Call</span><span class="p">(</span><span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">utility</span> <span class="s1">&#39;extends&#39;</span><span class="p">),</span> <span class="p">[</span><span class="nx">@child</span><span class="p">,</span> <span class="nx">@parent</span><span class="p">]).</span><span class="nx">compile</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-56">&#182;</a> </div> <h3>Access</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-57">&#182;</a> </div> <p>A <code>.</code> access into a property of a value, or the <code>::</code> shorthand for
397
- an access into the object's prototype.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Access = </span><span class="nx">class</span> <span class="nx">Access</span> <span class="k">extends</span> <span class="nx">Base</span>
398
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@name</span><span class="p">,</span> <span class="nx">tag</span><span class="p">)</span> <span class="o">-&gt;</span>
399
- <span class="vi">@name.asKey = </span><span class="kc">yes</span>
400
- <span class="vi">@proto = </span><span class="k">if</span> <span class="nx">tag</span> <span class="o">is</span> <span class="s1">&#39;proto&#39;</span> <span class="k">then</span> <span class="s1">&#39;.prototype&#39;</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span>
401
- <span class="vi">@soak = </span><span class="nx">tag</span> <span class="o">is</span> <span class="s1">&#39;soak&#39;</span>
402
-
403
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span>
404
-
405
- <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
406
- <span class="nv">name = </span><span class="nx">@name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
407
- <span class="nx">@proto</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">IS_STRING</span><span class="p">.</span><span class="nx">test</span> <span class="nx">name</span> <span class="k">then</span> <span class="s2">&quot;[#{name}]&quot;</span> <span class="k">else</span> <span class="s2">&quot;.#{name}&quot;</span>
408
-
409
- <span class="nx">isComplex</span><span class="o">:</span> <span class="nx">NO</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-58">&#182;</a> </div> <h3>Index</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">&#182;</a> </div> <p>A <code>[ ... ]</code> indexed access into an array or object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Index = </span><span class="nx">class</span> <span class="nx">Index</span> <span class="k">extends</span> <span class="nx">Base</span>
410
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@index</span><span class="p">)</span> <span class="o">-&gt;</span>
411
-
412
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;index&#39;</span><span class="p">]</span>
413
-
414
- <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
415
- <span class="p">(</span><span class="k">if</span> <span class="nx">@proto</span> <span class="k">then</span> <span class="s1">&#39;.prototype&#39;</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;[#{ @index.compile o, LEVEL_PAREN }]&quot;</span>
416
-
417
- <span class="nx">isComplex</span><span class="o">:</span> <span class="o">-&gt;</span>
418
- <span class="nx">@index</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-60"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-60">&#182;</a> </div> <h3>Range</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">&#182;</a> </div> <p>A range literal. Ranges can be used to extract portions (slices) of arrays,
419
- to specify a range for comprehensions, or as a value, to be expanded into the
420
- corresponding array of integers at runtime.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Range = </span><span class="nx">class</span> <span class="nx">Range</span> <span class="k">extends</span> <span class="nx">Base</span>
421
-
422
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;from&#39;</span><span class="p">,</span> <span class="s1">&#39;to&#39;</span><span class="p">]</span>
423
-
424
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@from</span><span class="p">,</span> <span class="nx">@to</span><span class="p">,</span> <span class="nx">tag</span><span class="p">)</span> <span class="o">-&gt;</span>
425
- <span class="vi">@exclusive = </span><span class="nx">tag</span> <span class="o">is</span> <span class="s1">&#39;exclusive&#39;</span>
426
- <span class="vi">@equals = </span><span class="k">if</span> <span class="nx">@exclusive</span> <span class="k">then</span> <span class="s1">&#39;&#39;</span> <span class="k">else</span> <span class="s1">&#39;=&#39;</span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">&#182;</a> </div> <p>Compiles the range's source variables -- where it starts and where it ends.
427
- But only if they need to be cached to avoid double evaluation.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileVariables</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
428
- <span class="nv">o = </span><span class="nx">merge</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">top</span><span class="o">:</span> <span class="kc">true</span><span class="p">)</span>
429
- <span class="p">[</span><span class="nx">@from</span><span class="p">,</span> <span class="nx">@fromVar</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@from</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
430
- <span class="p">[</span><span class="nx">@to</span><span class="p">,</span> <span class="nx">@toVar</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@to</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
431
- <span class="p">[</span><span class="nx">@fromNum</span><span class="p">,</span> <span class="nx">@toNum</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nx">@fromVar</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">SIMPLENUM</span><span class="p">),</span> <span class="nx">@toVar</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">SIMPLENUM</span><span class="p">)]</span>
432
- <span class="nv">parts = </span><span class="p">[]</span>
433
- <span class="nx">parts</span><span class="p">.</span><span class="nx">push</span> <span class="nx">@from</span> <span class="k">if</span> <span class="nx">@from</span> <span class="o">isnt</span> <span class="nx">@fromVar</span>
434
- <span class="nx">parts</span><span class="p">.</span><span class="nx">push</span> <span class="nx">@to</span> <span class="k">if</span> <span class="nx">@to</span> <span class="o">isnt</span> <span class="nx">@toVar</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-63">&#182;</a> </div> <p>When compiled normally, the range returns the contents of the <em>for loop</em>
435
- needed to iterate over the values in the range. Used by comprehensions.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
436
- <span class="nx">@compileVariables</span> <span class="nx">o</span>
437
- <span class="k">return</span> <span class="nx">@compileArray</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="nx">unless</span> <span class="nx">o</span><span class="p">.</span><span class="nx">index</span>
438
- <span class="k">return</span> <span class="nx">@compileSimple</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@fromNum</span> <span class="o">and</span> <span class="nx">@toNum</span>
439
- <span class="nv">idx = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">&#39;index&#39;</span>
440
- <span class="nv">step = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">&#39;step&#39;</span>
441
- <span class="nv">vars = </span><span class="s2">&quot;#{idx} = #{@from}&quot;</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@to</span> <span class="o">isnt</span> <span class="nx">@toVar</span> <span class="k">then</span> <span class="s2">&quot;, #{@to}&quot;</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span>
442
- <span class="nv">intro = </span><span class="s2">&quot;(#{@fromVar} &lt;= #{@toVar} ? #{idx}&quot;</span>
443
- <span class="nv">compare = </span><span class="s2">&quot;#{intro} &lt;#{@equals} #{@toVar} : #{idx} &gt;#{@equals} #{@toVar})&quot;</span>
444
- <span class="nv">stepPart = </span><span class="k">if</span> <span class="nx">step</span> <span class="k">then</span> <span class="nx">step</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">else</span> <span class="s1">&#39;1&#39;</span>
445
- <span class="nv">incr = </span><span class="k">if</span> <span class="nx">step</span> <span class="k">then</span> <span class="s2">&quot;#{idx} += #{stepPart}&quot;</span> <span class="k">else</span> <span class="s2">&quot;#{intro} += #{stepPart} : #{idx} -= #{stepPart})&quot;</span>
446
- <span class="s2">&quot;#{vars}; #{compare}; #{incr}&quot;</span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-64">&#182;</a> </div> <p>Compile a simple range comprehension, with integers.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileSimple</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
447
- <span class="p">[</span><span class="nx">from</span><span class="p">,</span> <span class="nx">to</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="o">+</span><span class="nx">@fromNum</span><span class="p">,</span> <span class="o">+</span><span class="nx">@toNum</span><span class="p">]</span>
448
- <span class="nv">idx = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">&#39;index&#39;</span>
449
- <span class="nv">step = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">&#39;step&#39;</span>
450
- <span class="nx">step</span> <span class="o">and=</span> <span class="s2">&quot;#{idx} += #{step.compile(o)}&quot;</span>
451
- <span class="k">if</span> <span class="nx">from</span> <span class="o">&lt;=</span> <span class="nx">to</span>
452
- <span class="s2">&quot;#{idx} = #{from}; #{idx} &lt;#{@equals} #{to}; #{step or &quot;</span><span class="c1">#{idx}++&quot;}&quot;</span>
453
- <span class="k">else</span>
454
- <span class="s2">&quot;#{idx} = #{from}; #{idx} &gt;#{@equals} #{to}; #{step or &quot;</span><span class="c1">#{idx}--&quot;}&quot;</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">&#182;</a> </div> <p>When used as a value, expand the range into the equivalent array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileArray</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
455
- <span class="k">if</span> <span class="nx">@fromNum</span> <span class="o">and</span> <span class="nx">@toNum</span> <span class="o">and</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">abs</span><span class="p">(</span><span class="nx">@fromNum</span> <span class="o">-</span> <span class="nx">@toNum</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">20</span>
456
- <span class="nv">range = </span><span class="p">[</span><span class="o">+</span><span class="nx">@fromNum</span><span class="p">..</span><span class="o">+</span><span class="nx">@toNum</span><span class="p">]</span>
457
- <span class="nx">range</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span> <span class="k">if</span> <span class="nx">@exclusive</span>
458
- <span class="k">return</span> <span class="s2">&quot;[#{ range.join(&#39;, &#39;) }]&quot;</span>
459
- <span class="nv">idt = </span><span class="nx">@tab</span> <span class="o">+</span> <span class="nx">TAB</span>
460
- <span class="nv">i = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;i&#39;</span>
461
- <span class="nv">result = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;results&#39;</span>
462
- <span class="nv">pre = </span><span class="s2">&quot;\n#{idt}#{result} = [];&quot;</span>
463
- <span class="k">if</span> <span class="nx">@fromNum</span> <span class="o">and</span> <span class="nx">@toNum</span>
464
- <span class="nv">o.index = </span><span class="nx">i</span>
465
- <span class="nv">body = </span><span class="nx">@compileSimple</span> <span class="nx">o</span>
466
- <span class="k">else</span>
467
- <span class="nv">vars = </span><span class="s2">&quot;#{i} = #{@from}&quot;</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@to</span> <span class="o">isnt</span> <span class="nx">@toVar</span> <span class="k">then</span> <span class="s2">&quot;, #{@to}&quot;</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span>
468
- <span class="nv">clause = </span><span class="s2">&quot;#{@fromVar} &lt;= #{@toVar} ?&quot;</span>
469
- <span class="nv">body = </span><span class="s2">&quot;var #{vars}; #{clause} #{i} &lt;#{@equals} #{@toVar} : #{i} &gt;#{@equals} #{@toVar}; #{clause} #{i} += 1 : #{i} -= 1&quot;</span>
470
- <span class="nv">post = </span><span class="s2">&quot;{ #{result}.push(#{i}); }\n#{idt}return #{result};\n#{o.indent}&quot;</span>
471
- <span class="s2">&quot;(function() {#{pre}\n#{idt}for (#{body})#{post}}).call(this)&quot;</span></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-66">&#182;</a> </div> <h3>Slice</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-67">&#182;</a> </div> <p>An array slice literal. Unlike JavaScript's <code>Array#slice</code>, the second parameter
472
- specifies the index of the end of the slice, just as the first parameter
473
- is the index of the beginning.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Slice = </span><span class="nx">class</span> <span class="nx">Slice</span> <span class="k">extends</span> <span class="nx">Base</span>
474
-
475
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;range&#39;</span><span class="p">]</span>
476
-
477
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@range</span><span class="p">)</span> <span class="o">-&gt;</span>
478
- <span class="k">super</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-68">&#182;</a> </div> <p>We have to be careful when trying to slice through the end of the array,
479
- <code>9e9</code> is used because not all implementations respect <code>undefined</code> or <code>1/0</code>.
480
- <code>9e9</code> should be safe because <code>9e9</code> > <code>2**32</code>, the max array length.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
481
- <span class="p">{</span><span class="nx">to</span><span class="p">,</span> <span class="nx">from</span><span class="p">}</span> <span class="o">=</span> <span class="nx">@range</span>
482
- <span class="nv">fromStr = </span><span class="nx">from</span> <span class="o">and</span> <span class="nx">from</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_PAREN</span><span class="p">)</span> <span class="o">or</span> <span class="s1">&#39;0&#39;</span>
483
- <span class="nv">compiled = </span><span class="nx">to</span> <span class="o">and</span> <span class="nx">to</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_PAREN</span>
484
- <span class="k">if</span> <span class="nx">to</span> <span class="o">and</span> <span class="o">not</span> <span class="p">(</span><span class="o">not</span> <span class="nx">@range</span><span class="p">.</span><span class="nx">exclusive</span> <span class="o">and</span> <span class="o">+</span><span class="nx">compiled</span> <span class="o">is</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
485
- <span class="nv">toStr = </span><span class="s1">&#39;, &#39;</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@range</span><span class="p">.</span><span class="nx">exclusive</span>
486
- <span class="nx">compiled</span>
487
- <span class="k">else</span> <span class="k">if</span> <span class="nx">SIMPLENUM</span><span class="p">.</span><span class="nx">test</span> <span class="nx">compiled</span>
488
- <span class="p">(</span><span class="o">+</span><span class="nx">compiled</span> <span class="o">+</span> <span class="mi">1</span><span class="p">).</span><span class="nx">toString</span><span class="p">()</span>
489
- <span class="k">else</span>
490
- <span class="s2">&quot;(#{compiled} + 1) || 9e9&quot;</span>
491
- <span class="s2">&quot;.slice(#{ fromStr }#{ toStr or &#39;&#39; })&quot;</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-69">&#182;</a> </div> <h3>Obj</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-70">&#182;</a> </div> <p>An object literal, nothing fancy.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Obj = </span><span class="nx">class</span> <span class="nx">Obj</span> <span class="k">extends</span> <span class="nx">Base</span>
492
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">props</span><span class="p">,</span> <span class="vi">@generated = </span><span class="kc">false</span><span class="p">)</span> <span class="o">-&gt;</span>
493
- <span class="vi">@objects = @properties = </span><span class="nx">props</span> <span class="o">or</span> <span class="p">[]</span>
494
-
495
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;properties&#39;</span><span class="p">]</span>
496
-
497
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
498
- <span class="nv">props = </span><span class="nx">@properties</span>
499
- <span class="k">return</span> <span class="p">(</span><span class="k">if</span> <span class="nx">@front</span> <span class="k">then</span> <span class="s1">&#39;({})&#39;</span> <span class="k">else</span> <span class="s1">&#39;{}&#39;</span><span class="p">)</span> <span class="nx">unless</span> <span class="nx">props</span><span class="p">.</span><span class="nx">length</span>
500
- <span class="nv">idt = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
501
- <span class="nv">lastNoncom = </span><span class="nx">@lastNonComment</span> <span class="nx">@properties</span>
502
- <span class="nv">props = </span><span class="k">for</span> <span class="nx">prop</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">props</span>
503
- <span class="nv">join = </span><span class="k">if</span> <span class="nx">i</span> <span class="o">is</span> <span class="nx">props</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span>
504
- <span class="s1">&#39;&#39;</span>
505
- <span class="k">else</span> <span class="k">if</span> <span class="nx">prop</span> <span class="o">is</span> <span class="nx">lastNoncom</span> <span class="o">or</span> <span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">Comment</span>
506
- <span class="s1">&#39;\n&#39;</span>
507
- <span class="k">else</span>
508
- <span class="s1">&#39;,\n&#39;</span>
509
- <span class="nv">indent = </span><span class="k">if</span> <span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">Comment</span> <span class="k">then</span> <span class="s1">&#39;&#39;</span> <span class="k">else</span> <span class="nx">idt</span>
510
- <span class="k">if</span> <span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span> <span class="nx">prop</span><span class="p">.</span><span class="k">this</span>
511
- <span class="nv">prop = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span><span class="p">,</span> <span class="nx">prop</span><span class="p">,</span> <span class="s1">&#39;object&#39;</span>
512
- <span class="k">if</span> <span class="nx">prop</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Comment</span>
513
- <span class="k">if</span> <span class="nx">prop</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Assign</span>
514
- <span class="nv">prop = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="nx">prop</span><span class="p">,</span> <span class="nx">prop</span><span class="p">,</span> <span class="s1">&#39;object&#39;</span>
515
- <span class="p">(</span><span class="nx">prop</span><span class="p">.</span><span class="nx">variable</span><span class="p">.</span><span class="nx">base</span> <span class="o">or</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">variable</span><span class="p">).</span><span class="nv">asKey = </span><span class="kc">yes</span>
516
- <span class="nx">indent</span> <span class="o">+</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_TOP</span><span class="p">)</span> <span class="o">+</span> <span class="nx">join</span>
517
- <span class="nv">props = </span><span class="nx">props</span><span class="p">.</span><span class="nx">join</span> <span class="s1">&#39;&#39;</span>
518
- <span class="nv">obj = </span><span class="s2">&quot;{#{ props and &#39;\n&#39; + props + &#39;\n&#39; + @tab }}&quot;</span>
519
- <span class="k">if</span> <span class="nx">@front</span> <span class="k">then</span> <span class="s2">&quot;(#{obj})&quot;</span> <span class="k">else</span> <span class="nx">obj</span>
520
-
521
- <span class="nx">assigns</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
522
- <span class="k">for</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">@properties</span> <span class="k">when</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">assigns</span> <span class="nx">name</span> <span class="k">then</span> <span class="k">return</span> <span class="kc">yes</span>
523
- <span class="kc">no</span></pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-71">&#182;</a> </div> <h3>Arr</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-72"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-72">&#182;</a> </div> <p>An array literal.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Arr = </span><span class="nx">class</span> <span class="nx">Arr</span> <span class="k">extends</span> <span class="nx">Base</span>
524
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">objs</span><span class="p">)</span> <span class="o">-&gt;</span>
525
- <span class="vi">@objects = </span><span class="nx">objs</span> <span class="o">or</span> <span class="p">[]</span>
526
-
527
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;objects&#39;</span><span class="p">]</span>
528
-
529
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
530
- <span class="k">return</span> <span class="s1">&#39;[]&#39;</span> <span class="nx">unless</span> <span class="nx">@objects</span><span class="p">.</span><span class="nx">length</span>
531
- <span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
532
- <span class="k">return</span> <span class="nx">code</span> <span class="k">if</span> <span class="nv">code = </span><span class="nx">Splat</span><span class="p">.</span><span class="nx">compileSplattedArray</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">@objects</span>
533
- <span class="nv">code = </span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span> <span class="k">for</span> <span class="nx">obj</span> <span class="k">in</span> <span class="nx">@objects</span><span class="p">).</span><span class="nx">join</span> <span class="s1">&#39;, &#39;</span>
534
- <span class="k">if</span> <span class="nx">code</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">&#39;\n&#39;</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span>
535
- <span class="s2">&quot;[\n#{o.indent}#{code}\n#{@tab}]&quot;</span>
536
- <span class="k">else</span>
537
- <span class="s2">&quot;[#{code}]&quot;</span>
538
-
539
- <span class="nx">assigns</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
540
- <span class="k">for</span> <span class="nx">obj</span> <span class="k">in</span> <span class="nx">@objects</span> <span class="k">when</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">assigns</span> <span class="nx">name</span> <span class="k">then</span> <span class="k">return</span> <span class="kc">yes</span>
541
- <span class="kc">no</span></pre></div> </td> </tr> <tr id="section-73"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-73">&#182;</a> </div> <h3>Class</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-74"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-74">&#182;</a> </div> <p>The CoffeeScript class definition.
542
- Initialize a <strong>Class</strong> with its name, an optional superclass, and a
543
- list of prototype property assignments.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Class = </span><span class="nx">class</span> <span class="nx">Class</span> <span class="k">extends</span> <span class="nx">Base</span>
544
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@variable</span><span class="p">,</span> <span class="nx">@parent</span><span class="p">,</span> <span class="vi">@body = </span><span class="k">new</span> <span class="nx">Expressions</span><span class="p">)</span> <span class="o">-&gt;</span>
545
- <span class="vi">@boundFuncs = </span><span class="p">[]</span>
546
- <span class="vi">@body.classBody = </span><span class="kc">yes</span>
547
-
548
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;variable&#39;</span><span class="p">,</span> <span class="s1">&#39;parent&#39;</span><span class="p">,</span> <span class="s1">&#39;body&#39;</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-75"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-75">&#182;</a> </div> <p>Figure out the appropriate name for the constructor function of this class.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">determineName</span><span class="o">:</span> <span class="o">-&gt;</span>
549
- <span class="k">return</span> <span class="kc">null</span> <span class="nx">unless</span> <span class="nx">@variable</span>
550
- <span class="nv">decl = </span><span class="k">if</span> <span class="nv">tail = </span><span class="nx">last</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">properties</span>
551
- <span class="nx">tail</span> <span class="k">instanceof</span> <span class="nx">Access</span> <span class="o">and</span> <span class="nx">tail</span><span class="p">.</span><span class="nx">name</span><span class="p">.</span><span class="nx">value</span>
552
- <span class="k">else</span>
553
- <span class="nx">@variable</span><span class="p">.</span><span class="nx">base</span><span class="p">.</span><span class="nx">value</span>
554
- <span class="nx">decl</span> <span class="o">and=</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">decl</span><span class="p">)</span> <span class="o">and</span> <span class="nx">decl</span></pre></div> </td> </tr> <tr id="section-76"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-76">&#182;</a> </div> <p>For all <code>this</code>-references and bound functions in the class definition,
555
- <code>this</code> is the Class being constructed.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">setContext</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
556
- <span class="nx">@body</span><span class="p">.</span><span class="nx">traverseChildren</span> <span class="kc">false</span><span class="p">,</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-&gt;</span>
557
- <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">classBody</span>
558
- <span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Literal</span> <span class="o">and</span> <span class="nx">node</span><span class="p">.</span><span class="nx">value</span> <span class="o">is</span> <span class="s1">&#39;this&#39;</span>
559
- <span class="nv">node.value = </span><span class="nx">name</span>
560
- <span class="k">else</span> <span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Code</span>
561
- <span class="nv">node.klass = </span><span class="nx">name</span>
562
- <span class="nv">node.context = </span><span class="nx">name</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">bound</span></pre></div> </td> </tr> <tr id="section-77"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-77">&#182;</a> </div> <p>Ensure that all functions bound to the instance are proxied in the
563
- constructor.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addBoundFunctions</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
564
- <span class="k">if</span> <span class="nx">@boundFuncs</span><span class="p">.</span><span class="nx">length</span>
565
- <span class="k">for</span> <span class="nx">bvar</span> <span class="k">in</span> <span class="nx">@boundFuncs</span>
566
- <span class="nv">bname = </span><span class="nx">bvar</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
567
- <span class="nx">@ctor</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">unshift</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s2">&quot;this.#{bname} = #{utility &#39;bind&#39;}(this.#{bname}, this);&quot;</span></pre></div> </td> </tr> <tr id="section-78"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-78">&#182;</a> </div> <p>Merge the properties from a top-level object as prototypal properties
568
- on the class.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addProperties</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">,</span> <span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
569
- <span class="nv">props = </span><span class="nx">node</span><span class="p">.</span><span class="nx">base</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">slice</span> <span class="mi">0</span>
570
- <span class="k">while</span> <span class="nv">assign = </span><span class="nx">props</span><span class="p">.</span><span class="nx">shift</span><span class="p">()</span>
571
- <span class="k">if</span> <span class="nx">assign</span> <span class="k">instanceof</span> <span class="nx">Assign</span>
572
- <span class="nv">base = </span><span class="nx">assign</span><span class="p">.</span><span class="nx">variable</span><span class="p">.</span><span class="nx">base</span>
573
- <span class="k">delete</span> <span class="nx">assign</span><span class="p">.</span><span class="nx">context</span>
574
- <span class="nv">func = </span><span class="nx">assign</span><span class="p">.</span><span class="nx">value</span>
575
- <span class="k">if</span> <span class="nx">base</span><span class="p">.</span><span class="nx">value</span> <span class="o">is</span> <span class="s1">&#39;constructor&#39;</span>
576
- <span class="k">if</span> <span class="nx">@ctor</span>
577
- <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s1">&#39;cannot define more than one constructor in a class&#39;</span>
578
- <span class="k">if</span> <span class="nx">func</span><span class="p">.</span><span class="nx">bound</span>
579
- <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s1">&#39;cannot define a constructor as a bound function&#39;</span>
580
- <span class="k">if</span> <span class="nx">func</span> <span class="k">instanceof</span> <span class="nx">Code</span>
581
- <span class="nv">assign = </span><span class="vi">@ctor = </span><span class="nx">func</span>
582
- <span class="k">else</span>
583
- <span class="nv">assign = </span><span class="vi">@ctor = </span><span class="k">new</span> <span class="nx">Assign</span><span class="p">(</span><span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">name</span><span class="p">),</span> <span class="nx">func</span><span class="p">)</span>
584
- <span class="k">else</span>
585
- <span class="nx">unless</span> <span class="nx">assign</span><span class="p">.</span><span class="nx">variable</span><span class="p">.</span><span class="k">this</span>
586
- <span class="nv">assign.variable = </span><span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span><span class="p">(</span><span class="nx">name</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">Access</span><span class="p">(</span><span class="nx">base</span><span class="p">,</span> <span class="s1">&#39;proto&#39;</span><span class="p">)])</span>
587
- <span class="k">if</span> <span class="nx">func</span> <span class="k">instanceof</span> <span class="nx">Code</span> <span class="o">and</span> <span class="nx">func</span><span class="p">.</span><span class="nx">bound</span>
588
- <span class="nx">@boundFuncs</span><span class="p">.</span><span class="nx">push</span> <span class="nx">base</span>
589
- <span class="nv">func.bound = </span><span class="kc">no</span>
590
- <span class="nx">assign</span></pre></div> </td> </tr> <tr id="section-79"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-79">&#182;</a> </div> <p>Walk the body of the class, looking for prototype properties to be converted.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">walkBody</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
591
- <span class="nx">@traverseChildren</span> <span class="kc">false</span><span class="p">,</span> <span class="p">(</span><span class="nx">child</span><span class="p">)</span> <span class="o">=&gt;</span>
592
- <span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">child</span> <span class="k">instanceof</span> <span class="nx">Class</span>
593
- <span class="k">if</span> <span class="nx">child</span> <span class="k">instanceof</span> <span class="nx">Expressions</span>
594
- <span class="k">for</span> <span class="nx">node</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nv">exps = </span><span class="nx">child</span><span class="p">.</span><span class="nx">expressions</span>
595
- <span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span> <span class="nx">node</span><span class="p">.</span><span class="nx">isObject</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span>
596
- <span class="nx">exps</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@addProperties</span> <span class="nx">node</span><span class="p">,</span> <span class="nx">name</span>
597
- <span class="nv">child.expressions = exps = </span><span class="nx">flatten</span> <span class="nx">exps</span></pre></div> </td> </tr> <tr id="section-80"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-80">&#182;</a> </div> <p>Make sure that a constructor is defined for the class, and properly
598
- configured.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ensureConstructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
599
- <span class="k">if</span> <span class="o">not</span> <span class="nx">@ctor</span>
600
- <span class="vi">@ctor = </span><span class="k">new</span> <span class="nx">Code</span>
601
- <span class="nx">@ctor</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">Call</span> <span class="s1">&#39;super&#39;</span><span class="p">,</span> <span class="p">[</span><span class="k">new</span> <span class="nx">Splat</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s1">&#39;arguments&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="nx">@parent</span>
602
- <span class="nx">@body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">@ctor</span>
603
- <span class="vi">@ctor.ctor = @ctor.name = </span><span class="nx">name</span>
604
- <span class="vi">@ctor.klass = </span><span class="kc">null</span>
605
- <span class="vi">@ctor.noReturn = </span><span class="kc">yes</span></pre></div> </td> </tr> <tr id="section-81"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-81">&#182;</a> </div> <p>Instead of generating the JavaScript string directly, we build up the
606
- equivalent syntax tree and compile that, in pieces. You can see the
607
- constructor, property assignments, and inheritance getting built out below.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
608
- <span class="nv">decl = </span><span class="nx">@determineName</span><span class="p">()</span>
609
- <span class="nv">name = </span><span class="nx">decl</span> <span class="o">or</span> <span class="nx">@name</span> <span class="o">or</span> <span class="s1">&#39;_Class&#39;</span>
610
- <span class="nv">lname = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">name</span>
611
-
612
- <span class="nx">@setContext</span> <span class="nx">name</span>
613
- <span class="nx">@walkBody</span> <span class="nx">name</span>
614
- <span class="nx">@body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">unshift</span> <span class="k">new</span> <span class="nx">Extends</span> <span class="nx">lname</span><span class="p">,</span> <span class="nx">@parent</span> <span class="k">if</span> <span class="nx">@parent</span>
615
- <span class="nx">@ensureConstructor</span> <span class="nx">name</span>
616
- <span class="nx">@body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">push</span> <span class="nx">lname</span>
617
- <span class="nx">@addBoundFunctions</span> <span class="nx">o</span>
618
-
619
- <span class="nv">klass = </span><span class="k">new</span> <span class="nx">Parens</span> <span class="nx">Closure</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="nx">@body</span><span class="p">),</span> <span class="kc">true</span>
620
- <span class="nv">klass = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="nx">@variable</span><span class="p">,</span> <span class="nx">klass</span> <span class="k">if</span> <span class="nx">@variable</span>
621
- <span class="nx">klass</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-82"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-82">&#182;</a> </div> <h3>Assign</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-83"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-83">&#182;</a> </div> <p>The <strong>Assign</strong> is used to assign a local variable to value, or to set the
622
- property of an object -- including within object literals.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Assign = </span><span class="nx">class</span> <span class="nx">Assign</span> <span class="k">extends</span> <span class="nx">Base</span>
623
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@variable</span><span class="p">,</span> <span class="nx">@value</span><span class="p">,</span> <span class="nx">@context</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">-&gt;</span>
624
- <span class="vi">@param = </span><span class="nx">options</span> <span class="o">and</span> <span class="nx">options</span><span class="p">.</span><span class="nx">param</span></pre></div> </td> </tr> <tr id="section-84"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-84">&#182;</a> </div> <p>Matchers for detecting class/method names</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">METHOD_DEF</span><span class="o">:</span> <span class="sr">/^(?:(\S+)\.prototype\.|\S+?)?\b([$A-Za-z_][$\w]*)$/</span>
625
-
626
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;variable&#39;</span><span class="p">,</span> <span class="s1">&#39;value&#39;</span><span class="p">]</span>
627
-
628
- <span class="nx">assigns</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
629
- <span class="err">@</span><span class="p">[</span><span class="k">if</span> <span class="nx">@context</span> <span class="o">is</span> <span class="s1">&#39;object&#39;</span> <span class="k">then</span> <span class="s1">&#39;value&#39;</span> <span class="k">else</span> <span class="s1">&#39;variable&#39;</span><span class="p">].</span><span class="nx">assigns</span> <span class="nx">name</span>
630
-
631
- <span class="nx">unfoldSoak</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
632
- <span class="nx">unfoldSoak</span> <span class="nx">o</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="s1">&#39;variable&#39;</span></pre></div> </td> </tr> <tr id="section-85"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-85">&#182;</a> </div> <p>Compile an assignment, delegating to <code>compilePatternMatch</code> or
633
- <code>compileSplice</code> if appropriate. Keep track of the name of the base object
634
- we've been assigned to, for correct internal references. If the variable
635
- has not been seen yet within the current scope, declare it.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
636
- <span class="k">if</span> <span class="nv">isValue = </span><span class="nx">@variable</span> <span class="k">instanceof</span> <span class="nx">Value</span>
637
- <span class="k">return</span> <span class="nx">@compilePatternMatch</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">isArray</span><span class="p">()</span> <span class="o">or</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">isObject</span><span class="p">()</span>
638
- <span class="k">return</span> <span class="nx">@compileSplice</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">isSplice</span><span class="p">()</span>
639
- <span class="k">return</span> <span class="nx">@compileConditional</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@context</span> <span class="k">in</span> <span class="p">[</span><span class="s1">&#39;||=&#39;</span><span class="p">,</span> <span class="s1">&#39;&amp;&amp;=&#39;</span><span class="p">,</span> <span class="s1">&#39;?=&#39;</span><span class="p">]</span>
640
- <span class="nv">name = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
641
- <span class="k">if</span> <span class="nx">@value</span> <span class="k">instanceof</span> <span class="nx">Code</span> <span class="o">and</span> <span class="nv">match = </span><span class="nx">@METHOD_DEF</span><span class="p">.</span><span class="nx">exec</span> <span class="nx">name</span>
642
- <span class="vi">@value.name = </span><span class="nx">match</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
643
- <span class="vi">@value.klass = </span><span class="nx">match</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">if</span> <span class="nx">match</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
644
- <span class="nv">val = </span><span class="nx">@value</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
645
- <span class="k">return</span> <span class="s2">&quot;#{name}: #{val}&quot;</span> <span class="k">if</span> <span class="nx">@context</span> <span class="o">is</span> <span class="s1">&#39;object&#39;</span>
646
- <span class="nx">unless</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">isAssignable</span><span class="p">()</span>
647
- <span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s2">&quot;\&quot;#{ @variable.compile o }\&quot; cannot be assigned.&quot;</span>
648
- <span class="nx">unless</span> <span class="nx">@context</span> <span class="o">or</span> <span class="nx">isValue</span> <span class="o">and</span> <span class="p">(</span><span class="nx">@variable</span><span class="p">.</span><span class="nx">namespaced</span> <span class="o">or</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">hasProperties</span><span class="p">())</span>
649
- <span class="k">if</span> <span class="nx">@param</span>
650
- <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">add</span> <span class="nx">name</span><span class="p">,</span> <span class="s1">&#39;var&#39;</span>
651
- <span class="k">else</span>
652
- <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">find</span> <span class="nx">name</span>
653
- <span class="nv">val = </span><span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot; #{ @context or &#39;=&#39; } &quot;</span> <span class="o">+</span> <span class="nx">val</span>
654
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&lt;=</span> <span class="nx">LEVEL_LIST</span> <span class="k">then</span> <span class="nx">val</span> <span class="k">else</span> <span class="s2">&quot;(#{val})&quot;</span></pre></div> </td> </tr> <tr id="section-86"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-86">&#182;</a> </div> <p>Brief implementation of recursive pattern matching, when assigning array or
655
- object literals to a value. Peeks at their properties to assign inner names.
656
- See the <a href="http://wiki.ecmascript.org/doku.php?id=harmony:destructuring">ECMAScript Harmony Wiki</a>
657
- for details.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compilePatternMatch</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
658
- <span class="nv">top = </span><span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">is</span> <span class="nx">LEVEL_TOP</span>
659
- <span class="p">{</span><span class="nx">value</span><span class="p">}</span> <span class="o">=</span> <span class="k">this</span>
660
- <span class="p">{</span><span class="nx">objects</span><span class="p">}</span> <span class="o">=</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">base</span>
661
- <span class="k">return</span> <span class="nx">value</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span> <span class="nx">unless</span> <span class="nv">olen = </span><span class="nx">objects</span><span class="p">.</span><span class="nx">length</span>
662
- <span class="nv">isObject = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">isObject</span><span class="p">()</span>
663
- <span class="k">if</span> <span class="nx">top</span> <span class="o">and</span> <span class="nx">olen</span> <span class="o">is</span> <span class="mi">1</span> <span class="o">and</span> <span class="p">(</span><span class="nv">obj = </span><span class="nx">objects</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Splat</span></pre></div> </td> </tr> <tr id="section-87"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-87">&#182;</a> </div> <p>Unroll simplest cases: <code>{v} = x</code> -> <code>v = x.v</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">Assign</span>
664
- <span class="p">{</span><span class="nx">variable</span><span class="o">:</span> <span class="p">{</span><span class="nx">base</span><span class="o">:</span> <span class="nx">idx</span><span class="p">},</span> <span class="nx">value</span><span class="o">:</span> <span class="nx">obj</span><span class="p">}</span> <span class="o">=</span> <span class="nx">obj</span>
665
- <span class="k">else</span>
666
- <span class="k">if</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">base</span> <span class="k">instanceof</span> <span class="nx">Parens</span>
667
- <span class="p">[</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">idx</span><span class="p">]</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()).</span><span class="nx">cacheReference</span> <span class="nx">o</span>
668
- <span class="k">else</span>
669
- <span class="nv">idx = </span><span class="k">if</span> <span class="nx">isObject</span>
670
- <span class="k">if</span> <span class="nx">obj</span><span class="p">.</span><span class="k">this</span> <span class="k">then</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span> <span class="k">else</span> <span class="nx">obj</span>
671
- <span class="k">else</span>
672
- <span class="k">new</span> <span class="nx">Literal</span> <span class="mi">0</span>
673
- <span class="nv">acc = </span><span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span> <span class="nx">idx</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">().</span><span class="nx">value</span> <span class="o">or</span> <span class="mi">0</span>
674
- <span class="nv">value = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">value</span>
675
- <span class="nx">value</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="p">(</span><span class="k">if</span> <span class="nx">acc</span> <span class="k">then</span> <span class="nx">Access</span> <span class="k">else</span> <span class="nx">Index</span><span class="p">)</span> <span class="nx">idx</span>
676
- <span class="k">return</span> <span class="k">new</span> <span class="nx">Assign</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">value</span><span class="p">).</span><span class="nx">compile</span> <span class="nx">o</span>
677
- <span class="nv">vvar = </span><span class="nx">value</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
678
- <span class="nv">assigns = </span><span class="p">[]</span>
679
- <span class="nv">splat = </span><span class="kc">false</span>
680
- <span class="k">if</span> <span class="o">not</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">vvar</span><span class="p">)</span> <span class="o">or</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">assigns</span><span class="p">(</span><span class="nx">vvar</span><span class="p">)</span>
681
- <span class="nx">assigns</span><span class="p">.</span><span class="nx">push</span> <span class="s2">&quot;#{ ref = o.scope.freeVariable &#39;ref&#39; } = #{vvar}&quot;</span>
682
- <span class="nv">vvar = </span><span class="nx">ref</span>
683
- <span class="k">for</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">objects</span></pre></div> </td> </tr> <tr id="section-88"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-88">&#182;</a> </div> <p>A regular array pattern-match.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">idx = </span><span class="nx">i</span>
684
- <span class="k">if</span> <span class="nx">isObject</span>
685
- <span class="k">if</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">Assign</span></pre></div> </td> </tr> <tr id="section-89"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-89">&#182;</a> </div> <p>A regular object pattern-match.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="p">{</span><span class="nx">variable</span><span class="o">:</span> <span class="p">{</span><span class="nx">base</span><span class="o">:</span> <span class="nx">idx</span><span class="p">},</span> <span class="nx">value</span><span class="o">:</span> <span class="nx">obj</span><span class="p">}</span> <span class="o">=</span> <span class="nx">obj</span>
686
- <span class="k">else</span></pre></div> </td> </tr> <tr id="section-90"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-90">&#182;</a> </div> <p>A shorthand <code>{a, b, @c} = val</code> pattern-match.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">base</span> <span class="k">instanceof</span> <span class="nx">Parens</span>
687
- <span class="p">[</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">idx</span><span class="p">]</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()).</span><span class="nx">cacheReference</span> <span class="nx">o</span>
688
- <span class="k">else</span>
689
- <span class="nv">idx = </span><span class="k">if</span> <span class="nx">obj</span><span class="p">.</span><span class="k">this</span> <span class="k">then</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span> <span class="k">else</span> <span class="nx">obj</span>
690
- <span class="k">if</span> <span class="o">not</span> <span class="nx">splat</span> <span class="o">and</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">Splat</span>
691
- <span class="nv">val = </span><span class="s2">&quot;#{olen} &lt;= #{vvar}.length ? #{ utility &#39;slice&#39; }.call(#{vvar}, #{i}&quot;</span>
692
- <span class="k">if</span> <span class="nv">rest = </span><span class="nx">olen</span> <span class="o">-</span> <span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span>
693
- <span class="nv">ivar = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;i&#39;</span>
694
- <span class="nx">val</span> <span class="o">+=</span> <span class="s2">&quot;, #{ivar} = #{vvar}.length - #{rest}) : (#{ivar} = #{i}, [])&quot;</span>
695
- <span class="k">else</span>
696
- <span class="nx">val</span> <span class="o">+=</span> <span class="s2">&quot;) : []&quot;</span>
697
- <span class="nv">val = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">val</span>
698
- <span class="nv">splat = </span><span class="s2">&quot;#{ivar}++&quot;</span>
699
- <span class="k">else</span>
700
- <span class="k">if</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">Splat</span>
701
- <span class="nv">obj = </span><span class="nx">obj</span><span class="p">.</span><span class="nx">name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
702
- <span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="o">\</span>
703
- <span class="s2">&quot;multiple splats are disallowed in an assignment: #{obj} ...&quot;</span>
704
- <span class="k">if</span> <span class="k">typeof</span> <span class="nx">idx</span> <span class="o">is</span> <span class="s1">&#39;number&#39;</span>
705
- <span class="nv">idx = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">splat</span> <span class="o">or</span> <span class="nx">idx</span>
706
- <span class="nv">acc = </span><span class="kc">no</span>
707
- <span class="k">else</span>
708
- <span class="nv">acc = </span><span class="nx">isObject</span> <span class="o">and</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span> <span class="nx">idx</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">().</span><span class="nx">value</span> <span class="o">or</span> <span class="mi">0</span>
709
- <span class="nv">val = </span><span class="k">new</span> <span class="nx">Value</span> <span class="k">new</span> <span class="nx">Literal</span><span class="p">(</span><span class="nx">vvar</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="p">(</span><span class="k">if</span> <span class="nx">acc</span> <span class="k">then</span> <span class="nx">Access</span> <span class="k">else</span> <span class="nx">Index</span><span class="p">)</span> <span class="nx">idx</span><span class="p">]</span>
710
- <span class="nx">assigns</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">Assign</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">val</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">param</span><span class="o">:</span> <span class="nx">@param</span><span class="p">).</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_TOP</span>
711
- <span class="nx">assigns</span><span class="p">.</span><span class="nx">push</span> <span class="nx">vvar</span> <span class="nx">unless</span> <span class="nx">top</span>
712
- <span class="nv">code = </span><span class="nx">assigns</span><span class="p">.</span><span class="nx">join</span> <span class="s1">&#39;, &#39;</span>
713
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&lt;</span> <span class="nx">LEVEL_LIST</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">&quot;(#{code})&quot;</span></pre></div> </td> </tr> <tr id="section-91"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-91">&#182;</a> </div> <p>When compiling a conditional assignment, take care to ensure that the
714
- operands are only evaluated once, even though we have to reference them
715
- more than once.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileConditional</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
716
- <span class="p">[</span><span class="nx">left</span><span class="p">,</span> <span class="nx">rite</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">cacheReference</span> <span class="nx">o</span>
717
- <span class="k">new</span> <span class="nx">Op</span><span class="p">(</span><span class="nx">@context</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="nx">left</span><span class="p">,</span> <span class="k">new</span> <span class="nx">Assign</span><span class="p">(</span><span class="nx">rite</span><span class="p">,</span> <span class="nx">@value</span><span class="p">,</span> <span class="s1">&#39;=&#39;</span><span class="p">)).</span><span class="nx">compile</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-92"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-92">&#182;</a> </div> <p>Compile the assignment from an array splice literal, using JavaScript's
718
- <code>Array#splice</code> method.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileSplice</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
719
- <span class="p">{</span><span class="nx">range</span><span class="o">:</span> <span class="p">{</span><span class="nx">from</span><span class="p">,</span> <span class="nx">to</span><span class="p">,</span> <span class="nx">exclusive</span><span class="p">}}</span> <span class="o">=</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span>
720
- <span class="nv">name = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
721
- <span class="p">[</span><span class="nx">fromDecl</span><span class="p">,</span> <span class="nx">fromRef</span><span class="p">]</span> <span class="o">=</span> <span class="nx">from</span><span class="o">?</span><span class="p">.</span><span class="nx">cache</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span><span class="p">)</span> <span class="o">or</span> <span class="p">[</span><span class="s1">&#39;0&#39;</span><span class="p">,</span> <span class="s1">&#39;0&#39;</span><span class="p">]</span>
722
- <span class="k">if</span> <span class="nx">to</span>
723
- <span class="k">if</span> <span class="nx">from</span><span class="o">?</span><span class="p">.</span><span class="nx">isSimpleNumber</span><span class="p">()</span> <span class="o">and</span> <span class="nx">to</span><span class="p">.</span><span class="nx">isSimpleNumber</span><span class="p">()</span>
724
- <span class="nv">to = </span><span class="o">+</span><span class="nx">to</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-</span> <span class="o">+</span><span class="nx">fromRef</span>
725
- <span class="nx">to</span> <span class="o">+=</span> <span class="mi">1</span> <span class="nx">unless</span> <span class="nx">exclusive</span>
726
- <span class="k">else</span>
727
- <span class="nv">to = </span><span class="nx">to</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; - &#39;</span> <span class="o">+</span> <span class="nx">fromRef</span>
728
- <span class="nx">to</span> <span class="o">+=</span> <span class="s1">&#39; + 1&#39;</span> <span class="nx">unless</span> <span class="nx">exclusive</span>
729
- <span class="k">else</span>
730
- <span class="nv">to = </span><span class="s2">&quot;9e9&quot;</span>
731
- <span class="p">[</span><span class="nx">valDef</span><span class="p">,</span> <span class="nx">valRef</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@value</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
732
- <span class="nv">code = </span><span class="s2">&quot;[].splice.apply(#{name}, [#{fromDecl}, #{to}].concat(#{valDef})), #{valRef}&quot;</span>
733
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&gt;</span> <span class="nx">LEVEL_TOP</span> <span class="k">then</span> <span class="s2">&quot;(#{code})&quot;</span> <span class="k">else</span> <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-93"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-93">&#182;</a> </div> <h3>Code</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-94"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-94">&#182;</a> </div> <p>A function definition. This is the only node that creates a new Scope.
734
- When for the purposes of walking the contents of a function body, the Code
735
- has no <em>children</em> -- they're within the inner scope.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Code = </span><span class="nx">class</span> <span class="nx">Code</span> <span class="k">extends</span> <span class="nx">Base</span>
736
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">params</span><span class="p">,</span> <span class="nx">body</span><span class="p">,</span> <span class="nx">tag</span><span class="p">)</span> <span class="o">-&gt;</span>
737
- <span class="vi">@params = </span><span class="nx">params</span> <span class="o">or</span> <span class="p">[]</span>
738
- <span class="vi">@body = </span><span class="nx">body</span> <span class="o">or</span> <span class="k">new</span> <span class="nx">Expressions</span>
739
- <span class="vi">@bound = </span><span class="nx">tag</span> <span class="o">is</span> <span class="s1">&#39;boundfunc&#39;</span>
740
- <span class="vi">@context = </span><span class="s1">&#39;this&#39;</span> <span class="k">if</span> <span class="nx">@bound</span>
741
-
742
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;params&#39;</span><span class="p">,</span> <span class="s1">&#39;body&#39;</span><span class="p">]</span>
743
-
744
- <span class="nx">isStatement</span><span class="o">:</span> <span class="o">-&gt;</span> <span class="o">!!</span><span class="nx">@ctor</span>
745
-
746
- <span class="nx">jumps</span><span class="o">:</span> <span class="nx">NO</span></pre></div> </td> </tr> <tr id="section-95"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-95">&#182;</a> </div> <p>Compilation creates a new scope unless explicitly asked to share with the
747
- outer scope. Handles splat parameters in the parameter list by peeking at
748
- the JavaScript <code>arguments</code> objects. If the function is bound with the <code>=&gt;</code>
749
- arrow, generates a wrapper that saves the current value of <code>this</code> through
750
- a closure.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
751
- <span class="nv">o.scope = </span><span class="k">new</span> <span class="nx">Scope</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">,</span> <span class="nx">@body</span><span class="p">,</span> <span class="k">this</span>
752
- <span class="nv">o.scope.shared = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">&#39;sharedScope&#39;</span>
753
- <span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
754
- <span class="k">delete</span> <span class="nx">o</span><span class="p">.</span><span class="nx">bare</span>
755
- <span class="k">delete</span> <span class="nx">o</span><span class="p">.</span><span class="nx">globals</span>
756
- <span class="nv">vars = </span><span class="p">[]</span>
757
- <span class="nv">exprs = </span><span class="p">[]</span>
758
- <span class="k">for</span> <span class="nx">param</span> <span class="k">in</span> <span class="nx">@params</span> <span class="k">when</span> <span class="nx">param</span><span class="p">.</span><span class="nx">splat</span>
759
- <span class="nv">splats = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="k">new</span> <span class="nx">Arr</span><span class="p">(</span><span class="nx">p</span><span class="p">.</span><span class="nx">asReference</span> <span class="nx">o</span> <span class="k">for</span> <span class="nx">p</span> <span class="k">in</span> <span class="nx">@params</span><span class="p">)),</span>
760
- <span class="k">new</span> <span class="nx">Value</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s1">&#39;arguments&#39;</span>
761
- <span class="k">break</span>
762
- <span class="k">for</span> <span class="nx">param</span> <span class="k">in</span> <span class="nx">@params</span>
763
- <span class="k">if</span> <span class="nx">param</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
764
- <span class="nv">val = ref = </span><span class="nx">param</span><span class="p">.</span><span class="nx">asReference</span> <span class="nx">o</span>
765
- <span class="nv">val = </span><span class="k">new</span> <span class="nx">Op</span> <span class="s1">&#39;?&#39;</span><span class="p">,</span> <span class="nx">ref</span><span class="p">,</span> <span class="nx">param</span><span class="p">.</span><span class="nx">value</span> <span class="k">if</span> <span class="nx">param</span><span class="p">.</span><span class="nx">value</span>
766
- <span class="nx">exprs</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">param</span><span class="p">.</span><span class="nx">name</span><span class="p">),</span> <span class="nx">val</span><span class="p">,</span> <span class="s1">&#39;=&#39;</span><span class="p">,</span> <span class="nx">param</span><span class="o">:</span> <span class="kc">yes</span>
767
- <span class="k">else</span>
768
- <span class="nv">ref = </span><span class="nx">param</span>
769
- <span class="k">if</span> <span class="nx">param</span><span class="p">.</span><span class="nx">value</span>
770
- <span class="nv">lit = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">ref</span><span class="p">.</span><span class="nx">name</span><span class="p">.</span><span class="nx">value</span> <span class="o">+</span> <span class="s1">&#39; == null&#39;</span>
771
- <span class="nv">val = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">param</span><span class="p">.</span><span class="nx">name</span><span class="p">),</span> <span class="nx">param</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span> <span class="s1">&#39;=&#39;</span>
772
- <span class="nx">exprs</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">If</span> <span class="nx">lit</span><span class="p">,</span> <span class="nx">val</span>
773
- <span class="nx">vars</span><span class="p">.</span><span class="nx">push</span> <span class="nx">ref</span> <span class="nx">unless</span> <span class="nx">splats</span>
774
- <span class="nv">wasEmpty = </span><span class="nx">@body</span><span class="p">.</span><span class="nx">isEmpty</span><span class="p">()</span>
775
- <span class="nx">exprs</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">splats</span> <span class="k">if</span> <span class="nx">splats</span>
776
- <span class="nx">@body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">exprs</span><span class="p">...</span> <span class="k">if</span> <span class="nx">exprs</span><span class="p">.</span><span class="nx">length</span>
777
- <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">parameter</span> <span class="nx">vars</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">v</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span> <span class="k">for</span> <span class="nx">v</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">vars</span> <span class="nx">unless</span> <span class="nx">splats</span>
778
- <span class="nx">@body</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span> <span class="nx">unless</span> <span class="nx">wasEmpty</span> <span class="o">or</span> <span class="nx">@noReturn</span>
779
- <span class="nv">idt = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span>
780
- <span class="nv">code = </span><span class="s1">&#39;function&#39;</span>
781
- <span class="nx">code</span> <span class="o">+=</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="nx">@name</span> <span class="k">if</span> <span class="nx">@ctor</span>
782
- <span class="nx">code</span> <span class="o">+=</span> <span class="s1">&#39;(&#39;</span> <span class="o">+</span> <span class="nx">vars</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;, &#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;) {&#39;</span>
783
- <span class="nx">code</span> <span class="o">+=</span> <span class="s2">&quot;\n#{ @body.compileWithDeclarations o }\n#{@tab}&quot;</span> <span class="nx">unless</span> <span class="nx">@body</span><span class="p">.</span><span class="nx">isEmpty</span><span class="p">()</span>
784
- <span class="nx">code</span> <span class="o">+=</span> <span class="s1">&#39;}&#39;</span>
785
- <span class="k">return</span> <span class="nx">@tab</span> <span class="o">+</span> <span class="nx">code</span> <span class="k">if</span> <span class="nx">@ctor</span>
786
- <span class="k">return</span> <span class="nx">utility</span><span class="p">(</span><span class="s1">&#39;bind&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;(#{code}, #{@context})&quot;</span> <span class="k">if</span> <span class="nx">@bound</span>
787
- <span class="k">if</span> <span class="nx">@front</span> <span class="o">or</span> <span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&gt;=</span> <span class="nx">LEVEL_ACCESS</span><span class="p">)</span> <span class="k">then</span> <span class="s2">&quot;(#{code})&quot;</span> <span class="k">else</span> <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-96"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-96">&#182;</a> </div> <p>Short-circuit <code>traverseChildren</code> method to prevent it from crossing scope boundaries
788
- unless <code>crossScope</code> is <code>true</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">traverseChildren</span><span class="o">:</span> <span class="p">(</span><span class="nx">crossScope</span><span class="p">,</span> <span class="nx">func</span><span class="p">)</span> <span class="o">-&gt;</span>
789
- <span class="k">super</span><span class="p">(</span><span class="nx">crossScope</span><span class="p">,</span> <span class="nx">func</span><span class="p">)</span> <span class="k">if</span> <span class="nx">crossScope</span></pre></div> </td> </tr> <tr id="section-97"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-97">&#182;</a> </div> <h3>Param</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-98"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-98">&#182;</a> </div> <p>A parameter in a function definition. Beyond a typical Javascript parameter,
790
- these parameters can also attach themselves to the context of the function,
791
- as well as be a splat, gathering up a group of parameters into an array.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Param = </span><span class="nx">class</span> <span class="nx">Param</span> <span class="k">extends</span> <span class="nx">Base</span>
792
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@name</span><span class="p">,</span> <span class="nx">@value</span><span class="p">,</span> <span class="nx">@splat</span><span class="p">)</span> <span class="o">-&gt;</span>
793
-
794
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;value&#39;</span><span class="p">]</span>
795
-
796
- <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
797
- <span class="nx">@name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
798
-
799
- <span class="nx">asReference</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
800
- <span class="k">return</span> <span class="nx">@reference</span> <span class="k">if</span> <span class="nx">@reference</span>
801
- <span class="nv">node = </span><span class="nx">@name</span>
802
- <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="k">this</span>
803
- <span class="nv">node = </span><span class="nx">node</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span>
804
- <span class="nv">node = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="nx">node</span><span class="p">.</span><span class="nx">value</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">value</span><span class="p">.</span><span class="nx">reserved</span>
805
- <span class="k">else</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
806
- <span class="nv">node = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;arg&#39;</span>
807
- <span class="nv">node = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">node</span>
808
- <span class="nv">node = </span><span class="k">new</span> <span class="nx">Splat</span> <span class="nx">node</span> <span class="k">if</span> <span class="nx">@splat</span>
809
- <span class="vi">@reference = </span><span class="nx">node</span>
810
-
811
- <span class="nx">isComplex</span><span class="o">:</span> <span class="o">-&gt;</span>
812
- <span class="nx">@name</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-99"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-99">&#182;</a> </div> <h3>Splat</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-100"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-100">&#182;</a> </div> <p>A splat, either as a parameter to a function, an argument to a call,
813
- or as part of a destructuring assignment.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Splat = </span><span class="nx">class</span> <span class="nx">Splat</span> <span class="k">extends</span> <span class="nx">Base</span>
814
-
815
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span>
816
-
817
- <span class="nx">isAssignable</span><span class="o">:</span> <span class="nx">YES</span>
818
-
819
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
820
- <span class="vi">@name = </span><span class="k">if</span> <span class="nx">name</span><span class="p">.</span><span class="nx">compile</span> <span class="k">then</span> <span class="nx">name</span> <span class="k">else</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="nx">name</span>
821
-
822
- <span class="nx">assigns</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
823
- <span class="nx">@name</span><span class="p">.</span><span class="nx">assigns</span> <span class="nx">name</span>
824
-
825
- <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
826
- <span class="k">if</span> <span class="nx">@index</span><span class="o">?</span> <span class="k">then</span> <span class="nx">@compileParam</span> <span class="nx">o</span> <span class="k">else</span> <span class="nx">@name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-101"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-101">&#182;</a> </div> <p>Utility function that converts arbitrary number of elements, mixed with
827
- splats, to a proper array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@compileSplattedArray</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">list</span><span class="p">,</span> <span class="nx">apply</span><span class="p">)</span> <span class="o">-&gt;</span>
828
- <span class="nv">index = </span><span class="o">-</span><span class="mi">1</span>
829
- <span class="k">continue</span> <span class="k">while</span> <span class="p">(</span><span class="nv">node = </span><span class="nx">list</span><span class="p">[</span><span class="o">++</span><span class="nx">index</span><span class="p">])</span> <span class="o">and</span> <span class="nx">node</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Splat</span>
830
- <span class="k">return</span> <span class="s1">&#39;&#39;</span> <span class="k">if</span> <span class="nx">index</span> <span class="o">&gt;=</span> <span class="nx">list</span><span class="p">.</span><span class="nx">length</span>
831
- <span class="k">if</span> <span class="nx">list</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">1</span>
832
- <span class="nv">code = </span><span class="nx">list</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
833
- <span class="k">return</span> <span class="nx">code</span> <span class="k">if</span> <span class="nx">apply</span>
834
- <span class="k">return</span> <span class="s2">&quot;#{ utility &#39;slice&#39; }.call(#{code})&quot;</span>
835
- <span class="nv">args = </span><span class="nx">list</span><span class="p">.</span><span class="nx">slice</span> <span class="nx">index</span>
836
- <span class="k">for</span> <span class="nx">node</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">args</span>
837
- <span class="nv">code = </span><span class="nx">node</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
838
- <span class="nx">args</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Splat</span>
839
- <span class="k">then</span> <span class="s2">&quot;#{ utility &#39;slice&#39; }.call(#{code})&quot;</span>
840
- <span class="k">else</span> <span class="s2">&quot;[#{code}]&quot;</span>
841
- <span class="k">return</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s2">&quot;.concat(#{ args.slice(1).join &#39;, &#39; })&quot;</span> <span class="k">if</span> <span class="nx">index</span> <span class="o">is</span> <span class="mi">0</span>
842
- <span class="nv">base = </span><span class="p">(</span><span class="nx">node</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span> <span class="k">for</span> <span class="nx">node</span> <span class="k">in</span> <span class="nx">list</span><span class="p">.</span><span class="nx">slice</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">index</span><span class="p">)</span>
843
- <span class="s2">&quot;[#{ base.join &#39;, &#39; }].concat(#{ args.join &#39;, &#39; })&quot;</span></pre></div> </td> </tr> <tr id="section-102"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-102">&#182;</a> </div> <h3>While</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-103"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-103">&#182;</a> </div> <p>A while loop, the only sort of low-level loop exposed by CoffeeScript. From
844
- it, all other loops can be manufactured. Useful in cases where you need more
845
- flexibility or more speed than a comprehension can provide.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.While = </span><span class="nx">class</span> <span class="nx">While</span> <span class="k">extends</span> <span class="nx">Base</span>
846
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">condition</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">-&gt;</span>
847
- <span class="vi">@condition = </span><span class="k">if</span> <span class="nx">options</span><span class="o">?</span><span class="p">.</span><span class="nx">invert</span> <span class="k">then</span> <span class="nx">condition</span><span class="p">.</span><span class="nx">invert</span><span class="p">()</span> <span class="k">else</span> <span class="nx">condition</span>
848
- <span class="vi">@guard = </span><span class="nx">options</span><span class="o">?</span><span class="p">.</span><span class="nx">guard</span>
849
-
850
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;condition&#39;</span><span class="p">,</span> <span class="s1">&#39;guard&#39;</span><span class="p">,</span> <span class="s1">&#39;body&#39;</span><span class="p">]</span>
851
-
852
- <span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
853
-
854
- <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-&gt;</span>
855
- <span class="vi">@returns = </span><span class="kc">yes</span>
856
- <span class="k">this</span>
857
-
858
- <span class="nx">addBody</span><span class="o">:</span> <span class="p">(</span><span class="nx">@body</span><span class="p">)</span> <span class="o">-&gt;</span>
859
- <span class="k">this</span>
860
-
861
- <span class="nx">jumps</span><span class="o">:</span> <span class="o">-&gt;</span>
862
- <span class="p">{</span><span class="nx">expressions</span><span class="p">}</span> <span class="o">=</span> <span class="nx">@body</span>
863
- <span class="k">return</span> <span class="kc">no</span> <span class="nx">unless</span> <span class="nx">expressions</span><span class="p">.</span><span class="nx">length</span>
864
- <span class="k">for</span> <span class="nx">node</span> <span class="k">in</span> <span class="nx">expressions</span>
865
- <span class="k">return</span> <span class="nx">node</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">jumps</span> <span class="nx">loop</span><span class="o">:</span> <span class="kc">yes</span>
866
- <span class="kc">no</span></pre></div> </td> </tr> <tr id="section-104"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-104">&#182;</a> </div> <p>The main difference from a JavaScript <em>while</em> is that the CoffeeScript
867
- <em>while</em> can be used as a part of a larger expression -- while loops may
868
- return an array containing the computed result of each iteration.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
869
- <span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
870
- <span class="nv">set = </span><span class="s1">&#39;&#39;</span>
871
- <span class="p">{</span><span class="nx">body</span><span class="p">}</span> <span class="o">=</span> <span class="k">this</span>
872
- <span class="k">if</span> <span class="nx">body</span><span class="p">.</span><span class="nx">isEmpty</span><span class="p">()</span>
873
- <span class="nv">body = </span><span class="s1">&#39;&#39;</span>
874
- <span class="k">else</span>
875
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&gt;</span> <span class="nx">LEVEL_TOP</span> <span class="o">or</span> <span class="nx">@returns</span>
876
- <span class="nv">rvar = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;results&#39;</span>
877
- <span class="nv">set = </span><span class="s2">&quot;#{@tab}#{rvar} = [];\n&quot;</span>
878
- <span class="nv">body = </span><span class="nx">Push</span><span class="p">.</span><span class="nx">wrap</span> <span class="nx">rvar</span><span class="p">,</span> <span class="nx">body</span> <span class="k">if</span> <span class="nx">body</span>
879
- <span class="nv">body = </span><span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="k">new</span> <span class="nx">If</span> <span class="nx">@guard</span><span class="p">,</span> <span class="nx">body</span><span class="p">]</span> <span class="k">if</span> <span class="nx">@guard</span>
880
- <span class="nv">body = </span><span class="s2">&quot;\n#{ body.compile o, LEVEL_TOP }\n#{@tab}&quot;</span>
881
- <span class="nv">code = </span><span class="nx">set</span> <span class="o">+</span> <span class="nx">@tab</span> <span class="o">+</span> <span class="s2">&quot;while (#{ @condition.compile o, LEVEL_PAREN }) {#{body}}&quot;</span>
882
- <span class="k">if</span> <span class="nx">@returns</span>
883
- <span class="nx">code</span> <span class="o">+=</span> <span class="s2">&quot;\n#{@tab}return #{rvar};&quot;</span>
884
- <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-105"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-105">&#182;</a> </div> <h3>Op</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-106"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-106">&#182;</a> </div> <p>Simple Arithmetic and logical operations. Performs some conversion from
885
- CoffeeScript operations into their JavaScript equivalents.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Op = </span><span class="nx">class</span> <span class="nx">Op</span> <span class="k">extends</span> <span class="nx">Base</span>
886
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">op</span><span class="p">,</span> <span class="nx">first</span><span class="p">,</span> <span class="nx">second</span><span class="p">,</span> <span class="nx">flip</span><span class="p">)</span> <span class="o">-&gt;</span>
887
- <span class="k">return</span> <span class="k">new</span> <span class="nx">In</span> <span class="nx">first</span><span class="p">,</span> <span class="nx">second</span> <span class="k">if</span> <span class="nx">op</span> <span class="o">is</span> <span class="s1">&#39;in&#39;</span>
888
- <span class="k">return</span> <span class="k">new</span> <span class="nx">Call</span> <span class="nx">first</span><span class="p">,</span> <span class="nx">first</span><span class="p">.</span><span class="nx">params</span> <span class="o">or</span> <span class="p">[]</span> <span class="k">if</span> <span class="nx">op</span> <span class="o">is</span> <span class="s1">&#39;do&#39;</span>
889
- <span class="k">if</span> <span class="nx">op</span> <span class="o">is</span> <span class="s1">&#39;new&#39;</span>
890
- <span class="k">return</span> <span class="nx">first</span><span class="p">.</span><span class="nx">newInstance</span><span class="p">()</span> <span class="k">if</span> <span class="nx">first</span> <span class="k">instanceof</span> <span class="nx">Call</span>
891
- <span class="nv">first = </span><span class="k">new</span> <span class="nx">Parens</span> <span class="nx">first</span> <span class="k">if</span> <span class="nx">first</span> <span class="k">instanceof</span> <span class="nx">Code</span> <span class="o">and</span> <span class="nx">first</span><span class="p">.</span><span class="nx">bound</span>
892
- <span class="vi">@operator = </span><span class="nx">CONVERSIONS</span><span class="p">[</span><span class="nx">op</span><span class="p">]</span> <span class="o">or</span> <span class="nx">op</span>
893
- <span class="vi">@first = </span><span class="nx">first</span>
894
- <span class="vi">@second = </span><span class="nx">second</span>
895
- <span class="vi">@flip = </span><span class="o">!!</span><span class="nx">flip</span>
896
- <span class="k">return</span> <span class="k">this</span></pre></div> </td> </tr> <tr id="section-107"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-107">&#182;</a> </div> <p>The map of conversions from CoffeeScript to JavaScript symbols.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">CONVERSIONS =</span>
897
- <span class="s1">&#39;==&#39;</span><span class="o">:</span> <span class="s1">&#39;===&#39;</span>
898
- <span class="s1">&#39;!=&#39;</span><span class="o">:</span> <span class="s1">&#39;!==&#39;</span>
899
- <span class="s1">&#39;of&#39;</span><span class="o">:</span> <span class="s1">&#39;in&#39;</span></pre></div> </td> </tr> <tr id="section-108"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-108">&#182;</a> </div> <p>The map of invertible operators.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">INVERSIONS =</span>
900
- <span class="s1">&#39;!==&#39;</span><span class="o">:</span> <span class="s1">&#39;===&#39;</span>
901
- <span class="s1">&#39;===&#39;</span><span class="o">:</span> <span class="s1">&#39;!==&#39;</span>
902
-
903
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;first&#39;</span><span class="p">,</span> <span class="s1">&#39;second&#39;</span><span class="p">]</span>
904
-
905
- <span class="nx">isSimpleNumber</span><span class="o">:</span> <span class="nx">NO</span>
906
-
907
- <span class="nx">isUnary</span><span class="o">:</span> <span class="o">-&gt;</span>
908
- <span class="o">not</span> <span class="nx">@second</span></pre></div> </td> </tr> <tr id="section-109"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-109">&#182;</a> </div> <p>Am I capable of
909
- <a href="http://docs.python.org/reference/expressions.html#notin">Python-style comparison chaining</a>?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">isChainable</span><span class="o">:</span> <span class="o">-&gt;</span>
910
- <span class="nx">@operator</span> <span class="k">in</span> <span class="p">[</span><span class="s1">&#39;&lt;&#39;</span><span class="p">,</span> <span class="s1">&#39;&gt;&#39;</span><span class="p">,</span> <span class="s1">&#39;&gt;=&#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;=&#39;</span><span class="p">,</span> <span class="s1">&#39;===&#39;</span><span class="p">,</span> <span class="s1">&#39;!==&#39;</span><span class="p">]</span>
911
-
912
- <span class="nx">invert</span><span class="o">:</span> <span class="o">-&gt;</span>
913
- <span class="k">if</span> <span class="nx">@isChainable</span><span class="p">()</span> <span class="o">and</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">isChainable</span><span class="p">()</span>
914
- <span class="nv">allInvertable = </span><span class="kc">yes</span>
915
- <span class="nv">curr = </span><span class="k">this</span>
916
- <span class="k">while</span> <span class="nx">curr</span> <span class="o">and</span> <span class="nx">curr</span><span class="p">.</span><span class="nx">operator</span>
917
- <span class="nx">allInvertable</span> <span class="o">and=</span> <span class="p">(</span><span class="nx">curr</span><span class="p">.</span><span class="nx">operator</span> <span class="k">of</span> <span class="nx">INVERSIONS</span><span class="p">)</span>
918
- <span class="nv">curr = </span><span class="nx">curr</span><span class="p">.</span><span class="nx">first</span>
919
- <span class="k">return</span> <span class="k">new</span> <span class="nx">Parens</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">invert</span><span class="p">()</span> <span class="nx">unless</span> <span class="nx">allInvertable</span>
920
- <span class="nv">curr = </span><span class="k">this</span>
921
- <span class="k">while</span> <span class="nx">curr</span> <span class="o">and</span> <span class="nx">curr</span><span class="p">.</span><span class="nx">operator</span>
922
- <span class="nv">curr.invert = </span><span class="o">!</span><span class="nx">curr</span><span class="p">.</span><span class="nx">invert</span>
923
- <span class="nv">curr.operator = </span><span class="nx">INVERSIONS</span><span class="p">[</span><span class="nx">curr</span><span class="p">.</span><span class="nx">operator</span><span class="p">]</span>
924
- <span class="nv">curr = </span><span class="nx">curr</span><span class="p">.</span><span class="nx">first</span>
925
- <span class="k">this</span>
926
- <span class="k">else</span> <span class="k">if</span> <span class="nv">op = </span><span class="nx">INVERSIONS</span><span class="p">[</span><span class="nx">@operator</span><span class="p">]</span>
927
- <span class="vi">@operator = </span><span class="nx">op</span>
928
- <span class="k">if</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span> <span class="k">instanceof</span> <span class="nx">Op</span>
929
- <span class="nx">@first</span><span class="p">.</span><span class="nx">invert</span><span class="p">()</span>
930
- <span class="k">this</span>
931
- <span class="k">else</span> <span class="k">if</span> <span class="nx">@second</span>
932
- <span class="k">new</span> <span class="nx">Parens</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">invert</span><span class="p">()</span>
933
- <span class="k">else</span> <span class="k">if</span> <span class="nx">@operator</span> <span class="o">is</span> <span class="s1">&#39;!&#39;</span> <span class="o">and</span> <span class="p">(</span><span class="nv">fst = </span><span class="nx">@first</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">())</span> <span class="k">instanceof</span> <span class="nx">Op</span> <span class="o">and</span>
934
- <span class="nx">fst</span><span class="p">.</span><span class="nx">operator</span> <span class="k">in</span> <span class="p">[</span><span class="s1">&#39;!&#39;</span><span class="p">,</span> <span class="s1">&#39;in&#39;</span><span class="p">,</span> <span class="s1">&#39;instanceof&#39;</span><span class="p">]</span>
935
- <span class="nx">fst</span>
936
- <span class="k">else</span>
937
- <span class="k">new</span> <span class="nx">Op</span> <span class="s1">&#39;!&#39;</span><span class="p">,</span> <span class="k">this</span>
938
-
939
- <span class="nx">unfoldSoak</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
940
- <span class="nx">@operator</span> <span class="k">in</span> <span class="p">[</span><span class="s1">&#39;++&#39;</span><span class="p">,</span> <span class="s1">&#39;--&#39;</span><span class="p">,</span> <span class="s1">&#39;delete&#39;</span><span class="p">]</span> <span class="o">and</span> <span class="nx">unfoldSoak</span> <span class="nx">o</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="s1">&#39;first&#39;</span>
941
-
942
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
943
- <span class="k">return</span> <span class="nx">@compileUnary</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@isUnary</span><span class="p">()</span>
944
- <span class="k">return</span> <span class="nx">@compileChain</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@isChainable</span><span class="p">()</span> <span class="o">and</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">isChainable</span><span class="p">()</span>
945
- <span class="k">return</span> <span class="nx">@compileExistence</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@operator</span> <span class="o">is</span> <span class="s1">&#39;?&#39;</span>
946
- <span class="vi">@first.front = </span><span class="nx">@front</span>
947
- <span class="nv">code = </span><span class="nx">@first</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="nx">@operator</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span>
948
- <span class="nx">@second</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span><span class="p">)</span>
949
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&lt;=</span> <span class="nx">LEVEL_OP</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">&quot;(#{code})&quot;</span></pre></div> </td> </tr> <tr id="section-110"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-110">&#182;</a> </div> <p>Mimic Python's chained comparisons when multiple comparison operators are
950
- used sequentially. For example:</p>
951
-
952
- <pre><code>bin/coffee -e 'console.log 50 &lt; 65 &gt; 10'
953
- true
954
- </code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileChain</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
955
- <span class="p">[</span><span class="nx">@first</span><span class="p">.</span><span class="nx">second</span><span class="p">,</span> <span class="nx">shared</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">second</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span>
956
- <span class="nv">fst = </span><span class="nx">@first</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span>
957
- <span class="nv">code = </span><span class="s2">&quot;#{fst} #{if @invert then &#39;&amp;&amp;&#39; else &#39;||&#39;} #{ shared.compile o } #{@operator} #{ @second.compile o, LEVEL_OP }&quot;</span>
958
- <span class="s2">&quot;(#{code})&quot;</span>
959
-
960
- <span class="nx">compileExistence</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
961
- <span class="k">if</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
962
- <span class="nv">ref = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;ref&#39;</span>
963
- <span class="nv">fst = </span><span class="k">new</span> <span class="nx">Parens</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="k">new</span> <span class="nx">Literal</span><span class="p">(</span><span class="nx">ref</span><span class="p">),</span> <span class="nx">@first</span>
964
- <span class="k">else</span>
965
- <span class="nv">fst = </span><span class="nx">@first</span>
966
- <span class="nv">ref = </span><span class="nx">fst</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
967
- <span class="k">new</span> <span class="nx">Existence</span><span class="p">(</span><span class="nx">fst</span><span class="p">).</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot; ? #{ref} : #{ @second.compile o, LEVEL_LIST }&quot;</span></pre></div> </td> </tr> <tr id="section-111"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-111">&#182;</a> </div> <p>Compile a unary <strong>Op</strong>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileUnary</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
968
- <span class="nv">parts = </span><span class="p">[</span><span class="nv">op = </span><span class="nx">@operator</span><span class="p">]</span>
969
- <span class="nx">parts</span><span class="p">.</span><span class="nx">push</span> <span class="s1">&#39; &#39;</span> <span class="k">if</span> <span class="nx">op</span> <span class="k">in</span> <span class="p">[</span><span class="s1">&#39;new&#39;</span><span class="p">,</span> <span class="s1">&#39;typeof&#39;</span><span class="p">,</span> <span class="s1">&#39;delete&#39;</span><span class="p">]</span> <span class="o">or</span>
970
- <span class="nx">op</span> <span class="k">in</span> <span class="p">[</span><span class="s1">&#39;+&#39;</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">]</span> <span class="o">and</span> <span class="nx">@first</span> <span class="k">instanceof</span> <span class="nx">Op</span> <span class="o">and</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">operator</span> <span class="o">is</span> <span class="nx">op</span>
971
- <span class="nx">parts</span><span class="p">.</span><span class="nx">push</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span>
972
- <span class="nx">parts</span><span class="p">.</span><span class="nx">reverse</span><span class="p">()</span> <span class="k">if</span> <span class="nx">@flip</span>
973
- <span class="nx">parts</span><span class="p">.</span><span class="nx">join</span> <span class="s1">&#39;&#39;</span>
974
-
975
- <span class="nx">toString</span><span class="o">:</span> <span class="p">(</span><span class="nx">idt</span><span class="p">)</span> <span class="o">-&gt;</span>
976
- <span class="k">super</span> <span class="nx">idt</span><span class="p">,</span> <span class="nx">@constructor</span><span class="p">.</span><span class="nx">name</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="nx">@operator</span></pre></div> </td> </tr> <tr id="section-112"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-112">&#182;</a> </div> <h3>In</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.In = </span><span class="nx">class</span> <span class="nx">In</span> <span class="k">extends</span> <span class="nx">Base</span>
977
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@object</span><span class="p">,</span> <span class="nx">@array</span><span class="p">)</span> <span class="o">-&gt;</span>
978
-
979
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;object&#39;</span><span class="p">,</span> <span class="s1">&#39;array&#39;</span><span class="p">]</span>
980
-
981
- <span class="nx">invert</span><span class="o">:</span> <span class="nx">NEGATE</span>
982
-
983
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
984
- <span class="k">if</span> <span class="nx">@array</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span> <span class="nx">@array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">()</span>
985
- <span class="nx">@compileOrTest</span> <span class="nx">o</span>
986
- <span class="k">else</span>
987
- <span class="nx">@compileLoopTest</span> <span class="nx">o</span>
988
-
989
- <span class="nx">compileOrTest</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
990
- <span class="p">[</span><span class="nx">sub</span><span class="p">,</span> <span class="nx">ref</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@object</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span>
991
- <span class="p">[</span><span class="nx">cmp</span><span class="p">,</span> <span class="nx">cnj</span><span class="p">]</span> <span class="o">=</span> <span class="k">if</span> <span class="nx">@negated</span> <span class="k">then</span> <span class="p">[</span><span class="s1">&#39; !== &#39;</span><span class="p">,</span> <span class="s1">&#39; &amp;&amp; &#39;</span><span class="p">]</span> <span class="k">else</span> <span class="p">[</span><span class="s1">&#39; === &#39;</span><span class="p">,</span> <span class="s1">&#39; || &#39;</span><span class="p">]</span>
992
- <span class="nv">tests = </span><span class="k">for</span> <span class="nx">item</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">@array</span><span class="p">.</span><span class="nx">base</span><span class="p">.</span><span class="nx">objects</span>
993
- <span class="p">(</span><span class="k">if</span> <span class="nx">i</span> <span class="k">then</span> <span class="nx">ref</span> <span class="k">else</span> <span class="nx">sub</span><span class="p">)</span> <span class="o">+</span> <span class="nx">cmp</span> <span class="o">+</span> <span class="nx">item</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span>
994
- <span class="nv">tests = </span><span class="nx">tests</span><span class="p">.</span><span class="nx">join</span> <span class="nx">cnj</span>
995
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&lt;</span> <span class="nx">LEVEL_OP</span> <span class="k">then</span> <span class="nx">tests</span> <span class="k">else</span> <span class="s2">&quot;(#{tests})&quot;</span>
996
-
997
- <span class="nx">compileLoopTest</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
998
- <span class="p">[</span><span class="nx">sub</span><span class="p">,</span> <span class="nx">ref</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@object</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
999
- <span class="nv">code = </span><span class="nx">utility</span><span class="p">(</span><span class="s1">&#39;indexOf&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;.call(#{ @array.compile o, LEVEL_LIST }, #{ref}) &quot;</span> <span class="o">+</span>
1000
- <span class="k">if</span> <span class="nx">@negated</span> <span class="k">then</span> <span class="s1">&#39;&lt; 0&#39;</span> <span class="k">else</span> <span class="s1">&#39;&gt;= 0&#39;</span>
1001
- <span class="k">return</span> <span class="nx">code</span> <span class="k">if</span> <span class="nx">sub</span> <span class="o">is</span> <span class="nx">ref</span>
1002
- <span class="nv">code = </span><span class="nx">sub</span> <span class="o">+</span> <span class="s1">&#39;, &#39;</span> <span class="o">+</span> <span class="nx">code</span>
1003
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&lt;</span> <span class="nx">LEVEL_LIST</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">&quot;(#{code})&quot;</span>
1004
-
1005
- <span class="nx">toString</span><span class="o">:</span> <span class="p">(</span><span class="nx">idt</span><span class="p">)</span> <span class="o">-&gt;</span>
1006
- <span class="k">super</span> <span class="nx">idt</span><span class="p">,</span> <span class="nx">@constructor</span><span class="p">.</span><span class="nx">name</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@negated</span> <span class="k">then</span> <span class="s1">&#39;!&#39;</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span></pre></div> </td> </tr> <tr id="section-113"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-113">&#182;</a> </div> <h3>Try</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-114"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-114">&#182;</a> </div> <p>A classic <em>try/catch/finally</em> block.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Try = </span><span class="nx">class</span> <span class="nx">Try</span> <span class="k">extends</span> <span class="nx">Base</span>
1007
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@attempt</span><span class="p">,</span> <span class="nx">@error</span><span class="p">,</span> <span class="nx">@recovery</span><span class="p">,</span> <span class="nx">@ensure</span><span class="p">)</span> <span class="o">-&gt;</span>
1008
-
1009
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;attempt&#39;</span><span class="p">,</span> <span class="s1">&#39;recovery&#39;</span><span class="p">,</span> <span class="s1">&#39;ensure&#39;</span><span class="p">]</span>
1010
-
1011
- <span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
1012
-
1013
- <span class="nx">jumps</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nx">@attempt</span><span class="p">.</span><span class="nx">jumps</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">or</span> <span class="nx">@recovery</span><span class="o">?</span><span class="p">.</span><span class="nx">jumps</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
1014
-
1015
- <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-&gt;</span>
1016
- <span class="vi">@attempt = </span><span class="nx">@attempt</span> <span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span> <span class="k">if</span> <span class="nx">@attempt</span>
1017
- <span class="vi">@recovery = </span><span class="nx">@recovery</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span> <span class="k">if</span> <span class="nx">@recovery</span>
1018
- <span class="k">this</span></pre></div> </td> </tr> <tr id="section-115"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-115">&#182;</a> </div> <p>Compilation is more or less as you would expect -- the <em>finally</em> clause
1019
- is optional, the <em>catch</em> is not.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
1020
- <span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
1021
- <span class="nv">errorPart = </span><span class="k">if</span> <span class="nx">@error</span> <span class="k">then</span> <span class="s2">&quot; (#{ @error.compile o }) &quot;</span> <span class="k">else</span> <span class="s1">&#39; &#39;</span>
1022
- <span class="nv">catchPart = </span><span class="k">if</span> <span class="nx">@recovery</span>
1023
- <span class="s2">&quot; catch#{errorPart}{\n#{ @recovery.compile o, LEVEL_TOP }\n#{@tab}}&quot;</span>
1024
- <span class="k">else</span> <span class="nx">unless</span> <span class="nx">@ensure</span> <span class="o">or</span> <span class="nx">@recovery</span>
1025
- <span class="s1">&#39; catch (_e) {}&#39;</span>
1026
- <span class="s2">&quot;&quot;&quot;</span>
1027
- <span class="s2"> #{@tab}try {</span>
1028
- <span class="s2"> #{ @attempt.compile o, LEVEL_TOP }</span>
1029
- <span class="s2"> #{@tab}}#{ catchPart or &#39;&#39; }</span>
1030
- <span class="s2"> &quot;&quot;&quot;</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@ensure</span> <span class="k">then</span> <span class="s2">&quot; finally {\n#{ @ensure.compile o, LEVEL_TOP }\n#{@tab}}&quot;</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span></pre></div> </td> </tr> <tr id="section-116"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-116">&#182;</a> </div> <h3>Throw</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-117"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-117">&#182;</a> </div> <p>Simple node to throw an exception.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Throw = </span><span class="nx">class</span> <span class="nx">Throw</span> <span class="k">extends</span> <span class="nx">Base</span>
1031
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@expression</span><span class="p">)</span> <span class="o">-&gt;</span>
1032
-
1033
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;expression&#39;</span><span class="p">]</span>
1034
-
1035
- <span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
1036
- <span class="nx">jumps</span><span class="o">:</span> <span class="nx">NO</span></pre></div> </td> </tr> <tr id="section-118"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-118">&#182;</a> </div> <p>A <strong>Throw</strong> is already a return, of sorts...</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">makeReturn</span><span class="o">:</span> <span class="nx">THIS</span>
1037
-
1038
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
1039
- <span class="nx">@tab</span> <span class="o">+</span> <span class="s2">&quot;throw #{ @expression.compile o };&quot;</span></pre></div> </td> </tr> <tr id="section-119"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-119">&#182;</a> </div> <h3>Existence</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-120"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-120">&#182;</a> </div> <p>Checks a variable for existence -- not <em>null</em> and not <em>undefined</em>. This is
1040
- similar to <code>.nil?</code> in Ruby, and avoids having to consult a JavaScript truth
1041
- table.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Existence = </span><span class="nx">class</span> <span class="nx">Existence</span> <span class="k">extends</span> <span class="nx">Base</span>
1042
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@expression</span><span class="p">)</span> <span class="o">-&gt;</span>
1043
-
1044
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;expression&#39;</span><span class="p">]</span>
1045
-
1046
- <span class="nx">invert</span><span class="o">:</span> <span class="nx">NEGATE</span>
1047
-
1048
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
1049
- <span class="nv">code = </span><span class="nx">@expression</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span>
1050
- <span class="nv">code = </span><span class="k">if</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">code</span><span class="p">)</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">check</span> <span class="nx">code</span>
1051
- <span class="k">if</span> <span class="nx">@negated</span>
1052
- <span class="s2">&quot;typeof #{code} == \&quot;undefined\&quot; || #{code} === null&quot;</span>
1053
- <span class="k">else</span>
1054
- <span class="s2">&quot;typeof #{code} != \&quot;undefined\&quot; &amp;&amp; #{code} !== null&quot;</span>
1055
- <span class="k">else</span>
1056
- <span class="nv">sym = </span><span class="k">if</span> <span class="nx">@negated</span> <span class="k">then</span> <span class="s1">&#39;==&#39;</span> <span class="k">else</span> <span class="s1">&#39;!=&#39;</span>
1057
- <span class="s2">&quot;#{code} #{sym} null&quot;</span>
1058
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&lt;=</span> <span class="nx">LEVEL_COND</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">&quot;(#{code})&quot;</span></pre></div> </td> </tr> <tr id="section-121"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-121">&#182;</a> </div> <h3>Parens</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-122"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-122">&#182;</a> </div> <p>An extra set of parentheses, specified explicitly in the source. At one time
1059
- we tried to clean up the results by detecting and removing redundant
1060
- parentheses, but no longer -- you can put in as many as you please.</p>
1061
-
1062
- <p>Parentheses are a good way to force any statement to become an expression.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Parens = </span><span class="nx">class</span> <span class="nx">Parens</span> <span class="k">extends</span> <span class="nx">Base</span>
1063
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@body</span><span class="p">)</span> <span class="o">-&gt;</span>
1064
-
1065
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;body&#39;</span><span class="p">]</span>
1066
-
1067
- <span class="nx">unwrap</span> <span class="o">:</span> <span class="o">-&gt;</span> <span class="nx">@body</span>
1068
- <span class="nx">isComplex</span> <span class="o">:</span> <span class="o">-&gt;</span> <span class="nx">@body</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
1069
- <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-&gt;</span> <span class="nx">@body</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span>
1070
-
1071
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
1072
- <span class="nv">expr = </span><span class="nx">@body</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span>
1073
- <span class="k">if</span> <span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">isAtomic</span><span class="p">()</span>
1074
- <span class="nv">expr.front = </span><span class="nx">@front</span>
1075
- <span class="k">return</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
1076
- <span class="nv">code = </span><span class="nx">expr</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_PAREN</span>
1077
- <span class="nv">bare = </span><span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&lt;</span> <span class="nx">LEVEL_OP</span> <span class="o">and</span> <span class="p">(</span><span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">Op</span> <span class="o">or</span> <span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">Call</span> <span class="o">or</span>
1078
- <span class="p">(</span><span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">For</span> <span class="o">and</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">returns</span><span class="p">))</span>
1079
- <span class="k">if</span> <span class="nx">bare</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">&quot;(#{code})&quot;</span></pre></div> </td> </tr> <tr id="section-123"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-123">&#182;</a> </div> <h3>For</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-124"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-124">&#182;</a> </div> <p>CoffeeScript's replacement for the <em>for</em> loop is our array and object
1080
- comprehensions, that compile into <em>for</em> loops here. They also act as an
1081
- expression, able to return the result of each filtered iteration.</p>
1082
-
1083
- <p>Unlike Python array comprehensions, they can be multi-line, and you can pass
1084
- the current index of the loop as a second parameter. Unlike Ruby blocks,
1085
- you can map and filter in a single pass.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.For = </span><span class="nx">class</span> <span class="nx">For</span> <span class="k">extends</span> <span class="nx">Base</span>
1086
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">body</span><span class="p">,</span> <span class="nx">source</span><span class="p">)</span> <span class="o">-&gt;</span>
1087
- <span class="p">{</span><span class="nx">@source</span><span class="p">,</span> <span class="nx">@guard</span><span class="p">,</span> <span class="nx">@step</span><span class="p">,</span> <span class="nx">@name</span><span class="p">,</span> <span class="nx">@index</span><span class="p">}</span> <span class="o">=</span> <span class="nx">source</span>
1088
- <span class="vi">@body = </span><span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">body</span><span class="p">]</span>
1089
- <span class="vi">@own = </span><span class="o">!!</span><span class="nx">source</span><span class="p">.</span><span class="nx">own</span>
1090
- <span class="vi">@object = </span><span class="o">!!</span><span class="nx">source</span><span class="p">.</span><span class="nx">object</span>
1091
- <span class="p">[</span><span class="nx">@name</span><span class="p">,</span> <span class="nx">@index</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nx">@index</span><span class="p">,</span> <span class="nx">@name</span><span class="p">]</span> <span class="k">if</span> <span class="nx">@object</span>
1092
- <span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">&#39;index cannot be a pattern matching expression&#39;</span> <span class="k">if</span> <span class="nx">@index</span> <span class="k">instanceof</span> <span class="nx">Value</span>
1093
- <span class="vi">@range = </span><span class="nx">@source</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span> <span class="nx">@source</span><span class="p">.</span><span class="nx">base</span> <span class="k">instanceof</span> <span class="nx">Range</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@source</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">length</span>
1094
- <span class="vi">@pattern = </span><span class="nx">@name</span> <span class="k">instanceof</span> <span class="nx">Value</span>
1095
- <span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">&#39;indexes do not apply to range loops&#39;</span> <span class="k">if</span> <span class="nx">@range</span> <span class="o">and</span> <span class="nx">@index</span>
1096
- <span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">&#39;cannot pattern match over range loops&#39;</span> <span class="k">if</span> <span class="nx">@range</span> <span class="o">and</span> <span class="nx">@pattern</span>
1097
- <span class="vi">@returns = </span><span class="kc">false</span>
1098
-
1099
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;body&#39;</span><span class="p">,</span> <span class="s1">&#39;source&#39;</span><span class="p">,</span> <span class="s1">&#39;guard&#39;</span><span class="p">,</span> <span class="s1">&#39;step&#39;</span><span class="p">]</span>
1100
-
1101
- <span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
1102
-
1103
- <span class="nx">jumps</span><span class="o">:</span> <span class="nx">While</span><span class="o">::</span><span class="nx">jumps</span>
1104
-
1105
- <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-&gt;</span>
1106
- <span class="vi">@returns = </span><span class="kc">yes</span>
1107
- <span class="k">this</span></pre></div> </td> </tr> <tr id="section-125"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-125">&#182;</a> </div> <p>Welcome to the hairiest method in all of CoffeeScript. Handles the inner
1108
- loop, filtering, stepping, and result saving for array, object, and range
1109
- comprehensions. Some of the generated code can be shared in common, and
1110
- some cannot.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
1111
- <span class="nv">body = </span><span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">@body</span><span class="p">]</span>
1112
- <span class="nv">lastJumps = </span><span class="nx">last</span><span class="p">(</span><span class="nx">body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">)</span><span class="o">?</span><span class="p">.</span><span class="nx">jumps</span><span class="p">()</span>
1113
- <span class="vi">@returns = </span><span class="kc">no</span> <span class="k">if</span> <span class="nx">lastJumps</span> <span class="o">and</span> <span class="nx">lastJumps</span> <span class="k">instanceof</span> <span class="nx">Return</span>
1114
- <span class="nv">source = </span><span class="k">if</span> <span class="nx">@range</span> <span class="k">then</span> <span class="nx">@source</span><span class="p">.</span><span class="nx">base</span> <span class="k">else</span> <span class="nx">@source</span>
1115
- <span class="nv">scope = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span>
1116
- <span class="nv">name = </span><span class="nx">@name</span> <span class="o">and</span> <span class="nx">@name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
1117
- <span class="nv">index = </span><span class="nx">@index</span> <span class="o">and</span> <span class="nx">@index</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
1118
- <span class="nx">scope</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">immediate</span><span class="o">:</span> <span class="kc">yes</span><span class="p">)</span> <span class="k">if</span> <span class="nx">name</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@pattern</span>
1119
- <span class="nx">scope</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="nx">immediate</span><span class="o">:</span> <span class="kc">yes</span><span class="p">)</span> <span class="k">if</span> <span class="nx">index</span>
1120
- <span class="nv">rvar = </span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;results&#39;</span> <span class="k">if</span> <span class="nx">@returns</span>
1121
- <span class="nv">ivar = </span><span class="p">(</span><span class="k">if</span> <span class="nx">@range</span> <span class="k">then</span> <span class="nx">name</span> <span class="k">else</span> <span class="nx">index</span><span class="p">)</span> <span class="o">or</span> <span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;i&#39;</span>
1122
- <span class="nv">name = </span><span class="nx">ivar</span> <span class="k">if</span> <span class="nx">@pattern</span>
1123
- <span class="nv">varPart = </span><span class="s1">&#39;&#39;</span>
1124
- <span class="nv">guardPart = </span><span class="s1">&#39;&#39;</span>
1125
- <span class="nv">defPart = </span><span class="s1">&#39;&#39;</span>
1126
- <span class="nv">idt1 = </span><span class="nx">@tab</span> <span class="o">+</span> <span class="nx">TAB</span>
1127
- <span class="k">if</span> <span class="nx">@range</span>
1128
- <span class="nv">forPart = </span><span class="nx">source</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">merge</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="p">{</span><span class="nx">index</span><span class="o">:</span> <span class="nx">ivar</span><span class="p">,</span> <span class="nx">@step</span><span class="p">})</span>
1129
- <span class="k">else</span>
1130
- <span class="nv">svar = </span><span class="nx">@source</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
1131
- <span class="k">if</span> <span class="p">(</span><span class="nx">name</span> <span class="o">or</span> <span class="nx">@own</span><span class="p">)</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span> <span class="nx">svar</span>
1132
- <span class="nv">defPart = </span><span class="s2">&quot;#{@tab}#{ref = scope.freeVariable &#39;ref&#39;} = #{svar};\n&quot;</span>
1133
- <span class="nv">svar = </span><span class="nx">ref</span>
1134
- <span class="k">if</span> <span class="nx">name</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@pattern</span>
1135
- <span class="nv">namePart = </span><span class="s2">&quot;#{name} = #{svar}[#{ivar}]&quot;</span>
1136
- <span class="nx">unless</span> <span class="nx">@object</span>
1137
- <span class="nv">lvar = </span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;len&#39;</span>
1138
- <span class="nv">stepPart = </span><span class="k">if</span> <span class="nx">@step</span> <span class="k">then</span> <span class="s2">&quot;#{ivar} += #{ @step.compile(o, LEVEL_OP) }&quot;</span> <span class="k">else</span> <span class="s2">&quot;#{ivar}++&quot;</span>
1139
- <span class="nv">forPart = </span><span class="s2">&quot;#{ivar} = 0, #{lvar} = #{svar}.length; #{ivar} &lt; #{lvar}; #{stepPart}&quot;</span>
1140
- <span class="k">if</span> <span class="nx">@returns</span>
1141
- <span class="nv">resultPart = </span><span class="s2">&quot;#{@tab}#{rvar} = [];\n&quot;</span>
1142
- <span class="nv">returnResult = </span><span class="s2">&quot;\n#{@tab}return #{rvar};&quot;</span>
1143
- <span class="nv">body = </span><span class="nx">Push</span><span class="p">.</span><span class="nx">wrap</span> <span class="nx">rvar</span><span class="p">,</span> <span class="nx">body</span>
1144
- <span class="k">if</span> <span class="nx">@guard</span>
1145
- <span class="nv">body = </span><span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="k">new</span> <span class="nx">If</span> <span class="nx">@guard</span><span class="p">,</span> <span class="nx">body</span><span class="p">]</span>
1146
- <span class="k">if</span> <span class="nx">@pattern</span>
1147
- <span class="nx">body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">unshift</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">@name</span><span class="p">,</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s2">&quot;#{svar}[#{ivar}]&quot;</span>
1148
- <span class="nx">defPart</span> <span class="o">+=</span> <span class="nx">@pluckDirectCall</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">body</span>
1149
- <span class="nv">varPart = </span><span class="s2">&quot;\n#{idt1}#{namePart};&quot;</span> <span class="k">if</span> <span class="nx">namePart</span>
1150
- <span class="k">if</span> <span class="nx">@object</span>
1151
- <span class="nv">forPart = </span><span class="s2">&quot;#{ivar} in #{svar}&quot;</span>
1152
- <span class="nv">guardPart = </span><span class="s2">&quot;\n#{idt1}if (!#{utility(&#39;hasProp&#39;)}.call(#{svar}, #{ivar})) continue;&quot;</span> <span class="k">if</span> <span class="nx">@own</span>
1153
- <span class="nv">body = </span><span class="nx">body</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">merge</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">indent</span><span class="o">:</span> <span class="nx">idt1</span><span class="p">),</span> <span class="nx">LEVEL_TOP</span>
1154
- <span class="nv">body = </span><span class="s1">&#39;\n&#39;</span> <span class="o">+</span> <span class="nx">body</span> <span class="o">+</span> <span class="s1">&#39;\n&#39;</span> <span class="k">if</span> <span class="nx">body</span>
1155
- <span class="s2">&quot;&quot;&quot;</span>
1156
- <span class="s2"> #{defPart}#{resultPart or &#39;&#39;}#{@tab}for (#{forPart}) {#{guardPart}#{varPart}#{body}#{@tab}}#{returnResult or &#39;&#39;}</span>
1157
- <span class="s2"> &quot;&quot;&quot;</span>
1158
-
1159
- <span class="nx">pluckDirectCall</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">body</span><span class="p">)</span> <span class="o">-&gt;</span>
1160
- <span class="nv">defs = </span><span class="s1">&#39;&#39;</span>
1161
- <span class="k">for</span> <span class="nx">expr</span><span class="p">,</span> <span class="nx">idx</span> <span class="k">in</span> <span class="nx">body</span><span class="p">.</span><span class="nx">expressions</span>
1162
- <span class="nv">expr = </span><span class="nx">expr</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()</span>
1163
- <span class="k">continue</span> <span class="nx">unless</span> <span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">Call</span>
1164
- <span class="nv">val = </span><span class="nx">expr</span><span class="p">.</span><span class="nx">variable</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()</span>
1165
- <span class="k">continue</span> <span class="nx">unless</span> <span class="p">(</span><span class="nx">val</span> <span class="k">instanceof</span> <span class="nx">Code</span><span class="p">)</span> <span class="o">or</span>
1166
- <span class="p">(</span><span class="nx">val</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span>
1167
- <span class="nx">val</span><span class="p">.</span><span class="nx">base</span><span class="o">?</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()</span> <span class="k">instanceof</span> <span class="nx">Code</span> <span class="o">and</span>
1168
- <span class="nx">val</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">1</span> <span class="o">and</span>
1169
- <span class="nx">val</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span><span class="o">?</span><span class="p">.</span><span class="nx">value</span> <span class="k">in</span> <span class="p">[</span><span class="s1">&#39;call&#39;</span><span class="p">,</span> <span class="s1">&#39;apply&#39;</span><span class="p">])</span>
1170
- <span class="nv">fn = </span><span class="nx">val</span><span class="p">.</span><span class="nx">base</span><span class="o">?</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()</span> <span class="o">or</span> <span class="nx">val</span>
1171
- <span class="nv">ref = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">&#39;fn&#39;</span>
1172
- <span class="nv">base = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">ref</span>
1173
- <span class="k">if</span> <span class="nx">val</span><span class="p">.</span><span class="nx">base</span>
1174
- <span class="p">[</span><span class="nx">val</span><span class="p">.</span><span class="nx">base</span><span class="p">,</span> <span class="nx">base</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nx">base</span><span class="p">,</span> <span class="nx">val</span><span class="p">]</span>
1175
- <span class="nx">args</span><span class="p">.</span><span class="nx">unshift</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s1">&#39;this&#39;</span>
1176
- <span class="nx">body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">[</span><span class="nx">idx</span><span class="p">]</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Call</span> <span class="nx">base</span><span class="p">,</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">args</span>
1177
- <span class="nx">defs</span> <span class="o">+=</span> <span class="nx">@tab</span> <span class="o">+</span> <span class="k">new</span> <span class="nx">Assign</span><span class="p">(</span><span class="nx">ref</span><span class="p">,</span> <span class="nx">fn</span><span class="p">).</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_TOP</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;;\n&#39;</span>
1178
- <span class="nx">defs</span></pre></div> </td> </tr> <tr id="section-126"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-126">&#182;</a> </div> <h3>Switch</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-127"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-127">&#182;</a> </div> <p>A JavaScript <em>switch</em> statement. Converts into a returnable expression on-demand.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Switch = </span><span class="nx">class</span> <span class="nx">Switch</span> <span class="k">extends</span> <span class="nx">Base</span>
1179
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@subject</span><span class="p">,</span> <span class="nx">@cases</span><span class="p">,</span> <span class="nx">@otherwise</span><span class="p">)</span> <span class="o">-&gt;</span>
1180
-
1181
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;subject&#39;</span><span class="p">,</span> <span class="s1">&#39;cases&#39;</span><span class="p">,</span> <span class="s1">&#39;otherwise&#39;</span><span class="p">]</span>
1182
-
1183
- <span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
1184
-
1185
- <span class="nx">jumps</span><span class="o">:</span> <span class="p">(</span><span class="nv">o = </span><span class="p">{</span><span class="nx">block</span><span class="o">:</span> <span class="kc">yes</span><span class="p">})</span> <span class="o">-&gt;</span>
1186
- <span class="k">for</span> <span class="p">[</span><span class="nx">conds</span><span class="p">,</span> <span class="nx">block</span><span class="p">]</span> <span class="k">in</span> <span class="nx">@cases</span>
1187
- <span class="k">return</span> <span class="nx">block</span> <span class="k">if</span> <span class="nx">block</span><span class="p">.</span><span class="nx">jumps</span> <span class="nx">o</span>
1188
- <span class="nx">@otherwise</span><span class="o">?</span><span class="p">.</span><span class="nx">jumps</span> <span class="nx">o</span>
1189
-
1190
- <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-&gt;</span>
1191
- <span class="nx">pair</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">makeReturn</span><span class="p">()</span> <span class="k">for</span> <span class="nx">pair</span> <span class="k">in</span> <span class="nx">@cases</span>
1192
- <span class="nx">@otherwise</span><span class="o">?</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span>
1193
- <span class="k">this</span>
1194
-
1195
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
1196
- <span class="nv">idt1 = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+</span> <span class="nx">TAB</span>
1197
- <span class="nv">idt2 = o.indent = </span><span class="nx">idt1</span> <span class="o">+</span> <span class="nx">TAB</span>
1198
- <span class="nv">code = </span><span class="nx">@tab</span> <span class="o">+</span> <span class="s2">&quot;switch (#{ @subject?.compile(o, LEVEL_PAREN) or false }) {\n&quot;</span>
1199
- <span class="k">for</span> <span class="p">[</span><span class="nx">conditions</span><span class="p">,</span> <span class="nx">block</span><span class="p">],</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">@cases</span>
1200
- <span class="k">for</span> <span class="nx">cond</span> <span class="k">in</span> <span class="nx">flatten</span> <span class="p">[</span><span class="nx">conditions</span><span class="p">]</span>
1201
- <span class="nv">cond = </span><span class="nx">cond</span><span class="p">.</span><span class="nx">invert</span><span class="p">()</span> <span class="nx">unless</span> <span class="nx">@subject</span>
1202
- <span class="nx">code</span> <span class="o">+=</span> <span class="nx">idt1</span> <span class="o">+</span> <span class="s2">&quot;case #{ cond.compile o, LEVEL_PAREN }:\n&quot;</span>
1203
- <span class="nx">code</span> <span class="o">+=</span> <span class="nx">body</span> <span class="o">+</span> <span class="s1">&#39;\n&#39;</span> <span class="k">if</span> <span class="nv">body = </span><span class="nx">block</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_TOP</span>
1204
- <span class="k">break</span> <span class="k">if</span> <span class="nx">i</span> <span class="o">is</span> <span class="nx">@cases</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@otherwise</span>
1205
- <span class="nv">expr = </span><span class="nx">@lastNonComment</span> <span class="nx">block</span><span class="p">.</span><span class="nx">expressions</span>
1206
- <span class="nv">jumper = </span><span class="nx">expr</span><span class="p">.</span><span class="nx">jumps</span><span class="p">()</span>
1207
- <span class="k">if</span> <span class="o">not</span> <span class="nx">expr</span> <span class="o">or</span> <span class="o">not</span> <span class="nx">jumper</span> <span class="o">or</span> <span class="p">(</span><span class="nx">jumper</span> <span class="k">instanceof</span> <span class="nx">Literal</span> <span class="o">and</span> <span class="nx">jumper</span><span class="p">.</span><span class="nx">value</span> <span class="o">is</span> <span class="s1">&#39;debugger&#39;</span><span class="p">)</span>
1208
- <span class="nx">code</span> <span class="o">+=</span> <span class="nx">idt2</span> <span class="o">+</span> <span class="s1">&#39;break;\n&#39;</span>
1209
- <span class="nx">code</span> <span class="o">+=</span> <span class="nx">idt1</span> <span class="o">+</span> <span class="s2">&quot;default:\n#{ @otherwise.compile o, LEVEL_TOP }\n&quot;</span> <span class="k">if</span> <span class="nx">@otherwise</span> <span class="o">and</span> <span class="nx">@otherwise</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">length</span>
1210
- <span class="nx">code</span> <span class="o">+</span> <span class="nx">@tab</span> <span class="o">+</span> <span class="s1">&#39;}&#39;</span></pre></div> </td> </tr> <tr id="section-128"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-128">&#182;</a> </div> <h3>If</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-129"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-129">&#182;</a> </div> <p><em>If/else</em> statements. Acts as an expression by pushing down requested returns
1211
- to the last line of each clause.</p>
1212
-
1213
- <p>Single-expression <strong>Ifs</strong> are compiled into conditional operators if possible,
1214
- because ternaries are already proper expressions, and don't need conversion.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.If = </span><span class="nx">class</span> <span class="nx">If</span> <span class="k">extends</span> <span class="nx">Base</span>
1215
- <span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">condition</span><span class="p">,</span> <span class="nx">@body</span><span class="p">,</span> <span class="nv">options = </span><span class="p">{})</span> <span class="o">-&gt;</span>
1216
- <span class="vi">@condition = </span><span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">type</span> <span class="o">is</span> <span class="s1">&#39;unless&#39;</span> <span class="k">then</span> <span class="nx">condition</span><span class="p">.</span><span class="nx">invert</span><span class="p">()</span> <span class="k">else</span> <span class="nx">condition</span>
1217
- <span class="vi">@elseBody = </span><span class="kc">null</span>
1218
- <span class="vi">@isChain = </span><span class="kc">false</span>
1219
- <span class="p">{</span><span class="nx">@soak</span><span class="p">}</span> <span class="o">=</span> <span class="nx">options</span>
1220
-
1221
- <span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">&#39;condition&#39;</span><span class="p">,</span> <span class="s1">&#39;body&#39;</span><span class="p">,</span> <span class="s1">&#39;elseBody&#39;</span><span class="p">]</span>
1222
-
1223
- <span class="nx">bodyNode</span><span class="o">:</span> <span class="o">-&gt;</span> <span class="nx">@body</span><span class="o">?</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span>
1224
- <span class="nx">elseBodyNode</span><span class="o">:</span> <span class="o">-&gt;</span> <span class="nx">@elseBody</span><span class="o">?</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-130"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-130">&#182;</a> </div> <p>Rewrite a chain of <strong>Ifs</strong> to add a default case as the final <em>else</em>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addElse</span><span class="o">:</span> <span class="p">(</span><span class="nx">elseBody</span><span class="p">)</span> <span class="o">-&gt;</span>
1225
- <span class="k">if</span> <span class="nx">@isChain</span>
1226
- <span class="nx">@elseBodyNode</span><span class="p">().</span><span class="nx">addElse</span> <span class="nx">elseBody</span>
1227
- <span class="k">else</span>
1228
- <span class="vi">@isChain = </span><span class="nx">elseBody</span> <span class="k">instanceof</span> <span class="nx">If</span>
1229
- <span class="vi">@elseBody = </span><span class="nx">@ensureExpressions</span> <span class="nx">elseBody</span>
1230
- <span class="k">this</span></pre></div> </td> </tr> <tr id="section-131"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-131">&#182;</a> </div> <p>The <strong>If</strong> only compiles into a statement if either of its bodies needs
1231
- to be a statement. Otherwise a conditional operator is safe.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">isStatement</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
1232
- <span class="nx">o</span><span class="o">?</span><span class="p">.</span><span class="nx">level</span> <span class="o">is</span> <span class="nx">LEVEL_TOP</span> <span class="o">or</span>
1233
- <span class="nx">@bodyNode</span><span class="p">().</span><span class="nx">isStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">or</span> <span class="nx">@elseBodyNode</span><span class="p">()</span><span class="o">?</span><span class="p">.</span><span class="nx">isStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
1234
-
1235
- <span class="nx">jumps</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nx">@body</span><span class="p">.</span><span class="nx">jumps</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">or</span> <span class="nx">@elseBody</span><span class="o">?</span><span class="p">.</span><span class="nx">jumps</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
1236
-
1237
- <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
1238
- <span class="k">if</span> <span class="nx">@isStatement</span> <span class="nx">o</span> <span class="k">then</span> <span class="nx">@compileStatement</span> <span class="nx">o</span> <span class="k">else</span> <span class="nx">@compileExpression</span> <span class="nx">o</span>
1239
-
1240
- <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-&gt;</span>
1241
- <span class="nx">@body</span> <span class="o">and=</span> <span class="k">new</span> <span class="nx">Expressions</span> <span class="p">[</span><span class="nx">@body</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()]</span>
1242
- <span class="nx">@elseBody</span> <span class="o">and=</span> <span class="k">new</span> <span class="nx">Expressions</span> <span class="p">[</span><span class="nx">@elseBody</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()]</span>
1243
- <span class="k">this</span>
1244
-
1245
- <span class="nx">ensureExpressions</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-&gt;</span>
1246
- <span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Expressions</span> <span class="k">then</span> <span class="nx">node</span> <span class="k">else</span> <span class="k">new</span> <span class="nx">Expressions</span> <span class="p">[</span><span class="nx">node</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-132"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-132">&#182;</a> </div> <p>Compile the <strong>If</strong> as a regular <em>if-else</em> statement. Flattened chains
1247
- force inner <em>else</em> bodies into statement form.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileStatement</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
1248
- <span class="nv">child = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">&#39;chainChild&#39;</span>
1249
- <span class="nv">cond = </span><span class="nx">@condition</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_PAREN</span>
1250
- <span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
1251
- <span class="nv">body = </span><span class="nx">@ensureExpressions</span><span class="p">(</span><span class="nx">@body</span><span class="p">).</span><span class="nx">compile</span> <span class="nx">o</span>
1252
- <span class="nv">body = </span><span class="s2">&quot;\n#{body}\n#{@tab}&quot;</span> <span class="k">if</span> <span class="nx">body</span>
1253
- <span class="nv">ifPart = </span><span class="s2">&quot;if (#{cond}) {#{body}}&quot;</span>
1254
- <span class="nv">ifPart = </span><span class="nx">@tab</span> <span class="o">+</span> <span class="nx">ifPart</span> <span class="nx">unless</span> <span class="nx">child</span>
1255
- <span class="k">return</span> <span class="nx">ifPart</span> <span class="nx">unless</span> <span class="nx">@elseBody</span>
1256
- <span class="nx">ifPart</span> <span class="o">+</span> <span class="s1">&#39; else &#39;</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@isChain</span>
1257
- <span class="nv">o.indent = </span><span class="nx">@tab</span>
1258
- <span class="nv">o.chainChild = </span><span class="kc">yes</span>
1259
- <span class="nx">@elseBody</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">().</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_TOP</span>
1260
- <span class="k">else</span>
1261
- <span class="s2">&quot;{\n#{ @elseBody.compile o, LEVEL_TOP }\n#{@tab}}&quot;</span></pre></div> </td> </tr> <tr id="section-133"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-133">&#182;</a> </div> <p>Compile the If as a conditional operator.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileExpression</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-&gt;</span>
1262
- <span class="nv">cond = </span><span class="nx">@condition</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_COND</span>
1263
- <span class="nv">body = </span><span class="nx">@bodyNode</span><span class="p">().</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
1264
- <span class="nv">alt = </span><span class="k">if</span> <span class="nx">@elseBodyNode</span><span class="p">()</span> <span class="k">then</span> <span class="nx">@elseBodyNode</span><span class="p">().</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span><span class="p">)</span> <span class="k">else</span> <span class="s1">&#39;void 0&#39;</span>
1265
- <span class="nv">code = </span><span class="s2">&quot;#{cond} ? #{body} : #{alt}&quot;</span>
1266
- <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">&gt;=</span> <span class="nx">LEVEL_COND</span> <span class="k">then</span> <span class="s2">&quot;(#{code})&quot;</span> <span class="k">else</span> <span class="nx">code</span>
1267
-
1268
- <span class="nx">unfoldSoak</span><span class="o">:</span> <span class="o">-&gt;</span>
1269
- <span class="nx">@soak</span> <span class="o">and</span> <span class="k">this</span></pre></div> </td> </tr> <tr id="section-134"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-134">&#182;</a> </div> <h2>Faux-Nodes</h2>
1270
-
1271
- <p>Faux-nodes are never created by the grammar, but are used during code
1272
- generation to generate other combinations of nodes.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-135"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-135">&#182;</a> </div> <h3>Push</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-136"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-136">&#182;</a> </div> <p>The <strong>Push</strong> creates the tree for <code>array.push(value)</code>,
1273
- which is helpful for recording the result arrays from comprehensions.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">Push =</span>
1274
- <span class="nx">wrap</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">exps</span><span class="p">)</span> <span class="o">-&gt;</span>
1275
- <span class="k">return</span> <span class="nx">exps</span> <span class="k">if</span> <span class="nx">exps</span><span class="p">.</span><span class="nx">isEmpty</span><span class="p">()</span> <span class="o">or</span> <span class="nx">last</span><span class="p">(</span><span class="nx">exps</span><span class="p">.</span><span class="nx">expressions</span><span class="p">).</span><span class="nx">jumps</span><span class="p">()</span>
1276
- <span class="nx">exps</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">Call</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span><span class="p">(</span><span class="nx">name</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">Access</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s1">&#39;push&#39;</span><span class="p">]),</span> <span class="p">[</span><span class="nx">exps</span><span class="p">.</span><span class="nx">pop</span><span class="p">()]</span></pre></div> </td> </tr> <tr id="section-137"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-137">&#182;</a> </div> <h3>Closure</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-138"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-138">&#182;</a> </div> <p>A faux-node used to wrap an expressions body in a closure.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">Closure =</span></pre></div> </td> </tr> <tr id="section-139"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-139">&#182;</a> </div> <p>Wrap the expressions body, unless it contains a pure statement,
1277
- in which case, no dice. If the body mentions <code>this</code> or <code>arguments</code>,
1278
- then make sure that the closure wrapper preserves the original values.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">wrap</span><span class="o">:</span> <span class="p">(</span><span class="nx">expressions</span><span class="p">,</span> <span class="nx">statement</span><span class="p">,</span> <span class="nx">noReturn</span><span class="p">)</span> <span class="o">-&gt;</span>
1279
- <span class="k">return</span> <span class="nx">expressions</span> <span class="k">if</span> <span class="nx">expressions</span><span class="p">.</span><span class="nx">jumps</span><span class="p">()</span>
1280
- <span class="nv">func = </span><span class="k">new</span> <span class="nx">Code</span> <span class="p">[],</span> <span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">expressions</span><span class="p">]</span>
1281
- <span class="nv">args = </span><span class="p">[]</span>
1282
- <span class="k">if</span> <span class="p">(</span><span class="nv">mentionsArgs = </span><span class="nx">expressions</span><span class="p">.</span><span class="nx">contains</span> <span class="nx">@literalArgs</span><span class="p">)</span> <span class="o">or</span>
1283
- <span class="p">(</span> <span class="nx">expressions</span><span class="p">.</span><span class="nx">contains</span> <span class="nx">@literalThis</span><span class="p">)</span>
1284
- <span class="nv">meth = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="k">if</span> <span class="nx">mentionsArgs</span> <span class="k">then</span> <span class="s1">&#39;apply&#39;</span> <span class="k">else</span> <span class="s1">&#39;call&#39;</span>
1285
- <span class="nv">args = </span><span class="p">[</span><span class="k">new</span> <span class="nx">Literal</span> <span class="s1">&#39;this&#39;</span><span class="p">]</span>
1286
- <span class="nx">args</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s1">&#39;arguments&#39;</span> <span class="k">if</span> <span class="nx">mentionsArgs</span>
1287
- <span class="nv">func = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">func</span><span class="p">,</span> <span class="p">[</span><span class="k">new</span> <span class="nx">Access</span> <span class="nx">meth</span><span class="p">]</span>
1288
- <span class="nv">func.noReturn = </span><span class="nx">noReturn</span>
1289
- <span class="nv">call = </span><span class="k">new</span> <span class="nx">Call</span> <span class="nx">func</span><span class="p">,</span> <span class="nx">args</span>
1290
- <span class="k">if</span> <span class="nx">statement</span> <span class="k">then</span> <span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">call</span><span class="p">]</span> <span class="k">else</span> <span class="nx">call</span>
1291
-
1292
- <span class="nx">literalArgs</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-&gt;</span>
1293
- <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Literal</span> <span class="o">and</span> <span class="nx">node</span><span class="p">.</span><span class="nx">value</span> <span class="o">is</span> <span class="s1">&#39;arguments&#39;</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">node</span><span class="p">.</span><span class="nx">asKey</span>
1294
- <span class="nx">literalThis</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-&gt;</span>
1295
- <span class="p">(</span><span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Literal</span> <span class="o">and</span> <span class="nx">node</span><span class="p">.</span><span class="nx">value</span> <span class="o">is</span> <span class="s1">&#39;this&#39;</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">node</span><span class="p">.</span><span class="nx">asKey</span><span class="p">)</span> <span class="o">or</span>
1296
- <span class="p">(</span><span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Code</span> <span class="o">and</span> <span class="nx">node</span><span class="p">.</span><span class="nx">bound</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-140"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-140">&#182;</a> </div> <p>Unfold a node's child if soak, then tuck the node under created <code>If</code></p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">unfoldSoak = </span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">parent</span><span class="p">,</span> <span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
1297
- <span class="k">return</span> <span class="nx">unless</span> <span class="nv">ifn = </span><span class="nx">parent</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">unfoldSoak</span> <span class="nx">o</span>
1298
- <span class="nx">parent</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">ifn</span><span class="p">.</span><span class="nx">body</span>
1299
- <span class="nv">ifn.body = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">parent</span>
1300
- <span class="nx">ifn</span></pre></div> </td> </tr> <tr id="section-141"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-141">&#182;</a> </div> <h2>Constants</h2> </td> <td class="code"> <div class="highlight"><pre><span class="nv">UTILITIES =</span></pre></div> </td> </tr> <tr id="section-142"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-142">&#182;</a> </div> <p>Correctly set up a prototype chain for inheritance, including a reference
1301
- to the superclass for <code>super()</code> calls, and copies of any static properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">extends</span><span class="o">:</span> <span class="s1">&#39;&#39;&#39;</span>
1302
- <span class="s1"> function(child, parent) {</span>
1303
- <span class="s1"> for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }</span>
1304
- <span class="s1"> function ctor() { this.constructor = child; }</span>
1305
- <span class="s1"> ctor.prototype = parent.prototype;</span>
1306
- <span class="s1"> child.prototype = new ctor;</span>
1307
- <span class="s1"> child.__super__ = parent.prototype;</span>
1308
- <span class="s1"> return child;</span>
1309
- <span class="s1"> }</span>
1310
- <span class="s1"> &#39;&#39;&#39;</span></pre></div> </td> </tr> <tr id="section-143"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-143">&#182;</a> </div> <p>Create a function bound to the current value of "this".</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">bind</span><span class="o">:</span> <span class="s1">&#39;&#39;&#39;</span>
1311
- <span class="s1"> function(fn, me){ return function(){ return fn.apply(me, arguments); }; }</span>
1312
- <span class="s1"> &#39;&#39;&#39;</span></pre></div> </td> </tr> <tr id="section-144"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-144">&#182;</a> </div> <p>Discover if an item is in an array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">indexOf</span><span class="o">:</span> <span class="s1">&#39;&#39;&#39;</span>
1313
- <span class="s1"> Array.prototype.indexOf || function(item) {</span>
1314
- <span class="s1"> for (var i = 0, l = this.length; i &lt; l; i++) {</span>
1315
- <span class="s1"> if (this[i] === item) return i;</span>
1316
- <span class="s1"> }</span>
1317
- <span class="s1"> return -1;</span>
1318
- <span class="s1"> }</span>
1319
- <span class="s1"> &#39;&#39;&#39;</span></pre></div> </td> </tr> <tr id="section-145"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-145">&#182;</a> </div> <p>Shortcuts to speed up the lookup time for native functions.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">hasProp</span><span class="o">:</span> <span class="s1">&#39;Object.prototype.hasOwnProperty&#39;</span>
1320
- <span class="nx">slice</span> <span class="o">:</span> <span class="s1">&#39;Array.prototype.slice&#39;</span></pre></div> </td> </tr> <tr id="section-146"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-146">&#182;</a> </div> <p>Levels indicates a node's position in the AST. Useful for knowing if
1321
- parens are necessary or superfluous.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">LEVEL_TOP = </span><span class="mi">1</span> <span class="c1"># ...;</span>
1322
- <span class="nv">LEVEL_PAREN = </span><span class="mi">2</span> <span class="c1"># (...)</span>
1323
- <span class="nv">LEVEL_LIST = </span><span class="mi">3</span> <span class="c1"># [...]</span>
1324
- <span class="nv">LEVEL_COND = </span><span class="mi">4</span> <span class="c1"># ... ? x : y</span>
1325
- <span class="nv">LEVEL_OP = </span><span class="mi">5</span> <span class="c1"># !...</span>
1326
- <span class="nv">LEVEL_ACCESS = </span><span class="mi">6</span> <span class="c1"># ...[0]</span></pre></div> </td> </tr> <tr id="section-147"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-147">&#182;</a> </div> <p>Tabs are two spaces for pretty printing.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">TAB = </span><span class="s1">&#39; &#39;</span></pre></div> </td> </tr> <tr id="section-148"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-148">&#182;</a> </div> <p>Trim out all trailing whitespace, so that the generated code plays nice
1327
- with Git.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">TRAILING_WHITESPACE = </span><span class="sr">/[ \t]+$/gm</span>
1328
-
1329
- <span class="nv">IDENTIFIER = </span><span class="sr">/^[$A-Za-z_][$\w]*$/</span>
1330
- <span class="nv">SIMPLENUM = </span><span class="sr">/^[+-]?\d+$/</span></pre></div> </td> </tr> <tr id="section-149"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-149">&#182;</a> </div> <p>Is a literal value a string?</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IS_STRING = </span><span class="sr">/^[&#39;&quot;]/</span></pre></div> </td> </tr> <tr id="section-150"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-150">&#182;</a> </div> <h2>Utility Functions</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-151"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-151">&#182;</a> </div> <p>Helper for ensuring that utility functions are assigned at the top level.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">utility = </span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-&gt;</span>
1331
- <span class="nv">ref = </span><span class="s2">&quot;__#{name}&quot;</span>
1332
- <span class="nx">Scope</span><span class="p">.</span><span class="nx">root</span><span class="p">.</span><span class="nx">assign</span> <span class="nx">ref</span><span class="p">,</span> <span class="nx">UTILITIES</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span>
1333
- <span class="nx">ref</span>
1334
-
1335
- <span class="nv">multident = </span><span class="p">(</span><span class="nx">code</span><span class="p">,</span> <span class="nx">tab</span><span class="p">)</span> <span class="o">-&gt;</span>
1336
- <span class="nx">code</span><span class="p">.</span><span class="nx">replace</span> <span class="sr">/\n/g</span><span class="p">,</span> <span class="s1">&#39;$&amp;&#39;</span> <span class="o">+</span> <span class="nx">tab</span>
1337
-
1338
- </pre></div> </td> </tr> </tbody> </table> </div> </body> </html>