# ARCHITECTURE: jq Interpreter

| Field | Value |
|-------|-------|
| Version | 20261028 V1 |
| Description | Defines the Python interpreter foundation and its shared execution boundaries. |
| Provides | Python application foundation |

## Structure and contracts

The application is a standalone Python interpreter. The root executable `jq` ultimately delegates
argument handling and JSON input and output to the interpreter. Establish the package `jq_interpreter`
with these shared interfaces:

- `jq_interpreter.errors.CompileError` and `jq_interpreter.errors.RuntimeError` distinguish errors
  detected before execution from errors raised during evaluation. The latter is an interpreter
  exception and need not inherit from Python's built-in `RuntimeError`.
- `jq_interpreter.lexer.tokenize(source)` returns an ordered iterable of tokens, each exposing
  `kind`, `value`, `line`, and `column`.
- `jq_interpreter.parser.parse(source)` consumes the lexer contract and returns an abstract syntax
  tree or raises `CompileError`.
- `jq_interpreter.evaluator.evaluate(ast, value, environment=None)` returns an iterator of results.
  It must not eagerly consume the result stream. Lexical environments carry bindings and function
  definitions without mutating an enclosing scope.
- Builtins receive and produce values through the evaluator's generator model. The command entry
  point maps compile and runtime errors to their distinct exit statuses.

The foundation establishes the package and error interfaces; subsequent stories implement the
lexer, parser, evaluator, builtins, and executable. An evaluator may yield values before raising
`RuntimeError`; callers must retain those values.

## Boundary ownership

| Boundary | Owner | Allowed low-level access |
|----------|-------|--------------------------|
| Source text and tokenization | Lexer | Python text and Unicode facilities |
| Grammar and static validation | Parser | Lexer token stream |
| Evaluation and lexical scope | Evaluator | AST, environments, and builtin interfaces |
| Builtins | Builtin modules | Python standard library and evaluator interfaces |
| Command input and output | Command entry point | Standard input, output, error, and process arguments |
| Persistence and external services | None | No persistent store or external service is required |

## Technology Stack

Python 3.11 or newer provides the interpreter and its standard-library runtime. POSIX shell runs
the supplied scoring entry point. No package installation or network access is required.

## Programmatic Acceptance

=== AC foundation-errors ===
Intent: The foundation exposes separate compile and runtime exception contracts for later interpreter stages.

from jq_interpreter.errors import CompileError, RuntimeError as JqRuntimeError

assert issubclass(CompileError, Exception)
assert issubclass(JqRuntimeError, Exception)
assert CompileError is not JqRuntimeError
assert not issubclass(CompileError, JqRuntimeError)
assert not issubclass(JqRuntimeError, CompileError)
=== END AC foundation-errors ===

## User Acceptance

- None.

## Guardrails

- Use Python and the standard library only; do not install packages or use an external jq runtime.
- Do not shell out to a system jq executable or use a third-party jq implementation or binding.
- Preserve ordered lazy generation, lexical scope, and distinct compile and runtime failures in
  the interfaces built on this foundation.
- Do not modify files under `sources/`.
