Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sstream>
- #include <map>
- #include "../include/Parser.hh"
- using namespace std;
- int Parser::parseFile(Brain* brain, ifstream &brainFile, int arguments)
- {
- map<string,int> labels;
- vector<InstructionIF> instructions;
- string line;
- int counter=-1;
- int brk_ind=-1;
- int ebk_ind=-1;
- int ret;
- while(getline(brainFile,line))
- {
- counter++;
- istringstream iss(line);
- if(line[0]=='#')
- {
- string label;
- iss>>label;
- cout<<label<<endl;
- if(labels.find(label)!=labels.end())
- {
- cerr<<"Label already exists"<<endl;
- return 1;
- }
- labels[label]=counter;
- }
- string instr;
- iss>>instr;
- if(instr=="add")
- {
- string res,first,second;
- iss>>res>>first>>second;
- vector<string> args;
- args.push_back(res);
- args.push_back(first);
- args.push_back(second);
- instructions.push_back(Instruction_add(&ret,args));
- }
- else if(instr=="sub")
- {
- cout<<"SUB"<<endl;
- string res,first,second;
- iss>>res>>first>>second;
- vector<string> args;
- args.push_back(res);
- args.push_back(first);
- args.push_back(second);
- instructions.push_back(Instruction_sub(&ret,args));
- }
- else if(instr=="mul")
- {
- string res,first,second;
- iss>>res>>first>>second;
- vector<string> args;
- args.push_back(res);
- args.push_back(first);
- args.push_back(second);
- instructions.push_back(Instruction_mul(&ret,args));
- }
- else if(instr=="div")
- {
- string res,first,second;
- iss>>res>>first>>second;
- vector<string> args;
- args.push_back(res);
- args.push_back(first);
- args.push_back(second);
- instructions.push_back(Instruction_div(&ret,args));
- }
- /*else if(instr=="ifL")
- {
- string first,second,location;
- iss>>first>>second>>location;
- vector<string> args;
- args.push_back(first);
- args.push_back(second);
- args.push_back(location);
- instructions.push_back(Instruction_ifL(&ret,args));
- }
- else if(instr=="ieL")
- {
- string first,second,im1,im2;
- iss>>first>>second>>im1>>im2;
- vector<string> args;
- args.push_back(first);
- args.push_back(second);
- args.push_back(im1);
- args.push_back(im2);
- //instructions.push_back(Instruction_ieL(&ret,args));
- }*/
- else if(instr=="ifl")
- {
- cout<<"IFL"<<endl;
- string first,second,location;
- iss>>first>>second>>location;
- vector<string> args;
- args.push_back(first);
- args.push_back(second);
- args.push_back(location);
- instructions.push_back(Instruction_ifl(&ret,args));
- }
- else if(instr=="iel")
- {
- string first,second,im1,im2;
- iss>>first>>second>>im1>>im2;
- vector<string> args;
- args.push_back(first);
- args.push_back(second);
- args.push_back(im1);
- args.push_back(im2);
- instructions.push_back(Instruction_iel(&ret,args));
- }
- else if(instr=="ife")
- {
- cout<<"IFE"<<endl;
- string first,second,location;
- iss>>first>>second>>location;
- vector<string> args;
- args.push_back(first);
- args.push_back(second);
- args.push_back(location);
- instructions.push_back(Instruction_ife(&ret,args));
- }
- else if(instr=="iee")
- {
- cout<<"IEE"<<endl;
- string first,second,im1,im2;
- iss>>first>>second>>im1>>im2;
- vector<string> args;
- args.push_back(first);
- args.push_back(second);
- args.push_back(im1);
- args.push_back(im2);
- instructions.push_back(Instruction_iee(&ret,args));
- }
- else if(instr=="jmp")
- {
- cout<<"JMP"<<endl;
- string im;
- iss>>im;
- vector<string> args;
- args.push_back(im);
- instructions.push_back(Instruction_jmp(&ret,args));
- }
- else if(instr=="put")
- {
- cout<<"PUT"<<endl;
- string first,second;
- iss>>first>>second;
- vector<string> args;
- args.push_back(first);
- args.push_back(second);
- instructions.push_back(Instruction_put(&ret,args));
- }
- else if(instr=="crt")
- {
- cout<<"CRT"<<endl;
- string first,second;
- iss>>first>>second;
- vector<string> args;
- args.push_back(first);
- args.push_back(second);
- instructions.push_back(Instruction_crt(&ret,args));
- }
- else if(instr=="cra")
- {
- string im1,im2,im3;
- iss>>im1>>im2>>im3;
- vector<string> args;
- args.push_back(im1);
- args.push_back(im2);
- args.push_back(im3);
- instructions.push_back(Instruction_cra(&ret,args));
- }
- else if(instr=="del")
- {
- cout<<"DEL"<<endl;
- string im;
- iss>>im;
- vector<string> args;
- args.push_back(im);
- instructions.push_back(Instruction_del(&ret,args));
- }
- else if(instr=="ret")
- {
- cout<<"RET"<<endl;
- string mes;
- iss>>mes;
- vector<string> args;
- args.push_back(mes);
- instructions.push_back(Instruction_ret(&ret,args));
- }
- else if(instr=="wat")
- {
- cout<<"WAT"<<endl;
- vector<string> args;
- instructions.push_back(Instruction_wat(&ret,args));
- }
- else if(instr=="brk")
- {
- cout<<"BRK"<<endl;
- if(brk_ind!=-1)
- {
- cerr<<"Instruction brk used more than once."<<endl;
- return 1;
- }
- brk_ind=counter;
- vector<string> args;
- instructions.push_back(Instruction_brk(&ret,args));
- }
- else if(instr=="ebk")
- {
- cout<<"EBK"<<endl;
- if(ebk_ind!=-1)
- {
- cerr<<"Instruction ebk used more than once."<<endl;
- return 1;
- }
- ebk_ind=counter;
- vector<string> args;
- instructions.push_back(Instruction_ebk(&ret,args));
- }
- else if(instr=="cln")
- {
- vector<string> args;
- instructions.push_back(Instruction_cln(&ret,args));
- }
- else if(instr=="DST")
- {
- string res,first,second;
- iss>>res>>first>>second;
- vector<string> args;
- args.push_back(res);
- args.push_back(first);
- args.push_back(second);
- instructions.push_back(Instruction_DST(&ret,args));
- }
- else if(instr=="SIM")
- {
- string pos,first,second;
- iss>>pos>>first>>second;
- vector<string> args;
- args.push_back(pos);
- args.push_back(first);
- args.push_back(second);
- instructions.push_back(Instruction_SIM(&ret,args));
- }
- else if(instr=="GTA")
- {
- string res,first;
- iss>>res>>first;
- vector<string> args;
- args.push_back(res);
- args.push_back(first);
- instructions.push_back(Instruction_GTA(&ret,args));
- }
- else if(instr=="GAP")
- {
- string im1,im2,im3;
- iss>>im1>>im2>>im3;
- vector<string> args;
- args.push_back(im1);
- args.push_back(im2);
- args.push_back(im3);
- instructions.push_back(Instruction_GAP(&ret,args));
- }
- else
- {
- cerr<<"Unrecognized command in brainFile."<<endl;
- return 1;
- }
- if(ret)
- {
- cerr<<"Error while constructing the instructions"<<endl;
- return 1;
- }
- }
- brain= new Brain(instructions);
- brain->setInstructionBreakPointer(brk_ind);
- brain->setErrorBreakPointer(ebk_ind);
- brain->labels=labels;
- return 0;
- }
- int Parser::parseImmediate(void* value, ArgumentType* type, string imed)
- {
- if(imed[0]=='*')
- {
- *type=STRING;
- *(string*)(value)=imed.substr(1);
- return 0;
- }
- istringstream iss(imed);
- int k;
- double d;
- iss>>k;
- if(iss.fail())
- {
- cerr<<"Non-numerical character found in non-string immediate."<<endl;
- return 1;
- }
- else
- {
- iss>>d;
- if(iss.fail())
- {
- string tmp;
- iss>>tmp;
- if(iss.fail())
- {
- *type=INT;
- *(int*)value=k;
- return 0;
- }
- else
- {
- cerr<<"Non-numerical character found in non-string immediate."<<endl;
- return 1;
- }
- }
- string tmp;
- iss>>tmp;
- if(iss.fail())
- {
- *type=DOUBLE;
- *(double*)value=k+d;
- return 0;
- }
- else
- {
- cerr<<"Non-numerical character found in non-string immediate."<<endl;
- return 1;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment