ArkScript
A small, lisp-inspired, functional scripting language
Ark::State Class Reference

Ark state to handle the dirty job of loading and compiling ArkScript code. More...

#include <State.hpp>

Collaboration diagram for Ark::State:
[legend]

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< Valuem_constants
 
std::vector< std::string > m_filenames
 
std::vector< internal::InstLocm_inst_locations
 
std::vector< bytecode_tm_pages
 
std::size_t m_max_page_size
 
bytecode_t m_code
 
std::unordered_map< std::string, Valuem_bound
 Values bound to the State, to be used by the VM.
 

Friends

class VM
 
class Repl
 
class internal::Closure
 
class internal::Debugger
 

Detailed Description

Ark state to handle the dirty job of loading and compiling ArkScript code.

Definition at line 37 of file State.hpp.

Constructor & Destructor Documentation

◆ State()

Ark::State::State ( const std::vector< std::filesystem::path > & libenv = {})
explicitnoexcept

Construct a new State object.

Parameters
libenva 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.

Member Function Documentation

◆ addPagesToContiguousBytecode()

void Ark::State::addPagesToContiguousBytecode ( const std::vector< bytecode_t > & pages,
std::size_t start )
private

Definition at line 235 of file State.cpp.

References m_code, and m_max_page_size.

Referenced by configure(), and extendBytecode().

◆ compile()

bool Ark::State::compile ( const std::string & file,
const std::string & output,
std::ostream * stream = nullptr )
nodiscardprivate

Reads and compiles code of file.

Parameters
filethe path of file code to compile
outputset path of .arkc file
streamoutput stream for the logger
Returns
true on success
false on failure and raise an exception

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().

◆ configure()

◆ doFile()

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.

Parameters
file_pathpath to an ArkScript code file
featurescompiler features to enable/disable
streamoptional output stream for the logger
Returns
true on success
false on failure

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().

◆ doString()

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.

Parameters
codethe ArkScript code
featurescompiler features to enable/disable
streamoptional output stream for the logger
Returns
true on success
false on failure

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().

◆ extendBytecode()

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.

Parameters
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().

◆ feed() [1/2]

bool Ark::State::feed ( const bytecode_t & bytecode,
bool fail_with_exception = false )

Feed the state with ArkScript bytecode.

Parameters
bytecode
fail_with_exception
Returns
true on success
false on failure

Definition at line 41 of file State.cpp.

References Ark::BytecodeReader::checkMagic(), configure(), Ark::BytecodeReader::feed(), and m_bytecode.

◆ feed() [2/2]

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.

Parameters
bytecode_filename
fail_with_exception
Returns
true on success
false on failure

Definition at line 33 of file State.cpp.

References feed(), Ark::Utils::fileExists(), and Ark::Utils::readFileAsBytes().

Referenced by compile(), doFile(), doString(), and feed().

◆ inst()

uint8_t Ark::State::inst ( const std::size_t pp,
const std::size_t ip ) const
inlinenodiscardconstexprprivatenoexcept

Get an instruction in a given page, with a given instruction pointer.

Parameters
pppage pointer
ipinstruction pointer
Returns
uint8_t instruction

Definition at line 198 of file State.hpp.

Referenced by Ark::VM::throwArityError().

◆ loadFunction()

void Ark::State::loadFunction ( const std::string & name,
Procedure::CallbackType && function )
noexcept

Register a function in the virtual machine.

Parameters
namethe name of the function in ArkScript
functionthe code of the function

Definition at line 148 of file State.cpp.

Referenced by Ark::Repl::registerBuiltins().

◆ maxPageSize()

std::size_t Ark::State::maxPageSize ( const std::vector< bytecode_t > & pages)
staticprivate

Compute the maximum length of the given code pages.

Parameters
pages
Returns
std::size_t

Definition at line 244 of file State.cpp.

Referenced by configure(), and extendBytecode().

◆ reset()

void Ark::State::reset ( )
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().

◆ setArgs()

void Ark::State::setArgs ( const std::vector< std::string > & args)
noexcept

Set the script arguments in sys:args.

Parameters
args

Definition at line 153 of file State.cpp.

References Ark::List, Ark::Value::list(), and Ark::internal::Language::SysArgs.

Referenced by main().

◆ setDebug()

void Ark::State::setDebug ( unsigned level)
noexcept

Set the debug level.

Parameters
levelbetween 0 (nothing) and 3 (maximum verbosity)

Definition at line 163 of file State.cpp.

Referenced by main().

◆ setLibDirs()

void Ark::State::setLibDirs ( const std::vector< std::filesystem::path > & libenv)
noexcept

Set the std search paths.

Parameters
libenvthe list of std search paths to set

Definition at line 168 of file State.cpp.

◆ throwStateError()

static void Ark::State::throwStateError ( const std::string & message)
inlinestaticprivate

Definition at line 157 of file State.hpp.

Referenced by configure().

Friends And Related Symbol Documentation

◆ internal::Closure

friend class internal::Closure
friend

Definition at line 135 of file State.hpp.

◆ internal::Debugger

friend class internal::Debugger
friend

Definition at line 136 of file State.hpp.

◆ Repl

friend class Repl
friend

Definition at line 134 of file State.hpp.

◆ VM

friend class VM
friend

Definition at line 133 of file State.hpp.

Member Data Documentation

◆ m_bound

std::unordered_map<std::string, Value> Ark::State::m_bound
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().

◆ m_bytecode

bytecode_t Ark::State::m_bytecode
private

Definition at line 165 of file State.hpp.

Referenced by configure(), and feed().

◆ m_code

bytecode_t Ark::State::m_code
private

Definition at line 176 of file State.hpp.

Referenced by addPagesToContiguousBytecode(), configure(), extendBytecode(), and reset().

◆ m_constants

std::vector<Value> Ark::State::m_constants
private

◆ m_debug_level

unsigned Ark::State::m_debug_level
private

Definition at line 162 of file State.hpp.

Referenced by compile(), and doString().

◆ m_features

uint16_t Ark::State::m_features
private

◆ m_filename

std::string Ark::State::m_filename
private

Definition at line 167 of file State.hpp.

Referenced by doFile(), and Ark::VM::loadPlugin().

◆ m_filenames

std::vector<std::string> Ark::State::m_filenames
private

◆ m_inst_locations

std::vector<internal::InstLoc> Ark::State::m_inst_locations
private

Definition at line 173 of file State.hpp.

Referenced by configure(), Ark::VM::findSourceLocation(), and reset().

◆ m_libenv

std::vector<std::filesystem::path> Ark::State::m_libenv
private

◆ m_max_page_size

std::size_t Ark::State::m_max_page_size
private

Definition at line 175 of file State.hpp.

Referenced by addPagesToContiguousBytecode(), configure(), extendBytecode(), and reset().

◆ m_pages

std::vector<bytecode_t> Ark::State::m_pages
private

Definition at line 174 of file State.hpp.

Referenced by configure(), extendBytecode(), and Ark::internal::Debugger::run().

◆ m_symbols


The documentation for this class was generated from the following files: