PH Logo
Logo intepreter modeled after UCB Logo.
/Users/paul/Documents/phlogo/core/Operator.cpp
00001 /*
00002  *  Operator.cpp
00003  *
00004  *  Author: Paul Hamilton
00005  *      Date: 14 Aug 2011
00006  *
00007  */
00008 #include "Operator.h"
00009         
00010 #include "World.h"
00011 #include "Interpreter.h"
00012 #include "TokenStream.h"
00013 #include "Thing.h"
00014 #include "Exceptions.h"
00015 
00016 #include <boost/lexical_cast.hpp>
00017 #include <math.h>
00018 
00019 using namespace std;
00020 using namespace boost;
00021 
00022 namespace phlogo {
00023 
00024 void Operator::doOp(Interpreter *itp, World *world, TokenStream *ts, long n1) {
00025     
00026     pThing thing;
00027     itp->getNextThing(world, ts, &thing);
00028     if (thing->isWord()) {
00029         try {
00030             op(n1, lexical_cast<long>(thing->getWord()));
00031         }
00032         catch (bad_cast &x) {
00033             try {
00034                 op(n1, lexical_cast<double>(thing->getWord()));
00035             }
00036             catch (bad_cast &x) {
00037                 BOOST_THROW_EXCEPTION( no_word_in_thing_exception() );
00038             }
00039         }
00040     }
00041     else if (thing->isLong()) {
00042         op(n1, thing->getLong());
00043     }
00044     else if (thing->isDouble()) {
00045         op(n1, thing->getDouble());
00046     }
00047     else
00048         BOOST_THROW_EXCEPTION( no_word_in_thing_exception() );
00049 }
00050 
00051 void Operator::doOp(Interpreter *itp, World *world, TokenStream *ts, double n1) {
00052     
00053     pThing thing;
00054     itp->getNextThing(world, ts, &thing);
00055     if (thing->isWord()) {
00056         try {
00057             op(n1, lexical_cast<long>(thing->getWord()));
00058         }
00059         catch (bad_cast &x) {
00060             try {
00061                 op(n1, lexical_cast<double>(thing->getWord()));
00062             }
00063             catch (bad_cast &x) {
00064                 BOOST_THROW_EXCEPTION( no_word_in_thing_exception() );
00065             }
00066         }
00067     }
00068     else if (thing->isLong()) {
00069         op(n1, thing->getLong());
00070     }
00071     else if (thing->isDouble()) {
00072         op(n1, thing->getDouble());
00073     }
00074     else
00075         BOOST_THROW_EXCEPTION( no_word_in_thing_exception() );
00076 }
00077 
00078 void Operator::doOp(Interpreter *itp, World *world, TokenStream *ts) {
00079     
00080     pThing thing;
00081     itp->getNextThing(world, ts, &thing);
00082     if (thing->isWord()) {
00083         try {
00084             doOp(itp, world, ts, lexical_cast<long>(thing->getWord()));
00085         }
00086         catch (bad_cast &x) {
00087             try {
00088                 doOp(itp, world, ts, lexical_cast<double>(thing->getWord()));
00089             }
00090             catch (bad_cast &x) {
00091                 BOOST_THROW_EXCEPTION( no_word_in_thing_exception() );
00092             }
00093         }
00094     }
00095     else if (thing->isLong()) {
00096         doOp(itp, world, ts, thing->getLong());
00097     }
00098     else if (thing->isDouble()) {
00099         doOp(itp, world, ts, thing->getDouble());
00100     }
00101     else
00102         BOOST_THROW_EXCEPTION( no_word_in_thing_exception() );
00103 }
00104 
00105 void Mul::op(long n1, long n2) {
00106     _world->allocResult()->set((long)(n1 * n2));
00107 }
00108 void Mul::op(double n1, long n2) {
00109     _world->allocResult()->set((double)(n1 * n2));
00110 }
00111 void Mul::op(long n1, double n2) {
00112     _world->allocResult()->set((double)(n1 * n2));
00113 }
00114 void Mul::op(double n1, double n2) {
00115     _world->allocResult()->set((double)(n1 * n2));
00116 }
00117 
00118 void Div::op(long n1, long n2) {
00119     _world->allocResult()->set((double)n1 / (double)n2);
00120 }
00121 void Div::op(double n1, long n2) {
00122     _world->allocResult()->set(n1 / (double)n2);
00123 }
00124 void Div::op(long n1, double n2) {
00125     _world->allocResult()->set((double)n1 / n2);
00126 }
00127 void Div::op(double n1, double n2) {
00128     _world->allocResult()->set(n1 / n2);
00129 }
00130 
00131 void Add::op(long n1, long n2) {
00132     _world->allocResult()->set(n1 + n2);
00133 }
00134 void Add::op(double n1, long n2) {
00135     _world->allocResult()->set(n1 + n2);
00136 }
00137 void Add::op(long n1, double n2) {
00138     _world->allocResult()->set(n1 + n2);
00139 }
00140 void Add::op(double n1, double n2) {
00141     _world->allocResult()->set(n1 + n2);
00142 }
00143 
00144 void Sub::op(long n1, long n2) {
00145     _world->allocResult()->set(n1 - n2);
00146 }
00147 void Sub::op(double n1, long n2) {
00148     _world->allocResult()->set(n1 - n2);
00149 }
00150 void Sub::op(long n1, double n2) {
00151     _world->allocResult()->set(n1 - n2);
00152 }
00153 void Sub::op(double n1, double n2) {
00154     _world->allocResult()->set(n1 - n2);
00155 }
00156     
00157 }
 All Classes Functions