PH Logo
Logo intepreter modeled after UCB Logo.
/Users/paul/Documents/phlogo/core/List.cpp
00001 /*
00002  *  List.cpp
00003  *
00004  *  Author: Paul Hamilton
00005  *      Date: 8 Jun 2011
00006  *
00007  */
00008 
00009 #include "List.h"
00010 
00011 #include "Verbs.h"
00012 #include "Thing.h"
00013 #include "ListArrayParser.h"
00014 #include "StringTokenStream.h"
00015 
00016 #include <boost/lexical_cast.hpp>
00017 
00018 using namespace std;
00019 using namespace boost;
00020 
00021 namespace phlogo {
00022 
00023 static const char LIST_START = '[';
00024 static const char LIST_END = ']';
00025 static const char SPACE = ' ';
00026 
00027 List::List(tListArray *list) {
00028         _list = list;
00029 }
00030 
00031 string List::str() {
00032 
00033         string s = "";
00034         for (tListArray::const_iterator i=_list->begin(); i != _list->end(); i++) {
00035                 if (s.length() != 0)
00036                         s += SPACE;
00037                 if ((*i)->isWord())
00038                         s += (*i)->getWord();
00039                 else if ((*i)->isLong()) 
00040                         s += lexical_cast<string>((*i)->getLong());
00041                 else if ((*i)->isDouble()) 
00042                         s += lexical_cast<string>((*i)->getDouble());
00043         else
00044         {
00045                         s += LIST_START + (*i)->getList().str() + LIST_END;
00046                 }
00047         }
00048         return s;
00049 }
00050 
00051 void List::add(pThing thing) {
00052         _list->push_back(thing); 
00053 }
00054 
00055 void List::addString(const std::string &s) {
00056         pThing thing(new Thing());
00057         thing->set(s);
00058         add(thing);
00059 }
00060 
00061 void List::clear() { 
00062         _list->clear();
00063 }
00064 
00065 unsigned int List::size() { 
00066         return _list->size(); 
00067 }
00068 
00069 pThing List::operator[](const size_t idx) { 
00070         return (*_list)[idx]; 
00071 }
00072 pThing List::get(const size_t idx) { 
00073         return (*_list)[idx]; 
00074 }
00075 
00076 void List::set(const size_t idx, pThing thing) { 
00077         (*_list)[idx] = thing; 
00078 }
00079 
00080 void List::push_front(pThing thing) {
00081         tListArray l2;
00082         for (tListArray::iterator i=_list->begin(); i!=_list->end(); i++)
00083                 l2.push_back(*i);
00084         _list->clear();
00085         _list->push_back(thing);
00086         for (tListArray::iterator i=l2.begin(); i!=l2.end(); i++)
00087                 _list->push_back(*i);
00088 }
00089 
00090 void List::push_back(pThing thing) {
00091         _list->push_back(thing);
00092 }
00093 
00094 pThing List::pop_front() {
00095     pThing t = _list->front();
00096     _list->pop_front();
00097     return t;
00098 }
00099 
00100 pThing List::pop_back() {
00101     pThing t = _list->back();
00102     _list->pop_back();
00103     return t;
00104 }
00105 
00106 bool List::isList(char c) {
00107         return c == LIST_START;
00108 }
00109 
00110 void List::fromWord(StringTokenStream *sts) {
00111         sts->forward(ListArrayParser::fromWord(_list, sts->remain(), 0));
00112         sts->skipWs();
00113 }
00114 
00115 }
 All Classes Functions