PH Logo
Logo intepreter modeled after UCB Logo.
/Users/paul/Documents/phlogo/core/WorkspaceVerbs.cpp
00001 /*
00002  *  PredicateVerbs.cpp
00003  *
00004  *  Author: Paul Hamilton
00005  *      Date: 17 Aug 2011
00006  *
00007  */
00008 
00009 #include "Verbs.h"
00010 
00011 #include "Thing.h"
00012 #include "World.h"
00013 #include "TokenStream.h"
00014 #include "Scope.h"
00015 #include "Interpreter.h"
00016 #include "Exceptions.h"
00017 
00018 #include <math.h>
00019 
00020 using namespace std;
00021 using namespace boost;
00022 
00023 namespace phlogo {
00024     
00025 void getVerbs(List *l, Verbs *v) {
00026     
00027         std::map<std::string, std::string>::const_iterator i = v->getVerbTokenMap().begin();
00028         for (; i != v->getVerbTokenMap().end(); i++) {
00029         pThing t(new Thing(i->first));
00030         l->add(t);
00031     }
00032 }
00033     
00034 void getFunctions(List *l, Verbs *v) {
00035     
00036     std::map<std::string, std::string>::const_iterator i = v->getFuncTokenMap().begin();
00037     for (; i != v->getFuncTokenMap().end(); i++) {
00038         pThing t(new Thing(i->first));
00039         l->add(t);
00040     }
00041 }
00042 
00043 void getVariables(List *l, World *w) {
00044     
00045     Scope *s = w->getScope();
00046         std::map<std::string, pThing >::const_iterator i = s->begin();
00047         for (; i != s->end(); i++) {
00048         pThing t(new Thing(i->first));
00049         l->add(t);
00050     }
00051 }
00052 
00053 void Verbs::contents(TokenStream *ts) {
00054     
00055     pThing t(new Thing());
00056     List l = t->allocList();
00057     {
00058         pThing t1(new Thing());
00059         List l1 = t1->allocList();
00060         getVerbs(&l1, this);
00061         getFunctions(&l1, this);
00062         l.add(t1);
00063     }
00064     {
00065         pThing t1(new Thing());
00066         List l1 = t1->allocList();
00067         getVariables(&l1, _world);
00068         l.add(t1);
00069     }
00070     {
00071         pThing t1(new Thing());
00072         List l1 = t1->allocList();
00073         l.add(t1);
00074     }
00075     _world->setResult(t);
00076     
00077 }
00078     
00079 void Verbs::procedures(TokenStream *ts) {
00080     
00081     pThing t(new Thing());
00082     List l = t->allocList();
00083     getFunctions(&l, this);
00084    _world->setResult(t);
00085     
00086 }
00087 void Verbs::primitives(TokenStream *ts) {
00088     
00089     pThing t(new Thing());
00090     List l = t->allocList();
00091     getVerbs(&l, this);
00092     _world->setResult(t);
00093     
00094 }
00095 void Verbs::names(TokenStream *ts) {
00096     
00097     pThing t(new Thing());
00098     List l = t->allocList();
00099     getVariables(&l, _world);
00100     _world->setResult(t);
00101     
00102 }
00103 
00104 void Verbs::erase(TokenStream *ts) {
00105     
00106         pThing thing;
00107         _itp->getNextThing(_world, ts, &thing);
00108         if (!thing->isList())
00109                 BOOST_THROW_EXCEPTION( no_list_in_thing_exception() );
00110         
00111         for (tListArray::iterator i=thing->getList().begin(); i != thing->getList().end(); i++) {
00112                 if ((*i)->isWord()) {
00113             string w = (*i)->getWord();
00114             // erase this.
00115             removeFunction(w);
00116             _world->getScope()->removeThing(w);
00117         }
00118         }
00119 }
00120 
00121 void Verbs::erall(TokenStream *ts) {
00122     
00123     removeFunctions();
00124     _world->getScope()->removeThings();
00125     
00126 }
00127 
00128 void Verbs::erps(TokenStream *ts) {
00129     
00130     removeFunctions();
00131 
00132 }
00133 
00134 void Verbs::erns(TokenStream *ts) {
00135 
00136     _world->getScope()->removeThings();
00137 
00138 }
00139 
00140 void Verbs::ern(TokenStream *ts) {
00141     
00142         pThing thing;
00143         _itp->getNextThing(_world, ts, &thing);
00144     if (thing->isList()) {
00145         
00146         for (tListArray::iterator i=thing->getList().begin(); i != thing->getList().end(); i++) {
00147             if ((*i)->isWord()) {
00148                 string w = (*i)->getWord();
00149                 // erase this.
00150                 _world->getScope()->removeThing(w);
00151             }
00152         }
00153         
00154     }
00155     else {
00156         
00157         _world->getScope()->removeThing(thing->getWord());
00158         
00159     }
00160         
00161 }
00162 
00163 void Verbs::procedurep(TokenStream *ts) {
00164 
00165     string s = getWord(ts);
00166         std::map<std::string, std::string>::const_iterator i = getFuncTokenMap().find(s);
00167     setBooleanResult(i != getFuncTokenMap().end());
00168 
00169 }
00170 
00171 void Verbs::primitivep(TokenStream *ts) {
00172     
00173     string s = getWord(ts);
00174         std::map<std::string, std::string>::const_iterator i = getVerbTokenMap().find(s);
00175     setBooleanResult(i != getVerbTokenMap().end());
00176     
00177 }
00178 
00179 void Verbs::namep(TokenStream *ts) {
00180     
00181     string name = getWord(ts);
00182     try {
00183         _world->getScope()->getThing(name);
00184         setBooleanResult(true);
00185     }
00186     catch (no_thing_exception &x) {
00187         setBooleanResult(false);
00188     }
00189     
00190 }
00191     
00192 }
 All Classes Functions