mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
1245 lines
No EOL
272 KiB
HTML
1245 lines
No EOL
272 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="octowrap"> <a class="octothorpe" 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="octowrap"> <a class="octothorpe" 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">merge</span><span class="p">,</span> <span class="nx">del</span><span class="p">,</span> <span class="nx">include</span><span class="p">,</span> <span class="nx">indexOf</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="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'./helpers'</span><span class="p">).</span><span class="nx">helpers</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-3">#</a> </div> <h3>BaseNode</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-4">#</a> </div> <p>The <strong>BaseNode</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.BaseNode = </span><span class="nx">class</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">constructor</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="vi">@tags = </span><span class="p">{}</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-5">#</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>
|
|
|
|
<p>If a Node is <em>topSensitive</em>, that means that it needs to compile differently
|
|
depending on whether it's being used as part of a larger expression, or is a
|
|
top-level statement within the function body.</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="vi">@options = </span><span class="nx">merge</span> <span class="nx">o</span> <span class="o">or</span> <span class="p">{}</span>
|
|
<span class="vi">@tab = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span>
|
|
<span class="nx">del</span> <span class="nx">@options</span><span class="p">,</span> <span class="s1">'chainRoot'</span> <span class="nx">unless</span> <span class="k">this</span> <span class="k">instanceof</span> <span class="nx">AccessorNode</span> <span class="o">or</span> <span class="k">this</span> <span class="k">instanceof</span> <span class="nx">IndexNode</span>
|
|
<span class="nv">top = </span><span class="k">if</span> <span class="nx">@topSensitive</span><span class="p">()</span> <span class="k">then</span> <span class="nx">@options</span><span class="p">.</span><span class="nx">top</span> <span class="k">else</span> <span class="nx">del</span> <span class="nx">@options</span><span class="p">,</span> <span class="s1">'top'</span>
|
|
<span class="nv">closure = </span><span class="nx">@isStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@isPureStatement</span><span class="p">()</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">top</span> <span class="o">and</span>
|
|
<span class="o">not</span> <span class="nx">@options</span><span class="p">.</span><span class="nx">asStatement</span> <span class="o">and</span> <span class="k">this</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">CommentNode</span> <span class="o">and</span>
|
|
<span class="o">not</span> <span class="nx">@containsPureStatement</span><span class="p">()</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">startLevel</span><span class="p">()</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">o</span><span class="p">.</span><span class="nx">keepLevel</span>
|
|
<span class="nv">code = </span><span class="k">if</span> <span class="nx">closure</span> <span class="k">then</span> <span class="nx">@compileClosure</span><span class="p">(</span><span class="nx">@options</span><span class="p">)</span> <span class="k">else</span> <span class="nx">@compileNode</span><span class="p">(</span><span class="nx">@options</span><span class="p">)</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">endLevel</span><span class="p">()</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">o</span><span class="p">.</span><span class="nx">keepLevel</span>
|
|
<span class="nx">code</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-6">#</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="vi">@tab = </span><span class="nx">o</span><span class="p">.</span><span class="nx">indent</span>
|
|
<span class="nv">o.sharedScope = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span>
|
|
<span class="nx">ClosureNode</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">compile</span> <span class="nx">o</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-7">#</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.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileReference</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">options</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">options</span> <span class="o">or=</span> <span class="p">{}</span>
|
|
<span class="nv">pair = </span><span class="k">if</span> <span class="o">not</span> <span class="p">(</span><span class="nx">@containsType</span><span class="p">(</span><span class="nx">CallNode</span><span class="p">)</span> <span class="o">or</span>
|
|
<span class="p">(</span><span class="k">this</span> <span class="k">instanceof</span> <span class="nx">ValueNode</span> <span class="o">and</span> <span class="p">(</span><span class="o">not</span> <span class="p">(</span><span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">LiteralNode</span><span class="p">)</span> <span class="o">or</span> <span class="nx">@hasProperties</span><span class="p">())))</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="k">else</span> <span class="k">if</span> <span class="k">this</span> <span class="k">instanceof</span> <span class="nx">ValueNode</span> <span class="o">and</span> <span class="nx">options</span><span class="p">.</span><span class="nx">assignment</span>
|
|
<span class="k">this</span><span class="p">.</span><span class="nx">cacheIndexes</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">reference = </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">compiled = </span><span class="k">new</span> <span class="nx">AssignNode</span> <span class="nx">reference</span><span class="p">,</span> <span class="k">this</span>
|
|
<span class="p">[</span><span class="nx">compiled</span><span class="p">,</span> <span class="nx">reference</span><span class="p">]</span>
|
|
<span class="k">return</span> <span class="p">[</span><span class="nx">pair</span><span class="p">[</span><span class="mi">0</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">pair</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)]</span> <span class="k">if</span> <span class="nx">options</span><span class="p">.</span><span class="nx">precompile</span>
|
|
<span class="nx">pair</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-8">#</a> </div> <p>Convenience method to grab the current indentation level, plus tabbing in.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">idt</span><span class="o">:</span> <span class="p">(</span><span class="nx">tabs</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">idt = </span><span class="nx">@tab</span> <span class="o">or</span> <span class="s1">''</span>
|
|
<span class="nv">num = </span><span class="p">(</span><span class="nx">tabs</span> <span class="o">or</span> <span class="mi">0</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
|
|
<span class="nx">idt</span> <span class="o">+=</span> <span class="nx">TAB</span> <span class="k">while</span> <span class="nx">num</span> <span class="o">-=</span> <span class="mi">1</span>
|
|
<span class="nx">idt</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-9">#</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 (eg IfNode, ForNode)...</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">ReturnNode</span> <span class="k">this</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-10">#</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">block</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">contains = </span><span class="kc">false</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">if</span> <span class="nx">block</span><span class="p">(</span><span class="nx">node</span><span class="p">)</span>
|
|
<span class="nv">contains = </span><span class="kc">true</span>
|
|
<span class="k">return</span> <span class="kc">false</span>
|
|
<span class="nx">contains</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-11">#</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">n</span><span class="p">)</span> <span class="o">-></span> <span class="nx">n</span> <span class="k">instanceof</span> <span class="nx">type</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-12">#</a> </div> <p>Convenience for the most common use of contains. Does the node contain
|
|
a pure statement?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">containsPureStatement</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@isPureStatement</span><span class="p">()</span> <span class="o">or</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="p">.</span><span class="nx">isPureStatement</span> <span class="o">and</span> <span class="nx">n</span><span class="p">.</span><span class="nx">isPureStatement</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-13">#</a> </div> <p>Perform an in-order traversal of the AST. Crosses scope boundaries.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">traverse</span><span class="o">:</span> <span class="p">(</span><span class="nx">block</span><span class="p">)</span> <span class="o">-></span> <span class="nx">@traverseChildren</span> <span class="kc">true</span><span class="p">,</span> <span class="nx">block</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" 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="nx">idt</span><span class="p">,</span> <span class="nx">override</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">idt</span> <span class="o">or=</span> <span class="s1">''</span>
|
|
<span class="nv">children = </span><span class="p">(</span><span class="nx">child</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="k">for</span> <span class="nx">child</span> <span class="k">in</span> <span class="nx">@collectChildren</span><span class="p">()).</span><span class="nx">join</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span>
|
|
<span class="s1">'\n'</span> <span class="o">+</span> <span class="nx">idt</span> <span class="o">+</span> <span class="p">(</span><span class="nx">override</span> <span class="o">or</span> <span class="nx">@class</span><span class="p">)</span> <span class="o">+</span> <span class="nx">children</span>
|
|
|
|
<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="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="k">this</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="k">this</span><span class="p">[</span><span class="nx">attr</span><span class="p">]]</span>
|
|
<span class="k">return</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">collectChildren</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nv">nodes = </span><span class="p">[]</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">nodes</span><span class="p">.</span><span class="nx">push</span> <span class="nx">node</span>
|
|
<span class="nx">nodes</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="nx">func</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">)</span>
|
|
<span class="nx">child</span><span class="p">.</span><span class="nx">traverseChildren</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">child</span> <span class="k">instanceof</span> <span class="nx">BaseNode</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-15">#</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">class</span><span class="o">:</span> <span class="s1">'BaseNode'</span>
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[]</span>
|
|
|
|
<span class="nx">unwrap</span> <span class="o">:</span> <span class="o">-></span> <span class="k">this</span>
|
|
<span class="nx">isStatement</span> <span class="o">:</span> <span class="o">-></span> <span class="kc">no</span>
|
|
<span class="nx">isPureStatement</span> <span class="o">:</span> <span class="o">-></span> <span class="kc">no</span>
|
|
<span class="nx">topSensitive</span> <span class="o">:</span> <span class="o">-></span> <span class="kc">no</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-16">#</a> </div> <h3>Expressions</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-17">#</a> </div> <p>The expressions body 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.Expressions = </span><span class="nx">class</span> <span class="nx">Expressions</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'Expressions'</span>
|
|
<span class="nx">children</span><span class="o">:</span> <span class="p">[</span><span class="s1">'expressions'</span><span class="p">]</span>
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="o">-></span> <span class="kc">yes</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="k">super</span><span class="p">()</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></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-18">#</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="p">(</span><span class="nx">node</span><span class="p">)</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-19">#</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="p">(</span><span class="nx">node</span><span class="p">)</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-20">#</a> </div> <p>If this Expressions 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-21"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-21">#</a> </div> <p>Is this an empty block of code?</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">empty</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@expressions</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">0</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-22">#</a> </div> <p>An Expressions 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">idx = </span><span class="nx">@expressions</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span>
|
|
<span class="nv">last = </span><span class="nx">@expressions</span><span class="p">[</span><span class="nx">idx</span><span class="p">]</span>
|
|
<span class="nv">last = </span><span class="nx">@expressions</span><span class="p">[</span><span class="nx">idx</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">]</span> <span class="k">if</span> <span class="nx">last</span> <span class="k">instanceof</span> <span class="nx">CommentNode</span>
|
|
<span class="k">return</span> <span class="k">this</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">last</span> <span class="o">or</span> <span class="nx">last</span> <span class="k">instanceof</span> <span class="nx">ReturnNode</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="nx">last</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-23">#</a> </div> <p>An <strong>Expressions</strong> is the only node that can serve as the root.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compile</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">o</span> <span class="o">or=</span> <span class="p">{}</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="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">else</span> <span class="nx">@compileRoot</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="p">(</span><span class="nx">@compileExpression</span><span class="p">(</span><span class="nx">node</span><span class="p">,</span> <span class="nx">merge</span><span class="p">(</span><span class="nx">o</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="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s2">"\n"</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-24">#</a> </div> <p>If we happen to be the top-level <strong>Expressions</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">noWrap</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="p">(</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="p">)</span>
|
|
<span class="nv">code = </span><span class="nx">@compileWithDeclarations</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
<span class="nv">code = </span><span class="nx">code</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">TRAILING_WHITESPACE</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">noWrap</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-25"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-25">#</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 = </span><span class="nx">@compileNode</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
<span class="nv">code = </span><span class="s2">"#{@tab}var #{o.scope.compiledAssignments()};\n#{code}"</span> <span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">hasAssignments</span><span class="p">(</span><span class="k">this</span><span class="p">)</span>
|
|
<span class="nv">code = </span><span class="s2">"#{@tab}var #{o.scope.compiledDeclarations()};\n#{code}"</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">o</span><span class="p">.</span><span class="nx">globals</span> <span class="o">and</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">hasDeclarations</span><span class="p">(</span><span class="k">this</span><span class="p">)</span>
|
|
<span class="nx">code</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-26">#</a> </div> <p>Compiles a single expression within the expressions 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">compileExpression</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</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">compiledNode = </span><span class="nx">node</span><span class="p">.</span><span class="nx">compile</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="k">if</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="k">then</span> <span class="nx">compiledNode</span> <span class="k">else</span> <span class="s2">"#{@idt()}#{compiledNode};"</span></pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-27">#</a> </div> <p>Wrap up the given nodes as an <strong>Expressions</strong>, unless it already happens
|
|
to be one.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">Expressions.wrap = </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">Expressions</span>
|
|
<span class="k">new</span> <span class="nx">Expressions</span><span class="p">(</span><span class="nx">nodes</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-28"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-28">#</a> </div> <h3>LiteralNode</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-29"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-29">#</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.LiteralNode = </span><span class="nx">class</span> <span class="nx">LiteralNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'LiteralNode'</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="k">super</span><span class="p">()</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">super</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-30"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-30">#</a> </div> <p>Break and continue must be treated as pure statements -- they lose their
|
|
meaning when wrapped in a closure.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">isStatement</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@value</span> <span class="o">is</span> <span class="s1">'break'</span> <span class="o">or</span> <span class="nx">@value</span> <span class="o">is</span> <span class="s1">'continue'</span> <span class="o">or</span> <span class="nx">@value</span> <span class="o">is</span> <span class="s1">'debugger'</span>
|
|
<span class="nx">isPureStatement</span><span class="o">:</span> <span class="nx">LiteralNode</span><span class="o">::</span><span class="nx">isStatement</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">idt = </span><span class="k">if</span> <span class="nx">@isStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">then</span> <span class="nx">@idt</span><span class="p">()</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="nv">end = </span><span class="k">if</span> <span class="nx">@isStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">then</span> <span class="s1">';'</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="nx">idt</span> <span class="o">+</span> <span class="nx">@value</span> <span class="o">+</span> <span class="nx">end</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="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-31"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-31">#</a> </div> <h3>ReturnNode</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-32"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-32">#</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.ReturnNode = </span><span class="nx">class</span> <span class="nx">ReturnNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'ReturnNode'</span>
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="o">-></span> <span class="kc">yes</span>
|
|
<span class="nx">isPureStatement</span><span class="o">:</span> <span class="o">-></span> <span class="kc">yes</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">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="k">super</span><span class="p">()</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="k">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="o">-></span>
|
|
<span class="nv">expr = </span><span class="nx">@expression</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</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="nx">unless</span> <span class="nx">expr</span> <span class="k">instanceof</span> <span class="nx">ReturnNode</span>
|
|
<span class="k">super</span> <span class="nx">o</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">o.asStatement = </span><span class="kc">true</span> <span class="k">if</span> <span class="nx">@expression</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="s2">"#{@tab}return #{@expression.compile(o)};"</span></pre></div> </td> </tr> <tr id="section-33"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-33">#</a> </div> <h3>ValueNode</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-34"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-34">#</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.ValueNode = </span><span class="nx">class</span> <span class="nx">ValueNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'ValueNode'</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-35"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-35">#</a> </div> <p>A <strong>ValueNode</strong> has a base and a list of property accesses.</p> </td> <td class="code"> <div class="highlight"><pre> <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">@properties</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">super</span><span class="p">()</span>
|
|
<span class="nx">@properties</span> <span class="o">or=</span> <span class="p">[]</span></pre></div> </td> </tr> <tr id="section-36"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-36">#</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="p">(</span><span class="nx">prop</span><span class="p">)</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-37"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-37">#</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="nx">@base</span> <span class="k">instanceof</span> <span class="nx">ArrayNode</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@hasProperties</span><span class="p">()</span>
|
|
|
|
<span class="nx">isObject</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">ObjectNode</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@hasProperties</span><span class="p">()</span>
|
|
|
|
<span class="nx">isSplice</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@hasProperties</span><span class="p">()</span> <span class="o">and</span> <span class="nx">@properties</span><span class="p">[</span><span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nx">SliceNode</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">@hasProperties</span><span class="p">()</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-38"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-38">#</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-39"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-39">#</a> </div> <p>Values are considered to be statements if their base is a statement.</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">@base</span><span class="p">.</span><span class="nx">isStatement</span> <span class="o">and</span> <span class="nx">@base</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">and</span> <span class="o">not</span> <span class="nx">@hasProperties</span><span class="p">()</span>
|
|
|
|
<span class="nx">isNumber</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">LiteralNode</span> <span class="o">and</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">value</span><span class="p">.</span><span class="nx">match</span> <span class="nx">NUMBER</span></pre></div> </td> </tr> <tr id="section-40"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-40">#</a> </div> <p>If the value node has indexes containing function calls, and the value node
|
|
needs to be used twice, in compound assignment ... then we need to cache
|
|
the value of the indexes.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">cacheIndexes</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">copy = </span><span class="k">new</span> <span class="nx">ValueNode</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="k">if</span> <span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">CallNode</span>
|
|
<span class="p">[</span><span class="nx">@base</span><span class="p">,</span> <span class="nx">copy</span><span class="p">.</span><span class="nx">base</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">compileReference</span> <span class="nx">o</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">copy</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">IndexNode</span> <span class="o">and</span> <span class="nx">prop</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">CallNode</span><span class="p">)</span>
|
|
<span class="p">[</span><span class="nx">index</span><span class="p">,</span> <span class="nx">indexVar</span><span class="p">]</span> <span class="o">=</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">index</span><span class="p">.</span><span class="nx">compileReference</span> <span class="nx">o</span>
|
|
<span class="k">this</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="o">=</span> <span class="k">new</span> <span class="nx">IndexNode</span> <span class="nx">index</span>
|
|
<span class="nx">copy</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="o">=</span> <span class="k">new</span> <span class="nx">IndexNode</span> <span class="nx">indexVar</span>
|
|
<span class="p">[</span><span class="k">this</span><span class="p">,</span> <span class="nx">copy</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-41"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-41">#</a> </div> <p>Override compile to unwrap the value when possible.</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="k">if</span> <span class="o">not</span> <span class="nx">o</span><span class="p">.</span><span class="nx">top</span> <span class="o">or</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="nx">o</span><span class="p">)</span> <span class="k">else</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-42"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-42">#</a> </div> <p>We compile a value to JavaScript by compiling and joining each property.
|
|
Things get much more insteresting if the chain of properties has <em>soak</em>
|
|
operators <code>?.</code> interspersed. Then we have to take care not to accidentally
|
|
evaluate a 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="nv">only = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'onlyFirst'</span>
|
|
<span class="nv">op = </span><span class="nx">@tags</span><span class="p">.</span><span class="nx">operation</span>
|
|
<span class="nv">props = </span><span class="k">if</span> <span class="nx">only</span> <span class="k">then</span> <span class="nx">@properties</span><span class="p">[</span><span class="mi">0</span><span class="p">...</span><span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">else</span> <span class="nx">@properties</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">chainRoot</span> <span class="o">or=</span> <span class="k">this</span>
|
|
<span class="k">for</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">props</span>
|
|
<span class="nv">hasSoak = </span><span class="kc">yes</span> <span class="k">if</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">soakNode</span>
|
|
<span class="k">if</span> <span class="nx">hasSoak</span> <span class="o">and</span> <span class="nx">@containsType</span> <span class="nx">CallNode</span>
|
|
<span class="p">[</span><span class="nx">me</span><span class="p">,</span> <span class="nx">copy</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@cacheIndexes</span> <span class="nx">o</span>
|
|
<span class="vi">@base.parenthetical = </span><span class="kc">yes</span> <span class="k">if</span> <span class="nx">@parenthetical</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">props</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="nv">baseline = </span><span class="nx">@base</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nv">baseline = </span><span class="s2">"(#{baseline})"</span> <span class="k">if</span> <span class="nx">@hasProperties</span><span class="p">()</span> <span class="o">and</span> <span class="p">(</span><span class="nx">@base</span> <span class="k">instanceof</span> <span class="nx">ObjectNode</span> <span class="o">or</span> <span class="nx">@isNumber</span><span class="p">())</span>
|
|
<span class="nv">complete = </span><span class="vi">@last = </span><span class="nx">baseline</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="vi">@source = </span><span class="nx">baseline</span>
|
|
<span class="k">if</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">soakNode</span>
|
|
<span class="k">if</span> <span class="nx">@base</span><span class="p">.</span><span class="nx">containsType</span><span class="p">(</span><span class="nx">CallNode</span><span class="p">)</span> <span class="o">and</span> <span class="nx">i</span> <span class="o">is</span> <span class="mi">0</span>
|
|
<span class="nv">temp = </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">complete = </span><span class="s2">"(#{ baseline = temp } = (#{complete}))"</span>
|
|
<span class="nv">complete = </span><span class="k">if</span> <span class="nx">i</span> <span class="o">is</span> <span class="mi">0</span>
|
|
<span class="s2">"(typeof #{complete} === \"undefined\" || #{baseline} === null) ? undefined : "</span>
|
|
<span class="k">else</span>
|
|
<span class="s2">"#{complete} == null ? undefined : "</span>
|
|
<span class="nx">complete</span> <span class="o">+=</span> <span class="p">(</span><span class="nx">baseline</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="k">else</span>
|
|
<span class="nv">part = </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="k">if</span> <span class="nx">hasSoak</span> <span class="o">and</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">containsType</span> <span class="nx">CallNode</span>
|
|
<span class="nx">baseline</span> <span class="o">+=</span> <span class="nx">copy</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="nx">compile</span> <span class="nx">o</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">baseline</span> <span class="o">+=</span> <span class="nx">part</span>
|
|
<span class="nx">complete</span> <span class="o">+=</span> <span class="nx">part</span>
|
|
<span class="vi">@last = </span><span class="nx">part</span>
|
|
|
|
<span class="k">if</span> <span class="nx">op</span> <span class="o">and</span> <span class="nx">@wrapped</span> <span class="k">then</span> <span class="s2">"(#{complete})"</span> <span class="k">else</span> <span class="nx">complete</span></pre></div> </td> </tr> <tr id="section-43"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-43">#</a> </div> <h3>CommentNode</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-44"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-44">#</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.CommentNode = </span><span class="nx">class</span> <span class="nx">CommentNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'CommentNode'</span>
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="o">-></span> <span class="kc">yes</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="k">super</span><span class="p">()</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></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="nx">@tab</span> <span class="o">+</span> <span class="s1">'/*'</span> <span class="o">+</span> <span class="nx">@comment</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/\r?\n/g</span><span class="p">,</span> <span class="s1">'\n'</span> <span class="o">+</span> <span class="nx">@tab</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'*/'</span></pre></div> </td> </tr> <tr id="section-45"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-45">#</a> </div> <h3>CallNode</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-46"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-46">#</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.CallNode = </span><span class="nx">class</span> <span class="nx">CallNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'CallNode'</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>
|
|
|
|
<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">@args</span><span class="p">,</span> <span class="nx">@exist</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">super</span><span class="p">()</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">@args</span> <span class="o">or=</span> <span class="p">[]</span>
|
|
<span class="vi">@first = @last = </span><span class="s1">''</span>
|
|
<span class="vi">@compileSplatArguments = </span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">SplatNode</span><span class="p">.</span><span class="nx">compileSplattedArray</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">@args</span><span class="p">,</span> <span class="nx">o</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-47"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-47">#</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="vi">@isNew = </span><span class="kc">true</span>
|
|
<span class="k">this</span>
|
|
|
|
<span class="nx">prefix</span><span class="o">:</span> <span class="o">-></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></pre></div> </td> </tr> <tr id="section-48"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-48">#</a> </div> <p>Grab the reference to the superclass' 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="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s2">"cannot call super outside of a function"</span> <span class="nx">unless</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">method</span>
|
|
<span class="nv">methname = </span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">method</span><span class="p">.</span><span class="nx">name</span>
|
|
<span class="nv">meth = </span><span class="k">if</span> <span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">method</span><span class="p">.</span><span class="nx">proto</span>
|
|
<span class="s2">"#{o.scope.method.proto}.__super__.#{methname}"</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">methname</span>
|
|
<span class="s2">"#{methname}.__super__.constructor"</span>
|
|
<span class="k">else</span> <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s2">"cannot call super on an anonymous function."</span></pre></div> </td> </tr> <tr id="section-49"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-49">#</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="nv">o.chainRoot = </span><span class="k">this</span> <span class="nx">unless</span> <span class="nx">o</span><span class="p">.</span><span class="nx">chainRoot</span>
|
|
<span class="nv">op = </span><span class="nx">@tags</span><span class="p">.</span><span class="nx">operation</span>
|
|
<span class="k">if</span> <span class="nx">@exist</span>
|
|
<span class="k">if</span> <span class="nx">@variable</span> <span class="k">instanceof</span> <span class="nx">ValueNode</span> <span class="o">and</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="nx">@variable</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nx">AccessorNode</span>
|
|
<span class="nv">methodAccessor = </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="p">[</span><span class="nx">first</span><span class="p">,</span> <span class="nx">meth</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">compileReference</span> <span class="nx">o</span>
|
|
<span class="vi">@first = </span><span class="k">new</span> <span class="nx">ValueNode</span><span class="p">(</span><span class="nx">first</span><span class="p">,</span> <span class="p">[</span><span class="nx">methodAccessor</span><span class="p">]).</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="vi">@meth = </span><span class="k">new</span> <span class="nx">ValueNode</span><span class="p">(</span><span class="nx">meth</span><span class="p">,</span> <span class="p">[</span><span class="nx">methodAccessor</span><span class="p">]).</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="k">else</span>
|
|
<span class="p">[</span><span class="nx">@first</span><span class="p">,</span> <span class="nx">@meth</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">compileReference</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">precompile</span><span class="o">:</span> <span class="kc">yes</span>
|
|
<span class="vi">@first = </span><span class="s2">"(typeof #{@first} === \"function\" ? "</span>
|
|
<span class="vi">@last = </span><span class="s2">" : undefined)"</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">@variable</span>
|
|
<span class="vi">@meth = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="k">for</span> <span class="nx">arg</span> <span class="k">in</span> <span class="nx">@args</span> <span class="k">when</span> <span class="nx">arg</span> <span class="k">instanceof</span> <span class="nx">SplatNode</span>
|
|
<span class="nv">code = </span><span class="nx">@compileSplat</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="o">not</span> <span class="nx">code</span>
|
|
<span class="nv">args = </span><span class="k">for</span> <span class="nx">arg</span> <span class="k">in</span> <span class="nx">@args</span>
|
|
<span class="nv">arg.parenthetical = </span><span class="kc">true</span>
|
|
<span class="nx">arg</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nv">code = </span><span class="k">if</span> <span class="nx">@isSuper</span>
|
|
<span class="nx">@compileSuper</span><span class="p">(</span><span class="nx">args</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">', '</span><span class="p">),</span> <span class="nx">o</span><span class="p">)</span>
|
|
<span class="k">else</span>
|
|
<span class="s2">"#{@first}#{@prefix()}#{@meth}(#{ args.join(', ') })#{@last}"</span>
|
|
<span class="k">if</span> <span class="nx">op</span> <span class="o">and</span> <span class="nx">@variable</span> <span class="o">and</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">wrapped</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-50"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-50">#</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-51"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-51">#</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="o">-></span>
|
|
<span class="nv">meth = </span><span class="nx">@meth</span> <span class="o">or</span> <span class="nx">@superReference</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
<span class="nv">obj = </span><span class="nx">@variable</span> <span class="o">and</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">source</span> <span class="o">or</span> <span class="s1">'this'</span>
|
|
<span class="k">if</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/\(/</span><span class="p">)</span>
|
|
<span class="nv">temp = </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">obj = </span><span class="nx">temp</span>
|
|
<span class="nv">meth = </span><span class="s2">"(#{temp} = #{ @variable.source })#{ @variable.last }"</span>
|
|
<span class="k">if</span> <span class="nx">@isNew</span>
|
|
<span class="nv">mentionsArgs = </span><span class="kc">no</span>
|
|
<span class="k">for</span> <span class="nx">arg</span> <span class="k">in</span> <span class="nx">@args</span>
|
|
<span class="nx">arg</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">mentionsArgs</span> <span class="o">or=</span> <span class="nx">n</span> <span class="k">instanceof</span> <span class="nx">LiteralNode</span> <span class="o">and</span> <span class="p">(</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="p">)</span>
|
|
<span class="nx">utility</span> <span class="s1">'extends'</span>
|
|
<span class="nv">a = </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">'ctor'</span>
|
|
<span class="nv">b = </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">c = </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">'result'</span>
|
|
<span class="s2">"""</span>
|
|
|
|
<span class="s2">#DIVIDER</span>
|
|
<span class="s2"> """</span>
|
|
<span class="k">else</span>
|
|
<span class="s2">"#{@first}#{@prefix()}#{meth}.apply(#{obj}, #{ @compileSplatArguments(o) })#{@last}"</span></pre></div> </td> </tr> <tr id="section-52"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-52">#</a> </div> <p>{@first}(function() {
|
|
{@idt(1)}var ctor = function(){};
|
|
{@idt(1)}__extends(ctor, #{a} = #{meth});
|
|
{@idt(1)}return typeof (#{c} = #{a}.apply(#{b} = new ctor, #{ @compileSplatArguments(o) })) === "object" ? #{c} : #{b};
|
|
{@tab}}).#{ if mentionsArgs then 'apply(this, arguments)' else 'call(this)'}#{@last}</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-53"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-53">#</a> </div> <h3>ExtendsNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.ExtendsNode = </span><span class="nx">class</span> <span class="nx">ExtendsNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'ExtendsNode'</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>
|
|
|
|
<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="k">super</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-54"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-54">#</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="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">ref = </span> <span class="k">new</span> <span class="nx">ValueNode</span> <span class="nx">literal</span> <span class="nx">utility</span> <span class="s1">'extends'</span>
|
|
<span class="p">(</span><span class="k">new</span> <span class="nx">CallNode</span> <span class="nx">ref</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-55"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-55">#</a> </div> <p>Hooks one constructor into another's prototype chain.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-56"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-56">#</a> </div> <h3>AccessorNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.AccessorNode = </span><span class="nx">class</span> <span class="nx">AccessorNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'AccessorNode'</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">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="k">super</span><span class="p">()</span>
|
|
<span class="vi">@prototype = </span><span class="k">if</span> <span class="nx">tag</span> <span class="o">is</span> <span class="s1">'prototype'</span> <span class="k">then</span> <span class="s1">'.prototype'</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="vi">@soakNode = </span><span class="nx">tag</span> <span class="o">is</span> <span class="s1">'soak'</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">name = </span><span class="nx">@name</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">chainRoot</span><span class="p">.</span><span class="nx">wrapped</span> <span class="o">or=</span> <span class="nx">@soakNode</span>
|
|
<span class="nv">namePart = </span><span class="k">if</span> <span class="nx">name</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">IS_STRING</span><span class="p">)</span> <span class="k">then</span> <span class="s2">"[#{name}]"</span> <span class="k">else</span> <span class="s2">".#{name}"</span>
|
|
<span class="nx">@prototype</span> <span class="o">+</span> <span class="nx">namePart</span></pre></div> </td> </tr> <tr id="section-57"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-57">#</a> </div> <p>A <code>.</code> accessor into a property of a value, or the <code>::</code> shorthand for
|
|
an accessor into the object's prototype.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-58"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-58">#</a> </div> <h3>IndexNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.IndexNode = </span><span class="nx">class</span> <span class="nx">IndexNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'IndexNode'</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">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="k">super</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="nx">o</span><span class="p">.</span><span class="nx">chainRoot</span><span class="p">.</span><span class="nx">wrapped</span> <span class="o">or=</span> <span class="nx">@soakNode</span>
|
|
<span class="nv">idx = </span><span class="nx">@index</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nv">prefix = </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="s2">"#{prefix}[#{idx}]"</span></pre></div> </td> </tr> <tr id="section-59"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-59">#</a> </div> <p>A <code>[ ... ]</code> indexed accessor into an array or object.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-60"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-60">#</a> </div> <h3>RangeNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.RangeNode = </span><span class="nx">class</span> <span class="nx">RangeNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'RangeNode'</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="k">super</span><span class="p">()</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-61"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-61">#</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="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="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">top</span><span class="o">:</span> <span class="kc">true</span><span class="p">)</span>
|
|
<span class="p">[</span><span class="nx">@from</span><span class="p">,</span> <span class="nx">@fromVar</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@from</span><span class="p">.</span><span class="nx">compileReference</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">precompile</span><span class="o">:</span> <span class="kc">yes</span>
|
|
<span class="p">[</span><span class="nx">@to</span><span class="p">,</span> <span class="nx">@toVar</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@to</span><span class="p">.</span><span class="nx">compileReference</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">precompile</span><span class="o">:</span> <span class="kc">yes</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="nv">parts = </span><span class="p">[]</span>
|
|
<span class="nx">parts</span><span class="p">.</span><span class="nx">push</span> <span class="nx">@from</span> <span class="k">if</span> <span class="nx">@from</span> <span class="o">isnt</span> <span class="nx">@fromVar</span>
|
|
<span class="nx">parts</span><span class="p">.</span><span class="nx">push</span> <span class="nx">@to</span> <span class="k">if</span> <span class="nx">@to</span> <span class="o">isnt</span> <span class="nx">@toVar</span>
|
|
<span class="k">if</span> <span class="nx">parts</span><span class="p">.</span><span class="nx">length</span> <span class="k">then</span> <span class="s2">"#{parts.join('; ')}; "</span> <span class="k">else</span> <span class="s1">''</span></pre></div> </td> </tr> <tr id="section-62"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-62">#</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">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">@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>
|
|
<span class="k">return</span> <span class="nx">@compileSimple</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@fromNum</span> <span class="o">and</span> <span class="nx">@toNum</span>
|
|
<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">step = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'step'</span>
|
|
<span class="nv">vars = </span><span class="s2">"#{idx} = #{@fromVar}"</span>
|
|
<span class="nv">intro = </span><span class="s2">"(#{@fromVar} <= #{@toVar} ? #{idx}"</span>
|
|
<span class="nv">compare = </span><span class="s2">"#{intro} <#{@equals} #{@toVar} : #{idx} >#{@equals} #{@toVar})"</span>
|
|
<span class="nv">stepPart = </span><span class="k">if</span> <span class="nx">step</span> <span class="k">then</span> <span class="nx">step</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">else</span> <span class="s1">'1'</span>
|
|
<span class="nv">incr = </span><span class="k">if</span> <span class="nx">step</span> <span class="k">then</span> <span class="s2">"#{idx} += #{stepPart}"</span> <span class="k">else</span> <span class="s2">"#{intro} += #{stepPart} : #{idx} -= #{stepPart})"</span>
|
|
<span class="s2">"#{vars}; #{compare}; #{incr}"</span></pre></div> </td> </tr> <tr id="section-63"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-63">#</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">compileSimple</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">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="nb">parseInt</span><span class="p">(</span><span class="nx">@fromNum</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="nb">parseInt</span><span class="p">(</span><span class="nx">@toNum</span><span class="p">,</span> <span class="mi">10</span><span class="p">)]</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">step = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'step'</span>
|
|
<span class="nx">step</span> <span class="o">and=</span> <span class="s2">"#{idx} += #{step.compile(o)}"</span>
|
|
<span class="k">if</span> <span class="nx">from</span> <span class="o"><=</span> <span class="nx">to</span>
|
|
<span class="s2">"#{idx} = #{from}; #{idx} <#{@equals} #{to}; #{step or "</span><span class="c1">#{idx}++"}"</span>
|
|
<span class="k">else</span>
|
|
<span class="s2">"#{idx} = #{from}; #{idx} >#{@equals} #{to}; #{step or "</span><span class="c1">#{idx}--"}"</span></pre></div> </td> </tr> <tr id="section-64"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-64">#</a> </div> <p>Compile a simple range comprehension, with integers.</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="nv">idt = </span><span class="nx">@idt</span> <span class="mi">1</span>
|
|
<span class="nv">vars = </span><span class="nx">@compileVariables</span> <span class="nx">merge</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">indent</span><span class="o">:</span> <span class="nx">idt</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="o">+</span><span class="nx">@fromNum</span> <span class="o">-</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">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">'result'</span>
|
|
<span class="nv">pre = </span><span class="s2">"\n#{idt}#{result} = []; #{vars}"</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">@compileSimple</span> <span class="nx">o</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">clause = </span><span class="s2">"#{@fromVar} <= #{@toVar} ?"</span>
|
|
<span class="nv">body = </span><span class="s2">"var #{i} = #{@fromVar}; #{clause} #{i} <#{@equals} #{@toVar} : #{i} >#{@equals} #{@toVar}; #{clause} #{i} += 1 : #{i} -= 1"</span>
|
|
<span class="nv">post = </span><span class="s2">"{ #{result}.push(#{i}); }\n#{idt}return #{result};\n#{o.indent}"</span>
|
|
<span class="s2">"(function() {#{pre}\n#{idt}for (#{body})#{post}}).call(this)"</span></pre></div> </td> </tr> <tr id="section-65"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-65">#</a> </div> <p>When used as a value, expand the range into the equivalent array.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-66"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-66">#</a> </div> <h3>SliceNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.SliceNode = </span><span class="nx">class</span> <span class="nx">SliceNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'SliceNode'</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>
|
|
|
|
<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">from = </span> <span class="k">if</span> <span class="nx">@range</span><span class="p">.</span><span class="nx">from</span> <span class="k">then</span> <span class="nx">@range</span><span class="p">.</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="k">else</span> <span class="s1">'0'</span>
|
|
<span class="nv">to = </span> <span class="k">if</span> <span class="nx">@range</span><span class="p">.</span><span class="nx">to</span> <span class="k">then</span> <span class="nx">@range</span><span class="p">.</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="k">else</span> <span class="s1">''</span>
|
|
<span class="nx">to</span> <span class="o">+=</span> <span class="k">if</span> <span class="o">not</span> <span class="nx">to</span> <span class="o">or</span> <span class="nx">@range</span><span class="p">.</span><span class="nx">exclusive</span> <span class="k">then</span> <span class="s1">''</span> <span class="k">else</span> <span class="s1">' + 1'</span>
|
|
<span class="nv">to = </span> <span class="s1">', '</span> <span class="o">+</span> <span class="nx">to</span> <span class="k">if</span> <span class="nx">to</span>
|
|
<span class="s2">".slice(#{from}#{to})"</span></pre></div> </td> </tr> <tr id="section-67"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-67">#</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></pre></div> </td> </tr> <tr id="section-68"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-68">#</a> </div> <h3>ObjectNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.ObjectNode = </span><span class="nx">class</span> <span class="nx">ObjectNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'ObjectNode'</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">topSensitive</span><span class="o">:</span> <span class="o">-></span> <span class="kc">true</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="o">-></span>
|
|
<span class="k">super</span><span class="p">()</span>
|
|
<span class="vi">@objects = @properties = </span><span class="nx">props</span> <span class="o">or</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">top = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'top'</span>
|
|
<span class="nv">o.indent = </span><span class="nx">@idt</span> <span class="mi">1</span>
|
|
<span class="nv">nonComments = </span><span class="nx">prop</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="p">(</span><span class="nx">prop</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">CommentNode</span><span class="p">)</span>
|
|
<span class="nv">lastNoncom = </span> <span class="nx">nonComments</span><span class="p">[</span><span class="nx">nonComments</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</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">@properties</span>
|
|
<span class="nv">join = </span><span class="s2">",\n"</span>
|
|
<span class="nv">join = </span><span class="s2">"\n"</span> <span class="k">if</span> <span class="p">(</span><span class="nx">prop</span> <span class="o">is</span> <span class="nx">lastNoncom</span><span class="p">)</span> <span class="o">or</span> <span class="p">(</span><span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">CommentNode</span><span class="p">)</span>
|
|
<span class="nv">join = </span><span class="s1">''</span> <span class="k">if</span> <span class="nx">i</span> <span class="o">is</span> <span class="nx">@properties</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span>
|
|
<span class="nv">indent = </span><span class="k">if</span> <span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">CommentNode</span> <span class="k">then</span> <span class="s1">''</span> <span class="k">else</span> <span class="nx">@idt</span> <span class="mi">1</span>
|
|
<span class="nv">prop = </span><span class="k">new</span> <span class="nx">AssignNode</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="nx">unless</span> <span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">AssignNode</span> <span class="o">or</span> <span class="nx">prop</span> <span class="k">instanceof</span> <span class="nx">CommentNode</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="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="p">(</span><span class="s1">''</span><span class="p">)</span>
|
|
<span class="nv">obj = </span><span class="s1">'{'</span> <span class="o">+</span> <span class="p">(</span><span class="k">if</span> <span class="nx">props</span> <span class="k">then</span> <span class="s1">'\n'</span> <span class="o">+</span> <span class="nx">props</span> <span class="o">+</span> <span class="s1">'\n'</span> <span class="o">+</span> <span class="nx">@idt</span><span class="p">()</span> <span class="k">else</span> <span class="s1">''</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'}'</span>
|
|
<span class="k">if</span> <span class="nx">top</span> <span class="k">then</span> <span class="s2">"(#{obj})"</span> <span class="k">else</span> <span class="nx">obj</span></pre></div> </td> </tr> <tr id="section-69"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-69">#</a> </div> <p>An object literal, nothing fancy.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-70"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-70">#</a> </div> <h3>ArrayNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.ArrayNode = </span><span class="nx">class</span> <span class="nx">ArrayNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'ArrayNode'</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">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@objects</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">super</span><span class="p">()</span>
|
|
<span class="nx">@objects</span> <span class="o">or=</span> <span class="p">[]</span>
|
|
<span class="vi">@compileSplatLiteral = </span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">SplatNode</span><span class="p">.</span><span class="nx">compileSplattedArray</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">@objects</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="nv">o.indent = </span><span class="nx">@idt</span> <span class="mi">1</span>
|
|
<span class="nv">objects = </span><span class="p">[]</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>
|
|
<span class="nv">code = </span><span class="nx">obj</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">SplatNode</span>
|
|
<span class="k">return</span> <span class="nx">@compileSplatLiteral</span> <span class="nx">o</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">CommentNode</span>
|
|
<span class="nx">objects</span><span class="p">.</span><span class="nx">push</span> <span class="s2">"\n#{code}\n#{o.indent}"</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">i</span> <span class="o">is</span> <span class="nx">@objects</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span>
|
|
<span class="nx">objects</span><span class="p">.</span><span class="nx">push</span> <span class="nx">code</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">objects</span><span class="p">.</span><span class="nx">push</span> <span class="s2">"#{code}, "</span>
|
|
<span class="nv">objects = </span><span class="nx">objects</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="nx">indexOf</span><span class="p">(</span><span class="nx">objects</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#{@idt(1)}#{objects}\n#{@tab}]"</span>
|
|
<span class="k">else</span>
|
|
<span class="s2">"[#{objects}]"</span></pre></div> </td> </tr> <tr id="section-71"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-71">#</a> </div> <p>An array literal.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-72"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-72">#</a> </div> <h3>ClassNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.ClassNode = </span><span class="nx">class</span> <span class="nx">ClassNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'ClassNode'</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">'properties'</span><span class="p">]</span>
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="o">-></span> <span class="kc">yes</span></pre></div> </td> </tr> <tr id="section-73"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-73">#</a> </div> <p>The CoffeeScript class definition.</p> </td> <td class="code"> <div class="highlight"><pre> <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="nx">@properties</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">super</span><span class="p">()</span>
|
|
<span class="nx">@properties</span> <span class="o">or=</span> <span class="p">[]</span>
|
|
<span class="vi">@returns = </span><span class="kc">false</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="vi">@returns = </span><span class="kc">true</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-74"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-74">#</a> </div> <p>Initialize a <strong>ClassNode</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="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">@variable = </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">'ctor'</span> <span class="k">if</span> <span class="nx">@variable</span> <span class="o">is</span> <span class="s1">'__temp__'</span>
|
|
<span class="nv">extension = </span><span class="nx">@parent</span> <span class="o">and</span> <span class="k">new</span> <span class="nx">ExtendsNode</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="nv">props = </span><span class="k">new</span> <span class="nx">Expressions</span>
|
|
<span class="nv">o.top = </span><span class="kc">true</span>
|
|
<span class="nv">me = </span><span class="kc">null</span>
|
|
<span class="nv">className = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nv">constScope = </span><span class="kc">null</span>
|
|
|
|
<span class="k">if</span> <span class="nx">@parent</span>
|
|
<span class="nv">applied = </span><span class="k">new</span> <span class="nx">ValueNode</span><span class="p">(</span><span class="nx">@parent</span><span class="p">,</span> <span class="p">[</span><span class="k">new</span> <span class="nx">AccessorNode</span><span class="p">(</span><span class="nx">literal</span><span class="p">(</span><span class="s1">'apply'</span><span class="p">))])</span>
|
|
<span class="nv">constructor = </span><span class="k">new</span> <span class="nx">CodeNode</span><span class="p">([],</span> <span class="k">new</span> <span class="nx">Expressions</span><span class="p">([</span>
|
|
<span class="k">new</span> <span class="nx">CallNode</span><span class="p">(</span><span class="nx">applied</span><span class="p">,</span> <span class="p">[</span><span class="nx">literal</span><span class="p">(</span><span class="s1">'this'</span><span class="p">),</span> <span class="nx">literal</span><span class="p">(</span><span class="s1">'arguments'</span><span class="p">)])</span>
|
|
<span class="p">]))</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">constructor = </span><span class="k">new</span> <span class="nx">CodeNode</span>
|
|
|
|
<span class="k">for</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">@properties</span>
|
|
<span class="p">[</span><span class="nx">pvar</span><span class="p">,</span> <span class="nx">func</span><span class="p">]</span> <span class="o">=</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">prop</span><span class="p">.</span><span class="nx">value</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="nx">pvar</span> <span class="o">and</span> <span class="nx">pvar</span><span class="p">.</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="o">and</span> <span class="nx">func</span> <span class="k">instanceof</span> <span class="nx">CodeNode</span>
|
|
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s2">"cannot define a constructor as a bound function."</span> <span class="k">if</span> <span class="nx">func</span><span class="p">.</span><span class="nx">bound</span>
|
|
<span class="nv">func.name = </span><span class="nx">className</span>
|
|
<span class="nx">func</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">ReturnNode</span> <span class="nx">literal</span> <span class="s1">'this'</span>
|
|
<span class="vi">@variable = </span><span class="k">new</span> <span class="nx">ValueNode</span> <span class="nx">@variable</span>
|
|
<span class="vi">@variable.namespaced = </span><span class="nx">include</span> <span class="nx">func</span><span class="p">.</span><span class="nx">name</span><span class="p">,</span> <span class="s1">'.'</span>
|
|
<span class="nv">constructor = </span><span class="nx">func</span>
|
|
<span class="k">continue</span>
|
|
<span class="k">if</span> <span class="nx">func</span> <span class="k">instanceof</span> <span class="nx">CodeNode</span> <span class="o">and</span> <span class="nx">func</span><span class="p">.</span><span class="nx">bound</span>
|
|
<span class="k">if</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">context</span> <span class="o">is</span> <span class="s1">'this'</span>
|
|
<span class="nv">func.context = </span><span class="nx">className</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">func.bound = </span><span class="kc">false</span>
|
|
<span class="nx">constScope</span> <span class="o">or=</span> <span class="k">new</span> <span class="nx">Scope</span><span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">,</span> <span class="nx">constructor</span><span class="p">.</span><span class="nx">body</span><span class="p">,</span> <span class="nx">constructor</span><span class="p">)</span>
|
|
<span class="nx">me</span> <span class="o">or=</span> <span class="nx">constScope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'this'</span>
|
|
<span class="nv">pname = </span><span class="nx">pvar</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">constructor</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">ReturnNode</span> <span class="nx">literal</span> <span class="s1">'this'</span> <span class="k">if</span> <span class="nx">constructor</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">empty</span><span class="p">()</span>
|
|
<span class="nx">constructor</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">literal</span> <span class="s2">"this.#{pname} = function(){ return #{className}.prototype.#{pname}.apply(#{me}, arguments); }"</span>
|
|
<span class="k">if</span> <span class="nx">pvar</span>
|
|
<span class="nv">access = </span><span class="k">if</span> <span class="nx">prop</span><span class="p">.</span><span class="nx">context</span> <span class="o">is</span> <span class="s1">'this'</span> <span class="k">then</span> <span class="nx">pvar</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="k">else</span> <span class="k">new</span> <span class="nx">AccessorNode</span><span class="p">(</span><span class="nx">pvar</span><span class="p">,</span> <span class="s1">'prototype'</span><span class="p">)</span>
|
|
<span class="nv">val = </span><span class="k">new</span> <span class="nx">ValueNode</span><span class="p">(</span><span class="nx">@variable</span><span class="p">,</span> <span class="p">[</span><span class="nx">access</span><span class="p">])</span>
|
|
<span class="nv">prop = </span><span class="k">new</span> <span class="nx">AssignNode</span><span class="p">(</span><span class="nx">val</span><span class="p">,</span> <span class="nx">func</span><span class="p">)</span>
|
|
<span class="nx">props</span><span class="p">.</span><span class="nx">push</span> <span class="nx">prop</span>
|
|
|
|
<span class="nx">constructor</span><span class="p">.</span><span class="nx">body</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">literal</span> <span class="s2">"#{me} = this"</span> <span class="k">if</span> <span class="nx">me</span>
|
|
<span class="nv">construct = </span><span class="nx">@idt</span><span class="p">()</span> <span class="o">+</span> <span class="p">(</span><span class="k">new</span> <span class="nx">AssignNode</span><span class="p">(</span><span class="nx">@variable</span><span class="p">,</span> <span class="nx">constructor</span><span class="p">)).</span><span class="nx">compile</span><span class="p">(</span><span class="nx">merge</span> <span class="nx">o</span><span class="p">,</span> <span class="p">{</span><span class="nx">sharedScope</span><span class="o">:</span> <span class="nx">constScope</span><span class="p">})</span> <span class="o">+</span> <span class="s1">';'</span>
|
|
<span class="nv">props = </span><span class="k">if</span> <span class="o">!</span><span class="nx">props</span><span class="p">.</span><span class="nx">empty</span><span class="p">()</span> <span class="k">then</span> <span class="s1">'\n'</span> <span class="o">+</span> <span class="nx">props</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="nv">extension = </span><span class="k">if</span> <span class="nx">extension</span> <span class="k">then</span> <span class="s1">'\n'</span> <span class="o">+</span> <span class="nx">@idt</span><span class="p">()</span> <span class="o">+</span> <span class="nx">extension</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="k">else</span> <span class="s1">''</span>
|
|
<span class="nv">returns = </span><span class="k">if</span> <span class="nx">@returns</span> <span class="k">then</span> <span class="s1">'\n'</span> <span class="o">+</span> <span class="k">new</span> <span class="nx">ReturnNode</span><span class="p">(</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="k">else</span> <span class="s1">''</span>
|
|
<span class="nx">construct</span> <span class="o">+</span> <span class="nx">extension</span> <span class="o">+</span> <span class="nx">props</span> <span class="o">+</span> <span class="nx">returns</span></pre></div> </td> </tr> <tr id="section-75"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-75">#</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></pre></div> </td> </tr> <tr id="section-76"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-76">#</a> </div> <h3>AssignNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.AssignNode = </span><span class="nx">class</span> <span class="nx">AssignNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span></pre></div> </td> </tr> <tr id="section-77"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-77">#</a> </div> <p>The <strong>AssignNode</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="nx">PROTO_ASSIGN</span><span class="o">:</span> <span class="sr">/^(\S+)\.prototype/</span>
|
|
<span class="nx">LEADING_DOT</span><span class="o">:</span> <span class="sr">/^\.(prototype\.)?/</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'AssignNode'</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">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="o">-></span>
|
|
<span class="k">super</span><span class="p">()</span>
|
|
|
|
<span class="nx">topSensitive</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="kc">true</span>
|
|
|
|
<span class="nx">isValue</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@variable</span> <span class="k">instanceof</span> <span class="nx">ValueNode</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">return</span> <span class="k">new</span> <span class="nx">Expressions</span> <span class="p">[</span><span class="k">this</span><span class="p">,</span> <span class="k">new</span> <span class="nx">ReturnNode</span><span class="p">(</span><span class="nx">@variable</span><span class="p">)]</span>
|
|
<span class="k">else</span>
|
|
<span class="k">super</span><span class="p">()</span>
|
|
|
|
<span class="nx">isStatement</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@isValue</span><span class="p">()</span> <span class="o">and</span> <span class="p">(</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></pre></div> </td> </tr> <tr id="section-78"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-78">#</a> </div> <p>Matchers for detecting prototype assignments.</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">top = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'top'</span>
|
|
<span class="k">return</span> <span class="nx">@compilePatternMatch</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@isStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="nx">@compileSplice</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@isValue</span><span class="p">()</span> <span class="o">and</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">isSplice</span><span class="p">()</span>
|
|
<span class="nv">stmt = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'asStatement'</span>
|
|
<span class="nv">name = </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="nv">last = </span><span class="k">if</span> <span class="nx">@isValue</span><span class="p">()</span> <span class="k">then</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">last</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">@LEADING_DOT</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span> <span class="k">else</span> <span class="nx">name</span>
|
|
<span class="nv">match = </span><span class="nx">name</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">@PROTO_ASSIGN</span><span class="p">)</span>
|
|
<span class="nv">proto = </span><span class="nx">match</span> <span class="o">and</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">@value</span> <span class="k">instanceof</span> <span class="nx">CodeNode</span>
|
|
<span class="vi">@value.name = </span> <span class="nx">last</span> <span class="k">if</span> <span class="nx">last</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">IDENTIFIER</span><span class="p">)</span>
|
|
<span class="vi">@value.proto = </span><span class="nx">proto</span> <span class="k">if</span> <span class="nx">proto</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="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="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="nx">unless</span> <span class="nx">@isValue</span><span class="p">()</span> <span class="o">and</span> <span class="p">(</span><span class="nx">@variable</span><span class="p">.</span><span class="nx">hasProperties</span><span class="p">()</span> <span class="o">or</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">namespaced</span><span class="p">)</span>
|
|
<span class="nv">val = </span><span class="s2">"#{name} = #{val}"</span>
|
|
<span class="k">return</span> <span class="s2">"#{@tab}#{val};"</span> <span class="k">if</span> <span class="nx">stmt</span>
|
|
<span class="k">if</span> <span class="nx">top</span> <span class="o">or</span> <span class="nx">@parenthetical</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-79"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-79">#</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">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">valVar = </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">value = </span><span class="k">if</span> <span class="nx">@value</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="k">then</span> <span class="nx">ClosureNode</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="nx">@value</span><span class="p">)</span> <span class="k">else</span> <span class="nx">@value</span>
|
|
<span class="nv">assigns = </span><span class="p">[</span><span class="s2">"#{@tab}#{valVar} = #{ value.compile(o) };"</span><span class="p">]</span>
|
|
<span class="nv">o.top = </span><span class="kc">true</span>
|
|
<span class="nv">o.asStatement = </span><span class="kc">true</span>
|
|
<span class="nv">splat = </span><span class="kc">false</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">@variable</span><span class="p">.</span><span class="nx">base</span><span class="p">.</span><span class="nx">objects</span></pre></div> </td> </tr> <tr id="section-80"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-80">#</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="nv">idx = </span><span class="nx">i</span>
|
|
<span class="k">if</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">obj</span> <span class="k">instanceof</span> <span class="nx">AssignNode</span></pre></div> </td> </tr> <tr id="section-81"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-81">#</a> </div> <p>A regular array pattern-match.</p> </td> <td class="code"> <div class="highlight"><pre> <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="p">[</span><span class="nx">obj</span><span class="p">.</span><span class="nx">value</span><span class="p">,</span> <span class="nx">obj</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">else</span></pre></div> </td> </tr> <tr id="section-82"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-82">#</a> </div> <p>A regular object pattern-match.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nv">idx = </span><span class="nx">obj</span>
|
|
<span class="k">if</span> <span class="o">not</span> <span class="p">(</span><span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">ValueNode</span> <span class="o">or</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">SplatNode</span><span class="p">)</span>
|
|
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span> <span class="s1">'pattern matching must use only identifiers on the left-hand side.'</span>
|
|
<span class="nv">isString = </span><span class="nx">idx</span><span class="p">.</span><span class="nx">value</span> <span class="o">and</span> <span class="nx">idx</span><span class="p">.</span><span class="nx">value</span><span class="p">.</span><span class="nx">match</span> <span class="nx">IS_STRING</span>
|
|
<span class="nv">accessClass = </span><span class="k">if</span> <span class="nx">isString</span> <span class="o">or</span> <span class="nx">@variable</span><span class="p">.</span><span class="nx">isArray</span><span class="p">()</span> <span class="k">then</span> <span class="nx">IndexNode</span> <span class="k">else</span> <span class="nx">AccessorNode</span>
|
|
<span class="k">if</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nx">SplatNode</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">splat</span>
|
|
<span class="nv">val = </span><span class="nx">literal</span> <span class="nx">obj</span><span class="p">.</span><span class="nx">compileValue</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">valVar</span><span class="p">,</span>
|
|
<span class="p">(</span><span class="nv">oindex = </span><span class="nx">indexOf</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="nx">objects</span><span class="p">,</span> <span class="nx">obj</span><span class="p">)),</span>
|
|
<span class="p">(</span><span class="nv">olength = </span><span class="nx">@variable</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="nx">length</span><span class="p">)</span> <span class="o">-</span> <span class="nx">oindex</span> <span class="o">-</span> <span class="mi">1</span>
|
|
<span class="nv">splat = </span><span class="kc">true</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">idx = </span><span class="nx">literal</span><span class="p">(</span><span class="k">if</span> <span class="nx">splat</span> <span class="k">then</span> <span class="s2">"#{valVar}.length - #{olength - idx}"</span> <span class="k">else</span> <span class="nx">idx</span><span class="p">)</span> <span class="k">if</span> <span class="k">typeof</span> <span class="nx">idx</span> <span class="o">isnt</span> <span class="s1">'object'</span>
|
|
<span class="nv">val = </span><span class="k">new</span> <span class="nx">ValueNode</span><span class="p">(</span><span class="nx">literal</span><span class="p">(</span><span class="nx">valVar</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">accessClass</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="p">(</span><span class="k">new</span> <span class="nx">AssignNode</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="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">))</span>
|
|
<span class="nv">code = </span><span class="nx">assigns</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s2">"\n"</span><span class="p">)</span>
|
|
<span class="nx">code</span></pre></div> </td> </tr> <tr id="section-83"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-83">#</a> </div> <p>A shorthand <code>{a, b, c} = val</code> pattern-match.</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="nv">name = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">merge</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">onlyFirst</span><span class="o">:</span> <span class="kc">true</span>
|
|
<span class="nv">l = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">properties</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="nv">range = </span><span class="nx">@variable</span><span class="p">.</span><span class="nx">properties</span><span class="p">[</span><span class="nx">l</span> <span class="o">-</span> <span class="mi">1</span><span class="p">].</span><span class="nx">range</span>
|
|
<span class="nv">plus = </span><span class="k">if</span> <span class="nx">range</span><span class="p">.</span><span class="nx">exclusive</span> <span class="k">then</span> <span class="s1">''</span> <span class="k">else</span> <span class="s1">' + 1'</span>
|
|
<span class="nv">from = </span><span class="k">if</span> <span class="nx">range</span><span class="p">.</span><span class="nx">from</span> <span class="k">then</span> <span class="nx">range</span><span class="p">.</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="k">else</span> <span class="s1">'0'</span>
|
|
<span class="nv">to = </span><span class="k">if</span> <span class="nx">range</span><span class="p">.</span><span class="nx">to</span> <span class="k">then</span> <span class="nx">range</span><span class="p">.</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">from</span> <span class="o">+</span> <span class="nx">plus</span> <span class="k">else</span> <span class="s2">"#{name}.length"</span>
|
|
<span class="nv">val = </span><span class="nx">@value</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="s2">"#{name}.splice.apply(#{name}, [#{from}, #{to}].concat(#{val}))"</span></pre></div> </td> </tr> <tr id="section-84"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-84">#</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></pre></div> </td> </tr> <tr id="section-85"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-85">#</a> </div> <h3>CodeNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.CodeNode = </span><span class="nx">class</span> <span class="nx">CodeNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'CodeNode'</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">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="k">super</span><span class="p">()</span>
|
|
<span class="nx">@params</span> <span class="o">or=</span> <span class="p">[]</span>
|
|
<span class="nx">@body</span> <span class="o">or=</span> <span class="k">new</span> <span class="nx">Expressions</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></pre></div> </td> </tr> <tr id="section-86"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-86">#</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 CodeNode
|
|
has no <em>children</em> -- they're within the inner scope.</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">sharedScope = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'sharedScope'</span>
|
|
<span class="nv">top = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'top'</span>
|
|
<span class="nv">o.scope = </span><span class="nx">sharedScope</span> <span class="o">or</span> <span class="k">new</span> <span class="nx">Scope</span><span class="p">(</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="p">)</span>
|
|
<span class="nv">o.top = </span><span class="kc">true</span>
|
|
<span class="nv">o.indent = </span><span class="nx">@idt</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
|
<span class="nv">empty = </span><span class="nx">@body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">0</span>
|
|
<span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'noWrap'</span>
|
|
<span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'globals'</span>
|
|
<span class="nv">splat = </span><span class="kc">undefined</span>
|
|
<span class="nv">params = </span><span class="p">[]</span>
|
|
<span class="k">for</span> <span class="nx">param</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">@params</span>
|
|
<span class="k">if</span> <span class="nx">splat</span>
|
|
<span class="k">if</span> <span class="nx">param</span><span class="p">.</span><span class="nx">attach</span>
|
|
<span class="nv">param.assign = </span><span class="k">new</span> <span class="nx">AssignNode</span> <span class="k">new</span> <span class="nx">ValueNode</span> <span class="nx">literal</span><span class="p">(</span><span class="s1">'this'</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">AccessorNode</span> <span class="nx">param</span><span class="p">.</span><span class="nx">value</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">splice</span> <span class="nx">splat</span><span class="p">.</span><span class="nx">index</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">param</span><span class="p">.</span><span class="nx">assign</span>
|
|
<span class="nx">splat</span><span class="p">.</span><span class="nx">trailings</span><span class="p">.</span><span class="nx">push</span> <span class="nx">param</span>
|
|
<span class="k">else</span>
|
|
<span class="k">if</span> <span class="nx">param</span><span class="p">.</span><span class="nx">attach</span>
|
|
<span class="p">{</span><span class="nx">value</span><span class="p">}</span> <span class="o">=</span> <span class="nx">param</span>
|
|
<span class="p">[</span><span class="nx">param</span><span class="p">,</span> <span class="nx">param</span><span class="p">.</span><span class="nx">splat</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="nx">literal</span><span class="p">(</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="p">),</span> <span class="nx">param</span><span class="p">.</span><span class="nx">splat</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">AssignNode</span> <span class="k">new</span> <span class="nx">ValueNode</span><span class="p">(</span><span class="nx">literal</span><span class="p">(</span><span class="s1">'this'</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">AccessorNode</span> <span class="nx">value</span><span class="p">]),</span> <span class="nx">param</span>
|
|
<span class="k">if</span> <span class="nx">param</span><span class="p">.</span><span class="nx">splat</span>
|
|
<span class="nv">splat = </span><span class="k">new</span> <span class="nx">SplatNode</span> <span class="nx">param</span><span class="p">.</span><span class="nx">value</span>
|
|
<span class="nv">splat.index = </span><span class="nx">i</span>
|
|
<span class="nv">splat.trailings = </span><span class="p">[]</span>
|
|
<span class="nv">splat.arglength = </span><span class="nx">@params</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="nx">@body</span><span class="p">.</span><span class="nx">unshift</span><span class="p">(</span><span class="nx">splat</span><span class="p">)</span>
|
|
<span class="k">else</span>
|
|
<span class="nx">params</span><span class="p">.</span><span class="nx">push</span> <span class="nx">param</span>
|
|
<span class="nv">params = </span><span class="p">(</span><span class="nx">param</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">for</span> <span class="nx">param</span> <span class="k">in</span> <span class="nx">params</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">unless</span> <span class="nx">empty</span>
|
|
<span class="p">(</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="p">(</span><span class="nx">param</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="nv">code = </span><span class="k">if</span> <span class="nx">@body</span><span class="p">.</span><span class="nx">expressions</span><span class="p">.</span><span class="nx">length</span> <span class="k">then</span> <span class="s2">"\n#{ @body.compileWithDeclarations(o) }\n"</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="nv">func = </span><span class="s2">"function(#{ params.join(', ') }) {#{code}#{ code and @tab }}"</span>
|
|
<span class="k">return</span> <span class="s2">"#{utility('bind')}(#{func}, #{@context})"</span> <span class="k">if</span> <span class="nx">@bound</span>
|
|
<span class="k">if</span> <span class="nx">top</span> <span class="k">then</span> <span class="s2">"(#{func})"</span> <span class="k">else</span> <span class="nx">func</span>
|
|
|
|
<span class="nx">topSensitive</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="kc">true</span></pre></div> </td> </tr> <tr id="section-87"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-87">#</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> objects. 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">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>
|
|
|
|
<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="nx">idt</span> <span class="o">or=</span> <span class="s1">''</span>
|
|
<span class="nv">children = </span><span class="p">(</span><span class="nx">child</span><span class="p">.</span><span class="nx">toString</span><span class="p">(</span><span class="nx">idt</span> <span class="o">+</span> <span class="nx">TAB</span><span class="p">)</span> <span class="k">for</span> <span class="nx">child</span> <span class="k">in</span> <span class="nx">@collectChildren</span><span class="p">()).</span><span class="nx">join</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span>
|
|
<span class="s1">'\n'</span> <span class="o">+</span> <span class="nx">idt</span> <span class="o">+</span> <span class="nx">children</span></pre></div> </td> </tr> <tr id="section-88"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-88">#</a> </div> <p>Short-circuit traverseChildren method to prevent it from crossing scope boundaries
|
|
unless crossScope is true</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-89"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-89">#</a> </div> <h3>ParamNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.ParamNode = </span><span class="nx">class</span> <span class="nx">ParamNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'ParamNode'</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">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@name</span><span class="p">,</span> <span class="nx">@attach</span><span class="p">,</span> <span class="nx">@splat</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">super</span><span class="p">()</span>
|
|
<span class="vi">@value = </span><span class="nx">literal</span> <span class="nx">@name</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">@value</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</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">if</span> <span class="nx">@attach</span> <span class="k">then</span> <span class="p">(</span><span class="nx">literal</span> <span class="s1">'@'</span> <span class="o">+</span> <span class="nx">@name</span><span class="p">).</span><span class="nx">toString</span> <span class="nx">idt</span> <span class="k">else</span> <span class="nx">@value</span><span class="p">.</span><span class="nx">toString</span> <span class="nx">idt</span></pre></div> </td> </tr> <tr id="section-90"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-90">#</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></pre></div> </td> </tr> <tr id="section-91"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-91">#</a> </div> <h3>SplatNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.SplatNode = </span><span class="nx">class</span> <span class="nx">SplatNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'SplatNode'</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">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="k">super</span><span class="p">()</span>
|
|
<span class="nv">name = </span><span class="nx">literal</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="nx">unless</span> <span class="nx">name</span><span class="p">.</span><span class="nx">compile</span>
|
|
<span class="vi">@name = </span><span class="nx">name</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">@index</span><span class="o">?</span> <span class="k">then</span> <span class="nx">@compileParam</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">else</span> <span class="nx">@name</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-92"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-92">#</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="nx">compileParam</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="p">(</span><span class="nx">o</span><span class="p">)</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="nv">end = </span><span class="s1">''</span>
|
|
<span class="k">if</span> <span class="nx">@trailings</span><span class="p">.</span><span class="nx">length</span>
|
|
<span class="nv">len = </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">'len'</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">assign</span> <span class="nx">len</span><span class="p">,</span> <span class="s2">"arguments.length"</span>
|
|
<span class="nv">variadic = </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">'result'</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">assign</span> <span class="nx">variadic</span><span class="p">,</span> <span class="nx">len</span> <span class="o">+</span> <span class="s1">' >= '</span> <span class="o">+</span> <span class="nx">@arglength</span>
|
|
<span class="nv">end = </span><span class="k">if</span> <span class="nx">@trailings</span><span class="p">.</span><span class="nx">length</span> <span class="k">then</span> <span class="s2">", #{len} - #{@trailings.length}"</span>
|
|
<span class="k">for</span> <span class="nx">trailing</span><span class="p">,</span> <span class="nx">idx</span> <span class="k">in</span> <span class="nx">@trailings</span>
|
|
<span class="k">if</span> <span class="nx">trailing</span><span class="p">.</span><span class="nx">attach</span>
|
|
<span class="nv">assign = </span><span class="nx">trailing</span><span class="p">.</span><span class="nx">assign</span>
|
|
<span class="nv">trailing = </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">assign.value = </span><span class="nx">trailing</span>
|
|
<span class="nv">pos = </span><span class="nx">@trailings</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="nx">idx</span>
|
|
<span class="nx">o</span><span class="p">.</span><span class="nx">scope</span><span class="p">.</span><span class="nx">assign</span><span class="p">(</span><span class="nx">trailing</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="s2">"arguments[#{variadic} ? #{len} - #{pos} : #{@index + idx}]"</span><span class="p">)</span>
|
|
<span class="s2">"#{name} = #{utility('slice')}.call(arguments, #{@index}#{end})"</span></pre></div> </td> </tr> <tr id="section-93"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-93">#</a> </div> <p>Compiling a parameter splat means recovering the parameters that succeed
|
|
the splat in the parameter list, by slicing the arguments object.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileValue</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="nx">index</span><span class="p">,</span> <span class="nx">trailings</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">trail = </span><span class="k">if</span> <span class="nx">trailings</span> <span class="k">then</span> <span class="s2">", #{name}.length - #{trailings}"</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="s2">"#{utility 'slice'}.call(#{name}, #{index}#{trail})"</span></pre></div> </td> </tr> <tr id="section-94"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-94">#</a> </div> <p>A compiling a splat as a destructuring assignment means slicing arguments
|
|
from the right-hand-side's corresponding 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">list</span><span class="p">,</span> <span class="nx">o</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">args = </span><span class="p">[]</span>
|
|
<span class="k">for</span> <span class="nx">arg</span><span class="p">,</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">list</span>
|
|
<span class="nv">code = </span><span class="nx">arg</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nv">prev = </span><span class="nx">args</span><span class="p">[</span><span class="nv">last = </span><span class="nx">args</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="nx">arg</span> <span class="o">not</span> <span class="k">instanceof</span> <span class="nx">SplatNode</span>
|
|
<span class="k">if</span> <span class="nx">prev</span> <span class="o">and</span> <span class="nx">starts</span><span class="p">(</span><span class="nx">prev</span><span class="p">,</span> <span class="s1">'['</span><span class="p">)</span> <span class="o">and</span> <span class="nx">ends</span><span class="p">(</span><span class="nx">prev</span><span class="p">,</span> <span class="s1">']'</span><span class="p">)</span>
|
|
<span class="nx">args</span><span class="p">[</span><span class="nx">last</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"#{prev.substr(0, prev.length - 1)}, #{code}]"</span>
|
|
<span class="k">continue</span>
|
|
<span class="k">else</span> <span class="k">if</span> <span class="nx">prev</span> <span class="o">and</span> <span class="nx">starts</span><span class="p">(</span><span class="nx">prev</span><span class="p">,</span> <span class="s1">'.concat(['</span><span class="p">)</span> <span class="o">and</span> <span class="nx">ends</span><span class="p">(</span><span class="nx">prev</span><span class="p">,</span> <span class="s1">'])'</span><span class="p">)</span>
|
|
<span class="nx">args</span><span class="p">[</span><span class="nx">last</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"#{prev.substr(0, prev.length - 2)}, #{code}])"</span>
|
|
<span class="k">continue</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">code = </span><span class="s2">"[#{code}]"</span>
|
|
<span class="nx">args</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="k">if</span> <span class="nx">i</span> <span class="o">is</span> <span class="mi">0</span> <span class="k">then</span> <span class="nx">code</span> <span class="k">else</span> <span class="s2">".concat(#{code})"</span><span class="p">)</span>
|
|
<span class="nx">args</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">''</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-95"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-95">#</a> </div> <p>Utility function that converts arbitrary number of elements, mixed with
|
|
splats, to a proper array</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-96"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-96">#</a> </div> <h3>WhileNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.WhileNode = </span><span class="nx">class</span> <span class="nx">WhileNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'WhileNode'</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="o">-></span> <span class="kc">yes</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">opts</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">super</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="nx">opts</span> <span class="o">and</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">invert</span>
|
|
<span class="nv">condition = </span><span class="k">new</span> <span class="nx">ParentheticalNode</span> <span class="nx">condition</span> <span class="k">if</span> <span class="nx">condition</span> <span class="k">instanceof</span> <span class="nx">OpNode</span>
|
|
<span class="nv">condition = </span><span class="k">new</span> <span class="nx">OpNode</span><span class="p">(</span><span class="s1">'!'</span><span class="p">,</span> <span class="nx">condition</span><span class="p">)</span>
|
|
<span class="vi">@condition = </span><span class="nx">condition</span>
|
|
<span class="vi">@guard = </span><span class="nx">opts</span> <span class="o">and</span> <span class="nx">opts</span><span class="p">.</span><span class="nx">guard</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="vi">@body = </span><span class="nx">body</span>
|
|
<span class="k">this</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="vi">@returns = </span><span class="kc">true</span>
|
|
<span class="k">this</span>
|
|
|
|
<span class="nx">topSensitive</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="kc">true</span></pre></div> </td> </tr> <tr id="section-97"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-97">#</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="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">top = </span> <span class="nx">del</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="s1">'top'</span><span class="p">)</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@returns</span>
|
|
<span class="nv">o.indent = </span> <span class="nx">@idt</span> <span class="mi">1</span>
|
|
<span class="nv">o.top = </span> <span class="kc">true</span>
|
|
<span class="vi">@condition.parenthetical = </span><span class="kc">yes</span>
|
|
<span class="nv">cond = </span> <span class="nx">@condition</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="nv">set = </span> <span class="s1">''</span>
|
|
<span class="nx">unless</span> <span class="nx">top</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">'result'</span>
|
|
<span class="nv">set = </span><span class="s2">"#{@tab}#{rvar} = [];\n"</span>
|
|
<span class="vi">@body = </span><span class="nx">PushNode</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="nx">rvar</span><span class="p">,</span> <span class="nx">@body</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@body</span>
|
|
<span class="nv">pre = </span><span class="s2">"#{set}#{@tab}while (#{cond})"</span>
|
|
<span class="vi">@body = </span><span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span><span class="p">([</span><span class="k">new</span> <span class="nx">IfNode</span><span class="p">(</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="k">if</span> <span class="nx">@returns</span>
|
|
<span class="nv">post = </span><span class="s1">'\n'</span> <span class="o">+</span> <span class="k">new</span> <span class="nx">ReturnNode</span><span class="p">(</span><span class="nx">literal</span><span class="p">(</span><span class="nx">rvar</span><span class="p">)).</span><span class="nx">compile</span><span class="p">(</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">@idt</span><span class="p">()))</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">post = </span><span class="s1">''</span>
|
|
<span class="s2">"#{pre} {\n#{ @body.compile(o) }\n#{@tab}}#{post}"</span></pre></div> </td> </tr> <tr id="section-98"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-98">#</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></pre></div> </td> </tr> <tr id="section-99"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-99">#</a> </div> <h3>OpNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.OpNode = </span><span class="nx">class</span> <span class="nx">OpNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span></pre></div> </td> </tr> <tr id="section-100"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-100">#</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="nx">CONVERSIONS</span><span class="o">:</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></pre></div> </td> </tr> <tr id="section-101"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-101">#</a> </div> <p>The map of conversions from CoffeeScript to JavaScript symbols.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">INVERSIONS</span><span class="o">:</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></pre></div> </td> </tr> <tr id="section-102"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-102">#</a> </div> <p>The map of invertible operators.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">CHAINABLE</span><span class="o">:</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></pre></div> </td> </tr> <tr id="section-103"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-103">#</a> </div> <p>The list of operators for which we perform
|
|
<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">ASSIGNMENT</span><span class="o">:</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></pre></div> </td> </tr> <tr id="section-104"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-104">#</a> </div> <p>Our assignment operators that have no JavaScript equivalent.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">PREFIX_OPERATORS</span><span class="o">:</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="nx">class</span><span class="o">:</span> <span class="s1">'OpNode'</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">constructor</span><span class="o">:</span> <span class="p">(</span><span class="nx">@operator</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">super</span><span class="p">()</span>
|
|
<span class="vi">@operator = </span><span class="nx">@CONVERSIONS</span><span class="p">[</span><span class="nx">@operator</span><span class="p">]</span> <span class="o">or</span> <span class="nx">@operator</span>
|
|
<span class="vi">@flip = </span><span class="o">!!</span><span class="nx">flip</span>
|
|
<span class="k">if</span> <span class="nx">@first</span> <span class="k">instanceof</span> <span class="nx">ValueNode</span> <span class="o">and</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">base</span> <span class="k">instanceof</span> <span class="nx">ObjectNode</span>
|
|
<span class="vi">@first = </span><span class="k">new</span> <span class="nx">ParentheticalNode</span> <span class="nx">@first</span>
|
|
<span class="vi">@first.tags.operation = </span><span class="kc">yes</span>
|
|
<span class="vi">@second.tags.operation = </span><span class="kc">yes</span> <span class="k">if</span> <span class="nx">@second</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">isInvertible</span><span class="o">:</span> <span class="o">-></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">and</span>
|
|
<span class="o">not</span> <span class="p">(</span><span class="nx">@first</span> <span class="k">instanceof</span> <span class="nx">OpNode</span><span class="p">)</span> <span class="o">and</span> <span class="o">not</span> <span class="p">(</span><span class="nx">@second</span> <span class="k">instanceof</span> <span class="nx">OpNode</span><span class="p">)</span>
|
|
|
|
|
|
<span class="nx">isMutator</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">ends</span><span class="p">(</span><span class="nx">@operator</span><span class="p">,</span> <span class="s1">'='</span><span class="p">)</span> <span class="o">and</span> <span class="o">not</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="nx">isChainable</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">include</span><span class="p">(</span><span class="nx">@CHAINABLE</span><span class="p">,</span> <span class="nx">@operator</span><span class="p">)</span>
|
|
|
|
<span class="nx">invert</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="vi">@operator = </span><span class="nx">@INVERSIONS</span><span class="p">[</span><span class="nx">@operator</span><span class="p">]</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="p">(</span><span class="nx">idt</span><span class="p">,</span> <span class="nx">@class</span> <span class="o">+</span> <span class="s1">' '</span> <span class="o">+</span> <span class="nx">@operator</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">return</span> <span class="nx">@compileChain</span><span class="p">(</span><span class="nx">o</span><span class="p">)</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">unwrap</span><span class="p">()</span> <span class="k">instanceof</span> <span class="nx">OpNode</span> <span class="o">and</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">().</span><span class="nx">isChainable</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="nx">@compileAssignment</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">if</span> <span class="nx">indexOf</span><span class="p">(</span><span class="nx">@ASSIGNMENT</span><span class="p">,</span> <span class="nx">@operator</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">0</span>
|
|
<span class="k">return</span> <span class="nx">@compileUnary</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@isUnary</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="nx">@compileExistence</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@operator</span> <span class="o">is</span> <span class="s1">'?'</span>
|
|
<span class="vi">@first = </span><span class="k">new</span> <span class="nx">ParentheticalNode</span><span class="p">(</span><span class="nx">@first</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@first</span> <span class="k">instanceof</span> <span class="nx">OpNode</span> <span class="o">and</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">isMutator</span><span class="p">()</span>
|
|
<span class="vi">@second = </span><span class="k">new</span> <span class="nx">ParentheticalNode</span><span class="p">(</span><span class="nx">@second</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@second</span> <span class="k">instanceof</span> <span class="nx">OpNode</span> <span class="o">and</span> <span class="nx">@second</span><span class="p">.</span><span class="nx">isMutator</span><span class="p">()</span>
|
|
<span class="p">[</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">@operator</span><span class="p">,</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">join</span> <span class="s1">' '</span></pre></div> </td> </tr> <tr id="section-105"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-105">#</a> </div> <p>Operators must come before their operands with a space.</p> </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="nv">shared = </span><span class="nx">@first</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">().</span><span class="nx">second</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">shared</span><span class="p">.</span><span class="nx">compileReference</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">if</span> <span class="nx">shared</span><span class="p">.</span><span class="nx">containsType</span> <span class="nx">CallNode</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="p">[</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">@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">shared</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="s2">"(#{first}) && (#{shared} #{@operator} #{second})"</span></pre></div> </td> </tr> <tr id="section-106"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-106">#</a> </div> <p>Mimic Python's chained comparisons when multiple comparison operators are
|
|
used sequentially. For example:</p>
|
|
|
|
<pre><code>bin/coffee -e "puts 50 < 65 > 10"
|
|
true
|
|
</code></pre> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">compileAssignment</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">firstVar</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@first</span><span class="p">.</span><span class="nx">compileReference</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">precompile</span><span class="o">:</span> <span class="kc">yes</span><span class="p">,</span> <span class="nx">assignment</span><span class="o">:</span> <span class="kc">yes</span>
|
|
<span class="nv">second = </span><span class="nx">@second</span><span class="p">.</span><span class="nx">compile</span> <span class="nx">o</span>
|
|
<span class="nv">second = </span><span class="s2">"(#{second})"</span> <span class="k">if</span> <span class="nx">@second</span> <span class="k">instanceof</span> <span class="nx">OpNode</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="p">(</span><span class="nx">first</span><span class="p">)</span> <span class="k">if</span> <span class="nx">first</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="nx">IDENTIFIER</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="s2">"#{first} = #{ ExistenceNode.compileTest(o, literal(firstVar))[0] } ? #{firstVar} : #{second}"</span> <span class="k">if</span> <span class="nx">@operator</span> <span class="o">is</span> <span class="s1">'?='</span>
|
|
<span class="s2">"#{first} #{ @operator.substr(0, 2) } (#{firstVar} = #{second})"</span></pre></div> </td> </tr> <tr id="section-107"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-107">#</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">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="p">[</span><span class="nx">test</span><span class="p">,</span> <span class="nx">ref</span><span class="p">]</span> <span class="o">=</span> <span class="nx">ExistenceNode</span><span class="p">.</span><span class="nx">compileTest</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">@first</span><span class="p">)</span>
|
|
<span class="s2">"#{test} ? #{ref} : #{ @second.compile(o) }"</span></pre></div> </td> </tr> <tr id="section-108"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-108">#</a> </div> <p>If this is an existence operator, we delegate to <code>ExistenceNode.compileTest</code>
|
|
to give us the safe references for the variables.</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">space = </span><span class="k">if</span> <span class="nx">indexOf</span><span class="p">(</span><span class="nx">@PREFIX_OPERATORS</span><span class="p">,</span> <span class="nx">@operator</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">0</span> <span class="k">then</span> <span class="s1">' '</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="nv">parts = </span><span class="p">[</span><span class="nx">@operator</span><span class="p">,</span> <span class="nx">space</span><span class="p">,</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="nv">parts = </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="p">(</span><span class="s1">''</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-109"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-109">#</a> </div> <p>Compile a unary <strong>OpNode</strong>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.InNode = </span><span class="nx">class</span> <span class="nx">InNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'InNode'</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">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="k">super</span><span class="p">()</span>
|
|
|
|
<span class="nx">isArray</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@array</span> <span class="k">instanceof</span> <span class="nx">ValueNode</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="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">@obj1</span><span class="p">,</span> <span class="nx">@obj2</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@object</span><span class="p">.</span><span class="nx">compileReference</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">precompile</span><span class="o">:</span> <span class="kc">yes</span>
|
|
<span class="k">if</span> <span class="nx">@isArray</span><span class="p">()</span> <span class="k">then</span> <span class="nx">@compileOrTest</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">else</span> <span class="nx">@compileLoopTest</span><span class="p">(</span><span class="nx">o</span><span class="p">)</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="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="s2">"#{item.compile(o)} === #{if i then @obj2 else @obj1}"</span>
|
|
<span class="s2">"(#{tests.join(' || ')})"</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">@arr1</span><span class="p">,</span> <span class="nx">@arr2</span><span class="p">]</span> <span class="o">=</span> <span class="nx">@array</span><span class="p">.</span><span class="nx">compileReference</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">precompile</span><span class="o">:</span> <span class="kc">yes</span>
|
|
<span class="p">[</span><span class="nx">i</span><span class="p">,</span> <span class="nx">l</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</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="p">(</span><span class="s1">'i'</span><span class="p">),</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="p">(</span><span class="s1">'len'</span><span class="p">)]</span>
|
|
<span class="nv">prefix = </span><span class="k">if</span> <span class="nx">@obj1</span> <span class="o">isnt</span> <span class="nx">@obj2</span> <span class="k">then</span> <span class="nx">@obj1</span> <span class="o">+</span> <span class="s1">'; '</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="s2">"(function(){ #{prefix}for (var #{i}=0, #{l}=#{@arr1}.length; #{i}<#{l}; #{i}++) { if (#{@arr2}[#{i}] === #{@obj2}) return true; } return false; }).call(this)"</span></pre></div> </td> </tr> <tr id="section-110"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-110">#</a> </div> <h3>InNode</h3> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-111"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-111">#</a> </div> <h3>TryNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.TryNode = </span><span class="nx">class</span> <span class="nx">TryNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'TryNode'</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="o">-></span> <span class="kc">yes</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="k">super</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-112"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-112">#</a> </div> <p>A classic <em>try/catch/finally</em> block.</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.indent = </span><span class="nx">@idt</span> <span class="mi">1</span>
|
|
<span class="nv">o.top = </span><span class="kc">true</span>
|
|
<span class="nv">attemptPart = </span><span class="nx">@attempt</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="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="k">then</span> <span class="s2">" catch#{errorPart}{\n#{ @recovery.compile(o) }\n#{@tab}}"</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="nv">finallyPart = </span><span class="p">(</span><span class="nx">@ensure</span> <span class="o">or</span> <span class="s1">''</span><span class="p">)</span> <span class="o">and</span> <span class="s1">' finally {\n'</span> <span class="o">+</span> <span class="nx">@ensure</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">merge</span><span class="p">(</span><span class="nx">o</span><span class="p">))</span> <span class="o">+</span> <span class="s2">"\n#{@tab}}"</span>
|
|
<span class="s2">"#{@tab}try {\n#{attemptPart}\n#{@tab}}#{catchPart}#{finallyPart}"</span></pre></div> </td> </tr> <tr id="section-113"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-113">#</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></pre></div> </td> </tr> <tr id="section-114"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-114">#</a> </div> <h3>ThrowNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.ThrowNode = </span><span class="nx">class</span> <span class="nx">ThrowNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'ThrowNode'</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="o">-></span> <span class="kc">yes</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="k">super</span><span class="p">()</span></pre></div> </td> </tr> <tr id="section-115"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-115">#</a> </div> <p>Simple node to throw an exception.</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">return</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="s2">"#{@tab}throw #{@expression.compile(o)};"</span></pre></div> </td> </tr> <tr id="section-116"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-116">#</a> </div> <p>A <strong>ThrowNode</strong> is already a return, of sorts...</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-117"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-117">#</a> </div> <h3>ExistenceNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.ExistenceNode = </span><span class="nx">class</span> <span class="nx">ExistenceNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'ExistenceNode'</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">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="k">super</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">test = </span><span class="nx">ExistenceNode</span><span class="p">.</span><span class="nx">compileTest</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">@expression</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="nx">@parenthetical</span> <span class="k">then</span> <span class="nx">test</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nx">test</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="k">else</span> <span class="nx">test</span></pre></div> </td> </tr> <tr id="section-118"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-118">#</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="nx">@compileTest</span><span class="o">:</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">variable</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="o">=</span> <span class="nx">variable</span><span class="p">.</span><span class="nx">compileReference</span> <span class="nx">o</span><span class="p">,</span> <span class="nx">precompile</span><span class="o">:</span> <span class="kc">yes</span>
|
|
<span class="p">[</span><span class="s2">"(typeof #{first} !== \"undefined\" && #{second} !== null)"</span><span class="p">,</span> <span class="nx">second</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-119"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-119">#</a> </div> <p>The meat of the <strong>ExistenceNode</strong> is in this static <code>compileTest</code> method
|
|
because other nodes like to check the existence of their variables as well.
|
|
Be careful not to double-evaluate anything.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-120"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-120">#</a> </div> <h3>ParentheticalNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.ParentheticalNode = </span><span class="nx">class</span> <span class="nx">ParentheticalNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'ParentheticalNode'</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">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="k">super</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">@expression</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">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="nx">@expression</span><span class="p">.</span><span class="nx">makeReturn</span><span class="p">()</span>
|
|
|
|
<span class="nx">topSensitive</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="kc">yes</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">top = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'top'</span>
|
|
<span class="vi">@expression.parenthetical = </span><span class="kc">true</span>
|
|
<span class="nv">code = </span><span class="nx">@expression</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="nx">code</span> <span class="k">if</span> <span class="nx">top</span> <span class="o">and</span> <span class="nx">@expression</span><span class="p">.</span><span class="nx">isPureStatement</span> <span class="nx">o</span>
|
|
<span class="k">if</span> <span class="nx">@parenthetical</span> <span class="o">or</span> <span class="nx">@isStatement</span> <span class="nx">o</span>
|
|
<span class="k">return</span> <span class="k">if</span> <span class="nx">top</span> <span class="k">then</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">code</span>
|
|
<span class="s2">"(#{code})"</span></pre></div> </td> </tr> <tr id="section-121"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-121">#</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></pre></div> </td> </tr> <tr id="section-122"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-122">#</a> </div> <h3>ForNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.ForNode = </span><span class="nx">class</span> <span class="nx">ForNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'ForNode'</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="nx">isStatement</span><span class="o">:</span> <span class="o">-></span> <span class="kc">yes</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="nx">@name</span><span class="p">,</span> <span class="nx">@index</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">super</span><span class="p">()</span>
|
|
<span class="nx">@index</span> <span class="o">or=</span> <span class="kc">null</span>
|
|
<span class="vi">@source = </span><span class="nx">source</span><span class="p">.</span><span class="nx">source</span>
|
|
<span class="vi">@guard = </span><span class="nx">source</span><span class="p">.</span><span class="nx">guard</span>
|
|
<span class="vi">@step = </span><span class="nx">source</span><span class="p">.</span><span class="nx">step</span>
|
|
<span class="vi">@raw = </span><span class="o">!!</span><span class="nx">source</span><span class="p">.</span><span class="nx">raw</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="vi">@pattern = </span><span class="nx">@name</span> <span class="k">instanceof</span> <span class="nx">ValueNode</span>
|
|
<span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'index cannot be a pattern matching expression'</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@index</span> <span class="k">instanceof</span> <span class="nx">ValueNode</span>
|
|
<span class="vi">@returns = </span><span class="kc">false</span>
|
|
|
|
<span class="nx">topSensitive</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="kc">true</span>
|
|
|
|
<span class="nx">makeReturn</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="vi">@returns = </span><span class="kc">true</span>
|
|
<span class="k">this</span>
|
|
|
|
<span class="nx">compileReturnValue</span><span class="o">:</span> <span class="p">(</span><span class="nx">val</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">'\n'</span> <span class="o">+</span> <span class="k">new</span> <span class="nx">ReturnNode</span><span class="p">(</span><span class="nx">literal</span><span class="p">(</span><span class="nx">val</span><span class="p">)).</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">if</span> <span class="nx">@returns</span>
|
|
<span class="k">return</span> <span class="s1">'\n'</span> <span class="o">+</span> <span class="nx">val</span> <span class="k">if</span> <span class="nx">val</span>
|
|
<span class="s1">''</span></pre></div> </td> </tr> <tr id="section-123"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-123">#</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="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">topLevel = </span><span class="nx">del</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="s1">'top'</span><span class="p">)</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@returns</span>
|
|
<span class="nv">range = </span><span class="nx">@source</span> <span class="k">instanceof</span> <span class="nx">ValueNode</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">RangeNode</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="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">codeInBody = </span><span class="nx">@body</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">CodeNode</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="p">(</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="p">(</span><span class="nx">o</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>
|
|
<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="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="o">and</span> <span class="p">(</span><span class="nx">range</span> <span class="o">or</span> <span class="o">not</span> <span class="nx">codeInBody</span><span class="p">)</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">'result'</span> <span class="nx">unless</span> <span class="nx">topLevel</span>
|
|
<span class="nv">ivar = </span><span class="k">if</span> <span class="nx">codeInBody</span> <span class="k">then</span> <span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'i'</span> <span class="k">else</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="o">or</span> <span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'i'</span>
|
|
<span class="nv">varPart = </span><span class="s1">''</span>
|
|
<span class="nv">guardPart = </span><span class="s1">''</span>
|
|
<span class="nv">body = </span><span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span><span class="p">([</span><span class="nx">@body</span><span class="p">])</span>
|
|
<span class="k">if</span> <span class="nx">range</span>
|
|
<span class="nv">sourcePart = </span><span class="nx">source</span><span class="p">.</span><span class="nx">compileVariables</span><span class="p">(</span><span class="nx">o</span><span class="p">)</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="nx">o</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="o">:</span> <span class="nx">@step</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">svar = </span><span class="nx">scope</span><span class="p">.</span><span class="nx">freeVariable</span> <span class="s1">'ref'</span>
|
|
<span class="nv">sourcePart = </span><span class="s2">"#{svar} = #{ @source.compile(o) };"</span>
|
|
<span class="k">if</span> <span class="nx">@pattern</span>
|
|
<span class="nv">namePart = </span><span class="k">new</span> <span class="nx">AssignNode</span><span class="p">(</span><span class="nx">@name</span><span class="p">,</span> <span class="nx">literal</span><span class="p">(</span><span class="s2">"#{svar}[#{ivar}]"</span><span class="p">)).</span><span class="nx">compile</span><span class="p">(</span><span class="nx">merge</span> <span class="nx">o</span><span class="p">,</span> <span class="p">{</span><span class="nx">indent</span><span class="o">:</span> <span class="nx">@idt</span><span class="p">(</span><span class="mi">1</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">keepLevel</span><span class="o">:</span> <span class="kc">yes</span><span class="p">})</span> <span class="o">+</span> <span class="s1">'\n'</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">namePart = </span><span class="s2">"#{name} = #{svar}[#{ivar}]"</span> <span class="k">if</span> <span class="nx">name</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">stepPart = </span><span class="k">if</span> <span class="nx">@step</span> <span class="k">then</span> <span class="s2">"#{ivar} += #{ @step.compile(o) }"</span> <span class="k">else</span> <span class="s2">"#{ivar}++"</span>
|
|
<span class="nv">forPart = </span><span class="s2">"#{ivar} = 0, #{lvar} = #{svar}.length; #{ivar} < #{lvar}; #{stepPart}"</span>
|
|
<span class="nv">sourcePart = </span><span class="p">(</span><span class="k">if</span> <span class="nx">rvar</span> <span class="k">then</span> <span class="s2">"#{rvar} = []; "</span> <span class="k">else</span> <span class="s1">''</span><span class="p">)</span> <span class="o">+</span> <span class="nx">sourcePart</span>
|
|
<span class="nv">sourcePart = </span><span class="k">if</span> <span class="nx">sourcePart</span> <span class="k">then</span> <span class="s2">"#{@tab}#{sourcePart}\n#{@tab}"</span> <span class="k">else</span> <span class="nx">@tab</span>
|
|
<span class="nv">returnResult = </span><span class="nx">@compileReturnValue</span><span class="p">(</span><span class="nx">rvar</span><span class="p">,</span> <span class="nx">o</span><span class="p">)</span>
|
|
|
|
<span class="nv">body = </span><span class="nx">PushNode</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="nx">rvar</span><span class="p">,</span> <span class="nx">body</span><span class="p">)</span> <span class="nx">unless</span> <span class="nx">topLevel</span>
|
|
<span class="k">if</span> <span class="nx">@guard</span>
|
|
<span class="nv">body = </span><span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span><span class="p">([</span><span class="k">new</span> <span class="nx">IfNode</span><span class="p">(</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">codeInBody</span>
|
|
<span class="nx">body</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">literal</span> <span class="s2">"var #{name} = #{ivar}"</span> <span class="k">if</span> <span class="nx">range</span>
|
|
<span class="nx">body</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">literal</span> <span class="s2">"var #{namePart}"</span> <span class="k">if</span> <span class="nx">namePart</span>
|
|
<span class="nx">body</span><span class="p">.</span><span class="nx">unshift</span> <span class="nx">literal</span> <span class="s2">"var #{index} = #{ivar}"</span> <span class="k">if</span> <span class="nx">index</span>
|
|
<span class="nv">body = </span><span class="nx">ClosureNode</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="p">)</span>
|
|
<span class="k">else</span>
|
|
<span class="nv">varPart = </span><span class="p">(</span><span class="nx">namePart</span> <span class="o">or</span> <span class="s1">''</span><span class="p">)</span> <span class="o">and</span> <span class="p">(</span><span class="k">if</span> <span class="nx">@pattern</span> <span class="k">then</span> <span class="nx">namePart</span> <span class="k">else</span> <span class="s2">"#{@idt(1)}#{namePart};\n"</span><span class="p">)</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#{@idt(1)}if (!#{utility('hasProp')}.call(#{svar}, #{ivar})) continue;"</span> <span class="nx">unless</span> <span class="nx">@raw</span>
|
|
<span class="nv">body = </span><span class="nx">body</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</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">indent</span><span class="o">:</span> <span class="nx">@idt</span><span class="p">(</span><span class="mi">1</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="nv">vars = </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="s2">"#{name}, #{ivar}"</span>
|
|
<span class="s2">"#{sourcePart}for (#{forPart}) {#{guardPart}\n#{varPart}#{body}\n#{@tab}}#{returnResult}"</span></pre></div> </td> </tr> <tr id="section-124"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-124">#</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></pre></div> </td> </tr> <tr id="section-125"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-125">#</a> </div> <h3>SwitchNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.SwitchNode = </span><span class="nx">class</span> <span class="nx">SwitchNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'SwitchNode'</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="o">-></span> <span class="kc">yes</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="k">super</span><span class="p">()</span>
|
|
<span class="vi">@tags.subjectless = </span><span class="o">!</span><span class="nx">@subject</span>
|
|
<span class="nx">@subject</span> <span class="o">or=</span> <span class="nx">literal</span> <span class="s1">'true'</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="p">.</span><span class="nx">makeReturn</span><span class="p">()</span> <span class="k">if</span> <span class="nx">@otherwise</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">idt = o.indent = </span><span class="nx">@idt</span> <span class="mi">2</span>
|
|
<span class="nv">o.top = </span><span class="kc">yes</span>
|
|
<span class="nv">code = </span><span class="s2">"#{ @tab }switch (#{ @subject.compile o }) {"</span>
|
|
<span class="k">for</span> <span class="nx">pair</span> <span class="k">in</span> <span class="nx">@cases</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="o">=</span> <span class="nx">pair</span>
|
|
<span class="nv">exprs = </span><span class="nx">block</span><span class="p">.</span><span class="nx">expressions</span>
|
|
<span class="k">for</span> <span class="nx">condition</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">condition = </span><span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'!!'</span><span class="p">,</span> <span class="k">new</span> <span class="nx">ParentheticalNode</span> <span class="nx">condition</span> <span class="k">if</span> <span class="nx">@tags</span><span class="p">.</span><span class="nx">subjectless</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s2">"\n#{ @idt(1) }case #{ condition.compile o }:"</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s2">"\n#{ block.compile o }"</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s2">"\n#{ idt }break;"</span> <span class="nx">unless</span> <span class="nx">exprs</span><span class="p">[</span><span class="nx">exprs</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nx">ReturnNode</span>
|
|
<span class="k">if</span> <span class="nx">@otherwise</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s2">"\n#{ @idt(1) }default:\n#{ @otherwise.compile o }"</span>
|
|
<span class="nx">code</span> <span class="o">+=</span> <span class="s2">"\n#{ @tab }}"</span>
|
|
<span class="nx">code</span></pre></div> </td> </tr> <tr id="section-126"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-126">#</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></pre></div> </td> </tr> <tr id="section-127"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-127">#</a> </div> <h3>IfNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">exports.IfNode = </span><span class="nx">class</span> <span class="nx">IfNode</span> <span class="k">extends</span> <span class="nx">BaseNode</span>
|
|
|
|
<span class="nx">class</span><span class="o">:</span> <span class="s1">'IfNode'</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="s1">'assigner'</span><span class="p">]</span>
|
|
|
|
<span class="nx">topSensitive</span><span class="o">:</span> <span class="o">-></span> <span class="kc">true</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="nx">@tags</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nx">@tags</span> <span class="o">or=</span> <span class="p">{}</span>
|
|
<span class="k">if</span> <span class="nx">@tags</span><span class="p">.</span><span class="nx">invert</span>
|
|
<span class="k">if</span> <span class="nx">@condition</span> <span class="k">instanceof</span> <span class="nx">OpNode</span> <span class="o">and</span> <span class="nx">@condition</span><span class="p">.</span><span class="nx">isInvertible</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="k">else</span>
|
|
<span class="vi">@condition = </span><span class="k">new</span> <span class="nx">OpNode</span> <span class="s1">'!'</span><span class="p">,</span> <span class="k">new</span> <span class="nx">ParentheticalNode</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="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>
|
|
|
|
<span class="nx">forceStatement</span><span class="o">:</span> <span class="o">-></span>
|
|
<span class="vi">@tags.statement = </span><span class="kc">true</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-128"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-128">#</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>IfNodes</strong> are compiled into ternary 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="nx">addElse</span><span class="o">:</span> <span class="p">(</span><span class="nx">elseBody</span><span class="p">,</span> <span class="nx">statement</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="p">,</span> <span class="nx">statement</span>
|
|
<span class="k">else</span>
|
|
<span class="vi">@isChain = </span><span class="nx">elseBody</span> <span class="k">instanceof</span> <span class="nx">IfNode</span>
|
|
<span class="vi">@elseBody = </span><span class="nx">@ensureExpressions</span> <span class="nx">elseBody</span>
|
|
<span class="k">this</span></pre></div> </td> </tr> <tr id="section-129"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-129">#</a> </div> <p>Rewrite a chain of <strong>IfNodes</strong> to add a default case as the final <em>else</em>.</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">@statement</span> <span class="o">or=</span> <span class="o">!!</span><span class="p">((</span><span class="nx">o</span> <span class="o">and</span> <span class="nx">o</span><span class="p">.</span><span class="nx">top</span><span class="p">)</span> <span class="o">or</span> <span class="nx">@tags</span><span class="p">.</span><span class="nx">statement</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="p">(</span><span class="nx">@elseBody</span> <span class="o">and</span> <span class="nx">@elseBodyNode</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">compileCondition</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">conditions = </span><span class="nx">flatten</span> <span class="p">[</span><span class="nx">@condition</span><span class="p">]</span>
|
|
<span class="nx">conditions</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nv">parenthetical = </span><span class="kc">yes</span> <span class="k">if</span> <span class="nx">conditions</span><span class="p">.</span><span class="nx">length</span> <span class="o">is</span> <span class="mi">1</span>
|
|
<span class="p">(</span><span class="nx">cond</span><span class="p">.</span><span class="nx">compile</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">for</span> <span class="nx">cond</span> <span class="k">in</span> <span class="nx">conditions</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s1">' || '</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="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">then</span> <span class="nx">@compileStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="k">else</span> <span class="nx">@compileTernary</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="k">if</span> <span class="nx">@isStatement</span><span class="p">()</span>
|
|
<span class="nx">@body</span> <span class="o">and=</span> <span class="nx">@ensureExpressions</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="nx">@ensureExpressions</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="k">else</span>
|
|
<span class="k">new</span> <span class="nx">ReturnNode</span> <span class="k">this</span>
|
|
|
|
<span class="nx">ensureExpressions</span><span class="o">:</span> <span class="p">(</span><span class="nx">node</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">if</span> <span class="nx">node</span> <span class="k">instanceof</span> <span class="nx">Expressions</span> <span class="k">then</span> <span class="nx">node</span> <span class="k">else</span> <span class="k">new</span> <span class="nx">Expressions</span> <span class="p">[</span><span class="nx">node</span><span class="p">]</span></pre></div> </td> </tr> <tr id="section-130"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-130">#</a> </div> <p>The <strong>IfNode</strong> only compiles into a statement if either of its bodies needs
|
|
to be a statement. Otherwise a ternary is safe.</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">top = </span><span class="nx">del</span> <span class="nx">o</span><span class="p">,</span> <span class="s1">'top'</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">condO = </span><span class="nx">merge</span> <span class="nx">o</span>
|
|
<span class="nv">o.indent = </span><span class="nx">@idt</span> <span class="mi">1</span>
|
|
<span class="nv">o.top = </span><span class="kc">true</span>
|
|
<span class="nv">ifDent = </span><span class="k">if</span> <span class="nx">child</span> <span class="o">or</span> <span class="p">(</span><span class="nx">top</span> <span class="o">and</span> <span class="o">not</span> <span class="nx">@isStatement</span><span class="p">(</span><span class="nx">o</span><span class="p">))</span> <span class="k">then</span> <span class="s1">''</span> <span class="k">else</span> <span class="nx">@idt</span><span class="p">()</span>
|
|
<span class="nv">comDent = </span><span class="k">if</span> <span class="nx">child</span> <span class="k">then</span> <span class="nx">@idt</span><span class="p">()</span> <span class="k">else</span> <span class="s1">''</span>
|
|
<span class="nv">body = </span><span class="nx">@body</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="nv">ifPart = </span><span class="s2">"#{ifDent}if (#{ @compileCondition(condO) }) {\n#{body}\n#{@tab}}"</span>
|
|
<span class="k">return</span> <span class="nx">ifPart</span> <span class="nx">unless</span> <span class="nx">@elseBody</span>
|
|
<span class="nv">elsePart = </span><span class="k">if</span> <span class="nx">@isChain</span>
|
|
<span class="s1">' else '</span> <span class="o">+</span> <span class="nx">@elseBodyNode</span><span class="p">().</span><span class="nx">compile</span><span class="p">(</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">indent</span><span class="o">:</span> <span class="nx">@idt</span><span class="p">(),</span> <span class="nx">chainChild</span><span class="o">:</span> <span class="kc">true</span><span class="p">}))</span>
|
|
<span class="k">else</span>
|
|
<span class="s2">" else {\n#{ @elseBody.compile(o) }\n#{@tab}}"</span>
|
|
<span class="s2">"#{ifPart}#{elsePart}"</span></pre></div> </td> </tr> <tr id="section-131"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-131">#</a> </div> <p>Compile the <strong>IfNode</strong> 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">compileTernary</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">@bodyNode</span><span class="p">().</span><span class="nv">tags.operation = </span><span class="vi">@condition.tags.operation = </span><span class="kc">yes</span>
|
|
<span class="nx">@elseBodyNode</span><span class="p">().</span><span class="nv">tags.operation = </span><span class="kc">yes</span> <span class="k">if</span> <span class="nx">@elseBody</span>
|
|
<span class="nv">ifPart = </span><span class="nx">@condition</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">@bodyNode</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="nv">elsePart = </span><span class="k">if</span> <span class="nx">@elseBody</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="k">else</span> <span class="s1">'null'</span>
|
|
<span class="nv">code = </span><span class="s2">"#{ifPart} : #{elsePart}"</span>
|
|
<span class="k">if</span> <span class="nx">@tags</span><span class="p">.</span><span class="nx">operation</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-132"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-132">#</a> </div> <p>Compile the IfNode as a ternary operator.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-133"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-133">#</a> </div> <h2>Faux-Nodes</h2> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-134"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-134">#</a> </div> <h3>PushNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">PushNode = exports.PushNode =</span>
|
|
<span class="nx">wrap</span><span class="o">:</span> <span class="p">(</span><span class="nx">array</span><span class="p">,</span> <span class="nx">expressions</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="nv">expr = </span><span class="nx">expressions</span><span class="p">.</span><span class="nx">unwrap</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="nx">expressions</span> <span class="k">if</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">isPureStatement</span><span class="p">()</span> <span class="o">or</span> <span class="nx">expr</span><span class="p">.</span><span class="nx">containsPureStatement</span><span class="p">()</span>
|
|
<span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span><span class="p">([</span><span class="k">new</span> <span class="nx">CallNode</span><span class="p">(</span>
|
|
<span class="k">new</span> <span class="nx">ValueNode</span><span class="p">(</span><span class="nx">literal</span><span class="p">(</span><span class="nx">array</span><span class="p">),</span> <span class="p">[</span><span class="k">new</span> <span class="nx">AccessorNode</span><span class="p">(</span><span class="nx">literal</span><span class="p">(</span><span class="s1">'push'</span><span class="p">))]),</span> <span class="p">[</span><span class="nx">expr</span><span class="p">]</span>
|
|
<span class="p">)])</span></pre></div> </td> </tr> <tr id="section-135"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-135">#</a> </div> <p>Faux-nodes are never created by the grammar, but are used during code
|
|
generation to generate other combinations of nodes. The <strong>PushNode</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></pre></div> </td> </tr> <tr id="section-136"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-136">#</a> </div> <h3>ClosureNode</h3> </td> <td class="code"> <div class="highlight"><pre><span class="nv">ClosureNode = exports.ClosureNode =</span></pre></div> </td> </tr> <tr id="section-137"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-137">#</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="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="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">containsPureStatement</span><span class="p">()</span>
|
|
<span class="nv">func = </span><span class="k">new</span> <span class="nx">ParentheticalNode</span><span class="p">(</span><span class="k">new</span> <span class="nx">CodeNode</span><span class="p">([],</span> <span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span><span class="p">([</span><span class="nx">expressions</span><span class="p">])))</span>
|
|
<span class="nv">args = </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="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">LiteralNode</span> <span class="o">and</span> <span class="p">(</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="p">)</span>
|
|
<span class="nv">mentionsThis = </span><span class="nx">expressions</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="p">(</span><span class="nx">n</span> <span class="k">instanceof</span> <span class="nx">LiteralNode</span> <span class="o">and</span> <span class="p">(</span><span class="nx">n</span><span class="p">.</span><span class="nx">value</span> <span class="o">is</span> <span class="s1">'this'</span><span class="p">))</span> <span class="o">or</span>
|
|
<span class="p">(</span><span class="nx">n</span> <span class="k">instanceof</span> <span class="nx">CodeNode</span> <span class="o">and</span> <span class="nx">n</span><span class="p">.</span><span class="nx">bound</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="nx">mentionsArgs</span> <span class="o">or</span> <span class="nx">mentionsThis</span>
|
|
<span class="nv">meth = </span><span class="nx">literal</span><span class="p">(</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="p">)</span>
|
|
<span class="nv">args = </span><span class="p">[</span><span class="nx">literal</span><span class="p">(</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="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">ValueNode</span> <span class="nx">func</span><span class="p">,</span> <span class="p">[</span><span class="k">new</span> <span class="nx">AccessorNode</span><span class="p">(</span><span class="nx">meth</span><span class="p">)]</span>
|
|
<span class="nv">call = </span><span class="k">new</span> <span class="nx">CallNode</span><span class="p">(</span><span class="nx">func</span><span class="p">,</span> <span class="nx">args</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="nx">statement</span> <span class="k">then</span> <span class="nx">Expressions</span><span class="p">.</span><span class="nx">wrap</span><span class="p">([</span><span class="nx">call</span><span class="p">])</span> <span class="k">else</span> <span class="nx">call</span></pre></div> </td> </tr> <tr id="section-138"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-138">#</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="nv">UTILITIES =</span></pre></div> </td> </tr> <tr id="section-139"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-139">#</a> </div> <h2>Utility Functions</h2> </td> <td class="code"> <div class="highlight"><pre> <span class="k">extends</span><span class="o">:</span> <span class="s2">"""</span>
|
|
<span class="s2"> function(child, parent) {</span>
|
|
<span class="s2"> var ctor = function(){};</span>
|
|
<span class="s2"> ctor.prototype = parent.prototype;</span>
|
|
<span class="s2"> child.prototype = new ctor();</span>
|
|
<span class="s2"> child.prototype.constructor = child;</span>
|
|
<span class="s2"> if (typeof parent.extended === "</span><span class="nx">function</span><span class="s2">") parent.extended(child);</span>
|
|
<span class="s2"> child.__super__ = parent.prototype;</span>
|
|
<span class="s2"> }</span>
|
|
<span class="s2"> """</span></pre></div> </td> </tr> <tr id="section-140"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-140">#</a> </div> <p>Correctly set up a prototype chain for inheritance, including a reference
|
|
to the superclass for <code>super()</code> calls. See:
|
|
<a href="http://closure-library.googlecode.com/svn/docs/closureGoogBase.js.source.html#line1206">goog.inherits</a>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">bind</span><span class="o">:</span> <span class="s2">"""</span>
|
|
<span class="s2"> function(func, context) {</span>
|
|
<span class="s2"> return function(){ return func.apply(context, arguments); };</span>
|
|
<span class="s2"> }</span>
|
|
<span class="s2"> """</span></pre></div> </td> </tr> <tr id="section-141"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-141">#</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">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-142"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-142">#</a> </div> <p>Shortcuts to speed up the lookup time for native functions.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-143"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-143">#</a> </div> <h2>Constants</h2> </td> <td class="code"> <div class="highlight"><pre><span class="nv">TAB = </span><span class="s1">' '</span></pre></div> </td> </tr> <tr id="section-144"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-144">#</a> </div> <p>Tabs are two spaces for pretty printing.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">TRAILING_WHITESPACE = </span><span class="sr">/[ \t]+$/gm</span></pre></div> </td> </tr> <tr id="section-145"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-145">#</a> </div> <p>Trim out all trailing whitespace, so that the generated code plays nice
|
|
with Git.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IDENTIFIER = </span><span class="sr">/^[a-zA-Z\$_](\w|\$)*$/</span>
|
|
<span class="nv">NUMBER = </span><span class="sr">/^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-]?[0-9]+)?)))\b$/i</span>
|
|
<span class="nv">SIMPLENUM = </span><span class="sr">/^-?\d+$/</span></pre></div> </td> </tr> <tr id="section-146"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-146">#</a> </div> <p>Keep these identifier regexes in sync with the Lexer.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">IS_STRING = </span><span class="sr">/^['"]/</span></pre></div> </td> </tr> <tr id="section-147"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-147">#</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-148"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-148">#</a> </div> <h2>Utility Functions</h2> </td> <td class="code"> <div class="highlight"><pre><span class="nv">literal = </span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></span>
|
|
<span class="k">new</span> <span class="nx">LiteralNode</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span></pre></div> </td> </tr> <tr id="section-149"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-149">#</a> </div> <p>Handy helper for a generating LiteralNode.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nv">utility = </span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="o">-></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>
|
|
|
|
</pre></div> </td> </tr> <tr id="section-150"> <td class="docs"> <div class="octowrap"> <a class="octothorpe" href="#section-150">#</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> |