-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWVM_Main.cpp
85 lines (70 loc) · 1.97 KB
/
SWVM_Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "stdafx.h"
#include "stack"
#include "DataType.h"
#include "VM_instructions.h"
#include "VirtualMachine.h"
#include "EncodeConvHelper.h"
#include "error_messages.h"
#define STACK_INITSIZE 100
// the stack
// because all the data in
std::vector<CVirtualMachineInstruction> vec_inst;
//////////////////////////////////////////////
//////////////////////////////////////////////
// wmain parameters
//
CFileEncodeIO fil_coder;
int wmain(int argc, wchar_t** argv)
{
bool isText = false;
wchar_t *source_file = NULL;
for (int i = 1; i < argc; ++i)
{
if (wcscmp(argv[i], L"-t") == 0)
isText = true;
else
source_file = argv[i];
}
if (!source_file)
{
printf("Error: no assembly file entered.\n");
exit(IDS_ERROR_VM_NOFILE);
}
// text format
bool bRet = fil_coder.LoadFile(source_file, TRUE);
if (!bRet)
{
printf("Open file \'%ws\' failed.", source_file);
exit(IDS_ERROR_VM_FILEERROR);
}
CInstructionTranslator translator;
const wchar_t *file_data = fil_coder.GetValue();
std::wstringstream din(file_data);
size_t iLine = 0;
while (!din.eof())
{
iLine++;
std::wstring line;
std::getline(din, line);
if (line.size() == 0)
continue;
std::wstringstream linein(line);
std::wstring code_str;
__int64 p_a = 0, p_b = 0;
linein >> code_str >> p_a >> p_b;
CVirtualMachineInstruction inst(VI_NOP, p_a, p_b);
bRet = translator.GetInstruction(code_str, inst.inst);
if (!bRet)
{
printf("On Line %zd,\n", iLine);
wprintf(L"%ws\n\n", line.c_str());
wprintf(L"invalid code \'%ws\'.\n", code_str.c_str());
exit(IDS_ERROR_VM_INSTRUCTION);
}
vec_inst.push_back(inst);
}
CVirutalMachineInterpreter prog;
if (vec_inst.size() > 0)
prog.interpret(&vec_inst[0]);
return 0;
}