Lines
100 %
Functions
Branches
//! DO / DO* with runtime-valued bounds. `N` is bound to a real
//! runtime Ratio via the host fn; the loop test exercises the
//! compile_do_runtime path that allocates a wasm local for `I` and
//! emits the step/test wasm.
use super::common::compile_and_validate;
/// Wraps a do-loop body so `N` is a runtime Ratio.
fn wrap_with_runtime_n(body: &str) -> String {
// N is a loop bound — a count — so it is a runtime Index (I32). ADR-0028:
// an integer counter loop stays in the Index stratum end to end.
format!("(let* ((N (entity-count))) {body})")
}
#[test]
fn runtime_do_simple() {
compile_and_validate(&wrap_with_runtime_n("(do ((I 0 (+ I 1))) ((>= I N) I))"));
fn runtime_do_with_body() {
compile_and_validate(&wrap_with_runtime_n(
"(let* ((SUM 0)) \
(do ((I 0 (+ I 1))) ((>= I N) SUM) \
(setf SUM (+ SUM I))))",
));
fn runtime_do_star() {
compile_and_validate(&wrap_with_runtime_n("(do* ((I 0 (+ I 1))) ((>= I N) I))"));
fn runtime_do_multiple_vars() {
"(do ((I 0 (+ I 1)) (J N (- J 1))) ((>= I J) (+ I J)))",
fn runtime_do_no_result_form() {
compile_and_validate(&wrap_with_runtime_n("(do ((I 0 (+ I 1))) ((>= I N)))"));
/// A runtime DO whose accumulator step is `(cons <host-fn> acc)` drives the
/// pair-element inference walker (`infer_result_pair_element` →
/// `step_pair_element` → `resolve_pair_element_from_expr`), which eval-probes
/// the pure-native cons car (gated by `is_pure_native_expr` / `head_is_native`)
/// to size the accumulator local. The car here is a native host-fn call, so
/// the probe fires and the accumulator is sized to its element type.
fn runtime_do_cons_accumulator_pure_native_car() {
"(do ((I 0 (+ I 1)) (ACC nil (cons (transaction-post-date 0) ACC))) ((>= I N) ACC))",
/// The same accumulator shape with a USER function car: the probe runs the
/// call through the depth-guarded inliner, so the accumulator is sized from it.
fn runtime_do_cons_accumulator_user_fn_car_is_probed() {
"(defun g (x) (* x 2/1)) \
(do ((I 0 (+ I 1)) (ACC nil (cons (g (transaction-post-date 0)) ACC))) ((>= I N) ACC))",