9# pragma warning(disable : 4996)
19 State::State(
const std::vector<std::filesystem::path>& libenv) noexcept :
52 catch (
const std::exception& e)
54 fmt::println(
"{}", e.what());
59 bool State::compile(
const std::string& file,
const std::string& output,
const uint16_t features)
const
70 const std::string destination = output.empty() ? (file.substr(0, file.find_last_of(
'.')) +
".arkc") : output;
81 fmt::print(fmt::fg(fmt::color::red),
"Can not find file '{}'\n", file);
92 const std::string short_filename = (std::filesystem::path(file)).filename().string();
93 const std::string filename = short_filename.substr(0, short_filename.find_last_of(
'.')) +
".arkc";
94 const std::filesystem::path directory = (std::filesystem::path(file)).parent_path() /
ARK_CACHE_DIRNAME;
95 const std::string path = (directory / filename).
string();
97 if (!exists(directory))
98 create_directory(directory);
103 else if (
feed(bytecode))
123 m_binded[name] =
Value(std::move(function));
129 std::ranges::transform(args, std::back_inserter(val.
list()), [](
const std::string& arg) {
138 m_debug_level = level;
148 using namespace internal;
150 const auto [major, minor, patch] = bcr.
version();
153 std::string str_version = std::to_string(major) +
"." +
154 std::to_string(minor) +
"." +
155 std::to_string(patch);
159 const auto bytecode_hash = bcr.
sha256();
161 std::vector<unsigned char> hash(picosha2::k_digest_size);
164 for (std::size_t j = 0; j < picosha2::k_digest_size; ++j)
166#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
167 if (hash[j] != bytecode_hash[j])
172 const auto syms = bcr.
symbols();
173 const auto vals = bcr.
values(syms);
176 const auto [pages, _] = bcr.
code(inst_locs);
195 for (std::size_t i = 0, end = pages.size(); i < end; ++i)
197 for (std::size_t j = 0, end_j = pages[i].size(); j < end_j; ++j)
A bytecode disassembler for ArkScript.
Constants used by ArkScript.
constexpr std::string_view ARK_VERSION
constexpr int ARK_VERSION_MAJOR
#define ARK_CACHE_DIRNAME
Lots of utilities about the filesystem.
State used by the virtual machine: it loads the bytecode, can compile it if needed,...
In charge of welding everything needed to compile code.
This class is just a helper to.
Filenames filenames(const Values &values) const
InstLocations instLocations(const Filenames &filenames) const
Code code(const InstLocations &instLocations) const
Values values(const Symbols &symbols) const
std::vector< unsigned char > sha256() const
void feed(const std::string &file)
Construct needed data before displaying information about a given file.
std::function< Value(std::vector< Value > &, VM *)> CallbackType
std::vector< std::filesystem::path > m_libenv
void setLibDirs(const std::vector< std::filesystem::path > &libenv) noexcept
Set the std search paths.
void configure(const BytecodeReader &bcr)
Called to configure the state (set the bytecode, debug level, call the compiler......
std::vector< Value > m_constants
static void throwStateError(const std::string &message)
std::vector< internal::InstLoc > m_inst_locations
bool feed(const std::string &bytecode_filename)
Feed the state by giving it the path to an existing bytecode file.
std::vector< std::string > m_filenames
bool doFile(const std::string &file, uint16_t features=DefaultFeatures)
Compile a file, and use the resulting bytecode.
void reset() noexcept
Reset State (all member variables related to execution)
std::unordered_map< std::string, Value > m_binded
Values binded to the State, to be used by the VM.
void loadFunction(const std::string &name, Procedure::CallbackType &&function) noexcept
Register a function in the virtual machine.
std::vector< std::string > m_symbols
std::size_t m_max_page_size
bool compile(const std::string &file, const std::string &output, uint16_t features) const
Reads and compiles code of file.
void setArgs(const std::vector< std::string > &args) noexcept
Set the script arguments in sys:args.
bool doString(const std::string &code, uint16_t features=DefaultFeatures)
Compile a string (representing ArkScript code) and store resulting bytecode in m_bytecode.
State(const std::vector< std::filesystem::path > &libenv={}) noexcept
Construct a new State object.
void setDebug(unsigned level) noexcept
Set the debug level.
std::vector< Value > & list()
The welder joins all the compiler passes.
void registerSymbol(const std::string &name)
Register a symbol as a global in the compiler.
bool computeASTFromString(const std::string &code)
const bytecode_t & bytecode() const noexcept
bool saveBytecodeToFile(const std::string &filename)
Save the generated bytecode to a given file.
bool generateBytecode()
Compile the AST processed by computeASTFromFile / computeASTFromString.
bool computeASTFromFile(const std::string &filename)
bool fileExists(const std::string &name) noexcept
Checks if a file exists.
std::vector< uint8_t > readFileAsBytes(const std::string &name)
Helper to read the bytes of a file.
constexpr std::string_view SysArgs
constexpr std::size_t HeaderSize
std::vector< uint8_t > bytecode_t