# FEATURE: Bindings and Functions

| Field | Value |
|-------|-------|
| Version | 20261028 V1 |
| Description | Provide lexical bindings, destructuring, and user-defined jq functions. |
| Depends On | FEATURE-Control.md |
| Provides | jq lexical bindings and functions |
| Consumes | jq conditional and error control |

## Behavior

An `as` binding evaluates its source as a generator and runs the remaining expression once per result against the original input with lexical bindings in scope. Array and object patterns bind nested values, using null for missing elements or fields. Destructuring alternatives try subsequent patterns when binding or the downstream expression raises an error; variables absent from the successful pattern are null.

Function definitions are lexically scoped by name and arity, support shadowing and recursion, and capture their defining environment. Filter arguments remain filters evaluated with their invocation input; value parameters bind each argument result at the call site. Preserve backtracking and ordered multiplicity across function calls and returns. Reject undefined symbols and invalid patterns at compile time.

## Programmatic Acceptance

=== AC bindings-corpus ===
Intent: Bindings and function definitions execute the selected corpus cases with lexical scope and generator semantics.
Suite: scoped
Requires: executable=python3; scope=test
Probe: --list

import os
import re
import sys

sys.path.insert(0, "sources")
import run_conformance as harness

SELECT = r"^1 as \$x \| 2 as \$y|^1 as \$x \| \[\$x|^def f: \(1000,2000\); f$|^def a: 0; \. \| a$"
cases = harness.parse_corpus(harness.CORPUS.read_text(encoding="utf-8"))
excluded = harness.apply_exclusions(cases, harness.parse_exclusions(harness.EXCLUSIONS))
selected = [case for case in cases if re.search(SELECT, case.program)]
results = [
    harness.Result(case, harness.SKIP) if case.line in excluded
    else harness.run_case(case, [os.path.abspath("jq")], harness.DEFAULT_TIMEOUT)
    for case in selected
]
tally = {status: sum(result.status == status for result in results)
         for status in (harness.PASS, harness.FAIL, harness.ERROR, harness.SKIP)}
for result in results:
    if result.status in (harness.FAIL, harness.ERROR):
        print(harness._render_failure(result))
print(f"{tally['pass']} passed, {tally['fail']} failed, {tally['error']} errored")
assert sum(tally.values()) > 0
assert tally["fail"] == 0 and tally["error"] == 0, tally
=== END AC bindings-corpus ===

## User Acceptance

- None.

## Guardrails

- Preserve lexical scope, lazy evaluation, and generator ordering.
- Defer cases combining bindings with later capabilities to scoped integration.
