PH Logo
Logo intepreter modeled after UCB Logo.
/Users/paul/Documents/phlogo/core/DataMutatorVerbs.cpp
00001 /*
00002  *  DataSelectorVerbs.cpp
00003  *
00004  *  Author: Paul Hamilton
00005  *      Date: 13 Jun 2011
00006  *
00007  */
00008 
00009 #include "Verbs.h"
00010 
00011 #include "Interpreter.h"
00012 #include "World.h"
00013 #include "Exceptions.h"
00014 #include "List.h"
00015 #include "Thing.h"
00016 #include "Array.h"
00017 
00018 #include <boost/lexical_cast.hpp>
00019 
00020 using namespace std;
00021 using namespace boost;
00022 
00023 namespace phlogo {
00024 
00025 void Verbs::setitem(TokenStream *ts) {
00026 
00027         long index = getLong(ts);
00028                 
00029         pThing array;
00030         _itp->getNextThing(_world, ts, &array);
00031         
00032         pThing thing;
00033         _itp->getNextThing(_world, ts, &thing);
00034 
00035         if (array->isList())
00036                 array->getList().set(index-1, thing);
00037         else if (array->isArray())
00038                 array->getArray().set(index-1, thing);
00039         else
00040                 BOOST_THROW_EXCEPTION( not_lit_or_var_exception() );
00041                 
00042         _world->setResult(array);
00043         
00044 }
00045 
00046 void Verbs::mdsetitem(TokenStream *ts) {
00047 
00048         pThing thing;
00049         _itp->getNextThing(_world, ts, &thing);
00050         if (!thing->isList())
00051                 BOOST_THROW_EXCEPTION( no_list_in_thing_exception() );
00052         
00053         int x = boost::lexical_cast<int>(thing->getList().get(0)->getWord());
00054         int y = boost::lexical_cast<int>(thing->getList().get(1)->getWord());
00055     
00056         pThing array;
00057         _itp->getNextThing(_world, ts, &array);
00058         if (!array->isArray())
00059                 BOOST_THROW_EXCEPTION( no_array_in_thing_exception() );
00060         
00061         _itp->getNextThing(_world, ts, &thing);
00062     
00063         if ((int)array->getArray().size() < x)
00064                 BOOST_THROW_EXCEPTION( bad_index_exception() );
00065         if ((int)array->getArray().get(x-1)->getArray().size() < y)
00066                 BOOST_THROW_EXCEPTION( bad_index_exception() );
00067         
00068     array->getArray().get(x-1)->getArray().get(y-1)->set(thing);
00069 }
00070 
00071 void Verbs::setfirst(TokenStream *ts) {
00072 
00073         pThing array;
00074         _itp->getNextThing(_world, ts, &array);
00075         if (!array->isArray())
00076                 BOOST_THROW_EXCEPTION( no_array_in_thing_exception() );
00077         
00078         pThing thing;
00079         _itp->getNextThing(_world, ts, &thing);
00080     
00081         if ((int)array->getArray().size() < 1)
00082                 BOOST_THROW_EXCEPTION( bad_index_exception() );
00083     
00084     array->getArray().get(0)->set(thing);
00085 }
00086 
00087 void Verbs::setbf(TokenStream *ts) {
00088 
00089         pThing array;
00090         _itp->getNextThing(_world, ts, &array);
00091         if (!array->isArray())
00092                 BOOST_THROW_EXCEPTION( no_array_in_thing_exception() );
00093         
00094         pThing thing;
00095         _itp->getNextThing(_world, ts, &thing);
00096         if (!thing->isArray())
00097                 BOOST_THROW_EXCEPTION( no_array_in_thing_exception() );
00098     
00099     pThing first = array->getArray().get(0);
00100     Array a = array->allocArray(*(thing->getArray().getVector()));
00101     a.push_front(first);
00102 }
00103 
00104 void Verbs::push(TokenStream *ts) {
00105         
00106         pThing list;
00107         _itp->getNextThing(_world, ts, &list);
00108         if (!list->isList())
00109                 BOOST_THROW_EXCEPTION( no_list_in_thing_exception() );
00110         
00111         pThing thing;
00112         _itp->getNextThing(_world, ts, &thing);
00113 
00114     list->getList().push_front(thing);
00115 }
00116 
00117 void Verbs::pop(TokenStream *ts) {
00118 
00119         pThing list;
00120         _itp->getNextThing(_world, ts, &list);
00121         if (!list->isList())
00122                 BOOST_THROW_EXCEPTION( no_list_in_thing_exception() );
00123         
00124     _world->setResult(list->getList().pop_front());
00125     
00126 }
00127 
00128 void Verbs::queue(TokenStream *ts) {
00129 
00130         pThing list;
00131         _itp->getNextThing(_world, ts, &list);
00132         if (!list->isList())
00133                 BOOST_THROW_EXCEPTION( no_list_in_thing_exception() );
00134         
00135         pThing thing;
00136         _itp->getNextThing(_world, ts, &thing);
00137     
00138     list->getList().push_back(thing);
00139 }
00140 
00141 void Verbs::dequeue(TokenStream *ts) {
00142 
00143         pThing list;
00144         _itp->getNextThing(_world, ts, &list);
00145         if (!list->isList())
00146                 BOOST_THROW_EXCEPTION( no_list_in_thing_exception() );
00147         
00148     _world->setResult(list->getList().pop_back());
00149     
00150 }
00151 
00152 }
 All Classes Functions