-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEpsilon.cs
More file actions
219 lines (178 loc) · 11.1 KB
/
Epsilon.cs
File metadata and controls
219 lines (178 loc) · 11.1 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using CsCommandConfig;
namespace Epsilon;
public class Epsilon {
public static void Main(string[] args) {
ProgramConfiguration config = new();
config.AddOption(new StringsOption("branch"));
config.AddOption(new StringOption("file"));
config.AddOption(new StringOption("package-name"));
config.AddOption(new StringOption("url"));
config.AddOption(new EnumOption("verbosity", typeof(LogLevel), "NONE"));
config.AddOption(new EnumOption("cache-mode", ["dont-use", "dont-load", "auto", "always"], "AUTO"));
config.AddOption(new EnumOption("optimization-level", ["0", "min", "1", "normal", "2", "max"], "min"));
config.AddOption(new BoolOption("generate-error-frames", true));
config.AddOption(new BoolOption("link-builtins", true));
config.AddOption(new BoolOption("link-libraries", true));
config.AddOption(new BoolOption("link-builtin-modules", true));
config.AddOption(new StringsOption("clang-parse-options"));
config.AddOption(new StringsOption("linking-options"));
config.AddOption(new StringsOption("object-gen-options"));
config.AddOption(new StringsOption("packages"));
config.AddOption(new StringOption("output-location"));
config.AddOption(new EnumOption("output-type", ["executable", "llvm-ll", "llvm-bc",
"package-both", "package-obj", "object", "shared-object"], "executable"));
config.AddOption(new BoolOption("all-packages", false));
Builder.EPSLPROJShape = JSONConfigParser.GetShape(config);
ArgumentParser parser = new(
config, "epslc",
"A compiler for the Epsilon programming language",
priority: 1
);
parser.Flag(["verbose", "v"], "verbosity",
new ConstArgumentValue<string>("TEMP"), "Provide verbose output");
parser.Flag(["verbosity", "V"], "verbosity",
new StringArgumentValue(), "Provide output of the specified verbosity");
parser.Tree("compile");
parser.SetBranchHelp("Compile an Epsilon file or project");
parser.Positional("file", new StringArgumentValue(),
"The file or project to compile", isOptional: true);
parser.Flag(["cache-mode", "H"], "cache-mode", new StringArgumentValue(),
"The mode to use when loading and saving parital compilation files.");
parser.Flag(["opt", "O"], "optimization-level", new StringArgumentValue(),
"The optimization level");
parser.Flag(["no-generate-error-frames", "F"], "generate-error-frames",
new ConstArgumentValue<bool>(false), "Don't generate error frames");
parser.Flag(["no-link-builtins"], "link-builtins", new ConstArgumentValue<bool>(false),
"Don't link the result to Epsilon's builtins");
parser.Flag(["no-link-libraries"], "link-libraries", new ConstArgumentValue<bool>(false),
"Don't link the result to sources from libraries");
parser.Flag(["no-link-builtin-modules"], "link-builtin-modules", new ConstArgumentValue<bool>(false),
"Don't link the result to Epsilon's builtin module's sources");
parser.Flag(["clang-parse-options", "C"], "clang-parse-options", new MultiStringArgumentValue(terminator: "END"),
"Additional options to send to clang when parsing C or C++ code");
parser.Flag(["clang-parse-option", "c"], "clang-parse-options", new StringArgumentValue(),
"An option to send to clang when parsing C or C++ code");
parser.Flag(["linking-options", "Z"], "linking-options", new MultiStringArgumentValue(terminator: "END"),
"Additional options to send to clang or ld when linking");
parser.Flag(["linking-option", "z"], "linking-options", new StringArgumentValue(),
"An option to send to clang or ld when linking");
parser.Flag(["object-gen-options", "G"], "object-gen-options", new MultiStringArgumentValue(terminator: "END"),
"Additional options to send to llc or clang when generating object files");
parser.Flag(["object-gen-option", "g"], "object-gen-options", new StringArgumentValue(),
"An option to send to send to llc or clang when generating object files");
parser.Flag(["package", "p"], "packages", new StringArgumentValue(),
"A package to include during compilation");
parser.Flag(["output", "o"], "output-location", new StringArgumentValue(),
"The location to place the output in");
parser.Flag(["output-type", "t"], "output-type", new StringArgumentValue(),
"The type of output to produce");
parser.Tree("clean");
parser.SetBranchHelp("Clean up all of a project's temporary files");
parser.Positional("file", new StringArgumentValue(),
"The project to clean", isOptional: true);
parser.Tree("init");
parser.SetBranchHelp("Create a new Epsilon project");
parser.Positional("file", new StringArgumentValue(),
"The location of the new project file", isOptional: true);
parser.Tree("package");
parser.SetBranchHelp("Manage Epsilon packages");
parser.Tree("package", "add");
parser.SetBranchHelp("Add a package to a project");
parser.Positional("package-name", new StringArgumentValue(),
"The name of the package to add");
parser.Flag(["project", "P"], "file", new StringArgumentValue(),
"The project to add the package to");
parser.Tree("package", "remove");
parser.SetBranchHelp("Remove a package from a project");
parser.Positional("package-name", new StringArgumentValue(),
"The name of the package to remove");
parser.Flag(["project", "p"], "file", new StringArgumentValue(),
"The project to remove the package from");
parser.Tree("package", "list");
parser.SetBranchHelp("List a project's packages");
parser.Positional("file", new StringArgumentValue(),
"The project to list the packages of", isOptional: true);
parser.Flag(["all", "a"], "all-packages", new ConstArgumentValue<bool>(true),
"List all packages");
parser.Tree("package", "register");
parser.SetBranchHelp("Register the current project as a package");
parser.Flag(["project", "p"], "file", new StringArgumentValue(),
"The project to register as a package");
parser.Tree("package", "install");
parser.SetBranchHelp("Install a package from the specified URL");
parser.Positional("url", new StringArgumentValue(),
"The URL to install the package from");
parser.Flag(["project", "p"], "file", new StringArgumentValue(),
"The project to add the package to");
parser.Tree("package", "uninstall");
parser.SetBranchHelp("Uninstall a package");
parser.Positional("package-name", new StringArgumentValue(),
"The name of the package to uninstall");
parser.Flag(["project", "p"], "file", new StringArgumentValue(),
"The project to remove the package from");
parser.Parse(args);
Log.Verbosity = EnumOption.EnumProp<LogLevel>(config, "verbosity");
Builder builder = new();
TestResult(builder.WipeTempDir());
TestResult(builder.ComputeInputPath(config["file"], out string inputPath));
if (Utils.ItemsEqual(config["branch"], "compile")) {
string entryPath = inputPath;
JSONConfigParser EPSLPROJParser = new(config, json => {
if (json.HasKey("file")) {
entryPath = Utils.JoinPaths(
Utils.GetDirectoryName(inputPath),
json["file"].GetString()
);
}
}, priority: 0);
TestResult(builder.LoadEPSLPROJ(inputPath, EPSLPROJParser));
Subconfigs.AddClangParseConfigs(config["clang-parse-options"]);
Subconfigs.AddLinkingConfigs(config["linking-options"]);
Subconfigs.AddObjectGenConfigs(config["object-gen-options"]);
CacheMode cacheMode = EnumHelpers.ParseCacheMode(config["cache-mode"]);
OptimizationLevel optimizationLevel = EnumHelpers.ParseOptimizationLevel(
config["optimization-level"]);
bool generateErrorFrames = config["generate-error-frames"]
&& optimizationLevel == OptimizationLevel.MIN;
OutputType outputType = EnumHelpers.ParseOutputType(config["output-type"]);
bool linkBuiltins = config["link-builtins"] && !outputType.MustntLinkBuiltins();
bool linkLibraries = config["link-libraries"] && !outputType.MustntLinkLibraries();
bool linkBuiltinModules = config["link-builtin-modules"] && !outputType.MustntLinkBuiltinModules();
TestResult(builder.LoadPackagesForCompilation(config["packages"]));
TestResult(builder.LoadEPSLCACHE(inputPath, cacheMode, out EPSLCACHE cache));
if (cacheMode > CacheMode.DONTLOAD && EPSLCACHE.MustDiscardCache(cache.LastOutputType, outputType)) {
Log.Info($"Cached data generated with the previous output type, {cache.LastOutputType}, cannot be used with the current output type, {outputType}. As such, all cached data is being disregarded.");
cacheMode = CacheMode.DONTLOAD;
}
BuildSettings settings = new(
entryPath, config["output-location"], cache, cacheMode, optimizationLevel,
outputType, generateErrorFrames, linkBuiltins, linkLibraries, linkBuiltinModules
);
TestResult(builder.Build(settings));
} else if (Utils.ItemsEqual(config["branch"], "clean")) {
TestResult(builder.LoadEPSLCACHE(inputPath, CacheMode.AUTO, out EPSLCACHE cache));
TestResult(builder.Teardown(cache));
} else if (Utils.ItemsEqual(config["branch"], "init")) {
TestResult(builder.CreateEPSLPROJ(inputPath));
} else if (Utils.ItemsEqual(config["branch"], "package", "add")) {
TestResult(builder.AddPackageToEPSLPROJ(config["package-name"], inputPath));
} else if (Utils.ItemsEqual(config["branch"], "package", "remove")) {
TestResult(builder.RemovePackageFromEPSLPROJ(config["package-name"], inputPath));
} else if (Utils.ItemsEqual(config["branch"], "package", "list")) {
TestResult(builder.ListPackages(inputPath, config["all-packages"]));
} else if (Utils.ItemsEqual(config["branch"], "package", "register")) {
TestResult(builder.RegisterProjectAsPackage(inputPath));
} else if (Utils.ItemsEqual(config["branch"], "package", "install")) {
TestResult(builder.InstallPackage(config["url"], inputPath));
} else if (Utils.ItemsEqual(config["branch"], "package", "uninstall")) {
TestResult(builder.UninstallPackage(config["package-name"], inputPath));
} else {
throw new InvalidOperationException();
}
}
static void TestResult(ResultStatus result) {
if (result != ResultStatus.GOOD) {
Environment.Exit(1);
}
}
}