00001 #include "List.h"
00002 #include "Parser.h"
00003
00004 #include <assert.h>
00005 #include <sstream>
00006
00007 using namespace std;
00008
00009 List::List(int lineNumber)
00010 :_item(NULL),_next(NULL),_lineNumber(lineNumber)
00011 {
00012 }
00013
00014 List::List(List *list)
00015 :_item(NULL),_next(NULL)
00016 {
00017 if(list) {
00018
00019 if(list->_item) {
00020 if(list->_item->atomic())
00021 _item = new Atom((Atom *)list->_item);
00022 else
00023 _item = new List((List *)list->_item);
00024 }
00025
00026
00027
00028 List *listNext = list->_next;
00029 List *myNext = this;
00030 while(listNext != NULL) {
00031 myNext->_next = listNext->copy();
00032 myNext = myNext->_next;
00033 listNext = listNext->_next;
00034 }
00035 }
00036 }
00037
00038 List *List::copy()
00039 {
00040 List *newList = new List(_lineNumber);
00041
00042 if(_item) {
00043 if(_item->atomic())
00044 newList->_item = new Atom((Atom *)_item);
00045 else
00046 newList->_item = new List((List *)_item);
00047 }
00048
00049 return newList;
00050 }
00051
00052 List::~List()
00053 {
00054
00055
00056 delete _item;
00057
00058 List *current = _next;
00059 while(current != NULL) {
00060 List *next = current->_next;
00061 current->_next = NULL;
00062 delete current;
00063 current = next;
00064 }
00065
00066
00067
00068
00069
00070
00071 }
00072
00073 int List::getInt()
00074 {
00075 cerr << _lineNumber << ": Attempt to get int from list object" << endl;
00076 return 0;
00077 }
00078
00079 double List::getDouble()
00080 {
00081 cerr << _lineNumber << ": Attempt to get double from list object" << endl;
00082 return 0;
00083 }
00084
00085
00086
00087 std::string List::getString()
00088 {
00089 if(empty())
00090 return "";
00091 else {
00092 stringstream listString;
00093 listString << Parser::open << first()->getString();
00094 string restString = rest()->getRestString();
00095 if(restString != "")
00096 listString << " " << rest()->getRestString();
00097 listString << Parser::close;
00098 return listString.str();
00099 }
00100 }
00101
00102 std::string List::getRestString()
00103 {
00104 if(empty())
00105 return "";
00106 else {
00107 stringstream listString;
00108 listString << first()->getString();
00109 string restString = rest()->getRestString();
00110 if(restString != "")
00111 listString << " " << rest()->getRestString();
00112 return listString.str();
00113 }
00114 }
00115
00116 std::string List::firstString()
00117 {
00118 return first()->getString();
00119 }
00120
00121 int List::firstInt()
00122 {
00123 return first()->getInt();
00124 }
00125
00126 double List::firstDouble()
00127 {
00128 return first()->getDouble();
00129 }
00130
00131 List *List::firstList()
00132 {
00133 return first()->getList();
00134 }
00135
00136 ListItem *List::first()
00137 {
00138 return _item;
00139 }
00140
00141 List *List::rest()
00142 {
00143 return _next;
00144 }
00145
00146 List *List::cons(ListItem *first)
00147 {
00148 List *newList = new List();
00149 newList->_item = first;
00150 newList->_next = this;
00151 return newList;
00152 }
00153
00154 List *List::snoc(ListItem *last)
00155 {
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 List* current = this;
00168
00169 while(!current->empty())
00170 current = current->rest();
00171
00172 current->_item = last;
00173 current->_next = new List();
00174
00175 return this;
00176 }
00177
00178 void List::findAndReplace(std::string find, std::string replace)
00179 {
00180 List * current = this;
00181
00182 while(!current->empty()) {
00183 current->_item->findAndReplace(find,replace);
00184 current = current->rest();
00185 }
00186 }
00187
00188 Atom::Atom(std::string aString,int lineNumber)
00189 :_string(aString),_lineNumber(lineNumber)
00190 {
00191 }
00192
00193 Atom::Atom(int anInt, int lineNumber)
00194 :_lineNumber(lineNumber)
00195 {
00196 stringstream num;
00197 num << anInt;
00198 _string = num.str();
00199 }
00200
00201 Atom::Atom(double aDouble, int lineNumber)
00202 :_lineNumber(lineNumber)
00203 {
00204 stringstream num;
00205 num << aDouble;
00206 _string = num.str();
00207 }
00208
00209 Atom::Atom(Atom *atom)
00210 :_string(atom->_string),_lineNumber(atom->_lineNumber)
00211 {
00212 }
00213
00214 List *Atom::getList()
00215 {
00216 cerr << _lineNumber << ": Attempt to getList on atomic element: " << _string << endl;
00217 return NULL;
00218 }
00219
00220 int Atom::getInt()
00221 {
00222 return atoi(_string.c_str());
00223 }
00224
00225 double Atom::getDouble()
00226 {
00227 return atof(_string.c_str());
00228 }
00229
00230 void Atom::findAndReplace(std::string find, std::string replace)
00231 {
00232 if(_string == find)
00233 _string = replace;
00234 }
00235
00236 std::string Atom::firstString()
00237 {
00238 cerr << _lineNumber << ": Attempt to firstString on atomic element: " << _string << endl;
00239 return "";
00240 }
00241
00242 List *Atom::firstList()
00243 {
00244 cerr << _lineNumber << ": Attempt to firstList on atomic element: " << _string << endl;
00245 return NULL;
00246 }