<?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>Floorplanner Tech Blog &#187; syntax tree</title>
	<atom:link href="http://techblog.floorplanner.com/tag/syntax-tree/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.floorplanner.com</link>
	<description>Our latest geek adventures!</description>
	<lastBuildDate>Tue, 16 Mar 2010 18:45:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Evaluating static ParseTree subtrees</title>
		<link>http://techblog.floorplanner.com/2009/10/03/evaluating-static-parsetree-subtrees/</link>
		<comments>http://techblog.floorplanner.com/2009/10/03/evaluating-static-parsetree-subtrees/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 12:34:21 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[evaluate]]></category>
		<category><![CDATA[ParseTree]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[syntax tree]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=734</guid>
		<description><![CDATA[ParseTree is a very useful to gem that can translate Ruby code into a syntax tree. I recently needed to evaluate a static part of this tree to return the original hash it represented. I wrote a simple method called ParseTree.eval_static_tree for this purpose. 
The method can only evaluate trees that have a static value [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://parsetree.rubyforge.org/">ParseTree</a> is a very useful to gem that can translate Ruby code into a syntax tree. I recently needed to evaluate a static part of this tree to return the original hash it represented. I wrote a simple method called <code>ParseTree.eval_static_tree</code> for this purpose. </p>
<p>The method can only evaluate trees that have a static value composed of hashes, arrays, strings, symbols and numerics. You can however pass a block to the function that will be called for every dynamic part the method encounters (function calls, etc.) </p>
<p><script src="http://gist.github.com/200607.js"></script></p>
<p>A quick sample on how to use it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">code = <span style="color:#996600;">'{ :static_array =&gt; [&quot;str&quot;, 123, 4.5], :dynamic =&gt; method_call }'</span>
tree = ParseTree.<span style="color:#9900CC;">translate</span><span style="color:#006600; font-weight:bold;">&#40;</span>code<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; [:hash, [:lit, :static_array], [:array, [:str, &quot;str&quot;], </span>
<span style="color:#008000; font-style:italic;">#       [:lit, 123], [:lit, 4.5]], [:lit, :dynamic], [:vcall, :method_call]]</span>
&nbsp;
ParseTree.<span style="color:#9900CC;">eval_static_tree</span><span style="color:#006600; font-weight:bold;">&#40;</span>tree<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># =&gt; RuntimeError: tree is not static: :vcall ...</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Pass a block to simply return nil for every dynamic item in the tree.</span>
ParseTree.<span style="color:#9900CC;">eval_static_tree</span><span style="color:#006600; font-weight:bold;">&#40;</span>tree<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>dynamic_subtree<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># =&gt; {:dynamic=&gt;nil, :static_array=&gt;[&quot;str&quot;, 123, 4.5]}</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/10/03/evaluating-static-parsetree-subtrees/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
