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. More...
 
bool feed (const std::string &bytecode_filename)
 Feed the state by giving it the path to an existing bytecode file. More...
 
bool feed (const bytecode_t &bytecode)
 Feed the state with ArkScript bytecode. More...
 
bool doFile (const std::string &filename)
 Compile a file, and use the resulting bytecode. More...
 
bool doString (const std::string &code)
 Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode. More...
 
void loadFunction (const std::string &name, Value::ProcType function) noexcept
 Register a function in the virtual machine. More...
 
void setArgs (const std::vector< std::string > &args) noexcept
 Set the script arguments in sys:args. More...
 
void setDebug (unsigned level) noexcept
 Set the debug level. More...
 
void setLibDirs (const std::vector< std::string > &libenv) noexcept
 Set the std search paths. More...
 
void reset () noexcept
 Reset State (all member variables related to execution) More...
 

Private Member Functions

void configure ()
 Called to configure the state (set the bytecode, debug level, call the compiler...) More...
 
bool compile (const std::string &file, const std::string &output)
 Reads and compiles code of file. More...
 
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 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 80 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

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

References ARK_CACHE_DIRNAME, compile(), feed(), Ark::BytecodeReader::feed(), Ark::Utils::fileExists(), Ark::Utils::getFilenameFromPath(), 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 63 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(), m_bytecode, and m_filename.

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 340 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 132 of file State.hpp.

Referenced by configure().

Friends And Related Function Documentation

◆ Repl

friend class Repl
friend

Definition at line 113 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 150 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 139 of file State.hpp.

Referenced by configure(), and feed().

◆ m_constants

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

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

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

◆ m_filename

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

Definition at line 141 of file State.hpp.

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

◆ m_libenv

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

Definition at line 140 of file State.hpp.

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

◆ m_options

uint16_t Ark::State::m_options
private

Definition at line 142 of file State.hpp.

Referenced by compile(), and doString().

◆ m_pages

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

Definition at line 147 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 145 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: