9# pragma warning(disable : 4996)
19 State::State(
const std::vector<std::filesystem::path>& libenv) noexcept :
47 catch (
const std::exception& e)
49 fmt::println(
"{}", e.what());
54 bool State::compile(
const std::string& file,
const std::string& output,
const uint16_t features)
const
65 const std::string destination = output.empty() ? (file.substr(0, file.find_last_of(
'.')) +
".arkc") : output;
76 fmt::print(fmt::fg(fmt::color::red),
"Can not find file '{}'\n", file);
87 const std::string short_filename = (std::filesystem::path(file)).filename().string();
88 const std::string filename = short_filename.substr(0, short_filename.find_last_of(
'.')) +
".arkc";
89 const std::filesystem::path directory = (std::filesystem::path(file)).parent_path() /
ARK_CACHE_DIRNAME;
90 const std::string path = (directory / filename).
string();
92 if (!exists(directory))
93 create_directory(directory);
98 else if (
feed(bytecode))
118 m_binded[name] =
Value(function);
124 std::ranges::transform(args, std::back_inserter(val.
list()), [](
const std::string& arg) {
134 m_debug_level = level;
144 using namespace internal;
146 const auto [major, minor, patch] = bcr.
version();
149 std::string str_version = std::to_string(major) +
"." +
150 std::to_string(minor) +
"." +
151 std::to_string(patch);
155 const auto bytecode_hash = bcr.
sha256();
157 std::vector<unsigned char> hash(picosha2::k_digest_size);
160 for (std::size_t j = 0; j < picosha2::k_digest_size; ++j)
162#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
163 if (hash[j] != bytecode_hash[j])
168 const auto syms = bcr.
symbols();
169 const auto vals = bcr.
values(syms);
170 const auto [pages, _] = bcr.
code(vals);
A bytecode disassembler for ArkScript.
Constants used by ArkScript.
constexpr std::string_view ARK_VERSION
#define ARK_PLATFORM_NAME
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.
Values values(const Symbols &symbols) const
std::vector< unsigned char > sha256() const
Code code(const Values &values) const
void feed(const std::string &file)
Construct needed data before displaying information about a given file.
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)
bool feed(const std::string &bytecode_filename)
Feed the state by giving it the path to an existing bytecode file.
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
std::vector< std::string > m_symbols
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.
std::vector< bytecode_t > m_pages
void loadFunction(const std::string &name, Value::ProcType function) noexcept
Register a function in the virtual machine.
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()
Value(*)(std::vector< Value > &, VM *) ProcType
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 SysPlatform
constexpr std::string_view SysArgs
std::vector< uint8_t > bytecode_t