|
|
|
@ -23,8 +23,6 @@
|
|
|
|
|
equivalent in JavaScript, it's just another way of saying it.
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<!-- -->
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
<b>Disclaimer:</b>
|
|
|
|
|
CoffeeScript is just for fun and seriously alpha. I'm sure that there are still
|
|
|
|
@ -112,13 +110,13 @@ cubed_list<span class="Keyword">:</span> math.cube(num) <span class="Keyword">fo
|
|
|
|
|
};
|
|
|
|
|
<span class="Comment"><span class="Comment">//</span> Array comprehensions:</span>
|
|
|
|
|
<span class="Storage">var</span> cubed_list;
|
|
|
|
|
<span class="Storage">var</span> a <span class="Keyword">=</span> list;
|
|
|
|
|
<span class="Storage">var</span> d <span class="Keyword">=</span> [];
|
|
|
|
|
<span class="Keyword">for</span> (<span class="Storage">var</span> b<span class="Keyword">=</span><span class="Number">0</span>, c<span class="Keyword">=</span>a.<span class="LibraryConstant">length</span>; b<span class="Keyword"><</span>c; b<span class="Keyword">++</span>) {
|
|
|
|
|
<span class="Storage">var</span> num <span class="Keyword">=</span> a[b];
|
|
|
|
|
d[b] <span class="Keyword">=</span> math.cube(num);
|
|
|
|
|
<span class="Storage">var</span> __a <span class="Keyword">=</span> list;
|
|
|
|
|
<span class="Storage">var</span> __d <span class="Keyword">=</span> [];
|
|
|
|
|
<span class="Keyword">for</span> (<span class="Storage">var</span> __b<span class="Keyword">=</span><span class="Number">0</span>, __c<span class="Keyword">=</span>__a.<span class="LibraryConstant">length</span>; __b<span class="Keyword"><</span>__c; __b<span class="Keyword">++</span>) {
|
|
|
|
|
<span class="Storage">var</span> num <span class="Keyword">=</span> __a[__b];
|
|
|
|
|
__d[__b] <span class="Keyword">=</span> math.cube(num);
|
|
|
|
|
}
|
|
|
|
|
cubed_list <span class="Keyword">=</span> d;
|
|
|
|
|
cubed_list <span class="Keyword">=</span> __d;
|
|
|
|
|
</pre><button onclick='javascript:
|
|
|
|
|
// Assignment:
|
|
|
|
|
var number = 42;
|
|
|
|
@ -143,17 +141,17 @@ var math = {
|
|
|
|
|
};
|
|
|
|
|
// Array comprehensions:
|
|
|
|
|
var cubed_list;
|
|
|
|
|
var a = list;
|
|
|
|
|
var d = [];
|
|
|
|
|
for (var b=0, c=a.length; b<c; b++) {
|
|
|
|
|
var num = a[b];
|
|
|
|
|
d[b] = math.cube(num);
|
|
|
|
|
var __a = list;
|
|
|
|
|
var __d = [];
|
|
|
|
|
for (var __b=0, __c=__a.length; __b<__c; __b++) {
|
|
|
|
|
var num = __a[__b];
|
|
|
|
|
__d[__b] = math.cube(num);
|
|
|
|
|
}
|
|
|
|
|
cubed_list = d;
|
|
|
|
|
cubed_list = __d;
|
|
|
|
|
;alert(cubed_list);'>run: cubed_list</button><br class='clear' /></div>
|
|
|
|
|
|
|
|
|
|
<h2 id="installation">Installation and Usage</h2>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
The CoffeeScript compiler is written in pure Ruby, and is available
|
|
|
|
|
as a Ruby Gem.
|
|
|
|
@ -165,14 +163,31 @@ gem install coffee-script</pre>
|
|
|
|
|
<p>
|
|
|
|
|
Installing the gem provides the <tt>coffee-script</tt> command, which can
|
|
|
|
|
be used to compile CoffeeScript <tt>.cs</tt> files into JavaScript, as
|
|
|
|
|
well as debug them. By default, <tt>coffee-script</tt> writes out the
|
|
|
|
|
JavaScript as <tt>.js</tt> files in the same directory, but output
|
|
|
|
|
well as debug them. In conjunction with
|
|
|
|
|
<a href="http://narwhaljs.org/">Narwhal</a>, the <tt>coffee-script</tt>
|
|
|
|
|
command also provides direct evaluation and an interactive REPL.
|
|
|
|
|
When compiling to JavaScript, <tt>coffee-script</tt> writes the output
|
|
|
|
|
as <tt>.js</tt> files in the same directory by default, but output
|
|
|
|
|
can be customized with the following options:
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
<tr>
|
|
|
|
|
<td width="25%"><code>-o, --output [DIR]</code></td>
|
|
|
|
|
<td width="25%"><code>-i, --interactive</code></td>
|
|
|
|
|
<td>
|
|
|
|
|
Launch an interactive CoffeeScript session.
|
|
|
|
|
Requires <a href="http://narwhaljs.org/">Narwhal</a>.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>-r, --run</code></td>
|
|
|
|
|
<td>
|
|
|
|
|
Compile and execute the CoffeeScripts without saving the intermediate
|
|
|
|
|
JavaScript. Requires <a href="http://narwhaljs.org/">Narwhal</a>.
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><code>-o, --output [DIR]</code></td>
|
|
|
|
|
<td>
|
|
|
|
|
Write out all compiled JavaScript files into the specified directory.
|
|
|
|
|
</td>
|
|
|
|
@ -195,7 +210,7 @@ gem install coffee-script</pre>
|
|
|
|
|
<td><code>-l, --lint</code></td>
|
|
|
|
|
<td>
|
|
|
|
|
If the <tt>jsl</tt> (JavaScript Lint) command is installed, use it
|
|
|
|
|
to check the compilation of a CoffeeScript file. (Handy in
|
|
|
|
|
to check the compilation of a CoffeeScript file. (Handy in
|
|
|
|
|
conjunction with <tt>--watch</tt>)
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
@ -264,15 +279,15 @@ coffee-script --print app/scripts/*.cs > concatenation.js</pre>
|
|
|
|
|
the line will do just as well. All other whitespace is
|
|
|
|
|
not significant. Instead of using curly braces <tt>{ }</tt>
|
|
|
|
|
to delimit a block of code, use a period <tt>.</tt> to mark the end of a
|
|
|
|
|
block, for
|
|
|
|
|
<a href="#functions">functions</a>,
|
|
|
|
|
<a href="#conditionals">if-statements</a>,
|
|
|
|
|
block, for
|
|
|
|
|
<a href="#functions">functions</a>,
|
|
|
|
|
<a href="#conditionals">if-statements</a>,
|
|
|
|
|
<a href="#switch">switch</a>, and <a href="#try">try/catch</a>.
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p id="functions">
|
|
|
|
|
<b class="header">Functions and Invocation</b>
|
|
|
|
|
Functions are defined by a list of parameters, an arrow, and the
|
|
|
|
|
Functions are defined by a list of parameters, an arrow, and the
|
|
|
|
|
function body. The empty function looks like this: <tt>=>.</tt>
|
|
|
|
|
</p>
|
|
|
|
|
<div class='code'><pre class="idle"><span class="FunctionName">square</span><span class="Keyword">:</span> <span class="FunctionArgument">x</span> <span class="Storage">=></span> x <span class="Keyword">*</span> x.
|
|
|
|
@ -453,7 +468,7 @@ var eldest = 24 > 21 ? "Liz" : "Ike";
|
|
|
|
|
The same mechanism is used to push down assignment through <b>switch</b>
|
|
|
|
|
statements, and <b>if-elses</b> (although the ternary operator is preferred).
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p id="aliases">
|
|
|
|
|
<b class="header">Aliases</b>
|
|
|
|
|
Because the <tt>==</tt> operator frequently causes undesirable coercion,
|
|
|
|
@ -472,7 +487,7 @@ var eldest = 24 > 21 ? "Liz" : "Ike";
|
|
|
|
|
</p>
|
|
|
|
|
<p>
|
|
|
|
|
Instead of a newline or semicolon, <tt>then</tt> can be used to separate
|
|
|
|
|
conditions from expressions, in <b>while</b>,
|
|
|
|
|
conditions from expressions, in <b>while</b>,
|
|
|
|
|
<b>if</b>/<b>else</b>, and <b>switch</b>/<b>when</b> statements.
|
|
|
|
|
</p>
|
|
|
|
|
<p>
|
|
|
|
@ -543,18 +558,18 @@ highlight(row) <span class="Keyword">for</span> row, i <span class="Keyword">in<
|
|
|
|
|
</pre><pre class="idle">
|
|
|
|
|
<span class="Comment"><span class="Comment">//</span> Eat lunch.</span>
|
|
|
|
|
<span class="Storage">var</span> lunch;
|
|
|
|
|
<span class="Storage">var</span> a <span class="Keyword">=</span> [<span class="String"><span class="String">'</span>toast<span class="String">'</span></span>, <span class="String"><span class="String">'</span>cheese<span class="String">'</span></span>, <span class="String"><span class="String">'</span>wine<span class="String">'</span></span>];
|
|
|
|
|
<span class="Storage">var</span> d <span class="Keyword">=</span> [];
|
|
|
|
|
<span class="Keyword">for</span> (<span class="Storage">var</span> b<span class="Keyword">=</span><span class="Number">0</span>, c<span class="Keyword">=</span>a.<span class="LibraryConstant">length</span>; b<span class="Keyword"><</span>c; b<span class="Keyword">++</span>) {
|
|
|
|
|
<span class="Storage">var</span> food <span class="Keyword">=</span> a[b];
|
|
|
|
|
d[b] <span class="Keyword">=</span> food.eat();
|
|
|
|
|
<span class="Storage">var</span> __a <span class="Keyword">=</span> [<span class="String"><span class="String">'</span>toast<span class="String">'</span></span>, <span class="String"><span class="String">'</span>cheese<span class="String">'</span></span>, <span class="String"><span class="String">'</span>wine<span class="String">'</span></span>];
|
|
|
|
|
<span class="Storage">var</span> __d <span class="Keyword">=</span> [];
|
|
|
|
|
<span class="Keyword">for</span> (<span class="Storage">var</span> __b<span class="Keyword">=</span><span class="Number">0</span>, __c<span class="Keyword">=</span>__a.<span class="LibraryConstant">length</span>; __b<span class="Keyword"><</span>__c; __b<span class="Keyword">++</span>) {
|
|
|
|
|
<span class="Storage">var</span> food <span class="Keyword">=</span> __a[__b];
|
|
|
|
|
__d[__b] <span class="Keyword">=</span> food.eat();
|
|
|
|
|
}
|
|
|
|
|
lunch <span class="Keyword">=</span> d;
|
|
|
|
|
lunch <span class="Keyword">=</span> __d;
|
|
|
|
|
<span class="Comment"><span class="Comment">//</span> Zebra-stripe a table.</span>
|
|
|
|
|
<span class="Storage">var</span> e <span class="Keyword">=</span> table;
|
|
|
|
|
<span class="Keyword">for</span> (<span class="Storage">var</span> f<span class="Keyword">=</span><span class="Number">0</span>, g<span class="Keyword">=</span>e.<span class="LibraryConstant">length</span>; f<span class="Keyword"><</span>g; f<span class="Keyword">++</span>) {
|
|
|
|
|
<span class="Storage">var</span> row <span class="Keyword">=</span> e[f];
|
|
|
|
|
<span class="Storage">var</span> i <span class="Keyword">=</span> f;
|
|
|
|
|
<span class="Storage">var</span> __e <span class="Keyword">=</span> table;
|
|
|
|
|
<span class="Keyword">for</span> (<span class="Storage">var</span> __f<span class="Keyword">=</span><span class="Number">0</span>, __g<span class="Keyword">=</span>__e.<span class="LibraryConstant">length</span>; __f<span class="Keyword"><</span>__g; __f<span class="Keyword">++</span>) {
|
|
|
|
|
<span class="Storage">var</span> row <span class="Keyword">=</span> __e[__f];
|
|
|
|
|
<span class="Storage">var</span> i <span class="Keyword">=</span> __f;
|
|
|
|
|
i <span class="Keyword">%</span> <span class="Number">2</span> <span class="Keyword">===</span> <span class="Number">0</span> ? highlight(row) : <span class="BuiltInConstant">null</span>;
|
|
|
|
|
}
|
|
|
|
|
</pre><br class='clear' /></div>
|
|
|
|
@ -769,14 +784,14 @@ to interest me on shore, I thought I would sail \
|
|
|
|
|
about a little and see the watery part of the \
|
|
|
|
|
world...";
|
|
|
|
|
;alert(moby_dick);'>run: moby_dick</button><br class='clear' /></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<h2 id="contributing">Contributing</h2>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
Here's a wish list of things that would be wonderful to have in
|
|
|
|
|
CoffeeScript:
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
<li>
|
|
|
|
|
A JavaScript version of the compiler, perhaps using Alessandro Warth's
|
|
|
|
@ -791,7 +806,7 @@ world...";
|
|
|
|
|
should be able to compile properly.
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
|
A tutorial that introduces CoffeeScript from the ground up for folks
|
|
|
|
|
A tutorial that introduces CoffeeScript from the ground up for folks
|
|
|
|
|
without knowledge of JavaScript.
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
@ -799,31 +814,45 @@ world...";
|
|
|
|
|
having a JavaScript version of the compiler).
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
|
A lot of the code generation in <tt>nodes.rb</tt> gets into messy
|
|
|
|
|
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>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<h2 id="change_log">Change Log</h2>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
<b class="header" style="margin-top: 20px;">0.1.3</b>
|
|
|
|
|
The <tt>coffee-script</tt> command now includes <tt>--interactive</tt>,
|
|
|
|
|
which launches an interactive CoffeeScript session, and <tt>--run</tt>,
|
|
|
|
|
which directly compiles and executes a script. Both options depend on a
|
|
|
|
|
working installation of Narwhal.
|
|
|
|
|
The <tt>aint</tt> keyword has been replaced by <tt>isnt</tt>, which goes
|
|
|
|
|
together a little smoother with <tt>is</tt>.
|
|
|
|
|
Quoted strings are now allowed as identifiers within object literals: eg.
|
|
|
|
|
<tt>{"5+5": 10}</tt>.
|
|
|
|
|
All assignment operators now use a colon: <tt>+:</tt>, <tt>-:</tt>,
|
|
|
|
|
<tt>*:</tt>, etc.
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
<b class="header" style="margin-top: 20px;">0.1.2</b>
|
|
|
|
|
Fixed a bug with calling <tt>super()</tt> through more than one level of
|
|
|
|
|
Fixed a bug with calling <tt>super()</tt> through more than one level of
|
|
|
|
|
inheritance, with the re-addition of the <tt>extends</tt> keyword.
|
|
|
|
|
Added experimental <a href="http://narwhaljs.org/">Narwhal</a>
|
|
|
|
|
support (as a Tusk package), contributed by
|
|
|
|
|
Added experimental <a href="http://narwhaljs.org/">Narwhal</a>
|
|
|
|
|
support (as a Tusk package), contributed by
|
|
|
|
|
<a href="http://tlrobinson.net/">Tom Robinson</a>, including
|
|
|
|
|
<b>bin/cs</b> as a CoffeeScript REPL and interpreter.
|
|
|
|
|
New <tt>--no-wrap</tt> option to suppress the safety function
|
|
|
|
|
wrapper.
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
<b class="header" style="margin-top: 20px;">0.1.1</b>
|
|
|
|
|
Added <tt>instanceof</tt> and <tt>typeof</tt> as operators.
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
<b class="header" style="margin-top: 20px;">0.1.0</b>
|
|
|
|
|
Initial CoffeeScript release.
|
|
|
|
|