updated and rebuilt docs

This commit is contained in:
leaf corcoran 2011-10-01 13:05:57 -07:00
parent ed89e8447a
commit c15089ee1c
2 changed files with 93 additions and 37 deletions

View File

@ -28,8 +28,8 @@ a certain amount. They are aliases for their expanded equivalents.
x = 0
x += 10
s = "hello "
s ..= "world"
s = "hello "
s ..= "world"
## Comments
@ -81,9 +81,9 @@ of the variable where the function is stored. When chaining together function
calls, the arguments are applied to the closest function to the left.
sum 10, 20
print sum 10, 20
print sum 10, 20
a b c "a", "b", "c"
a b c "a", "b", "c"
In order to avoid ambiguity in when calling functions, parentheses can also be
used to surround the arguments. This is required here in order to make sure the
@ -412,19 +412,19 @@ must be explicitly returned.
have_coins = false
if have_coins
print "Got coins"
else
else
print "No coins"
A short syntax for single statements can also be used:
have_coins = false
if have_coins then print "Got coins" else print "No coins"
have_coins = false
if have_coins then print "Got coins" else print "No coins"
Because if statements can be used as expressions, this can able be written as:
have_coins = false
print if have_coins then "Got coins" else "No coins"
have_coins = false
print if have_coins then "Got coins" else "No coins"
Conditionals can also be used in return statements and assignments:
@ -558,8 +558,8 @@ be declared as local, special syntax is required to declare a variable globally.
The export keyword makes it so any following assignments to the specified names
will not be assigned locally.
export var_name, var_name2
var_name, var_name3 = "hello", "world"
export var_name, var_name2
var_name, var_name3 = "hello", "world"
This is especially useful when declaring what will be externally visible in a
@ -655,23 +655,25 @@ The function stub syntax is a shorthand for creating a new closure function
that bundles both the object and function. This new function calls the wrapped
function in the correct context of the object.
Its syntax is the same as calling an instance method with the `\` operator but
Its syntax is the same as calling an instance method with the <code>\\</code> operator but
with no argument list provided.
my_object = {
value: 1000
print: => print "the value:", @value
write: => print "the value:", @value
}
run_callback (func) ->
print "running callback..."
func!
-- this will not work, the function has to no reference to my_object
run_callback my_object.print
-- this will not work:
-- the function has to no reference to my_object
run_callback my_object.write
-- function stub syntax lets us bundle the object into a new function
run_callback my_object\print
-- function stub syntax
-- lets us bundle the object into a new function
run_callback my_object\write
## The Using Clause; Controlling Destructive Assignment

View File

@ -1,7 +1,7 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- Autogenerated from moonscript/docs/reference.md on Mon Aug 29 08:50:21 2011 -->
<!-- Autogenerated from moonscript/docs/reference.md on Sat Oct 1 12:41:57 2011 -->
<meta charset="UTF-8">
<title>MoonScript v0.2.0</title>
@ -212,6 +212,8 @@ p > code , li > code {
<li><a href="#with_statement">With Statement</a></li>
<li><a href="#function_stubs">Function Stubs</a></li>
<li><a href="#the_using_clause_controlling_destructive_assignment">The Using Clause; Controlling Destructive Assignment</a></li>
</ul>
<li><a href="#moonscript_api">MoonScript API</a></li>
@ -529,7 +531,7 @@ arguments if we know we will be using a lower indentation futher on.</p>
<span class="l_symbol">}</span></code></pre></td></tr></tbody></table>
<p>The same thing can be done with other block level statements like
<a href="#Conditionals">conditionals</a>. We can use indentation level to determine what
<a href="#conditionals">conditionals</a>. We can use indentation level to determine what
statement a value belongs to:</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_keyword">if</span> <span class="l_atom">func</span> <span class="l_number">1</span>,<span class="l_number">2</span>,<span class="l_number">3</span>,
@ -991,11 +993,11 @@ need to return the results of the loop.</p>
<span class="l_keyword">while</span> <span class="l_atom">running</span> <span class="l_symbol">=</span><span class="l_symbol">=</span> <span class="l_special">true</span> <span class="l_keyword">do</span> <span class="l_atom">my_function</span><span class="l_symbol">!</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_keyword">local</span> <span class="l_atom">i</span> <span class="l_symbol">=</span> <span class="l_number">10</span>
<span class="l_atom">while</span> <span class="l_atom">i</span> &gt; <span class="l_number">0</span> <span class="l_keyword">do</span>
<span class="l_keyword">while</span> <span class="l_atom">i</span> &gt; <span class="l_number">0</span> <span class="l_keyword">do</span>
<span class="l_builtins">print</span>(<span class="l_atom">i</span>)
<span class="l_atom">i</span> <span class="l_symbol">=</span> <span class="l_atom">i</span> - <span class="l_number">1</span>
<span class="l_keyword">end</span>
<span class="l_atom">while</span> <span class="l_atom">running</span> <span class="l_symbol">=</span><span class="l_symbol">=</span> <span class="l_special">true</span> <span class="l_keyword">do</span>
<span class="l_keyword">while</span> <span class="l_atom">running</span> <span class="l_symbol">=</span><span class="l_symbol">=</span> <span class="l_special">true</span> <span class="l_keyword">do</span>
<span class="l_atom">my_function</span>()
<span class="l_keyword">end</span></code></pre></td></tr></tbody></table>
@ -1264,7 +1266,7 @@ properties and methods from another class.</p>
<span class="l_table_key">size</span><span class="l_bold_symbol">:</span> <span class="l_number">10</span>
<span class="l_table_key">add_item</span><span class="l_bold_symbol">:</span> (<span class="l_atom">name</span>) <span class="l_fn_symbol">=&gt;</span>
<span class="l_keyword">if</span> <span class="l_symbol">#</span><span class="l_self_var">@items</span> <span class="l_symbol">&gt;</span> <span class="l_atom">size</span> <span class="l_keyword">then</span> <span class="l_atom">error</span> <span class="l_string">"backpack is full"</span>
<span class="l_atom">super</span> <span class="l_atom">name</span>
<span class="l_keyword">super</span> <span class="l_atom">name</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_keyword">local</span> <span class="l_atom">BackPack</span>
<span class="l_atom">BackPack</span> <span class="l_symbol">=</span> (<span class="l_fn_symbol">function</span>(<span class="l_atom">_parent_0</span>)
<span class="l_keyword">local</span> <span class="l_atom">_base_0</span> <span class="l_symbol">=</span> <span class="l_symbol">{</span>
@ -1336,7 +1338,7 @@ will not be assigned locally.</p>
module:</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_comment">-- my_module.moon
</span><span class="l_atom">module</span> <span class="l_string">"my_module"</span>, <span class="l_atom">package</span><span class="l_bold_symbol">.</span><span class="l_atom">seeall</span>
</span><span class="l_builtins">module</span> <span class="l_string">"my_module"</span>, <span class="l_builtins">package.seeall</span>
<span class="l_keyword">export</span> <span class="l_atom">print_result</span>
<span class="l_atom">length</span> <span class="l_symbol">=</span> (<span class="l_atom">x</span>, <span class="l_atom">y</span>) <span class="l_fn_symbol">-&gt;</span> <span class="l_atom">math</span><span class="l_bold_symbol">.</span><span class="l_atom">sqrt</span> <span class="l_atom">x</span><span class="l_symbol">*</span><span class="l_atom">x</span> + <span class="l_atom">y</span><span class="l_symbol">*</span><span class="l_atom">y</span>
@ -1345,12 +1347,12 @@ module:</p>
<span class="l_builtins">print</span> <span class="l_string">"Length is "</span>, <span class="l_atom">length</span> <span class="l_atom">x</span>, <span class="l_atom">y</span>
<span class="l_comment">-- main.moon
</span><span class="l_atom">require</span> <span class="l_string">"my_module"</span>
</span><span class="l_builtins">require</span> <span class="l_string">"my_module"</span>
<span class="l_atom">my_module</span><span class="l_bold_symbol">.</span><span class="l_atom">print_result</span> <span class="l_number">4</span>, <span class="l_number">5</span> <span class="l_comment">-- prints the result
</span>
<span class="l_builtins">print</span> <span class="l_atom">my_module</span><span class="l_bold_symbol">.</span><span class="l_atom">length</span> <span class="l_number">6</span>, <span class="l_number">7</span> <span class="l_comment">-- errors, `length` not visible
</span></code></pre></td><td><pre><code class="lua-code"><span class="l_atom">module</span>(<span class="l_string">"my_module"</span>, <span class="l_atom">package</span><span class="l_symbol">.</span><span class="l_atom">seeall</span>)
</span></code></pre></td><td><pre><code class="lua-code"><span class="l_builtins">module</span>(<span class="l_string">"my_module"</span>, <span class="l_builtins">package.seeall</span>)
<span class="l_keyword">local</span> <span class="l_atom">length</span>
<span class="l_atom">length</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">function</span>(<span class="l_atom">x</span>, <span class="l_atom">y</span>)
<span class="l_keyword">return</span> <span class="l_atom">math</span><span class="l_symbol">.</span><span class="l_atom">sqrt</span>(<span class="l_atom">x</span> * <span class="l_atom">x</span> + <span class="l_atom">y</span> * <span class="l_atom">y</span>)
@ -1358,7 +1360,7 @@ module:</p>
<span class="l_atom">print_result</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">function</span>(<span class="l_atom">x</span>, <span class="l_atom">y</span>)
<span class="l_keyword">return</span> <span class="l_builtins">print</span>(<span class="l_string">"Length is "</span>, <span class="l_atom">length</span>(<span class="l_atom">x</span>, <span class="l_atom">y</span>))
<span class="l_keyword">end</span>
<span class="l_atom">require</span>(<span class="l_string">"my_module"</span>)
<span class="l_builtins">require</span>(<span class="l_string">"my_module"</span>)
<span class="l_atom">my_module</span><span class="l_symbol">.</span><span class="l_atom">print_result</span>(<span class="l_number">4</span>, <span class="l_number">5</span>)
<span class="l_builtins">print</span>(<span class="l_atom">my_module</span><span class="l_symbol">.</span><span class="l_atom">length</span>(<span class="l_number">6</span>, <span class="l_number">7</span>))</code></pre></td></tr></tbody></table>
@ -1387,7 +1389,7 @@ to bind it to that table:</p>
<span class="l_keyword">import</span> <span class="l_symbol">\</span><span class="l_atom">add</span> <span class="l_keyword">from</span> <span class="l_atom">my_module</span>
<span class="l_builtins">print</span> <span class="l_atom">add</span>(<span class="l_number">22</span>) <span class="l_comment">-- equivalent to calling my_module:get(22)
<span class="l_builtins">print</span> <span class="l_atom">add</span>(<span class="l_number">22</span>) <span class="l_comment">-- equivalent to calling my_module\get 22
</span></code></pre></td><td><pre><code class="lua-code"><span class="l_keyword">local</span> <span class="l_atom">my_module</span> <span class="l_symbol">=</span> <span class="l_symbol">{</span>
<span class="l_atom">state</span> <span class="l_symbol">=</span> <span class="l_number">100</span>,
<span class="l_atom">add</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">function</span>(<span class="l_atom">self</span>, <span class="l_atom">value</span>)
@ -1407,8 +1409,11 @@ unnecessary noise. A common solution to this is to pass a table in as an
argument which contains a collection of keys and values to overwrite. The
downside to this is that the constructor of this object must support this form.</p>
<p>The <code>with</code> block helps to alleviate this. It lets us use a bare function and
index syntax in order to work with the object:</p>
<p>The <code>with</code> block helps to alleviate this. Within a <code>with</code> block we can use a
special statements that begin with either <code>.</code> or <code>\` which represent
those operations applied to the object we are using</code>with<code>on.</code></p>
<p>For example, we work with a newly created object:</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_keyword">with</span> <span class="l_proper">Person</span><span class="l_symbol">!</span>
<span class="l_bold_symbol">.</span><span class="l_atom">name</span> <span class="l_symbol">=</span> <span class="l_string">"Oswald"</span>
@ -1464,6 +1469,55 @@ it has been giving access to.</p>
<span class="l_atom">sister</span>
<span class="l_symbol">}</span>)</code></pre></td></tr></tbody></table>
<h2><a name="function_stubs"></a>Function Stubs</h2>
<p>It is common to pass a function from an object around as a value, for example,
passing an instance method into a function as a callback. If the function
expects the object it is operating on as the first argument then you must
somehow bundle that object with the function so it can be called properly.</p>
<p>The function stub syntax is a shorthand for creating a new closure function
that bundles both the object and function. This new function calls the wrapped
function in the correct context of the object.</p>
<p>Its syntax is the same as calling an instance method with the <code>\</code> operator but
with no argument list provided.</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_atom">my_object</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">{</span>
<span class="l_table_key">value</span><span class="l_bold_symbol">:</span> <span class="l_number">1000</span>
<span class="l_table_key">write</span><span class="l_bold_symbol">:</span> <span class="l_fn_symbol">=&gt;</span> <span class="l_builtins">print</span> <span class="l_string">"the value:"</span>, <span class="l_self_var">@value</span>
<span class="l_fn_symbol">}</span>
<span class="l_atom">run_callback</span> (<span class="l_atom">func</span>) <span class="l_fn_symbol">-&gt;</span>
<span class="l_builtins">print</span> <span class="l_string">"running callback..."</span>
<span class="l_atom">func</span><span class="l_symbol">!</span>
<span class="l_comment">-- this will not work:
</span><span class="l_comment">-- the function has to no reference to my_object
</span><span class="l_atom">run_callback</span> <span class="l_atom">my_object</span><span class="l_bold_symbol">.</span><span class="l_atom">write</span>
<span class="l_comment">-- function stub syntax
</span><span class="l_comment">-- lets us bundle the object into a new function
</span><span class="l_atom">run_callback</span> <span class="l_atom">my_object</span><span class="l_symbol">\</span><span class="l_atom">write</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_keyword">local</span> <span class="l_atom">my_object</span> <span class="l_symbol">=</span> <span class="l_symbol">{</span>
<span class="l_atom">value</span> <span class="l_symbol">=</span> <span class="l_number">1000</span>,
<span class="l_atom">write</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">function</span>(<span class="l_atom">self</span>)
<span class="l_keyword">return</span> <span class="l_builtins">print</span>(<span class="l_string">"the value:"</span>, <span class="l_atom">self</span><span class="l_symbol">.</span><span class="l_atom">value</span>)
<span class="l_keyword">end</span>
<span class="l_symbol">}</span>
<span class="l_atom">run_callback</span>(<span class="l_fn_symbol">function</span>(<span class="l_atom">func</span>)
<span class="l_builtins">print</span>(<span class="l_string">"running callback..."</span>)
<span class="l_keyword">return</span> <span class="l_atom">func</span>()
<span class="l_keyword">end</span>)
<span class="l_atom">run_callback</span>(<span class="l_atom">my_object</span><span class="l_symbol">.</span><span class="l_atom">write</span>)
<span class="l_atom">run_callback</span>((<span class="l_fn_symbol">function</span>()
<span class="l_keyword">local</span> <span class="l_atom">_base_0</span> <span class="l_symbol">=</span> <span class="l_atom">my_object</span>
<span class="l_keyword">local</span> <span class="l_atom">_fn_0</span> <span class="l_symbol">=</span> <span class="l_atom">_base_0</span><span class="l_symbol">.</span><span class="l_atom">write</span>
<span class="l_keyword">return</span> <span class="l_fn_symbol">function</span>(<span class="l_symbol">.</span><span class="l_symbol">.</span><span class="l_symbol">.</span>)
<span class="l_keyword">return</span> <span class="l_atom">_fn_0</span>(<span class="l_atom">_base_0</span>, <span class="l_symbol">.</span><span class="l_symbol">.</span><span class="l_symbol">.</span>)
<span class="l_keyword">end</span>
<span class="l_keyword">end</span>)())</code></pre></td></tr></tbody></table>
<h2><a name="the_using_clause_controlling_destructive_assignment"></a>The Using Clause; Controlling Destructive Assignment</h2>
<p>While lexical scoping can be a great help in reducing the complexity of the
@ -1487,7 +1541,7 @@ the following snippet:</p>
<span class="l_keyword">local</span> <span class="l_atom">my_func</span>
<span class="l_atom">my_func</span> <span class="l_symbol">=</span> <span class="l_fn_symbol">function</span>()
<span class="l_atom">i</span> <span class="l_symbol">=</span> <span class="l_number">10</span>
<span class="l_atom">while</span> <span class="l_atom">i</span> &gt; <span class="l_number">0</span> <span class="l_keyword">do</span>
<span class="l_keyword">while</span> <span class="l_atom">i</span> &gt; <span class="l_number">0</span> <span class="l_keyword">do</span>
<span class="l_builtins">print</span>(<span class="l_atom">i</span>)
<span class="l_atom">i</span> <span class="l_symbol">=</span> <span class="l_atom">i</span> - <span class="l_number">1</span>
<span class="l_keyword">end</span>
@ -1553,8 +1607,8 @@ accessed, they just cant be modified:</p>
<p>Upon installing MoonScript, a <code>moonscript</code> module is made available. The best
use of this module is making your Luas require function MoonScript aware.</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_atom">require</span> <span class="l_string">"moonscript"</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_atom">require</span>(<span class="l_string">"moonscript"</span>)</code></pre></td></tr></tbody></table>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_builtins">require</span> <span class="l_string">"moonscript"</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_builtins">require</span>(<span class="l_string">"moonscript"</span>)</code></pre></td></tr></tbody></table>
<p>After <code>moonscript</code> is required, Luas package loader is updated to search for
<code>.moon</code> files on any subsequent calls to <code>require</code>. The search path for <code>.moon</code>
@ -1615,8 +1669,8 @@ Lua code from MoonScript code.</p>
<p>Here is a quick example of how you would compile a MoonScript string to a Lua
String:</p>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_atom">require</span> <span class="l_string">"moonscript.parse"</span>
<span class="l_atom">require</span> <span class="l_string">"moonscript.compile"</span>
<table style="width: 100%;" cellspacing="0" cellpadding="1"><tbody><tr class="code-header"><td>moonscript</td><td>lua</td></tr><tr><td style="width: 50%" class="code-border"><pre><code class="moon-code"><span class="l_builtins">require</span> <span class="l_string">"moonscript.parse"</span>
<span class="l_builtins">require</span> <span class="l_string">"moonscript.compile"</span>
<span class="l_keyword">import</span> <span class="l_atom">parse</span>, <span class="l_atom">compile</span> <span class="l_keyword">from</span> <span class="l_atom">moonscript</span>
@ -1632,8 +1686,8 @@ String:</p>
<span class="l_comment">-- our code is ready
</span><span class="l_builtins">print</span> <span class="l_atom">lua_code</span>
</code></pre></td><td><pre><code class="lua-code"><span class="l_atom">require</span>(<span class="l_string">"moonscript.parse"</span>)
<span class="l_atom">require</span>(<span class="l_string">"moonscript.compile"</span>)
</code></pre></td><td><pre><code class="lua-code"><span class="l_builtins">require</span>(<span class="l_string">"moonscript.parse"</span>)
<span class="l_builtins">require</span>(<span class="l_string">"moonscript.compile"</span>)
<span class="l_keyword">local</span> <span class="l_atom">parse</span>, <span class="l_atom">compile</span> <span class="l_symbol">=</span> <span class="l_atom">moonscript</span><span class="l_symbol">.</span><span class="l_atom">parse</span>, <span class="l_atom">moonscript</span><span class="l_symbol">.</span><span class="l_atom">compile</span>
<span class="l_keyword">local</span> <span class="l_atom">moon_code</span> <span class="l_symbol">=</span> <span class="l_string">[[(-&gt; print "hello world")!]]</span>
<span class="l_keyword">local</span> <span class="l_atom">tree</span>, <span class="l_atom">err</span> <span class="l_symbol">=</span> <span class="l_atom">parse</span><span class="l_symbol">.</span><span class="l_atom">string</span>(<span class="l_atom">moon_code</span>)
@ -1720,7 +1774,7 @@ THE SOFTWARE.</p>
</div>
<div class="footer">Generated on Mon Aug 29 08:50:21 2011; MoonScript v0.2.0-dev</div>
<div class="footer">Generated on Sat Oct 1 12:41:57 2011; MoonScript v0.2.0-dev</div>