Advertisement
Guest User

Untitled

a guest
Jul 26th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1.  
  2. /** Script Operator */
  3. int sop(std::string cmd, std::map<char,int> var){
  4. /** Nothing */
  5. if(cmd.size()==0) return _nop;
  6.  
  7. /** Case of a single static value */
  8. 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){
  9. int tmp;
  10. if(cmd.substr(cmd.find_first_of('+')+1).size()>1) tmp=stoi(cmd.substr(cmd.find_first_of(' ')+1));
  11. else tmp=cmd[cmd.find_first_of('+')+1]-'0';
  12. return tmp;
  13. }
  14.  
  15. /** Case where there is a single variable */
  16. if(cmd.size()==1 || cmd.substr(cmd.find_first_of(' ')+1).size()==1){
  17. return var[cmd[cmd.find_first_of(' ')+1]];
  18. }
  19.  
  20. /** Case where there is a variable and a static/variable value */
  21. int a;
  22. if(isalpha(cmd[cmd.find_first_of(' ')+1])) a=var[cmd[cmd.find_first_of(' ')+1]];
  23. 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));
  24. std::string c;
  25. if(cmd.find_first_of('+')!=std::string::npos){
  26. if(isdigit(cmd[cmd.find_first_of('+')+1])){
  27. if(cmd.substr(cmd.find_first_of('+')+1).size()>0) c=cmd.substr(cmd.find_first_of('+')+1);
  28. } else{
  29. c=cmd.substr(cmd.find_first_of('+')+1);
  30. }
  31. return (sop(c,var)!=_nop)?a+sop(c,var):a;
  32. }
  33. if(cmd.find_first_of('-')!=std::string::npos){
  34. if(isdigit(cmd[cmd.find_first_of('-')+1])){
  35. if(cmd.substr(cmd.find_first_of('-')+1).size()>0) c=cmd.substr(cmd.find_first_of('-')+1);
  36. } else{
  37. c=cmd.substr(cmd.find_first_of('-')+1);
  38. }
  39. return (sop(c,var)!=_nop)?a-sop(c,var):a;
  40. }
  41. if(cmd.find_first_of('%')!=std::string::npos){
  42. if(isdigit(cmd[cmd.find_first_of('%')+1])){
  43. if(cmd.substr(cmd.find_first_of('%')+1).size()>0) c=cmd.substr(cmd.find_first_of('%')+1);
  44. } else{
  45. c=cmd.substr(cmd.find_first_of('%')+1);
  46. }
  47. return (sop(c,var)!=_nop)?a%sop(c,var):a;
  48. }
  49. if(cmd.find_first_of('*')!=std::string::npos){
  50. if(isdigit(cmd[cmd.find_first_of('*')+1])){
  51. if(cmd.substr(cmd.find_first_of('*')+1).size()>0) c=cmd.substr(cmd.find_first_of('*')+1);
  52. } else{
  53. c=cmd.substr(cmd.find_first_of('*')+1);
  54. }
  55. return (sop(c,var)!=_nop)?a*sop(c,var):a;
  56. }
  57. if(cmd.find("==")!=std::string::npos){
  58. if(isdigit(cmd[cmd.find_first_of('=')+2])){
  59. if(cmd.substr(cmd.find_first_of('=')+2).size()>0) c=cmd.substr(cmd.find_first_of('=')+2);
  60. } else{
  61. c=cmd.substr(cmd.find_first_of('=')+1);
  62. }
  63. return (sop(c,var)!=_nop)?a==sop(c,var):a;
  64. }
  65. if(cmd.find("!=")!=std::string::npos){
  66. if(isdigit(cmd[cmd.find_first_of('!')+2])){
  67. if(cmd.substr(cmd.find_first_of('!')+2).size()>0) c=cmd.substr(cmd.find_first_of('!')+2);
  68. } else{
  69. c=cmd.substr(cmd.find_first_of('!')+2);
  70. }
  71. return (sop(c,var)!=_nop)?a!=sop(c,var):a;
  72. }
  73. if(cmd.find("<=")!=std::string::npos){
  74. if(isdigit(cmd[cmd.find_first_of('<')+2])){
  75. if(cmd.substr(cmd.find_first_of('<')+2).size()>0) c=cmd.substr(cmd.find_first_of('<')+2);
  76. } else{
  77. c=cmd.substr(cmd.find_first_of('<')+2);
  78. }
  79. return (sop(c,var)!=_nop)?a<=sop(c,var):a;
  80. }
  81. if(cmd.find(">=")!=std::string::npos){
  82. if(isdigit(cmd[cmd.find_first_of('>')+2])){
  83. if(cmd.substr(cmd.find_first_of('>')+2).size()>0) c=cmd.substr(cmd.find_first_of('>')+2);
  84. } else{
  85. c=cmd.substr(cmd.find_first_of('>')+2);
  86. }
  87. return (sop(c,var)!=_nop)?a>=sop(c,var):a;
  88. }
  89. if(cmd.find_first_of('<')!=std::string::npos){
  90. if(isdigit(cmd[cmd.find_first_of('<')+1])){
  91. if(cmd.substr(cmd.find_first_of('<')+1).size()>0) c=cmd.substr(cmd.find_first_of('<')+1);
  92. } else{
  93. c=cmd.substr(cmd.find_first_of('<')+1);
  94. }
  95. return (sop(c,var)!=_nop)?a<sop(c,var):a;
  96. }
  97. if(cmd.find_first_of('>')!=std::string::npos){
  98. if(isdigit(cmd[cmd.find_first_of('>')+1])){
  99. if(cmd.substr(cmd.find_first_of('>')+1).size()>0) c=cmd.substr(cmd.find_first_of('>')+1);
  100. } else{
  101. c=cmd.substr(cmd.find_first_of('>')+1);
  102. }
  103. return (sop(c,var)!=_nop)?a>sop(c,var):a;
  104. }
  105. return _nop;
  106. }
  107.  
  108. int scriptInstruction(std::string cmd, std::map<char,int>& var, int i, std::string& sendThis){
  109. try{
  110. cmd=cmd.erase(0,1);
  111. if(cmd.find("goto")!=std::string::npos && cmd.size()>5){
  112. if(sop(cmd,var)!=_nop) return sop(cmd,var)-1;
  113. else{
  114. sendThis="=>"+cmd.substr(cmd.find_first_of(' ')+1);
  115. return i;
  116. }
  117. }
  118. if(cmd.find("skip")!=std::string::npos && cmd.size()>5){
  119. return i+sop(cmd,var);
  120. }
  121. if(cmd.find("back")!=std::string::npos && cmd.size()>5){
  122. return i-sop(cmd,var);
  123. }
  124. if(cmd.find("var")!=std::string::npos){
  125. var[cmd[4]]=sop(" "+cmd.substr(6),var);
  126. return i;
  127. }
  128. if(!cmd.find("sleep")){
  129. std::this_thread::sleep_for(std::chrono::milliseconds(sop(cmd,var)));
  130. return i;
  131. }
  132. if(!cmd.find("show")){
  133. if(sop(cmd,var)!=_nop) std::cout<<cmd.substr(5)<<"=="<<sop(cmd,var)<<std::endl;
  134. else std::cout<<cmd.substr(5)<<std::endl;
  135. return i;
  136. }
  137. if(!cmd.find("pause")){
  138. std::cout<<"Press a key to continue..."<<std::endl;
  139. system("pause>nul");
  140. return i;
  141. }
  142. if(!cmd.find("replace")){
  143. std::string str(cmd.substr(cmd.find_first_of(' ')+1));
  144. std::string answer("");
  145. while(str.find_first_of('$')!=std::string::npos){
  146. answer+=str.substr(0,str.find_first_of('$'))+std::to_string(var[str[str.find_first_of('$')+1]]);
  147. str=str.substr(str.find_first_of('$')+2);
  148. }
  149. sendThis=answer;
  150. return i;
  151. }
  152. if(!cmd.find("end")) return 999999;
  153. if(!cmd.find("if")){
  154. if(sop(cmd,var)) return i;
  155. else return i+1;
  156. }
  157. return i;
  158. }
  159. catch(std::exception& e){
  160. std::cerr<<"Line "<<i<<" : syntax error detected : "<<e.what()<<std::endl<<"->"<<cmd<<std::endl<<"Operation aborted..."<<std::endl;
  161. return 999999;
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement