-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
78 lines (72 loc) · 1.86 KB
/
main.cpp
File metadata and controls
78 lines (72 loc) · 1.86 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
#include "Commands.hpp"
#include "Assembler.hpp"
#include "MachineCode.hpp"
#include "errorChecking.hpp"
#include "printHex.hpp"
#include "Error.hpp"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
extern "C" {
int yyparse();
}
Assembler ASSEMBLER;
Commands& COMMANDS = ASSEMBLER;
const char* filename = "STDIN";
const char* outputFilename = "/dev/stdout";
const char* symbolsFilename = nullptr;
const char* relocationsFilename = nullptr;
std::ofstream outputFileStream;
int main(int argc, const char* argv[]) {
try {
int numOptions = 0;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
if (argv[i][1] == 'o') {
outputFilename = argv[i+1];
numOptions += 2;
}
else if (argv[i][1] == 's') {
symbolsFilename = argv[i+1];
numOptions += 2;
}
else if (argv[i][1] == 'r') {
relocationsFilename = argv[i+1];
numOptions += 2;
}
else {
throw Error(std::string("Unknown command line option ") + argv[i]);
}
}
}
if (argc > numOptions + 1) {
extern FILE* yyin;
filename = argv[numOptions + 1];
FILE* f = fopen(filename, "r");
if (!f) {
fprintf(stderr, "fatal error: could not open input file %s\n", argv[1]);
exit(-1);
}
yyin = f;
}
yyparse();
ASSEMBLER.resolveRemaining();
if (NUMBER_OF_ERRORS > 0)
return 1;
outputFileStream.open(outputFilename);
printHex(ASSEMBLER.segments(), outputFileStream);
if (symbolsFilename) {
std::ofstream symbolsStream(symbolsFilename);
ASSEMBLER.printSymbolTable(symbolsStream);
}
if (relocationsFilename) {
ASSEMBLER.printRelocations(std::cout);
}
return 0;
}
catch (const Error& e) {
std::cerr << e.message() << std::endl;
}
}