ArkScript
A small, fast, functional and scripting language for video games
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)
 Feed the state by giving it the path to an existing bytecode file.
 
bool feed (const bytecode_t &bytecode)
 Feed the state with ArkScript bytecode.
 
bool doFile (const std::string &file, uint16_t features=DefaultFeatures)
 Compile a file, and use the resulting bytecode.
 
bool doString (const std::string &code, uint16_t features=DefaultFeatures)
 Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode.
 
void loadFunction (const std::string &name, Value::ProcType 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)
 

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, uint16_t features) const
 Reads and compiles code of file.
 

Static Private Member Functions

static void throwStateError (const std::string &message)
 

Private Attributes

unsigned m_debug_level
 
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< bytecode_tm_pages
 
std::unordered_map< std::string, Valuem_binded
 

Friends

class VM
 
class internal::Closure
 
class Repl
 

Detailed Description

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

Definition at line 31 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.

Member Function Documentation

◆ compile()

bool Ark::State::compile ( const std::string & file,
const std::string & output,
uint16_t features ) const
private

Reads and compiles code of file.

Parameters
filethe path of file code to compile
outputset path of .arkc file
featurescompiler features to enable/disable
Returns
true on success
false on failure and raise an exception

Definition at line 54 of file State.cpp.

References Ark::Welder::computeASTFromFile(), Ark::Welder::generateBytecode(), m_binded, m_debug_level, m_libenv, Ark::Welder::registerSymbol(), and Ark::Welder::saveBytecodeToFile().

Referenced by doFile().

◆ configure()

void Ark::State::configure ( const BytecodeReader & bcr)
private

Called to configure the state (set the bytecode, debug level, call the compiler...)

Parameters
bcrreference to a pre-fed bytecode reader

Definition at line 142 of file State.cpp.

References ARK_VERSION, ARK_VERSION_MAJOR, Ark::BytecodeReader::code(), m_bytecode, m_constants, m_pages, m_symbols, Ark::BytecodeReader::sha256(), Ark::BytecodeReader::symbols(), throwStateError(), Ark::BytecodeReader::values(), and Ark::BytecodeReader::version().

Referenced by feed().

◆ doFile()

bool Ark::State::doFile ( const std::string & file,
uint16_t features = DefaultFeatures )

Compile a file, and use the resulting bytecode.

Parameters
filepath to an ArkScript code file
featurescompiler features to enable/disable
Returns
true on success
false on failure

Definition at line 72 of file State.cpp.

References ARK_CACHE_DIRNAME, Ark::BytecodeReader::checkMagic(), compile(), Ark::BytecodeReader::feed(), feed(), Ark::Utils::fileExists(), m_filename, and Ark::Utils::readFileAsBytes().

Referenced by main().

◆ doString()

bool Ark::State::doString ( const std::string & code,
uint16_t features = DefaultFeatures )

Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode.

Parameters
codethe ArkScript code
featurescompiler features to enable/disable
Returns
true on success
false on failure

Definition at line 103 of file State.cpp.

References Ark::Welder::bytecode(), Ark::Welder::computeASTFromString(), feed(), Ark::Welder::generateBytecode(), m_binded, m_debug_level, m_libenv, and Ark::Welder::registerSymbol().

Referenced by main().

◆ feed() [1/2]

bool Ark::State::feed ( const bytecode_t & bytecode)

Feed the state with ArkScript bytecode.

Parameters
bytecode
Returns
true on success
false on failure

Definition at line 33 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)

Feed the state by giving it the path to an existing bytecode file.

Parameters
bytecode_filename
Returns
true on success
false on failure

Definition at line 25 of file State.cpp.

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

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

◆ loadFunction()

void Ark::State::loadFunction ( const std::string & name,
Value::ProcType 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 116 of file State.cpp.

◆ reset()

void Ark::State::reset ( )
noexcept

Reset State (all member variables related to execution)

Definition at line 177 of file State.cpp.

References m_binded, m_constants, m_pages, and m_symbols.

◆ setArgs()

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

Set the script arguments in sys:args.

Parameters
args

Definition at line 121 of file State.cpp.

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

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 132 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 137 of file State.cpp.

◆ throwStateError()

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

Definition at line 137 of file State.hpp.

Referenced by configure().

Friends And Related Symbol Documentation

◆ internal::Closure

friend class internal::Closure
friend

Definition at line 115 of file State.hpp.

◆ Repl

friend class Repl
friend

Definition at line 116 of file State.hpp.

◆ VM

friend class VM
friend

Definition at line 114 of file State.hpp.

Member Data Documentation

◆ m_binded

std::unordered_map<std::string, Value> Ark::State::m_binded
private

Definition at line 154 of file State.hpp.

Referenced by compile(), doString(), Ark::VM::init(), and reset().

◆ m_bytecode

bytecode_t Ark::State::m_bytecode
private

Definition at line 144 of file State.hpp.

Referenced by configure(), and feed().

◆ m_constants

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

Definition at line 150 of file State.hpp.

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

◆ m_debug_level

unsigned Ark::State::m_debug_level
private

Definition at line 142 of file State.hpp.

Referenced by compile(), and doString().

◆ m_filename

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

Definition at line 146 of file State.hpp.

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

◆ m_libenv

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

Definition at line 145 of file State.hpp.

Referenced by compile(), doString(), and Ark::VM::loadPlugin().

◆ m_pages

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

Definition at line 151 of file State.hpp.

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

◆ m_symbols

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

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