#include "ProcessString.h" #include "node.h" #include #include #include #include "VariableMap.h" #include"OperatorMap.h" /** *ProcessString is a class that helps turn a string into nodes. It is used by *NodeCreator in this program. ProcessString is responsible for taking each *part of the string and looking up to see how each char of the string should *be dealt with. * *Written by Jessica Smith */ //constructor ProcessString::ProcessString(string & line, int & position, const VariableMap & variableMap) :myLine(line), myPosition(position), myVariable(variableMap) { } void ProcessString::processBeginning() { //adds to beginning of the string if it is neccesary so that the string can be processed and stored correctly string unaryString; //Processes the beginning of the user's entry deleteExcess(myLine);//removes any unkonw input if(checkSpecialOperator(myLine,myPosition))//checks for special //operators like ++, //+=, etc. {} else if(!isalpha(myLine[myPosition])&&!isdigit(myLine[myPosition])&&myLine[myPosition]!='(') { myLine="current=current"+myLine; } else if(isdigit(myLine[myPosition])) { myLine="current="+myLine;//adds current so evaluated using //current, current just becomes the //value of the user's input } else if(checkUnaryOperator(myLine,myPosition,unaryString)){}//alters string if unary operator myNodeList.push_back(findName(myLine,myPosition)) ;//pushes on variable myPosition++; } void ProcessString::processRest() { //Processes the rest of the string, creating new nodes as it goes for (;myPosition ProcessString::processedNodes() { //returns the vector of nodes from the original string return myNodeList; } Node * ProcessString::findVariable(const string & line, int & position) { //looks up variable and finds its value then creates a new node with constant value of the variable string name; name+=line[position];//pushes letter onto string if(position+1line.length()-1||line[position]=='=')//if variable not //to assign to //current just to //evaluate { Node * Var =new Variable(name); if(position>line.length()-1) { Var->postAlert(true); } position--; return(Var);//adds new variable node to NodeList } line="="+line; position=-1;//sets up tp start at beginning of strign next pass return (new Variable("current")); } Node* ProcessString::lookUpNode(const string & Symbol) { //looks up the symbol and creates new node return(myOperatorMap.lookUpOperator(Symbol)); } bool ProcessString::checkSpecialOperator(string & line,int & position) { //checks to see if there are any special operators,like increment, //must come directly after or before a variable and tacks on //apprioriate string e.g. current++ becomes string current+1 string temp=""; if(position+1==line.length()) return false; if(line[position]=='+'||line[position]=='-') { if(line[position+1]==line[position]) { if(position==0) { if(line.length()==2)//if only ++,-- { line=temp+"current=current"+line[position]+"1"; } else { temp=line.substr(2,(line.length()-2)); line=temp+"="+temp+line[position]+"1"; } } else { temp=line.substr(0,position); //temp is a variable line=temp+"="+temp+line[position]+"1"; position--; } return true; } } return(checkOpEquals(line,position)); //checks for assignment //operator and arithmetica operators } bool ProcessString::checkOpEquals(string & line, int &position) { //Checks to see if there are +=, -=,*=,/=,%= //Returns true if there is and updates string string temp; if((line[position]=='+'||line[position]=='-'||line[position]=='*'||line[position]== '/'||line[position]=='%')&&line[position+1]=='=') { if(position==0) { line=temp+"current=current"+line[position]+line.substr(position+2,line.length()-position-2); } else { temp=line.substr(0,position); line=temp+"="+temp+line[position]+line.substr(position+2,line.length()-position-2); position--; } return true; } return false; } void ProcessString::deleteExcess(string & line) { //alters line so that all white space and unknown characters are gone string temp; for(int position=0;position>num; return(new Constant(num));//adds constant node } bool ProcessString::checkUnaryOperator( string & line, int & position, string & unaryString) { string prev,uniCheck; int placeHolder=position; //holds location of original location prev+=line[position-1]; //checks to see if the input is a unary operator, saves if found in string if(line[position]=='-'&&line[position-1]!=')'&&line[position-1]!='!'&&myOperatorMap.isOperator(prev)) { unaryString="_"; return true; } else if(line[position]=='!'&&(isdigit(line[position-1])||line[position-1]==')')) { unaryString="!"; return true; } else{ while(isalpha(line[position])) { uniCheck+=line[position]; position++; } position--; if(myOperatorMap.isTypeOperator(uniCheck,UNARY_PRECEDENCE)) { unaryString=uniCheck; return true; } position=placeHolder; return false; } }