Skip to content
Snippets Groups Projects
Commit b3380fb8 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Merge branch 'feature/exit-function' into 'develop'

Add exit function in core module.

See merge request !177
parents 1a5011da db7a20bd
No related branches found
No related tags found
1 merge request!177Add exit function in core module.
...@@ -2540,11 +2540,16 @@ to files for instance) as we will see below. ...@@ -2540,11 +2540,16 @@ to files for instance) as we will see below.
**** ~core~ provided functions **** ~core~ provided functions
***** ~exit: Z -> void~
This function interrupts the execution of the script. The integer (~Z~)
value is the code that is returned when ~pugs~ exits.
***** ~getAvailableModules: void -> string~ ***** ~getAvailableModules: void -> string~
This function that is used in the preamble of this section is a This function that is used in the preamble of this section is a
function that returns a ~string~ that contains the list of modules that function that returns a ~string~ that contains the list of modules that
are available in the current version of pugs. are available in the current version of ~pugs~.
***** ~getModuleInfo: string -> string~ ***** ~getModuleInfo: string -> string~
......
...@@ -17,9 +17,11 @@ ...@@ -17,9 +17,11 @@
#include <language/utils/ASTDotPrinter.hpp> #include <language/utils/ASTDotPrinter.hpp>
#include <language/utils/ASTExecutionInfo.hpp> #include <language/utils/ASTExecutionInfo.hpp>
#include <language/utils/ASTPrinter.hpp> #include <language/utils/ASTPrinter.hpp>
#include <language/utils/Exit.hpp>
#include <language/utils/OperatorRepository.hpp> #include <language/utils/OperatorRepository.hpp>
#include <language/utils/SymbolTable.hpp> #include <language/utils/SymbolTable.hpp>
#include <utils/ConsoleManager.hpp> #include <utils/ConsoleManager.hpp>
#include <utils/ExecutionStatManager.hpp>
#include <utils/PugsAssert.hpp> #include <utils/PugsAssert.hpp>
#include <utils/PugsUtils.hpp> #include <utils/PugsUtils.hpp>
#include <utils/SignalManager.hpp> #include <utils/SignalManager.hpp>
...@@ -89,8 +91,12 @@ parser(const std::string& filename) ...@@ -89,8 +91,12 @@ parser(const std::string& filename)
ASTExecutionInfo execution_info{*root_node, module_importer.moduleRepository()}; ASTExecutionInfo execution_info{*root_node, module_importer.moduleRepository()};
ExecutionPolicy exec_all; ExecutionPolicy exec_all;
try {
root_node->execute(exec_all); root_node->execute(exec_all);
}
catch (language::Exit& e) {
ExecutionStatManager::getInstance().setExitCode(e.code());
}
root_node->m_symbol_table->clearValues(); root_node->m_symbol_table->clearValues();
OperatorRepository::destroy(); OperatorRepository::destroy();
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <language/utils/BinaryOperatorRegisterForString.hpp> #include <language/utils/BinaryOperatorRegisterForString.hpp>
#include <language/utils/BinaryOperatorRegisterForZ.hpp> #include <language/utils/BinaryOperatorRegisterForZ.hpp>
#include <language/utils/BuiltinFunctionEmbedder.hpp> #include <language/utils/BuiltinFunctionEmbedder.hpp>
#include <language/utils/Exit.hpp>
#include <language/utils/IncDecOperatorRegisterForN.hpp> #include <language/utils/IncDecOperatorRegisterForN.hpp>
#include <language/utils/IncDecOperatorRegisterForZ.hpp> #include <language/utils/IncDecOperatorRegisterForZ.hpp>
#include <language/utils/OFStream.hpp> #include <language/utils/OFStream.hpp>
...@@ -95,6 +96,21 @@ CoreModule::CoreModule() : BuiltinModule(true) ...@@ -95,6 +96,21 @@ CoreModule::CoreModule() : BuiltinModule(true)
)); ));
this->_addBuiltinFunction("exit", std::function(
[](const int64_t& exit_code) -> void {
const auto& location = ASTBacktrace::getInstance().sourceLocation();
std::cout << "\n** " << rang::fgB::yellow << "exit" << rang::fg::reset
<< " explicitly called with code " << rang::fgB::cyan << exit_code
<< rang::fg::reset << "\n from " << rang::style::underline
<< location.filename() << rang::style::reset << ':'
<< rang::fgB::yellow << location.line() << rang::fg::reset << '\n';
throw language::Exit(exit_code);
}
));
this->_addNameValue("cout", ast_node_data_type_from<std::shared_ptr<const OStream>>, this->_addNameValue("cout", ast_node_data_type_from<std::shared_ptr<const OStream>>,
EmbeddedData{std::make_shared<DataHandler<const OStream>>(std::make_shared<OStream>(std::cout))}); EmbeddedData{std::make_shared<DataHandler<const OStream>>(std::make_shared<OStream>(std::cout))});
......
#ifndef EXIT_HPP
#define EXIT_HPP
namespace language
{
class Exit
{
private:
int m_code = 0;
public:
int
code() const
{
return m_code;
}
Exit(int code) : m_code{code} {}
Exit(const Exit&) = default;
Exit(Exit&&) = default;
~Exit() = default;
};
} // namespace language
#endif // EXIT_HPP
...@@ -42,7 +42,9 @@ main(int argc, char* argv[]) ...@@ -42,7 +42,9 @@ main(int argc, char* argv[])
finalize(); finalize();
ParallelChecker::destroy(); ParallelChecker::destroy();
int return_code = ExecutionStatManager::getInstance().exitCode();
ExecutionStatManager::destroy(); ExecutionStatManager::destroy();
return 0; return return_code;
} }
...@@ -11,6 +11,7 @@ class ExecutionStatManager ...@@ -11,6 +11,7 @@ class ExecutionStatManager
Timer m_elapse_time; Timer m_elapse_time;
bool m_do_print = true; bool m_do_print = true;
int m_exit_code = 0;
std::string _prettyPrintTime(double seconds) const; std::string _prettyPrintTime(double seconds) const;
...@@ -38,6 +39,20 @@ class ExecutionStatManager ...@@ -38,6 +39,20 @@ class ExecutionStatManager
m_do_print = do_print; m_do_print = do_print;
} }
PUGS_INLINE
int
exitCode() const
{
return m_exit_code;
}
PUGS_INLINE
void
setExitCode(int exit_code)
{
m_exit_code = exit_code;
}
PUGS_INLINE PUGS_INLINE
static ExecutionStatManager& static ExecutionStatManager&
getInstance() getInstance()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment