<?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>ActionScript 3.0 Design Patterns &#187; Interpreter</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/interpreter-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:00:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ActionScript 3.0 Interpreter Design Pattern: A PostScript Tutor</title>
		<link>http://www.as3dp.com/2008/12/actionscript-30-interpreter-design-pattern-a-postscript-tutor/</link>
		<comments>http://www.as3dp.com/2008/12/actionscript-30-interpreter-design-pattern-a-postscript-tutor/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 22:57:11 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Interpreter]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=353</guid>
		<description><![CDATA[ActionScript 3.0 Interpreter Design Pattern Back in the day I used to do a lot of FORTH programming. For about 10 years I’d attend a weekly meeting of FORTH programming engineers at General Dynamics in San Diego and marvel at what they could do with this postfix language. (Postfix is often called reverse Polish notation [...]]]></description>
			<content:encoded><![CDATA[<p><strong>ActionScript 3.0 Interpreter Design Pattern</strong></p>
<p>Back in the day I used to do a lot of FORTH programming. For about 10 years I’d attend a weekly meeting of FORTH programming engineers at General Dynamics in San Diego and marvel at what they could do with this <em>postfix</em> language. (Postfix is often called <strong>reverse Polish notation</strong> after the Polish mathematician Jan Łukasiewicz who developed the mathematical notation placing the operator <em>after </em> the operands.) The following shows an example of this notation:</p>
<p><strong>30 2 * 4 +</strong><br />
 <strong>Result: 64</strong></p>
<p>Later, when Adobe came out with PostScript and Apple came out with their first laser printer, I was able to program in PostScript using a text editor and shooting it directly to the printer. Out would come these fabulous fonts, drawings and whatever else I had programmed in. Because PostScript used a similar postfix notation as FORTH, I was allowed to do a FORTH newsletter in PostScript. Only a few were produced since it was no easy task to develop a newsletter in code using Notepad! (Click <a href="http://www.sandlight.com/postscript/" target="_blank">here</a> to see the final PostScript calculator that this application generates. It works with PostScript data entry &#8212; use <strong>add</strong>, <strong>sub</strong>, <strong>mul</strong>, and <strong>div</strong> for <strong>+</strong>, <strong>-</strong>, <strong>*</strong>, and <strong>/</strong> respectively.)</p>
<p><strong>Interpret This!</strong></p>
<p>The Interpreter design pattern as described by Gamma, <em>et al</em> can be used to express instances of a recurring problem as sentences. Interpreting the sentences then solves the problem.  Right off the bat I was thinking, <em>¿como está usted?</em> translates to <em>how’s it going?</em> but that’s not exactly what GoF had in mind. Any spoken language is a bit too big for this kind of interpreter. Instead, the pattern describes how to define a grammar for simple languages. Gamma and his associates use the example of <strong>regular expressions</strong>, so loved by Perl programmers.</p>
<p>So when I went looking at some Interpreter examples, besides regular expressions, I found converters from Roman numerals to regular numbers, a Boolean language, a musical note interpreter from <em>do, rey, me, fa, so, la, ti </em> to Hertz (Hz) values, calculations using postfix notations and some other fairly modest examples. Because of my experiences with postfix languages, I decided to do one that set up as a PostScript data entry that would resolve to an outcome (solution) to the results of a postfix statement. I decided on PostScript whose math operators are word-like and not symbols. (e.g., Instead of using <strong>/</strong> for division, it uses <strong>div</strong>.) This would be a little more than the usual minimalist example since the user can use it to practice and learn PostScript math entries.</p>
<p><strong>Interpreter Design Pattern Formal Features</strong></p>
<p>Figure 1 provides an overview of the Interpreter pattern. Note that the Client is part of the pattern (instead of implied or not at all). Also note that the Client holds references to both the <strong>Context</strong> class and <strong>AbstractExpression</strong> interface.</p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2008/12/interpreter.png"><img class="alignnone size-full wp-image-355" title="interpreter" src="http://www.as3dp.com/wp-content/uploads/2008/12/interpreter.png" alt="" width="499" height="293" /></a></p>
<p><em><strong>Figure 1: </strong>Interpreter Class Diagram</em></p>
<p>Of all of the design patterns I’ve seen, this one has the most loose ends. Any statement put into a string and then interpreted generally requires some kind of parsing. GoF note the need for parsing and point out it can be from a table driven source, a recursive descent (or some other hand-crafted parser) or in the Client. The Context class is used as a global entry point for the Interpreter.  It’s probably heresy to do so, but I decided to put the parser in the Context. The Client sends the request to the Context and indirectly to the AbstractExpression. The Context class still acts as a global entry point, but it also parses the data (statement in PostScript notation), and the Terminal Expression classes still do the actual interpretation. I don’t see it as adding tighter coupling.  (If the parser were placed in the Client, I suppose it would be a purer version of the design pattern. If you think so, I’d like to see your implementation, and I’d be glad to put it on this blog.) Also, like most of the examples that I saw, this one does not include a NonterminalExpression. That is because I did not need alternation, repetition or sequence expressions.</p>
<p>The good news is that this implementation of the design pattern is wonderfully flexible and easy to update. If I wanted to change the language example from PostScript to something like Scheme, the pattern would be able to handle it with ease. Eventually, I’d like to re-do it with the parser in the Client and add several PostScript drawing commands by adding more terminal expression classes. I’d also like to add a NonterminalExpression for handling certain repetitions and sequences.<br />
 <span id="more-353"></span></p>
<p><strong>Building a PostScript Learning Tool</strong></p>
<p>Before going any further there are a couple of things you need to know. First, I used the Vector class in the examples. In order to use Vector classes you need Flash CS4. It does not work with Flex 3 either.  Also, you’ll the need Flash 10 player. (You may find an updated pre-release version of Flex at <strong> Adobe Labs</strong> that might work.) If none of those options are available to you, just change all of the Vector instances to Arrays. Why the Vector class rather than the Array class? Vectors have typed elements and Arrays don’t.</p>
<p>The goal of this Interpreter is to create an application that will take a string statement ordered in the correct PostScript format and display accurate results. So for example, if you enter,</p>
<p><strong>7 10 mul 20 add</strong></p>
<p>you will be returned,</p>
<p><strong>90</strong></p>
<p>It will be able to work through addition, subtraction, division and multiplication using PostScript formatting.</p>
<p><strong>The Terminal Expression Classes</strong></p>
<p>To get started, you will need classes to interpret the statements you enter. To do so, each of the elements of the entry must be interpreted to behave in a certain way. The parser (placed in the Context) divides up the string statement and sends each element to be evaluated by the terminal expression classes. The terminal class then does something, depending on whether the element is a number or one of the four arithmetic operations. If entered in the correct sequence, everything will work as it should. However, if not, the result will be unexpected. For example, if the user enters,</p>
<p><strong>2 4 6 8 add</strong></p>
<p>expecting that all the numbers will be added, he should find that only the last two numbers are added. Were he using PostScript, that’s what he should see as well. So, each of the four different operators have to have a separate class as do the numbers that are added to the Vector class used in parsing. The following six classes show the AbstractExperssion interface and the five terminal classes.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p353code3'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3533"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
</pre></td><td class="code" id="p353code3"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Abstract expression interface</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> AbExpression
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">function</span> interpret<span style="color: #66cc66;">&#40;</span>v:Vector.<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Terminal class for addition</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AddTermExp <span style="color: #0066CC;">implements</span> AbExpression
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> interpret<span style="color: #66cc66;">&#40;</span>v:Vector.<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               v.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> v.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + v.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
          <span style="color: #66cc66;">&#125;</span>
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Terminal class for subtraction</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SubtractTermExp <span style="color: #0066CC;">implements</span> AbExpression
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> interpret<span style="color: #66cc66;">&#40;</span>v:Vector.<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               v.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> -v.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + v.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Terminal class for multiplication</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MultiplyTermExp <span style="color: #0066CC;">implements</span> AbExpression
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> interpret<span style="color: #66cc66;">&#40;</span>v:Vector.<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               v.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> v.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> v.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
          <span style="color: #66cc66;">&#125;</span>
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Terminal class for division</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DivideTermExp <span style="color: #0066CC;">implements</span> AbExpression
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> interpret<span style="color: #66cc66;">&#40;</span>v:Vector.<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               <span style="color: #000000; font-weight: bold;">var</span> v1:<span style="color: #0066CC;">Number</span>=v.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
               <span style="color: #000000; font-weight: bold;">var</span> v2:<span style="color: #0066CC;">Number</span>=v.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
               v.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> v2 <span style="color: #66cc66;">/</span> v1 <span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Terminal class for numbers</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #000000; font-weight: bold;">class</span> ValueTermExp <span style="color: #0066CC;">implements</span> AbExpression
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> num:<span style="color: #0066CC;">Number</span>;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ValueTermExp<span style="color: #66cc66;">&#40;</span>num:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#123;</span>
               <span style="color: #0066CC;">this</span>.<span style="color: #006600;">num</span> = num;
          <span style="color: #66cc66;">&#125;</span>
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> interpret<span style="color: #66cc66;">&#40;</span>v:Vector.<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               v.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>num<span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>By using the <strong>pop()</strong> method with the Vector holding the parsed elements of the PostScript statement, we’re able to follow the LIFO (Last In First Out) sequence, which just happens to be the same sequence of operations followed by postfix languages. Were we using some language like Schema, we’d have to use different algorithms because the operators precede the operands, but when you stop to think about it, that’s not too difficult to make the changes. (We’d also have to so something about those parentheses!) Instead of having to re-structure the whole program, all you have to do is to make changes to the terminal classes.</p>
<p><strong>The Client and Context</strong></p>
<p>The Client is a big part of the Interpreter pattern. In fact, it is one of the recommended places for the syntax tree and parsing operations, but in this case it requests the parsing operations from the Context class. The Context class sets up and maintains the parsing sent to it by the Client class. This part of the design is a bit problematic because the parser is supposed to go in the Client and the Context is meant to be a global access point for the Interpreter pattern. However, setting it up as I did makes sense to me, and if anyone can find any violated OOP, I’ll go about fixing it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p353code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3534"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
</pre></td><td class="code" id="p353code4"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Client class</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
     <span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
     <span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldType</span>;
     <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
     <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> expression:<span style="color: #0066CC;">String</span>;
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> input:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> output:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> calculate:Calculate=<span style="color: #000000; font-weight: bold;">new</span> Calculate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#123;</span>
               input.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #006600;">INPUT</span>;
               input.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">140</span>,input.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">80</span>;
               input.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">120</span>,input.<span style="color: #0066CC;">height</span> = <span style="color: #cc66cc;">20</span>;
               input.<span style="color: #0066CC;">borderColor</span> = 0xcccccc;
               input.<span style="color: #0066CC;">border</span> = <span style="color: #000000; font-weight: bold;">true</span>;
               addChild<span style="color: #66cc66;">&#40;</span>input<span style="color: #66cc66;">&#41;</span>;
&nbsp;
               calculate.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">270</span>,calculate.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">75</span>;
               addChild<span style="color: #66cc66;">&#40;</span>calculate<span style="color: #66cc66;">&#41;</span>;
&nbsp;
               calculate.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,showResults<span style="color: #66cc66;">&#41;</span>;
               output.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
               output.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">140</span>,output.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">110</span>;
               addChild<span style="color: #66cc66;">&#40;</span>output<span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
&nbsp;
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> showResults<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#123;</span>
               expression = input.<span style="color: #0066CC;">text</span>;
               context = <span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span>expression<span style="color: #66cc66;">&#41;</span>;
               output.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;Result: &quot;</span> + context.<span style="color: #006600;">evaluate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Context class with Parser</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Context
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tree:Vector. = <span style="color: #000000; font-weight: bold;">new</span> Vector.  ;
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Vector. = <span style="color: #000000; font-weight: bold;">new</span> Vector.  ;
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> current:<span style="color: #0066CC;">Number</span>;
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> expElement:<span style="color: #0066CC;">String</span>;
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> arr:<span style="color: #0066CC;">Array</span>;
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Context<span style="color: #66cc66;">&#40;</span>statement:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#123;</span>
               arr = statement.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>;
               <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> piece:<span style="color: #0066CC;">String</span> <span style="color: #b1b100;">in</span> statement.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #808080; font-style: italic;">//el will be an element number</span>
                    <span style="color: #000000; font-weight: bold;">var</span> el = <span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span>piece<span style="color: #66cc66;">&#41;</span>;
                    <span style="color: #808080; font-style: italic;">//expElement is an expression element</span>
                    expElement = arr<span style="color: #66cc66;">&#91;</span>piece<span style="color: #66cc66;">&#93;</span>;
&nbsp;
                    <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>expElement<span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#123;</span>
                         <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;add&quot;</span> :
                              tree.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> AddTermExp  <span style="color: #66cc66;">&#41;</span>;
                              <span style="color: #b1b100;">break</span>;
&nbsp;
                         <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;sub&quot;</span> :
                              tree.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SubtractTermExp  <span style="color: #66cc66;">&#41;</span>;
                              <span style="color: #b1b100;">break</span>;
&nbsp;
                         <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;div&quot;</span> :
                              tree.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DivideTermExp  <span style="color: #66cc66;">&#41;</span>;
                              <span style="color: #b1b100;">break</span>;
&nbsp;
                         <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;mul&quot;</span> :
                              tree.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MultiplyTermExp  <span style="color: #66cc66;">&#41;</span>;
                              <span style="color: #b1b100;">break</span>;
&nbsp;
                         <span style="color: #000000; font-weight: bold;">default</span> :
                              tree.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ValueTermExp<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span>expElement<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                    <span style="color: #66cc66;">&#125;</span>
               <span style="color: #66cc66;">&#125;</span>
          <span style="color: #66cc66;">&#125;</span>
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> evaluate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
          <span style="color: #66cc66;">&#123;</span>
               <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">e</span>:AbExpression <span style="color: #b1b100;">in</span> tree<span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #0066CC;">e</span>.<span style="color: #006600;">interpret</span><span style="color: #66cc66;">&#40;</span>context<span style="color: #66cc66;">&#41;</span>;
               <span style="color: #66cc66;">&#125;</span>
               current = context.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
               <span style="color: #b1b100;">return</span> current;
          <span style="color: #66cc66;">&#125;</span>
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The way this Interpreter is set up in the example is pretty simple. The Client sends a request to the Context in the form of a string to be parsed. The Context class sends each part of the string to the terminal classes for a conversion to a number or operation. It then evaluates the Vector tree and returns the value to the client.</p>
<p><strong>More than Meets the Eye</strong></p>
<p>In the articles on the Interpreter design pattern, some of the authors grumbled about it as being rarely used, blew smoke, or cobbled together a sort-of Interpreter example. I can’t remember a decent explanation of the non-terminal expression class that did more than paraphrase exactly what the Gang of Four had said. In one example, the client could send a request to the NonTerminal Expression class but it did nothing. Most of the examples (including mine) reminded me of some lines from an old Jim Croche song, <em>Bad, bad, Leroy Brown</em>. To wit:</p>
<blockquote><p>Leroy [The Interpreter] looked like a jigsaw puzzle<br />
 With a couple of pieces gone</p>
</blockquote>
<p>This is one of those design patterns to which I would like to return. Much of the core of the pattern lies in the parser, and I’d like to better flesh out the non-terminal expression, both as a concept and as a class. Most likely I will add the parser to the Client, and because the Client is a full participant in the Interpreter pattern, there’s no reason not to do so. Any code or concepts from the readers on this aspect of the Interpreter would be most welcome.</p>
<p><strong>Getting Started in PostScript </strong></p>
<p>Figure 2 shows what you will see when you run the program:</p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2008/12/interpretps1.png"><img class="alignnone size-full wp-image-357" title="interpretps1" src="http://www.as3dp.com/wp-content/uploads/2008/12/interpretps1.png" alt="" width="394" height="160" /></a></p>
<p><em><strong>Figure 2:</strong>Coding in PostScript</em></p>
<p>The postfix notation is a stack-based way of working. Some calculators use postfix as well, but for the most part you don’t see it much any more. Before any processing is done, however, most languages rearrange entries so that they are set up in postfix order for better processing speed. Using this little application you can easily simulate working in a PostScript math environment. If you want the full PostScript language tutorial, you can download the book published in 1985 by Adobe at:</p>
<p>http://www-cdf.fnal.gov/offline/PostScript/BLUEBOOK.PDF</p>
<p>Then, as long as you stick with simple math operations, you can practice your PostScript skills <a href="http://www.sandlight.com/postscript">here</a>. The zipped file with all of the elements that make up the application can be found <a href="http://nemo.mwd.hartford.edu/~wsanders/PostScriptInterpreter.zip">here</a>.</p>
<p>The rules are simple. You add one or two values and an operator:</p>
<ul>
<li>add</li>
<li>sub</li>
<li>div</li>
<li>mul</li>
</ul>
<p>Then click Calculate. If you can predict what you’ll get before you click the Calculate button, then you’ve got it!</p>
<p>Wouldn’t it be fun to add more PostScript commands and then write terminal classes for the different commands? We’d really like to see anything you cook up with this. So get to coding and have some fun!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2008%2F12%2Factionscript-30-interpreter-design-pattern-a-postscript-tutor%2F&amp;title=ActionScript%203.0%20Interpreter%20Design%20Pattern%3A%20A%20PostScript%20Tutor" id="wpa2a_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2008/12/actionscript-30-interpreter-design-pattern-a-postscript-tutor/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

