Spec modularization: - Add (define-module :name) markers to primitives.sx creating 11 modules (7 core, 4 stdlib). Bootstrappers can now selectively include modules. - Add parse_primitives_by_module() to boundary_parser.py. - Remove split-ids primitive; inline at 4 call sites in blog/market queries. Python file split: - primitives.py: slimmed to registry + core primitives only (~350 lines) - primitives_stdlib.py: NEW — stdlib primitives (format, text, style, debug) - primitives_ctx.py: NEW — extracted 12 page context builders from IO - primitives_io.py: add register_io_handler decorator, auto-derive IO_PRIMITIVES from registry, move sync IO bridges here JS parity fixes: - = uses === (strict equality), != uses !== - round supports optional ndigits parameter - concat uses nil-check not falsy-check (preserves 0, "", false) - escape adds single quote entity (') matching Python/markupsafe - assert added (was missing from JS entirely) Bootstrapper modularization: - PRIMITIVES_JS_MODULES / PRIMITIVES_PY_MODULES dicts keyed by module - --modules CLI flag for selective inclusion (core.* always included) - Regenerated sx-ref.js and sx_ref.py with all fixes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
82 lines
1.5 KiB
Python
82 lines
1.5 KiB
Python
"""
|
|
S-expression language core.
|
|
|
|
Parse, evaluate, and serialize s-expressions. This package provides the
|
|
foundation for the composable fragment architecture described in
|
|
``docs/sx-architecture-plan.md``.
|
|
|
|
Quick start::
|
|
|
|
from shared.sx import parse, evaluate, serialize, Symbol, Keyword
|
|
|
|
expr = parse('(let ((x 10)) (+ x 1))')
|
|
result = evaluate(expr) # → 11
|
|
|
|
expr2 = parse('(map (fn (n) (* n n)) (list 1 2 3))')
|
|
result2 = evaluate(expr2) # → [1, 4, 9]
|
|
"""
|
|
|
|
from .types import (
|
|
NIL,
|
|
Component,
|
|
HandlerDef,
|
|
Keyword,
|
|
Lambda,
|
|
Macro,
|
|
Symbol,
|
|
)
|
|
from .parser import (
|
|
ParseError,
|
|
parse,
|
|
parse_all,
|
|
serialize,
|
|
)
|
|
import os as _os
|
|
|
|
if _os.environ.get("SX_USE_REF") == "1":
|
|
from .ref.sx_ref import (
|
|
EvalError,
|
|
evaluate,
|
|
make_env,
|
|
)
|
|
else:
|
|
from .evaluator import (
|
|
EvalError,
|
|
evaluate,
|
|
make_env,
|
|
)
|
|
|
|
from .primitives import (
|
|
all_primitives,
|
|
get_primitive,
|
|
register_primitive,
|
|
)
|
|
from . import primitives_stdlib # noqa: F401 — registers stdlib primitives
|
|
from .env import Env
|
|
|
|
__all__ = [
|
|
# Types
|
|
"Symbol",
|
|
"Keyword",
|
|
"Lambda",
|
|
"Macro",
|
|
"Component",
|
|
"HandlerDef",
|
|
"NIL",
|
|
# Parser
|
|
"parse",
|
|
"parse_all",
|
|
"serialize",
|
|
"ParseError",
|
|
# Evaluator
|
|
"evaluate",
|
|
"make_env",
|
|
"EvalError",
|
|
# Primitives
|
|
"register_primitive",
|
|
"get_primitive",
|
|
"all_primitives",
|
|
# Environment
|
|
"Env",
|
|
]
|