![]() |
ArkScript
A small, lisp-inspired, functional scripting language
|
Ark state to handle the dirty job of loading and compiling ArkScript code. More...
#include <State.hpp>
Public Member Functions | |
| State (const std::vector< std::filesystem::path > &libenv={}) noexcept | |
| Construct a new State object. | |
| bool | feed (const std::string &bytecode_filename, bool fail_with_exception=false) |
| Feed the state by giving it the path to an existing bytecode file. | |
| bool | feed (const bytecode_t &bytecode, bool fail_with_exception=false) |
| Feed the state with ArkScript bytecode. | |
| bool | doFile (const std::string &file_path, uint16_t features=DefaultFeatures, std::ostream *stream=nullptr) |
| Compile a file, and use the resulting bytecode. | |
| bool | doString (const std::string &code, uint16_t features=DefaultFeatures, std::ostream *stream=nullptr) |
| Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode. | |
| void | loadFunction (const std::string &name, Procedure::CallbackType &&function) noexcept |
| Register a function in the virtual machine. | |
| void | setArgs (const std::vector< std::string > &args) noexcept |
| Set the script arguments in sys:args. | |
| void | setDebug (unsigned level) noexcept |
| Set the debug level. | |
| void | setLibDirs (const std::vector< std::filesystem::path > &libenv) noexcept |
| Set the std search paths. | |
| void | reset () noexcept |
| Reset State (all member variables related to execution) | |
| void | extendBytecode (const std::vector< bytecode_t > &pages, const std::vector< std::string > &symbols, const std::vector< Value > &constants) |
| Used by the debugger to add code to the VM at runtime. | |
Private Member Functions | |
| void | configure (const BytecodeReader &bcr) |
| Called to configure the state (set the bytecode, debug level, call the compiler...) | |
| bool | compile (const std::string &file, const std::string &output, std::ostream *stream=nullptr) |
| Reads and compiles code of file. | |
| void | addPagesToContiguousBytecode (const std::vector< bytecode_t > &pages, std::size_t start) |
| constexpr uint8_t | inst (const std::size_t pp, const std::size_t ip) const noexcept |
| Get an instruction in a given page, with a given instruction pointer. | |
Static Private Member Functions | |
| static void | throwStateError (const std::string &message) |
| static std::size_t | maxPageSize (const std::vector< bytecode_t > &pages) |
| Compute the maximum length of the given code pages. | |
Private Attributes | |
| unsigned | m_debug_level |
| uint16_t | m_features |
| bytecode_t | m_bytecode |
| std::vector< std::filesystem::path > | m_libenv |
| std::string | m_filename |
| std::vector< std::string > | m_symbols |
| std::vector< Value > | m_constants |
| std::vector< std::string > | m_filenames |
| std::vector< internal::InstLoc > | m_inst_locations |
| std::vector< bytecode_t > | m_pages |
| std::size_t | m_max_page_size |
| bytecode_t | m_code |
| std::unordered_map< std::string, Value > | m_bound |
| Values bound to the State, to be used by the VM. | |
Friends | |
| class | VM |
| class | Repl |
| class | internal::Closure |
| class | internal::Debugger |
Ark state to handle the dirty job of loading and compiling ArkScript code.
|
explicitnoexcept |
Construct a new State object.
| libenv | a list of search paths for the std library |
Definition at line 19 of file State.cpp.
References Ark::List, Ark::internal::Language::SysArgs, and Ark::internal::Language::SysProgramName.
|
private |
Definition at line 235 of file State.cpp.
References m_code, and m_max_page_size.
Referenced by configure(), and extendBytecode().
|
nodiscardprivate |
Reads and compiles code of file.
| file | the path of file code to compile |
| output | set path of .arkc file |
| stream | output stream for the logger |
Definition at line 65 of file State.cpp.
References Ark::Welder::bytecode(), Ark::Welder::computeASTFromFile(), Ark::DisableCache, feed(), Ark::Welder::generateBytecode(), m_bound, m_debug_level, m_features, m_libenv, Ark::Welder::redirectLogsTo(), Ark::Welder::registerSymbol(), and Ark::Welder::saveBytecodeToFile().
Referenced by doFile().
|
private |
Called to configure the state (set the bytecode, debug level, call the compiler...)
| bcr | reference to a pre-fed bytecode reader |
Definition at line 173 of file State.cpp.
References addPagesToContiguousBytecode(), ARK_VERSION, ARK_VERSION_MAJOR, Ark::BytecodeReader::code(), Ark::BytecodeReader::filenames(), Ark::internal::bytecode::HeaderSize, Ark::BytecodeReader::instLocations(), m_bytecode, m_code, m_constants, m_filenames, m_inst_locations, m_max_page_size, m_pages, m_symbols, maxPageSize(), Ark::BytecodeReader::sha256(), Ark::BytecodeReader::symbols(), throwStateError(), Ark::BytecodeReader::values(), and Ark::BytecodeReader::version().
Referenced by feed().
| bool Ark::State::doFile | ( | const std::string & | file_path, |
| uint16_t | features = DefaultFeatures, | ||
| std::ostream * | stream = nullptr ) |
Compile a file, and use the resulting bytecode.
| file_path | path to an ArkScript code file |
| features | compiler features to enable/disable |
| stream | optional output stream for the logger |
Definition at line 88 of file State.cpp.
References ARK_CACHE_DIRNAME, Ark::BytecodeReader::checkMagic(), compile(), Ark::DisableCache, Ark::BytecodeReader::feed(), feed(), Ark::Utils::fileExists(), m_bound, m_features, m_filename, Ark::Utils::readFileAsBytes(), and Ark::internal::Language::SysProgramName.
Referenced by main().
| bool Ark::State::doString | ( | const std::string & | code, |
| uint16_t | features = DefaultFeatures, | ||
| std::ostream * | stream = nullptr ) |
Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode.
| code | the ArkScript code |
| features | compiler features to enable/disable |
| stream | optional output stream for the logger |
Definition at line 130 of file State.cpp.
References Ark::Welder::bytecode(), Ark::Welder::computeASTFromString(), feed(), Ark::Welder::generateBytecode(), m_bound, m_debug_level, m_features, m_libenv, Ark::Welder::redirectLogsTo(), and Ark::Welder::registerSymbol().
Referenced by __attribute__(), main(), and Ark::Repl::run().
| void Ark::State::extendBytecode | ( | const std::vector< bytecode_t > & | pages, |
| const std::vector< std::string > & | symbols, | ||
| const std::vector< Value > & | constants ) |
Used by the debugger to add code to the VM at runtime.
| pages | |
| symbols | |
| constants |
Definition at line 249 of file State.cpp.
References addPagesToContiguousBytecode(), m_code, m_constants, m_max_page_size, m_pages, m_symbols, maxPageSize(), and Ark::internal::NOP.
Referenced by Ark::internal::Debugger::run().
| bool Ark::State::feed | ( | const bytecode_t & | bytecode, |
| bool | fail_with_exception = false ) |
Feed the state with ArkScript bytecode.
| bytecode | |
| fail_with_exception |
Definition at line 41 of file State.cpp.
References Ark::BytecodeReader::checkMagic(), configure(), Ark::BytecodeReader::feed(), and m_bytecode.
| bool Ark::State::feed | ( | const std::string & | bytecode_filename, |
| bool | fail_with_exception = false ) |
Feed the state by giving it the path to an existing bytecode file.
| bytecode_filename | |
| fail_with_exception |
Definition at line 33 of file State.cpp.
References feed(), Ark::Utils::fileExists(), and Ark::Utils::readFileAsBytes().
Referenced by compile(), doFile(), doString(), and feed().
|
inlinenodiscardconstexprprivatenoexcept |
Get an instruction in a given page, with a given instruction pointer.
| pp | page pointer |
| ip | instruction pointer |
Definition at line 198 of file State.hpp.
Referenced by Ark::VM::throwArityError().
|
noexcept |
Register a function in the virtual machine.
| name | the name of the function in ArkScript |
| function | the code of the function |
Definition at line 148 of file State.cpp.
Referenced by Ark::Repl::registerBuiltins().
|
staticprivate |
Compute the maximum length of the given code pages.
| pages |
Definition at line 244 of file State.cpp.
Referenced by configure(), and extendBytecode().
|
noexcept |
Reset State (all member variables related to execution)
Definition at line 218 of file State.cpp.
References Ark::List, m_bound, m_code, m_constants, m_filenames, m_inst_locations, m_max_page_size, m_symbols, Ark::internal::Language::SysArgs, and Ark::internal::Language::SysProgramName.
Referenced by Ark::Repl::getLine(), and Ark::Repl::run().
|
noexcept |
Set the script arguments in sys:args.
| args |
Definition at line 153 of file State.cpp.
References Ark::List, Ark::Value::list(), and Ark::internal::Language::SysArgs.
Referenced by main().
|
noexcept |
|
noexcept |
|
inlinestaticprivate |
Definition at line 157 of file State.hpp.
Referenced by configure().
|
friend |
|
friend |
|
private |
Values bound to the State, to be used by the VM.
Definition at line 179 of file State.hpp.
Referenced by compile(), doFile(), doString(), Ark::VM::init(), and reset().
|
private |
Definition at line 165 of file State.hpp.
Referenced by configure(), and feed().
|
private |
Definition at line 176 of file State.hpp.
Referenced by addPagesToContiguousBytecode(), configure(), extendBytecode(), and reset().
|
private |
Definition at line 171 of file State.hpp.
Referenced by configure(), extendBytecode(), Ark::VM::initDebugger(), Ark::VM::loadPlugin(), reset(), Ark::VM::safeRun(), and Ark::VM::usePromptFileForDebugger().
|
private |
Definition at line 162 of file State.hpp.
Referenced by compile(), and doString().
|
private |
Definition at line 163 of file State.hpp.
Referenced by compile(), doFile(), doString(), Ark::VM::safeRun(), and Ark::VM::showBacktraceWithException().
|
private |
Definition at line 167 of file State.hpp.
Referenced by doFile(), and Ark::VM::loadPlugin().
|
private |
Definition at line 172 of file State.hpp.
Referenced by Ark::VM::backtrace(), configure(), Ark::VM::debugShowSource(), reset(), and Ark::internal::Debugger::showContext().
|
private |
Definition at line 173 of file State.hpp.
Referenced by configure(), Ark::VM::findSourceLocation(), and reset().
|
private |
Definition at line 166 of file State.hpp.
Referenced by compile(), doString(), Ark::VM::initDebugger(), Ark::VM::loadPlugin(), and Ark::VM::usePromptFileForDebugger().
|
private |
Definition at line 175 of file State.hpp.
Referenced by addPagesToContiguousBytecode(), configure(), extendBytecode(), and reset().
|
private |
Definition at line 174 of file State.hpp.
Referenced by configure(), extendBytecode(), and Ark::internal::Debugger::run().
|
private |
Definition at line 170 of file State.hpp.
Referenced by Ark::VM::backtrace(), configure(), extendBytecode(), Ark::VM::forceReloadPlugins(), Ark::VM::getField(), Ark::VM::init(), Ark::VM::initDebugger(), Ark::VM::loadPlugin(), reset(), Ark::VM::safeRun(), Ark::internal::Debugger::showLocals(), Ark::VM::throwArityError(), and Ark::VM::usePromptFileForDebugger().