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:

Public Member Functions

 State (uint16_t options=DefaultFeatures, const std::vector< std::string > &libpath={}) 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 &filename)
 Compile a file, and use the resulting bytecode.
 
bool doString (const std::string &code)
 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::string > &libenv) noexcept
 Set the std search paths.
 
void reset () noexcept
 Reset State (all member variables related to execution)
 

Private Member Functions

void configure ()
 Called to configure the state (set the bytecode, debug level, call the compiler...)
 
bool compile (const std::string &file, const std::string &output)
 Reads and compiles code of file.
 
void throwStateError (const std::string &message)
 

Private Attributes

unsigned m_debug_level
 
bytecode_t m_bytecode
 
std::vector< std::string > m_libenv
 
std::string m_filename
 
uint16_t m_options
 
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 30 of file State.hpp.

Constructor & Destructor Documentation

◆ State()

Ark::State::State ( uint16_t  options = DefaultFeatures,
const std::vector< std::string > &  libpath = {} 
)
noexcept

Construct a new State object.

Parameters
optionsthe options for the virtual machine, compiler, and parser
libpatha list of search paths for the std library

Definition at line 18 of file State.cpp.

References Ark::Utils::canonicalRelPath(), Ark::Utils::fileExists(), and Ark::Utils::splitString().

Member Function Documentation

◆ compile()

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

Reads and compiles code of file.

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

Definition at line 79 of file State.cpp.

References Ark::Compiler::compile(), Ark::Compiler::feed(), m_binded, m_debug_level, Ark::Compiler::m_defined_symbols, m_libenv, m_options, Ark::Utils::readFile(), and Ark::Compiler::saveTo().

Referenced by doFile().

◆ configure()

void Ark::State::configure ( )
private

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

Definition at line 201 of file State.cpp.

References ARK_VERSION_MAJOR, ARK_VERSION_STR, m_bytecode, m_constants, m_pages, m_symbols, and throwStateError().

Referenced by feed().

◆ doFile()

bool Ark::State::doFile ( const std::string &  filename)

Compile a file, and use the resulting bytecode.

Parameters
filenamepath to an ArkScript code file
Returns
true on success
false on failure

Definition at line 109 of file State.cpp.

References ARK_CACHE_DIRNAME, compile(), feed(), Ark::BytecodeReader::feed(), Ark::Utils::fileExists(), Ark::Utils::getFilenameFromPath(), m_filename, and Ark::BytecodeReader::timestamp().

Referenced by main().

◆ doString()

bool Ark::State::doString ( const std::string &  code)

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

Parameters
codethe ArkScript code
Returns
true on success
false on failure

Definition at line 151 of file State.cpp.

References Ark::Compiler::bytecode(), Ark::Compiler::compile(), feed(), Ark::Compiler::feed(), m_binded, m_debug_level, Ark::Compiler::m_defined_symbols, m_libenv, and m_options.

Referenced by main(), and Ark::Repl::run().

◆ 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 62 of file State.cpp.

References configure(), 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 42 of file State.cpp.

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

Referenced by doFile(), and doString().

◆ 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 176 of file State.cpp.

◆ reset()

void Ark::State::reset ( )
noexcept

Reset State (all member variables related to execution)

Definition at line 342 of file State.cpp.

References m_binded, m_constants, m_pages, and m_symbols.

Referenced by 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 181 of file State.cpp.

References ARK_PLATFORM_NAME, Ark::List, and Ark::Value::push_back().

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

Referenced by main(), and Ark::Repl::run().

◆ setLibDirs()

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

Set the std search paths.

Parameters
libenvthe list of std search paths to set

Definition at line 196 of file State.cpp.

◆ throwStateError()

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

Definition at line 133 of file State.hpp.

Referenced by configure().

Friends And Related Function Documentation

◆ internal::Closure

friend class internal::Closure
friend

Definition at line 113 of file State.hpp.

◆ Repl

friend class Repl
friend

Definition at line 114 of file State.hpp.

◆ VM

friend class VM
friend

Definition at line 112 of file State.hpp.

Member Data Documentation

◆ m_binded

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

Definition at line 151 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 140 of file State.hpp.

Referenced by configure(), and feed().

◆ m_constants

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

Definition at line 147 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 138 of file State.hpp.

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

◆ m_filename

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

Definition at line 142 of file State.hpp.

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

◆ m_libenv

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

Definition at line 141 of file State.hpp.

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

◆ m_options

uint16_t Ark::State::m_options
private

Definition at line 143 of file State.hpp.

Referenced by compile(), and doString().

◆ m_pages

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

Definition at line 148 of file State.hpp.

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

◆ m_symbols

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

Definition at line 146 of file State.hpp.

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


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