-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
130 lines (109 loc) · 3.65 KB
/
Copy pathmain.cpp
File metadata and controls
130 lines (109 loc) · 3.65 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <string>
#include "VirtualMachine.h"
#include "AssemblyCompiler.h"
#include "Opcode.h"
static const std::string AssemblyExtension(".bca");
static const std::string ExecutableExtension(".bce");
bool hasEnding (std::string const &fullString, std::string const &ending)
{
if (fullString.length() >= ending.length())
{
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
}
else
{
return false;
}
}
int main(int argc, char** argv)
{
if(argc != 3)
{
std::cout << "usage: [operation] [filename]" << std::endl;
return 1;
}
std::string filename = argv[2];
if(std::string(argv[1]) == "run")
{
std::cout << "running " << filename << std::endl;
std::cout << std::endl;
//Create a new VM / interpreter
VirtualMachine* pVM = new VirtualMachine();
pVM->LoadProgram(filename);
pVM->Interpret();
delete pVM;
pVM = nullptr;
std::cout << std::endl;
std::cout << "=======================" << std::endl;
std::cout << "script execution ended!" << std::endl;
}
else if(std::string(argv[1]) == "compile")
{
std::cout << "compiling " << filename << std::endl;
std::cout << std::endl;
AssemblyCompiler* pCmp = new AssemblyCompiler();
pCmp->LoadSource(filename);
pCmp->Compile();
std::string outname;
if(hasEnding(filename, AssemblyExtension))
{
outname = filename.substr(0, filename.size() - AssemblyExtension.size()) + ExecutableExtension;
}
else outname = filename + ExecutableExtension;
pCmp->Save(outname);
std::cout << std::endl;
std::cout << "=======================" << std::endl;
if(!(pCmp->GetState() == AssemblyCompiler::CompState::COMPILED))
{
std::cout << "script compilation failed!" << std::endl;
delete pCmp;
pCmp = nullptr;
return 3;
}
else
{
std::cout << "script compilation succeded! >> output file: " << outname << std::endl;
}
delete pCmp;
pCmp = nullptr;
}
else if(std::string(argv[1]) == "cRun")
{
std::cout << "compiling " << filename << std::endl;
std::cout << std::endl;
AssemblyCompiler* pCmp = new AssemblyCompiler();
pCmp->LoadSource(filename);
pCmp->Compile();
std::cout << std::endl;
std::cout << "=======================" << std::endl;
if(!(pCmp->GetState() == AssemblyCompiler::CompState::COMPILED))
{
std::cout << "script compilation failed!" << std::endl;
delete pCmp;
pCmp = nullptr;
return 3;
}
std::cout << "running " << filename << std::endl;
std::cout << std::endl;
VirtualMachine* pVM = new VirtualMachine();
pVM->SetProgram(pCmp->GetBytecode());
delete pCmp;
pCmp = nullptr;
pVM->Interpret();
delete pVM;
pVM = nullptr;
std::cout << std::endl;
std::cout << "=======================" << std::endl;
}
else
{
std::cout << "OPERATION NOT RECOGNIZED!" << std::endl;
std::cout << "usage: [operation] [filename]" << std::endl;
std::cout << "operations: " << std::endl;
std::cout << "\trun >> Run virtual machine with executable bytecode" << std::endl;
std::cout << "\tcompile >> compile assembly code to executable bytecode" << std::endl;
return 2;
}
return 0;
}