3#include <nlohmann/json.hpp>
6#include <unordered_map>
34 return {
NONE,
false, 0, 0.0,
"", {}};
38 return {
BOOL, v, 0, 0.0,
"", {}};
42 return {
INT,
false, v, 0.0,
"", {}};
46 return {
FLOAT,
false, 0, v,
"", {}};
50 return {
STRING,
false, 0, 0.0, s, {}};
54 return {
TUPLE,
false, 0, 0.0,
"", values};
58 return {
LIST,
false, 0, 0.0,
"", values};
72 const nlohmann::json &ast,
73 int max_iterations = 50000);
79 const std::string &func_name,
80 const std::vector<PyConstValue> &args);
90 const nlohmann::json &ast_;
91 int iteration_budget_;
93 static constexpr int MAX_CALL_DEPTH = 30;
101 bool allow_control_flow_ =
false;
103 using Env = std::unordered_map<std::string, PyConstValue>;
120 void seed_globals(Env &env);
122 std::optional<StmtResult> exec_block(
const nlohmann::json &body, Env &env);
123 std::optional<StmtResult> exec_stmt(
const nlohmann::json &stmt, Env &env);
128 const nlohmann::json &target,
136 bool resolve_str_window(
137 const nlohmann::json &args,
142 std::optional<PyConstValue>
143 eval_expr(
const nlohmann::json &node,
const Env &env);
145 std::optional<std::vector<PyConstValue>>
146 eval_call_args(
const nlohmann::json &args,
const Env &env);
149 static std::string slice_string(
150 const std::string &s,
151 std::optional<long long> start,
152 std::optional<long long> stop,
156 const nlohmann::json *find_function(
const std::string &name)
const;
std::optional< PyConstValue > try_eval_global_expr(const nlohmann::json &expr)
Try to evaluate an arbitrary expression at module scope, seeding the environment with module-level co...
Definition python_consteval.cpp:671
std::optional< PyConstValue > try_eval_call(const std::string &func_name, const std::vector< PyConstValue > &args)
Try to evaluate a function call with constant arguments.
Definition python_consteval.cpp:239
python_consteval(const nlohmann::json &ast, int max_iterations=50000)
Definition python_consteval.cpp:143
@ RETURN
Definition goto_program.h:39
Represents a constant Python value for compile-time evaluation.
Definition python_consteval.h:11
std::vector< PyConstValue > tuple_val
Definition python_consteval.h:30
static PyConstValue make_int(long long v)
Definition python_consteval.h:40
double float_val
Definition python_consteval.h:26
static PyConstValue make_float(double v)
Definition python_consteval.h:44
static PyConstValue make_list(const std::vector< PyConstValue > &values)
Definition python_consteval.h:56
static PyConstValue make_bool(bool v)
Definition python_consteval.h:36
bool is_truthy() const
Definition python_consteval.cpp:59
std::string string_val
Definition python_consteval.h:27
Kind kind
Definition python_consteval.h:23
static PyConstValue make_none()
Definition python_consteval.h:32
Kind
Definition python_consteval.h:13
@ STRING
Definition python_consteval.h:18
@ TUPLE
Definition python_consteval.h:19
@ BOOL
Definition python_consteval.h:15
@ INT
Definition python_consteval.h:16
@ NONE
Definition python_consteval.h:14
@ FLOAT
Definition python_consteval.h:17
@ LIST
Definition python_consteval.h:20
long long int_val
Definition python_consteval.h:25
bool bool_val
Definition python_consteval.h:24
static PyConstValue make_string(const std::string &s)
Definition python_consteval.h:48
static PyConstValue make_tuple(const std::vector< PyConstValue > &values)
Definition python_consteval.h:52