<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Baroquebobcat</title>
	<atom:link href="http://blog.baroquebobcat.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.baroquebobcat.com</link>
	<description>Ruby, Computer Science, Japan and Stuff</description>
	<lastBuildDate>Thu, 10 May 2012 00:27:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Mirah Office Hours: porting bootclasspath to newast</title>
		<link>http://blog.baroquebobcat.com/2012/05/09/mirah-office-hours-porting-bootclasspath-to-newast/</link>
		<comments>http://blog.baroquebobcat.com/2012/05/09/mirah-office-hours-porting-bootclasspath-to-newast/#comments</comments>
		<pubDate>Thu, 10 May 2012 00:27:40 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Mirah]]></category>

		<guid isPermaLink="false">http://blog.baroquebobcat.com/?p=791</guid>
		<description><![CDATA[In the past two weeks I fixed a bug, wrote a hacked together REPL and ported the new bootclasspath functionality to the newast branch. I also started working on fixing the newast branch&#8217;s Java source code generator support, but haven&#8217;t gotten very far with that yet. The bug I fixed, #185, was where macros called [...]]]></description>
				<content:encoded><![CDATA[<p>In the past two weeks I fixed a bug, wrote a hacked together REPL and ported the new bootclasspath functionality to the newast branch. I also started working on fixing the newast branch&#8217;s Java source code generator support, but haven&#8217;t gotten very far with that yet.</p>
<p>The bug I fixed, <a href="https://github.com/mirah/mirah/issues/185">#185</a>, was where macros called in a class body that were defined in the class body were blowing up when generating Java source code.</p>
<p><script src="https://gist.github.com/2649876.js?file=185"></script></p>
<p>The problem was that the code generator assumed it was inside a method scope when emitting the code for a scoped body. Macros produce a ScopedBody node, so when a macro was called outside a method, it blew up, because <tt>@method</tt> was nil. The ScopedBody code was making a bad assumption.</p>
<p><script src="https://gist.github.com/2649876.js?file=scoped_body_old.rb"></script></p>
<p>I fixed it by special casing the code to check to see whether it was in a method or not. It&#8217;s not very pretty, and I&#8217;m sure there&#8217;s a better way to do it, but it works and I wrote a test for it. The new code checks  <tt>@method</tt> and optionally wraps the call to super. Ugh.</p>
<p><script src="https://gist.github.com/2649876.js?file=scoped_body_new.rb"></script></p>
<p>I feel like there&#8217;s got to be a way to restructure the code to make this cleaner.</p>
<p><strong>Hacky REPL</strong></p>
<p>I&#8217;d been meaning to try putting together a REPL for Mirah for a few months. I&#8217;ve got pages of ideas sketched out. There&#8217;s some hard problems in making a nice REPL for a statically typed language and it looked like fun. I thought I&#8217;d need to learn about bindings and readline and how IRB works, and I never got started (IRB is kind of neat by the way).</p>
<p>Then, last week, I thought to myself &#8220;screw all this planning, how hard would it be to just hack something together? <a href="http://pastie.org/14416">Merb</a> wasn&#8217;t built in a day.&#8221; All interesting software gets its start as a short, clever hack. Why not make my own? So, I hacked something together in a couple hours.</p>
<p>Check it out: <a href="https://gist.github.com/2564819">gist.github.com/2564819</a></p>
<p>How does it work? Well, it loops over input&#8211;it&#8217;s a REPL, waiting for a complete statement and then parses and compiles it. But, it does that in a really hacky way.</p>
<p>First, it doesn&#8217;t print each statements value&#8211;so it&#8217;s not really a REPL, more like a REL, Read Evaluate Line.</p>
<p>Second, it does support multiline statements, but rather than having a nice scanner/partial parser, it just catches syntax errors and eats them if they look like an unfinished statement. Which is pretty crappy.</p>
<p><script src="https://gist.github.com/2649876.js?file=repl_parse.rb"></script></p>
<p>Finally, and most importantly, it doesn&#8217;t have a global binding&#8211;this means that you can&#8217;t do stuff like this:</p>
<p><script src="https://gist.github.com/2649876.js?file=wont_work_in_mirah_repl_hack"></script></p>
<p>because x goes out of scope after the line is evaluated. Fixing this, I think, is the really cool problem in writing a Mirah REPL.</p>
<p>On the other hand, you can define classes and create them, because the REPL uses a shared classloader. So there.</p>
<p><strong>This week AKA plans</strong></p>
<ul>
<li>
I&#8217;d like to fix type inference for blocks for methods that are inherited.</li>
<li>Get Java source working on newast</li>
<li>fix some of the dumber bugs on newast</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.baroquebobcat.com/2012/05/09/mirah-office-hours-porting-bootclasspath-to-newast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mirah Office Hours: After the Hackathon</title>
		<link>http://blog.baroquebobcat.com/2012/04/25/mirah-office-hours-after-the-hackathon-2/</link>
		<comments>http://blog.baroquebobcat.com/2012/04/25/mirah-office-hours-after-the-hackathon-2/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 15:19:03 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Mirah]]></category>

		<guid isPermaLink="false">http://blog.baroquebobcat.com/?p=784</guid>
		<description><![CDATA[This Sunday, I wanted to merge master into newast again. A few bugs have been fixed since then, and I wanted to have the tests for those bugs in the newast branch, since that’s the future. I’d also cleaned up some of the tooling around running commands and wanted those changes merged in. Because I’m [...]]]></description>
				<content:encoded><![CDATA[<p>This Sunday, I wanted to merge master into newast again. A few bugs have been fixed since then, and I wanted to have the tests for those bugs in the newast branch, since that’s the future. I’d also cleaned up some of the tooling around running commands and wanted those changes merged in. Because I’m stubborn and hard headed I tried to actually merge master into newast, but that wasn’t a great option</p>
<p><a href="http://www.flickr.com/photos/baroquebobcat/6965729384/" title="merge hell by baroquebobcat, on Flickr"><img src="http://farm6.staticflickr.com/5194/6965729384_f5a9af7825.jpg" width="500" height="224" alt="merge hell"></a></p>
<p>There were too many conflicts. Too many. And I couldn’t tell which ones were new changes and which when just things that were different. After making a couple attempts at using a merge and trying to break up the pieces one way or another, I found the answer. _git cherry-pick_ ftw. With cherry pick I could just merge the changes that had happened since the last merge&#8211;(<a href="https://github.com/mirah/mirah/compare/9a06b834...a86c3651">https://github.com/mirah/mirah/compare/9a06b834&#8230;a86c3651</a>) and not have to worry about all the other differences between the branches.</p>
<p>It worked pretty nicely, I cherry picked most of the changes without problems. Some of the commits that changed code that doesn’t exist any more, or has been converted to using the visitor pattern needed some modifications, but it was really straight forward.</p>
<p><strong>What I didn’t merge in: bootclasspath</strong></p>
<p>The &#8211;bootclasspath flag changes touched a number of things that I didn’t want to dig into this week, so I left those out. Fortunately, those changes were well factored, so I didn’t have to change much outside the commits making up that feature. I’m planning on checking that out next week. Check out <a href="https://github.com/mirah/mirah/commit/e00ab4bc6a3df6853eb3b951955660f378ef3a35">this commit</a> for more info.</p>
<p><strong>In the future</strong></p>
<p>Next time I work on a bug on master, I’ll try to get both newast and master fixed&#8211;if it makes sense. Doing it that way will make sure that they don’t get too out of sync.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baroquebobcat.com/2012/04/25/mirah-office-hours-after-the-hackathon-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mirah Hackathon</title>
		<link>http://blog.baroquebobcat.com/2012/04/24/mirah-hackathon/</link>
		<comments>http://blog.baroquebobcat.com/2012/04/24/mirah-hackathon/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 05:18:58 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Mirah]]></category>

		<guid isPermaLink="false">http://blog.baroquebobcat.com/?p=780</guid>
		<description><![CDATA[Happy mirah hackathon day! Mirah is simply freaking awesome. @mirahlang &#8212; Bruce Hauman (@bhauman) April 10, 2012 Last week wrapped up the Mirah Hackathon&#8211;two weeks of concentrated effort on Mirah. Check out the commits (github.com/mirah/mirah/compare/6d0821f5ba1122&#8230;f5fdb071). 46 commits! What does it all mean!? First let me explain: in March, Ryan Brown organized the hackathon so we [...]]]></description>
				<content:encoded><![CDATA[<blockquote class="twitter-tweet"><p>Happy mirah hackathon day! Mirah is simply freaking awesome. @<a href="https://twitter.com/mirahlang">mirahlang</a></p>
<p>&mdash; Bruce Hauman (@bhauman) <a href="https://twitter.com/bhauman/status/189739217851645952" data-datetime="2012-04-10T15:38:49+00:00">April 10, 2012</a></p></blockquote>
<p><script src="//platform.twitter.com/widgets.js" charset="utf-8"></script></p>
<p>Last week wrapped up the Mirah Hackathon&#8211;two weeks of concentrated effort on Mirah. Check out the commits (<a href="https://github.com/mirah/mirah/compare/6d0821f5ba1122...f5fdb071">github.com/mirah/mirah/compare/6d0821f5ba1122&#8230;f5fdb071</a>). 46 commits! What does it all mean!?</p>
<p>First let me explain: in March, Ryan Brown <a href="http://groups.google.com/group/mirah/browse_thread/thread/4ea1031576e8e5d9">organized the hackathon</a> so we could put some serious work into the newast branch&#8211;which is the future of Mirah. What’s the newast branch? It is the beginnings of moving Mirah’s compiler into Mirah, starting with the Abstract Syntax Tree. I’ve done some work with it <a href="http://blog.baroquebobcat.com/2012/02/29/mirah-office-hours-closures-in-closures/">before</a>, but over the last two weeks I saw a bit more of how it works than before.</p>
<p>So far I like where it’s going. The Ruby version of the compiler used methods on the AST node classes to do transformation, inference and compilation. That’s not great for encapsulation, but is pretty convenient. Newast uses a visitor pattern(http://en.wikipedia.org/wiki/Visitor_pattern), which makes it easier to modularize the compiler. It’s also easier to add new things that walk the tree.</p>
<p>The goals were ambitious</p>
<blockquote><p>* Documentation: what works, what doesn&#8217;t (and should), getting started, etc<br />
* Test writing to fill out gaps (this can kinda go along with documentation)<br />
* Codebase cleanup (on the new newast branch&#8230;may need to wait for<br />
Ryan to get it working well)<br />
* Bug tracker triage (close working bugs, try to fix simple ones, file<br />
unreported issues from mailing list)<br />
* Get pindah (Android framework) working (if it is not) and document<br />
how to do it.<br />
* Get dubious (GAE framework) working (if it is not) and document how to do it.<br />
* Port parts of Mirah&#8217;s Ruby code into Mirah (this is a goal of newast branch)</p></blockquote>
<p>To me it seems like the hackathon ended up mostly focusing on</p>
<blockquote><p>* Codebase cleanup (on the new newast branch&#8230;may need to wait for<br />
Ryan to get it working well)<br />
* Port parts of Mirah&#8217;s Ruby code into Mirah (this is a goal of newast branch)
</p></blockquote>
<p>As well as, getting some generics support in.</p>
<p>Before the hackathon, newast could run tests, but not that many were passing. Quite a bit of the basic functionality worked, but there was no block support and no macro support. Also, the parser was a pain to build, because it required a special branch off of master to be checked out, and the Rakefile of the parser to be modified to point at it.</p>
<p>After, newast is easier to build, blocks and macros work better and there’s some generics support.</p>
<p>Simple blocks work and simple macros work. Not all the builtins have been added yet, but the pattern for adding more is pretty straight forward.</p>
<p>Also, it’s become pretty easy to get the newast branch running locally. This rocks because it’ll make it that much easier for people to contribute.</p>
<p>Mirah on newast has some support for generics now. Mirah can now take advantage of methods and return types that are parameterized. It can’t define generics itself yet. For more information about the generic support, checkout the wiki page (<a href="https://github.com/mirah/mirah/wiki/Generics">mirah wiki</a>) on it.</p>
<p>Overall I think the hackathon was a success and we should totally do it again.</p>
<p><strong>Links</strong></p>
<ul>
<li>h<a href="http://groups.google.com/group/mirah/browse_thread/thread/4ea1031576e8e5d9">ackathon announced</a> on mailing list</li>
<li><a href="http://groups.google.com/group/mirah/browse_thread/thread/dbc4e7702ae2531c">planning the week before</a></li>
<li><a href="http://groups.google.com/group/mirah/browse_thread/thread/7d8cb399ee18ec0c">suggested tasks</a></li>
<li>It was even on <a href="http://news.ycombinator.com/item?id=3814733">hacker news</a></li>
<li>Most of the discussion during the hackthon was over IRC. The logs are on <a href="http://donttreadonme.co.uk/mirah/">donttreadonme.co.uk/mirah/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.baroquebobcat.com/2012/04/24/mirah-hackathon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Going to Mountain West Ruby Conf 2012</title>
		<link>http://blog.baroquebobcat.com/2012/03/13/going-to-mountain-west-ruby-conf-2012/</link>
		<comments>http://blog.baroquebobcat.com/2012/03/13/going-to-mountain-west-ruby-conf-2012/#comments</comments>
		<pubDate>Wed, 14 Mar 2012 04:30:04 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.baroquebobcat.com/?p=760</guid>
		<description><![CDATA[Tomorrow I&#8217;m heading out to Salt Lake City for #mwrc. It was my first Ruby conference and it&#8217;s still my favorite. The session line up looks great. See you there. mtnwestrubyconf.org]]></description>
				<content:encoded><![CDATA[<p>Tomorrow I&#8217;m heading out to Salt Lake City for #mwrc. It was my first Ruby conference and it&#8217;s still my favorite. The session line up looks great.</p>
<p>See you there.</p>
<p><a href="http://mtnwestrubyconf.org/">mtnwestrubyconf.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baroquebobcat.com/2012/03/13/going-to-mountain-west-ruby-conf-2012/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mirah Office Hours: The Yak That Will Not Die.</title>
		<link>http://blog.baroquebobcat.com/2012/03/06/mirah-office-hours-the-yak-that-will-not-die/</link>
		<comments>http://blog.baroquebobcat.com/2012/03/06/mirah-office-hours-the-yak-that-will-not-die/#comments</comments>
		<pubDate>Tue, 06 Mar 2012 16:13:08 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Mirah]]></category>

		<guid isPermaLink="false">http://blog.baroquebobcat.com/?p=761</guid>
		<description><![CDATA[The saga continues. Last week I thought I&#8217;d shaved the yak, but this week I found it still very hairy. Last week, I got closures in closures to get past the inference stage, but silly me, I didn&#8217;t actually try to compile or run them. Haha. Turns out they still don&#8217;t work. This Sunday I [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/revcyborg/22883042/" title="yak shaving by LiminalMike, on Flickr"><img src="http://farm1.staticflickr.com/17/22883042_01f3a1a3d2.jpg" width="500" height="386" alt="yak shaving"></a></p>
<p>The saga continues. Last week I thought I&#8217;d shaved the yak, but this week I found it still very hairy. Last week, I got  closures in closures to get past the inference stage, but silly me, I didn&#8217;t actually try to compile or run them. Haha. Turns out they still don&#8217;t work.</p>
<p>This Sunday I took another crack at creating view closures in Shatner. That&#8217;s where I discovered this little mess. Shatner is a great test bed for Mirah because it does crazy things with closures and macros. It&#8217;s also kind of irritating to debug because it does crazy things with closures and macros.</p>
<p>My test app looked like this:</p>
<p><script src="https://gist.github.com/1983237.js?file=closure_app.mirah"></script></p>
<p>I was trying to build closures for the view so it could share variable scope with the responding block. This particular example is pretty dumb&#8211;it doesn&#8217;t even have any variables to share. But it didn&#8217;t work and gave me the following stacktrace.</p>
<p><script src="https://gist.github.com/1983237.js?file=first_stacktrace"></script></p>
<p>The thing that caused the error on <a href="https://github.com/mirah/mirah/blob/96a90744b5288842f0ee55ecce64d2430345b486/lib/mirah/ast/scope.rb#L164">lib/mirah/ast/scope.rb#L164</a> shouldn&#8217;t have been nil, that we needed a name from, was a binding.</p>
<p>Bindings are how Mirah shares state between closures. It&#8217;s pretty neat how it works actually. The compiler determines what local variables are shared between the outer scope and the closure and creates a binding class to hold them. Then both the outer and inner scope use the binding object instead of local variables. That&#8217;s how it&#8217;s supposed to work anyway.</p>
<p>What Mirah was trying to do, was to ask the closure&#8217;s scope&#8217;s defining class for a binding type&#8211;a class definition for the shared binding, but the scope didn&#8217;t have a defining class. The scope of the closure was the static scope for SomeAppWithAnUnmacroedView, which didn&#8217;t have a defining_class because it&#8217;s the wrong sort of scope to have one.  I think that a static scope for a class body doesn&#8217;t have a defining_class because it doesn&#8217;t belong to an instance of a class, but I&#8217;m not exactly sure.</p>
<p>SomeAppWithAnUnmacroedView&#8217;s scope was the wrong scope because the block had been moved to the class&#8217;s initializer by Shatner. The get macro takes the passed block and appends it into the initializer. Moving it caused a problem because Mirah currently caches the scope of a scoped node to avoid having to do look up every time. This is fine usually, but because the macro had moved the block its scope should have changed. I unmemoized the scope by changing <tt>||=</tt> to <tt>=</tt> to see what would happen (<a href="https://github.com/mirah/mirah/blob/96a90744b5288842f0ee55ecce64d2430345b486/lib/mirah/ast/scope.rb#L20">lib/mirah/ast/scope.rb#L20</a>) .</p>
<p><script src="https://gist.github.com/1983237.js?file=second_stacktrace"></script></p>
<p>We&#8217;re making progress. Now the error happens in a different place. ScopedBody nodes don&#8217;t have a defining_class method. Maybe they should. I tried adding one, following the pattern I&#8217;d seen elsewhere.</p>
<p><script src="https://gist.github.com/1983237.js?file=defining_class.mirah"></script></p>
<p>And got a new stacktrace. This is great&#8211;it made it past inference and tried generating bytecode. And failed. But we&#8217;re still moving forward. Where the exception is raised, it looks like we&#8217;re trying to get a binding out of a hash, but the hash is nil (<a href="https://github.com/mirah/mirah/blob/96a90744b5288842f0ee55ecce64d2430345b486/lib/mirah/jvm/compiler/jvm_bytecode.rb#L529">lib/mirah/jvm/compiler/jvm_bytecode.rb#L529</a>). Going up a level, we&#8217;re asking the parent compiler for the binding with the passed name and we&#8217;re not finding it (<a href="https://github.com/mirah/mirah/blob/96a90744b5288842f0ee55ecce64d2430345b486/lib/mirah/jvm/compiler/jvm_bytecode.rb#L827 ">lib/mirah/jvm/compiler/jvm_bytecode.rb#L827<br />
</a>) . Why? The parent is another ClosureCompiler and they don&#8217;t have bindings on them.</p>
<p>So I added bindings to the closure compiler by calling super. The Base compiler class creates the binding hash used by most things, so I thought I just needed closures to do that too. </p>
<p><script src="https://gist.github.com/1983237.js?file=fourth_stacktrace"></script></p>
<p>Hooray it compiled! And we got a new and interesting error!</p>
<p>The new problem is that the code being generated is invalid. Ugh. Regenerating with <tt>-j</tt> shows us that <a href="https://gist.github.com/1983237#file_some_app_with_an_unmacroed_view.java">something&#8217;s wrong</a> with how things are organized. Argh!</p>
<p>And that&#8217;s as far as I got this week. I learned a quite a lot more about Mirah&#8217;s closure support and lack there of and got pretty far just following the stacktraces. I think I&#8217;ll do some more digging next week.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baroquebobcat.com/2012/03/06/mirah-office-hours-the-yak-that-will-not-die/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mirah Office Hours: Closures in Closures</title>
		<link>http://blog.baroquebobcat.com/2012/02/29/mirah-office-hours-closures-in-closures/</link>
		<comments>http://blog.baroquebobcat.com/2012/02/29/mirah-office-hours-closures-in-closures/#comments</comments>
		<pubDate>Thu, 01 Mar 2012 04:05:50 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Mirah]]></category>

		<guid isPermaLink="false">http://blog.baroquebobcat.com/?p=751</guid>
		<description><![CDATA[Way back in November, four months ago, I embarked on an adventure. I wanted to render views in Shatner in a friendly way. Unfortunately, at the time Mirah didn&#8217;t support defining closures inside other closures(#155). This is a big problem for all sorts of interesting use cases. In my particular use case, I was trying [...]]]></description>
				<content:encoded><![CDATA[<p>Way back in November, four months ago, I embarked on <a href="http://blog.baroquebobcat.com/2011/11/08/mirah-office-hours-6/">an adventure</a>. I wanted to render views in <a href="https://github.com/baroquebobcat/shatner">Shatner</a> in a friendly way. Unfortunately, at the time Mirah didn&#8217;t support defining closures inside other closures(<a href="https://github.com/mirah/mirah/issues/155">#155</a>). This is a big problem for all sorts of interesting use cases.</p>
<p>In my particular use case, I was trying to use a closure to get around another issue with Mirah&#8217;s edb plugin, where it didn&#8217;t like being passed unquotes (<a href="https://github.com/mirah/mirah/issues/152">#152</a>). My thought was that I could use edb to generate a render method on a closure class that would represent the view. That way, the view would have access to the environment inside the get block through the binding object that Mirah generates as part of how it handles closure creation.</p>
<p>The problem was that blocks that represent closures didn&#8217;t have the right kind of back reference to the ClosureDefinition that was added to them by the transformer. When the transformer generated the ClosureDefinition, it didn&#8217;t tell the block about it. Because the block didn&#8217;t know, it couldn&#8217;t tell closures inside it what scope they were in.</p>
<blockquote><p>A <a href="https://github.com/mirah/mirah/blob/96a90744b5288842f0ee55ecce64d2430345b486/lib/mirah/ast/class.rb#L172">ClosureDefinition</a> node is an AST node representing the class definition of a closure. The code generator uses these nodes, along with their attached blocks to make those Class$1234.class files you see when you have closures in Java.</p></blockquote>
<h3>An Example</h3>
<p><script src="https://gist.github.com/1946946.js?file=closure_in_closure.mirah"></script><br />
<noscript>
<pre>
def foo r:Runnable
  #...
end

foo do          # Block 1.
  foo do        # Block 2.
    puts 1
  end
end  
</pre>
</noscript>
<p>During the transform phase, the transformer adds a ClosureDefinition node for Block 1 that uses the outer class for it&#8217;s scope. It creates a constructor for the new ClosureDefinition. The constructor takes a binding that it will share with the enclosing scope. </p>
<p>Then the transformer looks at the body of Block 1, to use it to create method definitions on the ClosureDefinition for the abstract methods of the type that the Block is implementing. For Block 1, that&#8217;s run.</p>
<p>It gets to Block 2, and tries to create a ClosureDefinition for it. But that fails, because Block 1 doesn&#8217;t know its own type. It doesn&#8217;t have a reference back to the its ClosureDefinition.</p>
<h3>The Fix</h3>
<p>The error you would get with this was <tt>"undefined method `defining_class' for #&lt;Mirah::AST::Block:0x2f267610&gt;"</tt>. This was because most scoped body types had a defining_class method that pointed at the AST node representing the class they were defined on. Blocks didn&#8217;t. The way I fixed it was by <a href="https://github.com/mirah/mirah/commit/96a90744b5288842f0ee55ecce64d2430345b486#L0R179">adding a defining_class</a> method on Block and <a href="https://github.com/mirah/mirah/commit/96a90744b5288842f0ee55ecce64d2430345b486#L0R160">initializing the instance variable</a> it pointed to with the ClosureDefinition created during the transform step.</p>
<h3>Plans</h3>
<p>This weekend I&#8217;m hoping to either get back to working on Shatner, or to start working on a REPL for Mirah. I&#8217;ve already learned a little bit about how Mirah&#8217;s binding generation works&#8211;to build a REPL, I&#8217;d need to master it.</p>
<h3>New AST</h3>
<p>Also, a few weeks ago I finally <a href="https://github.com/mirah/mirah/commit/46422d33723802f9c5f6afed09ad62ffb87d526d">merged master</a> into the <a href="https://github.com/mirah/mirah/tree/newast">newast</a> branch. The newast branch is where <a href="https://github.com/ribrdb">ribrdb</a> has been working on making Mirah more self-hosted, which should make it much faster, as well as providing a good place to work out some of the edge cases in the language. Merging master is important because otherwise the more experimental branch will diverge too much &#038; become harder to merge back in later.</p>
<p>W00t!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baroquebobcat.com/2012/02/29/mirah-office-hours-closures-in-closures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mirah Office Hours: Digging In</title>
		<link>http://blog.baroquebobcat.com/2012/01/18/mirah-office-hours-digging-in/</link>
		<comments>http://blog.baroquebobcat.com/2012/01/18/mirah-office-hours-digging-in/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 02:05:47 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Mirah]]></category>

		<guid isPermaLink="false">http://blog.baroquebobcat.com/?p=738</guid>
		<description><![CDATA[This week I wanted to deal with one embarrassing bug, and some that looked fun. After looking through everything last week, I felt like I got a better sense about the current issues. So, I picked a few issues to work on that I thought I could dig into and get something working in an [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/baroquebobcat/6723138865/" title="Mirah -v Frankly embarrassing by baroquebobcat, on Flickr"><img src="http://farm8.staticflickr.com/7143/6723138865_a4c3f3a128_o.png" width="421" height="96" alt="Mirah -v Frankly embarrassing"></a></p>
<p>This week I wanted to deal with one embarrassing bug, and some that looked fun. After looking through everything last week, I felt like I got a better sense about the current issues. So, I picked a few issues to work on that I thought I could dig into and get something working in an afternoon. I think I was a little ambitious.</p>
<p>First I wanted to fix <a href="https://github.com/mirah/mirah/issues/161">#161</a> <tt>mirah -v</tt> causing an error because, frankly it&#8217;s embarrassing. I also thought it&#8217;d be pretty easy. <a href="https://github.com/mirah/mirah/commit/1a3da9f25fd9abea76c2aaf880e586d8834d5b09" title="fix 161">It was</a>, but the fix wasn&#8217;t as clean as I&#8217;d like.</p>
<p>Next time I&#8217;m in the option handling code, I&#8217;m going to do some house cleaning&#8211;it&#8217;s getting a bit ugly. For instance, parser should not take a list of commandline args and have to worry about &#8216;-e&#8217; etc. How to reorganize it I haven&#8217;t decided. Maybe using <a href="http://ruby-doc.org/stdlib-1.9.3/libdoc/optparse/rdoc/OptionParser.html">optparse</a>, though that has its own idiosyncrasies.</p>
<p><strong><a href="https://github.com/mirah/mirah/issues/13">#13</a> booleans don&#8217;t have an == method.</strong></p>
<p>I fixed this by <a href="https://github.com/mirah/mirah/commit/d680a3355cd98c7bb1a43cfda9cae21bad799960">adding an intrinsic</a> to the jvm backend code. It&#8217;s not especially pretty. The intrinsics code is tied up in a few files and some of them feel like balls of mud. It&#8217;s not readily apparent where to find things and what they do.</p>
<p>I&#8217;ve been thinking about how to make intrinsics nicer and more consistent. It might be nice to have some common types with a common set of expected method definitions that the various (hypothetical) backends should implement. That way you&#8217;d have some consistency across different Mirah backends.</p>
<p>I&#8217;d also like to reorganize the intrinsics and make their internal APIs easier to grok. Things I&#8217;d like to do with them, like allowing easy method aliasing are hard right now, because the API has a lot of sharp edge cases.</p>
<p><strong>Test cleanup</strong></p>
<p>After fixing #13, I renamed all the test files. I was getting frustrated w/ having them all be prefixed with <tt>test_</tt>. That made tabbing into the right test file take a couple more keystrokes. It added just enough friction that I wanted to change it. So I did.</p>
<p><strong><a href="https://github.com/mirah/mirah/issues/30">#30</a> Const Assign</strong></p>
<p>This one is definitely not a single Sunday afternoon project. And, honestly I didn&#8217;t expect it to be. I spent about an hour trying to figure out what would need to change to support creating constants. It looks kind of annoying.</p>
<p>First, you need to transform the mmeta AST nodes into a Mirah AST node for the Const Assign, which could be as simple as creating a static FieldAssign. Then you need to change the code generation to deal with that. Unfortunately, FieldAssign&#8217;s don&#8217;t currently know about access levels (public, private, protected) which means you&#8217;d either have to add access levels to field assign, or create a new AST class that would have to be dealt with in the code generation phase.</p>
<p>Thinking about it a little, it might be nicer to do the second thing. Then, different (hypothetical) backends could handle access levels for constants their own way. That might be handy, particularly if constants are special in different ways in different languages.</p>
<p><strong><a href="https://github.com/mirah/mirah/issues/41">#41</a> i++</strong></p>
<p>The other thing I looked at briefly was adding ++ to the grammar. This turned out to be rather hard looking because the parser&#8217;s master branch is tied to mirah&#8217;s newast branch, which has a lot of new things in it and doesn&#8217;t work with the current release yet. </p>
<p>I&#8217;m debating creating a new branch on the parser at a point before it started using the new AST. On the other hand, it might make sense to spend more time trying to update the newast branch so that it is up to date with master. Then I could get it closer to merging back in.</p>
<p>That&#8217;s all for this week.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baroquebobcat.com/2012/01/18/mirah-office-hours-digging-in/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mirah Office Hours: Back After the Holidays</title>
		<link>http://blog.baroquebobcat.com/2012/01/10/mirah-office-hours-back-after-the-holidays/</link>
		<comments>http://blog.baroquebobcat.com/2012/01/10/mirah-office-hours-back-after-the-holidays/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 16:29:25 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Mirah]]></category>

		<guid isPermaLink="false">http://blog.baroquebobcat.com/?p=718</guid>
		<description><![CDATA[This week I decided to go and read all of Mirah&#8217;s open issues starting with the earliest submitted ones. I&#8217;d been spending so much time just looking at the top of the list. I didn&#8217;t have a good sense for which ones were duplicates, which were pretty undefined and which were easy. Thankfully, Mirah doesn&#8217;t [...]]]></description>
				<content:encoded><![CDATA[<p>This week I decided to go and read all of Mirah&#8217;s open issues starting with the earliest submitted ones. I&#8217;d been spending so much time just looking at the top of the list. I didn&#8217;t have a good sense for which ones were duplicates, which were pretty undefined and which were easy. Thankfully, Mirah doesn&#8217;t have that many issues, so attempting to read through them all wasn&#8217;t a ridiculous undertaking. There were only 64, and I had filed about a half dozen of them myself.</p>
<p>In the end, I was able to close 7, and get a good sense of where a number of the other ones stood in terms of how much work it&#8217;ll take to fix them. Of course there were a few that made me confused, where I don&#8217;t have any idea where to start digging to fix them. </p>
<p>After going through the stack of issues, I have a better sense of what I want to try to tackle next week. Here&#8217;s a few that I think look fun.</p>
<p><strong><a href="https://github.com/mirah/mirah/issues/41">#41</a> &#8211; adding ++ to the language.</strong></p>
<p>++ isn&#8217;t in the grammar yet, so this would require learning more about the parser. And, I&#8217;d get to play around with the grammar, which is always fun, and something I haven&#8217;t done much of since graduating.</p>
<p><strong><a href="https://github.com/mirah/mirah/issues/127">#127</a> &#038; <a href="https://github.com/mirah/mirah/issues/42">#42</a> &#8211; working out the semantics of equality.</strong></p>
<p>Currently Mirah uses Java&#8217;s ==, which checks identity not equality. We&#8217;d like to use Java&#8217;s equals as our ==, but I&#8217;ve had a little trouble getting it working properly. Still, this is a fun one to hack on.</p>
<p><strong><a href="https://github.com/mirah/mirah/issues/45">#45</a> &#8211; an issue with how field assignments check typing<br />
</strong><br />
Namely, they don&#8217;t really. It isn&#8217;t completely straight forward but I think I&#8217;ve nailed down where to make the changes.</p>
<p><strong><a href="https://github.com/mirah/mirah/issues/57">#57</a> &#8211; who is self in a block</strong></p>
<p>This looks fun. I&#8217;m not quite sure how to make it work. The problem is that you want blocks to work like they do in Ruby, where self is the owner of the outer scope. Currently, Mirah&#8217;s bindings don&#8217;t do that, so self is the instance of the closure. Looking at this got me thinking about non local return(NLR). There was an interesting discussion about it on Twitter today with <a href="http://twitter.com/evanphx">@evanphx</a> saying</p>
<p><a href="http://www.flickr.com/photos/baroquebobcat/6670884691/" title="NLR in closure comment by @evanphx by baroquebobcat, on Flickr"><img src="http://farm8.staticflickr.com/7001/6670884691_673e1f3f78.jpg" width="500" height="225" alt="NLR in closure comment by @evanphx"></a></p>
<p>I think it&#8217;d be really interesting to try to add NLR to Mirah&#8217;s blocks. It might be crazy, but it could also be awesome.</p>
<p><strong><a href="https://github.com/mirah/mirah/issues/69">#69</a> &#8211; Mirah doesn&#8217;t check blocks signature against the signature of the method they&#8217;re supposed to be implementing.<br />
</strong><br />
Madness, but madness I think I can fix.</p>
<p><strong><a href="https://github.com/mirah/mirah/issues/74">#74</a> Constants!</strong></p>
<p>Mirah doesn&#8217;t let you create constants, other than classes and interfaces. I&#8217;d like to change that. And, the error tells you what&#8217;s missing. Now, all I&#8217;ve got to do is figure out how to implement it.</p>
<p><strong>Tune in next week&#8230;</strong></p>
<p>And that&#8217;s what I&#8217;m looking at doing next Sunday. Or some of it anyway. There&#8217;s a bunch of trickiness out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baroquebobcat.com/2012/01/10/mirah-office-hours-back-after-the-holidays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upgrading from Rails 2.3.8 to 2.3.14: find_or_create_by on associations changed</title>
		<link>http://blog.baroquebobcat.com/2012/01/09/upgrading-from-rails-2-3-8-to-2-3-14-find_or_create_by-on-associations-change/</link>
		<comments>http://blog.baroquebobcat.com/2012/01/09/upgrading-from-rails-2-3-8-to-2-3-14-find_or_create_by-on-associations-change/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 03:52:08 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[rails 2.3]]></category>

		<guid isPermaLink="false">http://blog.baroquebobcat.com/?p=729</guid>
		<description><![CDATA[Update: I&#8217;m not the first one to run into this: lighthouse ticket find_or_create-via-has_many-fails-for-hash-parameters and rails/rails#207. Between 2.3.8 and 2.3.9 a change was added to ActiveRecord::Associations::AssociationCollection#method_missing that caused find_or_create_by_* to no longer accept hashes as arguments. An app I was upgrading from 2.3.8 ran into this as it passed hashes to a find_or_create_by_* call. The symptom [...]]]></description>
				<content:encoded><![CDATA[<p><strong>Update:</strong> I&#8217;m not the first one to run into this: <a href="https://rails.lighthouseapp.com/projects/8994/tickets/6147-find_or_create-via-has_many-fails-for-hash-parameters">lighthouse ticket find_or_create-via-has_many-fails-for-hash-parameters</a> and <a href="https://github.com/rails/rails/pull/207">rails/rails#207</a>.<br />
Between 2.3.8 and 2.3.9 a change was added to <tt>ActiveRecord::Associations::AssociationCollection#method_missing</tt> that caused <tt>find_or_create_by_*</tt> to no longer accept hashes as arguments. An app I was upgrading from 2.3.8 ran into this as it passed hashes to a <tt>find_or_create_by_*</tt> call.</p>
<p>The symptom was that <tt>foobar</tt> in <tt>find_or_create_by_foobar</tt> became a serialized hash when saved to the database, instead of the string I was expecting.</p>
<p><script src="https://gist.github.com/1586708.js?file=db_col"></script></p>
<p>The reason it was changed was to ensure that the caches on the collection were updated properly (<a href="https://rails.lighthouseapp.com/projects/8994/tickets/1108">lighthouse 1108</a>) &#038; (<a href="https://github.com/rails/rails/commit/fad166c15277c72b370c90e890d509d0f6c9af63">commit fad166c1</a>), which is pretty important, but the fix didn&#8217;t take into account dealing w/ two of the cases that <tt>find_or_create_by_*</tt> accepts when called on an ActiveRecord class:</p>
<ul>
<li><tt>post.comments.find_or_create_by_body :body => 'bar', :type => 'baz'</tt></li>
<li><tt>post.comments.find_or_create_by_body 'bar', :type => 'baz'</tt></li>
</ul>
<p>The new behavior looked like this:</p>
<p><script src="https://gist.github.com/1586708.js?file=test.rb"></script></p>
<p>I wrote a patch for it (<a href="https://github.com/rails/rails/pull/4331">github.com/rails/rails/pull/4331</a>). Then I found out that 2.3 is only accepting security patches, so I closed it.</p>
<p>If you run into this and you want to use my patch, refer to gist <a href="https://gist.github.com/1586708">1586708</a> which has a monkey patchified version of it. If you want to work around the bug, you can use the optional block to set the attributes you were passing in the hash.</p>
<p><script src="https://gist.github.com/1586708.js?file=workaround.rb"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baroquebobcat.com/2012/01/09/upgrading-from-rails-2-3-8-to-2-3-14-find_or_create_by-on-associations-change/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test ActiveRecord with Reset Transactions Without Rails</title>
		<link>http://blog.baroquebobcat.com/2012/01/09/test-activerecord-with-reset-transactions-without-rails/</link>
		<comments>http://blog.baroquebobcat.com/2012/01/09/test-activerecord-with-reset-transactions-without-rails/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 03:23:58 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.baroquebobcat.com/?p=725</guid>
		<description><![CDATA[I found myself wanting to use Rails&#8217; test transaction functionality in a Rails-less environment, and couldn&#8217;t find a tutorial about it. So I dug into the Rails source to figure out how it worked&#8211;just enough to pull it out into a gist. So, if you add this to your test_helper.rb or equivalent, you too can [...]]]></description>
				<content:encoded><![CDATA[<p>I found myself wanting to use Rails&#8217; test transaction functionality in a Rails-less environment, and couldn&#8217;t find a tutorial about it. So I dug into the Rails source to figure out how it worked&#8211;just enough to pull it out into a gist. So, if you add this to your <tt>test_helper.rb</tt> or equivalent, you too can have your ActiveRecord tests wrapped in transactions that rollback after each case. It also lets you use fixtures, but who does that?</p>
<p><script src="https://gist.github.com/1571808.js?file=test_transactions_without_rails.rb"></script></p>
<noscript>
<code># Snippet to wrap tests with ActiveRecord db transactions outside Rails.<br />
#<br />
# references:<br />
#   https://github.com/rails/rails/blob/master/railties/lib/rails/test_help.rb#L27<br />
#   https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L704<br />
#<br />
# TestFixtures checks whether configurations is blank<br />
# to determine whether to use transactions/fixtures or not.<br />
# which is why we need to set it to something.<br />
ActiveRecord::Base.configurations= {test: {<br />
    database: 'something_test',<br />
    adapter: 'mysql',<br />
    # ...<br />
}}<br />
ActiveRecord::Base.establish_connection ActiveRecord::Base.configurations[:test]<br />
class ActiveSupport::TestCase < ::Test::Unit::TestCase<br />
  include ActiveRecord::TestFixtures<br />
  setup do<br />
    ActiveRecord::IdentityMap.clear<br />
  end<br />
# if you want to use fixtures you can set the fixture path, and it'll load them like rails does.<br />
# self.fixture_path = "my_fixtures"<br />
end</code><br />
</noscript>
]]></content:encoded>
			<wfw:commentRss>http://blog.baroquebobcat.com/2012/01/09/test-activerecord-with-reset-transactions-without-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
