<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -2,7 +2,7 @@
 % info go to &lt;URL:http://en.wikipedia.org/wiki/Shunting_yard_algorithm&gt;.
 
 -module(calc).
--export([parse/1, evaluate/1, stringify/1]).
+-export([parse/1, evaluate/1, stringify/1, compile/1, simulate/1, simplify/1]).
 
 parse(String) -&gt; parse(tokenize(String), [], []).
 
@@ -60,6 +60,80 @@ stringify({Oper, Exp0, Exp1}) -&gt;
     divide    -&gt; &quot;(&quot; ++ stringify(Exp0) ++ &quot;/&quot; ++ stringify(Exp1) ++ &quot;)&quot;
   end.
 
+compile(Exp) -&gt; compile(Exp, []).
+
+compile({num, Num}, Stack) -&gt;
+  [{push, Num}|Stack];
+compile({Oper, Exp}, Stack) -&gt;
+  compile(Exp, [Oper|Stack]);
+compile({Oper, Exp0, Exp1}, Stack) -&gt;
+  compile(Exp0, compile(Exp1, [Oper|Stack])).
+
+simulate(Instructions) -&gt; simulate(Instructions, []).
+
+simulate([], [Top]) -&gt;
+  Top;
+simulate([{push, Num}|Instructions], Stack) -&gt;
+  simulate(Instructions, [Num|Stack]);
+simulate([uminus|Instructions], [Stack0|Stack]) -&gt;
+  simulate(Instructions, [-Stack0|Stack]);
+simulate([Oper|Instructions], [Stack0|[Stack1|Stack]]) -&gt;
+  case Oper of
+    add       -&gt; simulate(Instructions, [Stack1 + Stack0|Stack]);
+    subtract  -&gt; simulate(Instructions, [Stack1 - Stack0|Stack]);
+    multiply  -&gt; simulate(Instructions, [Stack1 * Stack0|Stack]);
+    divide    -&gt; simulate(Instructions, [Stack1 / Stack0|Stack])
+  end.
+
+simplify({num, Num})                        -&gt; {num, Num};
+simplify({add, Exp0, {uminus, Exp1}})       -&gt; simplify({subtract, simplify(Exp0), simplify(Exp1)});
+simplify({subtract, Exp0, {uminus, Exp1}})  -&gt; simplify({add, simplify(Exp0), simplify(Exp1)});
+simplify({uminus, Exp}) -&gt;
+  Sexp = simplify(Exp),
+  case Sexp of
+    {uminus, NestedExp} -&gt; NestedExp;
+    _                   -&gt; {uminus, Sexp}
+  end;
+simplify({add, Exp0, Exp1}) -&gt;
+  Sexp0 = simplify(Exp0),
+  Sexp1 = simplify(Exp1),
+  if
+    Sexp0 == {num, 0} -&gt; Sexp1;
+    Sexp1 == {num, 0} -&gt; Sexp0;
+    true              -&gt; {add, Sexp0, Sexp1}
+  end;
+simplify({subtract, Exp0, Exp1}) -&gt;
+  Sexp0 = simplify(Exp0),
+  Sexp1 = simplify(Exp1),
+  if
+    Sexp0 == {num, 0} -&gt; {uminus, Sexp1};
+    Sexp1 == {num, 0} -&gt; Sexp0;
+    Sexp0 == Sexp1    -&gt; {num, 0};
+    true              -&gt; {subtract, Sexp0, Sexp1}
+  end;
+simplify({multiply, Exp0, Exp1}) -&gt;
+  Sexp0 = simplify(Exp0),
+  Sexp1 = simplify(Exp1),
+  if
+    Sexp0 == {num, 0}; Sexp1 == {num, 0}  -&gt; {num, 0};
+    Sexp0 == {num, 1}                     -&gt; Sexp1;
+    Sexp1 == {num, 1}                     -&gt; Sexp0;
+    true                                  -&gt; {multiply, Sexp0, Sexp1}
+  end;
+simplify({divide, Exp0, Exp1}) -&gt;
+  Sexp0 = simplify(Exp0),
+  Sexp1 = simplify(Exp1),
+  if
+    Sexp0 == Sexp1    -&gt; {num, 1};
+    Sexp0 == {num, 0} -&gt; {num, 0};
+    Sexp1 == {num, 1} -&gt; Sexp0;
+    true              -&gt; {divide, Sexp0, Sexp1}
+  end;
+simplify({Oper, Exp}) -&gt;
+  {Oper, simplify(Exp)};
+simplify({Oper, Exp0, Exp1}) -&gt;
+  {Oper, simplify(Exp0), simplify(Exp1)}.
+
 tokenize([]) -&gt; [];
 tokenize([H|T]) -&gt;
   if</diff>
      <filename>chapter3/calc.erl</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>fc1bb07e306a396b804a954f022c6fc32a73c7b4</id>
    </parent>
  </parents>
  <author>
    <name>John Parker</name>
    <email>jparker@urgetopunt.com</email>
  </author>
  <url>http://github.com/jparker/erlang_programming/commit/706baece6670d57706d7587c0d7cbbe40be039b0</url>
  <id>706baece6670d57706d7587c0d7cbbe40be039b0</id>
  <committed-date>2009-10-08T22:53:25-07:00</committed-date>
  <authored-date>2009-10-08T22:53:25-07:00</authored-date>
  <message>Implemented compile(), simulate() and simplify() functions</message>
  <tree>228adab825ca6641db4aa69cc3b8e05f854f20cd</tree>
  <committer>
    <name>John Parker</name>
    <email>jparker@urgetopunt.com</email>
  </committer>
</commit>
