Compare commits

...

314 Commits
0.2.3 ... 0.5.0

Author SHA1 Message Date
Chris Hoffman
f582b73035 Keep the correct state for watching files so the appropriate one is recompiled 2010-02-21 19:00:48 -06:00
Jeremy Ashkenas
e795f41bd2 wrapping up loose ends for 0.5.0 2010-02-21 19:28:12 -05:00
Jeremy Ashkenas
5d541232ef adding /usr/local/lib hardcoded versions of coffee and cake for the install script 2010-02-21 19:19:58 -05:00
Jeremy Ashkenas
07513ba928 documentation for Cake and Cakefiles 2010-02-21 18:37:52 -05:00
Jeremy Ashkenas
aded80c101 list padding for the docs 2010-02-21 17:59:17 -05:00
Jeremy Ashkenas
5d893947ea new version of the docs for 0.5.0 is done, or close to it. 2010-02-21 17:30:41 -05:00
Jeremy Ashkenas
535cf28220 one more documentation waypoint, now with running for Try CoffeeScript, and a recompiled Underscore.coffee -- wordsmithing next 2010-02-21 16:36:34 -05:00
Jeremy Ashkenas
dbe5328c33 removing the 'var' declaration from arguments-to-array conversions 2010-02-21 16:15:01 -05:00
Jeremy Ashkenas
6e15a4da0e waypoint on new coffeescript docs --they're coming along nicely 2010-02-21 16:13:09 -05:00
Jeremy Ashkenas
fc51f0ef6c first working draft of the new documentation 2010-02-21 14:30:21 -05:00
Jeremy Ashkenas
1f2c8df5fa updating to latest Node (0.1.3) API -- callbacks, not promises 2010-02-21 14:06:01 -05:00
Jeremy Ashkenas
bea40a7a92 re-enabling the --no-wrap flag, and cleaning up an unused method in command_line 2010-02-21 13:48:38 -05:00
Jeremy Ashkenas
f679590bef rengenerating documentation 2010-02-21 11:46:24 -05:00
Jeremy Ashkenas
bd61131f5c regenerating documentation 2010-02-21 11:45:03 -05:00
Jeremy Ashkenas
ff25361896 removing the build:parser -> build dependency in the Cakefile -- the asynchrony was breaking it anyway 2010-02-21 11:40:52 -05:00
Jeremy Ashkenas
08dcc7e107 removing commented-out parseerror 2010-02-21 11:22:49 -05:00
Jeremy Ashkenas
b027b5cf0d Allowing @[property] syntax. 2010-02-20 20:09:52 -05:00
Jeremy Ashkenas
0f2a2ee11e Improving soaks to avoid uncessesary parentheses. 2010-02-20 20:00:07 -05:00
Jeremy Ashkenas
a93229b14d fixing an off-by-one error in Splat compilation 2010-02-20 18:25:36 -05:00
Jeremy Ashkenas
2d3f6b80c1 Revert "fixing api changes (no more promises) for node v.0.1.29"
This reverts commit 9503ea3040.
2010-02-20 18:12:09 -05:00
alunny
9503ea3040 fixing api changes (no more promises) for node v.0.1.29 2010-02-20 14:45:16 -08:00
Jeremy Ashkenas
08539a156e merging cehoffman/master 2010-02-19 18:30:47 -05:00
Jeremy Ashkenas
b183b091ee removing redundant compile from command_line.coffee 2010-02-19 18:29:24 -05:00
Jeremy Ashkenas
c39415da44 using the new fs.writeFile API instead of fs.open -- much, much nicer 2010-02-19 18:27:50 -05:00
Chris Hoffman
2c461f6474 Simpler write method that assumes utf8 output like input reading does 2010-02-19 16:03:37 -06:00
Chris Hoffman
e213964793 No need to recompile the script for writing 2010-02-19 16:02:58 -06:00
Jeremy Ashkenas
dd753d3b78 Removing the notion of a ThisNode so that we don't have to worry about the special cases. Fixes Issue 180 2010-02-19 07:51:52 -05:00
Jeremy Ashkenas
45c0d4c2ea fixing standalone @ references 2010-02-18 21:50:42 -05:00
Jeremy Ashkenas
4f89f90dab adding syntax highlighting for standalone @ts 2010-02-18 21:32:54 -05:00
Jeremy Ashkenas
0270e48a01 using '== null' to check for soaked accessor chains instead of '== undefined' -- shorter, and slightly safer 2010-02-18 21:04:41 -05:00
Jeremy Ashkenas
a278d8f018 adding an extra pair of braces to the code produced by object comprehensions, so as to squeak by in JSLint's good graces 2010-02-18 20:22:53 -05:00
Jeremy Ashkenas
d4a180c413 carefully categorizing JS keywords from JS reserved words from Coffee keywords, so that we can throw syntax errors at compile time if JS keywords are getting assigned to. 2010-02-18 20:09:41 -05:00
Jeremy Ashkenas
28a8c05513 light refactor for repl.coffee 2010-02-17 23:51:43 -05:00
Jeremy Ashkenas
879b3d76bd light refactors to scope.coffee 2010-02-17 23:37:39 -05:00
Jeremy Ashkenas
138692183b removing the 'remote' parameter from Scope::find -- it wasn't used anymore 2010-02-17 23:26:03 -05:00
Jeremy Ashkenas
95f3e2f79f just namespacing the coffeescript compiler 2010-02-17 23:25:17 -05:00
Jeremy Ashkenas
dec9950649 Revert "namespacing CoffeeScript in the browser, and sniffing the 'require' function instead of the 'exports' object"
This reverts commit 2f6b69b580.
2010-02-17 23:22:05 -05:00
Jeremy Ashkenas
2f6b69b580 namespacing CoffeeScript in the browser, and sniffing the 'require' function instead of the 'exports' object 2010-02-17 23:15:37 -05:00
Jeremy Ashkenas
ff1fd97924 removing traces of Ruby from coffee-script.coffee, redoing narwhal support to use the new compiler (but untested) 2010-02-17 22:37:56 -05:00
Jeremy Ashkenas
87e60dccf0 starting to cache fancy switch values -- fixing issue #171 2010-02-17 21:23:59 -05:00
Jeremy Ashkenas
8ff977dc65 making 'no such task' errors a little nicer 2010-02-17 20:37:30 -05:00
Jeremy Ashkenas
fbfa12c733 moving 'throw' to a slightly higher level of precedence 2010-02-17 19:35:34 -05:00
Jeremy Ashkenas
6a45d25777 adding a test for parenthesized throws 2010-02-17 19:29:37 -05:00
Jeremy Ashkenas
2b5d596e10 allowing parenthetical nodes to wrap statements without necessarily expression-izing them -- tests are passing ... we'll see how this works in practice. 2010-02-17 19:19:51 -05:00
Jeremy Ashkenas
e2a71d3c2c removing the '--' from /usr/bin/env node, until we can find a workaround for linux. 2010-02-17 19:05:22 -05:00
Jeremy Ashkenas
807e0b479d add a note about installing Node.js first 2010-02-17 10:29:13 -05:00
Jeremy Ashkenas
dfa63839bb Updating fs module to the latest Node.js -- that's fs.readFile, not fs.cat, with string flags for fs.open 2010-02-17 08:51:27 -05:00
Jeremy Ashkenas
0490cb2920 added experimental (but working) method to install CoffeeScript: 'sudo bin/cake install' -- once you've done that, you can take cake and coffee out of their bin/ 2010-02-17 01:24:02 -05:00
Jeremy Ashkenas
2d0ad73af8 reorganizing lib/coffee_script to plain ol' lib 2010-02-17 00:55:56 -05:00
Jeremy Ashkenas
5a81fcd42e we don't need relative require's because the paths are set up correctly -- removing the unused runner.coffee script 2010-02-17 00:50:08 -05:00
Jeremy Ashkenas
6446e0004c The Great Purge. Removing the Ruby compiler, and all of its accoutrements. bin/coffee is now CoffeeScript-in-CoffeeScript 2010-02-17 00:33:55 -05:00
Jeremy Ashkenas
edf5f4947e removing all tasks from the Rakefile except for 'doc' ... we might need to keep that around for a while 2010-02-17 00:26:03 -05:00
Jeremy Ashkenas
b674163a40 adding build:ultraviolet and build:underscore tasks to the Cakefile 2010-02-17 00:22:06 -05:00
Jeremy Ashkenas
9f2badb3e9 got a really nice --watch flag for bin/node_coffee going, thanks to Node.js' process.watchFile. Can be used with --print, --lint, --tokens, --run, or whatever your needs may be 2010-02-16 23:59:32 -05:00
Jeremy Ashkenas
0610e20a3c fixing the extends keyword when the expressions are complex, and should only be run once -- not that it's good style -- ticket #143 2010-02-16 23:23:43 -05:00
Jeremy Ashkenas
bedc005d67 Adding a CoffeeScript equivalent to Rake/Make/Jake (Cake, naturally), and implementing all of our build and test tasks in the Cakefile. Run bin/cake to see the tasks. 2010-02-16 20:42:10 -05:00
Jeremy Ashkenas
a8a46257ae finished converting the test suite, to run it, do: bin/node_coffee -r tasks.coffee -- test 2010-02-16 19:45:25 -05:00
Jeremy Ashkenas
b41afe79b4 removed ruby tests, and started the process of converting the CoffeeScript tests over to use Node's assert() module 2010-02-16 19:38:52 -05:00
Jeremy Ashkenas
448ed36cd2 got a build script for rebuilding the compiler, too 2010-02-16 19:17:57 -05:00
Jeremy Ashkenas
c4d19cd1fa removing underscore.js from lib, and from the docs page 2010-02-16 18:39:31 -05:00
Jeremy Ashkenas
495ca64c46 removing underscore as a dependency for nodes.coffee -- let's be minimal 2010-02-16 18:38:03 -05:00
Jeremy Ashkenas
0f2cf552e9 using node's process.compile for better filename reporting on errors 2010-02-16 18:16:58 -05:00
Jeremy Ashkenas
f6a1f16146 Merge branch 'master' of git://github.com/olsonjeffery/coffee-script 2010-02-16 18:06:29 -05:00
Jeremy Ashkenas
db6bc0ba02 Implementing sethaurus' suggestion for better temp variable names -- getting rid of the numbers. 2010-02-16 18:00:40 -05:00
Jeremy Ashkenas
e4bb6c91e7 adding the list of javascript reserved words as syntax errors in CoffeeScript, so that no one uses them accidentally. 2010-02-16 17:41:38 -05:00
Jeremy Ashkenas
561a02c35f updating rewrite rules for jots multiline function case 2010-02-16 14:56:17 -05:00
Jeremy Ashkenas
51e80484e2 fixing an overzealous access in the rewriter 2010-02-16 10:12:40 -05:00
Jeremy Ashkenas
79fa4723ab use a temp var for range expansion, instead of 'i' 2010-02-16 08:58:29 -05:00
Jeremy Ashkenas
a3c8c0b492 allowing keywords as identifiers, when used as identifiers, because we've got more keywords than JavaScript does. 2010-02-16 08:43:58 -05:00
Jeremy Ashkenas
2f389f1d51 beginnings of a build script 2010-02-16 01:04:48 -05:00
Jeffery Olson
85e5dffad5 adding updated coffee.vim syntax file 2010-02-15 21:32:15 -08:00
Jeremy Ashkenas
fa63288f52 fixed the while-loop-condition-with-implicit-function-call bug 2010-02-15 23:05:54 -05:00
Jeremy Ashkenas
4ea8be8e0b switched from alphabetical __a __b temporary variables, to numeric _1, _2, which will be shorter in most cases 2010-02-15 21:55:57 -05:00
Jeremy Ashkenas
48c501a7a2 print_tokens should acutally, y'know, print 2010-02-15 20:46:36 -05:00
Jeremy Ashkenas
63c2b2bc64 adding paren wrappers for property accesses directly on object literals 2010-02-15 19:13:08 -05:00
Jeremy Ashkenas
639be2ff09 updating for Node.js' change from 'posix' to 'fs' 2010-02-15 19:08:14 -05:00
Jeremy Ashkenas
8b1b3ea402 updating docs with #coffeescript channel 2010-02-15 18:58:27 -05:00
Jeremy Ashkenas
9c3040b704 moving print_tokens into the actual coffee-script module, so we can use it separately from the command-line 2010-02-15 18:09:01 -05:00
Jeremy Ashkenas
e7291f57ba fixing allowing empty functions like () -> 2010-02-15 18:03:00 -05:00
Jeremy Ashkenas
3f6eceac77 whoops, screwed up the definition order in the inheritance example 2010-02-15 13:55:28 -05:00
Jeremy Ashkenas
ba7a454f92 adding a note to the docs about colon/equals equality 2010-02-15 13:43:35 -05:00
Jeremy Ashkenas
3092d74a08 updating the readme with a warning about the self-compiler 2010-02-14 23:40:18 -05:00
Jeremy Ashkenas
ff8e0c9751 added hugs' webserver example 2010-02-14 23:20:53 -05:00
Jeremy Ashkenas
3e518e3cf9 fixing long-flag-with-optional-part in optparse.coffee, and hiding bin/node_coffee --watch until we implement it 2010-02-14 20:50:45 -05:00
Jeremy Ashkenas
7667e16732 implementing grayrest's suggested cleanup for object comprehensions 2010-02-14 17:35:14 -05:00
Jeremy Ashkenas
e110042275 updated the rewriter to use @prop instead of this.prop -- compiled JS doesn't change 2010-02-14 16:09:11 -05:00
Jeremy Ashkenas
06677b0545 update the highlighted underscore to 0.5.8 2010-02-14 16:04:10 -05:00
Jeremy Ashkenas
20d105ba4e rename the repl id to try_coffe 2010-02-14 15:41:08 -05:00
Jeremy Ashkenas
e77f4f61aa removing the vendored optparse in favor of a pure-coffeescript optparse library 2010-02-14 15:16:33 -05:00
Jeremy Ashkenas
9de729e825 enabling the --eval option for the self-compiler 2010-02-13 23:27:13 -05:00
Jeremy Ashkenas
c39c2e3599 that's max-height 2010-02-13 19:14:28 -05:00
Jeremy Ashkenas
ecfa212189 updating Jison 2010-02-13 18:19:59 -05:00
Jeremy Ashkenas
97fd126a7f the improved error messages are still-to-be-improved 2010-02-13 16:28:07 -05:00
Jeremy Ashkenas
844c756940 working 'Try CoffeeScript' in the docs 2010-02-13 16:23:03 -05:00
Jeremy Ashkenas
a90bf75395 got coffeescript compiling in the browser 2010-02-13 15:25:04 -05:00
Jeremy Ashkenas
79bb0da153 add a -- to let us parse our own flags 2010-02-13 10:45:05 -05:00
Jeremy Ashkenas
c88b1f6a15 got the CoffeeScript-in-CoffeeScript REPL running, and boy is she fast 2010-02-13 10:27:18 -05:00
Jeremy Ashkenas
b224d58a36 don't make the command-line so OOP-y -- it's just a script 2010-02-13 10:16:28 -05:00
Jeremy Ashkenas
7d348b5eae display the usage message when called without arguments 2010-02-13 10:07:59 -05:00
Jeremy Ashkenas
02ac3edebf Nicer-looking --tokenize, more in line with what Ruby's doing 2010-02-13 09:59:13 -05:00
Jeremy Ashkenas
4bad3e0f4f nicer --tree printing, with values inlines to the right 2010-02-13 09:51:52 -05:00
Jeremy Ashkenas
8147ef554a after correctly recording objectnode's children, bin/node_coffee --tree is working pretty nicely 2010-02-13 09:46:07 -05:00
Jeremy Ashkenas
785c4fb5a0 recording else_body as a child of IfNode, when added after the face 2010-02-13 09:44:12 -05:00
Jeremy Ashkenas
13b2dc8d31 subtle bug in the order of the rewriter rules was causing some if/else chains to get confused by implicit indentation 2010-02-13 09:39:25 -05:00
Jeremy Ashkenas
a62923ff97 use Underscore's flatten -- start to make a nicer tree printer 2010-02-13 09:13:50 -05:00
Jeremy Ashkenas
dd6be80fca Use _.extend instead of the hand-rolled merge 2010-02-13 08:50:29 -05:00
Jeremy Ashkenas
8c077f0f65 using underscore's clone() for code generation 2010-02-13 02:32:17 -05:00
Jeremy Ashkenas
2c4c4cc93e using Underscore's compact in the code generation, insstead of our home-rolled one. 2010-02-13 02:30:47 -05:00
Jeremy Ashkenas
1ab3b183a8 Using underscore for an any() function. 2010-02-13 02:29:37 -05:00
Jeremy Ashkenas
e6a53bd852 fixing the lexer for indented files 2010-02-13 02:16:31 -05:00
Jeremy Ashkenas
b983b3fcdc moving Underscore.coffee from /examples to /src 2010-02-13 02:05:00 -05:00
Jeremy Ashkenas
89cac4071e updating underscore.coffee to 0.5.8 2010-02-13 02:03:48 -05:00
Jeremy Ashkenas
506ea8aa52 adding an if/else/chain test 2010-02-13 02:00:39 -05:00
Jeremy Ashkenas
b965fcf32d The moment of truth: CoffeeScript has now compiled itself ten times over. 2010-02-13 01:13:08 -05:00
Jeremy Ashkenas
126f6c2d88 use parseInt to make octal numbers to keep JSLint happy 2010-02-13 01:04:53 -05:00
Jeremy Ashkenas
3dc456572b finally, think we have this if/else/if/else/else thing licke 2010-02-13 01:02:10 -05:00
Jeremy Ashkenas
0f26072ad0 alright, really fixing the comments this time 2010-02-13 00:28:16 -05:00
Jeremy Ashkenas
dc9cec2611 fixing object/comment printing 2010-02-13 00:24:26 -05:00
Jeremy Ashkenas
c9aeae757b pulling the lexer and rewriter updates without the grammar change 2010-02-13 00:15:34 -05:00
Jeremy Ashkenas
094c2682bd Revert "one more bit to pass -- futzing with the if/else chains"
This reverts commit 066ee52615.
2010-02-13 00:11:14 -05:00
Jeremy Ashkenas
066ee52615 one more bit to pass -- futzing with the if/else chains 2010-02-12 23:35:03 -05:00
Jeremy Ashkenas
ee1c9b284a typo 2010-02-12 23:10:51 -05:00
Jeremy Ashkenas
d9fba94983 added coffee --lint to the self-compiler's resume 2010-02-12 23:09:57 -05:00
Jeremy Ashkenas
e02bedcf82 adding the ability to write out compiled coffeescript to the command line 2010-02-12 22:59:21 -05:00
Jeremy Ashkenas
1552470413 Happy Birthday, CoffeeScript-in-CoffeeScript, you just passed the entire test suite. 2010-02-12 22:24:57 -05:00
Jeremy Ashkenas
e2f3c2259b fix prototype_access lexing 2010-02-12 22:21:22 -05:00
Jeremy Ashkenas
249bd99656 fixing if/else rules 2010-02-12 22:16:26 -05:00
Jeremy Ashkenas
b36196286a safer lexing for ruby, too 2010-02-12 21:06:00 -05:00
Jeremy Ashkenas
b21780b738 safer lexing at the start of the files 2010-02-12 21:04:33 -05:00
Jeremy Ashkenas
207ec81821 waypoint ... somethings still a little off with the parser 2010-02-12 19:45:20 -05:00
Jeremy Ashkenas
d61aaf393a little further 2010-02-12 17:31:23 -05:00
Jeremy Ashkenas
19c44c9b62 self-compiling the arguments test 2010-02-12 16:44:29 -05:00
Jeremy Ashkenas
4deabf5e01 passing the funky comment test 2010-02-12 16:26:28 -05:00
Jeremy Ashkenas
1a6194e9f0 self-compiling switch statements 2010-02-12 16:23:52 -05:00
Jeremy Ashkenas
156a0b13d9 self-compiling closures as expressions 2010-02-12 14:26:01 -05:00
Jeremy Ashkenas
61a7f7a567 self-compiling the existential operator 2010-02-12 14:15:14 -05:00
Jeremy Ashkenas
dbcb9df22b correctly self-compiling @references 2010-02-12 13:56:58 -05:00
Jeremy Ashkenas
e2ad1190ac Array comprehensions are alive and well on the self-compiler 2010-02-12 13:52:57 -05:00
Jeremy Ashkenas
c0f9058f15 range comprehensions are working smooth 2010-02-12 13:51:44 -05:00
Jeremy Ashkenas
12859e575a recompiling with latest jison (modified) and passing codenode generation 2010-02-12 13:35:34 -05:00
Jeremy Ashkenas
3f765c356a Revert "upgrading to latest jison, rebuilding the parser"
This reverts commit fd8b540a66.
2010-02-12 00:15:09 -05:00
Jeremy Ashkenas
fd8b540a66 upgrading to latest jison, rebuilding the parser 2010-02-12 00:12:46 -05:00
Jeremy Ashkenas
29267593c2 fixed heredoc lexing regex for JS, now passing test_heredocs.coffee 2010-02-12 00:06:41 -05:00
Jeremy Ashkenas
1e74805aa4 test_operations.coffee is now compiling successfully 2010-02-11 23:59:56 -05:00
Jeremy Ashkenas
12685aa54a test_functions.coffee compiles and runs successfully. 2010-02-11 23:57:31 -05:00
Jeremy Ashkenas
04f07f4c15 empty functions are legal code 2010-02-11 23:39:25 -05:00
Jeremy Ashkenas
df386a3b3f updated to latest jison, rebuilt parser 2010-02-11 23:34:45 -05:00
Jeremy Ashkenas
13c49ad865 waypoint -- jison has a reverse order of operations from yacc 2010-02-11 23:29:12 -05:00
Jeremy Ashkenas
7c01bba4f4 added the ability to print the parse tree 2010-02-11 23:11:05 -05:00
Jeremy Ashkenas
950d1199c2 self-compiling ranges and expressions 2010-02-11 20:20:59 -05:00
Jeremy Ashkenas
38d1381c02 self-compiling pattern matching correctly 2010-02-11 20:11:11 -05:00
Jeremy Ashkenas
98f15d001f got pattern matching working 2010-02-11 18:44:00 -05:00
Jeremy Ashkenas
a379530d41 disabling traces in the jison parser fixes the performance problems -- ahh, much better 2010-02-11 18:34:30 -05:00
Jeremy Ashkenas
e19b67cb79 still not there 2010-02-11 02:52:41 -05:00
Jeremy Ashkenas
e883a559ca uncomment test 2010-02-11 02:41:24 -05:00
Jeremy Ashkenas
713f6f32e1 done for now 2010-02-11 02:39:57 -05:00
Jeremy Ashkenas
872b36c11d things are in motion -- bin/node_coffee is the new JS-only command line ... it can pass some of the tests 2010-02-11 01:57:33 -05:00
Jeremy Ashkenas
f761c25dcd It's beginning to come alive. 2010-02-10 23:24:05 -05:00
Jeremy Ashkenas
38e1991f82 and now with if/else statements, CoffeeScript-in-CoffeeScript is language-complete -- now for the shakedown cruise 2010-02-10 21:40:10 -05:00
Jeremy Ashkenas
13d3b3a3ce now self-compiling array/object/range comprehensions. 2010-02-10 20:19:59 -05:00
Jeremy Ashkenas
0c2a13b468 self-compiler handles throws, the existential operator, and parentheticals. 2010-02-10 19:13:11 -05:00
Jeremy Ashkenas
4e7408dc25 self-compiler: handles try/catch/finally blocks 2010-02-10 18:33:03 -05:00
Jeremy Ashkenas
76dac9c09c got about as far as I think I can with operator nodes for now, without zach's optimizations 2010-02-10 00:05:56 -05:00
Jeremy Ashkenas
9339058fc3 raising the precedence level of delete, instanceof, and typeof 2010-02-09 22:20:04 -05:00
Jeremy Ashkenas
8e3e06a6e9 Self-compiler: now does while loops 2010-02-09 22:05:17 -05:00
Jeremy Ashkenas
fd80d784f4 Self-compiler can compile splats. 2010-02-09 21:44:34 -05:00
Jeremy Ashkenas
ae4f6309e8 Self-compiler: can now compile functions. 2010-02-09 21:31:07 -05:00
Jeremy Ashkenas
c466537a26 Self-compiler: array literals 2010-02-09 21:01:25 -05:00
Jeremy Ashkenas
001c915c21 Self-compiler: object literals. 2010-02-09 20:53:25 -05:00
Jeremy Ashkenas
91a7102f11 Self-compiler: array slice literals. 2010-02-09 19:30:28 -05:00
Jeremy Ashkenas
a451e90374 upgrading the test harness 2010-02-09 08:15:11 -05:00
Jeremy Ashkenas
c6a6788694 removing the constructor safety check -- it wasn't safe enough (Issue 36) 2010-02-09 07:59:48 -05:00
Jeremy Ashkenas
522df2a355 CoffeeScript-in-CoffeeScript can compile @property references. 2010-02-08 23:51:34 -05:00
Jeremy Ashkenas
863de88671 CoffeeScript-in-CoffeeScript can compile object[indexes] 2010-02-08 23:45:46 -05:00
Jeremy Ashkenas
cb57a1ca1f CoffeeScript-in-CoffeeScript can compile dotted accessors 2010-02-08 23:42:03 -05:00
Jeremy Ashkenas
32098e5a13 CoffeeScript-in-CoffeeScript is now able to compile subClass extends superClass 2010-02-08 23:16:29 -05:00
Jeremy Ashkenas
210d673ef0 CoffeeScript-in-CoffeeScript is compiling function calls 2010-02-08 22:55:56 -05:00
Jeremy Ashkenas
0b5b6113ee CoffeeScript-in-CoffeeScript just had it's first self-compiled snippet. 2010-02-08 22:22:59 -05:00
Jeremy Ashkenas
d5c98165ea removed an old now-impossible branch of execution from ReturnNode 2010-02-08 21:16:14 -05:00
Jeremy Ashkenas
b8d22bc572 finished up the CoffeeScript version of the Scope object 2010-02-08 21:10:48 -05:00
Jeremy Ashkenas
69808ba523 trying out a new organizational scheme, with a quickie 'inherits' helper function 2010-02-08 20:20:11 -05:00
Jeremy Ashkenas
aabfba9599 nodes.coffee is continuing to roll along -- maybe a tenth implemented 2010-02-08 19:49:39 -05:00
Jeremy Ashkenas
135620b14a bumping up the existential operator to the highest precedence level 2010-02-08 11:56:46 -05:00
Jeremy Ashkenas
78a4974de9 allowing numbers to be used as object keys 2010-02-08 11:48:43 -05:00
Jeremy Ashkenas
e2d75e6771 CoffeeScript 0.3.2, just in time for the Github feature 2010-02-08 10:58:49 -05:00
Jeremy Ashkenas
1aa966bba6 merging jeff olson's work 2010-02-07 15:45:05 -05:00
Jeremy Ashkenas
a347183f3d waypoint -- parser.coffee can parse basic functions 2010-02-07 15:37:05 -05:00
Jeremy Ashkenas
56499984ca waypoint -- it's beginning to parser 2010-02-07 15:15:36 -05:00
Jeremy Ashkenas
7ec0a8d653 merging node into master -- you can now pass the --narwhal flag to use narwhal instead. All tests are executing successfully against both Node.js and Narwhal/Rhino backends 2010-02-07 12:52:07 -05:00
Jeremy Ashkenas
293c2ffb5b spacing 2010-02-07 11:59:19 -05:00
Jeremy Ashkenas
5ec096e40d merging all narwhal integration into a single file, so we can merge the node branch without breaking narwhal compatibility 2010-02-07 11:54:01 -05:00
Jeremy Ashkenas
47bc1d5fda added a blocks example 2010-02-07 11:33:29 -05:00
Jeremy Ashkenas
6a59c5c9a9 merging master 2010-02-06 10:15:03 -05:00
Jeremy Ashkenas
decaea0f5f adding 'by' to array comprehensions 2010-02-06 10:12:57 -05:00
Jeremy Ashkenas
0a1873dc42 adding assign and return like they should have been 2010-02-05 22:39:39 -05:00
Jeremy Ashkenas
96eb7e2339 merging master 2010-02-05 22:02:11 -05:00
Jeremy Ashkenas
b795ae7fe1 removing arguments as a keyword -- we can detect its use at code-generation time. 2010-02-05 22:01:11 -05:00
Jeffery Olson
74b9545dc8 work on nodes.coffee and adding scope.coffee 2010-02-04 10:36:33 -08:00
Jeremy Ashkenas
0d56b89d12 Merge branch 'master' into node 2010-02-03 18:16:43 -05:00
Jeremy Ashkenas
dc7d0f1568 fixing assigning to @properties within an expression 2010-02-03 18:16:31 -05:00
Jeremy Ashkenas
9b7cfe87b5 remove parens 2010-02-02 20:44:25 -05:00
Jeremy Ashkenas
1587901367 remove parens 2010-02-02 20:44:10 -05:00
Jeremy Ashkenas
df670d47d2 merging in master 2010-02-02 20:38:44 -05:00
Jeremy Ashkenas
cb7a1033fa adding @property for this.property 2010-02-02 20:36:46 -05:00
Jeremy Ashkenas
df588bc9e8 it's puts in node, not print 2010-02-02 10:43:23 -05:00
Jeremy Ashkenas
9648ae2de1 merged in master 2010-02-02 10:41:19 -05:00
Jeremy Ashkenas
c5c841f2fc fixing bug with mixed dot and soak accessors 2010-02-02 10:39:44 -05:00
Jeffery Olson
c8ac7f0533 starting port of nodes.rb to coffee-script.. Node only, so far 2010-02-01 20:57:03 -08:00
Jeremy Ashkenas
681d4f44f4 simplifying order of operations a bit 2010-02-01 18:31:23 -05:00
Jeremy Ashkenas
db00cd6ed4 adding precedence to the Jison parser 2010-01-31 12:55:00 -05:00
Jeremy Ashkenas
3a748755df removing the peg grammar 2010-01-31 01:25:07 -05:00
Jeremy Ashkenas
a0572f161d the parser seems too big to compile 2010-01-30 23:17:36 -05:00
Jeremy Ashkenas
bad50c9aee the rewriter is done 2010-01-30 18:29:53 -05:00
Jeremy Ashkenas
c6457e010d getting there with the rewriter 2010-01-30 17:47:19 -05:00
Jeremy Ashkenas
557cdbba71 rewriter is halfway done, and working 2010-01-30 17:24:48 -05:00
Jeremy Ashkenas
84feab3492 first little piece of the rewriter 2010-01-30 17:02:05 -05:00
Jeremy Ashkenas
e755188878 mixing in sys again, for the tests 2010-01-30 16:14:13 -05:00
Jeremy Ashkenas
babeebcc1a more progress with the lexer, perhaps it's done 2010-01-30 15:56:40 -05:00
Jeremy Ashkenas
f19360c6b9 waypoint on lexing... parses basic strings, no indentation yet 2010-01-30 14:00:23 -05:00
Jeremy Ashkenas
eff2f4b520 a little further on with the lexer 2010-01-30 00:37:38 -05:00
Jeremy Ashkenas
854c796fd6 first little bit of the lexer 2010-01-30 00:08:15 -05:00
Jeremy Ashkenas
b0ecb39e9f made the path handling a little more robust 2010-01-29 23:41:18 -05:00
Jeremy Ashkenas
f5a37035cf node conversion finished, narwhal removed. 2010-01-29 23:30:54 -05:00
Jeremy Ashkenas
e08e99a403 implementing the Node REPL. 2010-01-29 22:53:44 -05:00
Jeremy Ashkenas
ba2d9df25f first draft of node.js REPL 2010-01-29 22:51:51 -05:00
Jeremy Ashkenas
83285fe170 updating resources section with rack-coffee 2010-01-27 16:38:49 -05:00
Jeremy Ashkenas
5b2ab36246 CoffeeScript 0.3.1, quick patch for 'instanceof' 2010-01-27 08:01:18 -05:00
Jeremy Ashkenas
2f3a94678f fixing instanceof, with a quick test 2010-01-27 07:55:40 -05:00
Jeremy Ashkenas
ca0a65ab95 updating documentation for 0.3 2010-01-26 23:23:59 -05:00
Jeremy Ashkenas
3524d618d8 adding unary plus, new version of Underscore, still passes tests. Rebuilt Narwhal 2010-01-26 22:14:18 -05:00
Jeremy Ashkenas
386d3dd307 complete implicit functions, I think these are done. 2010-01-26 21:15:56 -05:00
Jeremy Ashkenas
e998a81b63 removing block literals in favor of implicit calls 2010-01-26 21:05:26 -05:00
Jeremy Ashkenas
aa93d3c387 first draft of whitespace-sensitive method calls and indexes. 2010-01-26 20:59:52 -05:00
Jeremy Ashkenas
ab4a4a5580 make nested implicit indentation just a little bit smarter about outdents and stack levels 2010-01-26 14:49:33 -05:00
Jeremy Ashkenas
3775f682de updated textmate highlighter for new function literal syntax 2010-01-26 10:54:49 -05:00
Jeremy Ashkenas
a9f016e292 trying out new arrows for function literals -> is a function, => is a bound function 2010-01-26 10:52:05 -05:00
Jeremy Ashkenas
55df898112 adding bound functions to the list of implicit call activator tokens 2010-01-26 10:41:28 -05:00
Jeremy Ashkenas
fb7fd53bdf enabling passed functions to fire implicit method calls 2010-01-26 02:27:19 -05:00
Jeremy Ashkenas
29e4043f26 tests passing with smarter block syntax with optional parens 2010-01-26 02:15:08 -05:00
Jeremy Ashkenas
460b3f6d8e first draft of mandatory parentheses around function definition param lists -- all tests pass 2010-01-26 00:40:58 -05:00
Jeremy Ashkenas
63b44a2b03 odd and even were backwards 2010-01-25 22:44:36 -05:00
Jeremy Ashkenas
8efcaf6eec moved CoffeeScript.tmbundle to extras and rewrote the installation instructions in plain text 2010-01-25 22:22:39 -05:00
Jeremy Ashkenas
a732e578ea Merge branch 'master' of git://github.com/olsonjeffery/coffee-script 2010-01-25 22:03:12 -05:00
Jeremy Ashkenas
d6e206b420 adding line number info to unclosed parens, objects, arrays, and indents 2010-01-25 21:07:18 -05:00
Jeremy Ashkenas
91e703052c fixing chained single-line if-elses with a smarter rewriter. 2010-01-25 20:52:33 -05:00
Jeffery Olson
f393b1c897 adding vim syntax file in a new "extras" folder
- also added a VIM-SYNTAX-HOWTO.md readme file
2010-01-25 07:48:06 -08:00
Jeremy Ashkenas
2875de5e73 changed the docs for optional parens 2010-01-25 00:14:00 -05:00
Jeremy Ashkenas
8d63d269b8 making all postfix forms close out implicit calls, as in Ruby 2010-01-24 23:56:27 -05:00
Jeremy Ashkenas
a5d39efdd2 converted the tests to use optional parentheses -- lot's of little subtleties to work out 2010-01-24 23:40:45 -05:00
Jeremy Ashkenas
70e3a6ef2f first draft of optional parentheses, with a couple tests ... more to follow 2010-01-24 22:32:06 -05:00
Jeremy Ashkenas
4b267b401a another poignant example 2010-01-24 20:04:28 -05:00
Jeremy Ashkenas
e6f010b983 adding more examples to the computer_science folder, and fiddling with operator precedence 2010-01-24 18:59:29 -05:00
Jeremy Ashkenas
af53a04932 added test for lexical scope sharing through generated closure wrappers, something uncommonly used, but that was a regression 2010-01-24 13:39:27 -05:00
Jeremy Ashkenas
817e8deb27 adding soaked method calls, with caching 2010-01-24 12:52:15 -05:00
Jeremy Ashkenas
d728c3d669 added existence chains with '?.' -- soaks up attempts to access undefined properties, returning 'undefined' 2010-01-23 23:30:55 -05:00
Jeremy Ashkenas
9160500e84 removing 'this' rewriting in favor of correctly calling generated closures 2010-01-23 21:11:27 -05:00
Jeremy Ashkenas
c3ce2ea9b1 added automatic safety closure wrapper for functions declared within for loops. 2010-01-23 17:53:07 -05:00
Jeremy Ashkenas
5f94186b40 adding the compiled parser back into the repo (after all that) so that it can be used as the source for the narwhal package 2010-01-23 12:44:36 -05:00
Jeremy Ashkenas
791d874058 fixing comments as the last line of a block 2010-01-20 20:36:31 -05:00
Jeremy Ashkenas
a8ae37a428 fixing bug with multiple linebreaks in heredocs 2010-01-19 09:49:23 -05:00
Jeremy Ashkenas
b9c09bfa4e doc updates -- widened the code segments for the sake of the JavaScript 2010-01-17 18:33:31 -05:00
Jeremy Ashkenas
63c9b5c2f0 CoffeeScript 0.2.6 is on the books 2010-01-17 18:12:59 -05:00
Jeremy Ashkenas
80fbe02fda ignoring the top-down parser that doesn't work 2010-01-17 17:41:38 -05:00
Jeremy Ashkenas
e514a39dd2 added binary search example -- chapter 6 of beautiful code 2010-01-17 16:18:24 -05:00
Jeremy Ashkenas
4a32c58221 added bentley's chapter from beautiful code to the examples/tests -- quicksort runtime analysis 2010-01-17 15:58:44 -05:00
Jeremy Ashkenas
4609ad78c2 added the first chapter of beautiful code as a coffeescript example 2010-01-17 15:36:46 -05:00
Jeremy Ashkenas
2d90a751f7 edits for clarity 2010-01-17 14:55:06 -05:00
Jeremy Ashkenas
8647b54a61 rename compile_double_reference to compile_reference 2010-01-17 14:26:00 -05:00
Jeremy Ashkenas
8e1f3c0eca generating multiple calls to the same function should use compile_double_reference to ensure a single evaluation of the call itself. 2010-01-17 14:23:41 -05:00
Jeremy Ashkenas
c4d0903e6a fixing assignment-in-condition 2010-01-17 10:40:59 -05:00
Jeremy Ashkenas
e72ef1a61a reverting change 2010-01-17 10:28:04 -05:00
Jeremy Ashkenas
d7d9cb8d28 only let returns stop an expression from being closure-ified -- breaks and continues may be valid 2010-01-17 10:21:24 -05:00
Jeremy Ashkenas
f6c8e81ea6 the existential operator can now be used infix as well 2010-01-16 23:03:54 -05:00
Jeremy Ashkenas
52539ae7d2 abbreviating the existential operator 2010-01-16 22:26:34 -05:00
Jeremy Ashkenas
95b362499f added the conditional existence operator 2010-01-16 22:17:55 -05:00
Jeremy Ashkenas
0bc4da2b51 ensure that functions are only called once, when chaining comparators 2010-01-16 22:04:08 -05:00
Jeremy Ashkenas
9679fc0b52 removing redundant unary check 2010-01-16 16:49:03 -05:00
Jeremy Ashkenas
9cb0564972 added Python's chainable comparisons, like: 10 > 5 > 1 2010-01-16 16:37:49 -05:00
Jeremy Ashkenas
c6c0c7d059 simplification of function and prototype naming -- last_assign, immediate_assign, and proto_assign are gone, in favor of 'name' and 'proto' properties on CodeNodes 2010-01-16 15:44:07 -05:00
Jeremy Ashkenas
62e946b8ce purely empty functions at the top level should be wrapped in parens, so as not to cause a JS syntax error 2010-01-16 15:02:04 -05:00
Jeremy Ashkenas
6c782b7723 fixes for syntax highlighting assignments and regexes 2010-01-16 14:28:42 -05:00
Jeremy Ashkenas
9eff443032 arguments no longer is just a find-and-replace -- it'll fix the arguments variable at the top of scope if you use it in a function body 2010-01-16 12:52:26 -05:00
Jeremy Ashkenas
8957feedb4 expression closure wrappers are now safer -- they won't be generated if there's a statement_only inside 2010-01-16 12:10:31 -05:00
Jeremy Ashkenas
1cd7fa8ebe added children macro to Node, using it so that all nodes now have a 'children' method -- used for safe references to 'this' within closure wrappers 2010-01-16 11:24:10 -05:00
Jeremy Ashkenas
701cdb4c13 never try to push a statement_only 2010-01-15 19:47:16 -05:00
Jeremy Ashkenas
8dc5da9cc9 adding coffee-haml-filter to the resources section 2010-01-14 14:44:03 -05:00
Jeremy Ashkenas
001cc29deb slightly shorter generated code for ==> 2010-01-14 08:55:09 -05:00
Jeremy Ashkenas
e77e520607 CoffeeScript 0.2.5 is on the books 2010-01-13 23:24:45 -05:00
Jeremy Ashkenas
ed8a54995d with splats allowed in destructuring assignment 2010-01-13 22:25:58 -05:00
Jeremy Ashkenas
2d206e7b60 pulling out pushes into a pushnode 2010-01-13 21:33:46 -05:00
Jeremy Ashkenas
bb9fdd3015 while loops can now be used as expressions -- they return an array containing the computed result of each iteration. 2010-01-13 21:27:22 -05:00
Jeremy Ashkenas
1e7d638435 adding bound functions, with test 2010-01-13 20:59:57 -05:00
Jeremy Ashkenas
0ceca0778c adding when clauses with multiple values 2010-01-13 19:56:35 -05:00
Jeremy Ashkenas
abd9ab5c71 unified ParamSplatNode and ArgSplatNode into SplatNode 2010-01-12 23:49:47 -05:00
Jeremy Ashkenas
ea349a1a59 more safety type-checks in nodes.rb 2010-01-12 23:26:35 -05:00
Jeremy Ashkenas
f0d5db7e66 fixing heredocs to use the left-most indent as the indentation guide -- not just the first line of the heredoc 2010-01-12 23:06:12 -05:00
Jeremy Ashkenas
914ba1c244 removing commented-out bit 2010-01-12 18:01:12 -05:00
Jeremy Ashkenas
844ea33274 mistaken commit 2010-01-12 17:45:06 -05:00
Jeremy Ashkenas
87e04e9952 nicer syntax error messages for newlines and indentation 2010-01-12 17:44:37 -05:00
Jeremy Ashkenas
197914bcf7 nicer syntax error messages for newlines and indentation 2010-01-12 17:44:03 -05:00
Jeremy Ashkenas
8dfbd1a2a8 using Object.prototype.hasOwnProperty.call instead of obj.hasOwnProperty, with an alias, for Rhino and java objects 2010-01-12 17:35:37 -05:00
Jeremy Ashkenas
c19647ad33 adding and fixing test for empty strings 2010-01-12 08:52:44 -05:00
Jeremy Ashkenas
27f7ef09af allow leading newlines in coffee scripts 2010-01-12 08:49:39 -05:00
Jeremy Ashkenas
9a61bbf005 CoffeeScript 0.2.4, with pattern matching and heredocs 2010-01-12 00:09:23 -05:00
Jeremy Ashkenas
c8d505e85d heredoc docs 2010-01-12 00:00:38 -05:00
Jeremy Ashkenas
477c510345 adding heredocs, with tests 2010-01-11 23:53:50 -05:00
Jeremy Ashkenas
c3029faca7 documentation for pattern matching 2010-01-11 22:55:01 -05:00
Jeremy Ashkenas
186797a745 got compile_pattern_match about as small as its going to get 2010-01-11 22:16:23 -05:00
Jeremy Ashkenas
d54fa2f2a1 a whole chunk of compile_pattern_match was redundant, axed it -- along with the array/vs/object split 2010-01-11 22:12:18 -05:00
Jeremy Ashkenas
5e1e949bf6 a passing test for destructuring assignment (it needs a better name) 2010-01-11 22:04:25 -05:00
Jeremy Ashkenas
6c980d8adc first draft of destructuring assignment -- working out the wrinkles -- not sure if we want to do the full spec 2010-01-11 21:44:47 -05:00
Jeremy Ashkenas
2f63439bff swapping around the order of variable declaration in array comprehensions, so that it comes out in the proper order: __a, __b, __c 2010-01-11 09:16:08 -05:00
Jeremy Ashkenas
c7cb308b6d adding note about parens-around-ambiguous-function-defs to the docs 2010-01-11 09:09:06 -05:00
Jeremy Ashkenas
9cc7d6af27 little lexer tweak 2010-01-11 08:46:50 -05:00
237 changed files with 17372 additions and 4609 deletions

2
.gitignore vendored
View File

@@ -1,6 +1,6 @@
presentation
test.coffee
parser.output
lib/coffee_script/parser.rb
test/fixtures/underscore
examples/beautiful_code/parse.coffee
*.gem

57
Cakefile Normal file
View File

@@ -0,0 +1,57 @@
fs: require 'fs'
coffee: require 'coffee-script'
# Run a CoffeeScript through our node/coffee interpreter.
run: (args) ->
proc: process.createChildProcess 'bin/coffee', args
proc.addListener 'error', (err) -> if err then puts err
task 'install', 'install CoffeeScript into /usr/local', ->
exec([
'mkdir -p /usr/local/lib/coffee-script'
'cp -rf bin lib LICENSE README package.json src vendor /usr/local/lib/coffee-script'
'ln -sf /usr/local/lib/coffee-script/lib/bin/coffee /usr/local/bin/coffee'
'ln -sf /usr/local/lib/coffee-script/lib/bin/cake /usr/local/bin/cake'
].join(' && '))
task 'build', 'build the CoffeeScript language from source', ->
fs.readdir 'src', (err, files) ->
files: 'src/' + file for file in files when file.match(/\.coffee$/)
run ['-o', 'lib'].concat(files)
task 'build:parser', 'rebuild the Jison parser (run build first)', ->
parser: require('grammar').parser
js: parser.generate()
parser_path: 'lib/parser.js'
fs.writeFile parser_path, js
task 'build:ultraviolet', 'build and install the Ultraviolet syntax highlighter', ->
exec('plist2syntax extras/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage').addCallback ->
exec 'sudo mv coffeescript.yaml /usr/local/lib/ruby/gems/1.8/gems/ultraviolet-0.10.2/syntax/coffeescript.syntax'
task 'build:underscore', 'rebuild the Underscore.coffee documentation page', ->
exec 'uv -s coffeescript -t idle -h examples/underscore.coffee > documentation/underscore.html'
task 'test', 'run the CoffeeScript language test suite', ->
process.mixin require 'assert'
test_count: 0
start_time: new Date()
[original_ok, original_throws]: [ok, throws]
process.mixin {
ok: (args...) -> test_count += 1; original_ok(args...)
throws: (args...) -> test_count += 1; original_throws(args...)
}
process.addListener 'exit', ->
time: ((new Date() - start_time) / 1000).toFixed(2)
puts '\033[0;32mpassed ' + test_count + ' tests in ' + time + ' seconds\033[0m'
fs.readdir 'test', (err, files) ->
for file in files
fs.readFile 'test/' + file, (err, source) ->
js: coffee.compile source
process.compile js, file

14
README
View File

@@ -22,20 +22,20 @@
CoffeeScript is a little language that compiles into JavaScript.
Install the compiler:
gem install coffee-script
Install Node.js, and then the CoffeeScript compiler:
sudo bin/cake install
Compile a script:
coffee /path/to/script.coffee
For documentation, usage, and examples, see:
http://jashkenas.github.com/coffee-script/
http://coffeescript.org/
To suggest a feature or report a bug:
To suggest a feature, report a bug, or general discussion:
http://github.com/jashkenas/coffee-script/issues/
If you'd like to chat, drop by #coffeescript on Freenode.
The source repository:
git://github.com/jashkenas/coffee-script.git
To build CoffeeScript from source, install the "racc" gem and
run "rake build:parser". Then bin/coffee will work.

View File

@@ -2,47 +2,12 @@ require 'erb'
require 'fileutils'
require 'rake/testtask'
desc "Run all tests"
task :test do
$LOAD_PATH.unshift(File.expand_path('test'))
require 'redgreen' if Gem.available?('redgreen')
require 'test/unit'
Dir['test/*/**/test_*.rb'].each {|test| require test }
end
namespace :build do
desc "Recompile the Racc parser (pass -v and -g for verbose debugging)"
task :parser, :racc_args do |t, args|
sh "racc #{args[:racc_args]} -o lib/coffee_script/parser.rb lib/coffee_script/grammar.y"
end
desc "Compile the Narwhal interface for --interactive and --run"
task :narwhal do
sh "bin/coffee lib/coffee_script/narwhal/*.coffee -o lib/coffee_script/narwhal/lib/coffee-script"
sh "mv lib/coffee_script/narwhal/lib/coffee-script/coffee-script.js lib/coffee_script/narwhal/lib/coffee-script.js"
end
desc "Compile and install the Ultraviolet syntax highlighter"
task :ultraviolet do
sh "plist2syntax lib/coffee_script/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage"
sh "sudo mv coffeescript.yaml /usr/local/lib/ruby/gems/1.8/gems/ultraviolet-0.10.2/syntax/coffeescript.syntax"
end
desc "Rebuild the Underscore.coffee documentation page"
task :underscore do
sh "uv -s coffeescript -t idle -h examples/underscore.coffee > documentation/underscore.html"
end
end
desc "Build the documentation page"
task :doc do
source = 'documentation/index.html.erb'
child = fork { exec "bin/coffee documentation/coffee/*.coffee -o documentation/js -w" }
at_exit { Process.kill("INT", child) }
Signal.trap("INT") { exit }
# `uv -t idle -s coffeescript -h examples/underscore.coffee > documentation/underscore.html`
loop do
mtime = File.stat(source).mtime
if !@mtime || mtime > @mtime
@@ -53,20 +18,3 @@ task :doc do
sleep 1
end
end
namespace :gem do
desc 'Build and install the coffee-script gem'
task :install do
sh "gem build coffee-script.gemspec"
sh "sudo gem install #{Dir['*.gem'].join(' ')} --local --no-ri --no-rdoc"
end
desc 'Uninstall the coffee-script gem'
task :uninstall do
sh "sudo gem uninstall -x coffee-script"
end
end
task :default => :test

7
bin/cake Executable file
View File

@@ -0,0 +1,7 @@
#!/usr/bin/env node
process.mixin(require('sys'));
require.paths.unshift('./lib');
require('cake').run();

View File

@@ -1,5 +1,7 @@
#!/usr/bin/env ruby
#!/usr/bin/env node
require "#{File.dirname(__FILE__)}/../lib/coffee_script/command_line.rb"
process.mixin(require('sys'));
CoffeeScript::CommandLine.new
require.paths.unshift('./lib');
require('command_line').run();

View File

@@ -1,26 +0,0 @@
Gem::Specification.new do |s|
s.name = 'coffee-script'
s.version = '0.2.3' # Keep version in sync with coffee-script.rb
s.date = '2010-1-10'
s.homepage = "http://jashkenas.github.com/coffee-script/"
s.summary = "The CoffeeScript Compiler"
s.description = <<-EOS
CoffeeScript is a little language that compiles into JavaScript. Think
of it as JavaScript's less ostentatious kid brother -- the same genes,
roughly the same height, but a different sense of style. Apart from a
handful of bonus goodies, statements in CoffeeScript correspond
one-to-one with their equivalent in JavaScript, it's just another
way of saying it.
EOS
s.authors = ['Jeremy Ashkenas']
s.email = 'jashkenas@gmail.com'
s.rubyforge_project = 'coffee-script'
s.has_rdoc = false
s.require_paths = ['lib']
s.executables = ['coffee']
s.files = Dir['bin/*', 'examples/*', 'lib/**/*', 'coffee-script.gemspec', 'LICENSE', 'README', 'package.json']
end

View File

@@ -5,3 +5,5 @@ volume: 10 if band isnt spinal_tap
let_the_wild_rumpus_begin() unless answer is no
if car.speed < speed_limit then accelerate()
print "My name is " + @name

View File

@@ -1,4 +1,4 @@
backwards: =>
alert(arguments.reverse())
backwards: ->
alert arguments.reverse()
backwards("stairway", "to", "heaven")
backwards "stairway", "to", "heaven"

View File

@@ -4,4 +4,4 @@ lunch: eat(food) for food in ['toast', 'cheese', 'wine']
# Naive collision detection.
for roid in asteroids
for roid2 in asteroids when roid isnt roid2
roid.explode() if roid.overlaps(roid2)
roid.explode() if roid.overlaps roid2

View File

@@ -1,4 +0,0 @@
$('table.list').each() table =>
$('tr.account', table).each() row =>
row.show()
row.highlight()

View File

@@ -0,0 +1,5 @@
process.mixin require 'assert'
task 'test', 'run each of the unit tests', ->
for test in test_files
fs.readFile test, (err, code) -> eval coffee.compile code

View File

@@ -0,0 +1,5 @@
cholesterol: 127
healthy: 200 > cholesterol > 60

View File

@@ -1 +1,8 @@
solipsism: true if mind? and not world?
solipsism: true if mind? and not world?
speed ?= 140

View File

@@ -1,4 +1,4 @@
grade: student =>
grade: (student) ->
if student.excellent_work
"A+"
else if student.okay_stuff

View File

@@ -2,5 +2,5 @@ alert(
try
nonexistent / undefined
catch error
"Caught an error: " + error
"And the error is ... " + error
)

View File

@@ -0,0 +1,6 @@
Account: (customer, cart) ->
@customer: customer
@cart: cart
$('.shopping_cart').bind 'click', (event) =>
@customer.purchase @cart

View File

@@ -1,2 +1,2 @@
square: x => x * x
cube: x => square(x) * x
square: (x) -> x * x
cube: (x) -> square(x) * x

View File

@@ -0,0 +1,5 @@
html: '''
<strong>
cup of coffeescript
</strong>
'''

View File

@@ -0,0 +1,5 @@
weather_report: (location) ->
# Make an Ajax request to fetch the weather...
[location, 72, "Mostly Sunny"]
[city, temp, forecast]: weather_report "Berkeley, CA"

View File

@@ -0,0 +1,13 @@
futurists: {
sculptor: "Umberto Boccioni"
painter: "Vladimir Burliuk"
poet: {
name: "F.T. Marinetti"
address: [
"Via Roma 42R"
"Bellagio, Italy 22021"
]
}
}
{poet: {name: poet, address: [street, city]}}: futurists

View File

@@ -6,7 +6,7 @@ opposite_day: true
number: -42 if opposite_day
# Functions:
square: x => x * x
square: (x) -> x * x
# Arrays:
list: [1, 2, 3, 4, 5]
@@ -15,15 +15,15 @@ list: [1, 2, 3, 4, 5]
math: {
root: Math.sqrt
square: square
cube: x => x * square(x)
cube: (x) -> x * square x
}
# Splats:
race: winner, runners... =>
print(winner, runners)
race: (winner, runners...) ->
print winner, runners
# Existence:
alert("I knew it!") if elvis?
alert "I knew it!" if elvis?
# Array comprehensions:
cubed_list: math.cube(num) for num in list
cubed_list: math.cube num for num in list

View File

@@ -0,0 +1,4 @@
bait: 1000
and_switch: 0
[bait, and_switch]: [and_switch, bait]

View File

@@ -1,6 +1,6 @@
countdown: num for num in [10..1]
egg_delivery: =>
egg_delivery: ->
for i in [0...eggs.length] by 12
dozen_eggs: eggs[i...i+12]
deliver(new egg_carton(dozen))
deliver new egg_carton(dozen)

View File

@@ -1,5 +1,5 @@
num: 1
change_numbers: =>
change_numbers: ->
new_num: -1
num: 10
new_num: change_numbers()

View File

@@ -0,0 +1 @@
lottery.draw_winner()?.address?.zipcode

View File

@@ -1,6 +1,6 @@
gold: silver: the_field: "unknown"
medalists: first, second, rest... =>
award_medals: (first, second, rest...) ->
gold: first
silver: second
the_field: rest
@@ -18,8 +18,8 @@ contenders: [
"Usain Bolt"
]
medalists(contenders...)
award_medals contenders...
alert("Gold: " + gold)
alert("Silver: " + silver)
alert("The Field: " + the_field)
alert "Gold: " + gold
alert "Silver: " + silver
alert "The Field: " + the_field

View File

@@ -1,21 +1,30 @@
Animal: =>
Animal::move: meters =>
alert(this.name + " moved " + meters + "m.")
Animal: ->
Animal::move: (meters) ->
alert @name + " moved " + meters + "m."
Snake: (name) ->
@name: name
this
Snake: name => this.name: name
Snake extends Animal
Snake::move: =>
alert("Slithering...")
super(5)
Horse: name => this.name: name
Snake::move: ->
alert "Slithering..."
super 5
Horse: (name) ->
@name: name
this
Horse extends Animal
Horse::move: =>
alert("Galloping...")
super(45)
sam: new Snake("Sammy the Python")
tom: new Horse("Tommy the Palomino")
Horse::move: ->
alert "Galloping..."
super 45
sam: new Snake "Sammy the Python"
tom: new Horse "Tommy the Palomino"
sam.move()
tom.move()

View File

@@ -1,9 +1,10 @@
switch day
when "Tuesday" then eat_breakfast()
when "Wednesday" then go_to_the_park()
when "Saturday"
when "Mon" then go_to_work()
when "Tue" then go_to_the_park()
when "Thu" then go_ice_fishing()
when "Fri", "Sat"
if day is bingo_day
go_to_bingo()
go_dancing()
when "Sunday" then go_to_church()
when "Sun" then go_to_church()
else go_to_work()

View File

@@ -2,6 +2,6 @@ try
all_hell_breaks_loose()
cats_and_dogs_living_together()
catch error
print(error)
print error
finally
clean_up()

View File

@@ -1,5 +1,10 @@
while demand > supply
sell()
restock()
# Econ 101
if this.studying_economics
while supply > demand then buy()
while supply < demand then sell()
while supply > demand then buy()
# Nursery Rhyme
num: 6
lyrics: while num -= 1
num + " little monkeys, jumping on the bed.
One fell out and bumped his head."

View File

@@ -1,147 +0,0 @@
pre.amy .PolymorphicVariants {
color: #60B0FF;
font-style: italic;
}
pre.amy .KeywordDecorator {
color: #D0D0FF;
}
pre.amy .Punctuation {
color: #805080;
}
pre.amy .InheritedClass {
}
pre.amy .InvalidDepricated {
background-color: #CC66FF;
color: #200020;
}
pre.amy .LibraryVariable {
}
pre.amy .TokenReferenceOcamlyacc {
color: #3CB0D0;
}
pre.amy .Storage {
color: #B0FFF0;
}
pre.amy .KeywordOperator {
color: #A0A0FF;
}
pre.amy .CharacterConstant {
color: #666666;
}
pre.amy .line-numbers {
background-color: #800000;
color: #000000;
}
pre.amy .ClassName {
color: #70E080;
}
pre.amy .Int64Constant {
font-style: italic;
}
pre.amy .NonTerminalReferenceOcamlyacc {
color: #C0F0F0;
}
pre.amy .TokenDefinitionOcamlyacc {
color: #3080A0;
}
pre.amy .ClassType {
color: #70E0A0;
}
pre.amy .ControlKeyword {
color: #80A0FF;
}
pre.amy .LineNumberDirectives {
text-decoration: underline;
color: #C080C0;
}
pre.amy .FloatingPointConstant {
text-decoration: underline;
}
pre.amy .Int32Constant {
font-weight: bold;
}
pre.amy .TagName {
color: #009090;
}
pre.amy .ModuleTypeDefinitions {
text-decoration: underline;
color: #B000B0;
}
pre.amy .Integer {
color: #7090B0;
}
pre.amy .Camlp4TempParser {
}
pre.amy .InvalidIllegal {
font-weight: bold;
background-color: #FFFF00;
color: #400080;
}
pre.amy .LibraryConstant {
background-color: #200020;
}
pre.amy .ModuleDefinitions {
color: #B000B0;
}
pre.amy .Variants {
color: #60B0FF;
}
pre.amy .CompilerDirectives {
color: #C080C0;
}
pre.amy .FloatingPointInfixOperator {
text-decoration: underline;
}
pre.amy .BuiltInConstant1 {
}
pre.amy {
background-color: #200020;
color: #D0D0FF;
}
pre.amy .FunctionArgument {
color: #80B0B0;
}
pre.amy .FloatingPointPrefixOperator {
text-decoration: underline;
}
pre.amy .NativeintConstant {
font-weight: bold;
}
pre.amy .BuiltInConstant {
color: #707090;
}
pre.amy .BooleanConstant {
color: #8080A0;
}
pre.amy .LibraryClassType {
}
pre.amy .TagAttribute {
}
pre.amy .Keyword {
color: #A080FF;
}
pre.amy .UserDefinedConstant {
}
pre.amy .String {
color: #999999;
}
pre.amy .Camlp4Code {
background-color: #350060;
}
pre.amy .NonTerminalDefinitionOcamlyacc {
color: #90E0E0;
}
pre.amy .FunctionName {
color: #50A0A0;
}
pre.amy .SupportModules {
color: #A00050;
}
pre.amy .Variable {
color: #008080;
}
pre.amy .Comment {
background-color: #200020;
color: #404080;
font-style: italic;
}

View File

@@ -1,29 +1,29 @@
body {
font-size: 14px;
line-height: 20px;
background: #f3f3f9;
color: #191933;
font-family: Arial, Helvetica, sans-serif;
}
div.container {
width: 850px;
margin: 50px 0 50px 50px;
width: 950px;
margin: 100px 0 50px 50px;
}
p {
padding-left: 13px;
width: 625px;
}
a {
color: #000055;
}
h1, h2, h3, h4, h5, h6 {
padding-left: 13px;
margin-top: 40px;
}
br.clear {
height: 0;
clear: both;
}
ul {
padding-left: 20px;
}
b.header {
color: #000055;
display: block;
@@ -31,7 +31,7 @@ b.header {
font-size: 16px;
}
li {
margin-bottom: 7px;
margin-bottom: 10px;
}
table {
margin: 16px 0 0 13px; padding: 0;
@@ -43,7 +43,7 @@ table {
td {
padding: 9px 15px 9px 0;
}
code, pre, tt {
code, pre, tt, textarea {
font-family: Monaco, Consolas, "Lucida Console", monospace;
font-size: 12px;
line-height: 18px;
@@ -66,7 +66,7 @@ code, pre, tt {
div.code {
position: relative;
border: 1px solid #cacaca;
background: #fff;
background: #fafaff;
padding: 7px 0 10px 0;
-moz-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px;
-webkit-box-shadow: 0px 0px 7px #cacaca;
@@ -75,13 +75,133 @@ div.code {
position: absolute;
right: 8px; bottom: 8px;
}
div.code pre {
div.code pre, div.code textarea {
float: left;
width: 410px;
width: 450px;
background: #fafaff;
border-left: 1px dotted #559;
padding: 0 0 0 12px;
margin: 0;
}
div.code pre:first-child {
border-left: 0;
}
}
#fadeout {
z-index: 50;
position: fixed;
left: 0; top: 0; right: 0;
height: 100px;
background: -webkit-gradient(linear, left top, left bottom, from(rgba(255, 255, 255, 255)), to(rgba(255, 255, 255, 0)));
background: -moz-linear-gradient(top, rgba(255, 255, 255, 255), rgba(255, 255, 255, 0));
}
#flybar {
position: fixed;
z-index: 100;
height: 50px;
left: 40px; right: 40px; top: 25px;
background: #ddd;
padding-left: 235px;
background: -webkit-gradient(linear, left top, left bottom, from(#f5f5f5), to(#d0d0d0));
background: -moz-linear-gradient(top, #f5f5f5, #d0d0d0);
filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#F5F5F5', EndColorStr='#D0D0D0');
-webkit-border-radius: 20px; -moz-border-radius: 20px; border-radius: 20px;
-webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777;
}
#logo {
display: block;
width: 215px; height: 50px;
background: url('logo.png');
position: absolute;
top: 0px; left: 10px;
}
.navigation {
height: 50px;
font: bold 11px/50px Arial;
text-transform: uppercase;
position: relative;
float: left;
padding: 0 20px;
border: 1px solid #bbb;
border-top: 0; border-bottom: 0;
cursor: pointer;
}
.navigation.try {
border-left: 0;
}
.navigation:hover {
background: #d0d0d0;
background: -webkit-gradient(linear, left top, left bottom, from(#f0f0f0), to(#c0c0c0));
background: -moz-linear-gradient(top, #f0f0f0, #c0c0c0);
filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#F0F0F0', EndColorStr='#C0C0C0');
}
.navigation .contents {
display: none;
position: absolute;
background: #fff;
top: 50px; left: 0;
padding: 5px 0;
-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px;
-webkit-border-top-left-radius: 0; -moz-border-radius-topleft: 0;
-webkit-border-top-right-radius: 0; -moz-border-radius-topright: 0;
-webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777;
}
.navigation:hover .contents {
display: block;
}
.navigation .contents.repl_wrapper {
left: -162px;
width: 700px;
padding: 0;
}
.navigation .contents.repl_wrapper .code {
-webkit-box-shadow: none; -moz-box-shadow: none;
background: transparent;
border: 0;
}
.navigation .code button {
bottom: 10px;
}
.navigation .compile {
left: 240px; right: auto;
}
.navigation .contents a {
display: block;
width: 300px;
text-transform: none;
text-decoration: none;
font-weight: normal;
height: 12px;
line-height: 12px;
padding: 4px 10px;
}
.navigation .contents a:hover {
background: #f0f0f0;
}
.bookmark {
display: block;
width: 0; height: 0;
position: relative;
top: -90px;
}
#repl_source, #repl_results {
background: transparent;
}
#repl_source {
border: 0;
padding: 5px 7px;
margin-left: 5px;
min-height: 250px;
resize: none;
width: 295px;
}
#repl_results {
font-family: Monaco, Consolas, "Lucida Console", monospace;
text-transform: none;
font-weight: normal;
min-height: 260px;
width: 355px;
}

View File

@@ -28,6 +28,7 @@ pre.idle .LibraryConstant {
color: #A535AE;
}
pre.idle .FunctionArgument {
color: #0076ad;
}
pre.idle .BuiltInConstant {
color: #A535AE;

BIN
documentation/css/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

@@ -25,9 +25,66 @@
</head>
<body>
<div class="container">
<div id="fadeout"></div>
<h1><sub style="font-size: 100px;">&#9749;</sub> CoffeeScript</h1>
<div id="flybar">
<a id="logo" href="#top"> </a>
<div class="navigation">
<div class="button">
Table of Contents
</div>
<div class="contents">
<a href="#overview">Mini Overview</a>
<a href="#installation">Installation and Usage</a>
<a href="#language">Language Reference</a>
<a href="#whitespace">Significant Whitespace</a>
<a href="#functions">Functions and Invocation</a>
<a href="#assignment">Assignment</a>
<a href="#objects_and_arrays">Objects and Arrays</a>
<a href="#lexical_scope">Lexical Scoping and Variable Safety</a>
<a href="#conditionals">Conditionals, Ternaries, and Conditional Assignment</a>
<a href="#aliases">Aliases</a>
<a href="#splats">Splats...</a>
<a href="#arguments">Arguments are Arrays</a>
<a href="#while">While Loops</a>
<a href="#comprehensions">Comprehensions (Arrays, Objects, and Ranges)</a>
<a href="#slice_splice">Array Slicing and Splicing with Ranges</a>
<a href="#expressions">Everything is an Expression</a>
<a href="#existence">The Existential Operator</a>
<a href="#inheritance">Inheritance, and Calling Super from a Subclass</a>
<a href="#pattern_matching">Pattern Matching</a>
<a href="#fat_arrow">Function Binding</a>
<a href="#embedded">Embedded JavaScript</a>
<a href="#switch">Switch/When/Else</a>
<a href="#try">Try/Catch/Finally</a>
<a href="#comparisons">Chained Comparisons</a>
<a href="#strings">Multiline Strings and Heredocs</a>
<a href="#cake">Cake, and Cakefiles</a>
<a href="#resources">Resources</a>
<a href="#change_log">Change Log</a>
</div>
</div>
<div class="navigation try">
<div class="button">
Try CoffeeScript
</div>
<div class="contents repl_wrapper">
<div class="code">
<textarea id="repl_source">reverse: (string) ->
string.split('').reverse().join ''
alert reverse '!tpircseeffoC'</textarea>
<pre id="repl_results"></pre>
<button class="compile" onclick="javascript: repl_compile();">compile</button>
<button class="run" onclick="javascript: repl_run();">run</button>
<br class="clear" />
</div>
</div>
</div>
</div>
<div class="container">
<span class="bookmark" id="top"></span>
<p>
CoffeeScript is a little language that compiles into JavaScript. Think
@@ -39,8 +96,7 @@
<p>
<b>Disclaimer:</b>
CoffeeScript is just for fun and seriously alpha. I'm sure that there are still
plenty of holes in the walls and leaks in the roof. <i>There are no guarantees
CoffeeScript is just for fun. Until it reaches 1.0, <i>there are no guarantees
that the syntax won't change between versions.</i> That said,
it compiles into clean JavaScript (the good parts) that can use existing
JavaScript libraries seamlessly, and passes through
@@ -51,40 +107,13 @@
<p>
<b>Latest Version:</b>
<a href="http://gemcutter.org/gems/coffee-script">0.2.3</a>
<a href="http://github.com/jashkenas/coffee-script/tarball/0.5.0">0.5.0</a>
</p>
<h2>Table of Contents</h2>
<p>
<a href="#overview">Mini Overview</a><br />
<a href="#installation">Installation and Usage</a><br />
<a href="#whitespace">Significant Whitespace</a><br />
<a href="#functions">Functions and Invocation</a><br />
<a href="#assignment">Assignment</a><br />
<a href="#objects_and_arrays">Objects and Arrays</a><br />
<a href="#lexical_scope">Lexical Scoping and Variable Safety</a><br />
<a href="#conditionals">Conditionals, Ternaries, and Conditional Assignment</a><br />
<a href="#existence">The Existence Operator</a><br />
<a href="#aliases">Aliases</a><br />
<a href="#splats">Splats...</a><br />
<a href="#arguments">Arguments are Arrays</a><br />
<a href="#while">While Loops</a><br />
<a href="#comprehensions">Comprehensions (Arrays, Objects, and Ranges)</a><br />
<a href="#slice_splice">Array Slicing and Splicing with Ranges</a><br />
<a href="#expressions">Everything is an Expression</a><br />
<a href="#inheritance">Inheritance, and Calling Super from a Subclass</a><br />
<a href="#blocks">Blocks</a><br />
<a href="#embedded">Embedded JavaScript</a><br />
<a href="#switch">Switch/When/Else</a><br />
<a href="#try">Try/Catch/Finally</a><br />
<a href="#strings">Multiline Strings</a><br />
<a href="#resources">Resources</a><br />
<a href="#contributing">Contributing</a><br />
<a href="#change_log">Change Log</a><br />
</p>
<h2 id="overview">Mini Overview</h2>
<h2>
<span id="overview" class="bookmark"></span>
Mini Overview
</h2>
<p><i>CoffeeScript on the left, compiled JavaScript output on the right.</i></p>
@@ -95,7 +124,7 @@
<a href="documentation/underscore.html">Underscore.coffee</a>, a port
of the <a href="http://documentcloud.github.com/underscore/">Underscore.js</a>
library of helper functions. Underscore.coffee can pass the entire Underscore.js
test suite. The CoffeeScript version is faster than the original for a number
test suite. The CoffeeScript version is faster than the original for a number
of methods (in general, due to the speed of CoffeeScript's array comprehensions), and
after being minified and gzipped, is only 241 bytes larger than the original
JavaScript version.
@@ -103,21 +132,35 @@
<a href="http://github.com/jashkenas/coffee-script/tree/master/examples/">examples</a> folder.
</p>
<h2 id="installation">Installation and Usage</h2>
<h2>
<span id="installation" class="bookmark"></span>
Installation and Usage
</h2>
<p>
The CoffeeScript compiler is written in pure Ruby, and is available
as a Ruby Gem.
The CoffeeScript compiler is written in pure CoffeeScript, and is available
as a <a href="http://nodejs.org/">Node.js</a> utility. The core compiler however,
does not depend on Node, and can be run in other server-side-JavaScript environments,
or in the browser (see "Try CoffeeScript", above).
</p>
<p>
To install, first make sure you have a working version of
<a href="http://nodejs.org/">Node.js</a>, 0.1.30 or higher. Then clone the CoffeeScript
<a href="http://github.com/jashkenas/coffee-script">source repository</a>
from GitHub, or download the latest
release: <a href="http://github.com/jashkenas/coffee-script/tarball/0.5.0">0.5.0</a>.
To install the CoffeeScript compiler system-wide
under <tt>/usr/local</tt>, open the directory and run:
</p>
<pre>
gem install coffee-script</pre>
sudo bin/cake install</pre>
<p>
Installing the gem provides the <tt>coffee</tt> command, which can
This provides the <tt>coffee</tt> command, which can
be used to compile CoffeeScript <tt>.coffee</tt> files into JavaScript, as
well as debug them. In conjunction with
<a href="http://narwhaljs.org/">Narwhal</a>, the <tt>coffee</tt>
well as debug them. The <tt>coffee</tt>
command also provides direct evaluation and an interactive REPL.
When compiling to JavaScript, <tt>coffee</tt> writes the output
as <tt>.js</tt> files in the same directory by default, but output
@@ -128,15 +171,14 @@ gem install coffee-script</pre>
<tr>
<td width="25%"><code>-i, --interactive</code></td>
<td>
Launch an interactive CoffeeScript session.
Requires <a href="http://narwhaljs.org/">Narwhal</a>.
Launch an interactive CoffeeScript session to try short snippets.
</td>
</tr>
<tr>
<td><code>-r, --run</code></td>
<td>
Compile and execute scripts without saving the intermediate
JavaScript. Requires <a href="http://narwhaljs.org/">Narwhal</a>.
Compile and execute a given CoffeeScript without saving the intermediate
JavaScript.
</td>
</tr>
<tr>
@@ -162,7 +204,9 @@ gem install coffee-script</pre>
<tr>
<td><code>-l, --lint</code></td>
<td>
If the <tt>jsl</tt> (JavaScript Lint) command is installed, use it
If the <tt>jsl</tt>
(<a href="http://www.javascriptlint.com/">JavaScript Lint</a>)
command is installed, use it
to check the compilation of a CoffeeScript file. (Handy in
conjunction with <tt>--watch</tt>)
</td>
@@ -171,42 +215,36 @@ gem install coffee-script</pre>
<td><code>-e, --eval</code></td>
<td>
Compile and print a little snippet of CoffeeScript directly from the
command line (or from <b>stdin</b>). For example:<br /><tt>coffee -e "square: x => x * x"</tt>
</td>
</tr>
<tr>
<td><code>-t, --tokens</code></td>
<td>
Instead of parsing the CoffeeScript, just lex it, and print out the
token stream: <tt>[:IDENTIFIER, "square"], [":", ":"], [:PARAM, "x"]</tt> ...
</td>
</tr>
<tr>
<td><code>-v, --verbose</code></td>
<td>
As the JavaScript is being generated, print out every step of code
generation, including lexical scope and the node in the
AST.
command line. For example:<br /><tt>coffee -e "square: (x) -> x * x"</tt>
</td>
</tr>
<tr>
<td><code>-n, --no-wrap</code></td>
<td>
Compile the JavaScript without the top-level function safety wrapper.
(Used for CoffeeScript as a Narwhal module.)
(Used for CoffeeScript as a Node.js module.)
</td>
</tr>
<tr>
<td><code>-g, --globals</code></td>
<td><code>-t, --tokens</code></td>
<td>
Suppress all variable declarations at the top-level, effectively adding
those variables to the global scope. (Used by the REPL.)
Instead of parsing the CoffeeScript, just lex it, and print out the
token stream: <tt>[IDENTIFIER square] [ASSIGN :] [PARAM_START (]</tt> ...
</td>
</tr>
<tr>
<td><code>--install-bundle</code></td>
<td><code>-tr, --tree</code></td>
<td>
Install the TextMate bundle for CoffeeScript syntax highlighting.
Instead of compiling the CoffeeScript, just lex and parse it, and print
out the parse tree:
<pre>
Expressions
Assign
Value "square"
Code "x"
Op *
Value "x"
Value "x"</pre>
</td>
</tr>
</table>
@@ -221,7 +259,10 @@ coffee --interactive
coffee --watch --lint experimental.coffee
coffee --print app/scripts/*.coffee > concatenation.js</pre>
<h2>Language Reference</h2>
<h2>
<span id="language" class="bookmark"></span>
Language Reference
</h2>
<p>
<i>
@@ -233,7 +274,14 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
</i>
</p>
<p id="whitespace">
<p>
<i>
Many of the examples can be run (where it makes sense) by pressing the "run"
button towards the bottom right. You can also paste examples into
"Try CoffeeScript" in the toolbar, and play with them from there.
</i>
<p>
<span id="whitespace" class="bookmark"></span>
<b class="header">Significant Whitespace</b>
CoffeeScript uses Python-style significant whitespace: You don't need to
use semicolons <tt>;</tt> to terminate expressions, ending
@@ -246,43 +294,57 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
</p>
<p>
You can use newlines to break up your expression into smaller pieces,
as long as CoffeeScript can tell that the line hasn't finished
(similar to how Ruby handles it). For example,
if the line ends in an operator, dot, or keyword.
You don't need to use parentheses to invoke a function if you're passing
arguments:<br /><tt>print "coffee"</tt>
</p>
<p id="functions">
<p>
You can use newlines to break up your expression into smaller pieces,
as long as CoffeeScript can determine that the line hasn't finished yet,
because it ends with an operator or a dot.
</p>
<p>
<span id="functions" class="bookmark"></span>
<b class="header">Functions and Invocation</b>
Functions are defined by a list of parameters, an arrow, and the
function body. The empty function looks like this: <tt>=></tt>. All
functions in CoffeeScript are named, for the benefit of debug messages.
function body. The empty function looks like this: <tt>-></tt> All
functions in CoffeeScript are named by default, for easier debugging.
</p>
<%= code_for('functions', 'cube(5)') %>
<p>
If you'd like to assign a function literal to a variable, but not have
it be named, just wrap the function definition in parentheses:
<tt>((x) -> x * x)</tt>
</p>
<p id="assignment">
<p>
<span id="assignment" class="bookmark"></span>
<b class="header">Assignment</b>
Use a colon <tt>:</tt> to assign, as in
<a href="http://json.org">JSON</a>. Equal signs are only needed for
mathy things.
mathy things. While colons are preferred, the two may be used interchangeably,
even within object literals.
</p>
<%= code_for('assignment', 'greeting') %>
<p>
Declarations of new variables are pushed up to the top of the nearest
All declaration of new variables is pushed up to the top of the nearest
lexical scope, so that assignment may always be performed within expressions.
</p>
<p id="objects_and_arrays">
<p>
<span id="objects_and_arrays" class="bookmark"></span>
<b class="header">Objects and Arrays</b>
Object and Array literals look very similar to their JavaScript cousins.
When you spread out each assignment on a separate line, the commas are
optional. In this way, assigning object properties looks the same as
assigning local variables, and can be moved around freely. You can mix
assigning local variables, and can be moved around freely. Feel free to mix
and match the two styles.
</p>
<%= code_for('objects_and_arrays', 'song.join(",")') %>
<p id="lexical_scope">
<p>
<span id="lexical_scope" class="bookmark"></span>
<b class="header">Lexical Scoping and Variable Safety</b>
The CoffeeScript compiler takes care to make sure that all of your variables
are properly declared within lexical scope &mdash; you never need to write
@@ -302,12 +364,18 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
CoffeeScript output is wrapped in an anonymous function:
<tt>(function(){ ... })();</tt> This safety wrapper, combined with the
automatic generation of the <tt>var</tt> keyword, make it exceedingly difficult
to pollute the global namespace by accident. If you'd like to create
global variables, attach them as properties on <b>window</b>,
or on the <b>exports</b> object in CommonJS.
to pollute the global namespace by accident.
</p>
<p>
If you'd like to create top-level variables for other scripts to use,
attach them as properties on <b>window</b>, or on the <b>exports</b>
object in CommonJS. The <b>existential operator</b> (below), gives you a
reliable way to figure out where to add them, if you're targeting both
CommonJS and the browser: <tt>root: exports ? this</tt>
</p>
<p id="conditionals">
<p>
<span id="conditionals" class="bookmark"></span>
<b class="header">Conditionals, Ternaries, and Conditional Assignment</b>
<b>If/else</b> statements can be written without the use of parentheses and
curly brackets. As with functions and other block expressions,
@@ -326,17 +394,8 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
truthy variables.
</p>
<p id="existence">
<b class="header">The Existence Operator</b>
It's a little difficult to check for the existence of a variable in
JavaScript. <tt>if (variable) ...</tt> comes close, but fails for zero,
the empty string, and false. The existence operator <tt>?</tt> returns true unless
a variable is <b>null</b> or <b>undefined</b>, which makes it analogous
to Ruby's <tt>nil?</tt>
</p>
<%= code_for('existence') %>
<p id="aliases">
<p>
<span id="aliases" class="bookmark"></span>
<b class="header">Aliases</b>
Because the <tt>==</tt> operator frequently causes undesirable coercion,
is intransitive, and has a different meaning than in other languages,
@@ -364,18 +423,23 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
<p>
For single-line statements, <tt>unless</tt> can be used as the inverse of <tt>if</tt>.
</p>
<p>
As a shortcut for <tt>this.property</tt>, you can use <tt>@property</tt>.
</p>
<%= code_for('aliases') %>
<p id="splats">
<p>
<span id="splats" class="bookmark"></span>
<b class="header">Splats...</b>
The JavaScript <b>arguments object</b> is a useful way to work with
functions that accept variable numbers of arguments. CoffeeScript provides
splats <tt>...</tt>, both for function definition as well as invocation,
making variable arguments a little bit more palatable.
making variable numbers of arguments a little bit more palatable.
</p>
<%= code_for('splats', true) %>
<p id="arguments">
<p>
<span id="arguments" class="bookmark"></span>
<b class="header">Arguments are Arrays</b>
If you reference the <b>arguments object</b> directly, it will be converted
into a real Array, making all of the
@@ -384,11 +448,15 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
</p>
<%= code_for('arguments', true) %>
<p id="while">
<p>
<span id="while" class="bookmark"></span>
<b class="header">While Loops</b>
The only low-level loop that CoffeeScript provides is the while loop.
The only low-level loop that CoffeeScript provides is the <b>while</b> loop. The
main difference from JavaScript is that the <b>while</b> loop can be used
as an expression, returning an array containing the result of each iteration
through the loop.
</p>
<%= code_for('while') %>
<%= code_for('while', 'lyrics.join("\n")') %>
<p>
Other JavaScript loops, such as <b>for</b> loops and <b>do-while</b> loops
can be mimicked by variations on <b>while</b>, but the hope is that you
@@ -396,7 +464,8 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
<b>each</b> (<b>forEach</b>) style iterators, or...
</p>
<p id="comprehensions">
<p>
<span id="comprehensions" class="bookmark"></span>
<b class="header">Comprehensions (Arrays, Objects, and Ranges)</b>
For your looping needs, CoffeeScript provides array comprehensions
similar to Python's. They replace (and compile into) <b>for</b> loops, with
@@ -415,12 +484,13 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
<%= code_for('range_comprehensions', 'countdown') %>
<p>
Comprehensions can also be used to iterate over the keys and values in
an object. Use <tt>of</tt> to signal comprehension over the properties of
an object. Use <tt>of</tt> to signal comprehension over the properties of
an object instead of the values in an array.
</p>
<%= code_for('object_comprehensions', 'ages.join(", ")') %>
<p id="slice_splice">
<p>
<span id="slice_splice" class="bookmark"></span>
<b class="header">Array Slicing and Splicing with Ranges</b>
CoffeeScript borrows Ruby's
<a href="http://ruby-doc.org/core/classes/Range.html">range syntax</a>
@@ -436,7 +506,8 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
</p>
<%= code_for('splices', 'numbers') %>
<p id="expressions">
<p>
<span id="expressions" class="bookmark"></span>
<b class="header">Everything is an Expression (at least, as much as possible)</b>
You might have noticed how even though we don't add return statements
to CoffeeScript functions, they nonetheless return their final value.
@@ -468,8 +539,45 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
into a function call:
</p>
<%= code_for('expressions_try', true) %>
<p>
There are a handful of statements in JavaScript that can't be meaningfully
converted into expressions, namely <tt>break</tt>, <tt>continue</tt>,
and <tt>return</tt>. If you make use of them within a block of code,
CoffeeScript won't try to perform the conversion.
</p>
<p id="inheritance">
<p>
<span id="existence" class="bookmark"></span>
<b class="header">The Existential Operator</b>
It's a little difficult to check for the existence of a variable in
JavaScript. <tt>if (variable) ...</tt> comes close, but fails for zero,
the empty string, and false. CoffeeScript's existential operator <tt>?</tt> returns true unless
a variable is <b>null</b> or <b>undefined</b>, which makes it analogous
to Ruby's <tt>nil?</tt>
</p>
<p>
It can also be used for safer conditional assignment than <tt>||=</tt>
provides, for cases where you may be handling numbers or strings.
</p>
<%= code_for('existence', 'speed') %>
<p>
The accessor variant of the existential operator <tt>?.</tt> can be used to soak
up null references in a chain of properties. Use it instead
of the dot accessor <tt>.</tt> in cases where the base value may be <b>null</b>
or <b>undefined</b>. If all of the properties exist then you'll get the expected
result, if the chain is broken, <b>undefined</b> is returned instead of
the <b>TypeError</b> that would be raised otherwise.
</p>
<%= code_for('soaks') %>
<p>
Soaking up nulls is similar to Ruby's
<a href="http://andand.rubyforge.org/">andand gem</a>, and to the
<a href="http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29">safe navigation operator</a>
in Groovy.
</p>
<p>
<span id="inheritance" class="bookmark"></span>
<b class="header">Inheritance, and Calling Super from a Subclass</b>
JavaScript's prototypal inheritance has always been a bit of a
brain-bender, with a whole family tree of libraries that provide a cleaner
@@ -481,33 +589,62 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
be completely usable if it weren't for a couple of small exceptions:
it's awkward to call <b>super</b> (the prototype object's
implementation of the current function), and it's awkward to correctly
set the prototype chain.
set the prototype chain.
</p>
<p>
CoffeeScript provides <tt>extends</tt>
to help with prototype setup, <tt>::</tt> for quick access to an
object's prototype, and converts <tt>super()</tt> into a call against
object's prototype, and converts <tt>super()</tt> into a call against
the immediate ancestor's method of the same name.
</p>
<%= code_for('super', true) %>
<p id="blocks">
<b class="header">Blocks</b>
Many common looping functions (in Prototype, jQuery, and Underscore,
for example) take a single function as their final argument. To make
final functions easier to pass, CoffeeScript includes block syntax,
so you don't have to close the parentheses on the other side.
<p>
<span id="pattern_matching" class="bookmark"></span>
<b class="header">Pattern Matching (Destructuring Assignment)</b>
To make extracting values from complex arrays and objects more convenient,
CoffeeScript implements ECMAScript Harmony's proposed
<a href="http://wiki.ecmascript.org/doku.php?id=harmony:destructuring">destructuring assignment</a>
syntax. When you assign an array or object literal to a value, CoffeeScript
breaks up and matches both sides against each other, assigning the values
on the right to the variables on the left. In the simplest case, it can be
used for parallel assignment:
</p>
<%= code_for('blocks') %>
<%= code_for('parallel_assignment', 'bait') %>
<p>
But it's also helpful for dealing with functions that return multiple
values.
</p>
<%= code_for('multiple_return_values', 'forecast') %>
<p>
Pattern matching can be used with any depth of array and object nesting,
to help pull out deeply nested properties.
</p>
<%= code_for('object_extraction', 'poet + " — " + street') %>
<p id="embedded">
<p>
<span id="fat_arrow" class="bookmark"></span>
<b class="header">Function binding</b>
The fat arrow <tt>=></tt> can be used to both define a function, and to bind
it to the current value of <tt>this</tt>, right on the spot. This is helpful
when using callback-based libraries like Prototype or jQuery, for creating
iterator functions to pass to <tt>each</tt>, or event-handler functions
to use with <tt>bind</tt>. Functions created with the fat arrow are able to access
properties of the <tt>this</tt> where they're defined.
</p>
<%= code_for('fat_arrow') %>
<p>
<span id="embedded" class="bookmark"></span>
<b class="header">Embedded JavaScript</b>
If you ever need to interpolate literal JavaScript snippets, you can
use backticks to pass JavaScript straight through.
Hopefully, you'll never need to use it, but if you ever need to intersperse
snippets of JavaScript within your CoffeeScript, you can
use backticks to pass it straight through.
</p>
<%= code_for('embedded', 'hi()') %>
<p id="switch">
<p>
<span id="switch" class="bookmark"></span>
<b class="header">Switch/When/Else</b>
<b>Switch</b> statements in JavaScript are rather broken. You can only
do comparisons based on string equality, and need to remember to <b>break</b> at the end of
@@ -517,82 +654,152 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
in a returnable, assignable expression. The format is: <tt>switch</tt> condition,
<tt>when</tt> clauses, <tt>else</tt> the default case.
</p>
<p>
As in Ruby, <b>switch</b> statements in CoffeeScript can take multiple
values for each <b>when</b> clause. If any of the values match, the clause
runs.
</p>
<%= code_for('switch') %>
<p id="try">
<p>
<span id="try" class="bookmark"></span>
<b class="header">Try/Catch/Finally</b>
Try/catch statements are just about the same as JavaScript (although
they work as expressions).
</p>
<%= code_for('try') %>
<p id="strings">
<b class="header">Multiline Strings</b>
<p>
<span id="comparisons" class="bookmark"></span>
<b class="header">Chained Comparisons</b>
CoffeeScript borrows
<a href="http://docs.python.org/reference/expressions.html#notin">chained comparisons</a>
from Python &mdash; making it easy to test if a value falls within a
certain range.
</p>
<%= code_for('comparisons', 'healthy') %>
<p>
<span id="strings" class="bookmark"></span>
<b class="header">Multiline Strings and Heredocs</b>
Multiline strings are allowed in CoffeeScript.
</p>
<%= code_for('strings', 'moby_dick') %>
<p>
Heredocs can be used to hold formatted or indentation-sensitive text
(or, if you just don't feel like escaping quotes and apostrophes). The
indentation level that begins the heredoc is maintained throughout, so
you can keep it all aligned with the body of your code.
</p>
<%= code_for('heredocs') %>
<h2 id="resources">Resources</h2>
<h2>
<span id="cake" class="bookmark"></span>
Cake, and Cakefiles
</h2>
<p>
CoffeeScript includes a simple build system similar to Make and Rake. Naturally,
it's called Cake, and is used for the build and test tasks for the CoffeeScript
language itself. Tasks are defined in a file named <tt>Cakefile</tt>, and
can be invoked by running <tt>cake taskname</tt> from within the directory.
To print a list of all the tasks, just run <tt>cake</tt>.
</p>
<p>
Task definitions are written in CoffeeScript, so you can put arbitrary code
in your Cakefile. Define a task with a name, a long description, and the
function to invoke when the task is run. Here's a hypothetical task
that uses the Node.js API.
</p>
<%= code_for('cake_tasks') %>
<h2>
<span id="resources" class="bookmark"></span>
Resources
</h2>
<ul>
<li>
<a href="http://github.com/jashkenas/coffee-script/">Source Code</a><br />
After checking out the source, make sure to run <tt>rake build:parser</tt>
to generate an up-to-date version of the Racc parser.
Use <tt>bin/coffee</tt> to test your changes,
<tt>rake test</tt> to run the test suite,
and <tt>rake gem:install</tt> to
create and install a custom version of the gem.
Use <tt>bin/coffee</tt> to test your changes,<br />
<tt>bin/cake test</tt> to run the test suite,<br />
<tt>bin/cake build</tt> to rebuild the CoffeeScript compiler, and <br />
<tt>bin/cake build:parser</tt> to regenerate the Jison parser if you're
working on the grammar.
</li>
<li>
<a href="http://github.com/jashkenas/coffee-script/issues">Bugs, Feature Requests, and General Discussion</a>
<a href="http://github.com/jashkenas/coffee-script/issues">CoffeeScript Issues</a><br />
Bugs reports, feature requests, and general discussion all belong here.
</li>
<li>
<a href="http://github.com/jnicklas/bistro_car">BistroCar</a><br />
A Rails plugin by
<a href="http://github.com/jnicklas">Jonas Nicklas</a>
that includes CoffeeScript helpers,
bundling and minification.
If you'd like to chat, stop by <tt>#coffeescript</tt> on Freenode.
</li>
</ul>
<h2 id="contributing">Contributing</h2>
<h2>
<span id="change_log" class="bookmark"></span>
Change Log
</h2>
<p>
Here's a wish list of things that would be wonderful to have contributed:
<b class="header" style="margin-top: 20px;">0.5.0</b>
CoffeeScript 0.5.0 is a major release, While there are no language changes,
the Ruby compiler has been removed in favor of a self-hosting
compiler written in pure CoffeeScript.
</p>
<ul>
<li>
<a href="http://github.com/jashkenas/coffee-script/issues#issue/8">
A CoffeeScript version of the compiler.
</a>
</li>
<li>
Test cases for any syntax errors you encounter that you think CoffeeScript
should be able to compile properly.
</li>
<li>
A tutorial that introduces CoffeeScript from the ground up for folks
without knowledge of JavaScript.
</li>
<li>
Integration with Processing.js's JavaScript API (this would depend on
having a JavaScript version of the compiler).
</li>
<li>
A lot of the code generation in <tt>nodes.rb</tt> gets into messy
string manipulation. Techniques for cleaning this up across the board
would be appreciated.
</li>
</ul>
<p>
<b class="header" style="margin-top: 20px;">0.3.2</b>
<tt>@property</tt> is now a shorthand for <tt>this.property</tt>.<br />
Switched the default JavaScript engine from Narwhal to Node.js. Pass
the <tt>--narwhal</tt> flag if you'd like to continue using it.
</p>
<p>
<b class="header" style="margin-top: 20px;">0.3.0</b>
CoffeeScript 0.3 includes major syntax changes:
<br />
The function symbol was changed to
<tt>-></tt>, and the bound function symbol is now <tt>=></tt>.
<br />
Parameter lists in function definitions must now be wrapped in parentheses.
<br />
Added property soaking, with the <tt>?.</tt> operator.
<br />
Made parentheses optional, when invoking functions with arguments.
<br />
Removed the obsolete block literal syntax.
</p>
<p>
<b class="header" style="margin-top: 20px;">0.2.6</b>
Added Python-style chained comparisons, the conditional existence
operator <tt>?=</tt>, and some examples from <i>Beautiful Code</i>.
Bugfixes relating to statement-to-expression conversion, arguments-to-array
conversion, and the TextMate syntax highlighter.
</p>
<p>
<b class="header" style="margin-top: 20px;">0.2.5</b>
The conditions in switch statements can now take multiple values at once &mdash;
If any of them are true, the case will run. Added the long arrow <tt>==></tt>,
which defines and immediately binds a function to <tt>this</tt>. While loops can
now be used as expressions, in the same way that comprehensions can. Splats
can be used within pattern matches to soak up the rest of an array.
</p>
<p>
<b class="header" style="margin-top: 20px;">0.2.4</b>
Added ECMAScript Harmony style destructuring assignment, for dealing with
extracting values from nested arrays and objects. Added indentation-sensitive
heredocs for nicely formatted strings or chunks of code.
</p>
<h2 id="change_log">Change Log</h2>
<p>
<b class="header" style="margin-top: 20px;">0.2.3</b>
Axed the unsatisfactory <tt>ino</tt> keyword, replacing it with <tt>of</tt> for
object comprehensions. They now look like: <tt>for key, value of object</tt>.
object comprehensions. They now look like: <tt>for prop, value of object</tt>.
</p>
<p>
@@ -625,7 +832,7 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
<b class="header" style="margin-top: 20px;">0.2.0</b>
Major release. Significant whitespace. Better statement-to-expression
conversion. Splats. Splice literals. Object comprehensions. Blocks.
The existence operator. Many thanks to all the folks who posted issues,
The existential operator. Many thanks to all the folks who posted issues,
with special thanks to
<a href="http://github.com/kamatsu">Liam O'Connor-Davis</a> for whitespace
and expression help.
@@ -697,5 +904,32 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
</div>
<script type="text/javascript" src="lib/rewriter.js"></script>
<script type="text/javascript" src="lib/lexer.js"></script>
<script type="text/javascript" src="lib/parser.js"></script>
<script type="text/javascript" src="lib/scope.js"></script>
<script type="text/javascript" src="lib/nodes.js"></script>
<script type="text/javascript" src="lib/coffee-script.js"></script>
<script type="text/javascript">
window.repl_compile = function() {
var source = document.getElementById('repl_source').value;
window.compiled_js = '';
try {
window.compiled_js = CoffeeScript.compile(source, {no_wrap: true});
} catch(error) {
alert(error);
}
document.getElementById('repl_results').innerHTML = window.compiled_js;
}
window.repl_run = function() {
try {
eval(window.compiled_js);
} catch(error) {
alert(error);
}
}
</script>
</body>
</html>

View File

@@ -10,4 +10,5 @@
let_the_wild_rumpus_begin();
}
car.speed < speed_limit ? accelerate() : null;
print("My name is " + this.name);
})();

View File

@@ -1,7 +1,8 @@
(function(){
var backwards;
backwards = function backwards() {
return alert(Array.prototype.slice.call(arguments, 0).reverse());
arguments = Array.prototype.slice.call(arguments, 0);
return alert(arguments.reverse());
};
backwards("stairway", "to", "heaven");
})();

View File

@@ -1,21 +1,21 @@
(function(){
var __a, __b, __c, __d, __e, __f, __g, food, lunch, roid, roid2;
var _a, _b, _c, _d, _e, _f, _g, food, lunch, roid, roid2;
// Eat lunch.
lunch = (function() {
__c = []; __a = ['toast', 'cheese', 'wine'];
for (__b=0; __b<__a.length; __b++) {
food = __a[__b];
__c.push(eat(food));
_a = []; _b = ['toast', 'cheese', 'wine'];
for (_c = 0; _c < _b.length; _c++) {
food = _b[_c];
_a.push(eat(food));
}
return __c;
})();
return _a;
}).call(this);
// Naive collision detection.
__d = asteroids;
for (__e=0; __e<__d.length; __e++) {
roid = __d[__e];
__f = asteroids;
for (__g=0; __g<__f.length; __g++) {
roid2 = __f[__g];
_d = asteroids;
for (_e = 0; _e < _d.length; _e++) {
roid = _d[_e];
_f = asteroids;
for (_g = 0; _g < _f.length; _g++) {
roid2 = _f[_g];
if (roid !== roid2) {
if (roid.overlaps(roid2)) {
roid.explode();

View File

@@ -1,8 +0,0 @@
(function(){
$('table.list').each(function(table) {
return $('tr.account', table).each(function(row) {
row.show();
return row.highlight();
});
});
})();

View File

@@ -0,0 +1,14 @@
(function(){
process.mixin(require('assert'));
task('test', 'run each of the unit tests', function() {
var _a, _b, _c, test;
_a = []; _b = test_files;
for (_c = 0; _c < _b.length; _c++) {
test = _b[_c];
_a.push(fs.readFile(test, function(err, code) {
return eval(coffee.compile(code));
}));
}
return _a;
});
})();

View File

@@ -0,0 +1,5 @@
(function(){
var cholesterol, healthy;
cholesterol = 127;
healthy = (200 > cholesterol) && (cholesterol > 60);
})();

View File

@@ -1,5 +1,5 @@
(function(){
var date, mood;
var date, expensive, mood;
if (singing) {
mood = greatly_improved;
}

View File

@@ -1,6 +1,7 @@
(function(){
var solipsism;
var solipsism, speed;
if ((typeof mind !== "undefined" && mind !== null) && !(typeof world !== "undefined" && world !== null)) {
solipsism = true;
}
speed = (typeof speed !== "undefined" && speed !== null) ? speed : 140;
})();

View File

@@ -1,4 +1,4 @@
(function(){
var one, six, three, two;
six = (one = 1) + (two = 2) + (three = 3);
six = ((one = 1)) + ((two = 2)) + ((three = 3));
})();

View File

@@ -1,13 +1,12 @@
(function(){
var __a, __b, globals, name;
var _a, _b, globals, name;
var __hasProp = Object.prototype.hasOwnProperty;
// The first ten global properties.
globals = ((function() {
__b = []; __a = window;
for (name in __a) {
if (__a.hasOwnProperty(name)) {
__b.push(name);
}
}
return __b;
})()).slice(0, 10);
globals = (function() {
_a = []; _b = window;
for (name in _b) { if (__hasProp.call(_b, name)) {
_a.push(name);
}}
return _a;
}).call(this).slice(0, 10);
})();

View File

@@ -3,7 +3,7 @@
try {
return nonexistent / undefined;
} catch (error) {
return "Caught an error: " + error;
return "And the error is ... " + error;
}
})());
}).call(this));
})();

View File

@@ -0,0 +1,15 @@
(function(){
var Account;
Account = function Account(customer, cart) {
this.customer = customer;
this.cart = cart;
return $('.shopping_cart').bind('click', (function(__this) {
var __func = function(event) {
return this.customer.purchase(this.cart);
};
return (function() {
return __func.apply(__this, arguments);
});
})(this));
};
})();

View File

@@ -0,0 +1,4 @@
(function(){
var html;
html = "<strong>\n cup of coffeescript\n</strong>";
})();

View File

@@ -0,0 +1,11 @@
(function(){
var _a, city, forecast, temp, weather_report;
weather_report = function weather_report(location) {
// Make an Ajax request to fetch the weather...
return [location, 72, "Mostly Sunny"];
};
_a = weather_report("Berkeley, CA");
city = _a[0];
temp = _a[1];
forecast = _a[2];
})();

View File

@@ -1,18 +1,17 @@
(function(){
var __a, __b, age, ages, child, years_old;
var _a, _b, age, ages, child, years_old;
var __hasProp = Object.prototype.hasOwnProperty;
years_old = {
max: 10,
ida: 9,
tim: 11
};
ages = (function() {
__b = []; __a = years_old;
for (child in __a) {
age = __a[child];
if (__a.hasOwnProperty(child)) {
__b.push(child + " is " + age);
}
}
return __b;
})();
_a = []; _b = years_old;
for (child in _b) { if (__hasProp.call(_b, child)) {
age = _b[child];
_a.push(child + " is " + age);
}}
return _a;
}).call(this);
})();

View File

@@ -0,0 +1,17 @@
(function(){
var _a, _b, _c, city, futurists, poet, street;
futurists = {
sculptor: "Umberto Boccioni",
painter: "Vladimir Burliuk",
poet: {
name: "F.T. Marinetti",
address: ["Via Roma 42R", "Bellagio, Italy 22021"]
}
};
_a = futurists;
_b = _a.poet;
poet = _b.name;
_c = _b.address;
street = _c[0];
city = _c[1];
})();

View File

@@ -1,5 +1,5 @@
(function(){
var __a, __b, __c, cubed_list, list, math, num, number, opposite_day, race, square;
var _a, _b, _c, cubed_list, list, math, num, number, opposite_day, race, square;
// Assignment:
number = 42;
opposite_day = true;
@@ -33,11 +33,11 @@
}
// Array comprehensions:
cubed_list = (function() {
__c = []; __a = list;
for (__b=0; __b<__a.length; __b++) {
num = __a[__b];
__c.push(math.cube(num));
_a = []; _b = list;
for (_c = 0; _c < _b.length; _c++) {
num = _b[_c];
_a.push(math.cube(num));
}
return __c;
})();
return _a;
}).call(this);
})();

View File

@@ -0,0 +1,8 @@
(function(){
var _a, and_switch, bait;
bait = 1000;
and_switch = 0;
_a = [and_switch, bait];
bait = _a[0];
and_switch = _a[1];
})();

View File

@@ -1,21 +1,21 @@
(function(){
var __a, __b, __c, __d, __e, countdown, egg_delivery, num;
var _a, _b, _c, _d, _e, countdown, egg_delivery, num;
countdown = (function() {
__b = []; __d = 10; __e = 1;
for (__c=0, num=__d; (__d <= __e ? num <= __e : num >= __e); (__d <= __e ? num += 1 : num -= 1), __c++) {
__b.push(num);
_a = []; _d = 10; _e = 1;
for (_c=0, num=_d; (_d <= _e ? num <= _e : num >= _e); (_d <= _e ? num += 1 : num -= 1), _c++) {
_a.push(num);
}
return __b;
})();
return _a;
}).call(this);
egg_delivery = function egg_delivery() {
var __f, __g, __h, __i, __j, dozen_eggs, i;
__g = []; __i = 0; __j = eggs.length;
for (__h=0, i=__i; (__i <= __j ? i < __j : i > __j); (__i <= __j ? i += 12 : i -= 12), __h++) {
__g.push((function() {
var _f, _g, _h, _i, _j, dozen_eggs, i;
_f = []; _i = 0; _j = eggs.length;
for (_h=0, i=_i; (_i <= _j ? i < _j : i > _j); (_i <= _j ? i += 12 : i -= 12), _h++) {
_f.push((function() {
dozen_eggs = eggs.slice(i, i + 12);
return deliver(new egg_carton(dozen));
})());
}).call(this));
}
return __g;
return _f;
};
})();

View File

@@ -0,0 +1,4 @@
(function(){
var _a;
(_a = lottery.draw_winner()) == null ? undefined : _a.address == null ? undefined : _a.address.zipcode;
})();

View File

@@ -1,7 +1,7 @@
(function(){
var contenders, gold, medalists, silver, the_field;
gold = silver = the_field = "unknown";
medalists = function medalists(first, second) {
var award_medals, contenders, gold, silver, the_field;
gold = (silver = (the_field = "unknown"));
award_medals = function award_medals(first, second) {
var rest;
rest = Array.prototype.slice.call(arguments, 2);
gold = first;
@@ -9,7 +9,7 @@
return the_field = rest;
};
contenders = ["Michael Phelps", "Liu Xiang", "Yao Ming", "Allyson Felix", "Shawn Johnson", "Roman Sebrle", "Guo Jingjing", "Tyson Gay", "Asafa Powell", "Usain Bolt"];
medalists.apply(this, contenders);
award_medals.apply(this, contenders);
alert("Gold: " + gold);
alert("Silver: " + silver);
alert("The Field: " + the_field);

View File

@@ -1,33 +1,30 @@
(function(){
var Animal, Horse, Snake, __a, __b, sam, tom;
Animal = function Animal() {
};
var Animal, Horse, Snake, _a, _b, sam, tom;
Animal = function Animal() { };
Animal.prototype.move = function move(meters) {
return alert(this.name + " moved " + meters + "m.");
};
Snake = function Snake(name) {
var __a;
__a = this.name = name;
return Snake === this.constructor ? this : __a;
this.name = name;
return this;
};
__a = function(){};
__a.prototype = Animal.prototype;
_a = function(){};
_a.prototype = Animal.prototype;
Snake.__superClass__ = Animal.prototype;
Snake.prototype = new __a();
Snake.prototype = new _a();
Snake.prototype.constructor = Snake;
Snake.prototype.move = function move() {
alert("Slithering...");
return Snake.__superClass__.move.call(this, 5);
};
Horse = function Horse(name) {
var __b;
__b = this.name = name;
return Horse === this.constructor ? this : __b;
this.name = name;
return this;
};
__b = function(){};
__b.prototype = Animal.prototype;
_b = function(){};
_b.prototype = Animal.prototype;
Horse.__superClass__ = Animal.prototype;
Horse.prototype = new __b();
Horse.prototype = new _b();
Horse.prototype.constructor = Horse;
Horse.prototype.move = function move() {
alert("Galloping...");

View File

@@ -1,14 +1,16 @@
(function(){
if (day === "Tuesday") {
eat_breakfast();
} else if (day === "Wednesday") {
if (day === "Mon") {
go_to_work();
} else if (day === "Tue") {
go_to_the_park();
} else if (day === "Saturday") {
} else if (day === "Thu") {
go_ice_fishing();
} else if (day === "Fri" || day === "Sat") {
if (day === bingo_day) {
go_to_bingo();
go_dancing();
}
} else if (day === "Sunday") {
} else if (day === "Sun") {
go_to_church();
} else {
go_to_work();

View File

@@ -1,9 +1,22 @@
(function(){
while (demand > supply) {
sell();
restock();
}
while (supply > demand) {
buy();
var _a, lyrics, num;
// Econ 101
if (this.studying_economics) {
while (supply > demand) {
buy();
}
while (supply < demand) {
sell();
}
}
// Nursery Rhyme
num = 6;
lyrics = (function() {
_a = [];
while (num -= 1) {
_a.push(num + " little monkeys, jumping on the bed. \
One fell out and bumped his head.");
}
return _a;
}).call(this);
})();

View File

@@ -1,77 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Quickie CoffeeScript Speed Tests</title>
<script type="text/javascript" src="http://www.broofa.com/Tools/JSLitmus/JSLitmus.js"></script>
</head>
<body>
<h1>Quickie CoffeeScript Speed Tests</h1>
<script type="text/javascript">
var num = 1000;
var arr = [];
while (num--) arr.push(num);
JSLitmus.test('current comprehensions', function() {
__a = arr;
__c = [];
for (__b in __a) {
if (__a.hasOwnProperty(__b)) {
num = __a[__b];
__d = num;
__c.push(num);
}
}
});
JSLitmus.test('raw for loop (best we can do)', function() {
__a = arr;
__c = new Array(__a.length);
for (__b=0; __b < __a.length; __b++) {
__c[__b] = __a[__b];
}
});
JSLitmus.test('current without hasOwnProperty check', function() {
__a = arr;
__c = [];
for (__b in __a) {
num = __a[__b];
__d = num;
__c.push(num);
}
});
JSLitmus.test('raw for..in loop', function() {
__a = arr;
__c = new Array(__a.length);
for (__b in __a) {
__c[__b] = __a[__b];
}
});
JSLitmus.test('weepy\'s comprehensions', function() {
__c = []; __a = arr;
__d = function(num, __b) {
__c.push(num);
};
if (__a instanceof Array) {
for (__b=0; __b<__a.length; __b++) __d(__a[__b], __b);
} else {
for (__b in __a) { if (__a.hasOwnProperty(__b)) __d(__a[__b], __b); }
}
});
JSLitmus.test('CoffeeScript 0.2.2 comprehensions', function() {
__c = []; __a = arr;
for (__b=0; __b<__a.length; __b++) {
num = __a[__b];
__c.push(num);
}
});
</script>
</body>
</html>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,16 @@
# Beautiful Code, Chapter 6.
# The implementation of binary search that is tested.
# Return the index of an element in a sorted list. (or -1, if not present)
index: (list, target) ->
[low, high]: [0, list.length]
while low < high
mid: (low + high) >> 1
val: list[mid]
return mid if val is target
if val < target then low: mid + 1 else high: mid
return -1
puts 2 is index([10, 20, 30, 40, 50], 30)
puts 4 is index([-97, 35, 67, 88, 1200], 1200)
puts 0 is index([0, 45, 70], 0)

View File

@@ -0,0 +1,13 @@
# Beautiful Code, Chapter 3.
# Produces the expected runtime of Quicksort, for every integer from 1 to N.
runtime: (N) ->
[sum, t]: [0, 0]
for n in [1..N]
sum += 2 * t
t: n - 1 + sum / n
t
puts runtime(3) is 2.6666666666666665
puts runtime(5) is 7.4
puts runtime(8) is 16.92142857142857

View File

@@ -0,0 +1,34 @@
# Beautiful Code, Chapter 1.
# Implements a regular expression matcher that supports character matches,
# '.', '^', '$', and '*'.
# Search for the regexp anywhere in the text.
match: (regexp, text) ->
return match_here(regexp.slice(1), text) if regexp[0] is '^'
while text
return true if match_here(regexp, text)
text: text.slice(1)
false
# Search for the regexp at the beginning of the text.
match_here: (regexp, text) ->
[cur, next]: [regexp[0], regexp[1]]
if regexp.length is 0 then return true
if next is '*' then return match_star(cur, regexp.slice(2), text)
if cur is '$' and not next then return text.length is 0
if text and (cur is '.' or cur is text[0]) then return match_here(regexp.slice(1), text.slice(1))
false
# Search for a kleene star match at the beginning of the text.
match_star: (c, regexp, text) ->
while true
return true if match_here(regexp, text)
return false unless text and (text[0] is c or c is '.')
text: text.slice(1)
puts match("ex", "some text")
puts match("s..t", "spit")
puts match("^..t", "buttercup")
puts match("i..$", "cherries")
puts match("o*m", "vrooooommm!")
puts match("^hel*o$", "hellllllo")

57
examples/blocks.coffee Normal file
View File

@@ -0,0 +1,57 @@
# After wycats' http://yehudakatz.com/2010/02/07/the-building-blocks-of-ruby/
# Sinatra.
get '/hello', ->
'Hello World'
# Append.
append: (location, data) ->
path: new Pathname location
throw "Location does not exist" unless path.exists()
File.open path, 'a', (file) ->
file.puts YAML.dump data
data
# Rubinius' File.open implementation.
File.open: (path, mode, block) ->
io: new File path, mode
return io unless block
try
block io
finally
try
io.close() unless io.closed()
catch error
# nothing, just swallow them.
# Write.
write: (location, data) ->
path = new Pathname location
raise "Location does not exist" unless path.exists()
File.open path, 'w', (file) ->
return false if Digest.MD5.hexdigest(file.read()) is data.hash()
file.puts YAML.dump data
true
# Rails' respond_to.
index: ->
people: Person.find 'all'
respond_to (format) ->
format.html()
format.xml -> render { xml: people.xml() }
# Synchronization.
synchronize: (block) ->
lock()
try block() finally unlock()

View File

@@ -1,14 +1,14 @@
# Functions:
square: x => x * x
square: (x) -> x * x
sum: x, y => x + y
sum: (x, y) -> x + y
odd: x => x % 2 is 0
odd: (x) -> x % 2 isnt 0
even: x => x % 2 isnt 0
even: (x) -> x % 2 is 0
run_loop: =>
fire_events(e => e.stopPropagation())
run_loop: ->
fire_events((e) -> e.stopPropagation())
listen()
wait()
@@ -22,14 +22,14 @@ spaced_out_multiline_object: {
three: new Idea()
inner_obj: {
freedom: => _.freedom()
freedom: -> _.freedom()
}
}
# Arrays:
stooges: [{moe: 45}, {curly: 43}, {larry: 46}]
exponents: [(x => x), (x => x * x), (x => x * x * x)]
exponents: [(x) -> x, (x) -> x * x, (x) -> x * x * x]
empty: []
@@ -54,7 +54,7 @@ decoration: medal_of_honor if war_hero
go_to_sleep() unless coffee
# Returning early:
race: =>
race: ->
run()
walk()
crawl()
@@ -103,7 +103,7 @@ while true
# Lexical scoping.
v_1: 5
change_a_and_set_b: =>
change_a_and_set_b: ->
v_1: 10
v_2: 15
v_2: 20
@@ -128,7 +128,7 @@ activity: switch day
else go_to_work()
# Semicolons can optionally be used instead of newlines.
wednesday: => eat_breakfast(); go_to_work(); eat_dinner()
wednesday: -> eat_breakfast(); go_to_work(); eat_dinner()
# Array slice literals.
zero_to_nine: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@@ -140,19 +140,19 @@ sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
aliquam erat volutpat. Ut wisi enim ad."
# Inheritance and calling super.
Animal: =>
Animal::move: meters =>
Animal: ->
Animal::move: (meters) ->
alert(this.name + " moved " + meters + "m.")
Snake: name => this.name: name
Snake: (name) -> this.name: name
Snake extends Animal
Snake::move: =>
Snake::move: ->
alert('Slithering...')
super(5)
Horse: name => this.name: name
Horse: (name) -> this.name: name
Horse extends Animal
Horse::move: =>
Horse::move: ->
alert('Galloping...')
super(45)

View File

@@ -0,0 +1,4 @@
Ported from Nicholas Zakas' collection of computer science fundamentals, written
in JavaScript. Originals available here:
http://github.com/nzakas/computer-science-in-javascript

View File

@@ -0,0 +1,25 @@
# Uses a binary search algorithm to locate a value in the specified array.
binary_search: (items, value) ->
start: 0
stop: items.length - 1
pivot: Math.floor((start + stop) / 2)
while items[pivot] isnt value and start < stop
# Adjust the search area.
stop: pivot - 1 if value < items[pivot]
start: pivot + 1 if value > items[pivot]
# Recalculate the pivot.
pivot: Math.floor((stop + start) / 2)
# Make sure we've found the correct value.
if items[pivot] is value then pivot else -1
# Test the function.
puts(2 is binary_search([10, 20, 30, 40, 50], 30))
puts(4 is binary_search([-97, 35, 67, 88, 1200], 1200))
puts(0 is binary_search([0, 45, 70], 0))
puts(-1 is binary_search([0, 45, 70], 10))

View File

@@ -0,0 +1,11 @@
# A bubble sort implementation, sorting the given array in-place.
bubble_sort: (list) ->
for i in [0...list.length]
for j in [0...list.length - i]
[list[j], list[j+1]]: [list[j+1], list[j]] if list[j] > list[j+1]
list
# Test the function.
puts(bubble_sort([3, 2, 1]).join(' ') is '1 2 3')
puts(bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9')

View File

@@ -0,0 +1,106 @@
# "Classic" linked list implementation that doesn't keep track of its size.
LinkedList: ->
this._head: null # Pointer to the first item in the list.
# Appends some data to the end of the list. This method traverses the existing
# list and places the value at the end in a new node.
LinkedList::add: (data) ->
# Create a new node object to wrap the data.
node: {data: data, next: null}
current: this._head ||= node
if this._head isnt node
current: current.next while current.next
current.next: node
this
# Retrieves the data at the given position in the list.
LinkedList::item: (index) ->
# Check for out-of-bounds values.
return null if index < 0
current: this._head or null
i: -1
# Advance through the list.
current: current.next while current and index > (i += 1)
# Return null if we've reached the end.
current and current.data
# Remove the item from the given location in the list.
LinkedList::remove: (index) ->
# Check for out-of-bounds values.
return null if index < 0
current: this._head or null
i: -1
# Special case: removing the first item.
if index is 0
this._head: current.next
else
# Find the right location.
[previous, current]: [current, current.next] while index > (i += 1)
# Skip over the item to remove.
previous.next: current.next
# Return the value.
current and current.data
# Calculate the number of items in the list.
LinkedList::size: ->
current: this._head
count: 0
while current
count += 1
current: current.next
count
# Convert the list into an array.
LinkedList::toArray: ->
result: []
current: this._head
while current
result.push(current.data)
current: current.next
result
# The string representation of the linked list.
LinkedList::toString: -> this.toArray().toString()
# Tests.
list: new LinkedList()
list.add("Hi")
puts(list.size() is 1)
puts(list.item(0) is "Hi")
puts(list.item(1) is null)
list: new LinkedList()
list.add("zero").add("one").add("two")
puts(list.size() is 3)
puts(list.item(2) is "two")
puts(list.remove(1) is "one")
puts(list.item(0) is "zero")
puts(list.item(1) is "two")
puts(list.size() is 2)
puts(list.item(-10) is null)

View File

@@ -0,0 +1,36 @@
# Use the Luhn algorithm to validate a numeric identifier, such as credit card
# numbers, national insurance numbers, etc.
# See: http://en.wikipedia.org/wiki/Luhn_algorithm
is_valid_identifier: (identifier) ->
sum: 0
alt: false
for i in [(identifier.length - 1)..0]
# Get the next digit.
num: parseInt(identifier.charAt(i), 10)
# If it's not a valid number, abort.
return false if isNaN(num)
# If it's an alternate number...
if alt
num *= 2
num: (num % 10) + 1 if num > 9
# Flip the alternate bit.
alt: !alt
# Add to the rest of the sum.
sum += num
# Determine if it's valid.
sum % 10 is 0
# Tests.
puts(is_valid_identifier("49927398716") is true)
puts(is_valid_identifier("4408041234567893") is true)
puts(is_valid_identifier("4408041234567890") is false)

View File

@@ -0,0 +1,19 @@
# Sorts an array in ascending natural order using merge sort.
merge_sort: (list) ->
return list if list.length is 1
result: []
pivot: Math.floor(list.length / 2)
left: merge_sort(list.slice(0, pivot))
right: merge_sort(list.slice(pivot))
while left.length and right.length
result.push(if left[0] < right[0] then left.shift() else right.shift())
result.concat(left).concat(right)
# Test the function.
puts(merge_sort([3, 2, 1]).join(' ') is '1 2 3')
puts(merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9')

View File

@@ -0,0 +1,23 @@
# An in-place selection sort.
selection_sort: (list) ->
len: list.length
# For each item in the list.
for i in [0...len]
# Set the minimum to this position.
min: i
# Check the rest of the array to see if anything is smaller.
(min: j if list[j] < list[min]) for j in [(i+1)...len]
# Swap if a smaller item has been found.
[list[i], list[min]]: [list[min], list[i]] if i isnt min
# The list is now sorted.
list
# Test the function.
puts(selection_sort([3, 2, 1]).join(' ') is '1 2 3')
puts(selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9')

View File

@@ -1,72 +0,0 @@
# Document Model
dc.model.Document: dc.Model.extend({
constructor: attributes => this.base(attributes)
# For display, show either the highlighted search results, or the summary,
# if no highlights are available.
# The import process will take care of this in the future, but the inline
# version of the summary has all runs of whitespace squeezed out.
displaySummary: =>
text: this.get('highlight') or this.get('summary') or ''
text and text.replace(/\s+/g, ' ')
# Return a list of the document's metadata. Think about caching this on the
# document by binding to Metadata, instead of on-the-fly.
metadata: =>
docId: this.id
_.select(Metadata.models(), (meta =>
_.any(meta.get('instances'), instance =>
instance.document_id is docId)))
bookmark: pageNumber =>
bookmark: new dc.model.Bookmark({title: this.get('title'), page_number: pageNumber, document_id: this.id})
Bookmarks.create(bookmark)
# Inspect.
toString: => 'Document ' + this.id + ' "' + this.get('title') + '"'
})
# Document Set
dc.model.DocumentSet: dc.model.RESTfulSet.extend({
resource: 'documents'
SELECTION_CHANGED: 'documents:selection_changed'
constructor: options =>
this.base(options)
_.bindAll(this, 'downloadSelectedViewers', 'downloadSelectedPDF', 'downloadSelectedFullText')
selected: => _.select(this.models(), m => m.get('selected'))
selectedIds: => _.pluck(this.selected(), 'id')
countSelected: => this.selected().length
downloadSelectedViewers: =>
dc.app.download('/download/' + this.selectedIds().join('/') + '/document_viewer.zip')
downloadSelectedPDF: =>
if this.countSelected() <= 1 then return window.open(this.selected()[0].get('pdf_url'))
dc.app.download('/download/' + this.selectedIds().join('/') + '/document_pdfs.zip')
downloadSelectedFullText: =>
if this.countSelected() <= 1 then return window.open(this.selected()[0].get('full_text_url'))
dc.app.download('/download/' + this.selectedIds().join('/') + '/document_text.zip')
# We override "_onModelEvent" to fire selection changed events when documents
# change their selected state.
_onModelEvent: e, model =>
this.base(e, model)
fire: e == dc.Model.CHANGED and model.hasChanged('selected')
if fire then _.defer(_(this.fire).bind(this, this.SELECTION_CHANGED, this))
})
# The main set of Documents, used by the search tab.
window.Documents: new dc.model.DocumentSet()
# The set of documents that is used to look at a particular label.
dc.app.LabeledDocuments: new dc.model.DocumentSet()

View File

@@ -2,7 +2,7 @@
# ['toast', 'cheese', 'wine'].each { |food| print food.capitalize }
['toast', 'wine', 'cheese'].each(food => print(food.capitalize()))
['toast', 'wine', 'cheese'].each (food) -> print(food.capitalize())
@@ -14,10 +14,43 @@
# end
LotteryTicket: {
get_picks: => this.picks
set_picks: nums => this.picks: nums
get_purchase: => this.purchase
set_purchase: amount => this.purchase: amount
get_picks: -> this.picks
set_picks: (nums) -> this.picks: nums
get_purchase: -> this.purchase
set_purchase: (amount) -> this.purchase: amount
}
# class << LotteryDraw
# def play
# result = LotteryTicket.new_random
# winners = {}
# @@tickets.each do |buyer, ticket_list|
# ticket_list.each do |ticket|
# score = ticket.score( result )
# next if score.zero?
# winners[buyer] ||= []
# winners[buyer] << [ ticket, score ]
# end
# end
# @@tickets.clear
# winners
# end
# end
LotteryDraw: {
play: ->
result: LotteryTicket.new_random()
winners: {}
this.tickets.each (buyer, ticket_list) ->
ticket_list.each (ticket) ->
score: ticket.score(result)
return if score is 0
winners[buyer] ||= []
winners[buyer].push([ticket, score])
this.tickets: {}
winners
}
@@ -32,8 +65,8 @@ LotteryTicket: {
# end
WishScanner: {
scan_for_a_wish: =>
wish: this.read().detect(thought => thought.index('wish: ') is 0)
scan_for_a_wish: ->
wish: this.read().detect((thought) -> thought.index('wish: ') is 0)
wish.replace('wish: ', '')
}
@@ -78,7 +111,7 @@ WishScanner: {
Creature : {
# This method applies a hit taken during a fight.
hit: damage =>
hit: (damage) ->
p_up: Math.rand(this.charisma)
if p_up % 9 is 7
this.life += p_up / 4
@@ -87,7 +120,7 @@ Creature : {
if this.life <= 0 then puts("[" + this.name + " has died.]")
# This method takes one turn in a fight.
fight: enemy, weapon =>
fight: (enemy, weapon) ->
if this.life <= 0 then return puts("[" + this.name + "is too dead to fight!]")
# Attack the opponent.
@@ -123,12 +156,12 @@ Creature : {
# Get evil idea and swap in code words
print("Enter your new idea: ")
idea: gets()
code_words.each(real, code => idea.replace(real, code))
code_words.each((real, code) -> idea.replace(real, code))
# Save the jibberish to a new file
print("File encoded. Please enter a name for this idea: ")
idea_name: gets().strip()
File.open("idea-" + idea_name + '.txt', 'w', file => file.write(idea))
File.open("idea-" + idea_name + '.txt', 'w', (file) -> file.write(idea))
@@ -144,7 +177,7 @@ File.open("idea-" + idea_name + '.txt', 'w', file => file.write(idea))
# end
# end
wipe_mutterings_from: sentence =>
wipe_mutterings_from: (sentence) ->
throw new Error("cannot wipe mutterings") unless sentence.indexOf
while sentence.indexOf('(') >= 0
open: sentence.indexOf('(') - 1

View File

@@ -8,7 +8,7 @@ print("Odelay!") for i in [1..5]
# add = (x, y): x + y.
# add(2, 4) string print
add: x, y => x + y
add: (x, y) -> x + y
print(add(2, 4))
@@ -31,7 +31,7 @@ print({language: 'Potion', pointless: true}['language'])
# minus = (x, y): x - y.
# minus (y=10, x=6)
minus: x, y => x - y
minus: (x, y) -> x - y
minus(6, 10)
@@ -53,8 +53,8 @@ for key, val of {dog: 'canine', cat: 'feline', fox: 'vulpine'}
# Person print = ():
# ('My name is ', /name, '.') join print.
Person: =>
Person::print: =>
Person: ->
Person::print: ->
print('My name is ' + this.name + '.')
@@ -71,9 +71,9 @@ print(p.name)
#
# Policeman ('Constable') print
Policeman: rank => this.rank: rank
Policeman: (rank) -> this.rank: rank
Policeman extends Person
Policeman::print: =>
Policeman::print: ->
print('My name is ' + this.name + " and I'm a " + this.rank + '.')
print(new Policeman('Constable'))
@@ -115,13 +115,13 @@ table: {
# String length = (): 10.
# this foul business...
String::length: => 10
String::length: -> 10
# block = :
# 'potion' print.
block: =>
block: ->
print('potion')
@@ -178,7 +178,7 @@ if (3).gender?
# HomePage get = (url):
# session = url query ? at ('session').
HomePage::get: url =>
HomePage::get: (url) ->
session: url.query.session if url.query?
@@ -187,7 +187,7 @@ HomePage::get: url =>
# b /left = BTree ()
# b /right = BTree ()
BTree: =>
BTree: ->
b: new BTree()
b.left: new BTree()
b.right: new BTree()
@@ -199,7 +199,7 @@ b.right: new BTree()
# if (b ? /left):
# 'left path found!' print.
BTree: =>
BTree: ->
b: new BTree()
print('left path found!') if b.left?

View File

@@ -1,20 +0,0 @@
# Identifiers run together:
# a b c
# Trailing comma in array:
# array: [1, 2, 3, 4, 5,]
# Unterminated object literal:
# obj: { one: 1, two: 2
# Numbers run together:
# 101 202
# Strings run together:
# str: "broken" "words"
# Forgot to terminate a function:
# obj: {
# first: a => a[0].
# last: a => a[a.length-1]
# }

View File

@@ -1,6 +1,6 @@
# Underscore.coffee
# (c) 2009 Jeremy Ashkenas, DocumentCloud Inc.
# (c) 2010 Jeremy Ashkenas, DocumentCloud Inc.
# Underscore is freely distributable under the terms of the MIT license.
# Portions of Underscore are inspired by or borrowed from Prototype.js,
# Oliver Steele's Functional, and John Resig's Micro-Templating.
@@ -21,7 +21,7 @@
# If Underscore is called as a function, it returns a wrapped object that
# can be used OO-style. This wrapper holds altered versions of all the
# underscore functions. Wrapped objects may be chained.
wrapper: obj =>
wrapper: (obj) ->
this._wrapped: obj
this
@@ -31,7 +31,7 @@
# Create a safe reference to the Underscore object forreference below.
_: root._: obj => new wrapper(obj)
_: root._: (obj) -> new wrapper(obj)
# Export the Underscore object for CommonJS.
@@ -47,18 +47,18 @@
# Current version.
_.VERSION: '0.5.5'
_.VERSION: '0.5.8'
# ------------------------ Collection Functions: ---------------------------
# The cornerstone, an each implementation.
# Handles objects implementing forEach, arrays, and raw objects.
_.each: obj, iterator, context =>
_.each: (obj, iterator, context) ->
index: 0
try
return obj.forEach(iterator, context) if obj.forEach
if _.isArray(obj) or _.isArguments(obj)
if _.isNumber(obj.length)
return iterator.call(context, obj[i], i, obj) for i in [0...obj.length]
iterator.call(context, val, key, obj) for key, val of obj
catch e
@@ -68,36 +68,36 @@
# Return the results of applying the iterator to each element. Use JavaScript
# 1.6's version of map, if possible.
_.map: obj, iterator, context =>
_.map: (obj, iterator, context) ->
return obj.map(iterator, context) if (obj and _.isFunction(obj.map))
results: []
_.each(obj) value, index, list =>
_.each obj, (value, index, list) ->
results.push(iterator.call(context, value, index, list))
results
# Reduce builds up a single result from a list of values. Also known as
# inject, or foldl. Uses JavaScript 1.8's version of reduce, if possible.
_.reduce: obj, memo, iterator, context =>
_.reduce: (obj, memo, iterator, context) ->
return obj.reduce(_.bind(iterator, context), memo) if (obj and _.isFunction(obj.reduce))
_.each(obj) value, index, list =>
_.each obj, (value, index, list) ->
memo: iterator.call(context, memo, value, index, list)
memo
# The right-associative version of reduce, also known as foldr. Uses
# JavaScript 1.8's version of reduceRight, if available.
_.reduceRight: obj, memo, iterator, context =>
_.reduceRight: (obj, memo, iterator, context) ->
return obj.reduceRight(_.bind(iterator, context), memo) if (obj and _.isFunction(obj.reduceRight))
_.each(_.clone(_.toArray(obj)).reverse()) value, index =>
_.each _.clone(_.toArray(obj)).reverse(), (value, index) ->
memo: iterator.call(context, memo, value, index, obj)
memo
# Return the first value which passes a truth test.
_.detect: obj, iterator, context =>
_.detect: (obj, iterator, context) ->
result: null
_.each(obj) value, index, list =>
_.each obj, (value, index, list) ->
if iterator.call(context, value, index, list)
result: value
_.breakLoop()
@@ -106,97 +106,97 @@
# Return all the elements that pass a truth test. Use JavaScript 1.6's
# filter(), if it exists.
_.select: obj, iterator, context =>
_.select: (obj, iterator, context) ->
if obj and _.isFunction(obj.filter) then return obj.filter(iterator, context)
results: []
_.each(obj) value, index, list =>
_.each obj, (value, index, list) ->
results.push(value) if iterator.call(context, value, index, list)
results
# Return all the elements for which a truth test fails.
_.reject: obj, iterator, context =>
_.reject: (obj, iterator, context) ->
results: []
_.each(obj) value, index, list =>
_.each obj, (value, index, list) ->
results.push(value) if not iterator.call(context, value, index, list)
results
# Determine whether all of the elements match a truth test. Delegate to
# JavaScript 1.6's every(), if it is present.
_.all: obj, iterator, context =>
_.all: (obj, iterator, context) ->
iterator ||= _.identity
return obj.every(iterator, context) if obj and _.isFunction(obj.every)
result: true
_.each(obj) value, index, list =>
_.each obj, (value, index, list) ->
_.breakLoop() unless (result: result and iterator.call(context, value, index, list))
result
# Determine if at least one element in the object matches a truth test. Use
# JavaScript 1.6's some(), if it exists.
_.any: obj, iterator, context =>
_.any: (obj, iterator, context) ->
iterator ||= _.identity
return obj.some(iterator, context) if obj and _.isFunction(obj.some)
result: false
_.each(obj) value, index, list =>
_.each obj, (value, index, list) ->
_.breakLoop() if (result: iterator.call(context, value, index, list))
result
# Determine if a given value is included in the array or object,
# based on '==='.
_.include: obj, target =>
return _.indexOf(obj, target) isnt -1 if _.isArray(obj)
_.include: (obj, target) ->
return _.indexOf(obj, target) isnt -1 if obj and _.isFunction(obj.indexOf)
for key, val of obj
return true if val is target
false
# Invoke a method with arguments on every item in a collection.
_.invoke: obj, method =>
_.invoke: (obj, method) ->
args: _.rest(arguments, 2)
(if method then val[method] else val).apply(val, args) for val in obj
# Convenience version of a common use case of map: fetching a property.
_.pluck: obj, key =>
_.map(obj, (val => val[key]))
_.pluck: (obj, key) ->
_.map(obj, ((val) -> val[key]))
# Return the maximum item or (item-based computation).
_.max: obj, iterator, context =>
_.max: (obj, iterator, context) ->
return Math.max.apply(Math, obj) if not iterator and _.isArray(obj)
result: {computed: -Infinity}
_.each(obj) value, index, list =>
_.each obj, (value, index, list) ->
computed: if iterator then iterator.call(context, value, index, list) else value
computed >= result.computed and (result: {value: value, computed: computed})
result.value
# Return the minimum element (or element-based computation).
_.min: obj, iterator, context =>
_.min: (obj, iterator, context) ->
return Math.min.apply(Math, obj) if not iterator and _.isArray(obj)
result: {computed: Infinity}
_.each(obj) value, index, list =>
_.each obj, (value, index, list) ->
computed: if iterator then iterator.call(context, value, index, list) else value
computed < result.computed and (result: {value: value, computed: computed})
result.value
# Sort the object's values by a criteria produced by an iterator.
_.sortBy: obj, iterator, context =>
_.pluck(((_.map(obj) value, index, list =>
_.sortBy: (obj, iterator, context) ->
_.pluck(((_.map obj, (value, index, list) ->
{value: value, criteria: iterator.call(context, value, index, list)}
).sort() left, right =>
).sort((left, right) ->
a: left.criteria; b: right.criteria
if a < b then -1 else if a > b then 1 else 0
), 'value')
)), 'value')
# Use a comparator function to figure out at what index an object should
# be inserted so as to maintain order. Uses binary search.
_.sortedIndex: array, obj, iterator =>
_.sortedIndex: (array, obj, iterator) ->
iterator ||= _.identity
low: 0; high: array.length
while low < high
@@ -206,7 +206,7 @@
# Convert anything iterable into a real, live array.
_.toArray: iterable =>
_.toArray: (iterable) ->
return [] if (!iterable)
return iterable.toArray() if (iterable.toArray)
return iterable if (_.isArray(iterable))
@@ -215,7 +215,7 @@
# Return the number of elements in an object.
_.size: obj => _.toArray(obj).length
_.size: (obj) -> _.toArray(obj).length
# -------------------------- Array Functions: ------------------------------
@@ -223,7 +223,7 @@
# Get the first element of an array. Passing "n" will return the first N
# values in the array. Aliased as "head". The "guard" check allows it to work
# with _.map.
_.first: array, n, guard =>
_.first: (array, n, guard) ->
if n and not guard then slice.call(array, 0, n) else array[0]
@@ -231,35 +231,35 @@
# Especially useful on the arguments object. Passing an "index" will return
# the rest of the values in the array from that index onward. The "guard"
# check allows it to work with _.map.
_.rest: array, index, guard =>
_.rest: (array, index, guard) ->
slice.call(array, if _.isUndefined(index) or guard then 1 else index)
# Get the last element of an array.
_.last: array => array[array.length - 1]
_.last: (array) -> array[array.length - 1]
# Trim out all falsy values from an array.
_.compact: array => array[i] for i in [0...array.length] when array[i]
_.compact: (array) -> item for item in array when item
# Return a completely flattened version of an array.
_.flatten: array =>
_.reduce(array, []) memo, value =>
_.flatten: (array) ->
_.reduce array, [], (memo, value) ->
return memo.concat(_.flatten(value)) if _.isArray(value)
memo.push(value)
memo
# Return a version of the array that does not contain the specified value(s).
_.without: array =>
_.without: (array) ->
values: _.rest(arguments)
val for val in _.toArray(array) when not _.include(values, val)
# Produce a duplicate-free version of the array. If the array has already
# been sorted, you have the option of using a faster algorithm.
_.uniq: array, isSorted =>
_.uniq: (array, isSorted) ->
memo: []
for el, i in _.toArray(array)
memo.push(el) if i is 0 || (if isSorted is true then _.last(memo) isnt el else not _.include(memo, el))
@@ -268,28 +268,27 @@
# Produce an array that contains every item shared between all the
# passed-in arrays.
_.intersect: array =>
_.intersect: (array) ->
rest: _.rest(arguments)
_.select(_.uniq(array)) item =>
_.all(rest) other =>
_.select _.uniq(array), (item) ->
_.all rest, (other) ->
_.indexOf(other, item) >= 0
# Zip together multiple lists into a single array -- elements that share
# an index go together.
_.zip: =>
args: _.toArray(arguments)
length: _.max(_.pluck(args, 'length'))
_.zip: ->
length: _.max(_.pluck(arguments, 'length'))
results: new Array(length)
for i in [0...length]
results[i]: _.pluck(args, String(i))
results[i]: _.pluck(arguments, String(i))
results
# If the browser doesn't supply us with indexOf (I'm looking at you, MSIE),
# we need this function. Return the position of the first occurence of an
# item in an array, or -1 if the item is not included in the array.
_.indexOf: array, item =>
_.indexOf: (array, item) ->
return array.indexOf(item) if array.indexOf
i: 0; l: array.length
while l - i
@@ -299,7 +298,7 @@
# Provide JavaScript 1.6's lastIndexOf, delegating to the native function,
# if possible.
_.lastIndexOf: array, item =>
_.lastIndexOf: (array, item) ->
return array.lastIndexOf(item) if array.lastIndexOf
i: array.length
while i
@@ -310,11 +309,11 @@
# Generate an integer Array containing an arithmetic progression. A port of
# the native Python range() function. See:
# http://docs.python.org/library/functions.html#range
_.range: start, stop, step =>
a: _.toArray(arguments)
_.range: (start, stop, step) ->
a: arguments
solo: a.length <= 1
i: start: if solo then 0 else a[0];
stop: if solo then a[0] else a[1];
i: start: if solo then 0 else a[0]
stop: if solo then a[0] else a[1]
step: a[2] or 1
len: Math.ceil((stop - start) / step)
return [] if len <= 0
@@ -331,45 +330,45 @@
# Create a function bound to a given object (assigning 'this', and arguments,
# optionally). Binding with arguments is also known as 'curry'.
_.bind: func, obj =>
_.bind: (func, obj) ->
args: _.rest(arguments, 2)
=> func.apply(obj or root, args.concat(_.toArray(arguments)))
-> func.apply(obj or root, args.concat(arguments))
# Bind all of an object's methods to that object. Useful for ensuring that
# all callbacks defined on an object belong to it.
_.bindAll: obj =>
_.bindAll: (obj) ->
funcs: if arguments.length > 1 then _.rest(arguments) else _.functions(obj)
_.each(funcs, (f => obj[f]: _.bind(obj[f], obj)))
_.each(funcs, (f) -> obj[f]: _.bind(obj[f], obj))
obj
# Delays a function for the given number of milliseconds, and then calls
# it with the arguments supplied.
_.delay: func, wait =>
_.delay: (func, wait) ->
args: _.rest(arguments, 2)
setTimeout((=> func.apply(func, args)), wait)
setTimeout((-> func.apply(func, args)), wait)
# Defers a function, scheduling it to run after the current call stack has
# cleared.
_.defer: func =>
_.defer: (func) ->
_.delay.apply(_, [func, 1].concat(_.rest(arguments)))
# Returns the first function passed as an argument to the second,
# allowing you to adjust arguments, run code before and after, and
# conditionally execute the original function.
_.wrap: func, wrapper =>
=> wrapper.apply(wrapper, [func].concat(_.toArray(arguments)))
_.wrap: (func, wrapper) ->
-> wrapper.apply(wrapper, [func].concat(arguments))
# Returns a function that is the composition of a list of functions, each
# consuming the return value of the function that follows.
_.compose: =>
funcs: _.toArray(arguments)
=>
args: _.toArray(arguments)
_.compose: ->
funcs: arguments
->
args: arguments
for i in [(funcs.length - 1)..0]
args: [funcs[i].apply(this, args)]
args[0]
@@ -378,43 +377,43 @@
# ------------------------- Object Functions: ----------------------------
# Retrieve the names of an object's properties.
_.keys: obj =>
_.keys: (obj) ->
return _.range(0, obj.length) if _.isArray(obj)
key for key, val of obj
# Retrieve the values of an object's properties.
_.values: obj =>
_.values: (obj) ->
_.map(obj, _.identity)
# Return a sorted list of the function names available in Underscore.
_.functions: obj =>
_.select(_.keys(obj), key => _.isFunction(obj[key])).sort()
_.functions: (obj) ->
_.select(_.keys(obj), (key) -> _.isFunction(obj[key])).sort()
# Extend a given object with all of the properties in a source object.
_.extend: destination, source =>
_.extend: (destination, source) ->
for key, val of source
destination[key]: val
destination
# Create a (shallow-cloned) duplicate of an object.
_.clone: obj =>
_.clone: (obj) ->
return obj.slice(0) if _.isArray(obj)
_.extend({}, obj)
# Invokes interceptor with the obj, and then returns obj.
# The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
_.tap: obj, interceptor =>
_.tap: (obj, interceptor) ->
interceptor(obj)
obj
# Perform a deep comparison to check if two objects are equal.
_.isEqual: a, b =>
_.isEqual: (a, b) ->
# Check object identity.
return true if a is b
# Different types?
@@ -450,93 +449,104 @@
# Is a given array or object empty?
_.isEmpty: obj => _.keys(obj).length is 0
_.isEmpty: (obj) -> _.keys(obj).length is 0
# Is a given value a DOM element?
_.isElement: obj => obj and obj.nodeType is 1
_.isElement: (obj) -> obj and obj.nodeType is 1
# Is a given value an array?
_.isArray: obj => !!(obj and obj.concat and obj.unshift)
_.isArray: (obj) -> !!(obj and obj.concat and obj.unshift)
# Is a given variable an arguments object?
_.isArguments: obj => obj and _.isNumber(obj.length) and !_.isArray(obj) and !propertyIsEnumerable.call(obj, 'length')
_.isArguments: (obj) -> obj and _.isNumber(obj.length) and not obj.concat and
not obj.substr and not obj.apply and not propertyIsEnumerable.call(obj, 'length')
# Is the given value a function?
_.isFunction: obj => !!(obj and obj.constructor and obj.call and obj.apply)
_.isFunction: (obj) -> !!(obj and obj.constructor and obj.call and obj.apply)
# Is the given value a string?
_.isString: obj => !!(obj is '' or (obj and obj.charCodeAt and obj.substr))
_.isString: (obj) -> !!(obj is '' or (obj and obj.charCodeAt and obj.substr))
# Is a given value a number?
_.isNumber: obj => toString.call(obj) is '[object Number]'
_.isNumber: (obj) -> (obj is +obj) or toString.call(obj) is '[object Number]'
# Is a given value a Date?
_.isDate: obj => !!(obj and obj.getTimezoneOffset and obj.setUTCFullYear)
_.isDate: (obj) -> !!(obj and obj.getTimezoneOffset and obj.setUTCFullYear)
# Is the given value a regular expression?
_.isRegExp: obj => !!(obj and obj.exec and (obj.ignoreCase or obj.ignoreCase is false))
_.isRegExp: (obj) -> !!(obj and obj.exec and (obj.ignoreCase or obj.ignoreCase is false))
# Is the given value NaN -- this one is interesting. NaN != NaN, and
# isNaN(undefined) == true, so we make sure it's a number first.
_.isNaN: obj => _.isNumber(obj) and window.isNaN(obj)
_.isNaN: (obj) -> _.isNumber(obj) and window.isNaN(obj)
# Is a given value equal to null?
_.isNull: obj => obj is null
_.isNull: (obj) -> obj is null
# Is a given variable undefined?
_.isUndefined: obj => typeof obj is 'undefined'
_.isUndefined: (obj) -> typeof obj is 'undefined'
# -------------------------- Utility Functions: --------------------------
# Run Underscore.js in noConflict mode, returning the '_' variable to its
# previous owner. Returns a reference to the Underscore object.
_.noConflict: =>
_.noConflict: ->
root._: previousUnderscore
this
# Keep the identity function around for default iterators.
_.identity: value => value
_.identity: (value) -> value
# Break out of the middle of an iteration.
_.breakLoop: => throw breaker
_.breakLoop: -> throw breaker
# Generate a unique integer id (unique within the entire client session).
# Useful for temporary DOM ids.
idCounter: 0
_.uniqueId: prefix =>
_.uniqueId: (prefix) ->
(prefix or '') + idCounter++
# By default, Underscore uses ERB-style template delimiters, change the
# following template settings to use alternative delimiters.
_.templateSettings: {
start: '<%'
end: '%>'
interpolate: /<%=(.+?)%>/g
}
# JavaScript templating a-la ERB, pilfered from John Resig's
# "Secrets of the JavaScript Ninja", page 83.
_.template: str, data =>
`var fn = new Function('obj',
# Single-quote fix from Rick Strahl's version.
_.template: (str, data) ->
c: _.templateSettings
fn: new Function 'obj',
'var p=[],print=function(){p.push.apply(p,arguments);};' +
'with(obj){p.push(\'' +
str.
replace(/[\r\t\n]/g, " ").
split("<%").join("\t").
replace(/((^|%>)[^\t]*)'/g, "$1\r").
replace(/\t=(.*?)%>/g, "',$1,'").
split("\t").join("');").
split("%>").join("p.push('").
split("\r").join("\\'") +
"');}return p.join('');")`
str.replace(/[\r\t\n]/g, " ")
.replace(new RegExp("'(?=[^"+c.end[0]+"]*"+c.end+")","g"),"\t")
.split("'").join("\\'")
.split("\t").join("'")
.replace(c.interpolate, "',$1,'")
.split(c.start).join("');")
.split(c.end).join("p.push('") +
"');}return p.join('');"
if data then fn(data) else fn
@@ -553,42 +563,41 @@
_.methods: _.functions
# /*------------------------ Setup the OOP Wrapper: --------------------------*/
# ------------------------ Setup the OOP Wrapper: --------------------------
# Helper function to continue chaining intermediate results.
result: obj, chain =>
result: (obj, chain) ->
if chain then _(obj).chain() else obj
# Add all of the Underscore functions to the wrapper object.
_.each(_.functions(_)) name =>
_.each _.functions(_), (name) ->
method: _[name]
wrapper.prototype[name]: =>
args: _.toArray(arguments)
unshift.call(args, this._wrapped)
result(method.apply(_, args), this._chain)
wrapper.prototype[name]: ->
unshift.call(arguments, this._wrapped)
result(method.apply(_, arguments), this._chain)
# Add all mutator Array functions to the wrapper.
_.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift']) name =>
_.each ['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], (name) ->
method: Array.prototype[name]
wrapper.prototype[name]: =>
wrapper.prototype[name]: ->
method.apply(this._wrapped, arguments)
result(this._wrapped, this._chain)
# Add all accessor Array functions to the wrapper.
_.each(['concat', 'join', 'slice']) name =>
_.each ['concat', 'join', 'slice'], (name) ->
method: Array.prototype[name]
wrapper.prototype[name]: =>
wrapper.prototype[name]: ->
result(method.apply(this._wrapped, arguments), this._chain)
# Start chaining a wrapped Underscore object.
wrapper::chain: =>
wrapper::chain: ->
this._chain: true
this
# Extracts the result from a wrapped and chained object.
wrapper::value: => this._wrapped
wrapper::value: -> this._wrapped

View File

@@ -0,0 +1,12 @@
# Contributed by Jason Huggins
http: require 'http'
server: http.createServer (req, res) ->
res.sendHeader 200, {'Content-Type': 'text/plain'}
res.sendBody 'Hello, World!'
res.finish()
server.listen 3000
puts "Server running at http://localhost:3000/"

View File

@@ -19,52 +19,31 @@
<dict>
<key>captures</key>
<dict>
<key>1</key>
<key>1</key>
<dict>
<key>name</key>
<string>entity.name.function.coffee</string>
<string>variable.parameter.function.coffee</string>
</dict>
<key>2</key>
<dict>
<key>name</key>
<string>keyword.operator.coffee</string>
</dict>
<key>3</key>
<dict>
<key>name</key>
<string>variable.parameter.function.coffee</string>
</dict>
<key>4</key>
<dict>
<key>name</key>
<string>storage.type.function.coffee</string>
</dict>
</dict>
<key>comment</key>
<string>match stuff like: funcName: =&gt; … </string>
<key>match</key>
<string>([a-zA-Z0-9_?.$:*]*)\s*(=|:)\s*([\w,\s]*?)\s*(=&gt;)</string>
<key>name</key>
<string>meta.function.coffee</string>
</dict>
<dict>
<key>captures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>variable.parameter.function.coffee</string>
</dict>
<key>2</key>
<key>5</key>
<dict>
<key>name</key>
<string>storage.type.function.coffee</string>
</dict>
</dict>
<key>comment</key>
<string>match stuff like: a =&gt; … </string>
<string>match stuff like: a -&gt; … </string>
<key>match</key>
<string>([a-zA-Z0-9_?., $:*]*)\s*(=&gt;)</string>
<string>(\()([a-zA-Z0-9_?.$]*(,\s*[a-zA-Z0-9_?.$]+)*)(\))\s*((=|-)&gt;)</string>
<key>name</key>
<string>meta.inline.function.coffee</string>
</dict>
@@ -93,6 +72,36 @@
<key>name</key>
<string>constant.numeric.coffee</string>
</dict>
<dict>
<key>match</key>
<string>(@)([a-zA-Z_$]\w*)?</string>
<key>name</key>
<string>variable.other.readwrite.instance.coffee</string>
</dict>
<dict>
<key>name</key>
<string>string.quoted.heredoc.coffee</string>
<key>begin</key>
<string>("""|''')</string>
<key>end</key>
<string>("""|''')</string>
<key>beginCaptures</key>
<dict>
<key>0</key>
<dict>
<key>name</key>
<string>punctuation.definition.string.begin.coffee</string>
</dict>
</dict>
<key>endCaptures</key>
<dict>
<key>0</key>
<dict>
<key>name</key>
<string>punctuation.definition.string.end.coffee</string>
</dict>
</dict>
</dict>
<dict>
<key>begin</key>
<string>'</string>
@@ -206,6 +215,39 @@
<key>name</key>
<string>comment.line.coffee</string>
</dict>
<dict>
<key>begin</key>
<string>(?&lt;=[=(:]|^|return)\s*(/)(?![/*+{}?])</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>punctuation.definition.string.begin.coffee</string>
</dict>
</dict>
<key>end</key>
<string>(/)[igm]*</string>
<key>endCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>punctuation.definition.string.end.coffee</string>
</dict>
</dict>
<key>name</key>
<string>string.regexp.coffee</string>
<key>patterns</key>
<array>
<dict>
<key>match</key>
<string>\\.</string>
<key>name</key>
<string>constant.character.escape.coffee</string>
</dict>
</array>
</dict>
<dict>
<key>match</key>
<string>\b(break|by|catch|continue|else|finally|for|in|of|if|return|switch|then|throw|try|unless|when|while)\b</string>
@@ -214,7 +256,7 @@
</dict>
<dict>
<key>match</key>
<string>\b([a-zA-Z$_](\w|\$|:|\.)*)(\:)\s</string>
<string>\b([a-zA-Z$_](\w|\$|:|\.)*\s*(?=\:))</string>
<key>name</key>
<string>variable.assignment.coffee</string>
<key>captures</key>
@@ -224,11 +266,6 @@
<key>name</key>
<string>entity.name.function.coffee</string>
</dict>
<key>3</key>
<dict>
<key>name</key>
<string>keyword.operator.coffee</string>
</dict>
</dict>
</dict>
<dict>
@@ -261,6 +298,12 @@
<key>name</key>
<string>keyword.other.coffee</string>
</dict>
<dict>
<key>match</key>
<string>(=|-)&gt;</string>
<key>name</key>
<string>storage.type.function.coffee</string>
</dict>
<dict>
<key>match</key>
<string>!|%|&amp;|\*|\/|\-\-|\-|\+\+|\+|~|===|==|=|!=|!==|&lt;=|&gt;=|&lt;&lt;=|&gt;&gt;=|&gt;&gt;&gt;=|&lt;&gt;|&lt;|&gt;|!|&amp;&amp;|\?|\|\||\:|\*=|(?&lt;!\()/=|%=|\+=|\-=|&amp;=|\^=|\b(instanceof|new|delete|typeof|and|or|is|isnt|not)\b</string>
@@ -273,39 +316,6 @@
<key>name</key>
<string>constant.language.coffee</string>
</dict>
<dict>
<key>begin</key>
<string>(?&lt;=[=(:]|^|return)\s*(/)(?![/*+{}?])</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>punctuation.definition.string.begin.coffee</string>
</dict>
</dict>
<key>end</key>
<string>(/)[igm]*</string>
<key>endCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>punctuation.definition.string.end.coffee</string>
</dict>
</dict>
<key>name</key>
<string>string.regexp.coffee</string>
<key>patterns</key>
<array>
<dict>
<key>match</key>
<string>\\.</string>
<key>name</key>
<string>constant.character.escape.coffee</string>
</dict>
</array>
</dict>
<dict>
<key>match</key>
<string>\;</string>

20
extras/EXTRAS Normal file
View File

@@ -0,0 +1,20 @@
This folder includes rough cuts of CoffeeScript syntax highlighters for
TextMate and Vim. Improvements to their lexing ability are always welcome.
To install the TextMate bundle, drop it into:
~/Library/Application Support/TextMate/Bundles
To install the Vim highlighter, copy "coffee.vim" into the "syntax" directory of
your vim72, and enable it in either of the following two ways:
* Manually, by running `:set syntax=coffee`
* Or automatically, by creating a "filetype.vim" file within "~/.vim", which
contains something along these lines:
if exists("did_load_filetypes")
finish
end
augroup filetypedetect
au! BufRead,BufNewFile *.coffee setfiletype coffee
augroup END

117
extras/coffee.vim Normal file
View File

@@ -0,0 +1,117 @@
" Vim syntax file
" Language: CoffeeScript
" Maintainer: Jeff Olson <olson.jeffery@gmail.com>
" URL: http://github.com/olsonjeffery
" Changes: (jro) initial port from javascript
" Last Change: 2006 Jun 19
" Adaptation of javascript.vim syntax file (distro'd w/ vim72),
" maintained by Claudio Fleiner <claudio@fleiner.com>
" with updates from Scott Shattuck (ss) <ss@technicalpursuit.com>
if !exists("main_syntax")
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
let main_syntax = 'coffee'
endif
syn case ignore
syn match coffeeLineComment "#.*" contains=@Spell,CoffeeCommentTodo
syn match coffeeSpecial "\\\d\d\d\|\\."
syn region coffeeStringD start=+"+ skip=+\\\\\|\\"+ end=+"\|$+ contains=coffeeSpecial,@htmlPreproc
syn region coffeeStringS start=+'+ skip=+\\\\\|\\'+ end=+'\|$+ contains=coffeeSpecial,@htmlPreproc
syn match coffeeSpecialCharacter "'\\.'"
syn match coffeeNumber "-\=\<\d\+L\=\>\|0[xX][0-9a-fA-F]\+\>"
syn region coffeeRegexpString start=+/[^/*]+me=e-1 skip=+\\\\\|\\/+ end=+/[gi]\{0,2\}\s*$+ end=+/[gi]\{0,2\}\s*[;.,)\]}]+me=e-1 contains=@htmlPreproc oneline
syn match coffeeFunctionParams "([^)]*)\s*->"
syn match coffeeBindFunctionParams "([^)]*)\s*=>"
syn match coffeePrototypeAccess "::"
syn match coffeeBindFunction "=[1]>[1]"
syn match coffeeFunction "->"
syn keyword coffeeExtends extends
syn keyword coffeeConditional if else switch then not
syn keyword coffeeRepeat while for in of
syn keyword coffeeBranch break continue
syn keyword coffeeOperator delete instanceof typeof
syn keyword coffeeType Array Boolean Date Function Number Object String RegExp
syn keyword coffeeStatement return with
syn keyword coffeeBoolean true false
syn keyword coffeeNull null undefined
syn keyword coffeeIdentifier arguments this var
syn keyword coffeeLabel case default
syn keyword coffeeException try catch finally throw
syn keyword coffeeMessage alert confirm prompt status
syn keyword coffeeGlobal self window top parent
syn keyword coffeeMember document event location
syn keyword coffeeDeprecated escape unescape
syn keyword coffeeReserved abstract boolean byte char class const debugger double enum export final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile
syn sync fromstart
syn sync maxlines=100
if main_syntax == "coffee"
syn sync ccomment coffeeComment
endif
" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_coffee_syn_inits")
if version < 508
let did_coffee_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink coffeePrototypeAccess Keyword
HiLink coffeeExtends Keyword
HiLink coffeeLineComment Comment
HiLink coffeeSpecial Special
HiLink coffeeStringS String
HiLink coffeeStringD String
HiLink coffeeCharacter Character
HiLink coffeeSpecialCharacter coffeeSpecial
HiLink coffeeNumber coffeeValue
HiLink coffeeConditional Conditional
HiLink coffeeRepeat Repeat
HiLink coffeeBranch Conditional
HiLink coffeeOperator Operator
HiLink coffeeType Type
HiLink coffeeStatement Statement
HiLink coffeeBindFunctionParams Function
HiLink coffeeFunctionParams Function
HiLink coffeeFunction Function
HiLink coffeeBindFunction Function
HiLink coffeeBraces Function
HiLink coffeeError Error
HiLink coffeeScrParenError coffeeError
HiLink coffeeNull Keyword
HiLink coffeeBoolean Boolean
HiLink coffeeRegexpString String
HiLink coffeeIdentifier Identifier
HiLink coffeeLabel Label
HiLink coffeeException Exception
HiLink coffeeMessage Keyword
HiLink coffeeGlobal Keyword
HiLink coffeeMember Keyword
HiLink coffeeDeprecated Exception
HiLink coffeeReserved Keyword
HiLink coffeeDebug Debug
HiLink coffeeConstant Label
delcommand HiLink
endif
let b:current_syntax = "coffee"
if main_syntax == 'coffee'
unlet main_syntax
endif
" vim: ts=8

1177
index.html

File diff suppressed because it is too large Load Diff

7
lib/bin/cake Executable file
View File

@@ -0,0 +1,7 @@
#!/usr/bin/env node
process.mixin(require('sys'));
require.paths.unshift('/usr/local/lib/coffee-script/lib');
require('cake').run();

7
lib/bin/coffee Executable file
View File

@@ -0,0 +1,7 @@
#!/usr/bin/env node
process.mixin(require('sys'));
require.paths.unshift('/usr/local/lib/coffee-script/lib');
require('command_line').run();

80
lib/cake.js Executable file
View File

@@ -0,0 +1,80 @@
(function(){
var coffee, fs, no_such_task, path, print_tasks, tasks;
var __hasProp = Object.prototype.hasOwnProperty;
// `cake` is a simplified version of Make (Rake, Jake) for CoffeeScript.
fs = require('fs');
path = require('path');
coffee = require('coffee-script');
tasks = {};
no_such_task = function no_such_task(task) {
process.stdio.writeError('No such task: "' + task + '"\n');
return process.exit(1);
};
// Mixin the Cake functionality.
process.mixin({
// Define a task with a name, a description, and the action itself.
task: function task(name, description, action) {
return tasks[name] = {
name: name,
description: description,
action: action
};
},
// Invoke another task in the Cakefile.
invoke: function invoke(name) {
if (!(tasks[name])) {
no_such_task(name);
}
return tasks[name].action();
}
});
// Display the list of Cake tasks.
print_tasks = function print_tasks() {
var _a, _b, _c, _d, _e, _f, _g, i, name, spaces, task;
_a = []; _b = tasks;
for (name in _b) { if (__hasProp.call(_b, name)) {
task = _b[name];
_a.push((function() {
spaces = 20 - name.length;
spaces = spaces > 0 ? (function() {
_c = []; _f = 0; _g = spaces;
for (_e=0, i=_f; (_f <= _g ? i <= _g : i >= _g); (_f <= _g ? i += 1 : i -= 1), _e++) {
_c.push(' ');
}
return _c;
}).call(this).join('') : '';
return puts("cake " + name + spaces + ' # ' + task.description);
}).call(this));
}}
return _a;
};
// Running `cake` runs the tasks you pass asynchronously (node-style), or
// prints them out, with no arguments.
exports.run = function run() {
return path.exists('Cakefile', function(exists) {
var args;
if (!(exists)) {
throw new Error('Cakefile not found in ' + process.cwd());
}
args = process.ARGV.slice(2, process.ARGV.length);
return fs.readFile('Cakefile', function(err, source) {
var _a, _b, _c, arg;
eval(coffee.compile(source));
if (!(args.length)) {
return print_tasks();
}
_a = []; _b = args;
for (_c = 0; _c < _b.length; _c++) {
arg = _b[_c];
_a.push((function() {
if (!(tasks[arg])) {
no_such_task(arg);
}
return tasks[arg].action();
}).call(this));
}
return _a;
});
});
};
})();

61
lib/coffee-script.js Normal file
View File

@@ -0,0 +1,61 @@
(function(){
var lexer, parser, path;
// Set up for both the browser and the server.
if ((typeof process !== "undefined" && process !== null)) {
process.mixin(require('nodes'));
path = require('path');
lexer = new (require('lexer').Lexer)();
parser = require('parser').parser;
} else {
lexer = new Lexer();
parser = exports.parser;
this.exports = (this.CoffeeScript = {});
}
// Thin wrapper for Jison compatibility around the real lexer.
parser.lexer = {
lex: function lex() {
var token;
token = this.tokens[this.pos] || [""];
this.pos += 1;
this.yylineno = token[2];
this.yytext = token[1];
return token[0];
},
setInput: function setInput(tokens) {
this.tokens = tokens;
return this.pos = 0;
},
upcomingInput: function upcomingInput() {
return "";
},
showPosition: function showPosition() {
return this.pos;
}
};
exports.VERSION = '0.5.0';
// Compile CoffeeScript to JavaScript, using the Coffee/Jison compiler.
exports.compile = function compile(code, options) {
return (parser.parse(lexer.tokenize(code))).compile(options);
};
// Just the tokens.
exports.tokenize = function tokenize(code) {
return lexer.tokenize(code);
};
// Just the nodes.
exports.tree = function tree(code) {
return parser.parse(lexer.tokenize(code));
};
// Pretty-print a token stream.
exports.print_tokens = function print_tokens(tokens) {
var _a, _b, _c, strings, token;
strings = (function() {
_a = []; _b = tokens;
for (_c = 0; _c < _b.length; _c++) {
token = _b[_c];
_a.push('[' + token[0] + ' ' + token[1].toString().replace(/\n/, '\\n') + ']');
}
return _a;
}).call(this);
return puts(strings.join(' '));
};
})();

View File

@@ -1,21 +0,0 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
require "coffee_script/lexer"
require "coffee_script/parser"
require "coffee_script/nodes"
require "coffee_script/value"
require "coffee_script/scope"
require "coffee_script/rewriter"
require "coffee_script/parse_error"
# Namespace for all CoffeeScript internal classes.
module CoffeeScript
VERSION = '0.2.3' # Keep in sync with the gemspec.
# Compile a script (String or IO) to JavaScript.
def self.compile(script, options={})
script = script.read if script.respond_to?(:read)
Parser.new.parse(script).compile(options)
end
end

View File

@@ -1,227 +0,0 @@
require 'optparse'
require 'fileutils'
require 'open3'
begin
require File.expand_path(File.dirname(__FILE__) + '/../coffee-script')
rescue LoadError => e
puts(e.message)
puts("use \"rake build:parser\" to regenerate parser.rb")
exit(1)
end
module CoffeeScript
# The CommandLine handles all of the functionality of the `coffee`
# utility.
class CommandLine
BANNER = <<-EOS
coffee compiles CoffeeScript source files into JavaScript.
Usage:
coffee path/to/script.coffee
EOS
# Seconds to pause between checks for changed source files.
WATCH_INTERVAL = 0.5
# Command to execute in Narwhal
PACKAGE = File.expand_path(File.dirname(__FILE__) + '/../..')
LAUNCHER = "narwhal -p #{PACKAGE} -e 'require(\"coffee-script\").run(system.args);'"
# Run the CommandLine off the contents of ARGV.
def initialize
@mtimes = {}
parse_options
return launch_repl if @options[:interactive]
return eval_scriptlet if @options[:eval]
check_sources
return run_scripts if @options[:run]
@sources.each {|source| compile_javascript(source) }
watch_coffee_scripts if @options[:watch]
end
# The "--help" usage message.
def usage
puts "\n#{@option_parser}\n"
exit
end
private
# Compiles (or partially compiles) the source CoffeeScript file, returning
# the desired JS, tokens, or lint results.
def compile_javascript(source)
script = File.read(source)
return tokens(script) if @options[:tokens]
js = compile(script, source)
return unless js
return puts(js) if @options[:print]
return lint(js) if @options[:lint]
File.open(path_for(source), 'w+') {|f| f.write(js) }
end
# Spins up a watcher thread to keep track of the modification times of the
# source files, recompiling them whenever they're saved.
def watch_coffee_scripts
watch_thread = Thread.start do
loop do
@sources.each do |source|
mtime = File.stat(source).mtime
@mtimes[source] ||= mtime
if mtime > @mtimes[source]
@mtimes[source] = mtime
compile_javascript(source)
end
end
sleep WATCH_INTERVAL
end
end
Signal.trap("INT") { watch_thread.kill }
watch_thread.join
end
# Ensure that all of the source files exist.
def check_sources
usage if @sources.empty?
missing = @sources.detect {|s| !File.exists?(s) }
if missing
STDERR.puts("File not found: '#{missing}'")
exit(1)
end
end
# Pipe compiled JS through JSLint (requires a working 'jsl' command).
def lint(js)
stdin, stdout, stderr = Open3.popen3('jsl -nologo -stdin')
stdin.write(js)
stdin.close
puts stdout.read.tr("\n", '')
errs = stderr.read.chomp
puts errs unless errs.empty?
stdout.close and stderr.close
end
# Eval a little piece of CoffeeScript directly from the command line.
def eval_scriptlet
script = STDIN.tty? ? @sources.join(' ') : STDIN.read
return tokens(script) if @options[:tokens]
js = compile(script)
return lint(js) if @options[:lint]
puts js
end
# Use Narwhal to run an interactive CoffeeScript session.
def launch_repl
exec "#{LAUNCHER}"
rescue Errno::ENOENT
puts "Error: Narwhal must be installed to use the interactive REPL."
exit(1)
end
# Use Narwhal to compile and execute CoffeeScripts.
def run_scripts
sources = @sources.join(' ')
exec "#{LAUNCHER} #{sources}"
rescue Errno::ENOENT
puts "Error: Narwhal must be installed in order to execute CoffeeScripts."
exit(1)
end
# Print the tokens that the lexer generates from a source script.
def tokens(script)
puts Lexer.new.tokenize(script).inspect
end
# Compile a single source file to JavaScript.
def compile(script, source='error')
begin
options = {}
options[:no_wrap] = true if @options[:no_wrap]
options[:globals] = true if @options[:globals]
CoffeeScript.compile(script, options)
rescue CoffeeScript::ParseError, SyntaxError => e
STDERR.puts "#{source}: #{e.message}"
exit(1) unless @options[:watch]
nil
end
end
# Write out JavaScript alongside CoffeeScript unless an output directory
# is specified.
def path_for(source)
filename = File.basename(source, File.extname(source)) + '.js'
dir = @options[:output] || File.dirname(source)
File.join(dir, filename)
end
# Install the CoffeeScript TextMate bundle to ~/Library.
def install_bundle
bundle_dir = File.expand_path('~/Library/Application Support/TextMate/Bundles/')
FileUtils.cp_r(File.dirname(__FILE__) + '/CoffeeScript.tmbundle', bundle_dir)
end
# Use OptionParser for all the options.
def parse_options
@options = {}
@option_parser = OptionParser.new do |opts|
opts.on('-i', '--interactive', 'run a CoffeeScript REPL (requires Narwhal)') do |i|
@options[:interactive] = true
end
opts.on('-r', '--run', 'compile and run a script (requires Narwhal)') do |r|
@options[:run] = true
end
opts.on('-o', '--output [DIR]', 'set the directory for compiled JavaScript') do |d|
@options[:output] = d
FileUtils.mkdir_p(d) unless File.exists?(d)
end
opts.on('-w', '--watch', 'watch scripts for changes, and recompile') do |w|
@options[:watch] = true
end
opts.on('-p', '--print', 'print the compiled JavaScript to stdout') do |d|
@options[:print] = true
end
opts.on('-l', '--lint', 'pipe the compiled JavaScript through JSLint') do |l|
@options[:lint] = true
end
opts.on('-e', '--eval', 'compile a cli scriptlet or read from stdin') do |e|
@options[:eval] = true
end
opts.on('-t', '--tokens', 'print the tokens that the lexer produces') do |t|
@options[:tokens] = true
end
opts.on('-v', '--verbose', 'print at every step of code generation') do |v|
ENV['VERBOSE'] = 'true'
end
opts.on('-n', '--no-wrap', 'raw output, no function safety wrapper') do |n|
@options[:no_wrap] = true
end
opts.on('-g', '--globals', 'attach all top-level variable as globals') do |n|
@options[:globals] = true
end
opts.on_tail('--install-bundle', 'install the CoffeeScript TextMate bundle') do |i|
install_bundle
exit
end
opts.on_tail('--version', 'display CoffeeScript version') do
puts "CoffeeScript version #{CoffeeScript::VERSION}"
exit
end
opts.on_tail('-h', '--help', 'display this help message') do
usage
end
end
@option_parser.banner = BANNER
begin
@option_parser.parse!(ARGV)
rescue OptionParser::InvalidOption => e
puts e.message
exit(1)
end
@sources = ARGV
end
end
end

View File

@@ -1,454 +0,0 @@
class Parser
# Declare terminal tokens produced by the lexer.
token IF ELSE UNLESS
token NUMBER STRING REGEX
token TRUE FALSE YES NO ON OFF
token IDENTIFIER PROPERTY_ACCESS PROTOTYPE_ACCESS
token CODE PARAM NEW RETURN
token TRY CATCH FINALLY THROW
token BREAK CONTINUE
token FOR IN OF BY WHEN WHILE
token SWITCH LEADING_WHEN
token DELETE INSTANCEOF TYPEOF
token SUPER EXTENDS
token ARGUMENTS
token NEWLINE
token COMMENT
token JS
token INDENT OUTDENT
# Declare order of operations.
prechigh
left '?'
nonassoc UMINUS NOT '!' '!!' '~' '++' '--'
left '*' '/' '%'
left '+' '-'
left '<<' '>>' '>>>'
left '&' '|' '^'
left '<=' '<' '>' '>='
right '==' '!=' IS ISNT
left '&&' '||' AND OR
right '-=' '+=' '/=' '*=' '%='
right DELETE INSTANCEOF TYPEOF
left '.'
right INDENT
left OUTDENT
right WHEN LEADING_WHEN IN OF BY
right THROW FOR NEW SUPER
left EXTENDS
left ASSIGN '||=' '&&='
right RETURN
right '=>' UNLESS IF ELSE WHILE
preclow
rule
# All parsing will end in this rule, being the trunk of the AST.
Root:
/* nothing */ { result = Expressions.new }
| Terminator { result = Expressions.new }
| Expressions { result = val[0] }
| Block Terminator { result = val[0] }
;
# Any list of expressions or method body, seperated by line breaks or semis.
Expressions:
Expression { result = Expressions.wrap(val) }
| Expressions Terminator Expression { result = val[0] << val[2] }
| Expressions Terminator { result = val[0] }
;
# All types of expressions in our language. The basic unit of CoffeeScript
# is the expression.
Expression:
Value
| Call
| Code
| Operation
| Assign
| If
| Try
| Throw
| Return
| While
| For
| Switch
| Extends
| Splat
| Existence
| Comment
;
# A block of expressions. Note that the Rewriter will convert some postfix
# forms into blocks for us, by altering the token stream.
Block:
INDENT Expressions OUTDENT { result = val[1] }
| INDENT OUTDENT { result = Expressions.new }
;
# Tokens that can terminate an expression.
Terminator:
"\n"
| ";"
;
# All hard-coded values. These can be printed straight to JavaScript.
Literal:
NUMBER { result = LiteralNode.new(val[0]) }
| STRING { result = LiteralNode.new(val[0]) }
| JS { result = LiteralNode.new(val[0]) }
| REGEX { result = LiteralNode.new(val[0]) }
| BREAK { result = LiteralNode.new(val[0]) }
| CONTINUE { result = LiteralNode.new(val[0]) }
| ARGUMENTS { result = LiteralNode.new(val[0]) }
| TRUE { result = LiteralNode.new(true) }
| FALSE { result = LiteralNode.new(false) }
| YES { result = LiteralNode.new(true) }
| NO { result = LiteralNode.new(false) }
| ON { result = LiteralNode.new(true) }
| OFF { result = LiteralNode.new(false) }
;
# Assignment to a variable (or index).
Assign:
Value ASSIGN Expression { result = AssignNode.new(val[0], val[2]) }
;
# Assignment within an object literal (can be quoted).
AssignObj:
IDENTIFIER ASSIGN Expression { result = AssignNode.new(ValueNode.new(val[0]), val[2], :object) }
| STRING ASSIGN Expression { result = AssignNode.new(ValueNode.new(LiteralNode.new(val[0])), val[2], :object) }
| Comment { result = val[0] }
;
# A return statement.
Return:
RETURN Expression { result = ReturnNode.new(val[1]) }
| RETURN { result = ReturnNode.new(ValueNode.new(Value.new('null'))) }
;
# A comment.
Comment:
COMMENT { result = CommentNode.new(val[0]) }
;
# Arithmetic and logical operators
# For Ruby's Operator precedence, see:
# https://www.cs.auckland.ac.nz/references/ruby/ProgrammingRuby/language.html
Operation:
'!' Expression { result = OpNode.new(val[0], val[1]) }
| '!!' Expression { result = OpNode.new(val[0], val[1]) }
| '-' Expression = UMINUS { result = OpNode.new(val[0], val[1]) }
| NOT Expression { result = OpNode.new(val[0], val[1]) }
| '~' Expression { result = OpNode.new(val[0], val[1]) }
| '--' Expression { result = OpNode.new(val[0], val[1]) }
| '++' Expression { result = OpNode.new(val[0], val[1]) }
| DELETE Expression { result = OpNode.new(val[0], val[1]) }
| TYPEOF Expression { result = OpNode.new(val[0], val[1]) }
| Expression '--' { result = OpNode.new(val[1], val[0], nil, true) }
| Expression '++' { result = OpNode.new(val[1], val[0], nil, true) }
| Expression '*' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '/' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '%' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '+' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '-' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '<<' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '>>' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '>>>' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '&' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '|' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '^' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '<=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '<' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '>' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '>=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '==' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '!=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression IS Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression ISNT Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '&&' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '||' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression AND Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression OR Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '-=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '+=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '/=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '*=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '%=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '||=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression '&&=' Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression INSTANCEOF Expression { result = OpNode.new(val[1], val[0], val[2]) }
| Expression IN Expression { result = OpNode.new(val[1], val[0], val[2]) }
;
# The existence operator.
Existence:
Expression '?' { result = ExistenceNode.new(val[0]) }
;
# Function definition.
Code:
ParamList "=>" Block { result = CodeNode.new(val[0], val[2]) }
| "=>" Block { result = CodeNode.new([], val[1]) }
;
# The parameters to a function definition.
ParamList:
Param { result = val }
| ParamList "," Param { result = val[0] << val[2] }
;
# A Parameter (or ParamSplat) in a function definition.
Param:
PARAM
| PARAM "." "." "." { result = ParamSplatNode.new(val[0]) }
;
# A regular splat.
Splat:
Expression "." "." "." { result = ArgSplatNode.new(val[0])}
;
# Expressions that can be treated as values.
Value:
IDENTIFIER { result = ValueNode.new(val[0]) }
| Literal { result = ValueNode.new(val[0]) }
| Array { result = ValueNode.new(val[0]) }
| Object { result = ValueNode.new(val[0]) }
| Parenthetical { result = ValueNode.new(val[0]) }
| Range { result = ValueNode.new(val[0]) }
| Value Accessor { result = val[0] << val[1] }
| Invocation Accessor { result = ValueNode.new(val[0], [val[1]]) }
;
# Accessing into an object or array, through dot or index notation.
Accessor:
PROPERTY_ACCESS IDENTIFIER { result = AccessorNode.new(val[1]) }
| PROTOTYPE_ACCESS IDENTIFIER { result = AccessorNode.new(val[1], true) }
| Index { result = val[0] }
| Range { result = SliceNode.new(val[0]) }
;
# Indexing into an object or array.
Index:
"[" Expression "]" { result = IndexNode.new(val[1]) }
;
# An object literal.
Object:
"{" AssignList "}" { result = ObjectNode.new(val[1]) }
;
# Assignment within an object literal (comma or newline separated).
AssignList:
/* nothing */ { result = [] }
| AssignObj { result = val }
| AssignList "," AssignObj { result = val[0] << val[2] }
| AssignList Terminator AssignObj { result = val[0] << val[2] }
| AssignList ","
Terminator AssignObj { result = val[0] << val[3] }
| INDENT AssignList OUTDENT { result = val[1] }
;
# All flavors of function call (instantiation, super, and regular).
Call:
Invocation { result = val[0] }
| NEW Invocation { result = val[1].new_instance }
| Super { result = val[0] }
;
# Extending an object's prototype.
Extends:
Value EXTENDS Value { result = ExtendsNode.new(val[0], val[2]) }
;
# A generic function invocation.
Invocation:
Value Arguments { result = CallNode.new(val[0], val[1]) }
| Invocation Arguments { result = CallNode.new(val[0], val[1]) }
# | Invocation Code { result = val[0] << val[1] }
;
# The list of arguments to a function invocation.
Arguments:
"(" ArgList ")" { result = val[1] }
| "(" ArgList ")" Code { result = val[1] << val[3] }
;
# Calling super.
Super:
SUPER "(" ArgList ")" { result = CallNode.new(:super, val[2]) }
;
# The range literal.
Range:
"[" Expression
"." "." Expression "]" { result = RangeNode.new(val[1], val[4]) }
| "[" Expression
"." "." "." Expression "]" { result = RangeNode.new(val[1], val[5], true) }
;
# The array literal.
Array:
"[" ArgList "]" { result = ArrayNode.new(val[1]) }
;
# A list of arguments to a method call, or as the contents of an array.
ArgList:
/* nothing */ { result = [] }
| Expression { result = val }
| INDENT Expression { result = [val[1]] }
| ArgList "," Expression { result = val[0] << val[2] }
| ArgList Terminator Expression { result = val[0] << val[2] }
| ArgList "," Terminator Expression { result = val[0] << val[3] }
| ArgList "," INDENT Expression { result = val[0] << val[3] }
| ArgList OUTDENT { result = val[0] }
;
# Try/catch/finally exception handling blocks.
Try:
TRY Block Catch { result = TryNode.new(val[1], val[2][0], val[2][1]) }
| TRY Block FINALLY Block { result = TryNode.new(val[1], nil, nil, val[3]) }
| TRY Block Catch
FINALLY Block { result = TryNode.new(val[1], val[2][0], val[2][1], val[4]) }
;
# A catch clause.
Catch:
CATCH IDENTIFIER Block { result = [val[1], val[2]] }
;
# Throw an exception.
Throw:
THROW Expression { result = ThrowNode.new(val[1]) }
;
# Parenthetical expressions.
Parenthetical:
"(" Expression ")" { result = ParentheticalNode.new(val[1], val[0].line) }
;
# The while loop. (there is no do..while).
While:
WHILE Expression Block { result = WhileNode.new(val[1], val[2]) }
| WHILE Expression { result = WhileNode.new(val[1], nil) }
;
# Array comprehensions, including guard and current index.
# Looks a little confusing, check nodes.rb for the arguments to ForNode.
For:
Expression FOR
ForVariables ForSource { result = ForNode.new(val[0], val[3], val[2][0], val[2][1]) }
| FOR ForVariables ForSource Block { result = ForNode.new(val[3], val[2], val[1][0], val[1][1]) }
;
# An array comprehension has variables for the current element and index.
ForVariables:
IDENTIFIER { result = val }
| IDENTIFIER "," IDENTIFIER { result = [val[0], val[2]] }
;
# The source of the array comprehension can optionally be filtered.
ForSource:
IN Expression { result = {:source => val[1]} }
| OF Expression { result = {:source => val[1], :object => true} }
| ForSource
WHEN Expression { result = val[0].merge(:filter => val[2]) }
| ForSource
BY Expression { result = val[0].merge(:step => val[2]) }
;
# Switch/When blocks.
Switch:
SWITCH Expression INDENT
Whens OUTDENT { result = val[3].rewrite_condition(val[1]) }
| SWITCH Expression INDENT
Whens ELSE Block OUTDENT { result = val[3].rewrite_condition(val[1]).add_else(val[5]) }
;
# The inner list of whens.
Whens:
When { result = val[0] }
| Whens When { result = val[0] << val[1] }
;
# An individual when.
When:
LEADING_WHEN Expression Block { result = IfNode.new(val[1], val[2], nil, {:statement => true}) }
| LEADING_WHEN Expression Block
Terminator { result = IfNode.new(val[1], val[2], nil, {:statement => true}) }
| Comment Terminator When { result = val[2].add_comment(val[0]) }
;
# The most basic form of "if".
IfBlock:
IF Expression Block { result = IfNode.new(val[1], val[2]) }
;
# An elsif portion of an if-else block.
ElsIf:
ELSE IfBlock { result = val[1].force_statement }
;
# Multiple elsifs can be chained together.
ElsIfs:
ElsIf { result = val[0] }
| ElsIfs ElsIf { result = val[0].add_else(val[1]) }
;
# Terminating else bodies are strictly optional.
ElseBody
/* nothing */ { result = nil }
| ELSE Block { result = val[1] }
;
# All the alternatives for ending an if-else block.
IfEnd:
ElseBody { result = val[0] }
| ElsIfs ElseBody { result = val[0].add_else(val[1]) }
;
# The full complement of if blocks, including postfix one-liner ifs and unlesses.
If:
IfBlock IfEnd { result = val[0].add_else(val[1]) }
| Expression IF Expression { result = IfNode.new(val[2], Expressions.wrap(val[0]), nil, {:statement => true}) }
| Expression UNLESS Expression { result = IfNode.new(val[2], Expressions.wrap(val[0]), nil, {:statement => true, :invert => true}) }
;
end
---- header
module CoffeeScript
---- inner
# Lex and parse a CoffeeScript.
def parse(code)
# Uncomment the following line to enable grammar debugging, in combination
# with the -g flag in the Rake build task.
# @yydebug = true
@tokens = Lexer.new.tokenize(code)
do_parse
end
# Retrieve the next token from the list.
def next_token
@tokens.shift
end
# Raise a custom error class that knows about line numbers.
def on_error(error_token_id, error_value, value_stack)
raise ParseError.new(token_to_str(error_token_id), error_value, value_stack)
end
---- footer
end

View File

@@ -1,240 +0,0 @@
module CoffeeScript
# The lexer reads a stream of CoffeeScript and divvys it up into tagged
# tokens. A minor bit of the ambiguity in the grammar has been avoided by
# pushing some extra smarts into the Lexer.
class Lexer
# The list of keywords passed verbatim to the parser.
KEYWORDS = ["if", "else", "then", "unless",
"true", "false", "yes", "no", "on", "off",
"and", "or", "is", "isnt", "not",
"new", "return",
"try", "catch", "finally", "throw",
"break", "continue",
"for", "in", "of", "by", "where", "while",
"switch", "when",
"super", "extends",
"arguments",
"delete", "instanceof", "typeof"]
# Token matching regexes.
IDENTIFIER = /\A([a-zA-Z$_](\w|\$)*)/
NUMBER = /\A(\b((0(x|X)[0-9a-fA-F]+)|([0-9]+(\.[0-9]+)?(e[+\-]?[0-9]+)?)))\b/i
STRING = /\A(""|''|"(.*?)([^\\]|\\\\)"|'(.*?)([^\\]|\\\\)')/m
JS = /\A(``|`(.*?)([^\\]|\\\\)`)/m
OPERATOR = /\A([+\*&|\/\-%=<>:!]+)/
WHITESPACE = /\A([ \t]+)/
COMMENT = /\A(((\n?[ \t]*)?#.*$)+)/
CODE = /\A(=>)/
REGEX = /\A(\/(.*?)([^\\]|\\\\)\/[imgy]{0,4})/
MULTI_DENT = /\A((\n([ \t]*))+)(\.)?/
LAST_DENT = /\n([ \t]*)/
ASSIGNMENT = /\A(:|=)\Z/
# Token cleaning regexes.
JS_CLEANER = /(\A`|`\Z)/
MULTILINER = /\n/
COMMENT_CLEANER = /(^\s*#|\n\s*$)/
NO_NEWLINE = /\A([+\*&|\/\-%=<>:!.\\][<>=&|]*|and|or|is|isnt|not|delete|typeof|instanceof)\Z/
# Tokens which a regular expression will never immediately follow, but which
# a division operator might.
# See: http://www.mozilla.org/js/language/js20-2002-04/rationale/syntax.html#regular-expressions
NOT_REGEX = [
:IDENTIFIER, :NUMBER, :REGEX, :STRING,
')', '++', '--', ']', '}',
:FALSE, :NULL, :THIS, :TRUE
]
# Scan by attempting to match tokens one character at a time. Slow and steady.
def tokenize(code)
@code = code.chomp # Cleanup code by remove extra line breaks
@i = 0 # Current character position we're parsing
@line = 1 # The current line.
@indent = 0 # The current indent level.
@indents = [] # The stack of all indent levels we are currently within.
@tokens = [] # Collection of all parsed tokens in the form [:TOKEN_TYPE, value]
while @i < @code.length
@chunk = @code[@i..-1]
extract_next_token
end
puts "original stream: #{@tokens.inspect}" if ENV['VERBOSE']
close_indentation
Rewriter.new.rewrite(@tokens)
end
# At every position, run through this list of attempted matches,
# short-circuiting if any of them succeed.
def extract_next_token
return if identifier_token
return if number_token
return if string_token
return if js_token
return if regex_token
return if indent_token
return if comment_token
return if whitespace_token
return literal_token
end
# Tokenizers ==========================================================
# Matches identifying literals: variables, keywords, method names, etc.
def identifier_token
return false unless identifier = @chunk[IDENTIFIER, 1]
# Keywords are special identifiers tagged with their own name,
# 'if' will result in an [:IF, "if"] token.
tag = KEYWORDS.include?(identifier) ? identifier.upcase.to_sym : :IDENTIFIER
tag = :LEADING_WHEN if tag == :WHEN && [:OUTDENT, :INDENT, "\n"].include?(last_tag)
@tokens[-1][0] = :PROPERTY_ACCESS if tag == :IDENTIFIER && last_value == '.' && !(@tokens[-2][1] == '.')
@tokens[-1][0] = :PROTOTYPE_ACCESS if tag == :IDENTIFIER && last_value == '::'
token(tag, identifier)
@i += identifier.length
end
# Matches numbers, including decimals, hex, and exponential notation.
def number_token
return false unless number = @chunk[NUMBER, 1]
token(:NUMBER, number)
@i += number.length
end
# Matches strings, including multi-line strings.
def string_token
return false unless string = @chunk[STRING, 1]
escaped = string.gsub(MULTILINER) do |match|
@line += 1
" \\\n"
end
token(:STRING, escaped)
@i += string.length
end
# Matches interpolated JavaScript.
def js_token
return false unless script = @chunk[JS, 1]
token(:JS, script.gsub(JS_CLEANER, ''))
@i += script.length
end
# Matches regular expression literals.
def regex_token
return false unless regex = @chunk[REGEX, 1]
return false if NOT_REGEX.include?(last_tag)
token(:REGEX, regex)
@i += regex.length
end
# Matches and consumes comments.
def comment_token
return false unless comment = @chunk[COMMENT, 1]
@line += comment.scan(MULTILINER).length
token(:COMMENT, comment.gsub(COMMENT_CLEANER, '').split(MULTILINER))
token("\n", "\n")
@i += comment.length
end
# Record tokens for indentation differing from the previous line.
def indent_token
return false unless indent = @chunk[MULTI_DENT, 1]
@line += indent.scan(MULTILINER).size
@i += indent.size
next_character = @chunk[MULTI_DENT, 4]
no_newlines = next_character == '.' || (last_value.to_s.match(NO_NEWLINE) && last_value != "=>")
return suppress_newlines(indent) if no_newlines
size = indent.scan(LAST_DENT).last.last.length
return newline_token(indent) if size == @indent
if size > @indent
token(:INDENT, size - @indent)
@indents << (size - @indent)
else
outdent_token(@indent - size)
end
@indent = size
end
# Record an oudent token or tokens, if we're moving back inwards past
# multiple recorded indents.
def outdent_token(move_out)
while move_out > 0 && !@indents.empty?
last_indent = @indents.pop
token(:OUTDENT, last_indent)
move_out -= last_indent
end
token("\n", "\n")
end
# Matches and consumes non-meaningful whitespace.
def whitespace_token
return false unless whitespace = @chunk[WHITESPACE, 1]
@i += whitespace.length
end
# Multiple newlines get merged together.
# Use a trailing \ to escape newlines.
def newline_token(newlines)
token("\n", "\n") unless last_value == "\n"
true
end
# Tokens to explicitly escape newlines are removed once their job is done.
def suppress_newlines(newlines)
@tokens.pop if last_value == "\\"
true
end
# We treat all other single characters as a token. Eg.: ( ) , . !
# Multi-character operators are also literal tokens, so that Racc can assign
# the proper order of operations.
def literal_token
value = @chunk[OPERATOR, 1]
tag_parameters if value && value.match(CODE)
value ||= @chunk[0,1]
tag = value.match(ASSIGNMENT) ? :ASSIGN : value
token(tag, value)
@i += value.length
end
# Helpers ==========================================================
# Add a token to the results, taking note of the line number, and
# immediately-preceding comment.
def token(tag, value)
@tokens << [tag, Value.new(value, @line)]
end
# Peek at the previous token's value.
def last_value
@tokens.last && @tokens.last[1]
end
# Peek at the previous token's tag.
def last_tag
@tokens.last && @tokens.last[0]
end
# A source of ambiguity in our grammar was parameter lists in function
# definitions (as opposed to argument lists in function calls). Tag
# parameter identifiers in order to avoid this. Also, parameter lists can
# make use of splats.
def tag_parameters
i = 0
loop do
i -= 1
tok = @tokens[i]
return if !tok
next if ['.', ','].include?(tok[0])
return if tok[0] != :IDENTIFIER
tok[0] = :PARAM
end
end
# Close up all remaining open blocks. IF the first token is an indent,
# axe it.
def close_indentation
outdent_token(@indent)
end
end
end

View File

@@ -1,62 +0,0 @@
# This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.coffee
# Executes the `coffee` Ruby program to convert from CoffeeScript
# to Javascript. Eventually this will hopefully happen entirely within JS.
# Require external dependencies.
OS: require('os')
File: require('file')
Readline: require('readline')
# The path to the CoffeeScript Compiler.
coffeePath: File.path(module.path).dirname().dirname().dirname().dirname().dirname().join('bin', 'coffee')
# Our general-purpose error handler.
checkForErrors: coffeeProcess =>
return true if coffeeProcess.wait() is 0
system.stderr.print(coffeeProcess.stderr.read())
throw new Error("CoffeeScript compile error")
# Run a simple REPL, round-tripping to the CoffeeScript compiler for every
# command.
exports.run: args =>
if args.length
for path, i in args
exports.evalCS(File.read(path))
delete args[i]
return true
while true
try
system.stdout.write('coffee> ').flush()
result: exports.evalCS(Readline.readline(), ['--globals'])
print(result) if result isnt undefined
catch e
print(e)
# Compile a given CoffeeScript file into JavaScript.
exports.compileFile: path =>
coffee: OS.popen([coffeePath, "--print", "--no-wrap", path])
checkForErrors(coffee)
coffee.stdout.read()
# Compile a string of CoffeeScript into JavaScript.
exports.compile: source, flags =>
coffee: OS.popen([coffeePath, "--eval", "--no-wrap"].concat(flags or []))
coffee.stdin.write(source).flush().close()
checkForErrors(coffee)
coffee.stdout.read()
# Evaluating a string of CoffeeScript first compiles it externally.
exports.evalCS: source, flags =>
eval(exports.compile(source, flags))
# Make a factory for the CoffeeScript environment.
exports.makeNarwhalFactory: path =>
code: exports.compileFile(path)
factoryText: "function(require,exports,module,system,print){" + code + "/**/\n}"
if system.engine is "rhino"
Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null)
else
# eval requires parentheses, but parentheses break compileFunction.
eval("(" + factoryText + ")")

View File

@@ -1,77 +0,0 @@
(function(){
var File, OS, Readline, checkForErrors, coffeePath;
// This (javascript) file is generated from lib/coffee_script/narwhal/coffee-script.coffee
// Executes the `coffee` Ruby program to convert from CoffeeScript
// to Javascript. Eventually this will hopefully happen entirely within JS.
// Require external dependencies.
OS = require('os');
File = require('file');
Readline = require('readline');
// The path to the CoffeeScript Compiler.
coffeePath = File.path(module.path).dirname().dirname().dirname().dirname().dirname().join('bin', 'coffee');
// Our general-purpose error handler.
checkForErrors = function checkForErrors(coffeeProcess) {
if (coffeeProcess.wait() === 0) {
return true;
}
system.stderr.print(coffeeProcess.stderr.read());
throw new Error("CoffeeScript compile error");
};
// Run a simple REPL, round-tripping to the CoffeeScript compiler for every
// command.
exports.run = function run(args) {
var __a, i, path, result;
if (args.length) {
__a = args;
for (i=0; i<__a.length; i++) {
path = __a[i];
exports.evalCS(File.read(path));
delete args[i];
}
return true;
}
while (true) {
try {
system.stdout.write('coffee> ').flush();
result = exports.evalCS(Readline.readline(), ['--globals']);
if (result !== undefined) {
print(result);
}
} catch (e) {
print(e);
}
}
return null;
};
// Compile a given CoffeeScript file into JavaScript.
exports.compileFile = function compileFile(path) {
var coffee;
coffee = OS.popen([coffeePath, "--print", "--no-wrap", path]);
checkForErrors(coffee);
return coffee.stdout.read();
};
// Compile a string of CoffeeScript into JavaScript.
exports.compile = function compile(source, flags) {
var coffee;
coffee = OS.popen([coffeePath, "--eval", "--no-wrap"].concat(flags || []));
coffee.stdin.write(source).flush().close();
checkForErrors(coffee);
return coffee.stdout.read();
};
// Evaluating a string of CoffeeScript first compiles it externally.
exports.evalCS = function evalCS(source, flags) {
return eval(exports.compile(source, flags));
};
// Make a factory for the CoffeeScript environment.
exports.makeNarwhalFactory = function makeNarwhalFactory(path) {
var code, factoryText;
code = exports.compileFile(path);
factoryText = "function(require,exports,module,system,print){" + code + "/**/\n}";
if (system.engine === "rhino") {
return Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(global, factoryText, path, 0, null);
} else {
// eval requires parentheses, but parentheses break compileFunction.
return eval("(" + factoryText + ")");
}
};
})();

View File

@@ -1,21 +0,0 @@
(function(){
var coffeescript, factories, loader;
// This (javascript) file is generated from lib/coffee_script/narwhal/loader.coffee
coffeescript = null;
factories = {
};
loader = {
// Reload the coffee-script environment from source.
reload: function reload(topId, path) {
coffeescript = coffeescript || require('coffee-script');
return factories[topId] = function() {
return coffeescript.makeNarwhalFactory(path);
};
},
// Ensure that the coffee-script environment is loaded.
load: function load(topId, path) {
return factories[topId] = factories[topId] || this.reload(topId, path);
}
};
require.loader.loaders.unshift([".coffee", loader]);
})();

View File

@@ -1,19 +0,0 @@
# This (javascript) file is generated from lib/coffee_script/narwhal/loader.coffee
coffeescript: null
factories: {}
loader: {
# Reload the coffee-script environment from source.
reload: topId, path =>
coffeescript ||= require('coffee-script')
factories[topId]: => coffeescript.makeNarwhalFactory(path)
# Ensure that the coffee-script environment is loaded.
load: topId, path =>
factories[topId] ||= this.reload(topId, path)
}
require.loader.loaders.unshift([".coffee", loader])

View File

@@ -1,858 +0,0 @@
module CoffeeScript
# The abstract base class for all CoffeeScript nodes.
# All nodes are implement a "compile_node" method, which performs the
# code generation for that node. To compile a node, call the "compile"
# method, which wraps "compile_node" in some extra smarts, to know when the
# generated code should be wrapped up in a closure. An options hash is passed
# and cloned throughout, containing messages from higher in the AST,
# information about the current scope, and indentation level.
class Node
# Tabs are two spaces for pretty-printing.
TAB = ' '
# Tag this node as a statement, meaning that it can't be used directly as
# the result of an expression.
def self.statement
class_eval "def statement?; true; end"
end
# Tag this node as a statement that cannot be transformed into an expression.
# (break, continue, etc.) It doesn't make sense to try to transform it.
def self.statement_only
statement
class_eval "def statement_only?; true; end"
end
def write(code)
puts "#{self.class.to_s}:\n#{@options.inspect}\n#{code}\n\n" if ENV['VERBOSE']
code
end
# This is extremely important -- we convert JS statements into expressions
# by wrapping them in a closure, only if it's possible, and we're not at
# the top level of a block (which would be unnecessary), and we haven't
# already been asked to return the result.
def compile(o={})
@options = o.dup
@indent = o[:indent]
top = self.is_a?(ForNode) ? @options[:top] : @options.delete(:top)
closure = statement? && !statement_only? && !top && !@options[:return]
closure ? compile_closure(@options) : compile_node(@options)
end
def compile_closure(o={})
indent = o[:indent]
@indent = (o[:indent] = idt(1))
"(function() {\n#{compile_node(o.merge(:return => true))}\n#{indent}})()"
end
# Quick short method for the current indentation level, plus tabbing in.
def idt(tabs=0)
@indent + (TAB * tabs)
end
# Default implementations of the common node methods.
def unwrap; self; end
def statement?; false; end
def statement_only?; false; end
end
# A collection of nodes, each one representing an expression.
class Expressions < Node
statement
attr_reader :expressions
TRAILING_WHITESPACE = /\s+$/
UPPERCASE = /[A-Z]/
# Wrap up a node as an Expressions, unless it already is.
def self.wrap(*nodes)
return nodes[0] if nodes.length == 1 && nodes[0].is_a?(Expressions)
Expressions.new(*nodes)
end
def initialize(*nodes)
@expressions = nodes.flatten
end
# Tack an expression on to the end of this expression list.
def <<(node)
@expressions << node
self
end
# Tack an expression on to the beginning of this expression list.
def unshift(node)
@expressions.unshift(node)
self
end
# If this Expressions consists of a single node, pull it back out.
def unwrap
@expressions.length == 1 ? @expressions.first : self
end
# Is the node last in this block of expressions.
def last?(node)
@last_index ||= @expressions.last.is_a?(CommentNode) ? -2 : -1
node == @expressions[@last_index]
end
# Determine if this is the expressions body within a constructor function.
# Constructors are capitalized by CoffeeScript convention.
def constructor?(o)
o[:top] && o[:last_assign] && o[:last_assign][0..0][UPPERCASE]
end
def compile(o={})
o[:scope] ? super(o) : compile_root(o)
end
# Compile each expression in the Expressions body.
def compile_node(options={})
write(@expressions.map {|n| compile_expression(n, options.dup) }.join("\n"))
end
# If this is the top-level Expressions, wrap everything in a safety closure.
def compile_root(o={})
indent = o[:no_wrap] ? '' : TAB
@indent = indent
o.merge!(:indent => indent, :scope => Scope.new(nil, self))
code = o[:globals] ? compile_node(o) : compile_with_declarations(o)
code.gsub!(TRAILING_WHITESPACE, '')
write(o[:no_wrap] ? code : "(function(){\n#{code}\n})();")
end
# Compile the expressions body, with declarations of all inner variables
# at the top.
def compile_with_declarations(o={})
code = compile_node(o)
return code unless o[:scope].declarations?(self)
write("#{idt}var #{o[:scope].declared_variables.join(', ')};\n#{code}")
end
# Compiles a single expression within the expression list.
def compile_expression(node, o)
@indent = o[:indent]
stmt = node.statement?
# We need to return the result if this is the last node in the expressions body.
returns = o.delete(:return) && last?(node) && !node.statement_only?
# Return the regular compile of the node, unless we need to return the result.
return "#{stmt ? '' : idt}#{node.compile(o.merge(:top => true))}#{stmt ? '' : ';'}" unless returns
# If it's a statement, the node knows how to return itself.
return node.compile(o.merge(:return => true)) if node.statement?
# If it's not part of a constructor, we can just return the value of the expression.
return "#{idt}return #{node.compile(o)};" unless constructor?(o)
# It's the last line of a constructor, add a safety check.
temp = o[:scope].free_variable
"#{idt}#{temp} = #{node.compile(o)};\n#{idt}return #{o[:last_assign]} === this.constructor ? this : #{temp};"
end
end
# Literals are static values that have a Ruby representation, eg.: a string, a number,
# true, false, nil, etc.
class LiteralNode < Node
# Values of a literal node that much be treated as a statement -- no
# sense returning or assigning them.
STATEMENTS = ['break', 'continue']
# If we get handed a literal reference to an arguments object, convert
# it to an array.
ARG_ARRAY = 'Array.prototype.slice.call(arguments, 0)'
attr_reader :value
# Wrap up a compiler-generated string as a LiteralNode.
def self.wrap(string)
self.new(Value.new(string))
end
def initialize(value)
@value = value
end
def statement?
STATEMENTS.include?(@value.to_s)
end
alias_method :statement_only?, :statement?
def compile_node(o)
@value = ARG_ARRAY if @value.to_s.to_sym == :arguments
indent = statement? ? idt : ''
ending = statement? ? ';' : ''
write "#{indent}#{@value}#{ending}"
end
end
# Return an expression, or wrap it in a closure and return it.
class ReturnNode < Node
statement_only
attr_reader :expression
def initialize(expression)
@expression = expression
end
def compile_node(o)
return write(@expression.compile(o.merge(:return => true))) if @expression.statement?
compiled = @expression.compile(o)
write(@expression.statement? ? "#{compiled}\n#{idt}return null;" : "#{idt}return #{compiled};")
end
end
# Pass through CoffeeScript comments into JavaScript comments at the
# same position.
class CommentNode < Node
statement_only
def initialize(lines)
@lines = lines.value
end
def compile_node(o={})
delimiter = "\n#{idt}//"
write("#{delimiter}#{@lines.join(delimiter)}")
end
end
# Node for a function invocation. Takes care of converting super() calls into
# calls against the prototype's function of the same name.
class CallNode < Node
attr_reader :variable, :arguments
def initialize(variable, arguments=[])
@variable, @arguments = variable, arguments
end
def new_instance
@new = true
self
end
def super?
@variable == :super
end
def prefix
@new ? "new " : ''
end
def splat?
@arguments.any? {|a| a.is_a?(ArgSplatNode) }
end
def <<(argument)
@arguments << argument
end
# Compile a vanilla function call.
def compile_node(o)
return write(compile_splat(o)) if splat?
args = @arguments.map{|a| a.compile(o) }.join(', ')
return write(compile_super(args, o)) if super?
write("#{prefix}#{@variable.compile(o)}(#{args})")
end
# Compile a call against the superclass's implementation of the current function.
def compile_super(args, o)
methname = o[:last_assign]
arg_part = args.empty? ? '' : ", #{args}"
meth = o[:proto_assign] ? "#{o[:proto_assign]}.__superClass__.#{methname}" :
"#{methname}.__superClass__.constructor"
"#{meth}.call(this#{arg_part})"
end
# Compile a function call being passed variable arguments.
def compile_splat(o)
meth = @variable.compile(o)
obj = @variable.source || 'this'
args = @arguments.map do |arg|
code = arg.compile(o)
code = arg.is_a?(ArgSplatNode) ? code : "[#{code}]"
arg.equal?(@arguments.first) ? code : ".concat(#{code})"
end
"#{prefix}#{meth}.apply(#{obj}, #{args.join('')})"
end
end
# Node to extend an object's prototype with an ancestor object.
# After goog.inherits from the Closure Library.
class ExtendsNode < Node
statement
attr_reader :sub_object, :super_object
def initialize(sub_object, super_object)
@sub_object, @super_object = sub_object, super_object
end
# Hooking one constructor into another's prototype chain.
def compile_node(o={})
constructor = o[:scope].free_variable
sub, sup = @sub_object.compile(o), @super_object.compile(o)
"#{idt}#{constructor} = function(){};\n#{idt}" +
"#{constructor}.prototype = #{sup}.prototype;\n#{idt}" +
"#{sub}.__superClass__ = #{sup}.prototype;\n#{idt}" +
"#{sub}.prototype = new #{constructor}();\n#{idt}" +
"#{sub}.prototype.constructor = #{sub};"
end
end
# A value, indexed or dotted into, or vanilla.
class ValueNode < Node
attr_reader :base, :properties, :last, :source
def initialize(base, properties=[])
@base, @properties = base, properties
end
def <<(other)
@properties << other
self
end
def properties?
return !@properties.empty?
end
# Values are statements if their base is a statement.
def statement?
@base.is_a?(Node) && @base.statement? && !properties?
end
def compile_node(o)
only = o.delete(:only_first)
props = only ? @properties[0...-1] : @properties
parts = [@base, props].flatten.map {|val| val.compile(o) }
@last = parts.last
@source = parts.length > 1 ? parts[0...-1].join('') : nil
write(parts.join(''))
end
end
# A dotted accessor into a part of a value, or the :: shorthand for
# an accessor into the object's prototype.
class AccessorNode < Node
attr_reader :name
def initialize(name, prototype=false)
@name, @prototype = name, prototype
end
def compile_node(o)
proto = @prototype ? "prototype." : ''
write(".#{proto}#{@name}")
end
end
# An indexed accessor into a part of an array or object.
class IndexNode < Node
attr_reader :index
def initialize(index)
@index = index
end
def compile_node(o)
write("[#{@index.compile(o)}]")
end
end
# A range literal. Ranges can be used to extract portions (slices) of arrays,
# or to specify a range for array comprehensions.
class RangeNode < Node
attr_reader :from, :to
def initialize(from, to, exclusive=false)
@from, @to, @exclusive = from, to, exclusive
end
def exclusive?
@exclusive
end
def compile_variables(o)
@indent = o[:indent]
@from_var, @to_var = o[:scope].free_variable, o[:scope].free_variable
from_val, to_val = @from.compile(o), @to.compile(o)
write("#{@from_var} = #{from_val}; #{@to_var} = #{to_val};\n#{idt}")
end
def compile_node(o)
return compile_array(o) unless o[:index]
idx, step = o.delete(:index), o.delete(:step)
vars = "#{idx}=#{@from_var}"
step = step ? step.compile(o) : '1'
equals = @exclusive ? '' : '='
compare = "(#{@from_var} <= #{@to_var} ? #{idx} <#{equals} #{@to_var} : #{idx} >#{equals} #{@to_var})"
incr = "(#{@from_var} <= #{@to_var} ? #{idx} += #{step} : #{idx} -= #{step})"
write("#{vars}; #{compare}; #{incr}")
end
# Expand the range into the equivalent array, if it's not being used as
# part of a comprehension, slice, or splice.
# TODO: This generates pretty ugly code ... shrink it.
def compile_array(o)
body = Expressions.wrap(LiteralNode.wrap('i'))
arr = Expressions.wrap(ForNode.new(body, {:source => ValueNode.new(self)}, Value.new('i')))
ParentheticalNode.new(CallNode.new(CodeNode.new([], arr))).compile(o)
end
end
# An array slice literal. Unlike JavaScript's Array#slice, the second parameter
# specifies the index of the end of the slice (just like the first parameter)
# is the index of the beginning.
class SliceNode < Node
attr_reader :range
def initialize(range)
@range = range
end
def compile_node(o)
from = @range.from.compile(o)
to = @range.to.compile(o)
plus_part = @range.exclusive? ? '' : ' + 1'
write(".slice(#{from}, #{to}#{plus_part})")
end
end
# Setting the value of a local variable, or the value of an object property.
class AssignNode < Node
PROTO_ASSIGN = /\A(\S+)\.prototype/
LEADING_DOT = /\A\.(prototype\.)?/
attr_reader :variable, :value, :context
def initialize(variable, value, context=nil)
@variable, @value, @context = variable, value, context
end
def compile_node(o)
return compile_splice(o) if @variable.properties.last.is_a?(SliceNode)
name = @variable.compile(o)
last = @variable.last.to_s.sub(LEADING_DOT, '')
proto = name[PROTO_ASSIGN, 1]
o = o.merge(:last_assign => last, :proto_assign => proto)
o[:immediate_assign] = last if @value.is_a?(CodeNode) && last.match(Lexer::IDENTIFIER)
return write("#{name}: #{@value.compile(o)}") if @context == :object
o[:scope].find(name) unless @variable.properties?
val = "#{name} = #{@value.compile(o)}"
write(o[:return] ? "#{idt}return (#{val})" : val)
end
def compile_splice(o)
var = @variable.compile(o.merge(:only_first => true))
range = @variable.properties.last.range
plus = range.exclusive? ? '' : ' + 1'
from = range.from.compile(o)
to = "#{range.to.compile(o)} - #{from}#{plus}"
write("#{var}.splice.apply(#{var}, [#{from}, #{to}].concat(#{@value.compile(o)}))")
end
end
# Simple Arithmetic and logical operations. Performs some conversion from
# CoffeeScript operations into their JavaScript equivalents.
class OpNode < Node
CONVERSIONS = {
:== => "===",
:'!=' => "!==",
:and => '&&',
:or => '||',
:is => '===',
:isnt => "!==",
:not => '!'
}
CONDITIONALS = [:'||=', :'&&=']
PREFIX_OPERATORS = [:typeof, :delete]
attr_reader :operator, :first, :second
def initialize(operator, first, second=nil, flip=false)
@first, @second, @flip = first, second, flip
@operator = CONVERSIONS[operator.to_sym] || operator
end
def unary?
@second.nil?
end
def compile_node(o)
return write(compile_conditional(o)) if CONDITIONALS.include?(@operator.to_sym)
return write(compile_unary(o)) if unary?
write("#{@first.compile(o)} #{@operator} #{@second.compile(o)}")
end
def compile_conditional(o)
first, second = @first.compile(o), @second.compile(o)
sym = @operator[0..1]
"#{first} = #{first} #{sym} #{second}"
end
def compile_unary(o)
space = PREFIX_OPERATORS.include?(@operator.to_sym) ? ' ' : ''
parts = [@operator.to_s, space, @first.compile(o)]
parts.reverse! if @flip
parts.join('')
end
end
# A function definition. The only node that creates a new Scope.
class CodeNode < Node
attr_reader :params, :body
def initialize(params, body)
@params = params
@body = body
end
def compile_node(o)
shared_scope = o.delete(:shared_scope)
o[:scope] = shared_scope || Scope.new(o[:scope], @body)
o[:return] = true
o[:top] = true
o[:indent] = idt(1)
o.delete(:no_wrap)
o.delete(:globals)
name = o.delete(:immediate_assign)
if @params.last.is_a?(ParamSplatNode)
splat = @params.pop
splat.index = @params.length
@body.unshift(splat)
end
@params.each {|id| o[:scope].parameter(id.to_s) }
code = @body.compile_with_declarations(o)
name_part = name ? " #{name}" : ''
write("function#{name_part}(#{@params.join(', ')}) {\n#{code}\n#{idt}}")
end
end
# A parameter splat in a function definition.
class ParamSplatNode < Node
attr_accessor :index
attr_reader :name
def initialize(name)
@name = name
end
def compile_node(o={})
o[:scope].find(@name)
write("#{@name} = Array.prototype.slice.call(arguments, #{@index})")
end
end
class ArgSplatNode < Node
attr_reader :value
def initialize(value)
@value = value
end
def compile_node(o={})
write(@value.compile(o))
end
end
# An object literal.
class ObjectNode < Node
attr_reader :properties
def initialize(properties = [])
@properties = properties
end
# All the mucking about with commas is to make sure that CommentNodes and
# AssignNodes get interleaved correctly, with no trailing commas or
# commas affixed to comments. TODO: Extract this and add it to ArrayNode.
def compile_node(o)
o[:indent] = idt(1)
joins = Hash.new("\n")
non_comments = @properties.select {|p| !p.is_a?(CommentNode) }
non_comments.each {|p| joins[p] = p == non_comments.last ? "\n" : ",\n" }
props = @properties.map { |prop|
join = joins[prop]
join = '' if prop == @properties.last
indent = prop.is_a?(CommentNode) ? '' : idt(1)
"#{indent}#{prop.compile(o)}#{join}"
}.join('')
write("{\n#{props}\n#{idt}}")
end
end
# An array literal.
class ArrayNode < Node
attr_reader :objects
def initialize(objects=[])
@objects = objects
end
def compile_node(o)
o[:indent] = idt(1)
objects = @objects.map { |obj|
code = obj.compile(o)
obj.is_a?(CommentNode) ? "\n#{code}\n#{o[:indent]}" :
obj == @objects.last ? code : "#{code}, "
}.join('')
ending = objects.include?("\n") ? "\n#{idt}]" : ']'
write("[#{objects}#{ending}")
end
end
# A while loop, the only sort of low-level loop exposed by CoffeeScript. From
# it, all other loops can be manufactured.
class WhileNode < Node
statement
attr_reader :condition, :body
def initialize(condition, body)
@condition, @body = condition, body
end
def compile_node(o)
returns = o.delete(:return)
o[:indent] = idt(1)
o[:top] = true
cond = @condition.compile(o)
post = returns ? "\n#{idt}return null;" : ''
return write("#{idt}while (#{cond}) null;#{post}") if @body.nil?
write("#{idt}while (#{cond}) {\n#{@body.compile(o)}\n#{idt}}#{post}")
end
end
# The replacement for the for loop is an array comprehension (that compiles)
# into a for loop. Also acts as an expression, able to return the result
# of the comprehenion. Unlike Python array comprehensions, it's able to pass
# the current index of the loop as a second parameter.
class ForNode < Node
statement
attr_reader :body, :source, :name, :index, :filter, :step
def initialize(body, source, name, index=nil)
@body, @name, @index = body, name, index
@source = source[:source]
@filter = source[:filter]
@step = source[:step]
@object = !!source[:object]
@name, @index = @index, @name if @object
end
def compile_node(o)
top_level = o.delete(:top) && !o[:return]
range = @source.is_a?(ValueNode) && @source.base.is_a?(RangeNode) && @source.properties.empty?
source = range ? @source.base : @source
scope = o[:scope]
name_found = @name && scope.find(@name)
index_found = @index && scope.find(@index)
body_dent = idt(1)
svar = scope.free_variable
ivar = range ? name : @index ? @index : scope.free_variable
rvar = scope.free_variable unless top_level
if range
index_var = scope.free_variable
source_part = source.compile_variables(o)
for_part = "#{index_var}=0, #{source.compile(o.merge(:index => ivar, :step => @step))}, #{index_var}++"
var_part = ''
else
index_var = nil
source_part = "#{svar} = #{source.compile(o)};\n#{idt}"
for_part = @object ? "#{ivar} in #{svar}" : "#{ivar}=0; #{ivar}<#{svar}.length; #{ivar}++"
var_part = @name ? "#{body_dent}#{@name} = #{svar}[#{ivar}];\n" : ''
end
body = @body
set_result = rvar ? "#{idt}#{rvar} = []; " : idt
return_result = rvar || ''
if top_level
body = Expressions.wrap(body)
else
body = Expressions.wrap(CallNode.new(
ValueNode.new(LiteralNode.new(rvar), [AccessorNode.new('push')]), [body.unwrap]
))
end
if o[:return]
return_result = "return #{return_result}" if o[:return]
o.delete(:return)
body = IfNode.new(@filter, body, nil, :statement => true) if @filter
elsif @filter
body = Expressions.wrap(IfNode.new(@filter, body))
end
if @object
body = Expressions.wrap(IfNode.new(
CallNode.new(ValueNode.new(LiteralNode.wrap(svar), [AccessorNode.new(Value.new('hasOwnProperty'))]), [LiteralNode.wrap(ivar)]),
Expressions.wrap(body),
nil,
{:statement => true}
))
end
return_result = "\n#{idt}#{return_result};" unless top_level
body = body.compile(o.merge(:indent => body_dent, :top => true))
vars = range ? @name : "#{@name}, #{ivar}"
return write(set_result + source_part + "for (#{for_part}) {\n#{var_part}#{body}\n#{idt}}\n#{idt}#{return_result}")
end
end
# A try/catch/finally block.
class TryNode < Node
statement
attr_reader :try, :error, :recovery, :finally
def initialize(try, error, recovery, finally=nil)
@try, @error, @recovery, @finally = try, error, recovery, finally
end
def compile_node(o)
o[:indent] = idt(1)
o[:top] = true
error_part = @error ? " (#{@error}) " : ' '
catch_part = @recovery && " catch#{error_part}{\n#{@recovery.compile(o)}\n#{idt}}"
finally_part = @finally && " finally {\n#{@finally.compile(o.merge(:return => nil))}\n#{idt}}"
write("#{idt}try {\n#{@try.compile(o)}\n#{idt}}#{catch_part}#{finally_part}")
end
end
# Throw an exception.
class ThrowNode < Node
statement_only
attr_reader :expression
def initialize(expression)
@expression = expression
end
def compile_node(o)
write("#{idt}throw #{@expression.compile(o)};")
end
end
# Check an expression for existence (meaning not null or undefined).
class ExistenceNode < Node
attr_reader :expression
def initialize(expression)
@expression = expression
end
def compile_node(o)
val = @expression.compile(o)
write("(typeof #{val} !== \"undefined\" && #{val} !== null)")
end
end
# An extra set of parentheses, supplied by the script source.
# You can't wrap parentheses around bits that get compiled into JS statements,
# unfortunately.
class ParentheticalNode < Node
attr_reader :expressions
def initialize(expressions, line=nil)
@expressions = expressions.unwrap
@line = line
end
def compile_node(o)
compiled = @expressions.compile(o)
compiled = compiled[0...-1] if compiled[-1..-1] == ';'
write("(#{compiled})")
end
end
# If/else statements. Switch/whens get compiled into these. Acts as an
# expression by pushing down requested returns to the expression bodies.
# Single-expression IfNodes are compiled into ternary operators if possible,
# because ternaries are first-class returnable assignable expressions.
class IfNode < Node
attr_reader :condition, :body, :else_body
def initialize(condition, body, else_body=nil, tags={})
@condition = condition
@body = body && body.unwrap
@else_body = else_body && else_body.unwrap
@tags = tags
@condition = OpNode.new("!", ParentheticalNode.new(@condition)) if @tags[:invert]
end
def <<(else_body)
eb = else_body.unwrap
@else_body ? @else_body << eb : @else_body = eb
self
end
def add_comment(comment)
@comment = comment
self
end
def force_statement
@tags[:statement] = true
self
end
# Rewrite a chain of IfNodes with their switch condition for equality.
def rewrite_condition(expression)
@condition = OpNode.new("is", expression, @condition)
@else_body.rewrite_condition(expression) if chain?
self
end
# Rewrite a chain of IfNodes to add a default case as the final else.
def add_else(exprs)
chain? ? @else_body.add_else(exprs) : @else_body = (exprs && exprs.unwrap)
self
end
# If the else_body is an IfNode itself, then we've got an if-else chain.
def chain?
@chain ||= @else_body && @else_body.is_a?(IfNode)
end
# The IfNode only compiles into a statement if either of the bodies needs
# to be a statement.
def statement?
@is_statement ||= !!(@comment || @tags[:statement] || @body.statement? || (@else_body && @else_body.statement?))
end
def compile_node(o)
write(statement? ? compile_statement(o) : compile_ternary(o))
end
# Compile the IfNode as a regular if-else statement. Flattened chains
# force sub-else bodies into statement form.
def compile_statement(o)
child = o.delete(:chain_child)
cond_o = o.dup
cond_o.delete(:return)
o[:indent] = idt(1)
o[:top] = true
if_dent = child ? '' : idt
com_dent = child ? idt : ''
prefix = @comment ? @comment.compile(cond_o) + "\n#{com_dent}" : ''
if_part = "#{prefix}#{if_dent}if (#{@condition.compile(cond_o)}) {\n#{Expressions.wrap(@body).compile(o)}\n#{idt}}"
return if_part unless @else_body
else_part = chain? ?
" else #{@else_body.compile(o.merge(:indent => idt, :chain_child => true))}" :
" else {\n#{Expressions.wrap(@else_body).compile(o)}\n#{idt}}"
if_part + else_part
end
# Compile the IfNode into a ternary operator.
def compile_ternary(o)
if_part = "#{@condition.compile(o)} ? #{@body.compile(o)}"
else_part = @else_body ? "#{@else_body.compile(o)}" : 'null'
"#{if_part} : #{else_part}"
end
end
end

View File

@@ -1,23 +0,0 @@
module CoffeeScript
# Racc will raise this Exception whenever a syntax error occurs. The main
# benefit over the Racc::ParseError is that the CoffeeScript::ParseError is
# line-number aware.
class ParseError < Racc::ParseError
def initialize(token_id, value, stack)
@token_id, @value, @stack = token_id, value, stack
end
def message
line = @value.respond_to?(:line) ? @value.line : "END"
line_part = "line #{line}:"
id_part = @token_id != @value.inspect ? ", unexpected #{@token_id.to_s.downcase}" : ""
val_part = ['INDENT', 'OUTDENT'].include?(@token_id) ? '' : " for '#{@value.to_s}'"
"#{line_part} syntax error#{val_part}#{id_part}"
end
alias_method :inspect, :message
end
end

Some files were not shown because too many files have changed in this diff Show More