-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
53 lines (47 loc) · 1.61 KB
/
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
#include <iostream>
#include <string>
#include "Assembler.h"
#include "VirtualMachine.h"
int main( int argc, char** argv )
{
int returnCode = 0;
if( argc < 2 || std::string( argv[1] ) == "-h" || std::string( argv[1] ) == "--help" ) {
std::cout << "Virtual Machine based on Von Neumann architecture\n"
"Usage:\n\tvnvm assembler <code.asm> [out.bin]- build binary file from assembler code\n"
"\tvnvm exec <filename.bin> - execute binary file\n"
"\nMore info on https://github.com/ivigns/virtual-machine\n";
} else if( std::string( argv[1] ) == "assembler" ) {
if( argc < 3 ) {
std::cerr << argv[1] << ": Not enough arguments.\n";
return 1;
}
CAssembler assembler;
try {
if( argc < 4 ) {
assembler.Compile( argv[2] );
} else {
assembler.Compile( argv[2], argv[3] );
}
} catch( std::exception& exception ) {
std::cerr << argv[1] << ": " << exception.what() << "\n";
return 1;
}
} else if( std::string( argv[1] ) == "exec" ) {
if( argc < 3 ) {
std::cerr << argv[1] << ": not enough arguments\n";
return 1;
}
CVirtualMachine vm;
vm.Load( argv[2] );
try {
returnCode = vm.Exec();
} catch( std::exception& exception ) {
std::cerr << argv[1] << ": " << exception.what() << "\n";
return 1;
}
} else {
std::cerr << argv[1] << ": unknown command\n";
return 1;
}
return returnCode;
}