Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <cstdlib>
  7. #include <windows.h>
  8.  
  9. #define PUSH        0x0 //Implicit command for pushing signed 64 bit integers onto the stack
  10. #define POP         0x1 //Pop the top item off of the stack
  11. #define PULL        0x2 //Pull arbitrary item off of the stack
  12. #define SWAP        0x3 //Swap the first two items on the stack
  13. #define SWITCH      0x4 //Switch out two arbitrary items on the stack
  14. #define GET         0x5 //Put arbitrary item on top of the stack
  15. #define INPUT       0x6 //Get arbitrary input
  16. #define PRINT_A     0x7 //Print the top item of the stack as a character and pop it off of the stack
  17. #define PRINT_I     0x8 //Print the top item of the stack as a number and pop it off of the stack
  18. #define DUP         0x9 //Duplicate the first item on the stack
  19. #define EMPTY       0xA //Empty the program's memory
  20. #define JUMP        0xB //Jump to an arbitrary position
  21. #define IF          0xC //If the top item of the stack is nonzero, pop it off. Continue as normal. Otherwise, pop it off then jump to an arbitrary code location specified by the top item of the stack
  22. #define EXIT        0xD //Stop execution and return the top item of the stack to the intepretter.
  23. #define ADD         0xE
  24. #define SUB         0xF
  25. #define MUL         0x10
  26. #define DIV         0x11
  27. #define MOD         0x12
  28. #define GREATER     0x13
  29. #define LESS        0x14
  30. #define EQ          0x15
  31. #define LESSEQ      0x16
  32. #define GREATEREQ   0x17
  33. #define BAND        0x18
  34. #define BNOT        0x19
  35. #define BOR         0x1A
  36. #define LOR         0x1B
  37. #define LAND        0x1C
  38. #define LNOT        0x1D
  39.  
  40.  
  41. #define MAX_MEM 100000 //100,000 * 8 = 800kb Memory max
  42.  
  43. struct CMD;
  44. struct Program;
  45.  
  46. signed long long Execute(Program* PRG);
  47. bool Parse(std::string Data, Program* PRG);
  48. std::string LowerCase(std::string Data);
  49.  
  50. struct CMD{
  51.     unsigned long OP;
  52.     signed long long v;
  53. };
  54.  
  55. struct Program{
  56.     std::vector<CMD> commands;
  57.     std::vector<signed long long> memory;
  58. };
  59.  
  60. int main()
  61. {
  62.  
  63.     while(1){
  64.         Program PRG;
  65.         char* buffer;
  66.  
  67.         system("cls");
  68.         std::cout << "\n-- Esoteric CMDLine> ";
  69.         std::cin.getline(buffer,4096);
  70.         std::string Input = std::string(buffer);
  71.  
  72.  
  73.         if(LowerCase(Input) == "quit"){
  74.             return 1;
  75.         }
  76.         else if(Parse(LowerCase(Input), &PRG)){
  77.             std::cout << "\nThe process returned: " << Execute(&PRG)  << "\nWith stack size: " << PRG.memory.size() << "\nPress enter to continue..." << std::endl;
  78.             std::cin.ignore();
  79.             std::cin.get();
  80.         }
  81.         else{
  82.             std::cout << "\nThe Program was not formatted correctly..." << std::endl;
  83.             std::cin.get();
  84.         }
  85.     }
  86.  
  87.     return 0; //Crazy freaking error
  88. }
  89.  
  90. bool Parse(std::string Data, Program* PRG){
  91.     std::cout << "\nParsing..." << std::endl;
  92.  
  93.     std::string buffer = "";
  94.     CMD temp;
  95.     for(unsigned int i = 0;i<Data.length();++i){
  96.         if(Data[i] != ' '){
  97.             buffer += Data[i];
  98.         }
  99.  
  100.         if(Data[i] == ' ' || i == (Data.length()-1)){
  101.             if(buffer.length() > 0){
  102.                 if(buffer == "pop"){
  103.                     temp.OP = POP;
  104.                     temp.v = 0;
  105.                     PRG->commands.push_back(temp);
  106.                 }
  107.                 else if(buffer == "pull"){
  108.                     temp.OP = PULL;
  109.                     temp.v = 0;
  110.                     PRG->commands.push_back(temp);
  111.                 }
  112.                 else if(buffer == "swap"){
  113.                     temp.OP = SWAP;
  114.                     temp.v = 0;
  115.                     PRG->commands.push_back(temp);
  116.                 }
  117.                 else if(buffer == "switch"){
  118.                     temp.OP = SWITCH;
  119.                     temp.v = 0;
  120.                     PRG->commands.push_back(temp);
  121.                 }
  122.                 else if(buffer == "get"){
  123.                     temp.OP = GET;
  124.                     temp.v = 0;
  125.                     PRG->commands.push_back(temp);
  126.                 }
  127.                 else if(buffer == "input"){
  128.                     temp.OP = INPUT;
  129.                     temp.v = 0;
  130.                     PRG->commands.push_back(temp);
  131.                 }
  132.                 else if(buffer == "print-a"){
  133.                     temp.OP = PRINT_A;
  134.                     temp.v = 0;
  135.                     PRG->commands.push_back(temp);
  136.                 }
  137.                 else if(buffer == "print-i"){
  138.                     temp.OP = PRINT_I;
  139.                     temp.v = 0;
  140.                     PRG->commands.push_back(temp);
  141.                 }
  142.                 else if(buffer == "dup"){
  143.                     temp.OP = DUP;
  144.                     temp.v = 0;
  145.                     PRG->commands.push_back(temp);
  146.                 }
  147.                 else if(buffer == "empty"){
  148.                     temp.OP = EMPTY;
  149.                     temp.v = 0;
  150.                     PRG->commands.push_back(temp);
  151.                 }
  152.                 else if(buffer == "jump"){
  153.                     temp.OP = JUMP;
  154.                     temp.v = 0;
  155.                     PRG->commands.push_back(temp);
  156.                 }
  157.                 else if(buffer == "if"){
  158.                     temp.OP = IF;
  159.                     temp.v = 0;
  160.                     PRG->commands.push_back(temp);
  161.                 }
  162.                 else if(buffer == "exit"){
  163.                     temp.OP = EXIT;
  164.                     temp.v = 0;
  165.                     PRG->commands.push_back(temp);
  166.                 }
  167.                 else if(buffer == "+"){
  168.                     temp.OP = ADD;
  169.                     temp.v = 0;
  170.                     PRG->commands.push_back(temp);
  171.                 }
  172.                 else if(buffer == "-"){
  173.                     temp.OP = SUB;
  174.                     temp.v = 0;
  175.                     PRG->commands.push_back(temp);
  176.                 }
  177.                 else if(buffer == "*"){
  178.                     temp.OP = MUL;
  179.                     temp.v = 0;
  180.                     PRG->commands.push_back(temp);
  181.                 }
  182.                 else if(buffer == "/"){
  183.                     temp.OP = DIV;
  184.                     temp.v = 0;
  185.                     PRG->commands.push_back(temp);
  186.                 }
  187.                 else if(buffer == "%"){
  188.                     temp.OP = MOD;
  189.                     temp.v = 0;
  190.                     PRG->commands.push_back(temp);
  191.                 }
  192.                 else if(buffer == ">"){
  193.                     temp.OP = GREATER;
  194.                     temp.v = 0;
  195.                     PRG->commands.push_back(temp);
  196.                 }
  197.                 else if(buffer == "<"){
  198.                     temp.OP = LESS;
  199.                     temp.v = 0;
  200.                     PRG->commands.push_back(temp);
  201.                 }
  202.                 else if(buffer == "="){
  203.                     temp.OP = EQ;
  204.                     temp.v = 0;
  205.                     PRG->commands.push_back(temp);
  206.                 }
  207.                 else if(buffer == ">="){
  208.                     temp.OP = GREATEREQ;
  209.                     temp.v = 0;
  210.                     PRG->commands.push_back(temp);
  211.                 }
  212.                 else if(buffer == "<="){
  213.                     temp.OP = LESSEQ;
  214.                     temp.v = 0;
  215.                     PRG->commands.push_back(temp);
  216.                 }
  217.                 else if(buffer == "&"){
  218.                     temp.OP = BAND;
  219.                     temp.v = 0;
  220.                     PRG->commands.push_back(temp);
  221.                 }
  222.                 else if(buffer == "~"){
  223.                     temp.OP = BNOT;
  224.                     temp.v = 0;
  225.                     PRG->commands.push_back(temp);
  226.                 }
  227.                 else if(buffer == "|"){
  228.                     temp.OP = BOR;
  229.                     temp.v = 0;
  230.                     PRG->commands.push_back(temp);
  231.                 }
  232.                 else if(buffer == "||"){
  233.                     temp.OP = LOR;
  234.                     temp.v = 0;
  235.                     PRG->commands.push_back(temp);
  236.                 }
  237.                 else if(buffer == "&&"){
  238.                     temp.OP = LAND;
  239.                     temp.v = 0;
  240.                     PRG->commands.push_back(temp);
  241.                 }
  242.                 else if(buffer == "!"){
  243.                     temp.OP = LNOT;
  244.                     temp.v = 0;
  245.                     PRG->commands.push_back(temp);
  246.                 }
  247.                 else if(buffer[0] == '0' || buffer[0] == '1' || buffer[0] == '2' || buffer[0] == '3' ||
  248.                             buffer[0] == '4' || buffer[0] == '5' || buffer[0] == '6' || buffer[0] == '7' ||
  249.                             buffer[0] == '8' || buffer[0] == '9'){
  250.                     temp.OP = PUSH;
  251.                     temp.v = atoi(buffer.c_str());
  252.                     PRG->commands.push_back(temp);
  253.                 }
  254.                 else{
  255.                     return 0;
  256.                 }
  257.             }
  258.             buffer = "";
  259.         }
  260.     }
  261.  
  262.     if(PRG->commands.size() < 1){
  263.         return 0;
  264.     }
  265.  
  266.     return 1;
  267.  
  268. }
  269.  
  270. signed long long Execute(Program* PRG){
  271.     std::cout << "\nExecuting..." << std::endl;
  272.  
  273.     for(unsigned int i = 0;i<PRG->commands.size();++i){
  274.         switch (PRG->commands[i].OP){
  275.             case PUSH:
  276.  
  277.                     if(PRG->memory.size() < MAX_MEM){
  278.                         PRG->memory.insert(PRG->memory.begin(),PRG->commands[i].v);
  279.                     }
  280.  
  281.                 break;
  282.             case POP:
  283.  
  284.                     if(PRG->memory.size() > 0){
  285.                         PRG->memory.erase(PRG->memory.begin());
  286.                     }
  287.  
  288.                 break;
  289.             case PULL:
  290.  
  291.                     if(PRG->memory.size() > PRG->memory[PRG->memory[0]]){
  292.                         PRG->memory.erase(PRG->memory.begin()+PRG->memory[PRG->memory[0]]);
  293.                     }
  294.  
  295.                 break;
  296.             case SWAP:
  297.  
  298.                     if(PRG->memory.size() > 1){
  299.                         signed long long t = PRG->memory[0];
  300.                         PRG->memory[0] = PRG->memory[1];
  301.                         PRG->memory[1] = t;
  302.                     }
  303.  
  304.                 break;
  305.             case SWITCH:
  306.  
  307.                     if(PRG->memory.size() > PRG->memory[0] && PRG->memory.size() > PRG->memory[1]){
  308.                         signed long long t = PRG->memory[PRG->memory[0]];
  309.                         PRG->memory[PRG->memory[0]] = PRG->memory[PRG->memory[1]];
  310.                         PRG->memory[PRG->memory[1]] = t;
  311.                     }
  312.  
  313.                 break;
  314.             case GET:
  315.                     if(PRG->memory.size() > PRG->memory[0]){
  316.                         signed long long t = PRG->memory[PRG->memory[0]];
  317.                         PRG->memory.erase(PRG->memory.begin()+PRG->memory[0]);
  318.                         PRG->memory.insert(PRG->memory.begin(), t);
  319.                     }
  320.                 break;
  321.             case INPUT:
  322.  
  323.                     signed long long t;
  324.                     std::cout << "> ";
  325.                     std::cin >> t;
  326.                     PRG->memory.insert(PRG->memory.begin(), t);
  327.                     std::cout << "\n";
  328.  
  329.                 break;
  330.             case PRINT_A:
  331.  
  332.                     std::cout << (char)PRG->memory[0];
  333.                     PRG->memory.erase(PRG->memory.begin());
  334.  
  335.                 break;
  336.             case PRINT_I:
  337.  
  338.                     std::cout << PRG->memory[0];
  339.                     PRG->memory.erase(PRG->memory.begin());
  340.  
  341.                 break;
  342.             case DUP:
  343.  
  344.                     PRG->memory.insert(PRG->memory.begin(), PRG->memory[0]);
  345.  
  346.                 break;
  347.             case EMPTY:
  348.  
  349.                     PRG->memory.empty();
  350.  
  351.                 break;
  352.             case JUMP:
  353.  
  354.                     if(PRG->commands.size() > PRG->memory[0]){
  355.                         i = PRG->memory[0];
  356.                     }
  357.  
  358.                 break;
  359.             case IF:
  360.  
  361.                     if(PRG->memory[0] == 0){
  362.                         if(PRG->commands.size() > PRG->memory[0]){
  363.                             i = PRG->memory[0];
  364.                         }
  365.                     }
  366.  
  367.                 break;
  368.             case EXIT:
  369.  
  370.                     if(PRG->memory.size() > 0){
  371.                         return PRG->memory[0];
  372.                     }
  373.                     else{
  374.                         return 0x0;
  375.                     }
  376.  
  377.                 break;
  378.             case ADD:
  379.                     if(PRG->memory.size() > 1){
  380.                         signed long long t = PRG->memory[0] + PRG->memory[1];
  381.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  382.                         PRG->memory.insert(PRG->memory.begin(), t);
  383.                     }
  384.                 break;
  385.             case SUB:
  386.                     if(PRG->memory.size() > 1){
  387.                         signed long long t = PRG->memory[0] - PRG->memory[1];
  388.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  389.                         PRG->memory.insert(PRG->memory.begin(), t);
  390.                     }
  391.                 break;
  392.             case MUL:
  393.                     if(PRG->memory.size() > 1){
  394.                         signed long long t = PRG->memory[0] * PRG->memory[1];
  395.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  396.                         PRG->memory.insert(PRG->memory.begin(), t);
  397.                     }
  398.                 break;
  399.             case DIV:
  400.                     if(PRG->memory.size() > 1 && PRG->memory[1] != 0){
  401.                         signed long long t = PRG->memory[0] / PRG->memory[1];
  402.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  403.                         PRG->memory.insert(PRG->memory.begin(), t);
  404.                     }
  405.                 break;
  406.             case MOD:
  407.                     if(PRG->memory.size() > 1 && PRG->memory[1] != 0){
  408.                         signed long long t = PRG->memory[0] % PRG->memory[1];
  409.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  410.                         PRG->memory.insert(PRG->memory.begin(), t);
  411.                     }
  412.                 break;
  413.             case GREATER:
  414.                     if(PRG->memory.size() > 1){
  415.                         signed long long t = PRG->memory[0] > PRG->memory[1];
  416.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  417.                         PRG->memory.insert(PRG->memory.begin(), t);
  418.                     }
  419.                 break;
  420.             case LESS:
  421.                     if(PRG->memory.size() > 1){
  422.                         signed long long t = PRG->memory[0] < PRG->memory[1];
  423.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  424.                         PRG->memory.insert(PRG->memory.begin(), t);
  425.                     }
  426.                 break;
  427.             case EQ:
  428.                     if(PRG->memory.size() > 1){
  429.                         signed long long t = PRG->memory[0] == PRG->memory[1];
  430.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  431.                         PRG->memory.insert(PRG->memory.begin(), t);
  432.                     }
  433.                 break;
  434.             case LESSEQ:
  435.                     if(PRG->memory.size() > 1){
  436.                         signed long long t = PRG->memory[0] <= PRG->memory[1];
  437.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  438.                         PRG->memory.insert(PRG->memory.begin(), t);
  439.                     }
  440.                 break;
  441.             case GREATEREQ:
  442.                     if(PRG->memory.size() > 1){
  443.                         signed long long t = PRG->memory[0] >= PRG->memory[1];
  444.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  445.                         PRG->memory.insert(PRG->memory.begin(), t);
  446.                     }
  447.                 break;
  448.             case BAND:
  449.                     if(PRG->memory.size() > 1){
  450.                         signed long long t = PRG->memory[0] & PRG->memory[1];
  451.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  452.                         PRG->memory.insert(PRG->memory.begin(), t);
  453.                     }
  454.                 break;
  455.             case BNOT:
  456.                     if(PRG->memory.size() > 0){
  457.                         signed long long t = ~PRG->memory[0];
  458.                         PRG->memory.erase(PRG->memory.begin());
  459.                         PRG->memory.insert(PRG->memory.begin(), t);
  460.                     }
  461.                 break;
  462.             case BOR:
  463.                     if(PRG->memory.size() > 1){
  464.                         signed long long t = PRG->memory[0] | PRG->memory[1];
  465.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  466.                         PRG->memory.insert(PRG->memory.begin(), t);
  467.                     }
  468.                 break;
  469.             case LOR:
  470.                     if(PRG->memory.size() > 1){
  471.                         signed long long t = PRG->memory[0] || PRG->memory[1];
  472.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  473.                         PRG->memory.insert(PRG->memory.begin(), t);
  474.                     }
  475.                 break;
  476.             case LAND:
  477.                     if(PRG->memory.size() > 1){
  478.                         signed long long t = PRG->memory[0] && PRG->memory[1];
  479.                         PRG->memory.erase(PRG->memory.begin(), PRG->memory.begin()+2);
  480.                         PRG->memory.insert(PRG->memory.begin(), t);
  481.                     }
  482.                 break;
  483.             case LNOT:
  484.                     if(PRG->memory.size() > 0){
  485.                         signed long long t = !PRG->memory[0];
  486.                         PRG->memory.erase(PRG->memory.begin());
  487.                         PRG->memory.insert(PRG->memory.begin(), t);
  488.                     }
  489.                 break;
  490.         }
  491.     }
  492. }
  493.  
  494. std::string LowerCase(std::string Data){
  495.     for(unsigned int i = 0;i<Data.length();++i){
  496.         Data[i] = tolower(Data[i]);
  497.     }
  498.  
  499.     return Data;
  500. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement