Skip to content

Commit

Permalink
Add output option to codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed May 7, 2023
1 parent b92119a commit 5c730e6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void Codegen::compile (
) {
auto code = std::get<0>(result);
auto flags = std::get<1>(result);
auto f = std::ofstream("build/output.c");
auto f = std::ofstream(path + ".c");

f << code;
f.close();
Expand Down Expand Up @@ -123,10 +123,10 @@ void Codegen::compile (
}
}

auto cmd = compiler + " build/output.c " + libraries + "-w -o " + path + flagsStr + (debug ? " -g" : "");
auto cmd = compiler + " " + path + ".c " + libraries + "-w -o " + path + flagsStr + (debug ? " -g" : "");
auto returnCode = std::system(cmd.c_str());

std::filesystem::remove("build/output.c");
std::filesystem::remove(path + ".c");

if (returnCode != 0) {
throw Error("failed to compile generated code");
Expand Down
7 changes: 5 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ int main (int argc, char *argv[]) {
auto isLex = false;
auto isParse = false;
auto fileName = std::optional<std::string>{};
auto output = std::string("build/a.out");
auto platform = std::string("default");

for (auto i = 1; i < argc; i++) {
Expand All @@ -57,7 +58,9 @@ int main (int argc, char *argv[]) {
auto optName = arg.substr(2, arg.find('=') - 2);
auto optVal = arg.substr(arg.find('=') + 1);

if (optName == "platform") {
if (optName == "output") {
output = optVal;
} else if (optName == "platform") {
if (std::set<std::string>{"linux", "macos", "windows"}.contains(optVal)) {
platform = optVal;
} else {
Expand Down Expand Up @@ -122,7 +125,7 @@ int main (int argc, char *argv[]) {
return EXIT_SUCCESS;
}

Codegen::compile("build/a.out", result, platform);
Codegen::compile(output, result, platform);
return EXIT_SUCCESS;
} catch (const Error &err) {
std::cerr << "Error: " << err.what() << std::endl;
Expand Down

0 comments on commit 5c730e6

Please sign in to comment.