From 62eec8011ddac87a98f4ce0f40f7ac7cbbc887ee Mon Sep 17 00:00:00 2001 From: Alexandre Plateau Date: Sun, 26 Nov 2023 17:54:31 +0100 Subject: [PATCH] fix: handle more compilation failure edge cases --- include/Ark/Compiler/Welder.hpp | 4 ++-- src/arkreactor/Compiler/Welder.cpp | 11 +++++++++-- src/arkreactor/VM/State.cpp | 7 +++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/Ark/Compiler/Welder.hpp b/include/Ark/Compiler/Welder.hpp index 6c332acb4..dcfc90a36 100644 --- a/include/Ark/Compiler/Welder.hpp +++ b/include/Ark/Compiler/Welder.hpp @@ -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; diff --git a/src/arkreactor/Compiler/Welder.cpp b/src/arkreactor/Compiler/Welder.cpp index d4013daaa..d564398e7 100644 --- a/src/arkreactor/Compiler/Welder.cpp +++ b/src/arkreactor/Compiler/Welder.cpp @@ -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(&m_bytecode[0]), m_bytecode.size() * sizeof(uint8_t)); output.close(); + return true; } const internal::Node& Welder::ast() const noexcept diff --git a/src/arkreactor/VM/State.cpp b/src/arkreactor/VM/State.cpp index 4be3de5f2..0e4491ac4 100644 --- a/src/arkreactor/VM/State.cpp +++ b/src/arkreactor/VM/State.cpp @@ -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; }