9# pragma warning(disable : 4996)
19 State::State(
const std::vector<std::filesystem::path>& libenv) noexcept :
32 bool State::feed(
const std::string& bytecode_filename,
const bool fail_with_exception)
54 catch (
const std::exception& e)
56 if (fail_with_exception)
59 fmt::println(
"{}", e.what());
64 bool State::compile(
const std::string& file,
const std::string& output,
const uint16_t features)
const
75 const std::string destination = output.empty() ? (file.substr(0, file.find_last_of(
'.')) +
".arkc") : output;
82 bool State::doFile(
const std::string& file_path,
const uint16_t features)
86 fmt::print(fmt::fg(fmt::color::red),
"Can not find file '{}'\n", file_path);
98 const std::string filename = std::filesystem::path(file_path).filename().replace_extension(
".arkc").string();
99 const std::filesystem::path cache_directory = std::filesystem::path(file_path).parent_path() /
ARK_CACHE_DIRNAME;
100 const std::string bytecode_path = (cache_directory / filename).
string();
102 if (!exists(cache_directory))
103 create_directory(cache_directory);
105 if (
compile(file_path, bytecode_path, features) &&
feed(bytecode_path))
108 else if (
feed(bytecode))
128 m_binded[name] =
Value(std::move(function));
134 std::ranges::transform(args, std::back_inserter(val.
list()), [](
const std::string& arg) {
143 m_debug_level = level;
153 using namespace internal;
155 const auto [major, minor, patch] = bcr.
version();
158 const std::string str_version = fmt::format(
"{}.{}.{}", major, minor, patch);
162 const auto bytecode_hash = bcr.
sha256();
164 std::vector<unsigned char> hash(picosha2::k_digest_size);
167 for (std::size_t j = 0; j < picosha2::k_digest_size; ++j)
169#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
170 if (hash[j] != bytecode_hash[j])
175 const auto syms = bcr.
symbols();
176 const auto vals = bcr.
values(syms);
179 const auto [pages, _] = bcr.
code(inst_locs);
198 for (std::size_t i = 0, end = pages.size(); i < end; ++i)
200 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......
bool doFile(const std::string &file_path, uint16_t features=DefaultFeatures)
Compile a file, and use the resulting bytecode.
std::vector< Value > m_constants
static void throwStateError(const std::string &message)
std::vector< internal::InstLoc > m_inst_locations
std::vector< std::string > m_filenames
void reset() noexcept
Reset State (all member variables related to execution)
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.
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.
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::string_view SysProgramName
constexpr std::size_t HeaderSize
std::vector< uint8_t > bytecode_t