PH Logo
Logo intepreter modeled after UCB Logo.
/Users/paul/Documents/phlogo/core/Array.cpp
00001 /*
00002  *  Array.cpp
00003  *
00004  *  Author: Paul Hamilton
00005  *      Date: 18 Jun 2011
00006  *
00007  */
00008 #include "Array.h"
00009         
00010 #include "Thing.h"
00011 #include "List.h"
00012 #include "ListArrayParser.h"
00013 #include "StringTokenStream.h"
00014 
00015 using namespace std;
00016 
00017 namespace phlogo {
00018 
00019 static const char ARRAY_START = '{';
00020 static const char ARRAY_END = '}';
00021 static const char LIST_START = '[';
00022 static const char LIST_END = ']';
00023 static const char *EMPTY_LIST = "[]";
00024 static const char SPACE = ' ';
00025 
00026 Array::Array(tListArray *array, int length) {
00027         _array = array;
00028         for (int i=0; i<length; i++) {
00029                 pThing l(new Thing());
00030                 _array->push_back(l);
00031         }
00032 }
00033 
00034 Array::Array(tListArray *array) {
00035         _array = array;
00036 }
00037 
00038 void Array::set(int i, pThing thing) {
00039         (*_array)[i] = thing;
00040 }
00041 
00042 void Array::push_front(pThing thing) {
00043     _array->push_front(thing);
00044 }
00045 
00046 void Array::clear() { 
00047         _array->clear();
00048 }
00049 
00050 unsigned int Array::size() { 
00051         return _array->size(); 
00052 }
00053 
00054 pThing Array::operator[](const size_t idx) { 
00055         return (*_array)[idx]; 
00056 }
00057 
00058 pThing Array::get(const size_t idx) { 
00059         return (*_array)[idx]; 
00060 }
00061 
00062 bool Array::isArray(char c) {
00063         return c == ARRAY_START;
00064 }
00065 
00066 std::string Array::str() {
00067                         
00068         string s(1, ARRAY_START);
00069         for (tListArray::const_iterator i=_array->begin(); i != _array->end(); i++) {
00070                 if (s.length() > 1 || s[0] != ARRAY_START)
00071                         s += SPACE;
00072                 if ((*i)->isWord())
00073                         s += (*i)->getWord();
00074                 else if ((*i)->isList())
00075                         s += LIST_START + (*i)->getList().str() + LIST_END;
00076                 else if ((*i)->isArray())
00077                         s += (*i)->getArray().str();
00078                 else
00079                         s += EMPTY_LIST;
00080         }
00081         return s + ARRAY_END;
00082         
00083 }
00084 
00085 void Array::fromWord(StringTokenStream *sts) {
00086         sts->forward(ListArrayParser::fromWord(_array, sts->remain(), 0));
00087         sts->skipWs();
00088 }
00089 
00090 }
 All Classes Functions