From d34efd9626230900af68df29e9e764b6a1e84feb Mon Sep 17 00:00:00 2001 From: Xuan Son Nguyen Date: Wed, 31 Dec 2025 11:43:53 +0100 Subject: [PATCH] rm type inference --- common/jinja/jinja-type-infer.h | 38 --------------------------------- common/jinja/jinja-value.h | 23 -------------------- common/jinja/jinja-vm.cpp | 15 ------------- common/jinja/jinja-vm.h | 36 ------------------------------- tests/test-chat-jinja.cpp | 19 ----------------- 5 files changed, 131 deletions(-) delete mode 100644 common/jinja/jinja-type-infer.h diff --git a/common/jinja/jinja-type-infer.h b/common/jinja/jinja-type-infer.h deleted file mode 100644 index 3f7508787f..0000000000 --- a/common/jinja/jinja-type-infer.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include -#include - -#include "jinja-value.h" - -namespace jinja { - -struct value_t; -using value = std::shared_ptr; - -// this is used as a hint for chat parsing -// it is not a 1-to-1 mapping to value_t derived types -enum class inferred_type { - numeric, // int, float - string, - boolean, - array, - object, - optional, // null, undefined - unknown, -}; - -static std::string inferred_type_to_string(inferred_type type) { - switch (type) { - case inferred_type::numeric: return "numeric"; - case inferred_type::string: return "string"; - case inferred_type::boolean: return "boolean"; - case inferred_type::array: return "array"; - case inferred_type::object: return "object"; - case inferred_type::optional: return "optional"; - case inferred_type::unknown: return "unknown"; - default: return "invalid"; - } -} - -} // namespace jinja diff --git a/common/jinja/jinja-value.h b/common/jinja/jinja-value.h index 6be5160a89..6483d460a3 100644 --- a/common/jinja/jinja-value.h +++ b/common/jinja/jinja-value.h @@ -9,7 +9,6 @@ #include #include "jinja-string.h" -#include "jinja-type-infer.h" namespace jinja { @@ -108,10 +107,6 @@ struct value_t { func_handler val_func; - // for type inference - std::set inf_types; - std::vector inf_vals; - value_t() = default; value_t(const value_t &) = default; virtual ~value_t() = default; @@ -343,23 +338,5 @@ using value_kwarg = std::shared_ptr; const func_builtins & global_builtins(); -static inferred_type value_to_inferred_type(const value & val) { - if (is_val(val) || is_val(val)) { - return inferred_type::numeric; - } else if (is_val(val)) { - return inferred_type::string; - } else if (is_val(val)) { - return inferred_type::boolean; - } else if (is_val(val)) { - return inferred_type::array; - } else if (is_val(val)) { - return inferred_type::object; - } else if (is_val(val) || is_val(val)) { - return inferred_type::optional; - } else { - return inferred_type::unknown; - } -} - } // namespace jinja diff --git a/common/jinja/jinja-vm.cpp b/common/jinja/jinja-vm.cpp index 89dd49ed0a..2a679517e8 100644 --- a/common/jinja/jinja-vm.cpp +++ b/common/jinja/jinja-vm.cpp @@ -102,8 +102,6 @@ value binary_expression::execute_impl(context & ctx) { value right_val = right->execute(ctx); JJ_DEBUG("Executing binary expression %s '%s' %s", left_val->type().c_str(), op.value.c_str(), right_val->type().c_str()); if (op.value == "==") { - ctx.mark_known_type(left_val, right_val); - ctx.mark_known_type(right_val, left_val); return mk_val(value_compare(left_val, right_val)); } else if (op.value == "!=") { return mk_val(!value_compare(left_val, right_val)); @@ -318,13 +316,6 @@ value test_expression::execute_impl(context & ctx) { args.args.push_back(input); auto res = it->second(args); - // hack: allow type inference - if (test_id == "defined" || test_id == "undefined" || test_id == "none") { - ctx.mark_known_type(input, inferred_type::optional); - } else if (test_id == "string") { - ctx.mark_known_type(input, inferred_type::string); - } - if (negate) { return mk_val(!res->as_bool()); } else { @@ -354,9 +345,6 @@ value unary_expression::execute_impl(context & ctx) { value if_statement::execute_impl(context & ctx) { value test_val = test->execute(ctx); - ctx.mark_known_type(test_val, inferred_type::boolean); - ctx.mark_known_type(test_val, inferred_type::optional); - auto out = mk_val(); if (test_val->as_bool()) { for (auto & stmt : body) { @@ -399,9 +387,6 @@ value for_statement::execute_impl(context & ctx) { iterable_val = mk_val(); } - ctx.mark_known_type(iterable_val, inferred_type::array); - ctx.mark_known_type(iterable_val, inferred_type::object); - if (!is_val(iterable_val) && !is_val(iterable_val)) { throw std::runtime_error("Expected iterable or object type in for loop: got " + iterable_val->type()); } diff --git a/common/jinja/jinja-vm.h b/common/jinja/jinja-vm.h index 0ac2e5f16a..3817e7f535 100644 --- a/common/jinja/jinja-vm.h +++ b/common/jinja/jinja-vm.h @@ -78,46 +78,10 @@ struct context { void set_val(const std::string & name, const value & val) { global->insert(name, val); - set_flattened_global_recursively(name, val); - } - - void mark_known_type(value & val, inferred_type type) { - val->inf_types.insert(type); - } - - void mark_known_type(value & val, value & known_val) { - mark_known_type(val, value_to_inferred_type(known_val)); - val->inf_vals.push_back(known_val); - } - - // FOR TESTING ONLY - const value_object & get_global_object() const { - return global; } private: value_object global; - -public: - std::map flatten_globals; // for debugging - void set_flattened_global_recursively(std::string path, const value & val) { - flatten_globals[path] = val; - if (is_val(val)) { - auto & obj = val->as_object(); - for (const auto & pair : obj) { - std::string child_path = path + "." + pair.first; - flatten_globals[child_path] = pair.second; - set_flattened_global_recursively(child_path, pair.second); - } - } else if (is_val(val)) { - auto & arr = val->as_array(); - for (size_t i = 0; i < arr.size(); ++i) { - std::string idx_path = path + "[" + std::to_string(i) + "]"; - flatten_globals[idx_path] = arr[i]; - set_flattened_global_recursively(idx_path, arr[i]); - } - } - } }; /** diff --git a/tests/test-chat-jinja.cpp b/tests/test-chat-jinja.cpp index b6a9a4a766..7f588a8878 100644 --- a/tests/test-chat-jinja.cpp +++ b/tests/test-chat-jinja.cpp @@ -13,7 +13,6 @@ #include "jinja/jinja-parser.h" #include "jinja/jinja-lexer.h" -#include "jinja/jinja-type-infer.h" void run_multiple(); void run_single(std::string contents); @@ -165,22 +164,4 @@ void run_single(std::string contents) { for (const auto & part : parts.get()->val_str.parts) { std::cout << (part.is_input ? "DATA" : "TMPL") << ": " << part.val << "\n"; } - - std::cout << "\n=== TYPES ===\n"; - auto & global_obj = ctx.flatten_globals; - for (const auto & pair : global_obj) { - std::string name = pair.first; - std::string inf_types; - for (const auto & t : pair.second->inf_types) { - inf_types += inferred_type_to_string(t) + " "; - } - if (inf_types.empty()) { - continue; - } - std::string inf_vals; - for (const auto & v : pair.second->inf_vals) { - inf_vals += v->as_string().str() + " ; "; - } - printf("Var: %-20s | Types: %-10s | Vals: %s\n", name.c_str(), inf_types.c_str(), inf_vals.c_str()); - } }