1416 lines
303 KiB
HTML
1416 lines
303 KiB
HTML
<!DOCTYPE html> <html> <head> <title>nodes.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To … <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="browser.html"> browser.coffee </a> <a class="source" href="cake.html"> cake.coffee </a> <a class="source" href="coffee-script.html"> coffee-script.coffee </a> <a class="source" href="command.html"> command.coffee </a> <a class="source" href="grammar.html"> grammar.coffee </a> <a class="source" href="helpers.html"> helpers.coffee </a> <a class="source" href="index.html"> index.coffee </a> <a class="source" href="lexer.html"> lexer.coffee </a> <a class="source" href="nodes.html"> nodes.coffee </a> <a class="source" href="optparse.html"> optparse.coffee </a> <a class="source" href="repl.html"> repl.coffee </a> <a class="source" href="rewriter.html"> rewriter.coffee </a> <a class="source" href="scope.html"> scope.coffee </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> nodes.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> <p><code>nodes.coffee</code> contains all of the node classes for the syntax tree. Most
|
|
nodes are created as the result of actions in the <a href="grammar.html">grammar</a>,
|
|
but some are created by other nodes as a method of code generation. To convert
|
|
the syntax tree into a string of JavaScript code, call <code>compile()</code> on the root.</p> </td> <td class="code"> <div class="highlight"><pre><span class="p">{</span><span class="nx">Scope</span><span class="p">}</span> <span class="o">=</span> <span class="nx">require</span> <span class="s1">'./scope'</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <p>Import the helpers we plan to use.</p> </td> <td class="code"> <div class="highlight"><pre><span class="p">{</span><span class="nx">compact</span><span class="p">,</span> <span class="nx">flatten</span><span class="p">,</span> <span class="nx">extend</span><span class="p">,</span> <span class="nx">merge</span><span class="p">,</span> <span class="nx">del</span><span class="p">,</span> <span class="nx">starts</span><span class="p">,</span> <span class="nx">ends</span><span class="p">,</span> <span class="nx">last</span><span class="p">}</span> <span class="o">=</span> <span class="nx">require</span> <span class="s1">'./helpers'</span>
|
|
|
|
<span class="nv">exports.extend = </span><span class="nx">extend</span> <span class="c1"># for parser</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <p>Constant functions for nodes that don't need customization.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">YES = </span><span class="o">-></span> <span class="kc">yes</span>
|
|
<span class="nv">NO = </span><span class="o">-></span> <span class="kc">no</span>
|
|
<span class="nv">THIS = </span><span class="o">-></span> <span class="k">this</span>
|
|
<span class="nv">NEGATE = </span><span class="o">-></span> <span class="vi">@negated = </span><span class="o">not</span> <span class="nx">@negated</span><span class="p">;</span> <span class="k">this</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <h3>Base</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>The <strong>Base</strong> is the abstract base class for all nodes in the syntax tree.
|
|
Each subclass implements the <code>compileNode</code> method, which performs the
|
|
code generation for that node. To compile a node to JavaScript,
|
|
call <code>compile</code> on it, which wraps <code>compileNode</code> in some generic extra smarts,
|
|
to know when the generated code needs to be wrapped up in a closure.
|
|
An options hash is passed and cloned throughout, containing information about
|
|
the environment from higher in the tree (such as if a returned value is
|
|
being requested by the surrounding function), information about the current
|
|
scope, and indentation level.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Base = </span><span class="nx">class</span> <span class="nx">Base</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>Common logic for determining whether to wrap this node in a closure before
|
|
compiling it, or to compile directly. We need to wrap if this node is a
|
|
<em>statement</em>, and it's not a <em>pureStatement</em>, 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 (because statements know how to
|
|
return results).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">lvl</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">o = </span><span class="nx">extend</span> <span class="p">{},</span> <span class="nx">o</span>
|
|
<span class="nv">o.level = </span><span class="nx">lvl</span> <span class="k">if</span> <span class="nx">lvl</span>
|
|
<span class="nv">node = </span><span class="nx">@unfoldSoak</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">or</span> <span class="k">this</span>
|
|
<span class="nv">node.tab = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">is</span> <span class="nx">LEVEL_TOP</span> <span class="o">or</span> <span class="o">not</span> <span class="nx">node</span><span class="p">.</span><span class="nx">isStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
<span class="nx">node</span><span class="p">.</span><span class="nx">compileNode</span> <span class="nx">o</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">node</span><span class="p">.</span><span class="nx">compileClosure</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>Statements converted into expressions via closure-wrapping share a scope
|
|
object with their parent closure, to preserve the expected lexical scope.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileClosure</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@jumps</span><span class="p">()</span> <span class="o">or</span> <span class="k">this</span> <span class="k">instanceof</span> <span class="nx">Throw</span>
|
|
<span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">'cannot use a pure statement in an expression.'</span>
|
|
<span class="nv">o.sharedScope = </span><span class="kc">yes</span>
|
|
<span class="nx">Closure</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">compileNode</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <p>If the code generation wishes to use the result of a complex expression
|
|
in multiple places, ensure that the expression is only ever evaluated once,
|
|
by assigning it to a temporary variable. Pass a level to precompile.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">cache</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">level</span><span class="p">,</span> <span class="nx">reused</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">unless</span> <span class="nx">@isComplex</span><span class="p">()</span>
|
|
<span class="nv">ref = </span><span class="k">if</span> <span class="nx">level</span> <span class="k">then</span> <span class="nx">@compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">level</span> <span class="k">else</span> <span class="k">this</span>
|
|
<span class="p">[</span><span class="nx">ref</span><span class="p">,</span> <span class="nx">ref</span><span class="p">]</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">ref = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">reused</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'ref'</span>
|
|
<span class="nv">sub = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="nx">ref</span><span class="p">,</span> <span class="k">this</span>
|
|
<span class="k">if</span> <span class="nx">level</span> <span class="k">then</span> <span class="p">[</span><span class="nx">sub</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">level</span><span class="p">),</span> <span class="nx">ref</span><span class="p">.</span><span class="nx">value</span><span class="p">]</span> <span class="k">else</span> <span class="p">[</span><span class="nx">sub</span><span class="p">,</span> <span class="nx">ref</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <p>Compile to a source/variable pair suitable for looping.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileLoopReference</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">src = tmp = </span><span class="nx">@compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="nx">unless</span> <span class="o">-</span><span class="kc">Infinity</span> <span class="o"><</span> <span class="o">+</span><span class="nx">src</span> <span class="o"><</span> <span class="kc">Infinity</span> <span class="o">or</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">src</span><span class="p">)</span> <span class="o">and</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">check</span><span class="p">(</span><span class="nx">src</span><span class="p">,</span> <span class="kc">yes</span><span class="p">)</span>
|
|
<span class="nv">src = </span><span class="s2">"#{ tmp = o.scope.freeVariable name } = #{src}"</span>
|
|
<span class="p">[</span><span class="nx">src</span><span class="p">,</span> <span class="nx">tmp</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>Construct a node that returns the current node's result.
|
|
Note that this is overridden for smarter behavior for
|
|
many statement nodes (e.g. If, For)...</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="k">new</span> <span class="nx">Return</span> <span class="k">this</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <p>Does this node, or any of its children, contain a node of a certain kind?
|
|
Recursively traverses down the <em>children</em> of the nodes, yielding to a block
|
|
and returning true when the block finds a match. <code>contains</code> does not cross
|
|
scope boundaries.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">contains</span><span class="o">:</span> <span class="p">(</span><span class="nx">pred</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">contains = </span><span class="kc">no</span>
|
|
<span class="nx">@traverseChildren</span> <span class="kc">no</span><span class="p">,</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">pred</span> <span class="nx">node</span>
|
|
<span class="nv">contains = </span><span class="kc">yes</span>
|
|
<span class="k">return</span> <span class="kc">no</span>
|
|
<span class="nx">contains</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <p>Is this node of a certain type, or does it contain the type?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">containsType</span><span class="o">:</span> <span class="p">(</span><span class="nx">type</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">this</span> <span class="k">instanceof</span> <span class="nx">type</span> <span class="o">or</span> <span class="nx">@contains</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-></span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">type</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>Pull out the last non-comment node of a node list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">lastNonComment</span><span class="o">:</span> <span class="p">(</span><span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">i = </span><span class="nx">list</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="k">return</span> <span class="nx">list</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="k">while</span> <span class="nx">i</span><span class="o">--</span> <span class="k">when</span> <span class="nx">list</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Comment</span>
|
|
<span class="kc">null</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <p><code>toString</code> representation of the node, for inspecting the parse tree.
|
|
This is what <code>coffee --nodes</code> prints out.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">toString</span><span class="o">:</span> <span class="p">(</span><span class="nv">idt = </span><span class="s1">''</span><span class="p">,</span> <span class="nv">name = </span><span class="nx">@constructor</span><span class="p">.</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">tree = </span><span class="s1">'\n'</span> <span class="o">+</span> <span class="nx">idt</span> <span class="o">+</span> <span class="nx">name</span>
|
|
<span class="nx">tree</span> <span class="o">+=</span> <span class="s1">'?'</span> <span class="k">if</span> <span class="nx">@soak</span>
|
|
<span class="nx">@eachChild</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-></span> <span class="nx">tree</span> <span class="o">+=</span> <span class="nx">node</span><span class="p">.</span><span class="nx">toString</span> <span class="nx">idt</span> <span class="o">+</span> <span class="nx">TAB</span>
|
|
<span class="nx">tree</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p>Passes each child to a function, breaking when the function returns <code>false</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">eachChild</span><span class="o">:</span> <span class="p">(</span><span class="nx">func</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="k">this</span> <span class="nx">unless</span> <span class="nx">@children</span>
|
|
<span class="k">for</span> <span class="nx">attr</span> <span class="k">in</span> <span class="nx">@children</span> <span class="k">when</span> <span class="err">@</span><span class="p">[</span><span class="nx">attr</span><span class="p">]</span>
|
|
<span class="k">for</span> <span class="nx">child</span> <span class="k">in</span> <span class="nx">flatten</span> <span class="p">[</span><span class="err">@</span><span class="p">[</span><span class="nx">attr</span><span class="p">]]</span>
|
|
<span class="k">return</span> <span class="k">this</span> <span class="k">if</span> <span class="nx">func</span><span class="p">(</span><span class="nx">child</span><span class="p">)</span> <span class="o">is</span> <span class="kc">false</span>
|
|
<span class="k">this</span>
|
|
|
|
<span class="nx">traverseChildren</span><span class="o">:</span> <span class="p">(</span><span class="nx">crossScope</span><span class="p">,</span> <span class="nx">func</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@eachChild</span> <span class="p">(</span><span class="nx">child</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">func</span><span class="p">(</span><span class="nx">child</span><span class="p">)</span> <span class="o">is</span> <span class="kc">false</span>
|
|
<span class="nx">child</span><span class="p">.</span><span class="nx">traverseChildren</span> <span class="nx">crossScope</span><span class="p">,</span> <span class="nx">func</span>
|
|
|
|
<span class="nx">invert</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="k">new</span> <span class="nx">Op</span> <span class="s1">'!'</span><span class="p">,</span> <span class="k">this</span>
|
|
|
|
<span class="nx">unwrapAll</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nv">node = </span><span class="k">this</span>
|
|
<span class="k">continue</span> <span class="nx">until</span> <span class="nx">node</span> <span class="o">is</span> <span class="nv">node = </span><span class="nx">node</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span>
|
|
<span class="nx">node</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <p>Default implementations of the common node properties and methods. Nodes
|
|
will override these with custom logic, if needed.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">children</span><span class="o">:</span> <span class="p">[]</span>
|
|
|
|
<span class="nx">isStatement</span> <span class="o">:</span> <span class="nx">NO</span>
|
|
<span class="nx">jumps</span> <span class="o">:</span> <span class="nx">NO</span>
|
|
<span class="nx">isComplex</span> <span class="o">:</span> <span class="nx">YES</span>
|
|
<span class="nx">isChainable</span> <span class="o">:</span> <span class="nx">NO</span>
|
|
<span class="nx">isAssignable</span> <span class="o">:</span> <span class="nx">NO</span>
|
|
|
|
<span class="nx">unwrap</span> <span class="o">:</span> <span class="nx">THIS</span>
|
|
<span class="nx">unfoldSoak</span> <span class="o">:</span> <span class="nx">NO</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div> <p>Is this node used to assign a certain variable?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">assigns</span><span class="o">:</span> <span class="nx">NO</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <h3>Block</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div> <p>The block is the list of expressions that forms the body of an
|
|
indented block of code -- the implementation of a function, a clause in an
|
|
<code>if</code>, <code>switch</code>, or <code>try</code>, and so on...</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Block = </span><span class="nx">class</span> <span class="nx">Block</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">nodes</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@expressions = </span><span class="nx">compact</span> <span class="nx">flatten</span> <span class="nx">nodes</span> <span class="o">or</span> <span class="p">[]</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'expressions'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</a> </div> <p>Tack an expression on to the end of this expression list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">push</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@expressions</span><span class="p">.</span><span class="nx">push</span> <span class="nx">node</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">¶</a> </div> <p>Remove and return the last expression of this expression list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">pop</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@expressions</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <p>Add an expression at the beginning of this expression list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">unshift</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@expressions</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">node</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <p>If this Block consists of just a single node, unwrap it by pulling
|
|
it back out.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">unwrap</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@expressions</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">1</span> <span class="k">then</span> <span class="nx">@expressions</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">else</span> <span class="k">this</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">¶</a> </div> <p>Is this an empty block of code?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">isEmpty</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="o">not</span> <span class="nx">@expressions</span><span class="p">.</span><span class="nx">length</span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">for</span> <span class="nx">exp</span> <span class="k">in</span> <span class="nx">@expressions</span> <span class="k">when</span> <span class="nx">exp</span><span class="p">.</span><span class="nx">isStatement</span> <span class="nx">o</span>
|
|
<span class="k">return</span> <span class="kc">yes</span>
|
|
<span class="kc">no</span>
|
|
|
|
<span class="nx">jumps</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">for</span> <span class="nx">exp</span> <span class="k">in</span> <span class="nx">@expressions</span>
|
|
<span class="k">return</span> <span class="nx">exp</span> <span class="k">if</span> <span class="nx">exp</span><span class="p">.</span><span class="nx">jumps</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">¶</a> </div> <p>A Block node does not return its entire body, rather it
|
|
ensures that the final expression is returned.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nv">len = </span><span class="nx">@expressions</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="k">while</span> <span class="nx">len</span><span class="o">--</span>
|
|
<span class="nv">expr = </span><span class="nx">@expressions</span><span class="p">[</span><span class="nx">len</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="nx">expr</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Comment</span>
|
|
<span class="nx">@expressions</span><span class="p">[</span><span class="nx">len</span><span class="p">]</span> <span class="o">=</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span>
|
|
<span class="nx">@expressions</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">len</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="k">if</span> <span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">Return</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">expression</span>
|
|
<span class="k">break</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">¶</a> </div> <p>A <strong>Block</strong> is the only node that can serve as the root.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nv">o = </span><span class="p">{},</span> <span class="nx">level</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span> <span class="k">then</span> <span class="k">super</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">level</span> <span class="k">else</span> <span class="nx">@compileRoot</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">¶</a> </div> <p>Compile all expressions within the <strong>Block</strong> body. If we need to
|
|
return the result, and it's an expression, simply return it. If it's a
|
|
statement, ask the statement to do so.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@tab = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span>
|
|
<span class="nv">top = </span><span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">is</span> <span class="nx">LEVEL_TOP</span>
|
|
<span class="nv">codes = </span><span class="p">[]</span>
|
|
<span class="k">for</span> <span class="nx">node</span> <span class="k">in</span> <span class="nx">@expressions</span>
|
|
<span class="nv">node = </span><span class="nx">node</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()</span>
|
|
<span class="nv">node = </span><span class="p">(</span><span class="nx">node</span><span class="p">.</span><span class="nx">unfoldSoak</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">or</span> <span class="nx">node</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Block</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-28">¶</a> </div> <p>This is a nested block. We don't do anything special here like enclose
|
|
it in a new scope; we just compile the statements in this block along with
|
|
our own</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">codes</span><span class="p">.</span><span class="nx">push</span> <span class="nx">node</span><span class="p">.</span><span class="nx">compileNode</span> <span class="nx">o</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">top</span>
|
|
<span class="nv">node.front = </span><span class="kc">true</span>
|
|
<span class="nv">code = </span><span class="nx">node</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nx">codes</span><span class="p">.</span><span class="nx">push</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">isStatement</span> <span class="nx">o</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="nx">@tab</span> <span class="o">+</span> <span class="nx">code</span> <span class="o">+</span> <span class="s1">';'</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">codes</span><span class="p">.</span><span class="nx">push</span> <span class="nx">node</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="k">return</span> <span class="nx">codes</span><span class="p">.</span><span class="nx">join</span> <span class="s1">'\n'</span> <span class="k">if</span> <span class="nx">top</span>
|
|
<span class="nv">code = </span><span class="nx">codes</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">', '</span><span class="p">)</span> <span class="o">or</span> <span class="s1">'void 0'</span>
|
|
<span class="k">if</span> <span class="nx">codes</span><span class="p">.</span><span class="nx">length</span> <span class="o">></span> <span class="mi">1</span> <span class="o">and</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">>=</span> <span class="nx">LEVEL_LIST</span> <span class="k">then</span> <span class="s2">"(#{code})"</span> <span class="k">else</span> <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-29">¶</a> </div> <p>If we happen to be the top-level <strong>Block</strong>, wrap everything in
|
|
a safety closure, unless requested not to.
|
|
It would be better not to generate them in the first place, but for now,
|
|
clean up obvious double-parentheses.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileRoot</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">o.indent = </span><span class="vi">@tab = </span><span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">bare</span> <span class="k">then</span> <span class="s1">''</span> <span class="k">else</span> <span class="nx">TAB</span>
|
|
<span class="nv">o.scope = </span><span class="k">new</span> <span class="nx">Scope</span> <span class="kc">null</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="kc">null</span>
|
|
<span class="nv">o.level = </span><span class="nx">LEVEL_TOP</span>
|
|
<span class="nv">code = </span><span class="nx">@compileWithDeclarations</span> <span class="nx">o</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">bare</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">"(function() {\n#{code}\n}).call(this);\n"</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-30">¶</a> </div> <p>Compile the expressions body for the contents of a function, with
|
|
declarations of all inner variables pushed up to the top.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileWithDeclarations</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">code = post = </span><span class="s1">''</span>
|
|
<span class="k">for</span> <span class="nx">exp</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">@expressions</span>
|
|
<span class="nv">exp = </span><span class="nx">exp</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span>
|
|
<span class="k">break</span> <span class="nx">unless</span> <span class="nx">exp</span> <span class="k">instanceof</span> <span class="nx">Comment</span> <span class="o">or</span> <span class="nx">exp</span> <span class="k">instanceof</span> <span class="nx">Literal</span>
|
|
<span class="nv">o = </span><span class="nx">merge</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">level</span><span class="o">:</span> <span class="nx">LEVEL_TOP</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="nx">i</span>
|
|
<span class="nv">rest = </span><span class="nx">@expressions</span><span class="p">.</span><span class="nx">splice</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">@expressions</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="nv">code = </span><span class="nx">@compileNode</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
<span class="vi">@expressions = </span><span class="nx">rest</span>
|
|
<span class="nv">post = </span><span class="nx">@compileNode</span> <span class="nx">o</span>
|
|
<span class="p">{</span><span class="nx">scope</span><span class="p">}</span> <span class="o">=</span> <span class="nx">o</span>
|
|
<span class="k">if</span> <span class="nx">scope</span><span class="p">.</span><span class="nx">expressions</span> <span class="o">is</span> <span class="k">this</span>
|
|
<span class="nv">declars = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">hasDeclarations</span><span class="p">()</span>
|
|
<span class="nv">assigns = </span><span class="nx">scope</span><span class="p">.</span><span class="nx">hasAssignments</span>
|
|
<span class="k">if</span> <span class="p">(</span><span class="nx">declars</span> <span class="o">or</span> <span class="nx">assigns</span><span class="p">)</span> <span class="o">and</span> <span class="nx">i</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s1">'\n'</span>
|
|
<span class="k">if</span> <span class="nx">declars</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s2">"#{@tab}var #{ scope.declaredVariables().join(', ') };\n"</span>
|
|
<span class="k">if</span> <span class="nx">assigns</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s2">"#{@tab}var #{ multident scope.assignedVariables().join(', '), @tab };\n"</span>
|
|
<span class="nx">code</span> <span class="o">+</span> <span class="nx">post</span></pre></div> </td> </tr> <tr id="section-31"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-31">¶</a> </div> <p>Wrap up the given nodes as a <strong>Block</strong>, unless it already happens
|
|
to be one.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@wrap</span><span class="o">:</span> <span class="p">(</span><span class="nx">nodes</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="nx">nodes</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">1</span> <span class="o">and</span> <span class="nx">nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nx">Block</span>
|
|
<span class="k">new</span> <span class="nx">Block</span> <span class="nx">nodes</span></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-32">¶</a> </div> <h3>Literal</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-33">¶</a> </div> <p>Literals are static values that can be passed through directly into
|
|
JavaScript without translation, such as: strings, numbers,
|
|
<code>true</code>, <code>false</code>, <code>null</code>...</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Literal = </span><span class="nx">class</span> <span class="nx">Literal</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@value</span><span class="p">)</span> <span class="o">-></span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@isStatement</span><span class="p">()</span> <span class="k">then</span> <span class="k">this</span> <span class="k">else</span> <span class="k">new</span> <span class="nx">Return</span> <span class="k">this</span>
|
|
|
|
<span class="nx">isAssignable</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span> <span class="nx">@value</span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@value</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'break'</span><span class="p">,</span> <span class="s1">'continue'</span><span class="p">,</span> <span class="s1">'debugger'</span><span class="p">]</span>
|
|
|
|
<span class="nx">isComplex</span><span class="o">:</span> <span class="nx">NO</span>
|
|
|
|
<span class="nx">assigns</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">name</span> <span class="o">is</span> <span class="nx">@value</span>
|
|
|
|
<span class="nx">jumps</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="kc">no</span> <span class="nx">unless</span> <span class="nx">@isStatement</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="o">not</span> <span class="p">(</span><span class="nx">o</span> <span class="o">and</span> <span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nx">loop</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">block</span> <span class="o">and</span> <span class="p">(</span><span class="nx">@value</span> <span class="o">isnt</span> <span class="s1">'continue'</span><span class="p">)))</span> <span class="k">then</span> <span class="k">this</span> <span class="k">else</span> <span class="kc">no</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">code = </span><span class="k">if</span> <span class="nx">@isUndefined</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">>=</span> <span class="nx">LEVEL_ACCESS</span> <span class="k">then</span> <span class="s1">'(void 0)'</span> <span class="k">else</span> <span class="s1">'void 0'</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">@value</span><span class="p">.</span><span class="nx">reserved</span>
|
|
<span class="s2">"\"#{@value}\""</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">@value</span>
|
|
<span class="k">if</span> <span class="nx">@isStatement</span><span class="p">()</span> <span class="k">then</span> <span class="s2">"#{@tab}#{code};"</span> <span class="k">else</span> <span class="nx">code</span>
|
|
|
|
<span class="nx">toString</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="s1">' "'</span> <span class="o">+</span> <span class="nx">@value</span> <span class="o">+</span> <span class="s1">'"'</span></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-34">¶</a> </div> <h3>Return</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-35"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-35">¶</a> </div> <p>A <code>return</code> is a <em>pureStatement</em> -- wrapping it in a closure wouldn't
|
|
make sense.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Return = </span><span class="nx">class</span> <span class="nx">Return</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">expr</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@expression = </span><span class="nx">expr</span> <span class="k">if</span> <span class="nx">expr</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">().</span><span class="nx">isUndefined</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'expression'</span><span class="p">]</span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="nx">THIS</span>
|
|
<span class="nx">jumps</span><span class="o">:</span> <span class="nx">THIS</span>
|
|
|
|
<span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">level</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">expr = </span><span class="nx">@expression</span><span class="o">?</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="nx">expr</span> <span class="o">and</span> <span class="nx">expr</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Return</span> <span class="k">then</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">level</span> <span class="k">else</span> <span class="k">super</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">level</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@tab</span> <span class="o">+</span> <span class="s2">"return#{ if @expression then ' ' + @expression.compile(o, LEVEL_PAREN) else '' };"</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-36">¶</a> </div> <h3>Value</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-37"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-37">¶</a> </div> <p>A value, variable or literal or parenthesized, indexed or dotted into,
|
|
or vanilla.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Value = </span><span class="nx">class</span> <span class="nx">Value</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">base</span><span class="p">,</span> <span class="nx">props</span><span class="p">,</span> <span class="nx">tag</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">base</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">props</span> <span class="o">and</span> <span class="nx">base</span> <span class="k">instanceof</span> <span class="nx">Value</span>
|
|
<span class="vi">@base = </span><span class="nx">base</span>
|
|
<span class="vi">@properties = </span><span class="nx">props</span> <span class="o">or</span> <span class="p">[]</span>
|
|
<span class="err">@</span><span class="p">[</span><span class="nx">tag</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span> <span class="k">if</span> <span class="nx">tag</span>
|
|
<span class="k">return</span> <span class="k">this</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'base'</span><span class="p">,</span> <span class="s1">'properties'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-38"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-38">¶</a> </div> <p>Add a property access to the list.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">push</span><span class="o">:</span> <span class="p">(</span><span class="nx">prop</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@properties</span><span class="p">.</span><span class="nx">push</span> <span class="nx">prop</span>
|
|
<span class="k">this</span>
|
|
|
|
<span class="nx">hasProperties</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="o">!!</span><span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span></pre></div> </td> </tr> <tr id="section-39"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-39">¶</a> </div> <p>Some boolean checks for the benefit of other nodes.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">isArray</span> <span class="o">:</span> <span class="o">-></span> <span class="o">not</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">and</span> <span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">Arr</span>
|
|
<span class="nx">isComplex</span> <span class="o">:</span> <span class="o">-></span> <span class="nx">@hasProperties</span><span class="p">()</span> <span class="o">or</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
|
|
<span class="nx">isAssignable</span> <span class="o">:</span> <span class="o">-></span> <span class="nx">@hasProperties</span><span class="p">()</span> <span class="o">or</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">isAssignable</span><span class="p">()</span>
|
|
<span class="nx">isSimpleNumber</span> <span class="o">:</span> <span class="o">-></span> <span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">Literal</span> <span class="o">and</span> <span class="nx">SIMPLENUM</span><span class="p">.</span><span class="nx">test</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">value</span>
|
|
<span class="nx">isAtomic</span> <span class="o">:</span> <span class="o">-></span>
|
|
<span class="k">for</span> <span class="nx">node</span> <span class="k">in</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">concat</span> <span class="nx">@base</span>
|
|
<span class="k">return</span> <span class="kc">no</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">soak</span> <span class="o">or</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Call</span>
|
|
<span class="kc">yes</span>
|
|
|
|
<span class="nx">isStatement</span> <span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span> <span class="o">not</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">and</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">isStatement</span> <span class="nx">o</span>
|
|
<span class="nx">assigns</span> <span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span> <span class="o">not</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">and</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">assigns</span> <span class="nx">name</span>
|
|
<span class="nx">jumps</span> <span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span> <span class="o">not</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">and</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">jumps</span> <span class="nx">o</span>
|
|
|
|
<span class="nx">isObject</span><span class="o">:</span> <span class="p">(</span><span class="nx">onlyGenerated</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="kc">no</span> <span class="k">if</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="p">(</span><span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">Obj</span><span class="p">)</span> <span class="o">and</span> <span class="p">(</span><span class="o">not</span> <span class="nx">onlyGenerated</span> <span class="o">or</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">generated</span><span class="p">)</span>
|
|
|
|
<span class="nx">isSplice</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">last</span><span class="p">(</span><span class="nx">@properties</span><span class="p">)</span> <span class="k">instanceof</span> <span class="nx">Slice</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="k">then</span> <span class="k">super</span><span class="p">()</span> <span class="k">else</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-40">¶</a> </div> <p>The value can be unwrapped as its inner node, if there are no attached
|
|
properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">unwrap</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="k">then</span> <span class="k">this</span> <span class="k">else</span> <span class="nx">@base</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-41">¶</a> </div> <p>A reference has base part (<code>this</code> value) and name part.
|
|
We cache them separately for compiling complex expressions.
|
|
<code>a()[b()] ?= c</code> -> <code>(_base = a())[_name = b()] ? _base[_name] = c</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">cacheReference</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">name = </span><span class="nx">last</span> <span class="nx">@properties</span>
|
|
<span class="k">if</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o"><</span> <span class="mi">2</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">name</span><span class="o">?</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="p">[</span><span class="k">this</span><span class="p">,</span> <span class="k">this</span><span class="p">]</span> <span class="c1"># `a` `a.b`</span>
|
|
<span class="nv">base = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">@base</span><span class="p">,</span> <span class="nx">@properties</span><span class="p">[...</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="nx">base</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span> <span class="c1"># `a().b`</span>
|
|
<span class="nv">bref = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'base'</span>
|
|
<span class="nv">base = </span><span class="k">new</span> <span class="nx">Value</span> <span class="k">new</span> <span class="nx">Parens</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">bref</span><span class="p">,</span> <span class="nx">base</span>
|
|
<span class="k">return</span> <span class="p">[</span><span class="nx">base</span><span class="p">,</span> <span class="nx">bref</span><span class="p">]</span> <span class="nx">unless</span> <span class="nx">name</span> <span class="c1"># `a()`</span>
|
|
<span class="k">if</span> <span class="nx">name</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span> <span class="c1"># `a[b()]`</span>
|
|
<span class="nv">nref = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'name'</span>
|
|
<span class="nv">name = </span><span class="k">new</span> <span class="nx">Index</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">nref</span><span class="p">,</span> <span class="nx">name</span><span class="p">.</span><span class="nx">index</span>
|
|
<span class="nv">nref = </span><span class="k">new</span> <span class="nx">Index</span> <span class="nx">nref</span>
|
|
<span class="p">[</span><span class="nx">base</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">name</span><span class="p">),</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">bref</span> <span class="o">or</span> <span class="nx">base</span><span class="p">.</span><span class="nx">base</span><span class="p">,</span> <span class="p">[</span><span class="nx">nref</span> <span class="o">or</span> <span class="nx">name</span><span class="p">])]</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-42">¶</a> </div> <p>We compile a value to JavaScript by compiling and joining each property.
|
|
Things get much more interesting if the chain of properties has <em>soak</em>
|
|
operators <code>?.</code> interspersed. Then we have to take care not to accidentally
|
|
evaluate anything twice when building the soak chain.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@base.front = </span><span class="nx">@front</span>
|
|
<span class="nv">props = </span><span class="nx">@properties</span>
|
|
<span class="nv">code = </span><span class="nx">@base</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="k">if</span> <span class="nx">props</span><span class="p">.</span><span class="nx">length</span> <span class="k">then</span> <span class="nx">LEVEL_ACCESS</span> <span class="k">else</span> <span class="kc">null</span>
|
|
<span class="nv">code = </span><span class="s2">"#{code}."</span> <span class="k">if</span> <span class="p">(</span><span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">Parens</span> <span class="o">or</span> <span class="nx">props</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="o">and</span> <span class="nx">SIMPLENUM</span><span class="p">.</span><span class="nx">test</span> <span class="nx">code</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span> <span class="k">for</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">props</span>
|
|
<span class="nx">code</span></pre></div> </td> </tr> <tr id="section-43"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-43">¶</a> </div> <p>Unfold a soak into an <code>If</code>: <code>a?.b</code> -> <code>a.b if a?</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">unfoldSoak</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">@unfoldedSoak</span> <span class="k">if</span> <span class="nx">@unfoldedSoak</span><span class="o">?</span>
|
|
<span class="nv">result = </span><span class="nx">do</span> <span class="o">=></span>
|
|
<span class="k">if</span> <span class="nv">ifn = </span><span class="nx">@base</span><span class="p">.</span><span class="nx">unfoldSoak</span> <span class="nx">o</span>
|
|
<span class="nb">Array</span><span class="o">::</span><span class="nx">push</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">ifn</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">properties</span><span class="p">,</span> <span class="nx">@properties</span>
|
|
<span class="k">return</span> <span class="nx">ifn</span>
|
|
<span class="k">for</span> <span class="nx">prop</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">@properties</span> <span class="k">when</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">soak</span>
|
|
<span class="nv">prop.soak = </span><span class="kc">off</span>
|
|
<span class="nv">fst = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">@base</span><span class="p">,</span> <span class="nx">@properties</span><span class="p">[...</span><span class="nx">i</span><span class="p">]</span>
|
|
<span class="nv">snd = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">@base</span><span class="p">,</span> <span class="nx">@properties</span><span class="p">[</span><span class="nx">i</span><span class="p">..]</span>
|
|
<span class="k">if</span> <span class="nx">fst</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
|
|
<span class="nv">ref = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'ref'</span>
|
|
<span class="nv">fst = </span><span class="k">new</span> <span class="nx">Parens</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">ref</span><span class="p">,</span> <span class="nx">fst</span>
|
|
<span class="nv">snd.base = </span><span class="nx">ref</span>
|
|
<span class="k">return</span> <span class="k">new</span> <span class="nx">If</span> <span class="k">new</span> <span class="nx">Existence</span><span class="p">(</span><span class="nx">fst</span><span class="p">),</span> <span class="nx">snd</span><span class="p">,</span> <span class="nx">soak</span><span class="o">:</span> <span class="kc">on</span>
|
|
<span class="kc">null</span>
|
|
<span class="vi">@unfoldedSoak = </span><span class="nx">result</span> <span class="o">or</span> <span class="kc">no</span></pre></div> </td> </tr> <tr id="section-44"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-44">¶</a> </div> <h3>Comment</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-45"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-45">¶</a> </div> <p>CoffeeScript passes through block comments as JavaScript block comments
|
|
at the same position.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Comment = </span><span class="nx">class</span> <span class="nx">Comment</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@comment</span><span class="p">)</span> <span class="o">-></span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="nx">THIS</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">level</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">code = </span><span class="s1">'/*'</span> <span class="o">+</span> <span class="nx">multident</span><span class="p">(</span><span class="nx">@comment</span><span class="p">,</span> <span class="nx">@tab</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'*/'</span>
|
|
<span class="nv">code = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+</span> <span class="nx">code</span> <span class="k">if</span> <span class="p">(</span><span class="nx">level</span> <span class="o">or</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span><span class="p">)</span> <span class="o">is</span> <span class="nx">LEVEL_TOP</span>
|
|
<span class="nx">code</span></pre></div> </td> </tr> <tr id="section-46"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-46">¶</a> </div> <h3>Call</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-47"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-47">¶</a> </div> <p>Node for a function invocation. Takes care of converting <code>super()</code> calls into
|
|
calls against the prototype's function of the same name.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Call = </span><span class="nx">class</span> <span class="nx">Call</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">variable</span><span class="p">,</span> <span class="vi">@args = </span><span class="p">[],</span> <span class="nx">@soak</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@isNew = </span><span class="kc">false</span>
|
|
<span class="vi">@isSuper = </span><span class="nx">variable</span> <span class="o">is</span> <span class="s1">'super'</span>
|
|
<span class="vi">@variable = </span><span class="k">if</span> <span class="nx">@isSuper</span> <span class="k">then</span> <span class="kc">null</span> <span class="k">else</span> <span class="nx">variable</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'variable'</span><span class="p">,</span> <span class="s1">'args'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-48">¶</a> </div> <p>Tag this invocation as creating a new instance.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">newInstance</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nv">base = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">base</span> <span class="o">or</span> <span class="nx">@variable</span>
|
|
<span class="k">if</span> <span class="nx">base</span> <span class="k">instanceof</span> <span class="nx">Call</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">base</span><span class="p">.</span><span class="nx">isNew</span>
|
|
<span class="nx">base</span><span class="p">.</span><span class="nx">newInstance</span><span class="p">()</span>
|
|
<span class="k">else</span>
|
|
<span class="vi">@isNew = </span><span class="kc">true</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-49"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-49">¶</a> </div> <p>Grab the reference to the superclass's implementation of the current
|
|
method.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">superReference</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">{</span><span class="nx">method</span><span class="p">}</span> <span class="o">=</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span>
|
|
<span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">'cannot call super outside of a function.'</span> <span class="nx">unless</span> <span class="nx">method</span>
|
|
<span class="p">{</span><span class="nx">name</span><span class="p">}</span> <span class="o">=</span> <span class="nx">method</span>
|
|
<span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">'cannot call super on an anonymous function.'</span> <span class="nx">unless</span> <span class="nx">name</span><span class="o">?</span>
|
|
<span class="k">if</span> <span class="nx">method</span><span class="p">.</span><span class="nx">klass</span>
|
|
<span class="p">(</span><span class="k">new</span> <span class="nx">Value</span> <span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">method</span><span class="p">.</span><span class="nx">klass</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">Access</span><span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span> <span class="s2">"__super__"</span><span class="p">),</span> <span class="k">new</span> <span class="nx">Access</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="nx">name</span><span class="p">]).</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="k">else</span>
|
|
<span class="s2">"#{name}.__super__.constructor"</span></pre></div> </td> </tr> <tr id="section-50"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-50">¶</a> </div> <p>Soaked chained invocations unfold into if/else ternary structures.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">unfoldSoak</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@soak</span>
|
|
<span class="k">if</span> <span class="nx">@variable</span>
|
|
<span class="k">return</span> <span class="nx">ifn</span> <span class="k">if</span> <span class="nv">ifn = </span><span class="nx">unfoldSoak</span> <span class="nx">o</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="s1">'variable'</span>
|
|
<span class="p">[</span><span class="nx">left</span><span class="p">,</span> <span class="nx">rite</span><span class="p">]</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">@variable</span><span class="p">).</span><span class="nx">cacheReference</span> <span class="nx">o</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">left = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">@superReference</span> <span class="nx">o</span>
|
|
<span class="nv">rite = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">left</span>
|
|
<span class="nv">rite = </span><span class="k">new</span> <span class="nx">Call</span> <span class="nx">rite</span><span class="p">,</span> <span class="nx">@args</span>
|
|
<span class="nv">rite.isNew = </span><span class="nx">@isNew</span>
|
|
<span class="nv">left = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="s2">"typeof #{ left.compile o } === \"function\""</span>
|
|
<span class="k">return</span> <span class="k">new</span> <span class="nx">If</span> <span class="nx">left</span><span class="p">,</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">rite</span><span class="p">),</span> <span class="nx">soak</span><span class="o">:</span> <span class="kc">yes</span>
|
|
<span class="nv">call = </span><span class="k">this</span>
|
|
<span class="nv">list = </span><span class="p">[]</span>
|
|
<span class="nx">loop</span>
|
|
<span class="k">if</span> <span class="nx">call</span><span class="p">.</span><span class="nx">variable</span> <span class="k">instanceof</span> <span class="nx">Call</span>
|
|
<span class="nx">list</span><span class="p">.</span><span class="nx">push</span> <span class="nx">call</span>
|
|
<span class="nv">call = </span><span class="nx">call</span><span class="p">.</span><span class="nx">variable</span>
|
|
<span class="k">continue</span>
|
|
<span class="k">break</span> <span class="nx">unless</span> <span class="nx">call</span><span class="p">.</span><span class="nx">variable</span> <span class="k">instanceof</span> <span class="nx">Value</span>
|
|
<span class="nx">list</span><span class="p">.</span><span class="nx">push</span> <span class="nx">call</span>
|
|
<span class="k">break</span> <span class="nx">unless</span> <span class="p">(</span><span class="nv">call = </span><span class="nx">call</span><span class="p">.</span><span class="nx">variable</span><span class="p">.</span><span class="nx">base</span><span class="p">)</span> <span class="k">instanceof</span> <span class="nx">Call</span>
|
|
<span class="k">for</span> <span class="nx">call</span> <span class="k">in</span> <span class="nx">list</span><span class="p">.</span><span class="nx">reverse</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="nx">ifn</span>
|
|
<span class="k">if</span> <span class="nx">call</span><span class="p">.</span><span class="nx">variable</span> <span class="k">instanceof</span> <span class="nx">Call</span>
|
|
<span class="nv">call.variable = </span><span class="nx">ifn</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">call.variable.base = </span><span class="nx">ifn</span>
|
|
<span class="nv">ifn = </span><span class="nx">unfoldSoak</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">call</span><span class="p">,</span> <span class="s1">'variable'</span>
|
|
<span class="nx">ifn</span></pre></div> </td> </tr> <tr id="section-51"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-51">¶</a> </div> <p>Walk through the objects in the arguments, moving over simple values.
|
|
This allows syntax like <code>call a: b, c</code> into <code>call({a: b}, c);</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">filterImplicitObjects</span><span class="o">:</span> <span class="p">(</span><span class="nx">list</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">nodes = </span><span class="p">[]</span>
|
|
<span class="k">for</span> <span class="nx">node</span> <span class="k">in</span> <span class="nx">list</span>
|
|
<span class="nx">unless</span> <span class="nx">node</span><span class="p">.</span><span class="nx">isObject</span><span class="o">?</span><span class="p">()</span> <span class="o">and</span> <span class="nx">node</span><span class="p">.</span><span class="nx">base</span><span class="p">.</span><span class="nx">generated</span>
|
|
<span class="nx">nodes</span><span class="p">.</span><span class="nx">push</span> <span class="nx">node</span>
|
|
<span class="k">continue</span>
|
|
<span class="nv">obj = </span><span class="kc">null</span>
|
|
<span class="k">for</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">node</span><span class="p">.</span><span class="nx">base</span><span class="p">.</span><span class="nx">properties</span>
|
|
<span class="k">if</span> <span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">Assign</span> <span class="o">or</span> <span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">Comment</span>
|
|
<span class="nx">nodes</span><span class="p">.</span><span class="nx">push</span> <span class="nv">obj = </span><span class="k">new</span> <span class="nx">Obj</span> <span class="nv">properties = </span><span class="p">[],</span> <span class="kc">true</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">obj</span>
|
|
<span class="nx">properties</span><span class="p">.</span><span class="nx">push</span> <span class="nx">prop</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">nodes</span><span class="p">.</span><span class="nx">push</span> <span class="nx">prop</span>
|
|
<span class="nv">obj = </span><span class="kc">null</span>
|
|
<span class="nx">nodes</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-52">¶</a> </div> <p>Compile a vanilla function call.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@variable</span><span class="o">?</span><span class="p">.</span><span class="nv">front = </span><span class="nx">@front</span>
|
|
<span class="k">if</span> <span class="nv">code = </span><span class="nx">Splat</span><span class="p">.</span><span class="nx">compileSplattedArray</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">@args</span><span class="p">,</span> <span class="kc">true</span>
|
|
<span class="k">return</span> <span class="nx">@compileSplat</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">code</span>
|
|
<span class="nv">args = </span><span class="nx">@filterImplicitObjects</span> <span class="nx">@args</span>
|
|
<span class="nv">args = </span><span class="p">(</span><span class="nx">arg</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span> <span class="k">for</span> <span class="nx">arg</span> <span class="k">in</span> <span class="nx">args</span><span class="p">).</span><span class="nx">join</span> <span class="s1">', '</span>
|
|
<span class="k">if</span> <span class="nx">@isSuper</span>
|
|
<span class="nx">@superReference</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">+</span> <span class="s2">".call(this#{ args and ', ' + args })"</span>
|
|
<span class="k">else</span>
|
|
<span class="p">(</span><span class="k">if</span> <span class="nx">@isNew</span> <span class="k">then</span> <span class="s1">'new '</span> <span class="k">else</span> <span class="s1">''</span><span class="p">)</span> <span class="o">+</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_ACCESS</span><span class="p">)</span> <span class="o">+</span> <span class="s2">"(#{args})"</span></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-53">¶</a> </div> <p><code>super()</code> is converted into a call against the superclass's implementation
|
|
of the current function.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileSuper</span><span class="o">:</span> <span class="p">(</span><span class="nx">args</span><span class="p">,</span> <span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="s2">"#{@superReference(o)}.call(this#{ if args.length then ', ' else '' }#{args})"</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-54">¶</a> </div> <p>If you call a function with a splat, it's converted into a JavaScript
|
|
<code>.apply()</code> call to allow an array of arguments to be passed.
|
|
If it's a constructor, then things get real tricky. We have to inject an
|
|
inner constructor in order to be able to pass the varargs.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileSplat</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">splatArgs</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="s2">"#{ @superReference o }.apply(this, #{splatArgs})"</span> <span class="k">if</span> <span class="nx">@isSuper</span>
|
|
<span class="k">if</span> <span class="nx">@isNew</span>
|
|
<span class="nv">idt = </span><span class="nx">@tab</span> <span class="o">+</span> <span class="nx">TAB</span>
|
|
<span class="k">return</span> <span class="s2">"""</span>
|
|
<span class="s2"> (function(func, args, ctor) {</span>
|
|
<span class="s2"> #{idt}ctor.prototype = func.prototype;</span>
|
|
<span class="s2"> #{idt}var child = new ctor, result = func.apply(child, args);</span>
|
|
<span class="s2"> #{idt}return typeof result === "</span><span class="nx">object</span><span class="s2">" ? result : child;</span>
|
|
<span class="s2"> #{@tab}})(#{ @variable.compile o, LEVEL_LIST }, #{splatArgs}, function() {})</span>
|
|
<span class="s2"> """</span>
|
|
<span class="nv">base = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">@variable</span>
|
|
<span class="k">if</span> <span class="p">(</span><span class="nv">name = </span><span class="nx">base</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">pop</span><span class="p">())</span> <span class="o">and</span> <span class="nx">base</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
|
|
<span class="nv">ref = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'ref'</span>
|
|
<span class="nv">fun = </span><span class="s2">"(#{ref} = #{ base.compile o, LEVEL_LIST })#{ name.compile o }"</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">fun = </span><span class="nx">base</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_ACCESS</span>
|
|
<span class="nv">fun = </span><span class="s2">"(#{fun})"</span> <span class="k">if</span> <span class="nx">SIMPLENUM</span><span class="p">.</span><span class="nx">test</span> <span class="nx">fun</span>
|
|
<span class="k">if</span> <span class="nx">name</span>
|
|
<span class="nv">ref = </span><span class="nx">fun</span>
|
|
<span class="nx">fun</span> <span class="o">+=</span> <span class="nx">name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">ref = </span><span class="s1">'null'</span>
|
|
<span class="s2">"#{fun}.apply(#{ref}, #{splatArgs})"</span></pre></div> </td> </tr> <tr id="section-55"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-55">¶</a> </div> <h3>Extends</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-56">¶</a> </div> <p>Node to extend an object's prototype with an ancestor object.
|
|
After <code>goog.inherits</code> from the
|
|
<a href="http://closure-library.googlecode.com/svn/docs/closureGoogBase.js.html">Closure Library</a>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Extends = </span><span class="nx">class</span> <span class="nx">Extends</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@child</span><span class="p">,</span> <span class="nx">@parent</span><span class="p">)</span> <span class="o">-></span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'child'</span><span class="p">,</span> <span class="s1">'parent'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-57">¶</a> </div> <p>Hooks one constructor into another's prototype chain.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">utility</span> <span class="s1">'hasProp'</span>
|
|
<span class="k">new</span> <span class="nx">Call</span><span class="p">(</span><span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">utility</span> <span class="s1">'extends'</span><span class="p">),</span> <span class="p">[</span><span class="nx">@child</span><span class="p">,</span> <span class="nx">@parent</span><span class="p">]).</span><span class="nx">compile</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-58">¶</a> </div> <h3>Access</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-59">¶</a> </div> <p>A <code>.</code> access into a property of a value, or the <code>::</code> shorthand for
|
|
an access into the object's prototype.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Access = </span><span class="nx">class</span> <span class="nx">Access</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@name</span><span class="p">,</span> <span class="nx">tag</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@name.asKey = </span><span class="kc">yes</span>
|
|
<span class="vi">@proto = </span><span class="k">if</span> <span class="nx">tag</span> <span class="o">is</span> <span class="s1">'proto'</span> <span class="k">then</span> <span class="s1">'.prototype'</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="vi">@soak = </span><span class="nx">tag</span> <span class="o">is</span> <span class="s1">'soak'</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'name'</span><span class="p">]</span>
|
|
|
|
<span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">name = </span><span class="nx">@name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nx">@proto</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span> <span class="nx">name</span> <span class="k">then</span> <span class="s2">".#{name}"</span> <span class="k">else</span> <span class="s2">"[#{name}]"</span>
|
|
|
|
<span class="nx">isComplex</span><span class="o">:</span> <span class="nx">NO</span></pre></div> </td> </tr> <tr id="section-60"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-60">¶</a> </div> <h3>Index</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-61"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-61">¶</a> </div> <p>A <code>[ ... ]</code> indexed access into an array or object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Index = </span><span class="nx">class</span> <span class="nx">Index</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@index</span><span class="p">)</span> <span class="o">-></span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'index'</span><span class="p">]</span>
|
|
|
|
<span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">(</span><span class="k">if</span> <span class="nx">@proto</span> <span class="k">then</span> <span class="s1">'.prototype'</span> <span class="k">else</span> <span class="s1">''</span><span class="p">)</span> <span class="o">+</span> <span class="s2">"[#{ @index.compile o, LEVEL_PAREN }]"</span>
|
|
|
|
<span class="nx">isComplex</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@index</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-62">¶</a> </div> <h3>Range</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-63">¶</a> </div> <p>A range literal. Ranges can be used to extract portions (slices) of arrays,
|
|
to specify a range for comprehensions, or as a value, to be expanded into the
|
|
corresponding array of integers at runtime.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Range = </span><span class="nx">class</span> <span class="nx">Range</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'from'</span><span class="p">,</span> <span class="s1">'to'</span><span class="p">]</span>
|
|
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@from</span><span class="p">,</span> <span class="nx">@to</span><span class="p">,</span> <span class="nx">tag</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@exclusive = </span><span class="nx">tag</span> <span class="o">is</span> <span class="s1">'exclusive'</span>
|
|
<span class="vi">@equals = </span><span class="k">if</span> <span class="nx">@exclusive</span> <span class="k">then</span> <span class="s1">''</span> <span class="k">else</span> <span class="s1">'='</span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-64">¶</a> </div> <p>Compiles the range's source variables -- where it starts and where it ends.
|
|
But only if they need to be cached to avoid double evaluation.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileVariables</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">o = </span><span class="nx">merge</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">top</span><span class="o">:</span> <span class="kc">true</span>
|
|
<span class="p">[</span><span class="nx">@fromC</span><span class="p">,</span> <span class="nx">@fromVar</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@from</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="p">[</span><span class="nx">@toC</span><span class="p">,</span> <span class="nx">@toVar</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@to</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="p">[</span><span class="nx">@step</span><span class="p">,</span> <span class="nx">@stepVar</span><span class="p">]</span> <span class="o">=</span> <span class="nx">step</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span> <span class="k">if</span> <span class="nv">step = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'step'</span>
|
|
<span class="p">[</span><span class="nx">@fromNum</span><span class="p">,</span> <span class="nx">@toNum</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nx">@fromVar</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">SIMPLENUM</span><span class="p">),</span> <span class="nx">@toVar</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">SIMPLENUM</span><span class="p">)]</span>
|
|
<span class="vi">@stepNum = </span><span class="nx">@stepVar</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">SIMPLENUM</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@stepVar</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-65">¶</a> </div> <p>When compiled normally, the range returns the contents of the <em>for loop</em>
|
|
needed to iterate over the values in the range. Used by comprehensions.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@compileVariables</span> <span class="nx">o</span> <span class="nx">unless</span> <span class="nx">@fromVar</span>
|
|
<span class="k">return</span> <span class="nx">@compileArray</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="nx">unless</span> <span class="nx">o</span><span class="p">.</span><span class="nx">index</span></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-66">¶</a> </div> <p>Set up endpoints.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">known = </span><span class="nx">@fromNum</span> <span class="o">and</span> <span class="nx">@toNum</span>
|
|
<span class="nv">idx = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'index'</span>
|
|
<span class="nv">varPart = </span><span class="s2">"#{idx} = #{@fromC}"</span>
|
|
<span class="nx">varPart</span> <span class="o">+=</span> <span class="s2">", #{@toC}"</span> <span class="k">if</span> <span class="nx">@toC</span> <span class="o">isnt</span> <span class="nx">@toVar</span>
|
|
<span class="nx">varPart</span> <span class="o">+=</span> <span class="s2">", #{@step}"</span> <span class="k">if</span> <span class="nx">@step</span> <span class="o">isnt</span> <span class="nx">@stepVar</span>
|
|
<span class="p">[</span><span class="nx">lt</span><span class="p">,</span> <span class="nx">gt</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"#{idx} <#{@equals}"</span><span class="p">,</span> <span class="s2">"#{idx} >#{@equals}"</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-67">¶</a> </div> <p>Generate the condition.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">condPart = </span><span class="k">if</span> <span class="nx">@stepNum</span>
|
|
<span class="nv">condPart = </span><span class="k">if</span> <span class="o">+</span><span class="nx">@stepNum</span> <span class="o">></span> <span class="mi">0</span> <span class="k">then</span> <span class="s2">"#{lt} #{@toVar}"</span> <span class="k">else</span> <span class="s2">"#{gt} #{@toVar}"</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">known</span>
|
|
<span class="p">[</span><span class="nx">from</span><span class="p">,</span> <span class="nx">to</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="o">+</span><span class="nx">@fromNum</span><span class="p">,</span> <span class="o">+</span><span class="nx">@toNum</span><span class="p">]</span>
|
|
<span class="nv">condPart = </span><span class="k">if</span> <span class="nx">from</span> <span class="o"><=</span> <span class="nx">to</span> <span class="k">then</span> <span class="s2">"#{lt} #{to}"</span> <span class="k">else</span> <span class="s2">"#{gt} #{to}"</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">cond = </span><span class="s2">"#{@fromVar} <= #{@toVar}"</span>
|
|
<span class="nv">condPart = </span><span class="s2">"#{cond} ? #{lt} #{@toVar} : #{gt} #{@toVar}"</span></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-68">¶</a> </div> <p>Generate the step.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">stepPart = </span><span class="k">if</span> <span class="nx">@stepVar</span>
|
|
<span class="s2">"#{idx} += #{@stepVar}"</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">known</span>
|
|
<span class="k">if</span> <span class="nx">from</span> <span class="o"><=</span> <span class="nx">to</span> <span class="k">then</span> <span class="s2">"#{idx}++"</span> <span class="k">else</span> <span class="s2">"#{idx}--"</span>
|
|
<span class="k">else</span>
|
|
<span class="s2">"#{cond} ? #{idx}++ : #{idx}--"</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-69">¶</a> </div> <p>The final loop body.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="s2">"#{varPart}; #{condPart}; #{stepPart}"</span></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-70">¶</a> </div> <p>When used as a value, expand the range into the equivalent array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileArray</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@fromNum</span> <span class="o">and</span> <span class="nx">@toNum</span> <span class="o">and</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">abs</span><span class="p">(</span><span class="nx">@fromNum</span> <span class="o">-</span> <span class="nx">@toNum</span><span class="p">)</span> <span class="o"><=</span> <span class="mi">20</span>
|
|
<span class="nv">range = </span><span class="p">[</span><span class="o">+</span><span class="nx">@fromNum</span><span class="p">..</span><span class="o">+</span><span class="nx">@toNum</span><span class="p">]</span>
|
|
<span class="nx">range</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span> <span class="k">if</span> <span class="nx">@exclusive</span>
|
|
<span class="k">return</span> <span class="s2">"[#{ range.join(', ') }]"</span>
|
|
<span class="nv">idt = </span><span class="nx">@tab</span> <span class="o">+</span> <span class="nx">TAB</span>
|
|
<span class="nv">i = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'i'</span>
|
|
<span class="nv">result = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'results'</span>
|
|
<span class="nv">pre = </span><span class="s2">"\n#{idt}#{result} = [];"</span>
|
|
<span class="k">if</span> <span class="nx">@fromNum</span> <span class="o">and</span> <span class="nx">@toNum</span>
|
|
<span class="nv">o.index = </span><span class="nx">i</span>
|
|
<span class="nv">body = </span><span class="nx">@compileNode</span> <span class="nx">o</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">vars = </span><span class="s2">"#{i} = #{@fromC}"</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@toC</span> <span class="o">isnt</span> <span class="nx">@toVar</span> <span class="k">then</span> <span class="s2">", #{@toC}"</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="nv">cond = </span><span class="s2">"#{@fromVar} <= #{@toVar}"</span>
|
|
<span class="nv">body = </span><span class="s2">"var #{vars}; #{cond} ? #{i} <#{@equals} #{@toVar} : #{i} >#{@equals} #{@toVar}; #{cond} ? #{i}++ : #{i}--"</span>
|
|
<span class="nv">post = </span><span class="s2">"{ #{result}.push(#{i}); }\n#{idt}return #{result};\n#{o.indent}"</span>
|
|
<span class="nv">hasArgs = </span><span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-></span> <span class="nx">node</span><span class="o">?</span><span class="p">.</span><span class="nx">contains</span> <span class="p">(</span><span class="nx">n</span><span class="p">)</span> <span class="o">-></span> <span class="nx">n</span> <span class="k">instanceof</span> <span class="nx">Literal</span> <span class="o">and</span> <span class="nx">n</span><span class="p">.</span><span class="nx">value</span> <span class="o">is</span> <span class="s1">'arguments'</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">n</span><span class="p">.</span><span class="nx">asKey</span>
|
|
<span class="nv">args = </span><span class="s1">', arguments'</span> <span class="k">if</span> <span class="nx">hasArgs</span><span class="p">(</span><span class="nx">@from</span><span class="p">)</span> <span class="o">or</span> <span class="nx">hasArgs</span><span class="p">(</span><span class="nx">@to</span><span class="p">)</span>
|
|
<span class="s2">"(function() {#{pre}\n#{idt}for (#{body})#{post}}).apply(this#{args ? ''})"</span></pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-71">¶</a> </div> <h3>Slice</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-72"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-72">¶</a> </div> <p>An array slice literal. Unlike JavaScript's <code>Array#slice</code>, the second parameter
|
|
specifies the index of the end of the slice, just as the first parameter
|
|
is the index of the beginning.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Slice = </span><span class="nx">class</span> <span class="nx">Slice</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'range'</span><span class="p">]</span>
|
|
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@range</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">super</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-73"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-73">¶</a> </div> <p>We have to be careful when trying to slice through the end of the array,
|
|
<code>9e9</code> is used because not all implementations respect <code>undefined</code> or <code>1/0</code>.
|
|
<code>9e9</code> should be safe because <code>9e9</code> > <code>2**32</code>, the max array length.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">{</span><span class="nx">to</span><span class="p">,</span> <span class="nx">from</span><span class="p">}</span> <span class="o">=</span> <span class="nx">@range</span>
|
|
<span class="nv">fromStr = </span><span class="nx">from</span> <span class="o">and</span> <span class="nx">from</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_PAREN</span><span class="p">)</span> <span class="o">or</span> <span class="s1">'0'</span>
|
|
<span class="nv">compiled = </span><span class="nx">to</span> <span class="o">and</span> <span class="nx">to</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_PAREN</span>
|
|
<span class="k">if</span> <span class="nx">to</span> <span class="o">and</span> <span class="o">not</span> <span class="p">(</span><span class="o">not</span> <span class="nx">@range</span><span class="p">.</span><span class="nx">exclusive</span> <span class="o">and</span> <span class="o">+</span><span class="nx">compiled</span> <span class="o">is</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
|
|
<span class="nv">toStr = </span><span class="s1">', '</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@range</span><span class="p">.</span><span class="nx">exclusive</span>
|
|
<span class="nx">compiled</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">SIMPLENUM</span><span class="p">.</span><span class="nx">test</span> <span class="nx">compiled</span>
|
|
<span class="p">(</span><span class="o">+</span><span class="nx">compiled</span> <span class="o">+</span> <span class="mi">1</span><span class="p">).</span><span class="nx">toString</span><span class="p">()</span>
|
|
<span class="k">else</span>
|
|
<span class="s2">"(#{compiled} + 1) || 9e9"</span>
|
|
<span class="s2">".slice(#{ fromStr }#{ toStr or '' })"</span></pre></div> </td> </tr> <tr id="section-74"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-74">¶</a> </div> <h3>Obj</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-75"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-75">¶</a> </div> <p>An object literal, nothing fancy.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Obj = </span><span class="nx">class</span> <span class="nx">Obj</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">props</span><span class="p">,</span> <span class="vi">@generated = </span><span class="kc">false</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@objects = @properties = </span><span class="nx">props</span> <span class="o">or</span> <span class="p">[]</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'properties'</span><span class="p">]</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">props = </span><span class="nx">@properties</span>
|
|
<span class="k">return</span> <span class="p">(</span><span class="k">if</span> <span class="nx">@front</span> <span class="k">then</span> <span class="s1">'({})'</span> <span class="k">else</span> <span class="s1">'{}'</span><span class="p">)</span> <span class="nx">unless</span> <span class="nx">props</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="k">if</span> <span class="nx">@generated</span>
|
|
<span class="k">for</span> <span class="nx">node</span> <span class="k">in</span> <span class="nx">props</span> <span class="k">when</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Value</span>
|
|
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s1">'cannot have an implicit value in an implicit object'</span>
|
|
<span class="nv">idt = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
|
|
<span class="nv">lastNoncom = </span><span class="nx">@lastNonComment</span> <span class="nx">@properties</span>
|
|
<span class="nv">props = </span><span class="k">for</span> <span class="nx">prop</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">props</span>
|
|
<span class="nv">join = </span><span class="k">if</span> <span class="nx">i</span> <span class="o">is</span> <span class="nx">props</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span>
|
|
<span class="s1">''</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">prop</span> <span class="o">is</span> <span class="nx">lastNoncom</span> <span class="o">or</span> <span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">Comment</span>
|
|
<span class="s1">'\n'</span>
|
|
<span class="k">else</span>
|
|
<span class="s1">',\n'</span>
|
|
<span class="nv">indent = </span><span class="k">if</span> <span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">Comment</span> <span class="k">then</span> <span class="s1">''</span> <span class="k">else</span> <span class="nx">idt</span>
|
|
<span class="k">if</span> <span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span> <span class="nx">prop</span><span class="p">.</span><span class="k">this</span>
|
|
<span class="nv">prop = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span><span class="p">,</span> <span class="nx">prop</span><span class="p">,</span> <span class="s1">'object'</span>
|
|
<span class="k">if</span> <span class="nx">prop</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Comment</span>
|
|
<span class="k">if</span> <span class="nx">prop</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Assign</span>
|
|
<span class="nv">prop = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="nx">prop</span><span class="p">,</span> <span class="nx">prop</span><span class="p">,</span> <span class="s1">'object'</span>
|
|
<span class="p">(</span><span class="nx">prop</span><span class="p">.</span><span class="nx">variable</span><span class="p">.</span><span class="nx">base</span> <span class="o">or</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">variable</span><span class="p">).</span><span class="nv">asKey = </span><span class="kc">yes</span>
|
|
<span class="nx">indent</span> <span class="o">+</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_TOP</span><span class="p">)</span> <span class="o">+</span> <span class="nx">join</span>
|
|
<span class="nv">props = </span><span class="nx">props</span><span class="p">.</span><span class="nx">join</span> <span class="s1">''</span>
|
|
<span class="nv">obj = </span><span class="s2">"{#{ props and '\n' + props + '\n' + @tab }}"</span>
|
|
<span class="k">if</span> <span class="nx">@front</span> <span class="k">then</span> <span class="s2">"(#{obj})"</span> <span class="k">else</span> <span class="nx">obj</span>
|
|
|
|
<span class="nx">assigns</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">for</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">@properties</span> <span class="k">when</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">assigns</span> <span class="nx">name</span> <span class="k">then</span> <span class="k">return</span> <span class="kc">yes</span>
|
|
<span class="kc">no</span></pre></div> </td> </tr> <tr id="section-76"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-76">¶</a> </div> <h3>Arr</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-77"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-77">¶</a> </div> <p>An array literal.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Arr = </span><span class="nx">class</span> <span class="nx">Arr</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">objs</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@objects = </span><span class="nx">objs</span> <span class="o">or</span> <span class="p">[]</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'objects'</span><span class="p">]</span>
|
|
|
|
<span class="nx">filterImplicitObjects</span><span class="o">:</span> <span class="nx">Call</span><span class="o">::</span><span class="nx">filterImplicitObjects</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="s1">'[]'</span> <span class="nx">unless</span> <span class="nx">@objects</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
|
|
<span class="nv">objs = </span><span class="nx">@filterImplicitObjects</span> <span class="nx">@objects</span>
|
|
<span class="k">return</span> <span class="nx">code</span> <span class="k">if</span> <span class="nv">code = </span><span class="nx">Splat</span><span class="p">.</span><span class="nx">compileSplattedArray</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">objs</span>
|
|
<span class="nv">code = </span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span> <span class="k">for</span> <span class="nx">obj</span> <span class="k">in</span> <span class="nx">objs</span><span class="p">).</span><span class="nx">join</span> <span class="s1">', '</span>
|
|
<span class="k">if</span> <span class="nx">code</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s1">'\n'</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">0</span>
|
|
<span class="s2">"[\n#{o.indent}#{code}\n#{@tab}]"</span>
|
|
<span class="k">else</span>
|
|
<span class="s2">"[#{code}]"</span>
|
|
|
|
<span class="nx">assigns</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">for</span> <span class="nx">obj</span> <span class="k">in</span> <span class="nx">@objects</span> <span class="k">when</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">assigns</span> <span class="nx">name</span> <span class="k">then</span> <span class="k">return</span> <span class="kc">yes</span>
|
|
<span class="kc">no</span></pre></div> </td> </tr> <tr id="section-78"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-78">¶</a> </div> <h3>Class</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-79"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-79">¶</a> </div> <p>The CoffeeScript class definition.
|
|
Initialize a <strong>Class</strong> with its name, an optional superclass, and a
|
|
list of prototype property assignments.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Class = </span><span class="nx">class</span> <span class="nx">Class</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@variable</span><span class="p">,</span> <span class="nx">@parent</span><span class="p">,</span> <span class="vi">@body = </span><span class="k">new</span> <span class="nx">Block</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@boundFuncs = </span><span class="p">[]</span>
|
|
<span class="vi">@body.classBody = </span><span class="kc">yes</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'variable'</span><span class="p">,</span> <span class="s1">'parent'</span><span class="p">,</span> <span class="s1">'body'</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-80"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-80">¶</a> </div> <p>Figure out the appropriate name for the constructor function of this class.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">determineName</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="kc">null</span> <span class="nx">unless</span> <span class="nx">@variable</span>
|
|
<span class="nv">decl = </span><span class="k">if</span> <span class="nv">tail = </span><span class="nx">last</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">properties</span>
|
|
<span class="nx">tail</span> <span class="k">instanceof</span> <span class="nx">Access</span> <span class="o">and</span> <span class="nx">tail</span><span class="p">.</span><span class="nx">name</span><span class="p">.</span><span class="nx">value</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">@variable</span><span class="p">.</span><span class="nx">base</span><span class="p">.</span><span class="nx">value</span>
|
|
<span class="nx">decl</span> <span class="o">and=</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">decl</span><span class="p">)</span> <span class="o">and</span> <span class="nx">decl</span></pre></div> </td> </tr> <tr id="section-81"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-81">¶</a> </div> <p>For all <code>this</code>-references and bound functions in the class definition,
|
|
<code>this</code> is the Class being constructed.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">setContext</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@body</span><span class="p">.</span><span class="nx">traverseChildren</span> <span class="kc">false</span><span class="p">,</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">classBody</span>
|
|
<span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Literal</span> <span class="o">and</span> <span class="nx">node</span><span class="p">.</span><span class="nx">value</span> <span class="o">is</span> <span class="s1">'this'</span>
|
|
<span class="nv">node.value = </span><span class="nx">name</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Code</span>
|
|
<span class="nv">node.klass = </span><span class="nx">name</span>
|
|
<span class="nv">node.context = </span><span class="nx">name</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">bound</span></pre></div> </td> </tr> <tr id="section-82"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-82">¶</a> </div> <p>Ensure that all functions bound to the instance are proxied in the
|
|
constructor.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addBoundFunctions</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@boundFuncs</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="k">for</span> <span class="nx">bvar</span> <span class="k">in</span> <span class="nx">@boundFuncs</span>
|
|
<span class="nv">lhs = </span><span class="p">(</span><span class="k">new</span> <span class="nx">Value</span> <span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span> <span class="s2">"this"</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">Access</span> <span class="nx">bvar</span><span class="p">]).</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nx">@ctor</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">unshift</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s2">"#{lhs} = #{utility 'bind'}(#{lhs}, this)"</span></pre></div> </td> </tr> <tr id="section-83"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-83">¶</a> </div> <p>Merge the properties from a top-level object as prototypal properties
|
|
on the class.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addProperties</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">,</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">props = </span><span class="nx">node</span><span class="p">.</span><span class="nx">base</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="mi">0</span><span class="p">..]</span>
|
|
<span class="nv">exprs = </span><span class="k">while</span> <span class="nv">assign = </span><span class="nx">props</span><span class="p">.</span><span class="nx">shift</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="nx">assign</span> <span class="k">instanceof</span> <span class="nx">Assign</span>
|
|
<span class="nv">base = </span><span class="nx">assign</span><span class="p">.</span><span class="nx">variable</span><span class="p">.</span><span class="nx">base</span>
|
|
<span class="k">delete</span> <span class="nx">assign</span><span class="p">.</span><span class="nx">context</span>
|
|
<span class="nv">func = </span><span class="nx">assign</span><span class="p">.</span><span class="nx">value</span>
|
|
<span class="k">if</span> <span class="nx">base</span><span class="p">.</span><span class="nx">value</span> <span class="o">is</span> <span class="s1">'constructor'</span>
|
|
<span class="k">if</span> <span class="nx">@ctor</span>
|
|
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s1">'cannot define more than one constructor in a class'</span>
|
|
<span class="k">if</span> <span class="nx">func</span><span class="p">.</span><span class="nx">bound</span>
|
|
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s1">'cannot define a constructor as a bound function'</span>
|
|
<span class="k">if</span> <span class="nx">func</span> <span class="k">instanceof</span> <span class="nx">Code</span>
|
|
<span class="nv">assign = </span><span class="vi">@ctor = </span><span class="nx">func</span>
|
|
<span class="k">else</span>
|
|
<span class="vi">@externalCtor = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'class'</span>
|
|
<span class="nv">assign = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="k">new</span> <span class="nx">Literal</span><span class="p">(</span><span class="nx">@externalCtor</span><span class="p">),</span> <span class="nx">func</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">unless</span> <span class="nx">assign</span><span class="p">.</span><span class="nx">variable</span><span class="p">.</span><span class="k">this</span>
|
|
<span class="nv">assign.variable = </span><span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span><span class="p">(</span><span class="nx">name</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">Access</span><span class="p">(</span><span class="nx">base</span><span class="p">,</span> <span class="s1">'proto'</span><span class="p">)])</span>
|
|
<span class="k">if</span> <span class="nx">func</span> <span class="k">instanceof</span> <span class="nx">Code</span> <span class="o">and</span> <span class="nx">func</span><span class="p">.</span><span class="nx">bound</span>
|
|
<span class="nx">@boundFuncs</span><span class="p">.</span><span class="nx">push</span> <span class="nx">base</span>
|
|
<span class="nv">func.bound = </span><span class="kc">no</span>
|
|
<span class="nx">assign</span>
|
|
<span class="nx">compact</span> <span class="nx">exprs</span></pre></div> </td> </tr> <tr id="section-84"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-84">¶</a> </div> <p>Walk the body of the class, looking for prototype properties to be converted.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">walkBody</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@traverseChildren</span> <span class="kc">false</span><span class="p">,</span> <span class="p">(</span><span class="nx">child</span><span class="p">)</span> <span class="o">=></span>
|
|
<span class="k">return</span> <span class="kc">false</span> <span class="k">if</span> <span class="nx">child</span> <span class="k">instanceof</span> <span class="nx">Class</span>
|
|
<span class="k">if</span> <span class="nx">child</span> <span class="k">instanceof</span> <span class="nx">Block</span>
|
|
<span class="k">for</span> <span class="nx">node</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nv">exps = </span><span class="nx">child</span><span class="p">.</span><span class="nx">expressions</span>
|
|
<span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span> <span class="nx">node</span><span class="p">.</span><span class="nx">isObject</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span>
|
|
<span class="nx">exps</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@addProperties</span> <span class="nx">node</span><span class="p">,</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">o</span>
|
|
<span class="nv">child.expressions = exps = </span><span class="nx">flatten</span> <span class="nx">exps</span></pre></div> </td> </tr> <tr id="section-85"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-85">¶</a> </div> <p>Make sure that a constructor is defined for the class, and properly
|
|
configured.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">ensureConstructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="o">not</span> <span class="nx">@ctor</span>
|
|
<span class="vi">@ctor = </span><span class="k">new</span> <span class="nx">Code</span>
|
|
<span class="nx">@ctor</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s2">"#{name}.__super__.constructor.apply(this, arguments)"</span> <span class="k">if</span> <span class="nx">@parent</span>
|
|
<span class="nx">@ctor</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s2">"#{@externalCtor}.apply(this, arguments)"</span> <span class="k">if</span> <span class="nx">@externalCtor</span>
|
|
<span class="nx">@body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">@ctor</span>
|
|
<span class="vi">@ctor.ctor = @ctor.name = </span><span class="nx">name</span>
|
|
<span class="vi">@ctor.klass = </span><span class="kc">null</span>
|
|
<span class="vi">@ctor.noReturn = </span><span class="kc">yes</span></pre></div> </td> </tr> <tr id="section-86"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-86">¶</a> </div> <p>Instead of generating the JavaScript string directly, we build up the
|
|
equivalent syntax tree and compile that, in pieces. You can see the
|
|
constructor, property assignments, and inheritance getting built out below.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">decl = </span><span class="nx">@determineName</span><span class="p">()</span>
|
|
<span class="nv">name = </span><span class="nx">decl</span> <span class="o">or</span> <span class="nx">@name</span> <span class="o">or</span> <span class="s1">'_Class'</span>
|
|
<span class="nv">lname = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">name</span>
|
|
|
|
<span class="nx">@setContext</span> <span class="nx">name</span>
|
|
<span class="nx">@walkBody</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">o</span>
|
|
<span class="nx">@ensureConstructor</span> <span class="nx">name</span>
|
|
<span class="nx">@body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">unshift</span> <span class="k">new</span> <span class="nx">Extends</span> <span class="nx">lname</span><span class="p">,</span> <span class="nx">@parent</span> <span class="k">if</span> <span class="nx">@parent</span>
|
|
<span class="nx">@body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">@ctor</span> <span class="nx">unless</span> <span class="nx">@ctor</span> <span class="k">instanceof</span> <span class="nx">Code</span>
|
|
<span class="nx">@body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">push</span> <span class="nx">lname</span>
|
|
<span class="nx">@addBoundFunctions</span> <span class="nx">o</span>
|
|
|
|
<span class="nv">klass = </span><span class="k">new</span> <span class="nx">Parens</span> <span class="nx">Closure</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="nx">@body</span><span class="p">),</span> <span class="kc">true</span>
|
|
<span class="nv">klass = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="nx">@variable</span><span class="p">,</span> <span class="nx">klass</span> <span class="k">if</span> <span class="nx">@variable</span>
|
|
<span class="nx">klass</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-87"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-87">¶</a> </div> <h3>Assign</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-88"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-88">¶</a> </div> <p>The <strong>Assign</strong> is used to assign a local variable to value, or to set the
|
|
property of an object -- including within object literals.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Assign = </span><span class="nx">class</span> <span class="nx">Assign</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@variable</span><span class="p">,</span> <span class="nx">@value</span><span class="p">,</span> <span class="nx">@context</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@param = </span><span class="nx">options</span> <span class="o">and</span> <span class="nx">options</span><span class="p">.</span><span class="nx">param</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'variable'</span><span class="p">,</span> <span class="s1">'value'</span><span class="p">]</span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">o</span><span class="o">?</span><span class="p">.</span><span class="nx">level</span> <span class="o">is</span> <span class="nx">LEVEL_TOP</span> <span class="o">and</span> <span class="nx">@context</span><span class="o">?</span> <span class="o">and</span> <span class="s2">"?"</span> <span class="k">in</span> <span class="nx">@context</span>
|
|
|
|
<span class="nx">assigns</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="err">@</span><span class="p">[</span><span class="k">if</span> <span class="nx">@context</span> <span class="o">is</span> <span class="s1">'object'</span> <span class="k">then</span> <span class="s1">'value'</span> <span class="k">else</span> <span class="s1">'variable'</span><span class="p">].</span><span class="nx">assigns</span> <span class="nx">name</span>
|
|
|
|
<span class="nx">unfoldSoak</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">unfoldSoak</span> <span class="nx">o</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="s1">'variable'</span></pre></div> </td> </tr> <tr id="section-89"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-89">¶</a> </div> <p>Compile an assignment, delegating to <code>compilePatternMatch</code> or
|
|
<code>compileSplice</code> if appropriate. Keep track of the name of the base object
|
|
we've been assigned to, for correct internal references. If the variable
|
|
has not been seen yet within the current scope, declare it.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nv">isValue = </span><span class="nx">@variable</span> <span class="k">instanceof</span> <span class="nx">Value</span>
|
|
<span class="k">return</span> <span class="nx">@compilePatternMatch</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">isArray</span><span class="p">()</span> <span class="o">or</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">isObject</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="nx">@compileSplice</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">isSplice</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="nx">@compileConditional</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@context</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'||='</span><span class="p">,</span> <span class="s1">'&&='</span><span class="p">,</span> <span class="s1">'?='</span><span class="p">]</span>
|
|
<span class="nv">name = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="nx">unless</span> <span class="nx">@context</span> <span class="o">or</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">isAssignable</span><span class="p">()</span>
|
|
<span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s2">"\"#{ @variable.compile o }\" cannot be assigned."</span>
|
|
<span class="nx">unless</span> <span class="nx">@context</span> <span class="o">or</span> <span class="nx">isValue</span> <span class="o">and</span> <span class="p">(</span><span class="nx">@variable</span><span class="p">.</span><span class="nx">namespaced</span> <span class="o">or</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">hasProperties</span><span class="p">())</span>
|
|
<span class="k">if</span> <span class="nx">@param</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">add</span> <span class="nx">name</span><span class="p">,</span> <span class="s1">'var'</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">find</span> <span class="nx">name</span>
|
|
<span class="k">if</span> <span class="nx">@value</span> <span class="k">instanceof</span> <span class="nx">Code</span> <span class="o">and</span> <span class="nv">match = </span><span class="nx">METHOD_DEF</span><span class="p">.</span><span class="nx">exec</span> <span class="nx">name</span>
|
|
<span class="vi">@value.klass = </span><span class="nx">match</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">if</span> <span class="nx">match</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
|
<span class="vi">@value.name = </span><span class="nx">match</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">?</span> <span class="nx">match</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">?</span> <span class="nx">match</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">?</span> <span class="nx">match</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span>
|
|
<span class="nv">val = </span><span class="nx">@value</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="k">return</span> <span class="s2">"#{name}: #{val}"</span> <span class="k">if</span> <span class="nx">@context</span> <span class="o">is</span> <span class="s1">'object'</span>
|
|
<span class="nv">val = </span><span class="nx">name</span> <span class="o">+</span> <span class="s2">" #{ @context or '=' } "</span> <span class="o">+</span> <span class="nx">val</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o"><=</span> <span class="nx">LEVEL_LIST</span> <span class="k">then</span> <span class="nx">val</span> <span class="k">else</span> <span class="s2">"(#{val})"</span></pre></div> </td> </tr> <tr id="section-90"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-90">¶</a> </div> <p>Brief implementation of recursive pattern matching, when assigning array or
|
|
object literals to a value. Peeks at their properties to assign inner names.
|
|
See the <a href="http://wiki.ecmascript.org/doku.php?id=harmony:destructuring">ECMAScript Harmony Wiki</a>
|
|
for details.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compilePatternMatch</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">top = </span><span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">is</span> <span class="nx">LEVEL_TOP</span>
|
|
<span class="p">{</span><span class="nx">value</span><span class="p">}</span> <span class="o">=</span> <span class="k">this</span>
|
|
<span class="p">{</span><span class="nx">objects</span><span class="p">}</span> <span class="o">=</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">base</span>
|
|
<span class="nx">unless</span> <span class="nv">olen = </span><span class="nx">objects</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="nv">code = </span><span class="nx">value</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="k">return</span> <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">>=</span> <span class="nx">LEVEL_OP</span> <span class="k">then</span> <span class="s2">"(#{code})"</span> <span class="k">else</span> <span class="nx">code</span>
|
|
<span class="nv">isObject = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">isObject</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="nx">top</span> <span class="o">and</span> <span class="nx">olen</span> <span class="o">is</span> <span class="mi">1</span> <span class="o">and</span> <span class="p">(</span><span class="nv">obj = </span><span class="nx">objects</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Splat</span></pre></div> </td> </tr> <tr id="section-91"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-91">¶</a> </div> <p>Unroll simplest cases: <code>{v} = x</code> -> <code>v = x.v</code></p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">Assign</span>
|
|
<span class="p">{</span><span class="nx">variable</span><span class="o">:</span> <span class="p">{</span><span class="nx">base</span><span class="o">:</span> <span class="nx">idx</span><span class="p">},</span> <span class="nx">value</span><span class="o">:</span> <span class="nx">obj</span><span class="p">}</span> <span class="o">=</span> <span class="nx">obj</span>
|
|
<span class="k">else</span>
|
|
<span class="k">if</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">base</span> <span class="k">instanceof</span> <span class="nx">Parens</span>
|
|
<span class="p">[</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">idx</span><span class="p">]</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()).</span><span class="nx">cacheReference</span> <span class="nx">o</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">idx = </span><span class="k">if</span> <span class="nx">isObject</span>
|
|
<span class="k">if</span> <span class="nx">obj</span><span class="p">.</span><span class="k">this</span> <span class="k">then</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span> <span class="k">else</span> <span class="nx">obj</span>
|
|
<span class="k">else</span>
|
|
<span class="k">new</span> <span class="nx">Literal</span> <span class="mi">0</span>
|
|
<span class="nv">acc = </span><span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span> <span class="nx">idx</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">().</span><span class="nx">value</span> <span class="o">or</span> <span class="mi">0</span>
|
|
<span class="nv">value = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">value</span>
|
|
<span class="nx">value</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="p">(</span><span class="k">if</span> <span class="nx">acc</span> <span class="k">then</span> <span class="nx">Access</span> <span class="k">else</span> <span class="nx">Index</span><span class="p">)</span> <span class="nx">idx</span>
|
|
<span class="k">return</span> <span class="k">new</span> <span class="nx">Assign</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">param</span><span class="o">:</span> <span class="nx">@param</span><span class="p">).</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_TOP</span>
|
|
<span class="nv">vvar = </span><span class="nx">value</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="nv">assigns = </span><span class="p">[]</span>
|
|
<span class="nv">splat = </span><span class="kc">false</span>
|
|
<span class="k">if</span> <span class="o">not</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">vvar</span><span class="p">)</span> <span class="o">or</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">assigns</span><span class="p">(</span><span class="nx">vvar</span><span class="p">)</span>
|
|
<span class="nx">assigns</span><span class="p">.</span><span class="nx">push</span> <span class="s2">"#{ ref = o.scope.freeVariable 'ref' } = #{vvar}"</span>
|
|
<span class="nv">vvar = </span><span class="nx">ref</span>
|
|
<span class="k">for</span> <span class="nx">obj</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">objects</span></pre></div> </td> </tr> <tr id="section-92"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-92">¶</a> </div> <p>A regular array pattern-match.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">idx = </span><span class="nx">i</span>
|
|
<span class="k">if</span> <span class="nx">isObject</span>
|
|
<span class="k">if</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">Assign</span></pre></div> </td> </tr> <tr id="section-93"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-93">¶</a> </div> <p>A regular object pattern-match.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="p">{</span><span class="nx">variable</span><span class="o">:</span> <span class="p">{</span><span class="nx">base</span><span class="o">:</span> <span class="nx">idx</span><span class="p">},</span> <span class="nx">value</span><span class="o">:</span> <span class="nx">obj</span><span class="p">}</span> <span class="o">=</span> <span class="nx">obj</span>
|
|
<span class="k">else</span></pre></div> </td> </tr> <tr id="section-94"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-94">¶</a> </div> <p>A shorthand <code>{a, b, @c} = val</code> pattern-match.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">base</span> <span class="k">instanceof</span> <span class="nx">Parens</span>
|
|
<span class="p">[</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">idx</span><span class="p">]</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">obj</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()).</span><span class="nx">cacheReference</span> <span class="nx">o</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">idx = </span><span class="k">if</span> <span class="nx">obj</span><span class="p">.</span><span class="k">this</span> <span class="k">then</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span> <span class="k">else</span> <span class="nx">obj</span>
|
|
<span class="k">if</span> <span class="o">not</span> <span class="nx">splat</span> <span class="o">and</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">Splat</span>
|
|
<span class="nv">val = </span><span class="s2">"#{olen} <= #{vvar}.length ? #{ utility 'slice' }.call(#{vvar}, #{i}"</span>
|
|
<span class="k">if</span> <span class="nv">rest = </span><span class="nx">olen</span> <span class="o">-</span> <span class="nx">i</span> <span class="o">-</span> <span class="mi">1</span>
|
|
<span class="nv">ivar = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'i'</span>
|
|
<span class="nx">val</span> <span class="o">+=</span> <span class="s2">", #{ivar} = #{vvar}.length - #{rest}) : (#{ivar} = #{i}, [])"</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">val</span> <span class="o">+=</span> <span class="s2">") : []"</span>
|
|
<span class="nv">val = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">val</span>
|
|
<span class="nv">splat = </span><span class="s2">"#{ivar}++"</span>
|
|
<span class="k">else</span>
|
|
<span class="k">if</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">Splat</span>
|
|
<span class="nv">obj = </span><span class="nx">obj</span><span class="p">.</span><span class="nx">name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="o">\</span>
|
|
<span class="s2">"multiple splats are disallowed in an assignment: #{obj} ..."</span>
|
|
<span class="k">if</span> <span class="k">typeof</span> <span class="nx">idx</span> <span class="o">is</span> <span class="s1">'number'</span>
|
|
<span class="nv">idx = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">splat</span> <span class="o">or</span> <span class="nx">idx</span>
|
|
<span class="nv">acc = </span><span class="kc">no</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">acc = </span><span class="nx">isObject</span> <span class="o">and</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span> <span class="nx">idx</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">().</span><span class="nx">value</span> <span class="o">or</span> <span class="mi">0</span>
|
|
<span class="nv">val = </span><span class="k">new</span> <span class="nx">Value</span> <span class="k">new</span> <span class="nx">Literal</span><span class="p">(</span><span class="nx">vvar</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="p">(</span><span class="k">if</span> <span class="nx">acc</span> <span class="k">then</span> <span class="nx">Access</span> <span class="k">else</span> <span class="nx">Index</span><span class="p">)</span> <span class="nx">idx</span><span class="p">]</span>
|
|
<span class="nx">assigns</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">Assign</span><span class="p">(</span><span class="nx">obj</span><span class="p">,</span> <span class="nx">val</span><span class="p">,</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">param</span><span class="o">:</span> <span class="nx">@param</span><span class="p">).</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_TOP</span>
|
|
<span class="nx">assigns</span><span class="p">.</span><span class="nx">push</span> <span class="nx">vvar</span> <span class="nx">unless</span> <span class="nx">top</span>
|
|
<span class="nv">code = </span><span class="nx">assigns</span><span class="p">.</span><span class="nx">join</span> <span class="s1">', '</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o"><</span> <span class="nx">LEVEL_LIST</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">"(#{code})"</span></pre></div> </td> </tr> <tr id="section-95"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-95">¶</a> </div> <p>When compiling a conditional assignment, take care to ensure that the
|
|
operands are only evaluated once, even though we have to reference them
|
|
more than once.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileConditional</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">[</span><span class="nx">left</span><span class="p">,</span> <span class="nx">rite</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">cacheReference</span> <span class="nx">o</span>
|
|
<span class="k">if</span> <span class="s2">"?"</span> <span class="k">in</span> <span class="nx">@context</span> <span class="k">then</span> <span class="nv">o.isExistentialEquals = </span><span class="kc">true</span>
|
|
<span class="k">new</span> <span class="nx">Op</span><span class="p">(</span><span class="nx">@context</span><span class="p">[</span><span class="mi">0</span><span class="p">...</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="nx">left</span><span class="p">,</span> <span class="k">new</span> <span class="nx">Assign</span><span class="p">(</span><span class="nx">rite</span><span class="p">,</span> <span class="nx">@value</span><span class="p">,</span> <span class="s1">'='</span><span class="p">)</span> <span class="p">).</span><span class="nx">compile</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-96"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-96">¶</a> </div> <p>Compile the assignment from an array splice literal, using JavaScript's
|
|
<code>Array#splice</code> method.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileSplice</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">{</span><span class="nx">range</span><span class="o">:</span> <span class="p">{</span><span class="nx">from</span><span class="p">,</span> <span class="nx">to</span><span class="p">,</span> <span class="nx">exclusive</span><span class="p">}}</span> <span class="o">=</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">pop</span><span class="p">()</span>
|
|
<span class="nv">name = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="p">[</span><span class="nx">fromDecl</span><span class="p">,</span> <span class="nx">fromRef</span><span class="p">]</span> <span class="o">=</span> <span class="nx">from</span><span class="o">?</span><span class="p">.</span><span class="nx">cache</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span><span class="p">)</span> <span class="o">or</span> <span class="p">[</span><span class="s1">'0'</span><span class="p">,</span> <span class="s1">'0'</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="nx">to</span>
|
|
<span class="k">if</span> <span class="nx">from</span><span class="o">?</span><span class="p">.</span><span class="nx">isSimpleNumber</span><span class="p">()</span> <span class="o">and</span> <span class="nx">to</span><span class="p">.</span><span class="nx">isSimpleNumber</span><span class="p">()</span>
|
|
<span class="nv">to = </span><span class="o">+</span><span class="nx">to</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-</span> <span class="o">+</span><span class="nx">fromRef</span>
|
|
<span class="nx">to</span> <span class="o">+=</span> <span class="mi">1</span> <span class="nx">unless</span> <span class="nx">exclusive</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">to = </span><span class="nx">to</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">+</span> <span class="s1">' - '</span> <span class="o">+</span> <span class="nx">fromRef</span>
|
|
<span class="nx">to</span> <span class="o">+=</span> <span class="s1">' + 1'</span> <span class="nx">unless</span> <span class="nx">exclusive</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">to = </span><span class="s2">"9e9"</span>
|
|
<span class="p">[</span><span class="nx">valDef</span><span class="p">,</span> <span class="nx">valRef</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@value</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="nv">code = </span><span class="s2">"[].splice.apply(#{name}, [#{fromDecl}, #{to}].concat(#{valDef})), #{valRef}"</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">></span> <span class="nx">LEVEL_TOP</span> <span class="k">then</span> <span class="s2">"(#{code})"</span> <span class="k">else</span> <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-97"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-97">¶</a> </div> <h3>Code</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-98"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-98">¶</a> </div> <p>A function definition. This is the only node that creates a new Scope.
|
|
When for the purposes of walking the contents of a function body, the Code
|
|
has no <em>children</em> -- they're within the inner scope.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Code = </span><span class="nx">class</span> <span class="nx">Code</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">params</span><span class="p">,</span> <span class="nx">body</span><span class="p">,</span> <span class="nx">tag</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@params = </span><span class="nx">params</span> <span class="o">or</span> <span class="p">[]</span>
|
|
<span class="vi">@body = </span><span class="nx">body</span> <span class="o">or</span> <span class="k">new</span> <span class="nx">Block</span>
|
|
<span class="vi">@bound = </span><span class="nx">tag</span> <span class="o">is</span> <span class="s1">'boundfunc'</span>
|
|
<span class="vi">@context = </span><span class="s1">'this'</span> <span class="k">if</span> <span class="nx">@bound</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'params'</span><span class="p">,</span> <span class="s1">'body'</span><span class="p">]</span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="o">-></span> <span class="o">!!</span><span class="nx">@ctor</span>
|
|
|
|
<span class="nx">jumps</span><span class="o">:</span> <span class="nx">NO</span></pre></div> </td> </tr> <tr id="section-99"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-99">¶</a> </div> <p>Compilation creates a new scope unless explicitly asked to share with the
|
|
outer scope. Handles splat parameters in the parameter list by peeking at
|
|
the JavaScript <code>arguments</code> object. If the function is bound with the <code>=></code>
|
|
arrow, generates a wrapper that saves the current value of <code>this</code> through
|
|
a closure.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">o.scope = </span><span class="k">new</span> <span class="nx">Scope</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">,</span> <span class="nx">@body</span><span class="p">,</span> <span class="k">this</span>
|
|
<span class="nv">o.scope.shared = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'sharedScope'</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
|
|
<span class="k">delete</span> <span class="nx">o</span><span class="p">.</span><span class="nx">bare</span>
|
|
<span class="nv">vars = </span><span class="p">[]</span>
|
|
<span class="nv">exprs = </span><span class="p">[]</span>
|
|
<span class="k">for</span> <span class="nx">param</span> <span class="k">in</span> <span class="nx">@params</span> <span class="k">when</span> <span class="nx">param</span><span class="p">.</span><span class="nx">splat</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">add</span> <span class="nx">p</span><span class="p">.</span><span class="nx">name</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span> <span class="s1">'var'</span><span class="p">,</span> <span class="kc">yes</span> <span class="k">for</span> <span class="nx">p</span> <span class="k">in</span> <span class="nx">@params</span> <span class="k">when</span> <span class="nx">p</span><span class="p">.</span><span class="nx">name</span><span class="p">.</span><span class="nx">value</span>
|
|
<span class="nv">splats = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="k">new</span> <span class="nx">Arr</span><span class="p">(</span><span class="nx">p</span><span class="p">.</span><span class="nx">asReference</span> <span class="nx">o</span> <span class="k">for</span> <span class="nx">p</span> <span class="k">in</span> <span class="nx">@params</span><span class="p">)),</span>
|
|
<span class="k">new</span> <span class="nx">Value</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s1">'arguments'</span>
|
|
<span class="k">break</span>
|
|
<span class="k">for</span> <span class="nx">param</span> <span class="k">in</span> <span class="nx">@params</span>
|
|
<span class="k">if</span> <span class="nx">param</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
|
|
<span class="nv">val = ref = </span><span class="nx">param</span><span class="p">.</span><span class="nx">asReference</span> <span class="nx">o</span>
|
|
<span class="nv">val = </span><span class="k">new</span> <span class="nx">Op</span> <span class="s1">'?'</span><span class="p">,</span> <span class="nx">ref</span><span class="p">,</span> <span class="nx">param</span><span class="p">.</span><span class="nx">value</span> <span class="k">if</span> <span class="nx">param</span><span class="p">.</span><span class="nx">value</span>
|
|
<span class="nx">exprs</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">param</span><span class="p">.</span><span class="nx">name</span><span class="p">),</span> <span class="nx">val</span><span class="p">,</span> <span class="s1">'='</span><span class="p">,</span> <span class="nx">param</span><span class="o">:</span> <span class="kc">yes</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">ref = </span><span class="nx">param</span>
|
|
<span class="k">if</span> <span class="nx">param</span><span class="p">.</span><span class="nx">value</span>
|
|
<span class="nv">lit = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">ref</span><span class="p">.</span><span class="nx">name</span><span class="p">.</span><span class="nx">value</span> <span class="o">+</span> <span class="s1">' == null'</span>
|
|
<span class="nv">val = </span><span class="k">new</span> <span class="nx">Assign</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="nx">param</span><span class="p">.</span><span class="nx">name</span><span class="p">),</span> <span class="nx">param</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span> <span class="s1">'='</span>
|
|
<span class="nx">exprs</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">If</span> <span class="nx">lit</span><span class="p">,</span> <span class="nx">val</span>
|
|
<span class="nx">vars</span><span class="p">.</span><span class="nx">push</span> <span class="nx">ref</span> <span class="nx">unless</span> <span class="nx">splats</span>
|
|
<span class="nv">wasEmpty = </span><span class="nx">@body</span><span class="p">.</span><span class="nx">isEmpty</span><span class="p">()</span>
|
|
<span class="nx">exprs</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">splats</span> <span class="k">if</span> <span class="nx">splats</span>
|
|
<span class="nx">@body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">exprs</span><span class="p">...</span> <span class="k">if</span> <span class="nx">exprs</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">parameter</span> <span class="nx">vars</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">v</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span> <span class="k">for</span> <span class="nx">v</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">vars</span> <span class="nx">unless</span> <span class="nx">splats</span>
|
|
<span class="nx">@body</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span> <span class="nx">unless</span> <span class="nx">wasEmpty</span> <span class="o">or</span> <span class="nx">@noReturn</span>
|
|
<span class="nv">idt = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span>
|
|
<span class="nv">code = </span><span class="s1">'function'</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s1">' '</span> <span class="o">+</span> <span class="nx">@name</span> <span class="k">if</span> <span class="nx">@ctor</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s1">'('</span> <span class="o">+</span> <span class="nx">vars</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">', '</span><span class="p">)</span> <span class="o">+</span> <span class="s1">') {'</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s2">"\n#{ @body.compileWithDeclarations o }\n#{@tab}"</span> <span class="nx">unless</span> <span class="nx">@body</span><span class="p">.</span><span class="nx">isEmpty</span><span class="p">()</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s1">'}'</span>
|
|
<span class="k">return</span> <span class="nx">@tab</span> <span class="o">+</span> <span class="nx">code</span> <span class="k">if</span> <span class="nx">@ctor</span>
|
|
<span class="k">return</span> <span class="nx">utility</span><span class="p">(</span><span class="s1">'bind'</span><span class="p">)</span> <span class="o">+</span> <span class="s2">"(#{code}, #{@context})"</span> <span class="k">if</span> <span class="nx">@bound</span>
|
|
<span class="k">if</span> <span class="nx">@front</span> <span class="o">or</span> <span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">>=</span> <span class="nx">LEVEL_ACCESS</span><span class="p">)</span> <span class="k">then</span> <span class="s2">"(#{code})"</span> <span class="k">else</span> <span class="nx">code</span></pre></div> </td> </tr> <tr id="section-100"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-100">¶</a> </div> <p>Short-circuit <code>traverseChildren</code> method to prevent it from crossing scope boundaries
|
|
unless <code>crossScope</code> is <code>true</code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">traverseChildren</span><span class="o">:</span> <span class="p">(</span><span class="nx">crossScope</span><span class="p">,</span> <span class="nx">func</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">super</span><span class="p">(</span><span class="nx">crossScope</span><span class="p">,</span> <span class="nx">func</span><span class="p">)</span> <span class="k">if</span> <span class="nx">crossScope</span></pre></div> </td> </tr> <tr id="section-101"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-101">¶</a> </div> <h3>Param</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-102"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-102">¶</a> </div> <p>A parameter in a function definition. Beyond a typical Javascript parameter,
|
|
these parameters can also attach themselves to the context of the function,
|
|
as well as be a splat, gathering up a group of parameters into an array.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Param = </span><span class="nx">class</span> <span class="nx">Param</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@name</span><span class="p">,</span> <span class="nx">@value</span><span class="p">,</span> <span class="nx">@splat</span><span class="p">)</span> <span class="o">-></span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'name'</span><span class="p">,</span> <span class="s1">'value'</span><span class="p">]</span>
|
|
|
|
<span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
|
|
<span class="nx">asReference</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">@reference</span> <span class="k">if</span> <span class="nx">@reference</span>
|
|
<span class="nv">node = </span><span class="nx">@name</span>
|
|
<span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="k">this</span>
|
|
<span class="nv">node = </span><span class="nx">node</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span>
|
|
<span class="nv">node = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="s1">'_'</span> <span class="o">+</span> <span class="nx">node</span><span class="p">.</span><span class="nx">value</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">value</span><span class="p">.</span><span class="nx">reserved</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
|
|
<span class="nv">node = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'arg'</span>
|
|
<span class="nv">node = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">node</span>
|
|
<span class="nv">node = </span><span class="k">new</span> <span class="nx">Splat</span> <span class="nx">node</span> <span class="k">if</span> <span class="nx">@splat</span>
|
|
<span class="vi">@reference = </span><span class="nx">node</span>
|
|
|
|
<span class="nx">isComplex</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@name</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-103"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-103">¶</a> </div> <h3>Splat</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-104"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-104">¶</a> </div> <p>A splat, either as a parameter to a function, an argument to a call,
|
|
or as part of a destructuring assignment.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Splat = </span><span class="nx">class</span> <span class="nx">Splat</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'name'</span><span class="p">]</span>
|
|
|
|
<span class="nx">isAssignable</span><span class="o">:</span> <span class="nx">YES</span>
|
|
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@name = </span><span class="k">if</span> <span class="nx">name</span><span class="p">.</span><span class="nx">compile</span> <span class="k">then</span> <span class="nx">name</span> <span class="k">else</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="nx">name</span>
|
|
|
|
<span class="nx">assigns</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@name</span><span class="p">.</span><span class="nx">assigns</span> <span class="nx">name</span>
|
|
|
|
<span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@index</span><span class="o">?</span> <span class="k">then</span> <span class="nx">@compileParam</span> <span class="nx">o</span> <span class="k">else</span> <span class="nx">@name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-105"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-105">¶</a> </div> <p>Utility function that converts an arbitrary number of elements, mixed with
|
|
splats, to a proper array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">@compileSplattedArray</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">list</span><span class="p">,</span> <span class="nx">apply</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">index = </span><span class="o">-</span><span class="mi">1</span>
|
|
<span class="k">continue</span> <span class="k">while</span> <span class="p">(</span><span class="nv">node = </span><span class="nx">list</span><span class="p">[</span><span class="o">++</span><span class="nx">index</span><span class="p">])</span> <span class="o">and</span> <span class="nx">node</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">Splat</span>
|
|
<span class="k">return</span> <span class="s1">''</span> <span class="k">if</span> <span class="nx">index</span> <span class="o">>=</span> <span class="nx">list</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="k">if</span> <span class="nx">list</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">1</span>
|
|
<span class="nv">code = </span><span class="nx">list</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="k">return</span> <span class="nx">code</span> <span class="k">if</span> <span class="nx">apply</span>
|
|
<span class="k">return</span> <span class="s2">"#{ utility 'slice' }.call(#{code})"</span>
|
|
<span class="nv">args = </span><span class="nx">list</span><span class="p">[</span><span class="nx">index</span><span class="p">..]</span>
|
|
<span class="k">for</span> <span class="nx">node</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">args</span>
|
|
<span class="nv">code = </span><span class="nx">node</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="nx">args</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Splat</span>
|
|
<span class="k">then</span> <span class="s2">"#{ utility 'slice' }.call(#{code})"</span>
|
|
<span class="k">else</span> <span class="s2">"[#{code}]"</span>
|
|
<span class="k">return</span> <span class="nx">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="s2">".concat(#{ args[1..].join ', ' })"</span> <span class="k">if</span> <span class="nx">index</span> <span class="o">is</span> <span class="mi">0</span>
|
|
<span class="nv">base = </span><span class="p">(</span><span class="nx">node</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span> <span class="k">for</span> <span class="nx">node</span> <span class="k">in</span> <span class="nx">list</span><span class="p">[</span><span class="mi">0</span><span class="p">...</span><span class="nx">index</span><span class="p">])</span>
|
|
<span class="s2">"[#{ base.join ', ' }].concat(#{ args.join ', ' })"</span></pre></div> </td> </tr> <tr id="section-106"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-106">¶</a> </div> <h3>While</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-107"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-107">¶</a> </div> <p>A while loop, the only sort of low-level loop exposed by CoffeeScript. From
|
|
it, all other loops can be manufactured. Useful in cases where you need more
|
|
flexibility or more speed than a comprehension can provide.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.While = </span><span class="nx">class</span> <span class="nx">While</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">condition</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="vi">@condition = </span><span class="k">if</span> <span class="nx">options</span><span class="o">?</span><span class="p">.</span><span class="nx">invert</span> <span class="k">then</span> <span class="nx">condition</span><span class="p">.</span><span class="nx">invert</span><span class="p">()</span> <span class="k">else</span> <span class="nx">condition</span>
|
|
<span class="vi">@guard = </span><span class="nx">options</span><span class="o">?</span><span class="p">.</span><span class="nx">guard</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'condition'</span><span class="p">,</span> <span class="s1">'guard'</span><span class="p">,</span> <span class="s1">'body'</span><span class="p">]</span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="vi">@returns = </span><span class="kc">yes</span>
|
|
<span class="k">this</span>
|
|
|
|
<span class="nx">addBody</span><span class="o">:</span> <span class="p">(</span><span class="nx">@body</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">this</span>
|
|
|
|
<span class="nx">jumps</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="p">{</span><span class="nx">expressions</span><span class="p">}</span> <span class="o">=</span> <span class="nx">@body</span>
|
|
<span class="k">return</span> <span class="kc">no</span> <span class="nx">unless</span> <span class="nx">expressions</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="k">for</span> <span class="nx">node</span> <span class="k">in</span> <span class="nx">expressions</span>
|
|
<span class="k">return</span> <span class="nx">node</span> <span class="k">if</span> <span class="nx">node</span><span class="p">.</span><span class="nx">jumps</span> <span class="nx">loop</span><span class="o">:</span> <span class="kc">yes</span>
|
|
<span class="kc">no</span></pre></div> </td> </tr> <tr id="section-108"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-108">¶</a> </div> <p>The main difference from a JavaScript <em>while</em> is that the CoffeeScript
|
|
<em>while</em> can be used as a part of a larger expression -- while loops may
|
|
return an array containing the computed result of each iteration.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
|
|
<span class="nv">set = </span><span class="s1">''</span>
|
|
<span class="p">{</span><span class="nx">body</span><span class="p">}</span> <span class="o">=</span> <span class="k">this</span>
|
|
<span class="k">if</span> <span class="nx">body</span><span class="p">.</span><span class="nx">isEmpty</span><span class="p">()</span>
|
|
<span class="nv">body = </span><span class="s1">''</span>
|
|
<span class="k">else</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">></span> <span class="nx">LEVEL_TOP</span> <span class="o">or</span> <span class="nx">@returns</span>
|
|
<span class="nv">rvar = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'results'</span>
|
|
<span class="nv">set = </span><span class="s2">"#{@tab}#{rvar} = [];\n"</span>
|
|
<span class="nv">body = </span><span class="nx">Push</span><span class="p">.</span><span class="nx">wrap</span> <span class="nx">rvar</span><span class="p">,</span> <span class="nx">body</span> <span class="k">if</span> <span class="nx">body</span>
|
|
<span class="nv">body = </span><span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="k">new</span> <span class="nx">If</span> <span class="nx">@guard</span><span class="p">,</span> <span class="nx">body</span><span class="p">]</span> <span class="k">if</span> <span class="nx">@guard</span>
|
|
<span class="nv">body = </span><span class="s2">"\n#{ body.compile o, LEVEL_TOP }\n#{@tab}"</span>
|
|
<span class="nv">code = </span><span class="nx">set</span> <span class="o">+</span> <span class="nx">@tab</span> <span class="o">+</span> <span class="s2">"while (#{ @condition.compile o, LEVEL_PAREN }) {#{body}}"</span>
|
|
<span class="k">if</span> <span class="nx">@returns</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s2">"\n#{@tab}return #{rvar};"</span>
|
|
<span class="nx">code</span></pre></div> </td> </tr> <tr id="section-109"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-109">¶</a> </div> <h3>Op</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-110"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-110">¶</a> </div> <p>Simple Arithmetic and logical operations. Performs some conversion from
|
|
CoffeeScript operations into their JavaScript equivalents.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Op = </span><span class="nx">class</span> <span class="nx">Op</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">op</span><span class="p">,</span> <span class="nx">first</span><span class="p">,</span> <span class="nx">second</span><span class="p">,</span> <span class="nx">flip</span> <span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="k">new</span> <span class="nx">In</span> <span class="nx">first</span><span class="p">,</span> <span class="nx">second</span> <span class="k">if</span> <span class="nx">op</span> <span class="o">is</span> <span class="s1">'in'</span>
|
|
<span class="k">if</span> <span class="nx">op</span> <span class="o">is</span> <span class="s1">'do'</span>
|
|
<span class="nv">call = </span><span class="k">new</span> <span class="nx">Call</span> <span class="nx">first</span><span class="p">,</span> <span class="nx">first</span><span class="p">.</span><span class="nx">params</span> <span class="o">or</span> <span class="p">[]</span>
|
|
<span class="nv">call.do = </span><span class="kc">yes</span>
|
|
<span class="k">return</span> <span class="nx">call</span>
|
|
<span class="k">if</span> <span class="nx">op</span> <span class="o">is</span> <span class="s1">'new'</span>
|
|
<span class="k">return</span> <span class="nx">first</span><span class="p">.</span><span class="nx">newInstance</span><span class="p">()</span> <span class="k">if</span> <span class="nx">first</span> <span class="k">instanceof</span> <span class="nx">Call</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">first</span><span class="p">.</span><span class="nx">do</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">first</span><span class="p">.</span><span class="nx">isNew</span>
|
|
<span class="nv">first = </span><span class="k">new</span> <span class="nx">Parens</span> <span class="nx">first</span> <span class="k">if</span> <span class="nx">first</span> <span class="k">instanceof</span> <span class="nx">Code</span> <span class="o">and</span> <span class="nx">first</span><span class="p">.</span><span class="nx">bound</span> <span class="o">or</span> <span class="nx">first</span><span class="p">.</span><span class="nx">do</span>
|
|
<span class="vi">@operator = </span><span class="nx">CONVERSIONS</span><span class="p">[</span><span class="nx">op</span><span class="p">]</span> <span class="o">or</span> <span class="nx">op</span>
|
|
<span class="vi">@first = </span><span class="nx">first</span>
|
|
<span class="vi">@second = </span><span class="nx">second</span>
|
|
<span class="vi">@flip = </span><span class="o">!!</span><span class="nx">flip</span>
|
|
<span class="k">return</span> <span class="k">this</span></pre></div> </td> </tr> <tr id="section-111"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-111">¶</a> </div> <p>The map of conversions from CoffeeScript to JavaScript symbols.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">CONVERSIONS =</span>
|
|
<span class="s1">'=='</span><span class="o">:</span> <span class="s1">'==='</span>
|
|
<span class="s1">'!='</span><span class="o">:</span> <span class="s1">'!=='</span>
|
|
<span class="s1">'of'</span><span class="o">:</span> <span class="s1">'in'</span></pre></div> </td> </tr> <tr id="section-112"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-112">¶</a> </div> <p>The map of invertible operators.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">INVERSIONS =</span>
|
|
<span class="s1">'!=='</span><span class="o">:</span> <span class="s1">'==='</span>
|
|
<span class="s1">'==='</span><span class="o">:</span> <span class="s1">'!=='</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'first'</span><span class="p">,</span> <span class="s1">'second'</span><span class="p">]</span>
|
|
|
|
<span class="nx">isSimpleNumber</span><span class="o">:</span> <span class="nx">NO</span>
|
|
|
|
<span class="nx">isUnary</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="o">not</span> <span class="nx">@second</span>
|
|
|
|
<span class="nx">isComplex</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="o">not</span> <span class="p">(</span><span class="nx">@isUnary</span><span class="p">()</span> <span class="o">and</span> <span class="p">(</span><span class="nx">@operator</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'+'</span><span class="p">,</span> <span class="s1">'-'</span><span class="p">]))</span> <span class="o">or</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-113"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-113">¶</a> </div> <p>Am I capable of
|
|
<a href="http://docs.python.org/reference/expressions.html#notin">Python-style comparison chaining</a>?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">isChainable</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@operator</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'<'</span><span class="p">,</span> <span class="s1">'>'</span><span class="p">,</span> <span class="s1">'>='</span><span class="p">,</span> <span class="s1">'<='</span><span class="p">,</span> <span class="s1">'==='</span><span class="p">,</span> <span class="s1">'!=='</span><span class="p">]</span>
|
|
|
|
<span class="nx">invert</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@isChainable</span><span class="p">()</span> <span class="o">and</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">isChainable</span><span class="p">()</span>
|
|
<span class="nv">allInvertable = </span><span class="kc">yes</span>
|
|
<span class="nv">curr = </span><span class="k">this</span>
|
|
<span class="k">while</span> <span class="nx">curr</span> <span class="o">and</span> <span class="nx">curr</span><span class="p">.</span><span class="nx">operator</span>
|
|
<span class="nx">allInvertable</span> <span class="o">and=</span> <span class="p">(</span><span class="nx">curr</span><span class="p">.</span><span class="nx">operator</span> <span class="k">of</span> <span class="nx">INVERSIONS</span><span class="p">)</span>
|
|
<span class="nv">curr = </span><span class="nx">curr</span><span class="p">.</span><span class="nx">first</span>
|
|
<span class="k">return</span> <span class="k">new</span> <span class="nx">Parens</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">invert</span><span class="p">()</span> <span class="nx">unless</span> <span class="nx">allInvertable</span>
|
|
<span class="nv">curr = </span><span class="k">this</span>
|
|
<span class="k">while</span> <span class="nx">curr</span> <span class="o">and</span> <span class="nx">curr</span><span class="p">.</span><span class="nx">operator</span>
|
|
<span class="nv">curr.invert = </span><span class="o">!</span><span class="nx">curr</span><span class="p">.</span><span class="nx">invert</span>
|
|
<span class="nv">curr.operator = </span><span class="nx">INVERSIONS</span><span class="p">[</span><span class="nx">curr</span><span class="p">.</span><span class="nx">operator</span><span class="p">]</span>
|
|
<span class="nv">curr = </span><span class="nx">curr</span><span class="p">.</span><span class="nx">first</span>
|
|
<span class="k">this</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nv">op = </span><span class="nx">INVERSIONS</span><span class="p">[</span><span class="nx">@operator</span><span class="p">]</span>
|
|
<span class="vi">@operator = </span><span class="nx">op</span>
|
|
<span class="k">if</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span> <span class="k">instanceof</span> <span class="nx">Op</span>
|
|
<span class="nx">@first</span><span class="p">.</span><span class="nx">invert</span><span class="p">()</span>
|
|
<span class="k">this</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">@second</span>
|
|
<span class="k">new</span> <span class="nx">Parens</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">invert</span><span class="p">()</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">@operator</span> <span class="o">is</span> <span class="s1">'!'</span> <span class="o">and</span> <span class="p">(</span><span class="nv">fst = </span><span class="nx">@first</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">())</span> <span class="k">instanceof</span> <span class="nx">Op</span> <span class="o">and</span>
|
|
<span class="nx">fst</span><span class="p">.</span><span class="nx">operator</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'!'</span><span class="p">,</span> <span class="s1">'in'</span><span class="p">,</span> <span class="s1">'instanceof'</span><span class="p">]</span>
|
|
<span class="nx">fst</span>
|
|
<span class="k">else</span>
|
|
<span class="k">new</span> <span class="nx">Op</span> <span class="s1">'!'</span><span class="p">,</span> <span class="k">this</span>
|
|
|
|
<span class="nx">unfoldSoak</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@operator</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'++'</span><span class="p">,</span> <span class="s1">'--'</span><span class="p">,</span> <span class="s1">'delete'</span><span class="p">]</span> <span class="o">and</span> <span class="nx">unfoldSoak</span> <span class="nx">o</span><span class="p">,</span> <span class="k">this</span><span class="p">,</span> <span class="s1">'first'</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">@compileUnary</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@isUnary</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="nx">@compileChain</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@isChainable</span><span class="p">()</span> <span class="o">and</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">isChainable</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="nx">@compileExistence</span> <span class="nx">o</span> <span class="k">if</span> <span class="nx">@operator</span> <span class="o">is</span> <span class="s1">'?'</span>
|
|
<span class="vi">@first.front = </span><span class="nx">@front</span>
|
|
<span class="nv">code = </span><span class="nx">@first</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span><span class="p">)</span> <span class="o">+</span> <span class="s1">' '</span> <span class="o">+</span> <span class="nx">@operator</span> <span class="o">+</span> <span class="s1">' '</span> <span class="o">+</span>
|
|
<span class="nx">@second</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o"><=</span> <span class="nx">LEVEL_OP</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">"(#{code})"</span></pre></div> </td> </tr> <tr id="section-114"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-114">¶</a> </div> <p>Mimic Python's chained comparisons when multiple comparison operators are
|
|
used sequentially. For example:</p>
|
|
|
|
<pre><code>bin/coffee -e 'console.log 50 < 65 > 10'
|
|
true
|
|
</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileChain</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">[</span><span class="nx">@first</span><span class="p">.</span><span class="nx">second</span><span class="p">,</span> <span class="nx">shared</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">second</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span>
|
|
<span class="nv">fst = </span><span class="nx">@first</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span>
|
|
<span class="nv">code = </span><span class="s2">"#{fst} #{if @invert then '&&' else '||'} #{ shared.compile o } #{@operator} #{ @second.compile o, LEVEL_OP }"</span>
|
|
<span class="s2">"(#{code})"</span>
|
|
|
|
<span class="nx">compileExistence</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
|
|
<span class="nv">ref = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'ref'</span>
|
|
<span class="nv">fst = </span><span class="k">new</span> <span class="nx">Parens</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">ref</span><span class="p">,</span> <span class="nx">@first</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">fst = </span><span class="nx">@first</span>
|
|
<span class="nv">ref = </span><span class="nx">fst</span>
|
|
<span class="k">new</span> <span class="nx">If</span><span class="p">(</span><span class="k">new</span> <span class="nx">Existence</span><span class="p">(</span><span class="nx">fst</span><span class="p">),</span> <span class="nx">ref</span><span class="p">,</span> <span class="nx">type</span><span class="o">:</span> <span class="s1">'if'</span><span class="p">).</span><span class="nx">addElse</span><span class="p">(</span><span class="nx">@second</span><span class="p">).</span><span class="nx">compile</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-115"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-115">¶</a> </div> <p>Compile a unary <strong>Op</strong>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileUnary</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">parts = </span><span class="p">[</span><span class="nv">op = </span><span class="nx">@operator</span><span class="p">]</span>
|
|
<span class="nx">parts</span><span class="p">.</span><span class="nx">push</span> <span class="s1">' '</span> <span class="k">if</span> <span class="nx">op</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'new'</span><span class="p">,</span> <span class="s1">'typeof'</span><span class="p">,</span> <span class="s1">'delete'</span><span class="p">]</span> <span class="o">or</span>
|
|
<span class="nx">op</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'+'</span><span class="p">,</span> <span class="s1">'-'</span><span class="p">]</span> <span class="o">and</span> <span class="nx">@first</span> <span class="k">instanceof</span> <span class="nx">Op</span> <span class="o">and</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">operator</span> <span class="o">is</span> <span class="nx">op</span>
|
|
<span class="vi">@first = </span><span class="k">new</span> <span class="nx">Parens</span> <span class="nx">@first</span> <span class="k">if</span> <span class="nx">op</span> <span class="o">is</span> <span class="s1">'new'</span> <span class="o">and</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">isStatement</span> <span class="nx">o</span>
|
|
<span class="nx">parts</span><span class="p">.</span><span class="nx">push</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span>
|
|
<span class="nx">parts</span><span class="p">.</span><span class="nx">reverse</span><span class="p">()</span> <span class="k">if</span> <span class="nx">@flip</span>
|
|
<span class="nx">parts</span><span class="p">.</span><span class="nx">join</span> <span class="s1">''</span>
|
|
|
|
<span class="nx">toString</span><span class="o">:</span> <span class="p">(</span><span class="nx">idt</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">super</span> <span class="nx">idt</span><span class="p">,</span> <span class="nx">@constructor</span><span class="p">.</span><span class="nx">name</span> <span class="o">+</span> <span class="s1">' '</span> <span class="o">+</span> <span class="nx">@operator</span></pre></div> </td> </tr> <tr id="section-116"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-116">¶</a> </div> <h3>In</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.In = </span><span class="nx">class</span> <span class="nx">In</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@object</span><span class="p">,</span> <span class="nx">@array</span><span class="p">)</span> <span class="o">-></span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'object'</span><span class="p">,</span> <span class="s1">'array'</span><span class="p">]</span>
|
|
|
|
<span class="nx">invert</span><span class="o">:</span> <span class="nx">NEGATE</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@array</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span> <span class="nx">@array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">()</span>
|
|
<span class="k">for</span> <span class="nx">obj</span> <span class="k">in</span> <span class="nx">@array</span><span class="p">.</span><span class="nx">base</span><span class="p">.</span><span class="nx">objects</span> <span class="k">when</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">Splat</span>
|
|
<span class="nv">hasSplat = </span><span class="kc">yes</span>
|
|
<span class="k">break</span></pre></div> </td> </tr> <tr id="section-117"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-117">¶</a> </div> <p><code>compileOrTest</code> only if we have an array literal with no splats</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">@compileOrTest</span> <span class="nx">o</span> <span class="nx">unless</span> <span class="nx">hasSplat</span>
|
|
<span class="nx">@compileLoopTest</span> <span class="nx">o</span>
|
|
|
|
<span class="nx">compileOrTest</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">[</span><span class="nx">sub</span><span class="p">,</span> <span class="nx">ref</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@object</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span>
|
|
<span class="p">[</span><span class="nx">cmp</span><span class="p">,</span> <span class="nx">cnj</span><span class="p">]</span> <span class="o">=</span> <span class="k">if</span> <span class="nx">@negated</span> <span class="k">then</span> <span class="p">[</span><span class="s1">' !== '</span><span class="p">,</span> <span class="s1">' && '</span><span class="p">]</span> <span class="k">else</span> <span class="p">[</span><span class="s1">' === '</span><span class="p">,</span> <span class="s1">' || '</span><span class="p">]</span>
|
|
<span class="nv">tests = </span><span class="k">for</span> <span class="nx">item</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">@array</span><span class="p">.</span><span class="nx">base</span><span class="p">.</span><span class="nx">objects</span>
|
|
<span class="p">(</span><span class="k">if</span> <span class="nx">i</span> <span class="k">then</span> <span class="nx">ref</span> <span class="k">else</span> <span class="nx">sub</span><span class="p">)</span> <span class="o">+</span> <span class="nx">cmp</span> <span class="o">+</span> <span class="nx">item</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span>
|
|
<span class="k">return</span> <span class="s1">'false'</span> <span class="k">if</span> <span class="nx">tests</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">0</span>
|
|
<span class="nv">tests = </span><span class="nx">tests</span><span class="p">.</span><span class="nx">join</span> <span class="nx">cnj</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o"><</span> <span class="nx">LEVEL_OP</span> <span class="k">then</span> <span class="nx">tests</span> <span class="k">else</span> <span class="s2">"(#{tests})"</span>
|
|
|
|
<span class="nx">compileLoopTest</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">[</span><span class="nx">sub</span><span class="p">,</span> <span class="nx">ref</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@object</span><span class="p">.</span><span class="nx">cache</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="nv">code = </span><span class="nx">utility</span><span class="p">(</span><span class="s1">'indexOf'</span><span class="p">)</span> <span class="o">+</span> <span class="s2">".call(#{ @array.compile o, LEVEL_LIST }, #{ref}) "</span> <span class="o">+</span>
|
|
<span class="k">if</span> <span class="nx">@negated</span> <span class="k">then</span> <span class="s1">'< 0'</span> <span class="k">else</span> <span class="s1">'>= 0'</span>
|
|
<span class="k">return</span> <span class="nx">code</span> <span class="k">if</span> <span class="nx">sub</span> <span class="o">is</span> <span class="nx">ref</span>
|
|
<span class="nv">code = </span><span class="nx">sub</span> <span class="o">+</span> <span class="s1">', '</span> <span class="o">+</span> <span class="nx">code</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o"><</span> <span class="nx">LEVEL_LIST</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">"(#{code})"</span>
|
|
|
|
<span class="nx">toString</span><span class="o">:</span> <span class="p">(</span><span class="nx">idt</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">super</span> <span class="nx">idt</span><span class="p">,</span> <span class="nx">@constructor</span><span class="p">.</span><span class="nx">name</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@negated</span> <span class="k">then</span> <span class="s1">'!'</span> <span class="k">else</span> <span class="s1">''</span></pre></div> </td> </tr> <tr id="section-118"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-118">¶</a> </div> <h3>Try</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-119"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-119">¶</a> </div> <p>A classic <em>try/catch/finally</em> block.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Try = </span><span class="nx">class</span> <span class="nx">Try</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@attempt</span><span class="p">,</span> <span class="nx">@error</span><span class="p">,</span> <span class="nx">@recovery</span><span class="p">,</span> <span class="nx">@ensure</span><span class="p">)</span> <span class="o">-></span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'attempt'</span><span class="p">,</span> <span class="s1">'recovery'</span><span class="p">,</span> <span class="s1">'ensure'</span><span class="p">]</span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
|
|
|
|
<span class="nx">jumps</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span> <span class="nx">@attempt</span><span class="p">.</span><span class="nx">jumps</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">or</span> <span class="nx">@recovery</span><span class="o">?</span><span class="p">.</span><span class="nx">jumps</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="vi">@attempt = </span><span class="nx">@attempt</span> <span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span> <span class="k">if</span> <span class="nx">@attempt</span>
|
|
<span class="vi">@recovery = </span><span class="nx">@recovery</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span> <span class="k">if</span> <span class="nx">@recovery</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-120"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-120">¶</a> </div> <p>Compilation is more or less as you would expect -- the <em>finally</em> clause
|
|
is optional, the <em>catch</em> is not.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
|
|
<span class="nv">errorPart = </span><span class="k">if</span> <span class="nx">@error</span> <span class="k">then</span> <span class="s2">" (#{ @error.compile o }) "</span> <span class="k">else</span> <span class="s1">' '</span>
|
|
<span class="nv">catchPart = </span><span class="k">if</span> <span class="nx">@recovery</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">add</span> <span class="nx">@error</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span> <span class="s1">'param'</span>
|
|
<span class="s2">" catch#{errorPart}{\n#{ @recovery.compile o, LEVEL_TOP }\n#{@tab}}"</span>
|
|
<span class="k">else</span> <span class="nx">unless</span> <span class="nx">@ensure</span> <span class="o">or</span> <span class="nx">@recovery</span>
|
|
<span class="s1">' catch (_e) {}'</span>
|
|
<span class="s2">"""</span>
|
|
<span class="s2"> #{@tab}try {</span>
|
|
<span class="s2"> #{ @attempt.compile o, LEVEL_TOP }</span>
|
|
<span class="s2"> #{@tab}}#{ catchPart or '' }</span>
|
|
<span class="s2"> """</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@ensure</span> <span class="k">then</span> <span class="s2">" finally {\n#{ @ensure.compile o, LEVEL_TOP }\n#{@tab}}"</span> <span class="k">else</span> <span class="s1">''</span></pre></div> </td> </tr> <tr id="section-121"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-121">¶</a> </div> <h3>Throw</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-122"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-122">¶</a> </div> <p>Simple node to throw an exception.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Throw = </span><span class="nx">class</span> <span class="nx">Throw</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@expression</span><span class="p">)</span> <span class="o">-></span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'expression'</span><span class="p">]</span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
|
|
<span class="nx">jumps</span><span class="o">:</span> <span class="nx">NO</span></pre></div> </td> </tr> <tr id="section-123"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-123">¶</a> </div> <p>A <strong>Throw</strong> is already a return, of sorts...</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">makeReturn</span><span class="o">:</span> <span class="nx">THIS</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@tab</span> <span class="o">+</span> <span class="s2">"throw #{ @expression.compile o };"</span></pre></div> </td> </tr> <tr id="section-124"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-124">¶</a> </div> <h3>Existence</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-125"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-125">¶</a> </div> <p>Checks a variable for existence -- not <em>null</em> and not <em>undefined</em>. This is
|
|
similar to <code>.nil?</code> in Ruby, and avoids having to consult a JavaScript truth
|
|
table.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Existence = </span><span class="nx">class</span> <span class="nx">Existence</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@expression</span><span class="p">)</span> <span class="o">-></span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'expression'</span><span class="p">]</span>
|
|
|
|
<span class="nx">invert</span><span class="o">:</span> <span class="nx">NEGATE</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">code = </span><span class="nx">@expression</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_OP</span>
|
|
<span class="nv">code = </span><span class="k">if</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">code</span><span class="p">)</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">check</span> <span class="nx">code</span>
|
|
<span class="p">[</span><span class="nx">cmp</span><span class="p">,</span> <span class="nx">cnj</span><span class="p">]</span> <span class="o">=</span> <span class="k">if</span> <span class="nx">@negated</span> <span class="k">then</span> <span class="p">[</span><span class="s1">'==='</span><span class="p">,</span> <span class="s1">'||'</span><span class="p">]</span> <span class="k">else</span> <span class="p">[</span><span class="s1">'!=='</span><span class="p">,</span> <span class="s1">'&&'</span><span class="p">]</span>
|
|
<span class="s2">"typeof #{code} #{cmp} \"undefined\" #{cnj} #{code} #{cmp} null"</span>
|
|
<span class="k">else</span></pre></div> </td> </tr> <tr id="section-126"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-126">¶</a> </div> <p>do not use strict equality here; it will break existing code</p> </td> <td class="code"> <div class="highlight"><pre> <span class="s2">"#{code} #{if @negated then '==' else '!='} null"</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o"><=</span> <span class="nx">LEVEL_COND</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">"(#{code})"</span></pre></div> </td> </tr> <tr id="section-127"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-127">¶</a> </div> <h3>Parens</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-128"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-128">¶</a> </div> <p>An extra set of parentheses, specified explicitly in the source. At one time
|
|
we tried to clean up the results by detecting and removing redundant
|
|
parentheses, but no longer -- you can put in as many as you please.</p>
|
|
|
|
<p>Parentheses are a good way to force any statement to become an expression.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Parens = </span><span class="nx">class</span> <span class="nx">Parens</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@body</span><span class="p">)</span> <span class="o">-></span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'body'</span><span class="p">]</span>
|
|
|
|
<span class="nx">unwrap</span> <span class="o">:</span> <span class="o">-></span> <span class="nx">@body</span>
|
|
<span class="nx">isComplex</span> <span class="o">:</span> <span class="o">-></span> <span class="nx">@body</span><span class="p">.</span><span class="nx">isComplex</span><span class="p">()</span>
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span> <span class="nx">@body</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">expr = </span><span class="nx">@body</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">isAtomic</span><span class="p">()</span>
|
|
<span class="nv">expr.front = </span><span class="nx">@front</span>
|
|
<span class="k">return</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nv">code = </span><span class="nx">expr</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_PAREN</span>
|
|
<span class="nv">bare = </span><span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o"><</span> <span class="nx">LEVEL_OP</span> <span class="o">and</span> <span class="p">(</span><span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">Op</span> <span class="o">or</span> <span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">Call</span> <span class="o">or</span>
|
|
<span class="p">(</span><span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">For</span> <span class="o">and</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">returns</span><span class="p">))</span>
|
|
<span class="k">if</span> <span class="nx">bare</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">"(#{code})"</span></pre></div> </td> </tr> <tr id="section-129"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-129">¶</a> </div> <h3>For</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-130"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-130">¶</a> </div> <p>CoffeeScript's replacement for the <em>for</em> loop is our array and object
|
|
comprehensions, that compile into <em>for</em> loops here. They also act as an
|
|
expression, able to return the result of each filtered iteration.</p>
|
|
|
|
<p>Unlike Python array comprehensions, they can be multi-line, and you can pass
|
|
the current index of the loop as a second parameter. Unlike Ruby blocks,
|
|
you can map and filter in a single pass.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.For = </span><span class="nx">class</span> <span class="nx">For</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">body</span><span class="p">,</span> <span class="nx">source</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">{</span><span class="nx">@source</span><span class="p">,</span> <span class="nx">@guard</span><span class="p">,</span> <span class="nx">@step</span><span class="p">,</span> <span class="nx">@name</span><span class="p">,</span> <span class="nx">@index</span><span class="p">}</span> <span class="o">=</span> <span class="nx">source</span>
|
|
<span class="vi">@body = </span><span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">body</span><span class="p">]</span>
|
|
<span class="vi">@own = </span><span class="o">!!</span><span class="nx">source</span><span class="p">.</span><span class="nx">own</span>
|
|
<span class="vi">@object = </span><span class="o">!!</span><span class="nx">source</span><span class="p">.</span><span class="nx">object</span>
|
|
<span class="p">[</span><span class="nx">@name</span><span class="p">,</span> <span class="nx">@index</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nx">@index</span><span class="p">,</span> <span class="nx">@name</span><span class="p">]</span> <span class="k">if</span> <span class="nx">@object</span>
|
|
<span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">'index cannot be a pattern matching expression'</span> <span class="k">if</span> <span class="nx">@index</span> <span class="k">instanceof</span> <span class="nx">Value</span>
|
|
<span class="vi">@range = </span><span class="nx">@source</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span> <span class="nx">@source</span><span class="p">.</span><span class="nx">base</span> <span class="k">instanceof</span> <span class="nx">Range</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@source</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="vi">@pattern = </span><span class="nx">@name</span> <span class="k">instanceof</span> <span class="nx">Value</span>
|
|
<span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">'indexes do not apply to range loops'</span> <span class="k">if</span> <span class="nx">@range</span> <span class="o">and</span> <span class="nx">@index</span>
|
|
<span class="k">throw</span> <span class="nx">SyntaxError</span> <span class="s1">'cannot pattern match over range loops'</span> <span class="k">if</span> <span class="nx">@range</span> <span class="o">and</span> <span class="nx">@pattern</span>
|
|
<span class="vi">@returns = </span><span class="kc">false</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'body'</span><span class="p">,</span> <span class="s1">'source'</span><span class="p">,</span> <span class="s1">'guard'</span><span class="p">,</span> <span class="s1">'step'</span><span class="p">]</span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
|
|
|
|
<span class="nx">jumps</span><span class="o">:</span> <span class="nx">While</span><span class="o">::</span><span class="nx">jumps</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="vi">@returns = </span><span class="kc">yes</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-131"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-131">¶</a> </div> <p>Welcome to the hairiest method in all of CoffeeScript. Handles the inner
|
|
loop, filtering, stepping, and result saving for array, object, and range
|
|
comprehensions. Some of the generated code can be shared in common, and
|
|
some cannot.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">body = </span><span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">@body</span><span class="p">]</span>
|
|
<span class="nv">lastJumps = </span><span class="nx">last</span><span class="p">(</span><span class="nx">body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">)</span><span class="o">?</span><span class="p">.</span><span class="nx">jumps</span><span class="p">()</span>
|
|
<span class="vi">@returns = </span><span class="kc">no</span> <span class="k">if</span> <span class="nx">lastJumps</span> <span class="o">and</span> <span class="nx">lastJumps</span> <span class="k">instanceof</span> <span class="nx">Return</span>
|
|
<span class="nv">source = </span><span class="k">if</span> <span class="nx">@range</span> <span class="k">then</span> <span class="nx">@source</span><span class="p">.</span><span class="nx">base</span> <span class="k">else</span> <span class="nx">@source</span>
|
|
<span class="nv">scope = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span>
|
|
<span class="nv">name = </span><span class="nx">@name</span> <span class="o">and</span> <span class="nx">@name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="nv">index = </span><span class="nx">@index</span> <span class="o">and</span> <span class="nx">@index</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="nx">scope</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">immediate</span><span class="o">:</span> <span class="kc">yes</span><span class="p">)</span> <span class="k">if</span> <span class="nx">name</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@pattern</span>
|
|
<span class="nx">scope</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="nx">immediate</span><span class="o">:</span> <span class="kc">yes</span><span class="p">)</span> <span class="k">if</span> <span class="nx">index</span>
|
|
<span class="nv">rvar = </span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'results'</span> <span class="k">if</span> <span class="nx">@returns</span>
|
|
<span class="nv">ivar = </span><span class="p">(</span><span class="k">if</span> <span class="nx">@range</span> <span class="k">then</span> <span class="nx">name</span> <span class="k">else</span> <span class="nx">index</span><span class="p">)</span> <span class="o">or</span> <span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'i'</span></pre></div> </td> </tr> <tr id="section-132"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-132">¶</a> </div> <p>the <code>_by</code> variable is created twice in <code>Range</code>s if we don't prevent it from being declared here</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">stepvar = </span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s2">"step"</span> <span class="k">if</span> <span class="nx">@step</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@range</span>
|
|
<span class="nv">name = </span><span class="nx">ivar</span> <span class="k">if</span> <span class="nx">@pattern</span>
|
|
<span class="nv">varPart = </span><span class="s1">''</span>
|
|
<span class="nv">guardPart = </span><span class="s1">''</span>
|
|
<span class="nv">defPart = </span><span class="s1">''</span>
|
|
<span class="nv">idt1 = </span><span class="nx">@tab</span> <span class="o">+</span> <span class="nx">TAB</span>
|
|
<span class="k">if</span> <span class="nx">@range</span>
|
|
<span class="nv">forPart = </span><span class="nx">source</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">merge</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="p">{</span><span class="nx">index</span><span class="o">:</span> <span class="nx">ivar</span><span class="p">,</span> <span class="nx">@step</span><span class="p">})</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">svar = </span><span class="nx">@source</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="k">if</span> <span class="p">(</span><span class="nx">name</span> <span class="o">or</span> <span class="nx">@own</span><span class="p">)</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">IDENTIFIER</span><span class="p">.</span><span class="nx">test</span> <span class="nx">svar</span>
|
|
<span class="nv">defPart = </span><span class="s2">"#{@tab}#{ref = scope.freeVariable 'ref'} = #{svar};\n"</span>
|
|
<span class="nv">svar = </span><span class="nx">ref</span>
|
|
<span class="k">if</span> <span class="nx">name</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@pattern</span>
|
|
<span class="nv">namePart = </span><span class="s2">"#{name} = #{svar}[#{ivar}]"</span>
|
|
<span class="nx">unless</span> <span class="nx">@object</span>
|
|
<span class="nv">lvar = </span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'len'</span>
|
|
<span class="nv">forVarPart = </span><span class="s2">"#{ivar} = 0, #{lvar} = #{svar}.length"</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@step</span> <span class="k">then</span> <span class="s2">", #{stepvar} = #{@step.compile(o, LEVEL_OP)}"</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="nv">stepPart = </span><span class="k">if</span> <span class="nx">@step</span> <span class="k">then</span> <span class="s2">"#{ivar} += #{stepvar}"</span> <span class="k">else</span> <span class="s2">"#{ivar}++"</span>
|
|
<span class="nv">forPart = </span><span class="s2">"#{forVarPart}; #{ivar} < #{lvar}; #{stepPart}"</span>
|
|
<span class="k">if</span> <span class="nx">@returns</span>
|
|
<span class="nv">resultPart = </span><span class="s2">"#{@tab}#{rvar} = [];\n"</span>
|
|
<span class="nv">returnResult = </span><span class="s2">"\n#{@tab}return #{rvar};"</span>
|
|
<span class="nv">body = </span><span class="nx">Push</span><span class="p">.</span><span class="nx">wrap</span> <span class="nx">rvar</span><span class="p">,</span> <span class="nx">body</span>
|
|
<span class="k">if</span> <span class="nx">@guard</span>
|
|
<span class="nv">body = </span><span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="k">new</span> <span class="nx">If</span> <span class="nx">@guard</span><span class="p">,</span> <span class="nx">body</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="nx">@pattern</span>
|
|
<span class="nx">body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">unshift</span> <span class="k">new</span> <span class="nx">Assign</span> <span class="nx">@name</span><span class="p">,</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s2">"#{svar}[#{ivar}]"</span>
|
|
<span class="nx">defPart</span> <span class="o">+=</span> <span class="nx">@pluckDirectCall</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">body</span>
|
|
<span class="nv">varPart = </span><span class="s2">"\n#{idt1}#{namePart};"</span> <span class="k">if</span> <span class="nx">namePart</span>
|
|
<span class="k">if</span> <span class="nx">@object</span>
|
|
<span class="nv">forPart = </span><span class="s2">"#{ivar} in #{svar}"</span>
|
|
<span class="nv">guardPart = </span><span class="s2">"\n#{idt1}if (!#{utility('hasProp')}.call(#{svar}, #{ivar})) continue;"</span> <span class="k">if</span> <span class="nx">@own</span>
|
|
<span class="nv">body = </span><span class="nx">body</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">merge</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">indent</span><span class="o">:</span> <span class="nx">idt1</span><span class="p">),</span> <span class="nx">LEVEL_TOP</span>
|
|
<span class="nv">body = </span><span class="s1">'\n'</span> <span class="o">+</span> <span class="nx">body</span> <span class="o">+</span> <span class="s1">'\n'</span> <span class="k">if</span> <span class="nx">body</span>
|
|
<span class="s2">"""</span>
|
|
<span class="s2"> #{defPart}#{resultPart or ''}#{@tab}for (#{forPart}) {#{guardPart}#{varPart}#{body}#{@tab}}#{returnResult or ''}</span>
|
|
<span class="s2"> """</span>
|
|
|
|
<span class="nx">pluckDirectCall</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">body</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">defs = </span><span class="s1">''</span>
|
|
<span class="k">for</span> <span class="nx">expr</span><span class="p">,</span> <span class="nx">idx</span> <span class="k">in</span> <span class="nx">body</span><span class="p">.</span><span class="nx">expressions</span>
|
|
<span class="nv">expr = </span><span class="nx">expr</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()</span>
|
|
<span class="k">continue</span> <span class="nx">unless</span> <span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">Call</span>
|
|
<span class="nv">val = </span><span class="nx">expr</span><span class="p">.</span><span class="nx">variable</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()</span>
|
|
<span class="k">continue</span> <span class="nx">unless</span> <span class="p">(</span><span class="nx">val</span> <span class="k">instanceof</span> <span class="nx">Code</span><span class="p">)</span> <span class="o">or</span>
|
|
<span class="p">(</span><span class="nx">val</span> <span class="k">instanceof</span> <span class="nx">Value</span> <span class="o">and</span>
|
|
<span class="nx">val</span><span class="p">.</span><span class="nx">base</span><span class="o">?</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()</span> <span class="k">instanceof</span> <span class="nx">Code</span> <span class="o">and</span>
|
|
<span class="nx">val</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">1</span> <span class="o">and</span>
|
|
<span class="nx">val</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">name</span><span class="o">?</span><span class="p">.</span><span class="nx">value</span> <span class="k">in</span> <span class="p">[</span><span class="s1">'call'</span><span class="p">,</span> <span class="s1">'apply'</span><span class="p">])</span>
|
|
<span class="nv">fn = </span><span class="nx">val</span><span class="p">.</span><span class="nx">base</span><span class="o">?</span><span class="p">.</span><span class="nx">unwrapAll</span><span class="p">()</span> <span class="o">or</span> <span class="nx">val</span>
|
|
<span class="nv">ref = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'fn'</span>
|
|
<span class="nv">base = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">ref</span>
|
|
<span class="k">if</span> <span class="nx">val</span><span class="p">.</span><span class="nx">base</span>
|
|
<span class="p">[</span><span class="nx">val</span><span class="p">.</span><span class="nx">base</span><span class="p">,</span> <span class="nx">base</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nx">base</span><span class="p">,</span> <span class="nx">val</span><span class="p">]</span>
|
|
<span class="nx">body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">[</span><span class="nx">idx</span><span class="p">]</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Call</span> <span class="nx">base</span><span class="p">,</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">args</span>
|
|
<span class="nx">defs</span> <span class="o">+=</span> <span class="nx">@tab</span> <span class="o">+</span> <span class="k">new</span> <span class="nx">Assign</span><span class="p">(</span><span class="nx">ref</span><span class="p">,</span> <span class="nx">fn</span><span class="p">).</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_TOP</span><span class="p">)</span> <span class="o">+</span> <span class="s1">';\n'</span>
|
|
<span class="nx">defs</span></pre></div> </td> </tr> <tr id="section-133"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-133">¶</a> </div> <h3>Switch</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-134"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-134">¶</a> </div> <p>A JavaScript <em>switch</em> statement. Converts into a returnable expression on-demand.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.Switch = </span><span class="nx">class</span> <span class="nx">Switch</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@subject</span><span class="p">,</span> <span class="nx">@cases</span><span class="p">,</span> <span class="nx">@otherwise</span><span class="p">)</span> <span class="o">-></span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'subject'</span><span class="p">,</span> <span class="s1">'cases'</span><span class="p">,</span> <span class="s1">'otherwise'</span><span class="p">]</span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="nx">YES</span>
|
|
|
|
<span class="nx">jumps</span><span class="o">:</span> <span class="p">(</span><span class="nv">o = </span><span class="p">{</span><span class="nx">block</span><span class="o">:</span> <span class="kc">yes</span><span class="p">})</span> <span class="o">-></span>
|
|
<span class="k">for</span> <span class="p">[</span><span class="nx">conds</span><span class="p">,</span> <span class="nx">block</span><span class="p">]</span> <span class="k">in</span> <span class="nx">@cases</span>
|
|
<span class="k">return</span> <span class="nx">block</span> <span class="k">if</span> <span class="nx">block</span><span class="p">.</span><span class="nx">jumps</span> <span class="nx">o</span>
|
|
<span class="nx">@otherwise</span><span class="o">?</span><span class="p">.</span><span class="nx">jumps</span> <span class="nx">o</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">pair</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">makeReturn</span><span class="p">()</span> <span class="k">for</span> <span class="nx">pair</span> <span class="k">in</span> <span class="nx">@cases</span>
|
|
<span class="nx">@otherwise</span><span class="o">?</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span>
|
|
<span class="k">this</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">idt1 = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+</span> <span class="nx">TAB</span>
|
|
<span class="nv">idt2 = o.indent = </span><span class="nx">idt1</span> <span class="o">+</span> <span class="nx">TAB</span>
|
|
<span class="nv">code = </span><span class="nx">@tab</span> <span class="o">+</span> <span class="s2">"switch (#{ @subject?.compile(o, LEVEL_PAREN) or false }) {\n"</span>
|
|
<span class="k">for</span> <span class="p">[</span><span class="nx">conditions</span><span class="p">,</span> <span class="nx">block</span><span class="p">],</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">@cases</span>
|
|
<span class="k">for</span> <span class="nx">cond</span> <span class="k">in</span> <span class="nx">flatten</span> <span class="p">[</span><span class="nx">conditions</span><span class="p">]</span>
|
|
<span class="nv">cond = </span><span class="nx">cond</span><span class="p">.</span><span class="nx">invert</span><span class="p">()</span> <span class="nx">unless</span> <span class="nx">@subject</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="nx">idt1</span> <span class="o">+</span> <span class="s2">"case #{ cond.compile o, LEVEL_PAREN }:\n"</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="nx">body</span> <span class="o">+</span> <span class="s1">'\n'</span> <span class="k">if</span> <span class="nv">body = </span><span class="nx">block</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_TOP</span>
|
|
<span class="k">break</span> <span class="k">if</span> <span class="nx">i</span> <span class="o">is</span> <span class="nx">@cases</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@otherwise</span>
|
|
<span class="nv">expr = </span><span class="nx">@lastNonComment</span> <span class="nx">block</span><span class="p">.</span><span class="nx">expressions</span>
|
|
<span class="k">continue</span> <span class="k">if</span> <span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">Return</span> <span class="o">or</span> <span class="p">(</span><span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">Literal</span> <span class="o">and</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">jumps</span><span class="p">()</span> <span class="o">and</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">value</span> <span class="o">isnt</span> <span class="s1">'debugger'</span><span class="p">)</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="nx">idt2</span> <span class="o">+</span> <span class="s1">'break;\n'</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="nx">idt1</span> <span class="o">+</span> <span class="s2">"default:\n#{ @otherwise.compile o, LEVEL_TOP }\n"</span> <span class="k">if</span> <span class="nx">@otherwise</span> <span class="o">and</span> <span class="nx">@otherwise</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="nx">code</span> <span class="o">+</span> <span class="nx">@tab</span> <span class="o">+</span> <span class="s1">'}'</span></pre></div> </td> </tr> <tr id="section-135"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-135">¶</a> </div> <h3>If</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-136"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-136">¶</a> </div> <p><em>If/else</em> statements. Acts as an expression by pushing down requested returns
|
|
to the last line of each clause.</p>
|
|
|
|
<p>Single-expression <strong>Ifs</strong> are compiled into conditional operators if possible,
|
|
because ternaries are already proper expressions, and don't need conversion.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.If = </span><span class="nx">class</span> <span class="nx">If</span> <span class="k">extends</span> <span class="nx">Base</span>
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">condition</span><span class="p">,</span> <span class="nx">@body</span><span class="p">,</span> <span class="nv">options = </span><span class="p">{})</span> <span class="o">-></span>
|
|
<span class="vi">@condition = </span><span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">type</span> <span class="o">is</span> <span class="s1">'unless'</span> <span class="k">then</span> <span class="nx">condition</span><span class="p">.</span><span class="nx">invert</span><span class="p">()</span> <span class="k">else</span> <span class="nx">condition</span>
|
|
<span class="vi">@elseBody = </span><span class="kc">null</span>
|
|
<span class="vi">@isChain = </span><span class="kc">false</span>
|
|
<span class="p">{</span><span class="nx">@soak</span><span class="p">}</span> <span class="o">=</span> <span class="nx">options</span>
|
|
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'condition'</span><span class="p">,</span> <span class="s1">'body'</span><span class="p">,</span> <span class="s1">'elseBody'</span><span class="p">]</span>
|
|
|
|
<span class="nx">bodyNode</span><span class="o">:</span> <span class="o">-></span> <span class="nx">@body</span><span class="o">?</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span>
|
|
<span class="nx">elseBodyNode</span><span class="o">:</span> <span class="o">-></span> <span class="nx">@elseBody</span><span class="o">?</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-137"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-137">¶</a> </div> <p>Rewrite a chain of <strong>Ifs</strong> to add a default case as the final <em>else</em>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">addElse</span><span class="o">:</span> <span class="p">(</span><span class="nx">elseBody</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@isChain</span>
|
|
<span class="nx">@elseBodyNode</span><span class="p">().</span><span class="nx">addElse</span> <span class="nx">elseBody</span>
|
|
<span class="k">else</span>
|
|
<span class="vi">@isChain = </span><span class="nx">elseBody</span> <span class="k">instanceof</span> <span class="nx">If</span>
|
|
<span class="vi">@elseBody = </span><span class="nx">@ensureBlock</span> <span class="nx">elseBody</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-138"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-138">¶</a> </div> <p>The <strong>If</strong> only compiles into a statement if either of its bodies needs
|
|
to be a statement. Otherwise a conditional operator is safe.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">isStatement</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">o</span><span class="o">?</span><span class="p">.</span><span class="nx">level</span> <span class="o">is</span> <span class="nx">LEVEL_TOP</span> <span class="o">or</span>
|
|
<span class="nx">@bodyNode</span><span class="p">().</span><span class="nx">isStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">or</span> <span class="nx">@elseBodyNode</span><span class="p">()</span><span class="o">?</span><span class="p">.</span><span class="nx">isStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
|
|
<span class="nx">jumps</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span> <span class="nx">@body</span><span class="p">.</span><span class="nx">jumps</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">or</span> <span class="nx">@elseBody</span><span class="o">?</span><span class="p">.</span><span class="nx">jumps</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
|
|
<span class="nx">compileNode</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@isStatement</span> <span class="nx">o</span> <span class="k">then</span> <span class="nx">@compileStatement</span> <span class="nx">o</span> <span class="k">else</span> <span class="nx">@compileExpression</span> <span class="nx">o</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@body</span> <span class="o">and=</span> <span class="k">new</span> <span class="nx">Block</span> <span class="p">[</span><span class="nx">@body</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()]</span>
|
|
<span class="nx">@elseBody</span> <span class="o">and=</span> <span class="k">new</span> <span class="nx">Block</span> <span class="p">[</span><span class="nx">@elseBody</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()]</span>
|
|
<span class="k">this</span>
|
|
|
|
<span class="nx">ensureBlock</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Block</span> <span class="k">then</span> <span class="nx">node</span> <span class="k">else</span> <span class="k">new</span> <span class="nx">Block</span> <span class="p">[</span><span class="nx">node</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-139"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-139">¶</a> </div> <p>Compile the <code>If</code> as a regular <em>if-else</em> statement. Flattened chains
|
|
force inner <em>else</em> bodies into statement form.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileStatement</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">child = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'chainChild'</span>
|
|
<span class="nv">exeq = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'isExistentialEquals'</span>
|
|
|
|
<span class="k">if</span> <span class="nx">exeq</span>
|
|
<span class="k">return</span> <span class="k">new</span> <span class="nx">If</span><span class="p">(</span><span class="nx">@condition</span><span class="p">.</span><span class="nx">invert</span><span class="p">(),</span> <span class="nx">@elseBodyNode</span><span class="p">(),</span> <span class="nx">type</span><span class="o">:</span> <span class="s1">'if'</span><span class="p">).</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
|
|
<span class="nv">cond = </span><span class="nx">@condition</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_PAREN</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">indent</span> <span class="o">+=</span> <span class="nx">TAB</span>
|
|
<span class="nv">body = </span><span class="nx">@ensureBlock</span><span class="p">(</span><span class="nx">@body</span><span class="p">).</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nv">body = </span><span class="s2">"\n#{body}\n#{@tab}"</span> <span class="k">if</span> <span class="nx">body</span>
|
|
<span class="nv">ifPart = </span><span class="s2">"if (#{cond}) {#{body}}"</span>
|
|
<span class="nv">ifPart = </span><span class="nx">@tab</span> <span class="o">+</span> <span class="nx">ifPart</span> <span class="nx">unless</span> <span class="nx">child</span>
|
|
<span class="k">return</span> <span class="nx">ifPart</span> <span class="nx">unless</span> <span class="nx">@elseBody</span>
|
|
<span class="nx">ifPart</span> <span class="o">+</span> <span class="s1">' else '</span> <span class="o">+</span> <span class="k">if</span> <span class="nx">@isChain</span>
|
|
<span class="nv">o.indent = </span><span class="nx">@tab</span>
|
|
<span class="nv">o.chainChild = </span><span class="kc">yes</span>
|
|
<span class="nx">@elseBody</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">().</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_TOP</span>
|
|
<span class="k">else</span>
|
|
<span class="s2">"{\n#{ @elseBody.compile o, LEVEL_TOP }\n#{@tab}}"</span></pre></div> </td> </tr> <tr id="section-140"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-140">¶</a> </div> <p>Compile the <code>If</code> as a conditional operator.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileExpression</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">cond = </span><span class="nx">@condition</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_COND</span>
|
|
<span class="nv">body = </span><span class="nx">@bodyNode</span><span class="p">().</span><span class="nx">compile</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span>
|
|
<span class="nv">alt = </span><span class="k">if</span> <span class="nx">@elseBodyNode</span><span class="p">()</span> <span class="k">then</span> <span class="nx">@elseBodyNode</span><span class="p">().</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">LEVEL_LIST</span><span class="p">)</span> <span class="k">else</span> <span class="s1">'void 0'</span>
|
|
<span class="nv">code = </span><span class="s2">"#{cond} ? #{body} : #{alt}"</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">level</span> <span class="o">>=</span> <span class="nx">LEVEL_COND</span> <span class="k">then</span> <span class="s2">"(#{code})"</span> <span class="k">else</span> <span class="nx">code</span>
|
|
|
|
<span class="nx">unfoldSoak</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@soak</span> <span class="o">and</span> <span class="k">this</span></pre></div> </td> </tr> <tr id="section-141"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-141">¶</a> </div> <h2>Faux-Nodes</h2>
|
|
|
|
<p>Faux-nodes are never created by the grammar, but are used during code
|
|
generation to generate other combinations of nodes.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-142"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-142">¶</a> </div> <h3>Push</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-143"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-143">¶</a> </div> <p>The <strong>Push</strong> creates the tree for <code>array.push(value)</code>,
|
|
which is helpful for recording the result arrays from comprehensions.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">Push =</span>
|
|
<span class="nx">wrap</span><span class="o">:</span> <span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">exps</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">exps</span> <span class="k">if</span> <span class="nx">exps</span><span class="p">.</span><span class="nx">isEmpty</span><span class="p">()</span> <span class="o">or</span> <span class="nx">last</span><span class="p">(</span><span class="nx">exps</span><span class="p">.</span><span class="nx">expressions</span><span class="p">).</span><span class="nx">jumps</span><span class="p">()</span>
|
|
<span class="nx">exps</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">Call</span> <span class="k">new</span> <span class="nx">Value</span><span class="p">(</span><span class="k">new</span> <span class="nx">Literal</span><span class="p">(</span><span class="nx">name</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">Access</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s1">'push'</span><span class="p">]),</span> <span class="p">[</span><span class="nx">exps</span><span class="p">.</span><span class="nx">pop</span><span class="p">()]</span></pre></div> </td> </tr> <tr id="section-144"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-144">¶</a> </div> <h3>Closure</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-145"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-145">¶</a> </div> <p>A faux-node used to wrap an expressions body in a closure.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">Closure =</span></pre></div> </td> </tr> <tr id="section-146"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-146">¶</a> </div> <p>Wrap the expressions body, unless it contains a pure statement,
|
|
in which case, no dice. If the body mentions <code>this</code> or <code>arguments</code>,
|
|
then make sure that the closure wrapper preserves the original values.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">wrap</span><span class="o">:</span> <span class="p">(</span><span class="nx">expressions</span><span class="p">,</span> <span class="nx">statement</span><span class="p">,</span> <span class="nx">noReturn</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">expressions</span> <span class="k">if</span> <span class="nx">expressions</span><span class="p">.</span><span class="nx">jumps</span><span class="p">()</span>
|
|
<span class="nv">func = </span><span class="k">new</span> <span class="nx">Code</span> <span class="p">[],</span> <span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">expressions</span><span class="p">]</span>
|
|
<span class="nv">args = </span><span class="p">[]</span>
|
|
<span class="k">if</span> <span class="p">(</span><span class="nv">mentionsArgs = </span><span class="nx">expressions</span><span class="p">.</span><span class="nx">contains</span> <span class="nx">@literalArgs</span><span class="p">)</span> <span class="o">or</span> <span class="nx">expressions</span><span class="p">.</span><span class="nx">contains</span> <span class="nx">@literalThis</span>
|
|
<span class="nv">meth = </span><span class="k">new</span> <span class="nx">Literal</span> <span class="k">if</span> <span class="nx">mentionsArgs</span> <span class="k">then</span> <span class="s1">'apply'</span> <span class="k">else</span> <span class="s1">'call'</span>
|
|
<span class="nv">args = </span><span class="p">[</span><span class="k">new</span> <span class="nx">Literal</span> <span class="s1">'this'</span><span class="p">]</span>
|
|
<span class="nx">args</span><span class="p">.</span><span class="nx">push</span> <span class="k">new</span> <span class="nx">Literal</span> <span class="s1">'arguments'</span> <span class="k">if</span> <span class="nx">mentionsArgs</span>
|
|
<span class="nv">func = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">func</span><span class="p">,</span> <span class="p">[</span><span class="k">new</span> <span class="nx">Access</span> <span class="nx">meth</span><span class="p">]</span>
|
|
<span class="nv">func.noReturn = </span><span class="nx">noReturn</span>
|
|
<span class="nv">call = </span><span class="k">new</span> <span class="nx">Call</span> <span class="nx">func</span><span class="p">,</span> <span class="nx">args</span>
|
|
<span class="k">if</span> <span class="nx">statement</span> <span class="k">then</span> <span class="nx">Block</span><span class="p">.</span><span class="nx">wrap</span> <span class="p">[</span><span class="nx">call</span><span class="p">]</span> <span class="k">else</span> <span class="nx">call</span>
|
|
|
|
<span class="nx">literalArgs</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Literal</span> <span class="o">and</span> <span class="nx">node</span><span class="p">.</span><span class="nx">value</span> <span class="o">is</span> <span class="s1">'arguments'</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">node</span><span class="p">.</span><span class="nx">asKey</span>
|
|
<span class="nx">literalThis</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="p">(</span><span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Literal</span> <span class="o">and</span> <span class="nx">node</span><span class="p">.</span><span class="nx">value</span> <span class="o">is</span> <span class="s1">'this'</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">node</span><span class="p">.</span><span class="nx">asKey</span><span class="p">)</span> <span class="o">or</span>
|
|
<span class="p">(</span><span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Code</span> <span class="o">and</span> <span class="nx">node</span><span class="p">.</span><span class="nx">bound</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-147"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-147">¶</a> </div> <p>Unfold a node's child if soak, then tuck the node under created <code>If</code></p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">unfoldSoak = </span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">parent</span><span class="p">,</span> <span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">return</span> <span class="nx">unless</span> <span class="nv">ifn = </span><span class="nx">parent</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">unfoldSoak</span> <span class="nx">o</span>
|
|
<span class="nx">parent</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">ifn</span><span class="p">.</span><span class="nx">body</span>
|
|
<span class="nv">ifn.body = </span><span class="k">new</span> <span class="nx">Value</span> <span class="nx">parent</span>
|
|
<span class="nx">ifn</span></pre></div> </td> </tr> <tr id="section-148"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-148">¶</a> </div> <h2>Constants</h2> </td> <td class="code"> <div class="highlight"><pre><span class="nv">UTILITIES =</span></pre></div> </td> </tr> <tr id="section-149"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-149">¶</a> </div> <p>Correctly set up a prototype chain for inheritance, including a reference
|
|
to the superclass for <code>super()</code> calls, and copies of any static properties.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">extends</span><span class="o">:</span> <span class="s1">'''</span>
|
|
<span class="s1"> function(child, parent) {</span>
|
|
<span class="s1"> for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }</span>
|
|
<span class="s1"> function ctor() { this.constructor = child; }</span>
|
|
<span class="s1"> ctor.prototype = parent.prototype;</span>
|
|
<span class="s1"> child.prototype = new ctor;</span>
|
|
<span class="s1"> child.__super__ = parent.prototype;</span>
|
|
<span class="s1"> return child;</span>
|
|
<span class="s1"> }</span>
|
|
<span class="s1"> '''</span></pre></div> </td> </tr> <tr id="section-150"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-150">¶</a> </div> <p>Create a function bound to the current value of "this".</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">bind</span><span class="o">:</span> <span class="s1">'''</span>
|
|
<span class="s1"> function(fn, me){ return function(){ return fn.apply(me, arguments); }; }</span>
|
|
<span class="s1"> '''</span></pre></div> </td> </tr> <tr id="section-151"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-151">¶</a> </div> <p>Discover if an item is in an array.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">indexOf</span><span class="o">:</span> <span class="s1">'''</span>
|
|
<span class="s1"> Array.prototype.indexOf || function(item) {</span>
|
|
<span class="s1"> for (var i = 0, l = this.length; i < l; i++) {</span>
|
|
<span class="s1"> if (this[i] === item) return i;</span>
|
|
<span class="s1"> }</span>
|
|
<span class="s1"> return -1;</span>
|
|
<span class="s1"> }</span>
|
|
<span class="s1"> '''</span></pre></div> </td> </tr> <tr id="section-152"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-152">¶</a> </div> <p>Shortcuts to speed up the lookup time for native functions.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">hasProp</span><span class="o">:</span> <span class="s1">'Object.prototype.hasOwnProperty'</span>
|
|
<span class="nx">slice</span> <span class="o">:</span> <span class="s1">'Array.prototype.slice'</span></pre></div> </td> </tr> <tr id="section-153"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-153">¶</a> </div> <p>Levels indicate a node's position in the AST. Useful for knowing if
|
|
parens are necessary or superfluous.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">LEVEL_TOP = </span><span class="mi">1</span> <span class="c1"># ...;</span>
|
|
<span class="nv">LEVEL_PAREN = </span><span class="mi">2</span> <span class="c1"># (...)</span>
|
|
<span class="nv">LEVEL_LIST = </span><span class="mi">3</span> <span class="c1"># [...]</span>
|
|
<span class="nv">LEVEL_COND = </span><span class="mi">4</span> <span class="c1"># ... ? x : y</span>
|
|
<span class="nv">LEVEL_OP = </span><span class="mi">5</span> <span class="c1"># !...</span>
|
|
<span class="nv">LEVEL_ACCESS = </span><span class="mi">6</span> <span class="c1"># ...[0]</span></pre></div> </td> </tr> <tr id="section-154"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-154">¶</a> </div> <p>Tabs are two spaces for pretty printing.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">TAB = </span><span class="s1">' '</span>
|
|
|
|
<span class="nv">IDENTIFIER_STR = </span><span class="s2">"[$A-Za-z_\\x7f-\\uffff][$\\w\\x7f-\\uffff]*"</span>
|
|
<span class="nv">IDENTIFIER = </span><span class="err">/// ^ #{IDENTIFIER_STR} $ ///</span>
|
|
<span class="nv">SIMPLENUM = </span><span class="sr">/^[+-]?\d+$/</span>
|
|
<span class="nv">METHOD_DEF = </span><span class="err">///</span>
|
|
<span class="o">^</span>
|
|
<span class="p">(</span><span class="o">?:</span>
|
|
<span class="p">(</span><span class="c1">#{IDENTIFIER_STR})</span>
|
|
<span class="err">\</span><span class="p">.</span><span class="nx">prototype</span>
|
|
<span class="p">(</span><span class="o">?:</span>
|
|
<span class="err">\</span><span class="p">.(</span><span class="c1">#{IDENTIFIER_STR})</span>
|
|
<span class="o">|</span> <span class="err">\</span><span class="p">[(</span><span class="s2">"(?:[^\\"</span><span class="err">\</span><span class="nx">r</span><span class="err">\</span><span class="nx">n</span><span class="p">]</span><span class="o">|</span><span class="err">\\</span><span class="p">.)</span><span class="o">*</span><span class="s2">"|'(?:[^\\'\r\n]|\\.)*')\]</span>
|
|
<span class="s2"> | \[(0x[\da-fA-F]+ | \d*\.?\d+ (?:[eE][+-]?\d+)?)\]</span>
|
|
<span class="s2"> )</span>
|
|
<span class="s2"> )</span>
|
|
<span class="s2"> |</span>
|
|
<span class="s2"> (#{IDENTIFIER_STR})</span>
|
|
<span class="s2"> $</span>
|
|
<span class="s2">///</span>
|
|
|
|
|
|
<span class="s2">#DIVIDER</span>
|
|
<span class="s2">IS_STRING = /^['"</span><span class="p">]</span><span class="o">/</span></pre></div> </td> </tr> <tr id="section-155"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-155">¶</a> </div> <p>Is a literal value a string?</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-156"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-156">¶</a> </div> <h2>Utility Functions</h2> </td> <td class="code"> <div class="highlight"><pre><span class="nv">utility = </span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">ref = </span><span class="s2">"__#{name}"</span>
|
|
<span class="nx">Scope</span><span class="p">.</span><span class="nx">root</span><span class="p">.</span><span class="nx">assign</span> <span class="nx">ref</span><span class="p">,</span> <span class="nx">UTILITIES</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span>
|
|
<span class="nx">ref</span>
|
|
|
|
<span class="nv">multident = </span><span class="p">(</span><span class="nx">code</span><span class="p">,</span> <span class="nx">tab</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">code</span><span class="p">.</span><span class="nx">replace</span> <span class="sr">/\n/g</span><span class="p">,</span> <span class="s1">'$&'</span> <span class="o">+</span> <span class="nx">tab</span>
|
|
|
|
</pre></div> </td> </tr> <tr id="section-157"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-157">¶</a> </div> <p>Helper for ensuring that utility functions are assigned at the top level.</p> </td> <td class="code"> <div class="highlight"><pre>undefined</pre></div> </td> </tr> </tbody> </table> </div> </body> </html> |