<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">

<channel>
<title><![CDATA[Perl 6: SMOP Lexical Scope Implementation]]></title>
<link>http://www.perlfoundation.org/perl6/index.cgi?smop_lexical_scope_implementation</link>
<description></description>
<pubDate>Fri, 08 Aug 2008 01:01:45 -0000</pubDate>
<webMaster>synedra@gmail.com</webMaster>
<generator>Socialtext Workspace v2.14.7.2</generator>

<item>
<title><![CDATA[SMOP Lexical Scope Implementation]]></title>
<link>http://www.perlfoundation.org/perl6/index.cgi?smop_lexical_scope_implementation</link>
<description><![CDATA[<div class="wiki">
<p>
One first hard step in implementing lexical scopes is to put together a lot of details that are spread over the specs. In this page we're going to sketch a model for the lexical scopes that support all the documented Perl 6 features.</p>
<h1 id="variable_lookup">Variable Lookup</h1>
<p>
The local variable lookup happens in the following order:</p>
<ol>
<li>MY</li>
<li>OUTER recursive</li>
</ol>
<h1 id="special_cases">Special cases</h1>
<h2 id="">$_</h2>
<p>
$_ is always locally defined in any block. Routines start with an empty $_, other Code objects have a signature of:</p>
<pre>
 -&gt; $_ is rw = OUTER::&lt;$_&gt;
</pre>
<br /><h2 id="and">$! and $/</h2>
<p>
These variables are created empty in every new scope and marked as contextual, so their value can be set from the inner scopes and be seen from the caller. For instance, the regex code sets $+/ when the match succeeds, so that the calling code can see the match object.</p>
<h1 id="modelling">Modelling</h1>
<h2 id="data_structure">Data Structure</h2>
<pre>
class LexicalScope {
  has %.entries handles postcircumfix:&lt;{}&gt;, exists;
  has LexicalScope $.outer is rw;
}
</pre>
<br /><h2 id="accessing_the_lexical_scope">Accessing the Lexical Scope</h2>
<p>
The lexical scope is an attribute of the continuation, meaning that the &quot;current continuation&quot; points to the &quot;current lexical scope&quot;. The Perl 6 specs doesn't specify a macro that return the current continuation, so we have one of the <a href="http://www.perlfoundation.org/perl6/index.cgi?smop_specific_macros" title="(59 months) There are some macros that are specific to SMOP ___RI___( obj) returns the Responder Interface for a...">SMOP Specific Macros</a> ___CURRENT_CONTINUATION___. This should provide access to the following API:</p>
<pre>
class Continuation {
  has LexicalScope $.lexical is rw;
  has Continuation $.outer_context is rw;
  has Continuation $.enclosing_routine is rw;
}
</pre>
<br /><p>
This should be part of the <a href="http://www.perlfoundation.org/perl6/index.cgi?smop_interpreter_implementation_api" title="(59 months) This is the API that must be implemented by any interpreter that wants to have its run loop integrat...">SMOP Interpreter Implementation API</a>.</p>
<h2 id="making_a_variable_lookup">Making a variable lookup</h2>
<pre>
class LexicalScope is also {
  method lookup($lexical: $name) {
    if ($lexical.exists($name)) {
      return $lexical{$name};
    } elsif ($lexical.outer) {
      return $lexical.outer.lookup($name);
    } else {
      fail(&quot;No variable $name in the current scope&quot;);
    }
  }
}
</pre>
</div>
]]></description>
<author>Daniel Ruoso</author>
<guid isPermaLink="true">http://www.perlfoundation.org/perl6/index.cgi?smop_lexical_scope_implementation</guid>
<pubDate>Fri, 08 Aug 2008 01:01:45 -0000</pubDate>
</item>

</channel>
</rss>