#include "../include/mbs/script.hpp" #include "evaluator.hpp" #include "parser.hpp" #include namespace mbs { TestResult test(std::string_view source, std::size_t max_source_length) { try { if (max_source_length > 0 && source.size() > max_source_length) { return TestResult{false, "source exceeds maximum length", std::nullopt}; } const auto ast = parse(source); if (const auto err = semantic_check(*ast); err.has_value()) { return TestResult{false, *err, std::nullopt}; } return TestResult{true, {}, std::nullopt}; } catch (const ParseError& ex) { return TestResult{false, ex.what(), ex.loc}; } catch (const std::exception& ex) { return TestResult{false, ex.what(), std::nullopt}; } } EvalValueResult eval_val(const std::string_view source, const nlohmann::json& context, std::size_t max_source_length) { try { if (max_source_length > 0 && source.size() > max_source_length) { return EvalErr{"source exceeds maximum length", std::nullopt}; } const auto ast = parse(source); if (const auto err = semantic_check(*ast); err.has_value()) { return EvalErr{*err, std::nullopt}; } return EvalOk{eval_expr(*ast, context)}; } catch (const ParseError& ex) { return EvalErr{ex.what(), ex.loc}; } catch (const std::exception& ex) { return EvalErr{ex.what(), std::nullopt}; } } EvalBoolResult eval_true(const std::string_view source, const nlohmann::json& context, std::size_t max_source_length) { const auto r = eval_val(source, context, max_source_length); // Check if it resulted in an error if (std::holds_alternative(r)) { const auto&[error, loc] = std::get(r); return EvalBoolResult{ false, false, error, loc}; } // If all executed OK const auto&[value] = std::get(r); return EvalBoolResult{true, json_truthy(value), {}, std::nullopt}; } } // namespace mbs