-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
77 lines (66 loc) · 1.43 KB
/
main.cc
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 <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <iostream>
#include <string>
#include "parser.h"
#include "assembler.h"
#include "logging.h"
Logging lg = "main.cc";
void print_usage(const char* pname)
{
printf("Usage: %s [-o OUTPUT] FILES...\n", pname);
}
int main(int argc, char** argv)
{
if (argc <= 1)
{
print_usage(argv[0]);
return 1;
}
std::string outfile = "a.out";
std::string fn;
int c;
while ((c=getopt(argc, argv, "o:hu")) != EOF)
{
switch (c)
{
case 'o':
outfile = optarg;
break;
case 'h':
case 'u':
print_usage(argv[0]);
return 0;
break;
case '?':
default:
print_usage(argv[0]);
return 1;
break;
}
}
if (optind == argc)
{
print_usage(argv[0]);
return 1;
}
fn = argv[optind++];
ASMAssembler assembler(fn, 0);
for (int i = optind; i < argc; ++i)
{
fn = argv[i];
assembler.add_file(fn);
}
std::ustring pog = assembler.assemble();
FILE *f = fopen(outfile.c_str(), "w");
if (!f)
{
perror("Unable to open file");
exit(1);
}
for (std::ustring::iterator it = pog.begin(); it != pog.end(); ++it)
fputc(*it, f);
fclose(f);
return 0;
}