Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.56 KB | None | 0 0
  1. #include <win32\singlewindow.h>
  2.  
  3. #include <win32\textbox.h>
  4. #include <win32\toolbar.h>
  5. #include <win32\filedialog.h>
  6. #include <win32\label.h>
  7.  
  8. #include <win32\file.h>
  9.  
  10. #include <headers\tokenizer.h>
  11. #include <headers\conversions.h>
  12.  
  13. #include <iostream>
  14. #include <iostream>
  15. #include <string>
  16. #include <vector>
  17.  
  18. #include <cstdio>
  19. #include <cctype>
  20.  
  21. #define ID_OPEN 501
  22. #define ID_TBOX1 502
  23. #define ID_TBOX2 503
  24. #define ID_TBAR 504
  25. #define ID_LBL1 505
  26.  
  27. #define ID_NEWFILE 0
  28. #define ID_OPENFILE 1
  29. #define ID_SAVEFILE 2
  30. #define ID_RUN 3
  31.  
  32. #define MEM_SIZE 0x1000
  33.  
  34. //#define DEBUG
  35.  
  36. char fileName[MAX_PATH] = "";
  37.  
  38. SingleWindow* wnd;
  39. TextBox* tbox1;
  40. TextBox* tbox2;
  41. Toolbar* tbar1;
  42. Label* lbl1;
  43.  
  44. using namespace std;
  45.  
  46. #ifndef INTERPRETER
  47. #define INTERPRETER
  48.  
  49. struct IntPtr
  50. {
  51.     string name;
  52.     int addr;
  53.  
  54.     IntPtr(string s, int n) : name(s), addr(n)
  55.     {
  56.  
  57.     }
  58. };
  59.  
  60. class Interpreter
  61. {
  62.     string code;
  63.     vector<string> tokens;
  64.     vector<IntPtr> ptrs;
  65.     int pos;
  66.     int EAX, EBX, ECX, EDX, ESP;
  67.     int* memory;
  68.     int* stack;
  69.  
  70.     bool zflag, dflag, iflag;
  71. public:
  72.     Interpreter(string c): code(c), pos(0), EAX(0), EBX(0), ECX(0), EDX(0), ESP(0)
  73.     {
  74.         CTokenizer tk;
  75.         tk.tokenize(code, " ()[]{};\n\t", tokens);
  76.  
  77.         zflag = dflag = iflag = false;
  78.         memory = new int[MEM_SIZE];
  79.         stack = new int[MEM_SIZE];
  80.  
  81.         #ifdef DEBUG
  82.         printf("String size: %d\n", code.size());
  83.         printf("Tokens: %d\n", tokens.size());
  84.         #endif
  85.     }
  86.     void ExpectedError(string what)
  87.     {
  88.         printf("Expected: %s\n", what.c_str());
  89.         exit(1);
  90.     }
  91.     int GetPtrValue(string n)
  92.     {
  93.         int val = 0;
  94.         for(int i = 0; i < ptrs.size(); i++)
  95.         {
  96.             if(ptrs[i].name == n)
  97.             {
  98.                 val = memory[ptrs[i].addr];
  99.                 #ifdef DEBUG
  100.                 printf("PTR: %s AT: %d VALUE: %d\n", n.c_str(), ptrs[i].addr, val);
  101.                 #endif
  102.             }
  103.         }
  104.  
  105.         return val;
  106.     }
  107.     int GetPtrAddr(string n)
  108.     {
  109.         int val = 0;
  110.         for(int i = 0; i < ptrs.size(); i++)
  111.         {
  112.             if(ptrs[i].name == n)
  113.             {
  114.                 val = ptrs[i].addr;
  115.                 #ifdef DEBUG
  116.                 printf("PTR: %s AT: %d\n", n.c_str(), ptrs[i].addr);
  117.                 #endif
  118.             }
  119.         }
  120.  
  121.         return val;
  122.     }
  123.     int GetVal(string bPar)
  124.     {
  125.         int bVal = 0;
  126.         if(bPar[0] == 'e')
  127.         {
  128.             #ifdef DEBUG
  129.             puts("B PAR: REGISTER");
  130.             #endif
  131.             if(bPar == "eax")
  132.                 bVal = EAX;
  133.             else if(bPar == "ebx")
  134.                 bVal = EBX;
  135.             else if(bPar == "ecx")
  136.                 bVal = ECX;
  137.             else if(bPar == "edx")
  138.                 bVal = EDX;
  139.         }
  140.         else if(bPar[0] == '$')
  141.         {
  142.             #ifdef DEBUG
  143.             puts("B PAR: PTR");
  144.             #endif
  145.             bVal = GetPtrValue(bPar.substr(1, bPar.size()-1));
  146.         }
  147.         else if(bPar[0] == 'a')
  148.         {
  149.             #ifdef DEBUG
  150.             puts("B PAR: ADDRESS");
  151.             #endif
  152.             string addr = bPar.substr(1, bPar.size()-1);
  153.             bVal = memory[conversions::cstrtoi((char*)addr.c_str())];
  154.         }
  155.         else
  156.         {
  157.             bVal = conversions::cstrtoi((char*)bPar.c_str());
  158.             #ifdef DEBUG
  159.             printf("B VAL: INTEGER = %d\n", bVal);
  160.             #endif
  161.         }
  162.  
  163.         return bVal;
  164.     }
  165.     void AssignToReg(string aPar, int bVal)
  166.     {
  167.         if(aPar == "eax")
  168.             EAX = bVal;
  169.         else if(aPar == "ebx")
  170.             EBX = bVal;
  171.         else if(aPar == "ecx")
  172.             ECX = bVal;
  173.         else if(aPar == "edx")
  174.             EDX = bVal;
  175.         else if(aPar == "esp")
  176.             ESP = bVal;
  177.     }
  178.     void Run()
  179.     {
  180.         // registers labels
  181.         for(int i = 0; i < tokens.size(); i++)
  182.         {
  183.             string tmp = tokens[i];
  184.             if(tmp[0] == '@')
  185.             {
  186.                 ParseLabel(i);
  187.             }
  188.         }
  189.         for(int i = 0; i < tokens.size(); i++)
  190.         {
  191.             string tmp = tokens[i];
  192.             if(tokens[i] == "print")
  193.             {
  194.                 ParsePrint(i);
  195.             }
  196.             else if(tokens[i] == "cprint")
  197.             {
  198.                 ParsePrintConsole(i);
  199.             }
  200.             else if(tokens[i] == "mov")
  201.             {
  202.                 ParseMov(i);
  203.                 i--;
  204.             }
  205.             else if(tokens[i] == "ptr")
  206.             {
  207.                 ParsePtr(i);
  208.             }
  209.             else if(tokens[i] == "push")
  210.             {
  211.                 ParsePush(i);
  212.             }
  213.             else if(tokens[i] == "pushad")
  214.             {
  215.                 ParsePushd(i);
  216.             }
  217.             else if(tokens[i] == "pop")
  218.             {
  219.                 ParsePop(i);
  220.             }
  221.             else if(tokens[i] == "clc")
  222.             {
  223.                 ParseClc(i);
  224.             }
  225.             else if(tokens[i] == "cld")
  226.             {
  227.                 ParseCld(i);
  228.             }
  229.             else if(tokens[i] == "cli")
  230.             {
  231.                 ParseCli(i);
  232.             }
  233.             else if(tokens[i] == "cmc")
  234.             {
  235.                 ParseCmc(i);
  236.             }
  237.             else if(tokens[i] == "add")
  238.             {
  239.                 ParseAdd(i);
  240.             }
  241.             else if(tokens[i] == "sub")
  242.             {
  243.                 ParseSub(i);
  244.             }
  245.             else if(tokens[i] == "div")
  246.             {
  247.                 ParseDiv(i);
  248.             }
  249.             else if(tokens[i] == "mul")
  250.             {
  251.                 ParseMul(i);
  252.             }
  253.             else if(tokens[i] == "inc")
  254.             {
  255.                 ParseInc(i);
  256.             }
  257.             else if(tokens[i] == "dec")
  258.             {
  259.                 ParseDec(i);
  260.             }
  261.             else if(tokens[i] == "jmp")
  262.             {
  263.                 ParseJmp(i);
  264.             }
  265.             else if(tokens[i] == "je" || tokens[i] == "jz")
  266.             {
  267.                 ParseJe(i);
  268.             }
  269.             else if(tokens[i] == "jne" || tokens[i] == "jnz")
  270.             {
  271.                 ParseJne(i);
  272.             }
  273.             else if(tokens[i] == "cmp")
  274.             {
  275.                 ParseCmp(i);
  276.             }
  277.             else if(tokens[i] == "CLS")
  278.             {
  279.                 ParseCls(i);
  280.             }
  281.             else if(tokens[i] == "SLEEP")
  282.             {
  283.                 ParseSleep(i);
  284.             }
  285.             else if(tokens[i] == "REGDEBUG")
  286.             {
  287.                 ParseRegDebug(i);
  288.             }
  289.             else if(tokens[i] == "FLAGDEBUG")
  290.             {
  291.                 ParseFlagDebug(i);
  292.             }
  293.             else if(tokens[i] == "/help" || tokens[i] == "/?")
  294.             {
  295.                 ParseHelp(i);
  296.             }
  297.         }
  298.     }
  299.     void ParseHelp(int& i)
  300.     {
  301.         //tbox2->SetText("");
  302.         tbox2->AppendText("Help: \r\n");
  303.         tbox2->AppendText("Number of keywords: 25\r\n");
  304.         tbox2->AppendText("print text; eg. print \"This is a text\"\r\n");
  305.         tbox2->AppendText("cprint text; eg. cprint \"This is a text\"\r\n");
  306.         tbox2->AppendText("mov dest source; dest = { eax | ebx | ecx | edx | address | pointer }, source = { eax | ebx | ecx | edx | address | pointer | constant } \r\n");
  307.         tbox2->AppendText("ptr name addr; name = name of a pointer, addr = addres to which pointer points, eg. ptr abc a10; \r\n");
  308.         tbox2->AppendText("push val; val = {register | address | constant }\r\n");
  309.         tbox2->AppendText("pushd; pushes all registers to stack\r\n");
  310.         tbox2->AppendText("pop dest; pops value from top of the stack to the destination\r\n");
  311.         tbox2->AppendText("add val1 val2; adds val1 to val2 and stores it in val1\r\n");
  312.         tbox2->AppendText("sub val1 val2; subtracts val1 from val2 and stores it in val1\r\n");
  313.         tbox2->AppendText("mul val1 val2; multiplicates val1 by val2 and stores it in val1\r\n");
  314.         tbox2->AppendText("div val1 val2; divides val1 by val2 and stores it in val1\r\n");
  315.         tbox2->AppendText("inc val1; incresses val1 by 1\r\n");
  316.         tbox2->AppendText("dec val1; decresses val1 by 1\r\n");
  317.         tbox2->AppendText("jmp label; jumps to a label\r\n");
  318.         tbox2->AppendText("je label/jz label; jumps to a label if zero flag is set to 1\r\n");
  319.         tbox2->AppendText("jne label/jnz label; jumps to a label if zero flag is set to 0\r\n");
  320.         tbox2->AppendText("cmp val1 val2; compares val1 to val2\r\n");
  321.         tbox2->AppendText("REGDEBUG; Shows values of all registers\r\n");
  322.         tbox2->AppendText("FLAGDEBUG; Shows values of all flags\r\n");
  323.         tbox2->AppendText("/help shows help");
  324.     }
  325.     void ParseRegDebug(int& i)
  326.     {
  327.         printf("EAX: %d, EBX %d, ECX %d, EDX %d, ESP %d\n", EAX, EBX, ECX, EDX, ESP);
  328.     }
  329.     void ParseCls(int& i)
  330.     {
  331.         tbox2->SetText("");
  332.     }
  333.     void ParseSleep(int& i)
  334.     {
  335.         i++;
  336.         int time = conversions::cstrtoi(((char*)tokens[i].c_str()));
  337.         Sleep(time);
  338.     }
  339.     void ParseFlagDebug(int& i)
  340.     {
  341.         printf("ZF: %d, DF: %d, IF: %d\n", zflag, dflag, iflag);
  342.     }
  343.     void ParseLabel(int& i)
  344.     {
  345.         string n = tokens[i].substr(1, tokens[i].size()-1);
  346.         #ifdef DEBUG
  347.         printf("LABEL %s, VAL: %d\n", n.c_str(), i);
  348.         #endif
  349.         ptrs.push_back(IntPtr(n, i));
  350.     }
  351.     void ParseJmp(int& i)
  352.     {
  353.         i++;
  354.         string labelName = tokens[i];
  355.         int newAddr = GetPtrAddr(labelName);
  356.         #ifdef DEBUG
  357.         printf("JMP OLD ADDR: %d, NEW ADDR: %d\n", i, newAddr);
  358.         #endif
  359.         i = newAddr;
  360.     }
  361.     void ParseJe(int& i)
  362.     {
  363.         i++;
  364.         string labelName = tokens[i];
  365.         int newAddr = GetPtrAddr(labelName);
  366.         if(zflag == 1)
  367.         {
  368.             #ifdef DEBUG
  369.             printf("JE OLD ADDR: %d, NEW ADDR: %d\n", i, newAddr);
  370.             #endif
  371.             i = newAddr;
  372.         }
  373.  
  374.     }
  375.     void ParseJne(int& i)
  376.     {
  377.         i++;
  378.         string labelName = tokens[i];
  379.         int newAddr = GetPtrAddr(labelName);
  380.         if(zflag == 0)
  381.         {
  382.             #ifdef DEBUG
  383.             printf("JNE OLD ADDR: %d, NEW ADDR: %d\n", i, newAddr);
  384.             #endif
  385.             i = newAddr;
  386.         }
  387.  
  388.     }
  389.     void ParseCmp(int& i)
  390.     {
  391.         i++;
  392.         string aPar = tokens[i];
  393.         i++;
  394.         string bPar = tokens[i];
  395.  
  396.  
  397.         int bVal = GetVal(bPar);
  398.         int aVal = GetVal(aPar);
  399.         #ifdef DEBUG
  400.         printf("CMP B VAL: %d, A VAL: %d\n", bVal, aVal);
  401.         #endif
  402.  
  403.         if(aVal == bVal)
  404.             zflag = true;
  405.         else
  406.             zflag = false;
  407.  
  408.  
  409.     }
  410.     void ParsePtr(int& i)
  411.     {
  412.         i++;
  413.         string name = tokens[i];
  414.         i++;
  415.         string addr = tokens[i].substr(1, tokens[i].size()-1);
  416.  
  417.         #ifdef DEBUG
  418.         printf("PARSE PTR %s[%s]\n", name.c_str(), addr.c_str());
  419.         #endif
  420.         ptrs.push_back(IntPtr(name, conversions::cstrtoi((char*)addr.c_str())));
  421.     }
  422.     void ParseAdd(int& i)
  423.     {
  424.         i++;
  425.         string aPar = tokens[i];
  426.         i++;
  427.         string bPar = tokens[i];
  428.  
  429.  
  430.         int bVal = GetVal(bPar);
  431.         int aVal = GetVal(aPar);
  432.  
  433.         if(aVal + bVal == 0)
  434.  
  435.         #ifdef DEBUG
  436.         printf("ADD B VAL: %d\n", bVal);
  437.         #endif
  438.  
  439.         if(aPar[0] == 'e')
  440.         {
  441.             AssignToReg(aPar, aVal + bVal);
  442.         }
  443.         else if(aPar[0] == '$')
  444.         {
  445.             int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
  446.             memory[addr] = aVal + bVal;
  447.         }
  448.         else if(aPar[0] == 'a')
  449.         {
  450.             int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
  451.             memory[addr] = aVal + bVal;
  452.         }
  453.     }
  454.     void ParseSub(int& i)
  455.     {
  456.         i++;
  457.         string aPar = tokens[i];
  458.         i++;
  459.         string bPar = tokens[i];
  460.  
  461.  
  462.         int bVal = GetVal(bPar);
  463.         int aVal = GetVal(aPar);
  464.  
  465.         #ifdef DEBUG
  466.         printf("SUB B VAL: %d\n", bVal);
  467.         #endif
  468.  
  469.         if(aPar[0] == 'e')
  470.         {
  471.             AssignToReg(aPar, aVal - bVal);
  472.         }
  473.         else if(aPar[0] == '$')
  474.         {
  475.             int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
  476.             memory[addr] = aVal - bVal;
  477.         }
  478.         else if(aPar[0] == 'a')
  479.         {
  480.             int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
  481.             memory[addr] = aVal - bVal;
  482.         }
  483.     }
  484.     void ParseMul(int& i)
  485.     {
  486.         i++;
  487.         string aPar = tokens[i];
  488.         i++;
  489.         string bPar = tokens[i];
  490.  
  491.         int bVal = GetVal(bPar);
  492.         int aVal = GetVal(aPar);
  493.  
  494.         #ifdef DEBUG
  495.         printf("MUL B VAL: %d\n", bVal);
  496.         #endif
  497.  
  498.         if(aPar[0] == 'e')
  499.         {
  500.             AssignToReg(aPar, aVal * bVal);
  501.         }
  502.         else if(aPar[0] == '$')
  503.         {
  504.             int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
  505.             memory[addr] = aVal * bVal;
  506.         }
  507.         else if(aPar[0] == 'a')
  508.         {
  509.             int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
  510.             memory[addr] = aVal * bVal;
  511.         }
  512.     }
  513.     void ParseDiv(int& i)
  514.     {
  515.         i++;
  516.         string aPar = tokens[i];
  517.         i++;
  518.         string bPar = tokens[i];
  519.  
  520.  
  521.         int bVal = GetVal(bPar);
  522.         int aVal = GetVal(aPar);
  523.  
  524.         #ifdef DEBUG
  525.         printf("DIV B VAL: %d\n", bVal);
  526.         #endif
  527.  
  528.         if(aPar[0] == 'e')
  529.         {
  530.             AssignToReg(aPar, aVal / bVal);
  531.         }
  532.         else if(aPar[0] == '$')
  533.         {
  534.             int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
  535.             memory[addr] = aVal / bVal;
  536.         }
  537.         else if(aPar[0] == 'a')
  538.         {
  539.             int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
  540.             memory[addr] = aVal / bVal;
  541.         }
  542.     }
  543.     void ParseDec(int& i)
  544.     {
  545.         i++;
  546.         string aPar = tokens[i];
  547.         int aVal = GetVal(aPar);
  548.  
  549.  
  550.         if(aPar[0] == 'e')
  551.         {
  552.             AssignToReg(aPar, --aVal);
  553.         }
  554.         else if(aPar[0] == '$')
  555.         {
  556.             int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
  557.             memory[addr] = --aVal;
  558.         }
  559.         else if(aPar[0] == 'a')
  560.         {
  561.             int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
  562.             memory[addr] = --aVal;
  563.         }
  564.     }
  565.     void ParseInc(int& i)
  566.     {
  567.         i++;
  568.         string aPar = tokens[i];
  569.         int aVal = GetVal(aPar);
  570.  
  571.  
  572.         if(aPar[0] == 'e')
  573.         {
  574.             AssignToReg(aPar, ++aVal);
  575.         }
  576.         else if(aPar[0] == '$')
  577.         {
  578.             int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
  579.             memory[addr] = ++aVal;
  580.         }
  581.         else if(aPar[0] == 'a')
  582.         {
  583.             int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
  584.             memory[addr] = ++aVal;
  585.         }
  586.     }
  587.     void ParseClc(int& i)
  588.     {
  589.         zflag = false;
  590.     }
  591.     void ParseCld(int& i)
  592.     {
  593.         dflag = false;
  594.     }
  595.     void ParseCli(int& i)
  596.     {
  597.         iflag = false;
  598.     }
  599.     void ParseCmc(int& i)
  600.     {
  601.         zflag = !zflag;
  602.     }
  603.     void ParsePush(int& i)
  604.     {
  605.         i++;
  606.         string bPar = tokens[i];
  607.  
  608.         #ifdef DEBUG
  609.         printf("PARSE PUSH VAL: %d\n", GetVal(bPar));
  610.         #endif
  611.  
  612.         stack[++ESP] = GetVal(bPar);
  613.         #ifdef DEBUG
  614.         puts("PUSHED");
  615.         #endif
  616.     }
  617.     void ParsePushd(int& i)
  618.     {
  619.         stack[++ESP] = EAX;
  620.         stack[++ESP] = EBX;
  621.         stack[++ESP] = ECX;
  622.         stack[++ESP] = EDX;
  623.         stack[++ESP] = ESP;
  624.     }
  625.     void ParsePop(int& i)
  626.     {
  627.         i++;
  628.         string aPar = tokens[i];
  629.  
  630.         #ifdef DEBUG
  631.         printf("POP: %s\n", aPar.c_str());
  632.         #endif
  633.  
  634.         if(aPar[0] == 'e')
  635.         {
  636.             AssignToReg(aPar, stack[ESP]);
  637.         }
  638.         else if(aPar[0] == '$')
  639.         {
  640.             int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
  641.  
  642.             #ifdef DEBUG
  643.             printf("POP [%d] = %d\n", addr, stack[ESP]);
  644.             #endif
  645.  
  646.             memory[addr] = stack[ESP];
  647.         }
  648.         else if(aPar[0] == 'a')
  649.         {
  650.             int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
  651.             memory[addr] = stack[ESP];
  652.         }
  653.         ESP--;
  654.     }
  655.     void ParseMov(int& i)
  656.     {
  657.         int bVal = 0;
  658.         i++;
  659.         string aPar = tokens[i];
  660.         i++;
  661.         string bPar = tokens[i];
  662.  
  663.         #ifdef DEBUG
  664.         printf("MOV A PAR: %s, B PAR: %s\n", aPar.c_str(), bPar.c_str());
  665.         #endif
  666.  
  667.         bVal = GetVal(bPar);
  668.  
  669.         if(aPar[0] == 'e')
  670.         {
  671.             #ifdef DEBUG
  672.             puts("A PAR: REGISTER");
  673.             #endif
  674.  
  675.             AssignToReg(aPar, bVal);
  676.         }
  677.         else if(aPar[0] == '$')
  678.         {
  679.             int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
  680.             memory[addr] = bVal;
  681.         }
  682.         else if(aPar[0] == 'a')
  683.         {
  684.             string addr = aPar.substr(1, aPar.size()-1);
  685.             int iaddr = conversions::cstrtoi((char*)addr.c_str());
  686.  
  687.             #ifdef DEBUG
  688.             printf("A PAR: ADDR[%d] = %d\n", iaddr, bVal);
  689.             #endif
  690.  
  691.             memory[iaddr] = bVal;
  692.         }
  693.         #ifdef DEBUG
  694.         printf("B PAR VAL: %d\n", bVal);
  695.         #endif
  696.  
  697.  
  698.     }
  699.     void ParsePrint(int& i)
  700.     {
  701.         #ifdef DEBUG
  702.         puts("\nParsePrint(int)");
  703.         cout << "Next token: " << tokens[i+1] << endl;
  704.         #endif
  705.  
  706.         string tmp = tokens[i+1];
  707.         i++;
  708.         if(tmp[0] == '"')
  709.         {
  710.             #ifdef DEBUG
  711.             puts("ParsePrint(int) found \"");
  712.             #endif
  713.  
  714.             int words = 0;
  715.             string sentence = "";
  716.             for(i; i < tokens.size(); i++)
  717.             {
  718.  
  719.                 #ifdef DEBUG
  720.                 printf("Word #%d: %s\n", i, tokens[i].c_str());
  721.                 #endif
  722.  
  723.                 tmp = tokens[i];
  724.                 words++;
  725.                 sentence += tmp + " ";
  726.                 if(tmp[tmp.size()-1] != '"')
  727.                 {
  728.  
  729.                 }
  730.                 else
  731.                 {
  732.                     break;
  733.                 }
  734.             }
  735.  
  736.             cout << sentence.substr(1, sentence.size()-3);
  737.         }
  738.         else
  739.         {
  740.             ExpectedError("Opening \"");
  741.         }
  742.     }
  743.     void ParsePrintConsole(int& i)
  744.     {
  745.         #ifdef DEBUG
  746.         puts("\nParsePrint(int)");
  747.         cout << "Next token: " << tokens[i+1] << endl;
  748.         #endif
  749.         string tmp = tokens[i+1];
  750.         i++;
  751.         if(tmp[0] == '"')
  752.         {
  753.             #ifdef DEBUG
  754.             puts("ParsePrint(int) found \"");
  755.             #endif
  756.             int words = 0;
  757.             string sentence = "";
  758.             for(i; i < tokens.size(); i++)
  759.             {
  760.                 #ifdef DEBUG
  761.                 printf("Word #%d: %s\n", i, tokens[i].c_str());
  762.                 #endif
  763.                 tmp = tokens[i];
  764.                 words++;
  765.                 sentence += tmp + " ";
  766.                 if(tmp[tmp.size()-1] != '"')
  767.                 {
  768.  
  769.                 }
  770.                 else
  771.                 {
  772.                     break;
  773.                 }
  774.             }
  775.  
  776.  
  777.             tbox2->AppendText(sentence.substr(1, sentence.size()-3));
  778.             //cout << sentence.substr(1, sentence.size()-3);
  779.         }
  780.         else
  781.         {
  782.             char integer[9];
  783.             int val = GetVal(tmp);
  784.             cout << val << endl;
  785.             tbox2->AppendText(itoa(val, integer, 10));
  786.         }
  787.     }
  788.  
  789. };
  790.  
  791. #endif
  792.  
  793.  
  794. double OpenFile()
  795. {
  796.  
  797.     int option = MessageBox(wnd->GetHWND(), "Do you want to open a new file?", "Information", MB_YESNO);
  798.  
  799.     if(option == IDYES){
  800.     OpenFileDialog dlg(wnd->GetHWND(), fileName);
  801.  
  802.     File file(fileName);
  803.     tbox1->SetText(file.OpenAndRead());
  804.     lbl1->SetText(fileName);
  805.     }
  806. }
  807. double SaveFile()
  808. {
  809.     File file(fileName);
  810.     file.OpenAndWrite(tbox1->GetText());
  811. }
  812. double NewFile()
  813. {
  814.     int option = MessageBox(wnd->GetHWND(), "Do you want to create a new file?", "Information", MB_YESNO);
  815.  
  816.     if(option == IDYES){
  817.         tbox1->SetText("");
  818.     }
  819. }
  820. double Run()
  821. {
  822.     tbox2->SetText("");
  823.     Interpreter interpreter(tbox1->GetText());
  824.     interpreter.Run();
  825. }
  826.  
  827. WPARAM win32Main()
  828. {
  829.     wnd = new SingleWindow("Interpreter v0.1", 600, 380, FIXEDWINDOW);
  830.     //open = new Button(ID_OPEN, wnd->GetHWND(), "Open", BUTTON, 10, 290, 100, 30);
  831.     tbox1 = new TextBox(ID_TBOX1, wnd->GetHWND(), NULL, 0, 26, 590, 219);
  832.     tbox2 = new TextBox(ID_TBOX2, wnd->GetHWND(), NULL, 0, 250, 590, 100);
  833.     HBRUSH hdcEdit = CreateSolidBrush(RGB(0, 0, 0));
  834.     InvalidateRect(tbox2->GetHWND(), NULL, TRUE);
  835.     SetClassLongPtr(tbox2->GetHWND(), GCLP_HBRBACKGROUND,(LONG_PTR)hdcEdit);
  836.    
  837.     lbl1 = new Label(ID_LBL1, wnd->GetHWND(), "No file opened.", 100, 9, 590, 15);
  838.     HFONT hFont = CreateFont(18,9, 0, 0, 400, false, false, false, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "DejaVu Sans Mono");
  839.     HFONT hFont2 = CreateFont(17,8, 0, 0, 400, false, false, false, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Consolas");
  840.     SendMessage(tbox1->GetHWND(), WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
  841.     SendMessage(tbox2->GetHWND(), WM_SETFONT, (WPARAM)hFont2, MAKELPARAM(TRUE, 0));
  842.     TBBUTTON tbb[4];
  843.  
  844.     ZeroMemory (tbb, sizeof(tbb));
  845.     for (int i = 0; i < 4; ++i) {
  846.      tbb[i].idCommand = i;
  847.      tbb[i].iBitmap = tbb[i].iString = i;
  848.      tbb[i].fsState = TBSTATE_ENABLED;
  849.      tbb[i].fsStyle = TBSTYLE_BUTTON;
  850.     }
  851.  
  852.     tbar1 = new Toolbar(ID_TBAR, wnd->GetHWND(), 4, "buttons2.bmp", tbb);
  853.  
  854.     tbar1->AddEvent(NewFile, ID_NEWFILE);
  855.     tbar1->AddEvent(OpenFile, ID_OPENFILE);
  856.     tbar1->AddEvent(SaveFile, ID_SAVEFILE);
  857.     tbar1->AddEvent(Run, ID_RUN);
  858.  
  859.     tbox2->SetText("Type /help or /? and run to see help\r\n");
  860.     lbl1->SetText("No file opened.");
  861.     return wnd->MessageLoop();
  862. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement