Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Script Operator */
- int sop(std::string cmd, std::map<char,int> var){
- /** Nothing */
- if(cmd.size()==0) return _nop;
- /** Case of a single static value */
- if((isdigit(cmd[0]) || isdigit(cmd[cmd.find_first_of(' ')+1])) && cmd.find_first_not_of("0123456789",cmd.find_first_of("0123456789"))==std::string::npos){
- int tmp;
- if(cmd.substr(cmd.find_first_of('+')+1).size()>1) tmp=stoi(cmd.substr(cmd.find_first_of(' ')+1));
- else tmp=cmd[cmd.find_first_of('+')+1]-'0';
- return tmp;
- }
- /** Case where there is a single variable */
- if(cmd.size()==1 || cmd.substr(cmd.find_first_of(' ')+1).size()==1){
- return var[cmd[cmd.find_first_of(' ')+1]];
- }
- /** Case where there is a variable and a static/variable value */
- int a;
- if(isalpha(cmd[cmd.find_first_of(' ')+1])) a=var[cmd[cmd.find_first_of(' ')+1]];
- else a=stoi(cmd.substr(cmd.find_first_of(' ')+1,cmd.find_first_not_of("0123456789",cmd.find_first_of(' ')+1)-cmd.find_first_of(' ')+1));
- std::string c;
- if(cmd.find_first_of('+')!=std::string::npos){
- if(isdigit(cmd[cmd.find_first_of('+')+1])){
- if(cmd.substr(cmd.find_first_of('+')+1).size()>0) c=cmd.substr(cmd.find_first_of('+')+1);
- } else{
- c=cmd.substr(cmd.find_first_of('+')+1);
- }
- return (sop(c,var)!=_nop)?a+sop(c,var):a;
- }
- if(cmd.find_first_of('-')!=std::string::npos){
- if(isdigit(cmd[cmd.find_first_of('-')+1])){
- if(cmd.substr(cmd.find_first_of('-')+1).size()>0) c=cmd.substr(cmd.find_first_of('-')+1);
- } else{
- c=cmd.substr(cmd.find_first_of('-')+1);
- }
- return (sop(c,var)!=_nop)?a-sop(c,var):a;
- }
- if(cmd.find_first_of('%')!=std::string::npos){
- if(isdigit(cmd[cmd.find_first_of('%')+1])){
- if(cmd.substr(cmd.find_first_of('%')+1).size()>0) c=cmd.substr(cmd.find_first_of('%')+1);
- } else{
- c=cmd.substr(cmd.find_first_of('%')+1);
- }
- return (sop(c,var)!=_nop)?a%sop(c,var):a;
- }
- if(cmd.find_first_of('*')!=std::string::npos){
- if(isdigit(cmd[cmd.find_first_of('*')+1])){
- if(cmd.substr(cmd.find_first_of('*')+1).size()>0) c=cmd.substr(cmd.find_first_of('*')+1);
- } else{
- c=cmd.substr(cmd.find_first_of('*')+1);
- }
- return (sop(c,var)!=_nop)?a*sop(c,var):a;
- }
- if(cmd.find("==")!=std::string::npos){
- if(isdigit(cmd[cmd.find_first_of('=')+2])){
- if(cmd.substr(cmd.find_first_of('=')+2).size()>0) c=cmd.substr(cmd.find_first_of('=')+2);
- } else{
- c=cmd.substr(cmd.find_first_of('=')+1);
- }
- return (sop(c,var)!=_nop)?a==sop(c,var):a;
- }
- if(cmd.find("!=")!=std::string::npos){
- if(isdigit(cmd[cmd.find_first_of('!')+2])){
- if(cmd.substr(cmd.find_first_of('!')+2).size()>0) c=cmd.substr(cmd.find_first_of('!')+2);
- } else{
- c=cmd.substr(cmd.find_first_of('!')+2);
- }
- return (sop(c,var)!=_nop)?a!=sop(c,var):a;
- }
- if(cmd.find("<=")!=std::string::npos){
- if(isdigit(cmd[cmd.find_first_of('<')+2])){
- if(cmd.substr(cmd.find_first_of('<')+2).size()>0) c=cmd.substr(cmd.find_first_of('<')+2);
- } else{
- c=cmd.substr(cmd.find_first_of('<')+2);
- }
- return (sop(c,var)!=_nop)?a<=sop(c,var):a;
- }
- if(cmd.find(">=")!=std::string::npos){
- if(isdigit(cmd[cmd.find_first_of('>')+2])){
- if(cmd.substr(cmd.find_first_of('>')+2).size()>0) c=cmd.substr(cmd.find_first_of('>')+2);
- } else{
- c=cmd.substr(cmd.find_first_of('>')+2);
- }
- return (sop(c,var)!=_nop)?a>=sop(c,var):a;
- }
- if(cmd.find_first_of('<')!=std::string::npos){
- if(isdigit(cmd[cmd.find_first_of('<')+1])){
- if(cmd.substr(cmd.find_first_of('<')+1).size()>0) c=cmd.substr(cmd.find_first_of('<')+1);
- } else{
- c=cmd.substr(cmd.find_first_of('<')+1);
- }
- return (sop(c,var)!=_nop)?a<sop(c,var):a;
- }
- if(cmd.find_first_of('>')!=std::string::npos){
- if(isdigit(cmd[cmd.find_first_of('>')+1])){
- if(cmd.substr(cmd.find_first_of('>')+1).size()>0) c=cmd.substr(cmd.find_first_of('>')+1);
- } else{
- c=cmd.substr(cmd.find_first_of('>')+1);
- }
- return (sop(c,var)!=_nop)?a>sop(c,var):a;
- }
- return _nop;
- }
- int scriptInstruction(std::string cmd, std::map<char,int>& var, int i, std::string& sendThis){
- try{
- cmd=cmd.erase(0,1);
- if(cmd.find("goto")!=std::string::npos && cmd.size()>5){
- if(sop(cmd,var)!=_nop) return sop(cmd,var)-1;
- else{
- sendThis="=>"+cmd.substr(cmd.find_first_of(' ')+1);
- return i;
- }
- }
- if(cmd.find("skip")!=std::string::npos && cmd.size()>5){
- return i+sop(cmd,var);
- }
- if(cmd.find("back")!=std::string::npos && cmd.size()>5){
- return i-sop(cmd,var);
- }
- if(cmd.find("var")!=std::string::npos){
- var[cmd[4]]=sop(" "+cmd.substr(6),var);
- return i;
- }
- if(!cmd.find("sleep")){
- std::this_thread::sleep_for(std::chrono::milliseconds(sop(cmd,var)));
- return i;
- }
- if(!cmd.find("show")){
- if(sop(cmd,var)!=_nop) std::cout<<cmd.substr(5)<<"=="<<sop(cmd,var)<<std::endl;
- else std::cout<<cmd.substr(5)<<std::endl;
- return i;
- }
- if(!cmd.find("pause")){
- std::cout<<"Press a key to continue..."<<std::endl;
- system("pause>nul");
- return i;
- }
- if(!cmd.find("replace")){
- std::string str(cmd.substr(cmd.find_first_of(' ')+1));
- std::string answer("");
- while(str.find_first_of('$')!=std::string::npos){
- answer+=str.substr(0,str.find_first_of('$'))+std::to_string(var[str[str.find_first_of('$')+1]]);
- str=str.substr(str.find_first_of('$')+2);
- }
- sendThis=answer;
- return i;
- }
- if(!cmd.find("end")) return 999999;
- if(!cmd.find("if")){
- if(sop(cmd,var)) return i;
- else return i+1;
- }
- return i;
- }
- catch(std::exception& e){
- std::cerr<<"Line "<<i<<" : syntax error detected : "<<e.what()<<std::endl<<"->"<<cmd<<std::endl<<"Operation aborted..."<<std::endl;
- return 999999;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement