2021-08-29 14:18:45 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-08-29 22:15:18 +02:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2021-09-25 16:24:08 +02:00
|
|
|
#include <hex/helpers/net.hpp>
|
2021-10-07 14:49:49 +02:00
|
|
|
#include <hex/helpers/file.hpp>
|
2021-08-29 22:15:18 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
#include <hex/pattern_language/token.hpp>
|
2021-09-08 15:18:24 +02:00
|
|
|
#include <hex/pattern_language/log_console.hpp>
|
|
|
|
#include <hex/pattern_language/evaluator.hpp>
|
2021-09-21 21:29:18 +02:00
|
|
|
#include <hex/pattern_language/pattern_data.hpp>
|
2021-01-22 18:01:39 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
#include <fmt/args.h>
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
2021-11-04 20:32:34 +01:00
|
|
|
std::string format(pl::Evaluator *ctx, auto params) {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto format = pl::Token::literalToString(params[0], true);
|
|
|
|
std::string message;
|
|
|
|
|
|
|
|
fmt::dynamic_format_arg_store<fmt::format_context> formatArgs;
|
|
|
|
|
|
|
|
for (u32 i = 1; i < params.size(); i++) {
|
|
|
|
auto ¶m = params[i];
|
|
|
|
|
|
|
|
std::visit(overloaded {
|
2022-01-24 20:53:17 +01:00
|
|
|
[&](pl::PatternData *value) {
|
|
|
|
formatArgs.push_back(value->toString(ctx->getProvider()));
|
|
|
|
},
|
|
|
|
[&](auto &&value) {
|
|
|
|
formatArgs.push_back(value);
|
|
|
|
} },
|
2022-02-01 22:09:44 +01:00
|
|
|
param);
|
2021-09-21 21:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return fmt::vformat(format, formatArgs);
|
|
|
|
} catch (fmt::format_error &error) {
|
|
|
|
hex::pl::LogConsole::abortEvaluation(hex::format("format error: {}", error.what()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:01:39 +01:00
|
|
|
void registerPatternLanguageFunctions() {
|
2021-09-08 15:18:24 +02:00
|
|
|
using namespace hex::pl;
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ContentRegistry::PatternLanguage::addColorPalette("hex.builtin.palette.pastel", { 0x70B4771F, 0x700E7FFF, 0x702CA02C, 0x702827D6, 0x70BD6794, 0x704B568C, 0x70C277E3, 0x707F7F7F, 0x7022BDBC, 0x70CFBE17 });
|
|
|
|
|
2022-01-30 17:49:38 +01:00
|
|
|
ContentRegistry::PatternLanguage::Namespace nsStd = { "builtin", "std" };
|
2021-08-27 09:56:20 +02:00
|
|
|
{
|
2021-09-21 21:29:18 +02:00
|
|
|
/* print(format, args...) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStd, "print", ContentRegistry::PatternLanguage::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
ctx->getConsole().log(LogConsole::Level::Info, format(ctx, params));
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return std::nullopt;
|
|
|
|
});
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
/* format(format, args...) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStd, "format", ContentRegistry::PatternLanguage::MoreParametersThan | 0, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
return format(ctx, params);
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
|
|
|
|
2021-12-10 11:55:27 +01:00
|
|
|
/* env(name) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStd, "env", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-12-10 11:55:27 +01:00
|
|
|
auto name = Token::literalToString(params[0], false);
|
|
|
|
|
2021-12-18 23:38:16 +01:00
|
|
|
auto env = ctx->getEnvVariable(name);
|
|
|
|
if (env)
|
|
|
|
return env;
|
|
|
|
else {
|
|
|
|
ctx->getConsole().log(LogConsole::Level::Warning, hex::format("environment variable '{}' does not exist", name));
|
|
|
|
return "";
|
|
|
|
}
|
2021-12-10 11:55:27 +01:00
|
|
|
});
|
2021-09-09 23:18:10 +02:00
|
|
|
|
2022-01-30 17:49:38 +01:00
|
|
|
/* pack_size(...) */
|
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStd, "sizeof_pack", 0 | ContentRegistry::PatternLanguage::ExactlyOrMoreParametersThan, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
|
|
|
return u128(params.size());
|
|
|
|
});
|
2021-09-09 23:18:10 +02:00
|
|
|
|
2022-01-30 17:49:38 +01:00
|
|
|
/* error(message) */
|
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStd, "error", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-02-01 22:09:44 +01:00
|
|
|
LogConsole::abortEvaluation(Token::literalToString(params[0], true));
|
2022-01-30 17:49:38 +01:00
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
});
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2022-01-30 17:49:38 +01:00
|
|
|
/* warning(message) */
|
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStd, "warning", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-02-01 22:09:44 +01:00
|
|
|
ctx->getConsole().log(LogConsole::Level::Warning, Token::literalToString(params[0], true));
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2022-01-30 17:49:38 +01:00
|
|
|
return std::nullopt;
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2022-01-30 17:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ContentRegistry::PatternLanguage::Namespace nsStdMem = { "builtin", "std", "mem" };
|
|
|
|
{
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-09 23:18:10 +02:00
|
|
|
/* base_address() */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "base_address", ContentRegistry::PatternLanguage::NoParameters, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
return u128(ctx->getProvider()->getBaseAddress());
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* size() */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "size", ContentRegistry::PatternLanguage::NoParameters, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
return u128(ctx->getProvider()->getActualSize());
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-09-09 23:18:10 +02:00
|
|
|
|
|
|
|
/* find_sequence(occurrence_index, bytes...) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "find_sequence", ContentRegistry::PatternLanguage::MoreParametersThan | 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto occurrenceIndex = Token::literalToUnsigned(params[0]);
|
|
|
|
|
2021-09-09 23:18:10 +02:00
|
|
|
std::vector<u8> sequence;
|
|
|
|
for (u32 i = 1; i < params.size(); i++) {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto byte = Token::literalToUnsigned(params[i]);
|
|
|
|
|
|
|
|
if (byte > 0xFF)
|
|
|
|
LogConsole::abortEvaluation(hex::format("byte #{} value out of range: {} > 0xFF", i, u64(byte)));
|
|
|
|
|
|
|
|
sequence.push_back(u8(byte & 0xFF));
|
2021-09-09 23:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> bytes(sequence.size(), 0x00);
|
|
|
|
u32 occurrences = 0;
|
2021-09-21 21:29:18 +02:00
|
|
|
for (u64 offset = 0; offset < ctx->getProvider()->getSize() - sequence.size(); offset++) {
|
|
|
|
ctx->getProvider()->read(offset, bytes.data(), bytes.size());
|
2021-09-09 23:18:10 +02:00
|
|
|
|
|
|
|
if (bytes == sequence) {
|
2021-09-21 21:29:18 +02:00
|
|
|
if (occurrences < occurrenceIndex) {
|
2021-09-09 23:18:10 +02:00
|
|
|
occurrences++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return u128(offset);
|
2021-09-09 23:18:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
LogConsole::abortEvaluation("failed to find sequence");
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* read_unsigned(address, size) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "read_unsigned", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto address = Token::literalToUnsigned(params[0]);
|
2022-02-01 22:09:44 +01:00
|
|
|
auto size = Token::literalToUnsigned(params[1]);
|
2021-09-21 21:29:18 +02:00
|
|
|
|
|
|
|
if (size > 16)
|
|
|
|
LogConsole::abortEvaluation("read size out of range");
|
|
|
|
|
|
|
|
u128 result = 0;
|
|
|
|
ctx->getProvider()->read(address, &result, size);
|
|
|
|
|
|
|
|
return result;
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* read_signed(address, size) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "read_signed", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto address = Token::literalToUnsigned(params[0]);
|
2022-02-01 22:09:44 +01:00
|
|
|
auto size = Token::literalToUnsigned(params[1]);
|
2021-09-21 21:29:18 +02:00
|
|
|
|
|
|
|
if (size > 16)
|
|
|
|
LogConsole::abortEvaluation("read size out of range");
|
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
i128 value;
|
2021-09-21 21:29:18 +02:00
|
|
|
ctx->getProvider()->read(address, &value, size);
|
|
|
|
return hex::signExtend(size * 8, value);
|
2021-09-09 23:18:10 +02:00
|
|
|
});
|
|
|
|
|
2021-09-21 23:29:30 +02:00
|
|
|
/* read_string(address, size) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMem, "read_string", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 23:29:30 +02:00
|
|
|
auto address = Token::literalToUnsigned(params[0]);
|
2022-02-01 22:09:44 +01:00
|
|
|
auto size = Token::literalToUnsigned(params[1]);
|
2021-09-21 23:29:30 +02:00
|
|
|
|
|
|
|
std::string result(size, '\x00');
|
|
|
|
ctx->getProvider()->read(address, result.data(), size);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
2021-08-27 09:56:20 +02:00
|
|
|
}
|
|
|
|
|
2022-01-30 17:49:38 +01:00
|
|
|
ContentRegistry::PatternLanguage::Namespace nsStdString = { "builtin", "std", "string" };
|
2021-08-27 09:56:20 +02:00
|
|
|
{
|
|
|
|
/* length(string) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdString, "length", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
2021-08-27 09:56:20 +02:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return u128(string.length());
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-08-27 09:56:20 +02:00
|
|
|
/* at(string, index) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdString, "at", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
2022-02-01 22:09:44 +01:00
|
|
|
auto index = Token::literalToSigned(params[1]);
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_MACOS)
|
|
|
|
const auto signIndex = index >> (sizeof(index) * 8 - 1);
|
2022-02-01 22:09:44 +01:00
|
|
|
const auto absIndex = (index ^ signIndex) - signIndex;
|
2022-01-24 20:53:17 +01:00
|
|
|
#else
|
2021-12-19 12:32:15 +01:00
|
|
|
const auto absIndex = std::abs(index);
|
2022-01-24 20:53:17 +01:00
|
|
|
#endif
|
2021-10-09 23:07:58 +02:00
|
|
|
|
|
|
|
if (absIndex > string.length())
|
2021-09-21 21:29:18 +02:00
|
|
|
LogConsole::abortEvaluation("character index out of range");
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
if (index >= 0)
|
|
|
|
return char(string[index]);
|
|
|
|
else
|
|
|
|
return char(string[string.length() - -index]);
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-01-22 18:01:39 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
/* substr(string, pos, count) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdString, "substr", 3, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-21 21:29:18 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
2022-02-01 22:09:44 +01:00
|
|
|
auto pos = Token::literalToUnsigned(params[1]);
|
|
|
|
auto size = Token::literalToUnsigned(params[2]);
|
2021-09-21 21:29:18 +02:00
|
|
|
|
2021-09-25 00:04:40 +02:00
|
|
|
if (pos > string.length())
|
2021-09-21 21:29:18 +02:00
|
|
|
LogConsole::abortEvaluation("character index out of range");
|
2021-03-02 13:55:43 +01:00
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
return string.substr(pos, size);
|
2021-08-27 09:56:20 +02:00
|
|
|
});
|
2021-09-21 21:29:18 +02:00
|
|
|
|
2021-09-26 02:23:10 +02:00
|
|
|
/* parse_int(string, base) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdString, "parse_int", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-26 02:23:10 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
2022-02-01 22:09:44 +01:00
|
|
|
auto base = Token::literalToUnsigned(params[1]);
|
2021-09-26 02:23:10 +02:00
|
|
|
|
2022-01-22 22:37:52 +01:00
|
|
|
return i128(std::strtoll(string.c_str(), nullptr, base));
|
2021-09-26 02:23:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
/* parse_float(string) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdString, "parse_float", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-26 02:23:10 +02:00
|
|
|
auto string = Token::literalToString(params[0], false);
|
|
|
|
|
|
|
|
return double(std::strtod(string.c_str(), nullptr));
|
|
|
|
});
|
2021-08-27 09:56:20 +02:00
|
|
|
}
|
2021-09-25 16:24:08 +02:00
|
|
|
|
2022-01-30 17:49:38 +01:00
|
|
|
ContentRegistry::PatternLanguage::Namespace nsStdHttp = { "builtin", "std", "http" };
|
2021-09-25 16:24:08 +02:00
|
|
|
{
|
|
|
|
/* get(url) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdHttp, "get", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-09-25 16:24:08 +02:00
|
|
|
const auto url = Token::literalToString(params[0], false);
|
|
|
|
|
|
|
|
hex::Net net;
|
|
|
|
return net.getString(url).get().body;
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-09-25 16:24:08 +02:00
|
|
|
}
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
|
2022-01-30 17:49:38 +01:00
|
|
|
ContentRegistry::PatternLanguage::Namespace nsStdFile = { "builtin", "std", "file" };
|
2021-10-07 14:49:49 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
static u32 fileCounter = 0;
|
|
|
|
static std::map<u32, File> openFiles;
|
|
|
|
|
|
|
|
/* open(path, mode) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "open", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-02-01 22:09:44 +01:00
|
|
|
const auto path = Token::literalToString(params[0], false);
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto modeEnum = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
File::Mode mode;
|
|
|
|
switch (modeEnum) {
|
2022-02-01 22:09:44 +01:00
|
|
|
case 1:
|
|
|
|
mode = File::Mode::Read;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mode = File::Mode::Write;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
mode = File::Mode::Create;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LogConsole::abortEvaluation("invalid file open mode");
|
2021-10-07 14:49:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto file = File(path, mode);
|
|
|
|
|
|
|
|
if (!file.isValid())
|
|
|
|
LogConsole::abortEvaluation(hex::format("failed to open file {}", path));
|
|
|
|
|
|
|
|
fileCounter++;
|
2022-01-24 20:53:17 +01:00
|
|
|
openFiles.emplace(std::pair { fileCounter, std::move(file) });
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
return u128(fileCounter);
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* close(file) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "close", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles.erase(file);
|
|
|
|
|
2022-01-30 12:43:43 +01:00
|
|
|
return std::nullopt;
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* read(file, size) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "read", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
const auto size = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
return openFiles[file].readString(size);
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* write(file, data) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "write", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
const auto data = Token::literalToString(params[1], true);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles[file].write(data);
|
|
|
|
|
2022-01-30 12:43:43 +01:00
|
|
|
return std::nullopt;
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* seek(file, offset) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "seek", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-02-01 22:09:44 +01:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto offset = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles[file].seek(offset);
|
|
|
|
|
2022-01-30 12:43:43 +01:00
|
|
|
return std::nullopt;
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* size(file) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "size", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
return u128(openFiles[file].getSize());
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* resize(file, size) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "resize", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
const auto size = Token::literalToUnsigned(params[1]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles[file].setSize(size);
|
|
|
|
|
2022-01-30 12:43:43 +01:00
|
|
|
return std::nullopt;
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* flush(file) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "flush", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles[file].flush();
|
|
|
|
|
2022-01-30 12:43:43 +01:00
|
|
|
return std::nullopt;
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
|
|
|
|
/* remove(file) */
|
2021-12-20 20:40:28 +01:00
|
|
|
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "remove", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2021-10-07 14:49:49 +02:00
|
|
|
const auto file = Token::literalToUnsigned(params[0]);
|
|
|
|
|
|
|
|
if (!openFiles.contains(file))
|
|
|
|
LogConsole::abortEvaluation("failed to access invalid file");
|
|
|
|
|
|
|
|
openFiles[file].remove();
|
|
|
|
|
2022-01-30 12:43:43 +01:00
|
|
|
return std::nullopt;
|
2021-12-20 20:40:28 +01:00
|
|
|
});
|
2021-10-07 14:49:49 +02:00
|
|
|
}
|
2022-01-30 20:46:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
ContentRegistry::PatternLanguage::Namespace nsStdMath = { "builtin", "std", "math" };
|
|
|
|
{
|
|
|
|
/* floor(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "floor", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::floor(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* ceil(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "ceil", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::ceil(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* round(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "round", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::round(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* trunc(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "trunc", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::trunc(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/* log10(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "log10", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::log10(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* log2(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "log2", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::log2(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* ln(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "ln", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::log(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/* fmod(x, y) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "fmod", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::fmod(Token::literalToFloatingPoint(params[0]), Token::literalToFloatingPoint(params[1]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* pow(base, exp) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "pow", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::pow(Token::literalToFloatingPoint(params[0]), Token::literalToFloatingPoint(params[1]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* sqrt(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "sqrt", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::sqrt(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* cbrt(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "cbrt", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::cbrt(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/* sin(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "sin", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::sin(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* cos(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "cos", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::cos(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* tan(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "tan", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::tan(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* asin(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "asin", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::asin(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* acos(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "acos", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::acos(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* atan(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "atan", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::atan(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* atan2(y, x) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "atan", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::atan2(Token::literalToFloatingPoint(params[0]), Token::literalToFloatingPoint(params[1]));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/* sinh(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "sinh", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::sinh(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* cosh(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "cosh", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::cosh(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* tanh(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "tanh", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::tanh(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* asinh(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "asinh", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::asinh(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* acosh(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "acosh", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::acosh(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
|
|
|
|
/* atanh(value) */
|
2022-01-30 20:48:44 +01:00
|
|
|
ContentRegistry::PatternLanguage::addFunction(nsStdMath, "atanh", 1, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
|
2022-01-30 20:46:02 +01:00
|
|
|
return std::atanh(Token::literalToFloatingPoint(params[0]));
|
|
|
|
});
|
|
|
|
}
|
2021-01-22 18:01:39 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 21:29:18 +02:00
|
|
|
}
|