-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.d
executable file
·68 lines (58 loc) · 1.82 KB
/
main.d
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
module main;
import core.stdc.stdlib: exit;
import std.stdio: File, writefln;
import std.getopt: config, getopt, defaultGetoptPrinter;
import backend.c: genCode;
import frontend.lexer: lexSource;
import frontend.parser: parseTokens;
import utils.messages: error;
import utils.files: readFile;
import compiler: compilerName, compilerVersion, compilerLicense;
void main(string[] args) {
// Get command line flags.
string sourcepath;
string outputpath;
bool printVersion;
bool onlyparse;
try {
auto cml = getopt(
args,
config.caseSensitive,
config.required, "c", "Set a file to compile", &sourcepath,
config.required, "o", "Set the output file", &outputpath,
"V|version", "Print the version and targets", &printVersion,
"P|parse", "Only parse, no compilation", &onlyparse
);
if (cml.helpWanted) {
defaultGetoptPrinter("Flags and options:", cml.options);
exit(0);
}
} catch (Exception e) {
error(e.msg);
}
if (printVersion) {
writefln("%s %s", compilerName, compilerVersion);
writefln("Distributed under the %s license.", compilerLicense);
exit(0);
}
// Frontend processing.
/*File sourceo;
try {
sourceo = File(sourcepath, "r");
} catch (Exception e) {
error(sourcepath ~ " couldn't be read: " ~ e.msg);
}*/
auto source = readFile(sourcepath);
auto tokens = lexSource(sourcepath, source);
auto ast = parseTokens(tokens);
if (onlyparse) {
return;
}
File output;
try {
output = File(outputpath, "w");
} catch (Exception e) {
error(outputpath ~ " couldn't be written: " ~ e.msg);
}
genCode(output, ast);
}