Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <win32\singlewindow.h>
- #include <win32\textbox.h>
- #include <win32\toolbar.h>
- #include <win32\filedialog.h>
- #include <win32\label.h>
- #include <win32\file.h>
- #include <headers\tokenizer.h>
- #include <headers\conversions.h>
- #include <iostream>
- #include <iostream>
- #include <string>
- #include <vector>
- #include <cstdio>
- #include <cctype>
- #define ID_OPEN 501
- #define ID_TBOX1 502
- #define ID_TBOX2 503
- #define ID_TBAR 504
- #define ID_LBL1 505
- #define ID_NEWFILE 0
- #define ID_OPENFILE 1
- #define ID_SAVEFILE 2
- #define ID_RUN 3
- #define MEM_SIZE 0x1000
- //#define DEBUG
- char fileName[MAX_PATH] = "";
- SingleWindow* wnd;
- TextBox* tbox1;
- TextBox* tbox2;
- Toolbar* tbar1;
- Label* lbl1;
- using namespace std;
- #ifndef INTERPRETER
- #define INTERPRETER
- struct IntPtr
- {
- string name;
- int addr;
- IntPtr(string s, int n) : name(s), addr(n)
- {
- }
- };
- class Interpreter
- {
- string code;
- vector<string> tokens;
- vector<IntPtr> ptrs;
- int pos;
- int EAX, EBX, ECX, EDX, ESP;
- int* memory;
- int* stack;
- bool zflag, dflag, iflag;
- public:
- Interpreter(string c): code(c), pos(0), EAX(0), EBX(0), ECX(0), EDX(0), ESP(0)
- {
- CTokenizer tk;
- tk.tokenize(code, " ()[]{};\n\t", tokens);
- zflag = dflag = iflag = false;
- memory = new int[MEM_SIZE];
- stack = new int[MEM_SIZE];
- #ifdef DEBUG
- printf("String size: %d\n", code.size());
- printf("Tokens: %d\n", tokens.size());
- #endif
- }
- void ExpectedError(string what)
- {
- printf("Expected: %s\n", what.c_str());
- exit(1);
- }
- int GetPtrValue(string n)
- {
- int val = 0;
- for(int i = 0; i < ptrs.size(); i++)
- {
- if(ptrs[i].name == n)
- {
- val = memory[ptrs[i].addr];
- #ifdef DEBUG
- printf("PTR: %s AT: %d VALUE: %d\n", n.c_str(), ptrs[i].addr, val);
- #endif
- }
- }
- return val;
- }
- int GetPtrAddr(string n)
- {
- int val = 0;
- for(int i = 0; i < ptrs.size(); i++)
- {
- if(ptrs[i].name == n)
- {
- val = ptrs[i].addr;
- #ifdef DEBUG
- printf("PTR: %s AT: %d\n", n.c_str(), ptrs[i].addr);
- #endif
- }
- }
- return val;
- }
- int GetVal(string bPar)
- {
- int bVal = 0;
- if(bPar[0] == 'e')
- {
- #ifdef DEBUG
- puts("B PAR: REGISTER");
- #endif
- if(bPar == "eax")
- bVal = EAX;
- else if(bPar == "ebx")
- bVal = EBX;
- else if(bPar == "ecx")
- bVal = ECX;
- else if(bPar == "edx")
- bVal = EDX;
- }
- else if(bPar[0] == '$')
- {
- #ifdef DEBUG
- puts("B PAR: PTR");
- #endif
- bVal = GetPtrValue(bPar.substr(1, bPar.size()-1));
- }
- else if(bPar[0] == 'a')
- {
- #ifdef DEBUG
- puts("B PAR: ADDRESS");
- #endif
- string addr = bPar.substr(1, bPar.size()-1);
- bVal = memory[conversions::cstrtoi((char*)addr.c_str())];
- }
- else
- {
- bVal = conversions::cstrtoi((char*)bPar.c_str());
- #ifdef DEBUG
- printf("B VAL: INTEGER = %d\n", bVal);
- #endif
- }
- return bVal;
- }
- void AssignToReg(string aPar, int bVal)
- {
- if(aPar == "eax")
- EAX = bVal;
- else if(aPar == "ebx")
- EBX = bVal;
- else if(aPar == "ecx")
- ECX = bVal;
- else if(aPar == "edx")
- EDX = bVal;
- else if(aPar == "esp")
- ESP = bVal;
- }
- void Run()
- {
- // registers labels
- for(int i = 0; i < tokens.size(); i++)
- {
- string tmp = tokens[i];
- if(tmp[0] == '@')
- {
- ParseLabel(i);
- }
- }
- for(int i = 0; i < tokens.size(); i++)
- {
- string tmp = tokens[i];
- if(tokens[i] == "print")
- {
- ParsePrint(i);
- }
- else if(tokens[i] == "cprint")
- {
- ParsePrintConsole(i);
- }
- else if(tokens[i] == "mov")
- {
- ParseMov(i);
- i--;
- }
- else if(tokens[i] == "ptr")
- {
- ParsePtr(i);
- }
- else if(tokens[i] == "push")
- {
- ParsePush(i);
- }
- else if(tokens[i] == "pushad")
- {
- ParsePushd(i);
- }
- else if(tokens[i] == "pop")
- {
- ParsePop(i);
- }
- else if(tokens[i] == "clc")
- {
- ParseClc(i);
- }
- else if(tokens[i] == "cld")
- {
- ParseCld(i);
- }
- else if(tokens[i] == "cli")
- {
- ParseCli(i);
- }
- else if(tokens[i] == "cmc")
- {
- ParseCmc(i);
- }
- else if(tokens[i] == "add")
- {
- ParseAdd(i);
- }
- else if(tokens[i] == "sub")
- {
- ParseSub(i);
- }
- else if(tokens[i] == "div")
- {
- ParseDiv(i);
- }
- else if(tokens[i] == "mul")
- {
- ParseMul(i);
- }
- else if(tokens[i] == "inc")
- {
- ParseInc(i);
- }
- else if(tokens[i] == "dec")
- {
- ParseDec(i);
- }
- else if(tokens[i] == "jmp")
- {
- ParseJmp(i);
- }
- else if(tokens[i] == "je" || tokens[i] == "jz")
- {
- ParseJe(i);
- }
- else if(tokens[i] == "jne" || tokens[i] == "jnz")
- {
- ParseJne(i);
- }
- else if(tokens[i] == "cmp")
- {
- ParseCmp(i);
- }
- else if(tokens[i] == "CLS")
- {
- ParseCls(i);
- }
- else if(tokens[i] == "SLEEP")
- {
- ParseSleep(i);
- }
- else if(tokens[i] == "REGDEBUG")
- {
- ParseRegDebug(i);
- }
- else if(tokens[i] == "FLAGDEBUG")
- {
- ParseFlagDebug(i);
- }
- else if(tokens[i] == "/help" || tokens[i] == "/?")
- {
- ParseHelp(i);
- }
- }
- }
- void ParseHelp(int& i)
- {
- //tbox2->SetText("");
- tbox2->AppendText("Help: \r\n");
- tbox2->AppendText("Number of keywords: 25\r\n");
- tbox2->AppendText("print text; eg. print \"This is a text\"\r\n");
- tbox2->AppendText("cprint text; eg. cprint \"This is a text\"\r\n");
- tbox2->AppendText("mov dest source; dest = { eax | ebx | ecx | edx | address | pointer }, source = { eax | ebx | ecx | edx | address | pointer | constant } \r\n");
- tbox2->AppendText("ptr name addr; name = name of a pointer, addr = addres to which pointer points, eg. ptr abc a10; \r\n");
- tbox2->AppendText("push val; val = {register | address | constant }\r\n");
- tbox2->AppendText("pushd; pushes all registers to stack\r\n");
- tbox2->AppendText("pop dest; pops value from top of the stack to the destination\r\n");
- tbox2->AppendText("add val1 val2; adds val1 to val2 and stores it in val1\r\n");
- tbox2->AppendText("sub val1 val2; subtracts val1 from val2 and stores it in val1\r\n");
- tbox2->AppendText("mul val1 val2; multiplicates val1 by val2 and stores it in val1\r\n");
- tbox2->AppendText("div val1 val2; divides val1 by val2 and stores it in val1\r\n");
- tbox2->AppendText("inc val1; incresses val1 by 1\r\n");
- tbox2->AppendText("dec val1; decresses val1 by 1\r\n");
- tbox2->AppendText("jmp label; jumps to a label\r\n");
- tbox2->AppendText("je label/jz label; jumps to a label if zero flag is set to 1\r\n");
- tbox2->AppendText("jne label/jnz label; jumps to a label if zero flag is set to 0\r\n");
- tbox2->AppendText("cmp val1 val2; compares val1 to val2\r\n");
- tbox2->AppendText("REGDEBUG; Shows values of all registers\r\n");
- tbox2->AppendText("FLAGDEBUG; Shows values of all flags\r\n");
- tbox2->AppendText("/help shows help");
- }
- void ParseRegDebug(int& i)
- {
- printf("EAX: %d, EBX %d, ECX %d, EDX %d, ESP %d\n", EAX, EBX, ECX, EDX, ESP);
- }
- void ParseCls(int& i)
- {
- tbox2->SetText("");
- }
- void ParseSleep(int& i)
- {
- i++;
- int time = conversions::cstrtoi(((char*)tokens[i].c_str()));
- Sleep(time);
- }
- void ParseFlagDebug(int& i)
- {
- printf("ZF: %d, DF: %d, IF: %d\n", zflag, dflag, iflag);
- }
- void ParseLabel(int& i)
- {
- string n = tokens[i].substr(1, tokens[i].size()-1);
- #ifdef DEBUG
- printf("LABEL %s, VAL: %d\n", n.c_str(), i);
- #endif
- ptrs.push_back(IntPtr(n, i));
- }
- void ParseJmp(int& i)
- {
- i++;
- string labelName = tokens[i];
- int newAddr = GetPtrAddr(labelName);
- #ifdef DEBUG
- printf("JMP OLD ADDR: %d, NEW ADDR: %d\n", i, newAddr);
- #endif
- i = newAddr;
- }
- void ParseJe(int& i)
- {
- i++;
- string labelName = tokens[i];
- int newAddr = GetPtrAddr(labelName);
- if(zflag == 1)
- {
- #ifdef DEBUG
- printf("JE OLD ADDR: %d, NEW ADDR: %d\n", i, newAddr);
- #endif
- i = newAddr;
- }
- }
- void ParseJne(int& i)
- {
- i++;
- string labelName = tokens[i];
- int newAddr = GetPtrAddr(labelName);
- if(zflag == 0)
- {
- #ifdef DEBUG
- printf("JNE OLD ADDR: %d, NEW ADDR: %d\n", i, newAddr);
- #endif
- i = newAddr;
- }
- }
- void ParseCmp(int& i)
- {
- i++;
- string aPar = tokens[i];
- i++;
- string bPar = tokens[i];
- int bVal = GetVal(bPar);
- int aVal = GetVal(aPar);
- #ifdef DEBUG
- printf("CMP B VAL: %d, A VAL: %d\n", bVal, aVal);
- #endif
- if(aVal == bVal)
- zflag = true;
- else
- zflag = false;
- }
- void ParsePtr(int& i)
- {
- i++;
- string name = tokens[i];
- i++;
- string addr = tokens[i].substr(1, tokens[i].size()-1);
- #ifdef DEBUG
- printf("PARSE PTR %s[%s]\n", name.c_str(), addr.c_str());
- #endif
- ptrs.push_back(IntPtr(name, conversions::cstrtoi((char*)addr.c_str())));
- }
- void ParseAdd(int& i)
- {
- i++;
- string aPar = tokens[i];
- i++;
- string bPar = tokens[i];
- int bVal = GetVal(bPar);
- int aVal = GetVal(aPar);
- if(aVal + bVal == 0)
- #ifdef DEBUG
- printf("ADD B VAL: %d\n", bVal);
- #endif
- if(aPar[0] == 'e')
- {
- AssignToReg(aPar, aVal + bVal);
- }
- else if(aPar[0] == '$')
- {
- int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
- memory[addr] = aVal + bVal;
- }
- else if(aPar[0] == 'a')
- {
- int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
- memory[addr] = aVal + bVal;
- }
- }
- void ParseSub(int& i)
- {
- i++;
- string aPar = tokens[i];
- i++;
- string bPar = tokens[i];
- int bVal = GetVal(bPar);
- int aVal = GetVal(aPar);
- #ifdef DEBUG
- printf("SUB B VAL: %d\n", bVal);
- #endif
- if(aPar[0] == 'e')
- {
- AssignToReg(aPar, aVal - bVal);
- }
- else if(aPar[0] == '$')
- {
- int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
- memory[addr] = aVal - bVal;
- }
- else if(aPar[0] == 'a')
- {
- int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
- memory[addr] = aVal - bVal;
- }
- }
- void ParseMul(int& i)
- {
- i++;
- string aPar = tokens[i];
- i++;
- string bPar = tokens[i];
- int bVal = GetVal(bPar);
- int aVal = GetVal(aPar);
- #ifdef DEBUG
- printf("MUL B VAL: %d\n", bVal);
- #endif
- if(aPar[0] == 'e')
- {
- AssignToReg(aPar, aVal * bVal);
- }
- else if(aPar[0] == '$')
- {
- int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
- memory[addr] = aVal * bVal;
- }
- else if(aPar[0] == 'a')
- {
- int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
- memory[addr] = aVal * bVal;
- }
- }
- void ParseDiv(int& i)
- {
- i++;
- string aPar = tokens[i];
- i++;
- string bPar = tokens[i];
- int bVal = GetVal(bPar);
- int aVal = GetVal(aPar);
- #ifdef DEBUG
- printf("DIV B VAL: %d\n", bVal);
- #endif
- if(aPar[0] == 'e')
- {
- AssignToReg(aPar, aVal / bVal);
- }
- else if(aPar[0] == '$')
- {
- int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
- memory[addr] = aVal / bVal;
- }
- else if(aPar[0] == 'a')
- {
- int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
- memory[addr] = aVal / bVal;
- }
- }
- void ParseDec(int& i)
- {
- i++;
- string aPar = tokens[i];
- int aVal = GetVal(aPar);
- if(aPar[0] == 'e')
- {
- AssignToReg(aPar, --aVal);
- }
- else if(aPar[0] == '$')
- {
- int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
- memory[addr] = --aVal;
- }
- else if(aPar[0] == 'a')
- {
- int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
- memory[addr] = --aVal;
- }
- }
- void ParseInc(int& i)
- {
- i++;
- string aPar = tokens[i];
- int aVal = GetVal(aPar);
- if(aPar[0] == 'e')
- {
- AssignToReg(aPar, ++aVal);
- }
- else if(aPar[0] == '$')
- {
- int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
- memory[addr] = ++aVal;
- }
- else if(aPar[0] == 'a')
- {
- int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
- memory[addr] = ++aVal;
- }
- }
- void ParseClc(int& i)
- {
- zflag = false;
- }
- void ParseCld(int& i)
- {
- dflag = false;
- }
- void ParseCli(int& i)
- {
- iflag = false;
- }
- void ParseCmc(int& i)
- {
- zflag = !zflag;
- }
- void ParsePush(int& i)
- {
- i++;
- string bPar = tokens[i];
- #ifdef DEBUG
- printf("PARSE PUSH VAL: %d\n", GetVal(bPar));
- #endif
- stack[++ESP] = GetVal(bPar);
- #ifdef DEBUG
- puts("PUSHED");
- #endif
- }
- void ParsePushd(int& i)
- {
- stack[++ESP] = EAX;
- stack[++ESP] = EBX;
- stack[++ESP] = ECX;
- stack[++ESP] = EDX;
- stack[++ESP] = ESP;
- }
- void ParsePop(int& i)
- {
- i++;
- string aPar = tokens[i];
- #ifdef DEBUG
- printf("POP: %s\n", aPar.c_str());
- #endif
- if(aPar[0] == 'e')
- {
- AssignToReg(aPar, stack[ESP]);
- }
- else if(aPar[0] == '$')
- {
- int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
- #ifdef DEBUG
- printf("POP [%d] = %d\n", addr, stack[ESP]);
- #endif
- memory[addr] = stack[ESP];
- }
- else if(aPar[0] == 'a')
- {
- int addr = conversions::cstrtoi((char*)aPar.substr(1, aPar.size()-1).c_str());
- memory[addr] = stack[ESP];
- }
- ESP--;
- }
- void ParseMov(int& i)
- {
- int bVal = 0;
- i++;
- string aPar = tokens[i];
- i++;
- string bPar = tokens[i];
- #ifdef DEBUG
- printf("MOV A PAR: %s, B PAR: %s\n", aPar.c_str(), bPar.c_str());
- #endif
- bVal = GetVal(bPar);
- if(aPar[0] == 'e')
- {
- #ifdef DEBUG
- puts("A PAR: REGISTER");
- #endif
- AssignToReg(aPar, bVal);
- }
- else if(aPar[0] == '$')
- {
- int addr = GetPtrAddr(aPar.substr(1, aPar.size()-1));
- memory[addr] = bVal;
- }
- else if(aPar[0] == 'a')
- {
- string addr = aPar.substr(1, aPar.size()-1);
- int iaddr = conversions::cstrtoi((char*)addr.c_str());
- #ifdef DEBUG
- printf("A PAR: ADDR[%d] = %d\n", iaddr, bVal);
- #endif
- memory[iaddr] = bVal;
- }
- #ifdef DEBUG
- printf("B PAR VAL: %d\n", bVal);
- #endif
- }
- void ParsePrint(int& i)
- {
- #ifdef DEBUG
- puts("\nParsePrint(int)");
- cout << "Next token: " << tokens[i+1] << endl;
- #endif
- string tmp = tokens[i+1];
- i++;
- if(tmp[0] == '"')
- {
- #ifdef DEBUG
- puts("ParsePrint(int) found \"");
- #endif
- int words = 0;
- string sentence = "";
- for(i; i < tokens.size(); i++)
- {
- #ifdef DEBUG
- printf("Word #%d: %s\n", i, tokens[i].c_str());
- #endif
- tmp = tokens[i];
- words++;
- sentence += tmp + " ";
- if(tmp[tmp.size()-1] != '"')
- {
- }
- else
- {
- break;
- }
- }
- cout << sentence.substr(1, sentence.size()-3);
- }
- else
- {
- ExpectedError("Opening \"");
- }
- }
- void ParsePrintConsole(int& i)
- {
- #ifdef DEBUG
- puts("\nParsePrint(int)");
- cout << "Next token: " << tokens[i+1] << endl;
- #endif
- string tmp = tokens[i+1];
- i++;
- if(tmp[0] == '"')
- {
- #ifdef DEBUG
- puts("ParsePrint(int) found \"");
- #endif
- int words = 0;
- string sentence = "";
- for(i; i < tokens.size(); i++)
- {
- #ifdef DEBUG
- printf("Word #%d: %s\n", i, tokens[i].c_str());
- #endif
- tmp = tokens[i];
- words++;
- sentence += tmp + " ";
- if(tmp[tmp.size()-1] != '"')
- {
- }
- else
- {
- break;
- }
- }
- tbox2->AppendText(sentence.substr(1, sentence.size()-3));
- //cout << sentence.substr(1, sentence.size()-3);
- }
- else
- {
- char integer[9];
- int val = GetVal(tmp);
- cout << val << endl;
- tbox2->AppendText(itoa(val, integer, 10));
- }
- }
- };
- #endif
- double OpenFile()
- {
- int option = MessageBox(wnd->GetHWND(), "Do you want to open a new file?", "Information", MB_YESNO);
- if(option == IDYES){
- OpenFileDialog dlg(wnd->GetHWND(), fileName);
- File file(fileName);
- tbox1->SetText(file.OpenAndRead());
- lbl1->SetText(fileName);
- }
- }
- double SaveFile()
- {
- File file(fileName);
- file.OpenAndWrite(tbox1->GetText());
- }
- double NewFile()
- {
- int option = MessageBox(wnd->GetHWND(), "Do you want to create a new file?", "Information", MB_YESNO);
- if(option == IDYES){
- tbox1->SetText("");
- }
- }
- double Run()
- {
- tbox2->SetText("");
- Interpreter interpreter(tbox1->GetText());
- interpreter.Run();
- }
- WPARAM win32Main()
- {
- wnd = new SingleWindow("Interpreter v0.1", 600, 380, FIXEDWINDOW);
- //open = new Button(ID_OPEN, wnd->GetHWND(), "Open", BUTTON, 10, 290, 100, 30);
- tbox1 = new TextBox(ID_TBOX1, wnd->GetHWND(), NULL, 0, 26, 590, 219);
- tbox2 = new TextBox(ID_TBOX2, wnd->GetHWND(), NULL, 0, 250, 590, 100);
- HBRUSH hdcEdit = CreateSolidBrush(RGB(0, 0, 0));
- InvalidateRect(tbox2->GetHWND(), NULL, TRUE);
- SetClassLongPtr(tbox2->GetHWND(), GCLP_HBRBACKGROUND,(LONG_PTR)hdcEdit);
- lbl1 = new Label(ID_LBL1, wnd->GetHWND(), "No file opened.", 100, 9, 590, 15);
- 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");
- HFONT hFont2 = CreateFont(17,8, 0, 0, 400, false, false, false, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Consolas");
- SendMessage(tbox1->GetHWND(), WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
- SendMessage(tbox2->GetHWND(), WM_SETFONT, (WPARAM)hFont2, MAKELPARAM(TRUE, 0));
- TBBUTTON tbb[4];
- ZeroMemory (tbb, sizeof(tbb));
- for (int i = 0; i < 4; ++i) {
- tbb[i].idCommand = i;
- tbb[i].iBitmap = tbb[i].iString = i;
- tbb[i].fsState = TBSTATE_ENABLED;
- tbb[i].fsStyle = TBSTYLE_BUTTON;
- }
- tbar1 = new Toolbar(ID_TBAR, wnd->GetHWND(), 4, "buttons2.bmp", tbb);
- tbar1->AddEvent(NewFile, ID_NEWFILE);
- tbar1->AddEvent(OpenFile, ID_OPENFILE);
- tbar1->AddEvent(SaveFile, ID_SAVEFILE);
- tbar1->AddEvent(Run, ID_RUN);
- tbox2->SetText("Type /help or /? and run to see help\r\n");
- lbl1->SetText("No file opened.");
- return wnd->MessageLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement