Created
August 18, 2019 22:10
-
-
Save Johann150/74c7ca0897556b32bebf50be9b7e065a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<fstream> | |
#include<string> | |
#include<stack> | |
#include<map> | |
void usage(const char* name){ | |
std::cerr<<name<<" <input> [<file>]\n\nIf the <file> argument is not supplied, the program will be read from stdin."<<std::endl; | |
} | |
int main(int argc,char** argv){ | |
if(argc<2){ | |
usage(argv[0]); | |
return 1; | |
} | |
if(argc>2){ | |
std::ifstream*f=new std::ifstream(argv[2]); | |
if(!f->is_open()){ | |
std::cerr<<"?file"<<std::endl; | |
return 1; | |
} | |
std::cin.rdbuf(f->rdbuf()); | |
} | |
std::string input=argv[1]; | |
std::string output; | |
std::map<size_t,std::string> commands; | |
// load all the lines | |
std::string buf; | |
while(std::getline(std::cin,buf)){ | |
if(buf.empty()||buf[0]=='#') // empty line or comment | |
continue; | |
size_t p; | |
try{ | |
size_t line=std::stoul(buf,&p); | |
commands[line]=buf.substr(p); | |
}catch(std::invalid_argument){ | |
std::cerr<<"invalid line number here: "<<buf<<std::endl; | |
return 2; | |
} | |
} | |
if(commands.empty()) // there are no commands | |
return 0; | |
// execute | |
size_t ptr=0; | |
/* | |
line of currently executed instruction | |
It's initial value has to be such that when it is incremented, it is greater than the highes line number to trigger the line looparound mechanism. | |
*/ | |
std::map<size_t,std::string>::iterator instr=commands.end(); | |
size_t p=0; // keeping track of position in currently executed line | |
buf.clear(); | |
while(ptr<input.size()){ | |
if(p>=buf.size()){ // either the line is empty or at end of line | |
if(instr==commands.end()){ | |
instr=commands.begin(); | |
}else if(++instr==commands.end()){ | |
instr=commands.begin(); | |
} | |
buf=instr->second; // load the new line | |
p=0; | |
} | |
p=buf.find_first_not_of("; \t",p);// will skip semicolon | |
if(p==std::string::npos){ | |
buf.clear(); | |
continue; | |
} | |
if(buf.compare(p,9,"INCREMENT")==0){ | |
p+=9; | |
ptr++; | |
}else if(buf.compare(p,9,"CONTINUE ")==0){ | |
p+=9; | |
try{ | |
size_t q; | |
unsigned long l=std::stoul(buf.substr(p),&q); | |
p+=q; | |
ptr+=l; | |
}catch(std::invalid_argument){ | |
std::cerr<<"invalid argument for CONTINUE"<<std::endl; | |
return 2; | |
} | |
}else if(buf.compare(p,4,"SET ")==0){ | |
p+=4; | |
try{ | |
size_t q; | |
unsigned long l=std::stoul(buf.substr(p),&q); | |
p+=q; | |
ptr=l; | |
}catch(std::invalid_argument){ | |
std::cerr<<"invalid argument for SET"<<std::endl; | |
return 2; | |
} | |
}else if(buf.compare(p,4,"HALT")==0){ | |
ptr=input.size(); | |
break; | |
}else if(buf.compare(p,4,"NEXT")==0){ | |
instr++; | |
buf=instr->second; // load the new line | |
p=0; | |
}else if(buf.compare(p,5,"GOTO ")==0){ | |
p+=5; | |
try{ | |
size_t q; | |
unsigned long l=std::stoul(buf.substr(p),&q); | |
p+=q; | |
while(!commands.count(l)) | |
l++; | |
instr=commands.find(l); | |
buf=instr->second; // load the new line | |
p=0; | |
}catch(std::invalid_argument){ | |
std::cerr<<"invalid argument for GOTO"<<std::endl; | |
return 2; | |
} | |
}else if(buf.compare(p,6,"STORE ")==0){ | |
p+=6; | |
try{ | |
size_t q; | |
long l=std::stol(buf.substr(p),&q); | |
p+=q; | |
if((l>=0||ptr>=-l)&&ptr+l<input.size()){ | |
output+=input.at(ptr+l); | |
} | |
}catch(std::invalid_argument){ | |
std::cerr<<"invalid argument for STORE"<<std::endl; | |
return 2; | |
} | |
}else if(buf.compare(p,5,"TYPE ")==0){ | |
p+=5; | |
p=buf.find_first_not_of(" \t",p); | |
if(buf[p]!='"'&&buf[p]!='\''){ | |
std::cerr<<"invalid argument for TYPE"<<std::endl; | |
return 2; | |
} | |
char start=buf[p++]; | |
std::string s; | |
while(buf[p]&&buf[p]!=start){ | |
s+=buf[p++]; | |
} | |
if(buf[p++]!=start){ | |
std::cerr<<"TYPE string not terminated"<<std::endl; | |
return 2; | |
} | |
output+=s; | |
}else if(buf.compare(p,5,"KEEP ")==0){ | |
p+=5; | |
try{ | |
size_t q; | |
unsigned long l=std::stoul(buf.substr(p),&q); | |
p+=q; | |
if(l>=output.size()){ | |
output.insert(output.end(),input[ptr]); | |
}else{ | |
output.insert(output.begin()+l,input[ptr]); | |
} | |
}catch(std::invalid_argument){ | |
std::cerr<<"invalid argument for KEEP"<<std::endl; | |
return 2; | |
} | |
}else if(buf.compare(p,3,"IF ")==0){ | |
p+=3; | |
p=buf.find_first_not_of(" \t",p); | |
if(buf[p]!='"'&&buf[p]!='\''){ | |
std::cerr<<"invalid argument 1 for IF"<<std::endl; | |
return 2; | |
} | |
char start=buf[p++]; | |
bool check=true; | |
size_t q=0; | |
while(buf[p]&&buf[p]!=start){ | |
if(input[ptr+q]!=buf[p]){ | |
check=false; | |
p=buf.find_first_of(start,p); | |
break; | |
} | |
p++; | |
q++; | |
} | |
if(buf[p]!=start){ | |
std::cerr<<"string end quote missing"<<std::endl; | |
return 2; | |
} | |
p++; // skip quote | |
try{ | |
size_t q; | |
unsigned long l=std::stoul(buf.substr(p),&q); | |
p+=q; | |
if(check){ | |
while(!commands.count(l)) | |
l++; | |
instr=commands.find(l); | |
} | |
}catch(std::invalid_argument){ | |
std::cerr<<"invalid argument 2 for IF"<<std::endl; | |
return 2; | |
} | |
p=buf.find_first_not_of(" \t",p); | |
if(buf.compare(p,5,"ELSE ")==0){ | |
p+=5; | |
p=buf.find_first_not_of(" \t",p); | |
try{ | |
size_t q; | |
unsigned long l=std::stoul(buf.substr(p),&q); | |
p+=q; | |
if(!check){ | |
while(!commands.count(l)) | |
l++; | |
instr=commands.find(l); | |
} | |
}catch(std::invalid_argument){ | |
std::cerr<<"invalid argument 3 for IF"<<std::endl; | |
return 2; | |
} | |
} | |
buf=instr->second; // load the new line | |
p=0; | |
}else if(buf.compare(p,4,"GET ")==0){ | |
p+=4; | |
p=buf.find_first_not_of(" \t",p); | |
bool check=false; | |
try{ | |
size_t q; | |
unsigned long l=std::stoul(buf.substr(p),&q); | |
p+=q; | |
if(l<output.size()&&output.at(output.size()-l)==input[ptr]){ | |
check=true; | |
} | |
}catch(std::invalid_argument){ | |
std::cerr<<"invalid argument 1 for GET"<<std::endl; | |
return 2; | |
} | |
try{ | |
size_t q; | |
unsigned long l=std::stoul(buf.substr(p),&q); | |
p+=q; | |
if(check){ | |
while(!commands.count(l)) | |
l++; | |
instr=commands.find(l); | |
} | |
}catch(std::invalid_argument){ | |
std::cerr<<"invalid argument 2 for GET"<<std::endl; | |
return 2; | |
} | |
p=buf.find_first_not_of(" \t",p); | |
if(buf.compare(p,5,"ELSE ")==0){ | |
p+=5; | |
p=buf.find_first_not_of(" \t",p); | |
try{ | |
size_t q; | |
unsigned long l=std::stoul(buf.substr(p),&q); | |
p+=q; | |
if(!check){ | |
while(!commands.count(l)) | |
l++; | |
instr=commands.find(l); | |
} | |
}catch(std::invalid_argument){ | |
std::cerr<<"invalid argument 3 for GET"<<std::endl; | |
return 2; | |
} | |
} | |
buf=instr->second; // load the new line | |
p=0; | |
}else{ | |
std::cerr<<"unknown command: "<<buf.substr(p)<<std::endl; | |
return 2; | |
} | |
} | |
// output final result | |
std::cout<<output<<std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment