PH Logo
Logo intepreter modeled after UCB Logo.
/Users/paul/Documents/phlogo/core/PredicateVerbs.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 
00013 #include <boost/algorithm/string/predicate.hpp>
00014 #include <boost/lexical_cast.hpp>
00015 
00016 using namespace std;
00017 using namespace boost::algorithm;
00018 using namespace boost;
00019 
00020 namespace phlogo {
00021 
00022 void Verbs::wordp(TokenStream *ts) {
00023     
00024     setBooleanResult(getThing(ts)->isWord());
00025 
00026 }
00027 
00028 void Verbs::listp(TokenStream *ts) {
00029     
00030     setBooleanResult(getThing(ts)->isList());
00031 
00032 }
00033 
00034 void Verbs::arrayp(TokenStream *ts) {
00035     
00036     setBooleanResult(getThing(ts)->isArray());
00037 
00038 }
00039 
00040 void Verbs::emptyp(TokenStream *ts) {
00041     
00042         pThing thing = getThing(ts);
00043     
00044     bool empty = false;
00045     if (thing->isList())
00046         empty = thing->getList().size() == 0;
00047     else if (thing->isArray())
00048         empty = thing->getArray().size() == 0;
00049     else if (thing->isWord())
00050         empty = thing->getWord().length() == 0;
00051     
00052     setBooleanResult(empty);
00053 }
00054 
00055 void Verbs::equalp(TokenStream *ts) {
00056     
00057     setBooleanResult(equal(getThing(ts), getThing(ts)));
00058 
00059 }
00060 
00061 void Verbs::notequalp(TokenStream *ts) {
00062     
00063     setBooleanResult(!equal(getThing(ts), getThing(ts)));
00064     
00065 }
00066 
00067 void Verbs::beforep(TokenStream *ts) {
00068     
00069     setBooleanResult(!lexicographical_compare(getWord(ts), getWord(ts)));
00070     
00071 }
00072 
00073 void Verbs::eq(TokenStream *ts) {
00074     
00075     setBooleanResult(getThing(ts) == getThing(ts));
00076     
00077 }
00078 
00079 
00080 void Verbs::memberp(TokenStream *ts) {
00081     
00082     pThing t = getThing(ts);
00083     pThing t2 = getThing(ts);
00084     
00085     if (t2->isList()) {
00086         List l = t2->getList();
00087         for (tListArray::iterator i = l.begin(); i != l.end(); i++) {
00088             if (equal(*i, t)) {
00089                 setBooleanResult(true);
00090                 return;
00091             }
00092         }
00093     }
00094     setBooleanResult(false);
00095 }
00096 
00097 
00098 void Verbs::substringp(TokenStream *ts) {
00099     
00100     string s1 = getWord(ts);
00101     string s2 = getWord(ts);
00102     setBooleanResult(s2.find(s1) != string::npos);
00103 
00104 }
00105 
00106 void Verbs::numberp(TokenStream *ts) {
00107     
00108     pThing t = getThing(ts);
00109     if (t->isLong() || t->isDouble()) {
00110         setBooleanResult(true);
00111         return;
00112     }
00113  
00114     if (t->isWord()) {
00115         string s = t->getWord();
00116         try {
00117             lexical_cast<long>(s);
00118             setBooleanResult(true);
00119             return;
00120         }
00121         catch (bad_cast &x) {
00122             try {
00123                 lexical_cast<double>(s);
00124                 setBooleanResult(true);
00125                 return;
00126             }
00127             catch (bad_cast &x) {
00128             }
00129         }
00130     }
00131     
00132     setBooleanResult(false);
00133 }
00134 
00135 }
 All Classes Functions