fix negate test

This commit is contained in:
Xuan Son Nguyen 2025-12-28 19:07:01 +01:00
parent acb0effa25
commit db09a7468d
3 changed files with 17 additions and 7 deletions

View File

@ -9,6 +9,8 @@
#include <optional>
#include <algorithm>
#define FILENAME "jinja-vm-builtins"
namespace jinja {
/**
@ -88,6 +90,7 @@ const func_builtins & global_builtins() {
throw raised_exception("namespace() arguments must be kwargs");
}
auto kwarg = cast_val<value_kwarg>(arg);
JJ_DEBUG("namespace: adding key '%s'", kwarg->key.c_str());
out->insert(kwarg->key, kwarg->val);
}
return out;
@ -132,7 +135,9 @@ const func_builtins & global_builtins() {
{"test_is_none", test_type_fn<value_null>},
{"test_is_defined", [](const func_args & args) -> value {
args.ensure_count(1);
return mk_val<value_bool>(!is_val<value_undefined>(args.args[0]));
bool res = !args.args[0]->is_undefined();
JJ_DEBUG("test_is_defined: result=%d", res ? 1 : 0);
return mk_val<value_bool>(res);
}},
{"test_is_undefined", test_type_fn<value_undefined>},
};

View File

@ -257,14 +257,20 @@ value test_expression::execute_impl(context & ctx) {
auto test_id = cast_stmt<identifier>(test)->val;
auto it = builtins.find("test_is_" + test_id);
JJ_DEBUG("Test expression %s '%s'", operand->type().c_str(), test_id.c_str());
JJ_DEBUG("Test expression %s '%s' %s", operand->type().c_str(), test_id.c_str(), negate ? "(negate)" : "");
if (it == builtins.end()) {
throw std::runtime_error("Unknown test '" + test_id + "'");
}
func_args args;
args.args.push_back(operand->execute(ctx));
return it->second(args);
auto res = it->second(args);
if (negate) {
return mk_val<value_bool>(!res->as_bool());
} else {
return res;
}
}
value unary_expression::execute_impl(context & ctx) {
@ -538,7 +544,6 @@ value member_expression::execute_impl(context & ctx) {
throw std::runtime_error("Cannot access object with non-string: got " + property->type());
}
auto key = property->as_string().str();
JJ_DEBUG("Accessing object property '%s'", key.c_str());
auto & obj = object->as_object();
auto it = obj.find(key);
if (it != obj.end()) {
@ -546,6 +551,7 @@ value member_expression::execute_impl(context & ctx) {
} else {
val = try_builtin_func(key, object, true);
}
JJ_DEBUG("Accessed property '%s' value, got type: %s", key.c_str(), val->type().c_str());
} else if (is_val<value_array>(object) || is_val<value_string>(object)) {
if (is_val<value_int>(property)) {

View File

@ -17,10 +17,9 @@ int main(void) {
//std::string contents = "{% if messages[0]['role'] != 'system' %}nice {{ messages[0]['content'] }}{% endif %}";
//std::string contents = "<some_tokens> {{ messages[a]['content'] }} <another_token>";
//std::string contents = "{{ aaa[bbb] }}";
//std::string contents = "{% if a is not defined %}hello{% endif %}";
std::ifstream infile("models/templates/mistralai-Ministral-3-14B-Reasoning-2512.jinja");
std::string contents((std::istreambuf_iterator<char>(infile)), std::istreambuf_iterator<char>());
std::ifstream infile("models/templates/mistralai-Ministral-3-14B-Reasoning-2512.jinja"); std::string contents((std::istreambuf_iterator<char>(infile)), std::istreambuf_iterator<char>());
std::cout << "=== INPUT ===\n" << contents << "\n\n";