Advertisement
YourMain12

Malboge Compiler

Jul 8th, 2023 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. // After running the compiler save your malboge code as .mb file and run it by opening it on the running compiler code
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8. using namespace std;
  9.  
  10. const char* opcodes = "ji*p</vo";
  11. const char* table = "5z]&gqtyfr$(we4{WP)H-Zn,[%\3dL+Q;>U!pJS72Fh"
  12.                     "OA1CB6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@";
  13. const int rotate[10] = {1, 3, 0, 7, 4, 2, 6, 9, 5, 8};
  14. const int memsize = 59049;
  15. vector<int> memory(memsize);
  16. int a = 0, c = 0, d = 0;
  17. map<char, int> instr;
  18.  
  19. void init_instr() {
  20.     for (int i = 0; i < 8; i++) {
  21.         instr[opcodes[i]] = i;
  22.     }
  23. }
  24.  
  25. void read_source(const char* filename) {
  26.     ifstream fin(filename);
  27.     if (!fin) {
  28.         cerr << "Error: cannot open file " << filename << endl;
  29.         exit(1);
  30.     }
  31.     string line;
  32.     int i = 0;
  33.     while (getline(fin, line)) {
  34.         for (char ch : line) {
  35.             if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') continue;
  36.             if (ch < 33 || ch > 126) {
  37.                 cerr << "Error: invalid character " << ch << endl;
  38.                 exit(1);
  39.             }
  40.             memory[i++] = ch;
  41.             if (i >= memsize) {
  42.                 cerr << "Error: source code too large" << endl;
  43.                 exit(1);
  44.             }
  45.         }
  46.     }
  47.     fin.close();
  48. }
  49.  
  50. string translate(int x) {
  51.     switch (x) {
  52.         case 0:
  53.             return "goto label" + to_string(d) + ";";
  54.         case 1:
  55.             return "cout << char(a);";
  56.         case 2:
  57.             return "cin >> a;";
  58.         case 3:
  59.             return "memory[d] = rotate[memory[d] % 10];";
  60.         case 4:
  61.             return "d = memory[d];";
  62.         case 5:
  63.             return "a = (memory[d] * a + memory[d] + a) % memsize;";
  64.         case 6:
  65.             return "return 0;";
  66.         case 7:
  67.             return "";
  68.         default:
  69.             cerr << "Error: unknown instruction " << x << endl;
  70.             exit(1);
  71.     }
  72. }
  73.  
  74. void generate_code() {
  75.     cout << "#include <iostream>" << endl;
  76.     cout << "#include <vector>" << endl;
  77.     cout << "using namespace std;" << endl;
  78.     cout << "const int rotate[10] = {1, 3, 0, 7, 4, 2, 6, 9, 5, 8};" << endl;
  79.     cout << "const int memsize = 59049;" << endl;
  80.     cout << "vector<int> memory(memsize);" << endl;
  81.     cout << "int a = 0, c = 0, d = 0;" << endl;
  82.     cout << "int main() {" << endl;
  83.     for (int i = 0; i < memsize; i++) {
  84.         if (memory[i] != 0) {
  85.             cout << "memory[" << i << "] = " << memory[i] << ";" << endl;
  86.         }
  87.     }
  88.     while (true) {
  89.         cout << "label" << c << ":" << endl;
  90.         int x = instr[memory[c]];
  91.         cout << translate(x) << endl;
  92.         memory[c] = table[memory[c] - 33];
  93.         c = (c + 1) % memsize;
  94.         d = (d + 1) % memsize;
  95.     }
  96.     cout << "}" << endl;
  97. }
  98.  
  99. int main(int argc, char** argv) {
  100.     if (argc != 2) {
  101.         cerr << "Usage: " << argv[0] << " <source file>" << endl;
  102.         return 1;
  103.     }
  104.     init_instr();
  105.     read_source(argv[1]);
  106.     generate_code();
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement