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