Skip to content

Commit

Permalink
fix: handle more compilation failure edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperFola committed Nov 26, 2023
1 parent 99bb6d5 commit 62eec80
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions include/Ark/Compiler/Welder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ namespace Ark
bool computeASTFromFile(const std::string& filename);
bool computeASTFromString(const std::string& code);

void generateBytecode();
void saveBytecodeToFile(const std::string& filename);
bool generateBytecode();
bool saveBytecodeToFile(const std::string& filename);

const internal::Node& ast() const noexcept;
const bytecode_t& bytecode() const noexcept;
Expand Down
11 changes: 9 additions & 2 deletions src/arkreactor/Compiler/Welder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,34 @@ namespace Ark
}
}

void Welder::generateBytecode()
bool Welder::generateBytecode()
{
try
{
m_compiler.process(m_optimizer.ast());
m_bytecode = m_compiler.bytecode();

return true;
}
catch (const CodeError& e)
{
Diagnostics::generate(e);
return false;
}
}

void Welder::saveBytecodeToFile(const std::string& filename)
bool Welder::saveBytecodeToFile(const std::string& filename)
{
if (m_debug >= 1)
std::cout << "Final bytecode size: " << m_bytecode.size() * sizeof(uint8_t) << "B\n";

if (m_bytecode.empty())
return false;

std::ofstream output(filename, std::ofstream::binary);
output.write(reinterpret_cast<char*>(&m_bytecode[0]), m_bytecode.size() * sizeof(uint8_t));
output.close();
return true;
}

const internal::Node& Welder::ast() const noexcept
Expand Down
7 changes: 5 additions & 2 deletions src/arkreactor/VM/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ namespace Ark

for (auto& p : m_binded)
welder.registerSymbol(p.first);
welder.generateBytecode();

if (!welder.generateBytecode())
return false;

std::string destination = output.empty() ? (file.substr(0, file.find_last_of('.')) + ".arkc") : output;
welder.saveBytecodeToFile(destination);
if (!welder.saveBytecodeToFile(destination))
return false;

return true;
}
Expand Down

0 comments on commit 62eec80

Please sign in to comment.