Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ggml org Llama cpp Jinja Runtime Header

From Leeroopedia
Knowledge Sources
Domains Template_Engine, Runtime
Last Updated 2026-02-15 00:00 GMT

Overview

Declares all AST node types, the execution context, the runtime class, and exception types for the Jinja template engine.

Description

This is the largest header in the Jinja engine, defining the complete AST type hierarchy and execution infrastructure. It defines the context class managing variable scopes with inheritance for nested blocks. It declares statement as the base class for all AST nodes with the execute() / execute_impl() pattern. It provides concrete node types for every Jinja construct: control flow (if_statement, for_statement, break_statement, continue_statement), assignments (set_statement), macros (macro_statement), expressions (binary_expression, unary_expression, member_expression, call_expression, filter_expression, test_expression, slice_expression, ternary_expression), and literals (integer_literal, float_literal, string_literal, array_literal, tuple_literal, object_literal). All other Jinja components depend on these definitions.

Usage

Include this header in any file that needs to work with the Jinja AST, execution context, or runtime. All Jinja engine components (lexer.cpp, parser.cpp, runtime.cpp) include this header.

Code Reference

Source Location

Signature

#pragma once

namespace jinja {

struct statement;
using statement_ptr = std::unique_ptr<statement>;
using statements = std::vector<statement_ptr>;

// Type checking helpers
template<typename T> bool is_stmt(const statement_ptr & ptr);
template<typename T> T * cast_stmt(statement_ptr & ptr);
template<typename T> const T * cast_stmt(const statement_ptr & ptr);

void enable_debug(bool enable);

struct context {
    std::shared_ptr<std::string> src;
    std::time_t current_time;
    bool is_get_stats = false;

    context(std::string src = "");
    context(const context & parent);  // inherits variables for nested scopes
    void set_val(const std::string & name, const value & val);
    value get_val(const std::string & name) const;
};

struct statement {
    size_t pos;
    value execute(context & ctx);          // wrapper with error handling
    virtual value execute_impl(context & ctx) = 0;
    virtual std::string type() const = 0;
};

// AST node types:
struct program;
struct if_statement;
struct for_statement;
struct set_statement;
struct macro_statement;
struct binary_expression;
struct member_expression;
struct call_expression;
struct filter_expression;
struct integer_literal;
struct string_literal;
struct array_literal;
// ... and many more

struct runtime {
    // Orchestrates lexing, parsing, and execution
};

} // namespace jinja

Import

#include "runtime.h"

I/O Contract

Inputs

Name Type Required Description
src std::string No Template source text for error reporting (shared across nested contexts)
parent const context & No Parent context for variable scope inheritance in nested blocks

Outputs

Name Type Description
context struct Variable scope with inherited values, built-in constants (true, false, none), and current time
statement_ptr unique_ptr<statement> Owning pointer to an AST node that can be executed against a context
program struct Top-level AST containing a vector of statement nodes

Usage Examples

#include "runtime.h"

// Create an execution context with template variables
jinja::context ctx("{% for m in messages %}{{ m.content }}{% endfor %}");

// Built-in constants are pre-loaded:
// ctx has "true", "True", "false", "False", "none", "None"

// Set custom variables
ctx.set_val("messages", my_messages_value);
ctx.set_val("add_generation_prompt", mk_val<value_bool>(true));

// Create a child scope (inherits parent variables)
jinja::context child_ctx(ctx);
child_ctx.set_val("loop_var", mk_val<value_int>(42));

// Type-check AST nodes
if (is_stmt<jinja::for_statement>(node)) {
    auto * for_node = cast_stmt<jinja::for_statement>(node);
    // ...
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment