runtime 1.1.4
Checked CEL programs, owned values, and reusable execution stacks
Loading...
Searching...
No Matches
cel::dyn_segment Class Reference

A move-only builder of checked, source-ordered operations. More...

#include <cel/runtime/dyn_segment.hpp>

Public Member Functions

 dyn_segment ()=default
 Creates an empty independent builder.
 dyn_segment (stack_origin origin)
 Creates an empty dependent fragment at the supplied ambient origin.
 dyn_segment (const dyn_segment &)=delete
dyn_segment & operator= (const dyn_segment &)=delete
 dyn_segment (dyn_segment &&) noexcept=default
 Transfers construction ownership.
dyn_segment & operator= (dyn_segment &&) noexcept=default
 Transfers construction ownership, releasing prior captures and metadata.
stack_origin origin () const noexcept
 Returns the retained ambient origin.
std::span< const value_type > operand_types (std::size_t arity) const noexcept
 Borrows the requested local logical operand suffix in older-first order.
stack_origin snapshot () const
 Returns the current absolute logical cursor and live-slot depth.
stack_origin origin_below (std::size_t count) const
 Returns the absolute origin below count local operands.
stack_origin before_top () const
 Returns the origin immediately below the top local operand.
void make_tuple (std::size_t count, stack_origin ambient)
 Emits an operation that consumes count source-ordered operands and returns a tuple.
void make_array (std::size_t count, value_type element, stack_origin ambient)
 Emits an operation that consumes count exact elements and returns an owning array.
void make_record (value_type record, stack_origin ambient)
 Emits a consuming record construction from schema-ordered operands.
void make_record (value_type record, std::span< const std::size_t > permutation, stack_origin ambient)
 Emits a consuming record construction from source-ordered fields and a schema permutation.
void project_tuple (std::size_t index)
 Emits an operation that consumes a tuple and transfers the selected field without cloning.
void project_record (std::string_view name)
 Emits an operation that consumes a record and transfers the named field without cloning.
void index_array ()
 Emits an operation that consumes an array and f64 index and transfers the selected element.
void make_range (range_kind kind)
 Emits a range construction consuming its source-ordered f64 endpoints.
void if_else (dyn_segment then_body, dyn_segment else_body)
 Emits a conditional that consumes bool and executes exactly one dependent body.
void logical_and (dyn_segment rhs)
 Emits a conjunction that consumes bool and executes bool rhs only when the left is true.
void logical_or (dyn_segment rhs)
 Emits a disjunction that consumes bool and executes bool rhs only when the left is false.
void push_closure (compiled_segment body, std::span< const value_type > parameters)
 Emits an installed callable retaining explicit parameters and independent frozen code.
void invoke_closure (std::size_t arity)
 Emits an invocation consuming an installed callee and its source-ordered arguments.
void push_argument (std::size_t index, value_type type)
 Emits a read that clones a borrowed positional input when the program executes.
void just (value literal)
 Retains a literal owner and emits an independent clone on every execution.
template<class R, class... Args, class F>
requires (detail::exact_callback<F, R, Args...>())
void op (F &&callback)
 Emits an arbitrary-arity const callback with exact declared operands and result.
template<class R, class F>
requires (detail::exact_callback<F, R>())
void op0 (F &&callback)
 Emits a zero-operand callback; void produces no value, otherwise pushes one result.
template<class R, class A, class F>
requires (detail::exact_callback<F, R, A>())
void op1 (F &&callback)
 Emits a unary operation that consumes one exact operand and produces R unless it is void.
template<class R, class A, class B, class F>
requires (detail::exact_callback<F, R, A, B>())
void op2 (F &&callback)
 Emits a binary operation that consumes older-first operands and produces R unless it is void.
template<class R, class A, class B, class C, class F>
requires (detail::exact_callback<F, R, A, B, C>())
void op3 (F &&callback)
 Emits a ternary operation consuming source-ordered operands and producing R unless it is void.
compiled_segment finish () &&
 Freezes exactly one result, mapping an empty final stack to source unit.

Detailed Description

A move-only builder of checked, source-ordered operations.

Operations consume older-first exact operands and report runtime failures with exceptions. Successful finish freezes captures and descriptors independently of builder lifetime. Owned callbacks retain their captures; referenced external state stays caller-owned. Destruction releases unfinished captures and metadata.

Complexity: destruction is linear in released code, metadata, and client destructor work. Emission costs include amortized O(1) instruction bookkeeping and, on logical stack growth, O(current stack depth) metadata transfer in addition to each operation's stated costs.

Example: dyn_segment b; b.just(value::from(2.0)); auto p = std::move(b).finish();. See the runtime.hpp example for complete construction, preparation, reservation, and execution.

Examples
getting_started.cpp.

Constructor & Destructor Documentation

◆ dyn_segment() [1/3]

cel::dyn_segment::dyn_segment ( )
default

Creates an empty independent builder.

Example: dyn_segment builder;.

◆ dyn_segment() [2/3]

cel::dyn_segment::dyn_segment ( stack_origin origin)
inlineexplicit

Creates an empty dependent fragment at the supplied ambient origin.

A nonzero byte or depth origin prevents independent execution or preparation after finish. Subsequent emission throws std::overflow_error for an unrepresentable absolute cursor or depth.

Example: dyn_segment branch(parent.before_top());.

◆ dyn_segment() [3/3]

cel::dyn_segment::dyn_segment ( dyn_segment && )
defaultnoexcept

Transfers construction ownership.

Use the source only for destruction or assignment after moving.

Member Function Documentation

◆ before_top()

stack_origin cel::dyn_segment::before_top ( ) const

Returns the origin immediately below the top local operand.

Throws std::invalid_argument if no local operand exists, or std::overflow_error if the absolute depth is unrepresentable.

Example: dyn_segment then_body(builder.before_top());.

◆ finish()

compiled_segment cel::dyn_segment::finish ( ) &&

Freezes exactly one result, mapping an empty final stack to source unit.

Retains the ambient origin; finishing does not make a dependent fragment independent.

Throws std::invalid_argument for leftover operands instead of discarding them.

Complexity: constant bookkeeping and frozen-program allocation; an empty stack also incurs source-unit emission costs.

Example: auto program = std::move(builder).finish();.

◆ if_else()

void cel::dyn_segment::if_else ( dyn_segment then_body,
dyn_segment else_body )

Emits a conditional that consumes bool and executes exactly one dependent body.

Bodies originate immediately below the condition and produce ownership-compatible results. All reads in both bodies are registered and preflighted before parent execution effects. Reservations use the maximum of both bodies' demands.

Throws std::invalid_argument for a missing or non-bool condition, wrong origins, bodies without exactly one result, or incompatible results/reads before publication. Throws std::overflow_error for accounting overflow.

Complexity: O(P + B * (P + B) + complete type-comparison work) construction time and O(P + B) retained read storage for P parent reads and B combined body reads. Execution costs only the selected body's work.

Example: builder.if_else(std::move(yes), std::move(no));.

◆ index_array()

void cel::dyn_segment::index_array ( )

Emits an operation that consumes an array and f64 index and transfers the selected element.

Execution retires the other elements without cloning the selected element.

Throws std::invalid_argument for missing or incompatible operands before publication. Execution throws std::out_of_range for nonfinite, negative, fractional, unrepresentable, or out-of-bounds indices.

Complexity: complete type-comparison work during construction; sibling retirement and selected canonical byte transfer during execution.

Example: builder.just(value::from(0.0)); builder.index_array();.

◆ invoke_closure()

void cel::dyn_segment::invoke_closure ( std::size_t arity)

Emits an invocation consuming an installed callee and its source-ordered arguments.

Complete ownership is bound during construction. Arguments stay valid across nested stack growth. Same-carrier exclusion covers staging, body execution, result publication, and cleanup. Known direct callee requirements are included; signature-only external costs are not inferred. Returns one independent result owner; callable components share code.

Throws std::invalid_argument for missing operands or arity/signature/carrier mismatch before publication; std::overflow_error for canonical accounting overflow. Execution exceptions retire acquired owners and preserve the caller prefix and original exception. Cleanup does not undo external callback effects.

Complexity: O(arity + complete type-comparison work) construction time and retained signature storage; argument canonical-byte transfer and callee work during execution. Execution allocates argument pointer tables and outer argument/result storage, not scalar public owner boxes.

Example: builder.invoke_closure(2);.

◆ just()

void cel::dyn_segment::just ( value literal)

Retains a literal owner and emits an independent clone on every execution.

Callable components share code under value's copy semantics rather than copying captures.

Throws std::invalid_argument if the complete value cannot be cloned. Throws std::overflow_error for canonical frame extent or depth overflow.

Complexity: ownership admission and class emission costs during construction, retaining one owner; the literal's canonical clone costs during execution.

Example: builder.just(value::from(true));.

Examples
getting_started.cpp.

◆ logical_and()

void cel::dyn_segment::logical_and ( dyn_segment rhs)

Emits a conjunction that consumes bool and executes bool rhs only when the left is true.

Follows if_else's origins, shared-read preflight, exceptions, and maximum-demand contracts. Complexity: follows if_else for parent/rhs read merging; O(selected rhs work) execution.

Example: builder.logical_and(std::move(rhs));.

◆ logical_or()

void cel::dyn_segment::logical_or ( dyn_segment rhs)

Emits a disjunction that consumes bool and executes bool rhs only when the left is false.

Follows if_else's origins, shared-read preflight, exceptions, and maximum-demand contracts. Complexity: follows if_else for parent/rhs read merging; O(selected rhs work) execution.

Example: builder.logical_or(std::move(rhs));.

◆ make_array()

void cel::dyn_segment::make_array ( std::size_t count,
value_type element,
stack_origin ambient )

Emits an operation that consumes count exact elements and returns an owning array.

Requires element ownership capability even when count is zero; execution transfers children.

Throws std::invalid_argument for missing operands, incompatible elements, absent ownership, or wrong ambient origin; std::overflow_error for extent/accounting overflow.

Complexity: O(count + complete type-comparison work) construction time and O(count) retained metadata; O(count + total canonical element bytes) execution work and array storage.

Example: builder.make_array(0, value_type::leaf<double>(), {});.

◆ make_range()

void cel::dyn_segment::make_range ( range_kind kind)

Emits a range construction consuming its source-ordered f64 endpoints.

Full ranges consume no endpoints. No floating iteration semantics are supplied.

Throws std::invalid_argument for an invalid kind or wrong operands, or std::overflow_error for canonical accounting overflow.

Complexity: endpoint type-comparison work during construction; constant execution work.

Example: builder.make_range(range_kind::range_full);.

◆ make_record() [1/2]

void cel::dyn_segment::make_record ( value_type record,
stack_origin ambient )

Emits a consuming record construction from schema-ordered operands.

Follows the permutation overload's ownership, validation, and complexity contracts.

Example: builder.make_record(schema, {});.

◆ make_record() [2/2]

void cel::dyn_segment::make_record ( value_type record,
std::span< const std::size_t > permutation,
stack_origin ambient )

Emits a consuming record construction from source-ordered fields and a schema permutation.

Each permutation[i] gives the schema position of source operand i, one entry per field. The permutation does not change expression evaluation order; children transfer without clones. Copies the map and retains record; neither input must outlive construction.

Throws std::invalid_argument for a non-record, wrong origin/types, or an incomplete or nonbijective map before publication; std::overflow_error for extent/accounting overflow.

Complexity: O(field count + complete type-comparison work) construction time and O(field count) retained metadata; O(field count + outer record byte size) execution work.

Example: builder.make_record(schema, std::array<std::size_t, 2>{1, 0}, {});.

◆ make_tuple()

void cel::dyn_segment::make_tuple ( std::size_t count,
stack_origin ambient )

Emits an operation that consumes count source-ordered operands and returns a tuple.

Count zero emits source unit. Execution transfers children without cloning them.

Throws std::invalid_argument for missing operands or wrong byte/depth origin before publication, or std::overflow_error for canonical layout/accounting overflow.

Complexity: O(count + complete type-comparison work) construction time and O(count) retained metadata; O(count + outer tuple byte size) execution work.

Example: builder.make_tuple(2, stack_origin{});.

◆ op()

template<class R, class... Args, class F>
requires (detail::exact_callback<F, R, Args...>())
void cel::dyn_segment::op ( F && callback)
inline

Emits an arbitrary-arity const callback with exact declared operands and result.

R is a trusted native adapter type or void; void consumes without producing a value. Parameter/result identities are exact after cv/reference removal. Reference results are copied into an independently owned result; implicit result conversions are not admitted. Use with_signature for ambiguous or uninspectable callback members. Generic members require deduction-proven exact parameter identities. Lvalue callbacks are copied; rvalue callbacks are moved into owned decayed storage. Referenced objects remain caller-owned, must outlive invocation, and require their own synchronization. Captures require nonthrowing destruction. Execution consumes the declared operands, invokes the callback, then publishes its result. Over-aligned callback operands use reference parameters on MSVC.

Throws std::invalid_argument for current stack mismatch before publication. Throws std::overflow_error for canonical frame extent or depth overflow. Callback exceptions propagate through the execution interface after suffix cleanup.

Complexity: O(arity + adapter construction + complete type-comparison work + callback construction) emission time, plus the class's bookkeeping costs. Execution includes concrete operand/result adaptation and callback work.

Example: builder.op<double, double>([](double n) { return n * 2.0; });.

◆ op0()

template<class R, class F>
requires (detail::exact_callback<F, R>())
void cel::dyn_segment::op0 ( F && callback)
inline

Emits a zero-operand callback; void produces no value, otherwise pushes one result.

Follows op's callback, validation, failure, and complexity contracts.

Example: builder.op0<double>([] { return 2.0; });.

◆ op1()

template<class R, class A, class F>
requires (detail::exact_callback<F, R, A>())
void cel::dyn_segment::op1 ( F && callback)
inline

Emits a unary operation that consumes one exact operand and produces R unless it is void.

Follows op's callback, validation, failure, and complexity contracts.

Example: builder.op1<bool, bool>([](bool b) { return !b; });.

◆ op2()

template<class R, class A, class B, class F>
requires (detail::exact_callback<F, R, A, B>())
void cel::dyn_segment::op2 ( F && callback)
inline

Emits a binary operation that consumes older-first operands and produces R unless it is void.

Follows op's callback, validation, failure, and complexity contracts.

Example: builder.op2<double, double, double>([](double a, double b) { return a - b; });.

Examples
getting_started.cpp.

◆ op3()

template<class R, class A, class B, class C, class F>
requires (detail::exact_callback<F, R, A, B, C>())
void cel::dyn_segment::op3 ( F && callback)
inline

Emits a ternary operation consuming source-ordered operands and producing R unless it is void.

Follows op's callback, validation, failure, and complexity contracts.

Example: builder.op3<double, double, double, double>( [](double a, double b, double c) { return a + b + c; });.

◆ operand_types()

std::span< const value_type > cel::dyn_segment::operand_types ( std::size_t arity) const
inlinenoexcept

Borrows the requested local logical operand suffix in older-first order.

Returns an empty view for zero arity or underflow; ambient origins are not operands. Leaves construction and callback state unchanged without allocating or finishing code. Do not use the view after emission, move, assignment, or destruction of this builder. Failed emission can also invalidate the view. Copying a descriptor from the view retains its immutable metadata independently.

Example: auto inputs = builder.operand_types(2); if (inputs.size() != 2) return;.

◆ operator=()

dyn_segment & cel::dyn_segment::operator= ( dyn_segment && )
defaultnoexcept

Transfers construction ownership, releasing prior captures and metadata.

Use the source only for destruction or assignment after moving, including self-move. Complexity: O(old captures and metadata).

◆ origin()

stack_origin cel::dyn_segment::origin ( ) const
inlinenoexcept

Returns the retained ambient origin.

Example: builder.origin() == stack_origin{}.

◆ origin_below()

stack_origin cel::dyn_segment::origin_below ( std::size_t count) const

Returns the absolute origin below count local operands.

Zero returns the current cursor/depth; the full local depth returns origin().

Throws std::invalid_argument on local underflow and std::overflow_error on depth overflow.

Example: builder.make_tuple(2, builder.origin_below(2));.

◆ project_record()

void cel::dyn_segment::project_record ( std::string_view name)

Emits an operation that consumes a record and transfers the named field without cloning.

Execution retires the record's other fields.

Throws std::invalid_argument for a missing or non-record operand or missing field before publication.

Complexity: field-name search and complete type-comparison work during construction; sibling retirement and selected canonical byte transfer during execution. Name search is linear in field count and the bytes compared against name.

Example: builder.project_record("enabled");.

◆ project_tuple()

void cel::dyn_segment::project_tuple ( std::size_t index)

Emits an operation that consumes a tuple and transfers the selected field without cloning.

Execution retires the tuple's other fields.

Throws std::invalid_argument for a missing or non-tuple operand or invalid index before publication.

Complexity: complete type-comparison work during construction; sibling retirement and selected canonical byte transfer during execution.

Example: builder.project_tuple(0);.

◆ push_argument()

void cel::dyn_segment::push_argument ( std::size_t index,
value_type type )

Emits a read that clones a borrowed positional input when the program executes.

Repeated declarations must have identical complete shapes and ownership plans.

Throws std::invalid_argument for incompatible repeated declarations or missing clone capability. Throws std::overflow_error for canonical frame extent or depth overflow.

Complexity: O(emitted read count + complete type-comparison work) construction time; cloning the input's canonical payload during execution. Class emission costs also apply.

Example: builder.push_argument(0, value_type::leaf<double>());.

Examples
getting_started.cpp.

◆ push_closure()

void cel::dyn_segment::push_closure ( compiled_segment body,
std::span< const value_type > parameters )

Emits an installed callable retaining explicit parameters and independent frozen code.

Results admit all complete owning types. Execution clones a reference-semantic carrier, not the callable's captures.

Precondition
body has not been moved from.

Throws std::invalid_argument for a dependent body, invalid parameters, or undeclared or incompatible reads; std::overflow_error for canonical accounting overflow.

Complexity: parameter binding and complete type-comparison work during construction; constant-size carrier allocation and clone during execution.

Example: builder.push_closure(std::move(body).finish(), parameters);.

◆ snapshot()

stack_origin cel::dyn_segment::snapshot ( ) const

Returns the current absolute logical cursor and live-slot depth.

Throws std::overflow_error if the absolute depth is unrepresentable.

Example: auto ambient = builder.snapshot();.


The documentation for this class was generated from the following file: