All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 4m11s
- env.py: Add MergedEnv with dual-parent lookup (primary for set!, secondary for reads), add dict-compat methods to Env - platform_py.py: make_lambda stores env reference (no copy), env_merge uses MergedEnv for proper set! propagation, ancestor detection prevents unbounded chains in TCO recursion, sf_set_bang walks scope chain - types.py: Component/Island io_refs defaults to None (not computed) instead of empty set, so component-pure? falls through to scan - run.py: Test env uses Env class, mock execute-action calls SX lambdas via _call_sx instead of direct Python call Spec tests: 320/320 (was 311/320) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
3062 lines
121 KiB
Python
3062 lines
121 KiB
Python
"""
|
|
sx_ref.py -- Generated from reference SX evaluator specification.
|
|
|
|
Bootstrap-compiled from shared/sx/ref/{eval,render,adapter-html,adapter-sx}.sx
|
|
Compare against hand-written evaluator.py / html.py for correctness verification.
|
|
|
|
DO NOT EDIT -- regenerate with: python run_py_sx.py
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
from typing import Any
|
|
|
|
|
|
# =========================================================================
|
|
# Types (reuse existing types)
|
|
# =========================================================================
|
|
|
|
from shared.sx.types import (
|
|
NIL, Symbol, Keyword, Lambda, Component, Island, Continuation, Macro,
|
|
HandlerDef, QueryDef, ActionDef, PageDef, _ShiftSignal,
|
|
)
|
|
from shared.sx.parser import SxExpr
|
|
from shared.sx.env import Env as _Env, MergedEnv as _MergedEnv
|
|
|
|
|
|
# =========================================================================
|
|
# Platform interface -- Python implementation
|
|
# =========================================================================
|
|
|
|
class _Thunk:
|
|
"""Deferred evaluation for TCO."""
|
|
__slots__ = ("expr", "env")
|
|
def __init__(self, expr, env):
|
|
self.expr = expr
|
|
self.env = env
|
|
|
|
|
|
class _RawHTML:
|
|
"""Marker for pre-rendered HTML that should not be escaped."""
|
|
__slots__ = ("html",)
|
|
def __init__(self, html: str):
|
|
self.html = html
|
|
|
|
|
|
def sx_truthy(x):
|
|
"""SX truthiness: everything is truthy except False, None, and NIL."""
|
|
if x is False:
|
|
return False
|
|
if x is None or x is NIL:
|
|
return False
|
|
return True
|
|
|
|
|
|
def sx_str(*args):
|
|
"""SX str: concatenate string representations, skipping nil."""
|
|
parts = []
|
|
for a in args:
|
|
if a is None or a is NIL:
|
|
continue
|
|
parts.append(str(a))
|
|
return "".join(parts)
|
|
|
|
|
|
def sx_and(*args):
|
|
"""SX and: return last truthy value or first falsy."""
|
|
result = True
|
|
for a in args:
|
|
if not sx_truthy(a):
|
|
return a
|
|
result = a
|
|
return result
|
|
|
|
|
|
def sx_or(*args):
|
|
"""SX or: return first truthy value or last value."""
|
|
for a in args:
|
|
if sx_truthy(a):
|
|
return a
|
|
return args[-1] if args else False
|
|
|
|
|
|
def _sx_begin(*args):
|
|
"""Evaluate all args (for side effects), return last."""
|
|
return args[-1] if args else NIL
|
|
|
|
|
|
|
|
def _sx_case(match_val, pairs):
|
|
"""Case dispatch: pairs is [(test_val, body_fn), ...]. None test = else."""
|
|
for test, body_fn in pairs:
|
|
if test is None: # :else clause
|
|
return body_fn()
|
|
if match_val == test:
|
|
return body_fn()
|
|
return NIL
|
|
|
|
|
|
def _sx_fn(f):
|
|
"""Identity wrapper for multi-expression lambda bodies."""
|
|
return f
|
|
|
|
|
|
def type_of(x):
|
|
if x is None or x is NIL:
|
|
return "nil"
|
|
if isinstance(x, bool):
|
|
return "boolean"
|
|
if isinstance(x, (int, float)):
|
|
return "number"
|
|
if isinstance(x, SxExpr):
|
|
return "sx-expr"
|
|
if isinstance(x, str):
|
|
return "string"
|
|
if isinstance(x, Symbol):
|
|
return "symbol"
|
|
if isinstance(x, Keyword):
|
|
return "keyword"
|
|
if isinstance(x, _Thunk):
|
|
return "thunk"
|
|
if isinstance(x, Lambda):
|
|
return "lambda"
|
|
if isinstance(x, Component):
|
|
return "component"
|
|
if isinstance(x, Island):
|
|
return "island"
|
|
if isinstance(x, _Signal):
|
|
return "signal"
|
|
if isinstance(x, Macro):
|
|
return "macro"
|
|
if isinstance(x, _RawHTML):
|
|
return "raw-html"
|
|
if isinstance(x, Continuation):
|
|
return "continuation"
|
|
if isinstance(x, list):
|
|
return "list"
|
|
if isinstance(x, dict):
|
|
return "dict"
|
|
return "unknown"
|
|
|
|
|
|
def symbol_name(s):
|
|
return s.name
|
|
|
|
|
|
def keyword_name(k):
|
|
return k.name
|
|
|
|
|
|
def make_symbol(n):
|
|
return Symbol(n)
|
|
|
|
|
|
def make_keyword(n):
|
|
return Keyword(n)
|
|
|
|
|
|
def _ensure_env(env):
|
|
"""Wrap plain dict in Env if needed."""
|
|
if isinstance(env, _Env):
|
|
return env
|
|
return _Env(env if isinstance(env, dict) else {})
|
|
|
|
|
|
def make_lambda(params, body, env):
|
|
return Lambda(params=list(params), body=body, closure=_ensure_env(env))
|
|
|
|
|
|
def make_component(name, params, has_children, body, env, affinity="auto"):
|
|
return Component(name=name, params=list(params), has_children=has_children,
|
|
body=body, closure=dict(env), affinity=str(affinity) if affinity else "auto")
|
|
|
|
|
|
def make_island(name, params, has_children, body, env):
|
|
return Island(name=name, params=list(params), has_children=has_children,
|
|
body=body, closure=dict(env))
|
|
|
|
|
|
def make_macro(params, rest_param, body, env, name=None):
|
|
return Macro(params=list(params), rest_param=rest_param, body=body,
|
|
closure=dict(env), name=name)
|
|
|
|
|
|
def make_handler_def(name, params, body, env):
|
|
return HandlerDef(name=name, params=list(params), body=body, closure=dict(env))
|
|
|
|
|
|
def make_query_def(name, params, doc, body, env):
|
|
return QueryDef(name=name, params=list(params), doc=doc, body=body, closure=dict(env))
|
|
|
|
|
|
def make_action_def(name, params, doc, body, env):
|
|
return ActionDef(name=name, params=list(params), doc=doc, body=body, closure=dict(env))
|
|
|
|
|
|
def make_page_def(name, slots, env):
|
|
path = slots.get("path", "")
|
|
auth_val = slots.get("auth", "public")
|
|
if isinstance(auth_val, Keyword):
|
|
auth = auth_val.name
|
|
elif isinstance(auth_val, list):
|
|
auth = [item.name if isinstance(item, Keyword) else str(item) for item in auth_val]
|
|
else:
|
|
auth = str(auth_val) if auth_val else "public"
|
|
layout = slots.get("layout")
|
|
if isinstance(layout, Keyword):
|
|
layout = layout.name
|
|
cache = None
|
|
stream_val = slots.get("stream")
|
|
stream = bool(trampoline(eval_expr(stream_val, env))) if stream_val is not None else False
|
|
return PageDef(
|
|
name=name, path=path, auth=auth, layout=layout, cache=cache,
|
|
data_expr=slots.get("data"), content_expr=slots.get("content"),
|
|
filter_expr=slots.get("filter"), aside_expr=slots.get("aside"),
|
|
menu_expr=slots.get("menu"), stream=stream,
|
|
fallback_expr=slots.get("fallback"), shell_expr=slots.get("shell"),
|
|
closure=dict(env),
|
|
)
|
|
|
|
|
|
def make_thunk(expr, env):
|
|
return _Thunk(expr, env)
|
|
|
|
|
|
def lambda_params(f):
|
|
return f.params
|
|
|
|
|
|
def lambda_body(f):
|
|
return f.body
|
|
|
|
|
|
def lambda_closure(f):
|
|
return f.closure
|
|
|
|
|
|
def lambda_name(f):
|
|
return f.name
|
|
|
|
|
|
def set_lambda_name(f, n):
|
|
f.name = n
|
|
|
|
|
|
def component_params(c):
|
|
return c.params
|
|
|
|
|
|
def component_body(c):
|
|
return c.body
|
|
|
|
|
|
def component_closure(c):
|
|
return c.closure
|
|
|
|
|
|
def component_has_children(c):
|
|
return c.has_children
|
|
|
|
|
|
def component_name(c):
|
|
return c.name
|
|
|
|
|
|
def component_affinity(c):
|
|
return getattr(c, 'affinity', 'auto')
|
|
|
|
|
|
def macro_params(m):
|
|
return m.params
|
|
|
|
|
|
def macro_rest_param(m):
|
|
return m.rest_param
|
|
|
|
|
|
def macro_body(m):
|
|
return m.body
|
|
|
|
|
|
def macro_closure(m):
|
|
return m.closure
|
|
|
|
|
|
def is_thunk(x):
|
|
return isinstance(x, _Thunk)
|
|
|
|
|
|
def thunk_expr(t):
|
|
return t.expr
|
|
|
|
|
|
def thunk_env(t):
|
|
return t.env
|
|
|
|
|
|
def is_callable(x):
|
|
return callable(x) or isinstance(x, Lambda)
|
|
|
|
|
|
def is_lambda(x):
|
|
return isinstance(x, Lambda)
|
|
|
|
|
|
def is_component(x):
|
|
return isinstance(x, Component)
|
|
|
|
|
|
def is_macro(x):
|
|
return isinstance(x, Macro)
|
|
|
|
|
|
def is_island(x):
|
|
return isinstance(x, Island)
|
|
|
|
|
|
def is_identical(a, b):
|
|
return a is b
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
# Signal platform -- reactive state primitives
|
|
# -------------------------------------------------------------------------
|
|
|
|
class _Signal:
|
|
"""Reactive signal container."""
|
|
__slots__ = ("value", "subscribers", "deps")
|
|
def __init__(self, value):
|
|
self.value = value
|
|
self.subscribers = []
|
|
self.deps = []
|
|
|
|
|
|
class _TrackingContext:
|
|
"""Context for discovering signal dependencies."""
|
|
__slots__ = ("notify_fn", "deps")
|
|
def __init__(self, notify_fn):
|
|
self.notify_fn = notify_fn
|
|
self.deps = []
|
|
|
|
|
|
_tracking_context = None
|
|
|
|
|
|
def make_signal(value):
|
|
return _Signal(value)
|
|
|
|
|
|
def is_signal(x):
|
|
return isinstance(x, _Signal)
|
|
|
|
|
|
def signal_value(s):
|
|
return s.value if isinstance(s, _Signal) else s
|
|
|
|
|
|
def signal_set_value(s, v):
|
|
if isinstance(s, _Signal):
|
|
s.value = v
|
|
|
|
|
|
def signal_subscribers(s):
|
|
return list(s.subscribers) if isinstance(s, _Signal) else []
|
|
|
|
|
|
def signal_add_sub(s, fn):
|
|
if isinstance(s, _Signal) and fn not in s.subscribers:
|
|
s.subscribers.append(fn)
|
|
|
|
|
|
def signal_remove_sub(s, fn):
|
|
if isinstance(s, _Signal) and fn in s.subscribers:
|
|
s.subscribers.remove(fn)
|
|
|
|
|
|
def signal_deps(s):
|
|
return list(s.deps) if isinstance(s, _Signal) else []
|
|
|
|
|
|
def signal_set_deps(s, deps):
|
|
if isinstance(s, _Signal):
|
|
s.deps = list(deps) if isinstance(deps, list) else []
|
|
|
|
|
|
def set_tracking_context(ctx):
|
|
global _tracking_context
|
|
_tracking_context = ctx
|
|
|
|
|
|
def get_tracking_context():
|
|
global _tracking_context
|
|
return _tracking_context if _tracking_context is not None else NIL
|
|
|
|
|
|
def make_tracking_context(notify_fn):
|
|
return _TrackingContext(notify_fn)
|
|
|
|
|
|
def tracking_context_deps(ctx):
|
|
return ctx.deps if isinstance(ctx, _TrackingContext) else []
|
|
|
|
|
|
def tracking_context_add_dep(ctx, s):
|
|
if isinstance(ctx, _TrackingContext) and s not in ctx.deps:
|
|
ctx.deps.append(s)
|
|
|
|
|
|
def tracking_context_notify_fn(ctx):
|
|
return ctx.notify_fn if isinstance(ctx, _TrackingContext) else NIL
|
|
|
|
|
|
def invoke(f, *args):
|
|
"""Call f with args — handles both native callables and SX lambdas.
|
|
|
|
In Python, all transpiled lambdas are natively callable, so this is
|
|
just a direct call. The JS host needs dispatch logic here because
|
|
SX lambdas from runtime-evaluated code are objects, not functions.
|
|
"""
|
|
return f(*args)
|
|
|
|
|
|
def json_serialize(obj):
|
|
import json
|
|
try:
|
|
return json.dumps(obj)
|
|
except (TypeError, ValueError):
|
|
return "{}"
|
|
|
|
|
|
def is_empty_dict(d):
|
|
if not isinstance(d, dict):
|
|
return True
|
|
return len(d) == 0
|
|
|
|
|
|
# DOM event primitives — no-ops on server (browser-only).
|
|
def dom_listen(el, name, handler):
|
|
return lambda: None
|
|
|
|
def dom_dispatch(el, name, detail=None):
|
|
return False
|
|
|
|
def event_detail(e):
|
|
return None
|
|
|
|
|
|
def env_has(env, name):
|
|
return name in env
|
|
|
|
|
|
def env_get(env, name):
|
|
return env.get(name, NIL)
|
|
|
|
|
|
def env_set(env, name, val):
|
|
env[name] = val
|
|
|
|
|
|
def env_extend(env):
|
|
return _ensure_env(env).extend()
|
|
|
|
|
|
def env_merge(base, overlay):
|
|
base = _ensure_env(base)
|
|
overlay = _ensure_env(overlay)
|
|
if base is overlay:
|
|
# Same env — just extend with empty local scope for params
|
|
return base.extend()
|
|
# Check if base is an ancestor of overlay — if so, no need to merge
|
|
# (common for self-recursive calls where closure == caller's ancestor)
|
|
p = overlay
|
|
depth = 0
|
|
while p is not None and depth < 100:
|
|
if p is base:
|
|
return base.extend()
|
|
p = getattr(p, '_parent', None)
|
|
depth += 1
|
|
# MergedEnv: reads walk base then overlay; set! walks base only
|
|
return _MergedEnv({}, primary=base, secondary=overlay)
|
|
|
|
|
|
def dict_set(d, k, v):
|
|
d[k] = v
|
|
|
|
|
|
def dict_get(d, k):
|
|
v = d.get(k)
|
|
return v if v is not None else NIL
|
|
|
|
|
|
def dict_has(d, k):
|
|
return k in d
|
|
|
|
|
|
def dict_delete(d, k):
|
|
d.pop(k, None)
|
|
|
|
|
|
def is_render_expr(expr):
|
|
"""Placeholder — overridden by transpiled version from render.sx."""
|
|
return False
|
|
|
|
|
|
# Render dispatch -- set by adapter
|
|
_render_expr_fn = None
|
|
|
|
# Render mode flag -- set by render-to-html/aser, checked by eval-list
|
|
# When false, render expressions (HTML tags, components) fall through to eval-call
|
|
_render_mode = False
|
|
|
|
|
|
def render_active_p():
|
|
return _render_mode
|
|
|
|
|
|
def set_render_active_b(val):
|
|
global _render_mode
|
|
_render_mode = bool(val)
|
|
|
|
|
|
def render_expr(expr, env):
|
|
if _render_expr_fn:
|
|
return _render_expr_fn(expr, env)
|
|
# No adapter — fall through to eval_call so components still evaluate
|
|
return eval_call(first(expr), rest(expr), env)
|
|
|
|
|
|
def strip_prefix(s, prefix):
|
|
return s[len(prefix):] if s.startswith(prefix) else s
|
|
|
|
|
|
def error(msg):
|
|
raise EvalError(msg)
|
|
|
|
|
|
def inspect(x):
|
|
return repr(x)
|
|
|
|
|
|
def escape_html(s):
|
|
s = str(s)
|
|
return s.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """)
|
|
|
|
|
|
def escape_attr(s):
|
|
return escape_html(s)
|
|
|
|
|
|
def raw_html_content(x):
|
|
return x.html
|
|
|
|
|
|
def make_raw_html(s):
|
|
return _RawHTML(s)
|
|
|
|
|
|
def sx_expr_source(x):
|
|
return x.source if isinstance(x, SxExpr) else str(x)
|
|
|
|
|
|
try:
|
|
from shared.sx.evaluator import EvalError
|
|
except ImportError:
|
|
class EvalError(Exception):
|
|
pass
|
|
|
|
|
|
def _sx_append(lst, item):
|
|
"""Append item to list, return the item (for expression context)."""
|
|
lst.append(item)
|
|
return item
|
|
|
|
|
|
def _sx_dict_set(d, k, v):
|
|
"""Set key in dict, return the value (for expression context)."""
|
|
d[k] = v
|
|
return v
|
|
|
|
|
|
def _sx_set_attr(obj, attr, val):
|
|
"""Set attribute on object, return the value."""
|
|
setattr(obj, attr, val)
|
|
return val
|
|
|
|
|
|
def _sx_cell_set(cells, name, val):
|
|
"""Set a mutable cell value. Returns the value."""
|
|
cells[name] = val
|
|
return val
|
|
|
|
|
|
def escape_string(s):
|
|
"""Escape a string for SX serialization."""
|
|
return (str(s)
|
|
.replace("\\", "\\\\")
|
|
.replace('"', '\\"')
|
|
.replace("\n", "\\n")
|
|
.replace("\t", "\\t")
|
|
.replace("</script", "<\\/script"))
|
|
|
|
|
|
def serialize(val):
|
|
"""Serialize an SX value to SX source text.
|
|
|
|
Note: parser.sx defines sx-serialize with a serialize alias, but parser.sx
|
|
is only included in JS builds (for client-side parsing). Python builds
|
|
provide this as a platform function.
|
|
"""
|
|
t = type_of(val)
|
|
if t == "sx-expr":
|
|
return val.source
|
|
if t == "nil":
|
|
return "nil"
|
|
if t == "boolean":
|
|
return "true" if val else "false"
|
|
if t == "number":
|
|
return str(val)
|
|
if t == "string":
|
|
return '"' + escape_string(val) + '"'
|
|
if t == "symbol":
|
|
return symbol_name(val)
|
|
if t == "keyword":
|
|
return ":" + keyword_name(val)
|
|
if t == "raw-html":
|
|
escaped = escape_string(raw_html_content(val))
|
|
return '(raw! "' + escaped + '")'
|
|
if t == "list":
|
|
if not val:
|
|
return "()"
|
|
items = [serialize(x) for x in val]
|
|
return "(" + " ".join(items) + ")"
|
|
if t == "dict":
|
|
items = []
|
|
for k, v in val.items():
|
|
items.append(":" + str(k))
|
|
items.append(serialize(v))
|
|
return "{" + " ".join(items) + "}"
|
|
if callable(val):
|
|
return "nil"
|
|
return str(val)
|
|
|
|
# Aliases for transpiled code — parser.sx defines sx-serialize/sx-serialize-dict
|
|
# but parser.sx is JS-only. Provide aliases so transpiled render.sx works.
|
|
sx_serialize = serialize
|
|
sx_serialize_dict = lambda d: serialize(d)
|
|
|
|
_SPECIAL_FORM_NAMES = frozenset() # Placeholder — overridden by transpiled adapter-sx.sx
|
|
_HO_FORM_NAMES = frozenset()
|
|
|
|
def is_special_form(name):
|
|
"""Placeholder — overridden by transpiled version from adapter-sx.sx."""
|
|
return False
|
|
|
|
def is_ho_form(name):
|
|
"""Placeholder — overridden by transpiled version from adapter-sx.sx."""
|
|
return False
|
|
|
|
|
|
def aser_special(name, expr, env):
|
|
"""Placeholder — overridden by transpiled version from adapter-sx.sx."""
|
|
return trampoline(eval_expr(expr, env))
|
|
|
|
|
|
# =========================================================================
|
|
# Primitives
|
|
# =========================================================================
|
|
|
|
# Save builtins before shadowing
|
|
_b_len = len
|
|
_b_map = map
|
|
_b_filter = filter
|
|
_b_range = range
|
|
_b_list = list
|
|
_b_dict = dict
|
|
_b_max = max
|
|
_b_min = min
|
|
_b_round = round
|
|
_b_abs = abs
|
|
_b_sum = sum
|
|
_b_zip = zip
|
|
_b_int = int
|
|
|
|
PRIMITIVES = {}
|
|
|
|
|
|
# core.arithmetic
|
|
PRIMITIVES["+"] = lambda *args: _b_sum(args)
|
|
PRIMITIVES["-"] = lambda a, b=None: -a if b is None else a - b
|
|
PRIMITIVES["*"] = lambda *args: _sx_mul(*args)
|
|
PRIMITIVES["/"] = lambda a, b: a / b
|
|
PRIMITIVES["mod"] = lambda a, b: a % b
|
|
PRIMITIVES["inc"] = lambda n: n + 1
|
|
PRIMITIVES["dec"] = lambda n: n - 1
|
|
PRIMITIVES["abs"] = _b_abs
|
|
PRIMITIVES["floor"] = math.floor
|
|
PRIMITIVES["ceil"] = math.ceil
|
|
PRIMITIVES["round"] = _b_round
|
|
PRIMITIVES["min"] = _b_min
|
|
PRIMITIVES["max"] = _b_max
|
|
PRIMITIVES["sqrt"] = math.sqrt
|
|
PRIMITIVES["pow"] = lambda x, n: x ** n
|
|
PRIMITIVES["clamp"] = lambda x, lo, hi: _b_max(lo, _b_min(hi, x))
|
|
|
|
def _sx_mul(*args):
|
|
r = 1
|
|
for a in args:
|
|
r *= a
|
|
return r
|
|
|
|
|
|
# core.comparison
|
|
PRIMITIVES["="] = lambda a, b: a == b
|
|
PRIMITIVES["!="] = lambda a, b: a != b
|
|
PRIMITIVES["<"] = lambda a, b: a < b
|
|
PRIMITIVES[">"] = lambda a, b: a > b
|
|
PRIMITIVES["<="] = lambda a, b: a <= b
|
|
PRIMITIVES[">="] = lambda a, b: a >= b
|
|
|
|
|
|
# core.logic
|
|
PRIMITIVES["not"] = lambda x: not sx_truthy(x)
|
|
|
|
|
|
# core.predicates
|
|
PRIMITIVES["nil?"] = lambda x: x is None or x is NIL
|
|
PRIMITIVES["number?"] = lambda x: isinstance(x, (int, float)) and not isinstance(x, bool)
|
|
PRIMITIVES["string?"] = lambda x: isinstance(x, str)
|
|
PRIMITIVES["list?"] = lambda x: isinstance(x, _b_list)
|
|
PRIMITIVES["dict?"] = lambda x: isinstance(x, _b_dict)
|
|
PRIMITIVES["continuation?"] = lambda x: isinstance(x, Continuation)
|
|
PRIMITIVES["empty?"] = lambda c: (
|
|
c is None or c is NIL or
|
|
(isinstance(c, (_b_list, str, _b_dict)) and _b_len(c) == 0)
|
|
)
|
|
PRIMITIVES["contains?"] = lambda c, k: (
|
|
str(k) in c if isinstance(c, str) else
|
|
k in c
|
|
)
|
|
PRIMITIVES["odd?"] = lambda n: n % 2 != 0
|
|
PRIMITIVES["even?"] = lambda n: n % 2 == 0
|
|
PRIMITIVES["zero?"] = lambda n: n == 0
|
|
|
|
|
|
# core.strings
|
|
PRIMITIVES["str"] = sx_str
|
|
PRIMITIVES["upper"] = lambda s: str(s).upper()
|
|
PRIMITIVES["lower"] = lambda s: str(s).lower()
|
|
PRIMITIVES["trim"] = lambda s: str(s).strip()
|
|
PRIMITIVES["split"] = lambda s, sep=" ": str(s).split(sep)
|
|
PRIMITIVES["join"] = lambda sep, coll: sep.join(str(x) for x in coll)
|
|
PRIMITIVES["replace"] = lambda s, old, new: s.replace(old, new)
|
|
PRIMITIVES["index-of"] = lambda s, needle, start=0: str(s).find(needle, start)
|
|
PRIMITIVES["starts-with?"] = lambda s, p: str(s).startswith(p)
|
|
PRIMITIVES["ends-with?"] = lambda s, p: str(s).endswith(p)
|
|
PRIMITIVES["slice"] = lambda c, a, b=None: c[int(a):] if (b is None or b is NIL) else c[int(a):int(b)]
|
|
PRIMITIVES["concat"] = lambda *args: _b_sum((a for a in args if a), [])
|
|
|
|
|
|
# core.collections
|
|
PRIMITIVES["list"] = lambda *args: _b_list(args)
|
|
PRIMITIVES["dict"] = lambda *args: {args[i]: args[i+1] for i in _b_range(0, _b_len(args)-1, 2)}
|
|
PRIMITIVES["range"] = lambda a, b, step=1: _b_list(_b_range(_b_int(a), _b_int(b), _b_int(step)))
|
|
PRIMITIVES["get"] = lambda c, k, default=NIL: c.get(k, default) if isinstance(c, _b_dict) else (c[k] if isinstance(c, (_b_list, str)) and isinstance(k, _b_int) and 0 <= k < _b_len(c) else (c.get(k, default) if hasattr(c, 'get') else default))
|
|
PRIMITIVES["len"] = lambda c: _b_len(c) if c is not None and c is not NIL else 0
|
|
PRIMITIVES["first"] = lambda c: c[0] if c and _b_len(c) > 0 else NIL
|
|
PRIMITIVES["last"] = lambda c: c[-1] if c and _b_len(c) > 0 else NIL
|
|
PRIMITIVES["rest"] = lambda c: c[1:] if c else []
|
|
PRIMITIVES["nth"] = lambda c, n: c[n] if c and 0 <= n < _b_len(c) else NIL
|
|
PRIMITIVES["cons"] = lambda x, c: [x] + (c or [])
|
|
PRIMITIVES["append"] = lambda c, x: (c or []) + (x if isinstance(x, list) else [x])
|
|
PRIMITIVES["chunk-every"] = lambda c, n: [c[i:i+n] for i in _b_range(0, _b_len(c), n)]
|
|
PRIMITIVES["zip-pairs"] = lambda c: [[c[i], c[i+1]] for i in _b_range(_b_len(c)-1)]
|
|
|
|
|
|
# core.dict
|
|
PRIMITIVES["keys"] = lambda d: _b_list((d or {}).keys())
|
|
PRIMITIVES["vals"] = lambda d: _b_list((d or {}).values())
|
|
PRIMITIVES["merge"] = lambda *args: _sx_merge_dicts(*args)
|
|
PRIMITIVES["has-key?"] = lambda d, k: isinstance(d, _b_dict) and k in d
|
|
PRIMITIVES["assoc"] = lambda d, *kvs: _sx_assoc(d, *kvs)
|
|
PRIMITIVES["dissoc"] = lambda d, *ks: {k: v for k, v in d.items() if k not in ks}
|
|
PRIMITIVES["into"] = lambda target, coll: (_b_list(coll) if isinstance(target, _b_list) else {p[0]: p[1] for p in coll if isinstance(p, _b_list) and _b_len(p) >= 2})
|
|
PRIMITIVES["zip"] = lambda *colls: [_b_list(t) for t in _b_zip(*colls)]
|
|
|
|
def _sx_merge_dicts(*args):
|
|
out = {}
|
|
for d in args:
|
|
if d and d is not NIL and isinstance(d, _b_dict):
|
|
out.update(d)
|
|
return out
|
|
|
|
def _sx_assoc(d, *kvs):
|
|
out = _b_dict(d) if d and d is not NIL else {}
|
|
for i in _b_range(0, _b_len(kvs) - 1, 2):
|
|
out[kvs[i]] = kvs[i + 1]
|
|
return out
|
|
|
|
|
|
# stdlib.format
|
|
PRIMITIVES["format-decimal"] = lambda v, p=2: f"{float(v):.{p}f}"
|
|
PRIMITIVES["parse-int"] = lambda v, d=0: _sx_parse_int(v, d)
|
|
PRIMITIVES["parse-datetime"] = lambda s: str(s) if s else NIL
|
|
|
|
def _sx_parse_int(v, default=0):
|
|
if v is None or v is NIL:
|
|
return default
|
|
s = str(v).strip()
|
|
# Match JS parseInt: extract leading integer portion
|
|
import re as _re
|
|
m = _re.match(r'^[+-]?\d+', s)
|
|
if m:
|
|
return _b_int(m.group())
|
|
return default
|
|
|
|
|
|
# stdlib.text
|
|
PRIMITIVES["pluralize"] = lambda n, s="", p="s": s if n == 1 else p
|
|
PRIMITIVES["escape"] = escape_html
|
|
PRIMITIVES["strip-tags"] = lambda s: _strip_tags(str(s))
|
|
|
|
import re as _re
|
|
def _strip_tags(s):
|
|
return _re.sub(r"<[^>]+>", "", s)
|
|
|
|
|
|
# stdlib.style — stubs (CSSX needs full runtime)
|
|
|
|
|
|
# stdlib.debug
|
|
PRIMITIVES["assert"] = lambda cond, msg="Assertion failed": (_ for _ in ()).throw(RuntimeError(f"Assertion error: {msg}")) if not sx_truthy(cond) else True
|
|
|
|
|
|
def is_primitive(name):
|
|
if name in PRIMITIVES:
|
|
return True
|
|
from shared.sx.primitives import get_primitive as _ext_get
|
|
return _ext_get(name) is not None
|
|
|
|
def get_primitive(name):
|
|
p = PRIMITIVES.get(name)
|
|
if p is not None:
|
|
return p
|
|
from shared.sx.primitives import get_primitive as _ext_get
|
|
return _ext_get(name)
|
|
|
|
# Higher-order helpers used by transpiled code
|
|
def map(fn, coll):
|
|
return [fn(x) for x in coll]
|
|
|
|
def map_indexed(fn, coll):
|
|
return [fn(i, item) for i, item in enumerate(coll)]
|
|
|
|
def filter(fn, coll):
|
|
return [x for x in coll if sx_truthy(fn(x))]
|
|
|
|
def reduce(fn, init, coll):
|
|
acc = init
|
|
for item in coll:
|
|
acc = fn(acc, item)
|
|
return acc
|
|
|
|
def some(fn, coll):
|
|
for item in coll:
|
|
r = fn(item)
|
|
if sx_truthy(r):
|
|
return r
|
|
return NIL
|
|
|
|
def every_p(fn, coll):
|
|
for item in coll:
|
|
if not sx_truthy(fn(item)):
|
|
return False
|
|
return True
|
|
|
|
def for_each(fn, coll):
|
|
for item in coll:
|
|
fn(item)
|
|
return NIL
|
|
|
|
def for_each_indexed(fn, coll):
|
|
for i, item in enumerate(coll):
|
|
fn(i, item)
|
|
return NIL
|
|
|
|
def map_dict(fn, d):
|
|
return {k: fn(k, v) for k, v in d.items()}
|
|
|
|
# Aliases used directly by transpiled code
|
|
first = PRIMITIVES["first"]
|
|
last = PRIMITIVES["last"]
|
|
rest = PRIMITIVES["rest"]
|
|
nth = PRIMITIVES["nth"]
|
|
len = PRIMITIVES["len"]
|
|
is_nil = PRIMITIVES["nil?"]
|
|
empty_p = PRIMITIVES["empty?"]
|
|
contains_p = PRIMITIVES["contains?"]
|
|
starts_with_p = PRIMITIVES["starts-with?"]
|
|
ends_with_p = PRIMITIVES["ends-with?"]
|
|
slice = PRIMITIVES["slice"]
|
|
get = PRIMITIVES["get"]
|
|
append = PRIMITIVES["append"]
|
|
cons = PRIMITIVES["cons"]
|
|
keys = PRIMITIVES["keys"]
|
|
join = PRIMITIVES["join"]
|
|
range = PRIMITIVES["range"]
|
|
apply = lambda f, args: f(*args)
|
|
assoc = PRIMITIVES["assoc"]
|
|
concat = PRIMITIVES["concat"]
|
|
split = PRIMITIVES["split"]
|
|
length = PRIMITIVES["len"]
|
|
merge = PRIMITIVES["merge"]
|
|
trim = PRIMITIVES["trim"]
|
|
replace = PRIMITIVES["replace"]
|
|
parse_int = PRIMITIVES["parse-int"]
|
|
upper = PRIMITIVES["upper"]
|
|
has_key_p = PRIMITIVES["has-key?"]
|
|
dissoc = PRIMITIVES["dissoc"]
|
|
index_of = PRIMITIVES["index-of"]
|
|
|
|
|
|
# =========================================================================
|
|
# Platform: deps module — component dependency analysis
|
|
# =========================================================================
|
|
|
|
import re as _re
|
|
|
|
def component_deps(c):
|
|
"""Return cached deps list for a component (may be empty)."""
|
|
return list(c.deps) if hasattr(c, "deps") and c.deps else []
|
|
|
|
def component_set_deps(c, deps):
|
|
"""Cache deps on a component."""
|
|
c.deps = set(deps) if not isinstance(deps, set) else deps
|
|
|
|
def component_css_classes(c):
|
|
"""Return pre-scanned CSS class list for a component."""
|
|
return list(c.css_classes) if hasattr(c, "css_classes") and c.css_classes else []
|
|
|
|
def env_components(env):
|
|
"""Placeholder — overridden by transpiled version from deps.sx."""
|
|
return [k for k, v in env.items()
|
|
if isinstance(v, (Component, Macro))]
|
|
|
|
def regex_find_all(pattern, source):
|
|
"""Return list of capture group 1 matches."""
|
|
return [m.group(1) for m in _re.finditer(pattern, source)]
|
|
|
|
def scan_css_classes(source):
|
|
"""Extract CSS class strings from SX source."""
|
|
classes = set()
|
|
for m in _re.finditer(r':class\s+"([^"]*)"', source):
|
|
classes.update(m.group(1).split())
|
|
for m in _re.finditer(r':class\s+\(str\s+((?:"[^"]*"\s*)+)\)', source):
|
|
for s in _re.findall(r'"([^"]*)"', m.group(1)):
|
|
classes.update(s.split())
|
|
for m in _re.finditer(r';;\s*@css\s+(.+)', source):
|
|
classes.update(m.group(1).split())
|
|
return list(classes)
|
|
|
|
def component_io_refs(c):
|
|
"""Return cached IO refs list, or NIL if not yet computed."""
|
|
if not hasattr(c, "io_refs") or c.io_refs is None:
|
|
return NIL
|
|
return list(c.io_refs)
|
|
|
|
def component_set_io_refs(c, refs):
|
|
"""Cache IO refs on a component."""
|
|
c.io_refs = set(refs) if not isinstance(refs, set) else refs
|
|
|
|
|
|
# === Transpiled from eval ===
|
|
|
|
# trampoline
|
|
def trampoline(val):
|
|
result = val
|
|
if sx_truthy(is_thunk(result)):
|
|
return trampoline(eval_expr(thunk_expr(result), thunk_env(result)))
|
|
else:
|
|
return result
|
|
|
|
# eval-expr
|
|
def eval_expr(expr, env):
|
|
_match = type_of(expr)
|
|
if _match == 'number':
|
|
return expr
|
|
elif _match == 'string':
|
|
return expr
|
|
elif _match == 'boolean':
|
|
return expr
|
|
elif _match == 'nil':
|
|
return NIL
|
|
elif _match == 'symbol':
|
|
name = symbol_name(expr)
|
|
if sx_truthy(env_has(env, name)):
|
|
return env_get(env, name)
|
|
elif sx_truthy(is_primitive(name)):
|
|
return get_primitive(name)
|
|
elif sx_truthy((name == 'true')):
|
|
return True
|
|
elif sx_truthy((name == 'false')):
|
|
return False
|
|
elif sx_truthy((name == 'nil')):
|
|
return NIL
|
|
else:
|
|
return error(sx_str('Undefined symbol: ', name))
|
|
elif _match == 'keyword':
|
|
return keyword_name(expr)
|
|
elif _match == 'dict':
|
|
return map_dict(lambda k, v: trampoline(eval_expr(v, env)), expr)
|
|
elif _match == 'list':
|
|
if sx_truthy(empty_p(expr)):
|
|
return []
|
|
else:
|
|
return eval_list(expr, env)
|
|
else:
|
|
return expr
|
|
|
|
# eval-list
|
|
def eval_list(expr, env):
|
|
head = first(expr)
|
|
args = rest(expr)
|
|
if sx_truthy((not sx_truthy(((type_of(head) == 'symbol') if sx_truthy((type_of(head) == 'symbol')) else ((type_of(head) == 'lambda') if sx_truthy((type_of(head) == 'lambda')) else (type_of(head) == 'list')))))):
|
|
return map(lambda x: trampoline(eval_expr(x, env)), expr)
|
|
else:
|
|
if sx_truthy((type_of(head) == 'symbol')):
|
|
name = symbol_name(head)
|
|
if sx_truthy((name == 'if')):
|
|
return sf_if(args, env)
|
|
elif sx_truthy((name == 'when')):
|
|
return sf_when(args, env)
|
|
elif sx_truthy((name == 'cond')):
|
|
return sf_cond(args, env)
|
|
elif sx_truthy((name == 'case')):
|
|
return sf_case(args, env)
|
|
elif sx_truthy((name == 'and')):
|
|
return sf_and(args, env)
|
|
elif sx_truthy((name == 'or')):
|
|
return sf_or(args, env)
|
|
elif sx_truthy((name == 'let')):
|
|
return sf_let(args, env)
|
|
elif sx_truthy((name == 'let*')):
|
|
return sf_let(args, env)
|
|
elif sx_truthy((name == 'letrec')):
|
|
return sf_letrec(args, env)
|
|
elif sx_truthy((name == 'lambda')):
|
|
return sf_lambda(args, env)
|
|
elif sx_truthy((name == 'fn')):
|
|
return sf_lambda(args, env)
|
|
elif sx_truthy((name == 'define')):
|
|
return sf_define(args, env)
|
|
elif sx_truthy((name == 'defcomp')):
|
|
return sf_defcomp(args, env)
|
|
elif sx_truthy((name == 'defisland')):
|
|
return sf_defisland(args, env)
|
|
elif sx_truthy((name == 'defmacro')):
|
|
return sf_defmacro(args, env)
|
|
elif sx_truthy((name == 'defstyle')):
|
|
return sf_defstyle(args, env)
|
|
elif sx_truthy((name == 'defhandler')):
|
|
return sf_defhandler(args, env)
|
|
elif sx_truthy((name == 'defpage')):
|
|
return sf_defpage(args, env)
|
|
elif sx_truthy((name == 'defquery')):
|
|
return sf_defquery(args, env)
|
|
elif sx_truthy((name == 'defaction')):
|
|
return sf_defaction(args, env)
|
|
elif sx_truthy((name == 'begin')):
|
|
return sf_begin(args, env)
|
|
elif sx_truthy((name == 'do')):
|
|
return sf_begin(args, env)
|
|
elif sx_truthy((name == 'quote')):
|
|
return sf_quote(args, env)
|
|
elif sx_truthy((name == 'quasiquote')):
|
|
return sf_quasiquote(args, env)
|
|
elif sx_truthy((name == '->')):
|
|
return sf_thread_first(args, env)
|
|
elif sx_truthy((name == 'set!')):
|
|
return sf_set_bang(args, env)
|
|
elif sx_truthy((name == 'reset')):
|
|
return sf_reset(args, env)
|
|
elif sx_truthy((name == 'shift')):
|
|
return sf_shift(args, env)
|
|
elif sx_truthy((name == 'dynamic-wind')):
|
|
return sf_dynamic_wind(args, env)
|
|
elif sx_truthy((name == 'map')):
|
|
return ho_map(args, env)
|
|
elif sx_truthy((name == 'map-indexed')):
|
|
return ho_map_indexed(args, env)
|
|
elif sx_truthy((name == 'filter')):
|
|
return ho_filter(args, env)
|
|
elif sx_truthy((name == 'reduce')):
|
|
return ho_reduce(args, env)
|
|
elif sx_truthy((name == 'some')):
|
|
return ho_some(args, env)
|
|
elif sx_truthy((name == 'every?')):
|
|
return ho_every(args, env)
|
|
elif sx_truthy((name == 'for-each')):
|
|
return ho_for_each(args, env)
|
|
elif sx_truthy((env_has(env, name) if not sx_truthy(env_has(env, name)) else is_macro(env_get(env, name)))):
|
|
mac = env_get(env, name)
|
|
return make_thunk(expand_macro(mac, args, env), env)
|
|
elif sx_truthy((render_active_p() if not sx_truthy(render_active_p()) else is_render_expr(expr))):
|
|
return render_expr(expr, env)
|
|
else:
|
|
return eval_call(head, args, env)
|
|
else:
|
|
return eval_call(head, args, env)
|
|
|
|
# eval-call
|
|
def eval_call(head, args, env):
|
|
f = trampoline(eval_expr(head, env))
|
|
evaluated_args = map(lambda a: trampoline(eval_expr(a, env)), args)
|
|
if sx_truthy((is_callable(f) if not sx_truthy(is_callable(f)) else ((not sx_truthy(is_lambda(f))) if not sx_truthy((not sx_truthy(is_lambda(f)))) else ((not sx_truthy(is_component(f))) if not sx_truthy((not sx_truthy(is_component(f)))) else (not sx_truthy(is_island(f))))))):
|
|
return apply(f, evaluated_args)
|
|
elif sx_truthy(is_lambda(f)):
|
|
return call_lambda(f, evaluated_args, env)
|
|
elif sx_truthy(is_component(f)):
|
|
return call_component(f, args, env)
|
|
elif sx_truthy(is_island(f)):
|
|
return call_component(f, args, env)
|
|
else:
|
|
return error(sx_str('Not callable: ', inspect(f)))
|
|
|
|
# call-lambda
|
|
def call_lambda(f, args, caller_env):
|
|
params = lambda_params(f)
|
|
local = env_merge(lambda_closure(f), caller_env)
|
|
if sx_truthy((len(args) > len(params))):
|
|
return error(sx_str((lambda_name(f) if sx_truthy(lambda_name(f)) else 'lambda'), ' expects ', len(params), ' args, got ', len(args)))
|
|
else:
|
|
for pair in zip(params, args):
|
|
local[first(pair)] = nth(pair, 1)
|
|
for p in slice(params, len(args)):
|
|
local[p] = NIL
|
|
return make_thunk(lambda_body(f), local)
|
|
|
|
# call-component
|
|
def call_component(comp, raw_args, env):
|
|
parsed = parse_keyword_args(raw_args, env)
|
|
kwargs = first(parsed)
|
|
children = nth(parsed, 1)
|
|
local = env_merge(component_closure(comp), env)
|
|
for p in component_params(comp):
|
|
local[p] = (dict_get(kwargs, p) if sx_truthy(dict_get(kwargs, p)) else NIL)
|
|
if sx_truthy(component_has_children(comp)):
|
|
local['children'] = children
|
|
return make_thunk(component_body(comp), local)
|
|
|
|
# parse-keyword-args
|
|
def parse_keyword_args(raw_args, env):
|
|
kwargs = {}
|
|
children = []
|
|
i = 0
|
|
reduce(lambda state, arg: (lambda idx: (lambda skip: (assoc(state, 'skip', False, 'i', (idx + 1)) if sx_truthy(skip) else (_sx_begin(_sx_dict_set(kwargs, keyword_name(arg), trampoline(eval_expr(nth(raw_args, (idx + 1)), env))), assoc(state, 'skip', True, 'i', (idx + 1))) if sx_truthy(((type_of(arg) == 'keyword') if not sx_truthy((type_of(arg) == 'keyword')) else ((idx + 1) < len(raw_args)))) else _sx_begin(_sx_append(children, trampoline(eval_expr(arg, env))), assoc(state, 'i', (idx + 1))))))(get(state, 'skip')))(get(state, 'i')), {'i': 0, 'skip': False}, raw_args)
|
|
return [kwargs, children]
|
|
|
|
# sf-if
|
|
def sf_if(args, env):
|
|
condition = trampoline(eval_expr(first(args), env))
|
|
if sx_truthy((condition if not sx_truthy(condition) else (not sx_truthy(is_nil(condition))))):
|
|
return make_thunk(nth(args, 1), env)
|
|
else:
|
|
if sx_truthy((len(args) > 2)):
|
|
return make_thunk(nth(args, 2), env)
|
|
else:
|
|
return NIL
|
|
|
|
# sf-when
|
|
def sf_when(args, env):
|
|
condition = trampoline(eval_expr(first(args), env))
|
|
if sx_truthy((condition if not sx_truthy(condition) else (not sx_truthy(is_nil(condition))))):
|
|
for e in slice(args, 1, (len(args) - 1)):
|
|
trampoline(eval_expr(e, env))
|
|
return make_thunk(last(args), env)
|
|
else:
|
|
return NIL
|
|
|
|
# sf-cond
|
|
def sf_cond(args, env):
|
|
if sx_truthy(((type_of(first(args)) == 'list') if not sx_truthy((type_of(first(args)) == 'list')) else (len(first(args)) == 2))):
|
|
return sf_cond_scheme(args, env)
|
|
else:
|
|
return sf_cond_clojure(args, env)
|
|
|
|
# sf-cond-scheme
|
|
def sf_cond_scheme(clauses, env):
|
|
if sx_truthy(empty_p(clauses)):
|
|
return NIL
|
|
else:
|
|
clause = first(clauses)
|
|
test = first(clause)
|
|
body = nth(clause, 1)
|
|
if sx_truthy((((type_of(test) == 'symbol') if not sx_truthy((type_of(test) == 'symbol')) else ((symbol_name(test) == 'else') if sx_truthy((symbol_name(test) == 'else')) else (symbol_name(test) == ':else'))) if sx_truthy(((type_of(test) == 'symbol') if not sx_truthy((type_of(test) == 'symbol')) else ((symbol_name(test) == 'else') if sx_truthy((symbol_name(test) == 'else')) else (symbol_name(test) == ':else')))) else ((type_of(test) == 'keyword') if not sx_truthy((type_of(test) == 'keyword')) else (keyword_name(test) == 'else')))):
|
|
return make_thunk(body, env)
|
|
else:
|
|
if sx_truthy(trampoline(eval_expr(test, env))):
|
|
return make_thunk(body, env)
|
|
else:
|
|
return sf_cond_scheme(rest(clauses), env)
|
|
|
|
# sf-cond-clojure
|
|
def sf_cond_clojure(clauses, env):
|
|
if sx_truthy((len(clauses) < 2)):
|
|
return NIL
|
|
else:
|
|
test = first(clauses)
|
|
body = nth(clauses, 1)
|
|
if sx_truthy((((type_of(test) == 'keyword') if not sx_truthy((type_of(test) == 'keyword')) else (keyword_name(test) == 'else')) if sx_truthy(((type_of(test) == 'keyword') if not sx_truthy((type_of(test) == 'keyword')) else (keyword_name(test) == 'else'))) else ((type_of(test) == 'symbol') if not sx_truthy((type_of(test) == 'symbol')) else ((symbol_name(test) == 'else') if sx_truthy((symbol_name(test) == 'else')) else (symbol_name(test) == ':else'))))):
|
|
return make_thunk(body, env)
|
|
else:
|
|
if sx_truthy(trampoline(eval_expr(test, env))):
|
|
return make_thunk(body, env)
|
|
else:
|
|
return sf_cond_clojure(slice(clauses, 2), env)
|
|
|
|
# sf-case
|
|
def sf_case(args, env):
|
|
match_val = trampoline(eval_expr(first(args), env))
|
|
clauses = rest(args)
|
|
return sf_case_loop(match_val, clauses, env)
|
|
|
|
# sf-case-loop
|
|
def sf_case_loop(match_val, clauses, env):
|
|
if sx_truthy((len(clauses) < 2)):
|
|
return NIL
|
|
else:
|
|
test = first(clauses)
|
|
body = nth(clauses, 1)
|
|
if sx_truthy((((type_of(test) == 'keyword') if not sx_truthy((type_of(test) == 'keyword')) else (keyword_name(test) == 'else')) if sx_truthy(((type_of(test) == 'keyword') if not sx_truthy((type_of(test) == 'keyword')) else (keyword_name(test) == 'else'))) else ((type_of(test) == 'symbol') if not sx_truthy((type_of(test) == 'symbol')) else ((symbol_name(test) == 'else') if sx_truthy((symbol_name(test) == 'else')) else (symbol_name(test) == ':else'))))):
|
|
return make_thunk(body, env)
|
|
else:
|
|
if sx_truthy((match_val == trampoline(eval_expr(test, env)))):
|
|
return make_thunk(body, env)
|
|
else:
|
|
return sf_case_loop(match_val, slice(clauses, 2), env)
|
|
|
|
# sf-and
|
|
def sf_and(args, env):
|
|
if sx_truthy(empty_p(args)):
|
|
return True
|
|
else:
|
|
val = trampoline(eval_expr(first(args), env))
|
|
if sx_truthy((not sx_truthy(val))):
|
|
return val
|
|
else:
|
|
if sx_truthy((len(args) == 1)):
|
|
return val
|
|
else:
|
|
return sf_and(rest(args), env)
|
|
|
|
# sf-or
|
|
def sf_or(args, env):
|
|
if sx_truthy(empty_p(args)):
|
|
return False
|
|
else:
|
|
val = trampoline(eval_expr(first(args), env))
|
|
if sx_truthy(val):
|
|
return val
|
|
else:
|
|
return sf_or(rest(args), env)
|
|
|
|
# sf-let
|
|
def sf_let(args, env):
|
|
if sx_truthy((type_of(first(args)) == 'symbol')):
|
|
return sf_named_let(args, env)
|
|
else:
|
|
bindings = first(args)
|
|
body = rest(args)
|
|
local = env_extend(env)
|
|
(for_each(lambda binding: (lambda vname: _sx_dict_set(local, vname, trampoline(eval_expr(nth(binding, 1), local))))((symbol_name(first(binding)) if sx_truthy((type_of(first(binding)) == 'symbol')) else first(binding))), bindings) if sx_truthy(((type_of(first(bindings)) == 'list') if not sx_truthy((type_of(first(bindings)) == 'list')) else (len(first(bindings)) == 2))) else (lambda i: reduce(lambda acc, pair_idx: (lambda vname: (lambda val_expr: _sx_dict_set(local, vname, trampoline(eval_expr(val_expr, local))))(nth(bindings, ((pair_idx * 2) + 1))))((symbol_name(nth(bindings, (pair_idx * 2))) if sx_truthy((type_of(nth(bindings, (pair_idx * 2))) == 'symbol')) else nth(bindings, (pair_idx * 2)))), NIL, range(0, (len(bindings) / 2))))(0))
|
|
for e in slice(body, 0, (len(body) - 1)):
|
|
trampoline(eval_expr(e, local))
|
|
return make_thunk(last(body), local)
|
|
|
|
# sf-named-let
|
|
def sf_named_let(args, env):
|
|
loop_name = symbol_name(first(args))
|
|
bindings = nth(args, 1)
|
|
body = slice(args, 2)
|
|
params = []
|
|
inits = []
|
|
(for_each(_sx_fn(lambda binding: (
|
|
_sx_append(params, (symbol_name(first(binding)) if sx_truthy((type_of(first(binding)) == 'symbol')) else first(binding))),
|
|
_sx_append(inits, nth(binding, 1))
|
|
)[-1]), bindings) if sx_truthy(((type_of(first(bindings)) == 'list') if not sx_truthy((type_of(first(bindings)) == 'list')) else (len(first(bindings)) == 2))) else reduce(lambda acc, pair_idx: _sx_begin(_sx_append(params, (symbol_name(nth(bindings, (pair_idx * 2))) if sx_truthy((type_of(nth(bindings, (pair_idx * 2))) == 'symbol')) else nth(bindings, (pair_idx * 2)))), _sx_append(inits, nth(bindings, ((pair_idx * 2) + 1)))), NIL, range(0, (len(bindings) / 2))))
|
|
loop_body = (first(body) if sx_truthy((len(body) == 1)) else cons(make_symbol('begin'), body))
|
|
loop_fn = make_lambda(params, loop_body, env)
|
|
loop_fn.name = loop_name
|
|
lambda_closure(loop_fn)[loop_name] = loop_fn
|
|
init_vals = map(lambda e: trampoline(eval_expr(e, env)), inits)
|
|
return call_lambda(loop_fn, init_vals, env)
|
|
|
|
# sf-lambda
|
|
def sf_lambda(args, env):
|
|
params_expr = first(args)
|
|
body_exprs = rest(args)
|
|
body = (first(body_exprs) if sx_truthy((len(body_exprs) == 1)) else cons(make_symbol('begin'), body_exprs))
|
|
param_names = map(lambda p: (symbol_name(p) if sx_truthy((type_of(p) == 'symbol')) else p), params_expr)
|
|
return make_lambda(param_names, body, env)
|
|
|
|
# sf-define
|
|
def sf_define(args, env):
|
|
name_sym = first(args)
|
|
value = trampoline(eval_expr(nth(args, 1), env))
|
|
if sx_truthy((is_lambda(value) if not sx_truthy(is_lambda(value)) else is_nil(lambda_name(value)))):
|
|
value.name = symbol_name(name_sym)
|
|
env[symbol_name(name_sym)] = value
|
|
return value
|
|
|
|
# sf-defcomp
|
|
def sf_defcomp(args, env):
|
|
name_sym = first(args)
|
|
params_raw = nth(args, 1)
|
|
body = last(args)
|
|
comp_name = strip_prefix(symbol_name(name_sym), '~')
|
|
parsed = parse_comp_params(params_raw)
|
|
params = first(parsed)
|
|
has_children = nth(parsed, 1)
|
|
affinity = defcomp_kwarg(args, 'affinity', 'auto')
|
|
comp = make_component(comp_name, params, has_children, body, env, affinity)
|
|
env[symbol_name(name_sym)] = comp
|
|
return comp
|
|
|
|
# defcomp-kwarg
|
|
def defcomp_kwarg(args, key, default_):
|
|
_cells = {}
|
|
end = (len(args) - 1)
|
|
_cells['result'] = default_
|
|
for i in range(2, end, 1):
|
|
if sx_truthy(((type_of(nth(args, i)) == 'keyword') if not sx_truthy((type_of(nth(args, i)) == 'keyword')) else ((keyword_name(nth(args, i)) == key) if not sx_truthy((keyword_name(nth(args, i)) == key)) else ((i + 1) < end)))):
|
|
val = nth(args, (i + 1))
|
|
_cells['result'] = (keyword_name(val) if sx_truthy((type_of(val) == 'keyword')) else val)
|
|
return _cells['result']
|
|
|
|
# parse-comp-params
|
|
def parse_comp_params(params_expr):
|
|
_cells = {}
|
|
params = []
|
|
_cells['has_children'] = False
|
|
_cells['in_key'] = False
|
|
for p in params_expr:
|
|
if sx_truthy((type_of(p) == 'symbol')):
|
|
name = symbol_name(p)
|
|
if sx_truthy((name == '&key')):
|
|
_cells['in_key'] = True
|
|
elif sx_truthy((name == '&rest')):
|
|
_cells['has_children'] = True
|
|
elif sx_truthy((name == '&children')):
|
|
_cells['has_children'] = True
|
|
elif sx_truthy(_cells['has_children']):
|
|
NIL
|
|
elif sx_truthy(_cells['in_key']):
|
|
params.append(name)
|
|
else:
|
|
params.append(name)
|
|
return [params, _cells['has_children']]
|
|
|
|
# sf-defisland
|
|
def sf_defisland(args, env):
|
|
name_sym = first(args)
|
|
params_raw = nth(args, 1)
|
|
body = last(args)
|
|
comp_name = strip_prefix(symbol_name(name_sym), '~')
|
|
parsed = parse_comp_params(params_raw)
|
|
params = first(parsed)
|
|
has_children = nth(parsed, 1)
|
|
island = make_island(comp_name, params, has_children, body, env)
|
|
env[symbol_name(name_sym)] = island
|
|
return island
|
|
|
|
# sf-defmacro
|
|
def sf_defmacro(args, env):
|
|
name_sym = first(args)
|
|
params_raw = nth(args, 1)
|
|
body = nth(args, 2)
|
|
parsed = parse_macro_params(params_raw)
|
|
params = first(parsed)
|
|
rest_param = nth(parsed, 1)
|
|
mac = make_macro(params, rest_param, body, env, symbol_name(name_sym))
|
|
env[symbol_name(name_sym)] = mac
|
|
return mac
|
|
|
|
# parse-macro-params
|
|
def parse_macro_params(params_expr):
|
|
_cells = {}
|
|
params = []
|
|
_cells['rest_param'] = NIL
|
|
reduce(lambda state, p: (assoc(state, 'in-rest', True) if sx_truthy(((type_of(p) == 'symbol') if not sx_truthy((type_of(p) == 'symbol')) else (symbol_name(p) == '&rest'))) else (_sx_begin(_sx_cell_set(_cells, 'rest_param', (symbol_name(p) if sx_truthy((type_of(p) == 'symbol')) else p)), state) if sx_truthy(get(state, 'in-rest')) else _sx_begin(_sx_append(params, (symbol_name(p) if sx_truthy((type_of(p) == 'symbol')) else p)), state))), {'in-rest': False}, params_expr)
|
|
return [params, _cells['rest_param']]
|
|
|
|
# sf-defstyle
|
|
def sf_defstyle(args, env):
|
|
name_sym = first(args)
|
|
value = trampoline(eval_expr(nth(args, 1), env))
|
|
env[symbol_name(name_sym)] = value
|
|
return value
|
|
|
|
# sf-begin
|
|
def sf_begin(args, env):
|
|
if sx_truthy(empty_p(args)):
|
|
return NIL
|
|
else:
|
|
for e in slice(args, 0, (len(args) - 1)):
|
|
trampoline(eval_expr(e, env))
|
|
return make_thunk(last(args), env)
|
|
|
|
# sf-quote
|
|
def sf_quote(args, env):
|
|
if sx_truthy(empty_p(args)):
|
|
return NIL
|
|
else:
|
|
return first(args)
|
|
|
|
# sf-quasiquote
|
|
def sf_quasiquote(args, env):
|
|
return qq_expand(first(args), env)
|
|
|
|
# qq-expand
|
|
def qq_expand(template, env):
|
|
if sx_truthy((not sx_truthy((type_of(template) == 'list')))):
|
|
return template
|
|
else:
|
|
if sx_truthy(empty_p(template)):
|
|
return []
|
|
else:
|
|
head = first(template)
|
|
if sx_truthy(((type_of(head) == 'symbol') if not sx_truthy((type_of(head) == 'symbol')) else (symbol_name(head) == 'unquote'))):
|
|
return trampoline(eval_expr(nth(template, 1), env))
|
|
else:
|
|
return reduce(lambda result, item: ((lambda spliced: (concat(result, spliced) if sx_truthy((type_of(spliced) == 'list')) else (result if sx_truthy(is_nil(spliced)) else concat(result, [spliced]))))(trampoline(eval_expr(nth(item, 1), env))) if sx_truthy(((type_of(item) == 'list') if not sx_truthy((type_of(item) == 'list')) else ((len(item) == 2) if not sx_truthy((len(item) == 2)) else ((type_of(first(item)) == 'symbol') if not sx_truthy((type_of(first(item)) == 'symbol')) else (symbol_name(first(item)) == 'splice-unquote'))))) else concat(result, [qq_expand(item, env)])), [], template)
|
|
|
|
# sf-thread-first
|
|
def sf_thread_first(args, env):
|
|
val = trampoline(eval_expr(first(args), env))
|
|
return reduce(lambda result, form: ((lambda f: (lambda rest_args: (lambda all_args: (apply(f, all_args) if sx_truthy((is_callable(f) if not sx_truthy(is_callable(f)) else (not sx_truthy(is_lambda(f))))) else (trampoline(call_lambda(f, all_args, env)) if sx_truthy(is_lambda(f)) else error(sx_str('-> form not callable: ', inspect(f))))))(cons(result, rest_args)))(map(lambda a: trampoline(eval_expr(a, env)), rest(form))))(trampoline(eval_expr(first(form), env))) if sx_truthy((type_of(form) == 'list')) else (lambda f: (f(result) if sx_truthy((is_callable(f) if not sx_truthy(is_callable(f)) else (not sx_truthy(is_lambda(f))))) else (trampoline(call_lambda(f, [result], env)) if sx_truthy(is_lambda(f)) else error(sx_str('-> form not callable: ', inspect(f))))))(trampoline(eval_expr(form, env)))), val, rest(args))
|
|
|
|
# sf-set!
|
|
def sf_set_bang(args, env):
|
|
name = symbol_name(first(args))
|
|
value = trampoline(eval_expr(nth(args, 1), env))
|
|
env[name] = value
|
|
return value
|
|
|
|
# sf-letrec
|
|
def sf_letrec(args, env):
|
|
bindings = first(args)
|
|
body = rest(args)
|
|
local = env_extend(env)
|
|
names = []
|
|
val_exprs = []
|
|
(for_each(lambda binding: (lambda vname: _sx_begin(_sx_append(names, vname), _sx_append(val_exprs, nth(binding, 1)), _sx_dict_set(local, vname, NIL)))((symbol_name(first(binding)) if sx_truthy((type_of(first(binding)) == 'symbol')) else first(binding))), bindings) if sx_truthy(((type_of(first(bindings)) == 'list') if not sx_truthy((type_of(first(bindings)) == 'list')) else (len(first(bindings)) == 2))) else reduce(lambda acc, pair_idx: (lambda vname: (lambda val_expr: _sx_begin(_sx_append(names, vname), _sx_append(val_exprs, val_expr), _sx_dict_set(local, vname, NIL)))(nth(bindings, ((pair_idx * 2) + 1))))((symbol_name(nth(bindings, (pair_idx * 2))) if sx_truthy((type_of(nth(bindings, (pair_idx * 2))) == 'symbol')) else nth(bindings, (pair_idx * 2)))), NIL, range(0, (len(bindings) / 2))))
|
|
values = map(lambda e: trampoline(eval_expr(e, local)), val_exprs)
|
|
for pair in zip(names, values):
|
|
local[first(pair)] = nth(pair, 1)
|
|
for val in values:
|
|
if sx_truthy(is_lambda(val)):
|
|
for n in names:
|
|
lambda_closure(val)[n] = env_get(local, n)
|
|
for e in slice(body, 0, (len(body) - 1)):
|
|
trampoline(eval_expr(e, local))
|
|
return make_thunk(last(body), local)
|
|
|
|
# sf-dynamic-wind
|
|
def sf_dynamic_wind(args, env):
|
|
before = trampoline(eval_expr(first(args), env))
|
|
body = trampoline(eval_expr(nth(args, 1), env))
|
|
after = trampoline(eval_expr(nth(args, 2), env))
|
|
call_thunk(before, env)
|
|
push_wind_b(before, after)
|
|
result = call_thunk(body, env)
|
|
pop_wind_b()
|
|
call_thunk(after, env)
|
|
return result
|
|
|
|
# expand-macro
|
|
def expand_macro(mac, raw_args, env):
|
|
local = env_merge(macro_closure(mac), env)
|
|
for pair in map_indexed(lambda i, p: [p, i], macro_params(mac)):
|
|
local[first(pair)] = (nth(raw_args, nth(pair, 1)) if sx_truthy((nth(pair, 1) < len(raw_args))) else NIL)
|
|
if sx_truthy(macro_rest_param(mac)):
|
|
local[macro_rest_param(mac)] = slice(raw_args, len(macro_params(mac)))
|
|
return trampoline(eval_expr(macro_body(mac), local))
|
|
|
|
# call-fn
|
|
def call_fn(f, args, env):
|
|
if sx_truthy(is_lambda(f)):
|
|
return trampoline(call_lambda(f, args, env))
|
|
elif sx_truthy(is_callable(f)):
|
|
return apply(f, args)
|
|
else:
|
|
return error(sx_str('Not callable in HO form: ', inspect(f)))
|
|
|
|
# ho-map
|
|
def ho_map(args, env):
|
|
f = trampoline(eval_expr(first(args), env))
|
|
coll = trampoline(eval_expr(nth(args, 1), env))
|
|
return map(lambda item: call_fn(f, [item], env), coll)
|
|
|
|
# ho-map-indexed
|
|
def ho_map_indexed(args, env):
|
|
f = trampoline(eval_expr(first(args), env))
|
|
coll = trampoline(eval_expr(nth(args, 1), env))
|
|
return map_indexed(lambda i, item: call_fn(f, [i, item], env), coll)
|
|
|
|
# ho-filter
|
|
def ho_filter(args, env):
|
|
f = trampoline(eval_expr(first(args), env))
|
|
coll = trampoline(eval_expr(nth(args, 1), env))
|
|
return filter(lambda item: call_fn(f, [item], env), coll)
|
|
|
|
# ho-reduce
|
|
def ho_reduce(args, env):
|
|
f = trampoline(eval_expr(first(args), env))
|
|
init = trampoline(eval_expr(nth(args, 1), env))
|
|
coll = trampoline(eval_expr(nth(args, 2), env))
|
|
return reduce(lambda acc, item: call_fn(f, [acc, item], env), init, coll)
|
|
|
|
# ho-some
|
|
def ho_some(args, env):
|
|
f = trampoline(eval_expr(first(args), env))
|
|
coll = trampoline(eval_expr(nth(args, 1), env))
|
|
return some(lambda item: call_fn(f, [item], env), coll)
|
|
|
|
# ho-every
|
|
def ho_every(args, env):
|
|
f = trampoline(eval_expr(first(args), env))
|
|
coll = trampoline(eval_expr(nth(args, 1), env))
|
|
return every_p(lambda item: call_fn(f, [item], env), coll)
|
|
|
|
# ho-for-each
|
|
def ho_for_each(args, env):
|
|
f = trampoline(eval_expr(first(args), env))
|
|
coll = trampoline(eval_expr(nth(args, 1), env))
|
|
return for_each(lambda item: call_fn(f, [item], env), coll)
|
|
|
|
|
|
# === Transpiled from forms (server definition forms) ===
|
|
|
|
# parse-key-params
|
|
def parse_key_params(params_expr):
|
|
_cells = {}
|
|
params = []
|
|
_cells['in_key'] = False
|
|
for p in params_expr:
|
|
if sx_truthy((type_of(p) == 'symbol')):
|
|
name = symbol_name(p)
|
|
if sx_truthy((name == '&key')):
|
|
_cells['in_key'] = True
|
|
elif sx_truthy(_cells['in_key']):
|
|
params.append(name)
|
|
else:
|
|
params.append(name)
|
|
return params
|
|
|
|
# sf-defhandler
|
|
def sf_defhandler(args, env):
|
|
name_sym = first(args)
|
|
params_raw = nth(args, 1)
|
|
body = nth(args, 2)
|
|
name = symbol_name(name_sym)
|
|
params = parse_key_params(params_raw)
|
|
hdef = make_handler_def(name, params, body, env)
|
|
env[sx_str('handler:', name)] = hdef
|
|
return hdef
|
|
|
|
# sf-defquery
|
|
def sf_defquery(args, env):
|
|
name_sym = first(args)
|
|
params_raw = nth(args, 1)
|
|
name = symbol_name(name_sym)
|
|
params = parse_key_params(params_raw)
|
|
has_doc = ((len(args) >= 4) if not sx_truthy((len(args) >= 4)) else (type_of(nth(args, 2)) == 'string'))
|
|
doc = (nth(args, 2) if sx_truthy(has_doc) else '')
|
|
body = (nth(args, 3) if sx_truthy(has_doc) else nth(args, 2))
|
|
qdef = make_query_def(name, params, doc, body, env)
|
|
env[sx_str('query:', name)] = qdef
|
|
return qdef
|
|
|
|
# sf-defaction
|
|
def sf_defaction(args, env):
|
|
name_sym = first(args)
|
|
params_raw = nth(args, 1)
|
|
name = symbol_name(name_sym)
|
|
params = parse_key_params(params_raw)
|
|
has_doc = ((len(args) >= 4) if not sx_truthy((len(args) >= 4)) else (type_of(nth(args, 2)) == 'string'))
|
|
doc = (nth(args, 2) if sx_truthy(has_doc) else '')
|
|
body = (nth(args, 3) if sx_truthy(has_doc) else nth(args, 2))
|
|
adef = make_action_def(name, params, doc, body, env)
|
|
env[sx_str('action:', name)] = adef
|
|
return adef
|
|
|
|
# sf-defpage
|
|
def sf_defpage(args, env):
|
|
name_sym = first(args)
|
|
name = symbol_name(name_sym)
|
|
slots = {}
|
|
i = 1
|
|
max_i = len(args)
|
|
for idx in range(1, max_i, 2):
|
|
if sx_truthy(((idx < max_i) if not sx_truthy((idx < max_i)) else (type_of(nth(args, idx)) == 'keyword'))):
|
|
if sx_truthy(((idx + 1) < max_i)):
|
|
slots[keyword_name(nth(args, idx))] = nth(args, (idx + 1))
|
|
pdef = make_page_def(name, slots, env)
|
|
env[sx_str('page:', name)] = pdef
|
|
return pdef
|
|
|
|
# stream-chunk-id
|
|
def stream_chunk_id(chunk):
|
|
if sx_truthy(has_key_p(chunk, 'stream-id')):
|
|
return get(chunk, 'stream-id')
|
|
else:
|
|
return 'stream-content'
|
|
|
|
# stream-chunk-bindings
|
|
def stream_chunk_bindings(chunk):
|
|
return dissoc(chunk, 'stream-id')
|
|
|
|
# normalize-binding-key
|
|
def normalize_binding_key(key):
|
|
return replace(key, '_', '-')
|
|
|
|
# bind-stream-chunk
|
|
def bind_stream_chunk(chunk, base_env):
|
|
env = merge({}, base_env)
|
|
bindings = stream_chunk_bindings(chunk)
|
|
for key in keys(bindings):
|
|
env[normalize_binding_key(key)] = get(bindings, key)
|
|
return env
|
|
|
|
# validate-stream-data
|
|
def validate_stream_data(data):
|
|
return ((type_of(data) == 'list') if not sx_truthy((type_of(data) == 'list')) else every_p(lambda item: (type_of(item) == 'dict'), data))
|
|
|
|
|
|
# === Transpiled from render (core) ===
|
|
|
|
# HTML_TAGS
|
|
HTML_TAGS = ['html', 'head', 'body', 'title', 'meta', 'link', 'script', 'style', 'noscript', 'header', 'nav', 'main', 'section', 'article', 'aside', 'footer', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hgroup', 'div', 'p', 'blockquote', 'pre', 'figure', 'figcaption', 'address', 'details', 'summary', 'a', 'span', 'em', 'strong', 'small', 'b', 'i', 'u', 's', 'mark', 'sub', 'sup', 'abbr', 'cite', 'code', 'time', 'br', 'wbr', 'hr', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'table', 'thead', 'tbody', 'tfoot', 'tr', 'th', 'td', 'caption', 'colgroup', 'col', 'form', 'input', 'textarea', 'select', 'option', 'optgroup', 'button', 'label', 'fieldset', 'legend', 'output', 'datalist', 'img', 'video', 'audio', 'source', 'picture', 'canvas', 'iframe', 'svg', 'math', 'path', 'circle', 'ellipse', 'rect', 'line', 'polyline', 'polygon', 'text', 'tspan', 'g', 'defs', 'use', 'clipPath', 'mask', 'pattern', 'linearGradient', 'radialGradient', 'stop', 'filter', 'feGaussianBlur', 'feOffset', 'feBlend', 'feColorMatrix', 'feComposite', 'feMerge', 'feMergeNode', 'feTurbulence', 'feComponentTransfer', 'feFuncR', 'feFuncG', 'feFuncB', 'feFuncA', 'feDisplacementMap', 'feFlood', 'feImage', 'feMorphology', 'feSpecularLighting', 'feDiffuseLighting', 'fePointLight', 'feSpotLight', 'feDistantLight', 'animate', 'animateTransform', 'foreignObject', 'template', 'slot', 'dialog', 'menu']
|
|
|
|
# VOID_ELEMENTS
|
|
VOID_ELEMENTS = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']
|
|
|
|
# BOOLEAN_ATTRS
|
|
BOOLEAN_ATTRS = ['async', 'autofocus', 'autoplay', 'checked', 'controls', 'default', 'defer', 'disabled', 'formnovalidate', 'hidden', 'inert', 'ismap', 'loop', 'multiple', 'muted', 'nomodule', 'novalidate', 'open', 'playsinline', 'readonly', 'required', 'reversed', 'selected']
|
|
|
|
# definition-form?
|
|
def is_definition_form(name):
|
|
return ((name == 'define') if sx_truthy((name == 'define')) else ((name == 'defcomp') if sx_truthy((name == 'defcomp')) else ((name == 'defisland') if sx_truthy((name == 'defisland')) else ((name == 'defmacro') if sx_truthy((name == 'defmacro')) else ((name == 'defstyle') if sx_truthy((name == 'defstyle')) else (name == 'defhandler'))))))
|
|
|
|
# parse-element-args
|
|
def parse_element_args(args, env):
|
|
attrs = {}
|
|
children = []
|
|
reduce(lambda state, arg: (lambda skip: (assoc(state, 'skip', False, 'i', (get(state, 'i') + 1)) if sx_truthy(skip) else ((lambda val: _sx_begin(_sx_dict_set(attrs, keyword_name(arg), val), assoc(state, 'skip', True, 'i', (get(state, 'i') + 1))))(trampoline(eval_expr(nth(args, (get(state, 'i') + 1)), env))) if sx_truthy(((type_of(arg) == 'keyword') if not sx_truthy((type_of(arg) == 'keyword')) else ((get(state, 'i') + 1) < len(args)))) else _sx_begin(_sx_append(children, arg), assoc(state, 'i', (get(state, 'i') + 1))))))(get(state, 'skip')), {'i': 0, 'skip': False}, args)
|
|
return [attrs, children]
|
|
|
|
# render-attrs
|
|
def render_attrs(attrs):
|
|
return join('', map(lambda key: (lambda val: (sx_str(' ', key) if sx_truthy((contains_p(BOOLEAN_ATTRS, key) if not sx_truthy(contains_p(BOOLEAN_ATTRS, key)) else val)) else ('' if sx_truthy((contains_p(BOOLEAN_ATTRS, key) if not sx_truthy(contains_p(BOOLEAN_ATTRS, key)) else (not sx_truthy(val)))) else ('' if sx_truthy(is_nil(val)) else sx_str(' ', key, '="', escape_attr(sx_str(val)), '"')))))(dict_get(attrs, key)), keys(attrs)))
|
|
|
|
# eval-cond
|
|
def eval_cond(clauses, env):
|
|
if sx_truthy(((not sx_truthy(empty_p(clauses))) if not sx_truthy((not sx_truthy(empty_p(clauses)))) else ((type_of(first(clauses)) == 'list') if not sx_truthy((type_of(first(clauses)) == 'list')) else (len(first(clauses)) == 2)))):
|
|
return eval_cond_scheme(clauses, env)
|
|
else:
|
|
return eval_cond_clojure(clauses, env)
|
|
|
|
# eval-cond-scheme
|
|
def eval_cond_scheme(clauses, env):
|
|
if sx_truthy(empty_p(clauses)):
|
|
return NIL
|
|
else:
|
|
clause = first(clauses)
|
|
test = first(clause)
|
|
body = nth(clause, 1)
|
|
if sx_truthy((((type_of(test) == 'symbol') if not sx_truthy((type_of(test) == 'symbol')) else ((symbol_name(test) == 'else') if sx_truthy((symbol_name(test) == 'else')) else (symbol_name(test) == ':else'))) if sx_truthy(((type_of(test) == 'symbol') if not sx_truthy((type_of(test) == 'symbol')) else ((symbol_name(test) == 'else') if sx_truthy((symbol_name(test) == 'else')) else (symbol_name(test) == ':else')))) else ((type_of(test) == 'keyword') if not sx_truthy((type_of(test) == 'keyword')) else (keyword_name(test) == 'else')))):
|
|
return body
|
|
else:
|
|
if sx_truthy(trampoline(eval_expr(test, env))):
|
|
return body
|
|
else:
|
|
return eval_cond_scheme(rest(clauses), env)
|
|
|
|
# eval-cond-clojure
|
|
def eval_cond_clojure(clauses, env):
|
|
if sx_truthy((len(clauses) < 2)):
|
|
return NIL
|
|
else:
|
|
test = first(clauses)
|
|
body = nth(clauses, 1)
|
|
if sx_truthy((((type_of(test) == 'keyword') if not sx_truthy((type_of(test) == 'keyword')) else (keyword_name(test) == 'else')) if sx_truthy(((type_of(test) == 'keyword') if not sx_truthy((type_of(test) == 'keyword')) else (keyword_name(test) == 'else'))) else ((type_of(test) == 'symbol') if not sx_truthy((type_of(test) == 'symbol')) else ((symbol_name(test) == 'else') if sx_truthy((symbol_name(test) == 'else')) else (symbol_name(test) == ':else'))))):
|
|
return body
|
|
else:
|
|
if sx_truthy(trampoline(eval_expr(test, env))):
|
|
return body
|
|
else:
|
|
return eval_cond_clojure(slice(clauses, 2), env)
|
|
|
|
# process-bindings
|
|
def process_bindings(bindings, env):
|
|
local = merge(env)
|
|
for pair in bindings:
|
|
if sx_truthy(((type_of(pair) == 'list') if not sx_truthy((type_of(pair) == 'list')) else (len(pair) >= 2))):
|
|
name = (symbol_name(first(pair)) if sx_truthy((type_of(first(pair)) == 'symbol')) else sx_str(first(pair)))
|
|
local[name] = trampoline(eval_expr(nth(pair, 1), local))
|
|
return local
|
|
|
|
# is-render-expr?
|
|
def is_render_expr(expr):
|
|
if sx_truthy(((not sx_truthy((type_of(expr) == 'list'))) if sx_truthy((not sx_truthy((type_of(expr) == 'list')))) else empty_p(expr))):
|
|
return False
|
|
else:
|
|
h = first(expr)
|
|
if sx_truthy((not sx_truthy((type_of(h) == 'symbol')))):
|
|
return False
|
|
else:
|
|
n = symbol_name(h)
|
|
return ((n == '<>') if sx_truthy((n == '<>')) else ((n == 'raw!') if sx_truthy((n == 'raw!')) else (starts_with_p(n, '~') if sx_truthy(starts_with_p(n, '~')) else (starts_with_p(n, 'html:') if sx_truthy(starts_with_p(n, 'html:')) else (contains_p(HTML_TAGS, n) if sx_truthy(contains_p(HTML_TAGS, n)) else ((index_of(n, '-') > 0) if not sx_truthy((index_of(n, '-') > 0)) else ((len(expr) > 1) if not sx_truthy((len(expr) > 1)) else (type_of(nth(expr, 1)) == 'keyword'))))))))
|
|
|
|
|
|
# === Transpiled from adapter-html ===
|
|
|
|
# render-to-html
|
|
def render_to_html(expr, env):
|
|
set_render_active_b(True)
|
|
_match = type_of(expr)
|
|
if _match == 'nil':
|
|
return ''
|
|
elif _match == 'string':
|
|
return escape_html(expr)
|
|
elif _match == 'number':
|
|
return sx_str(expr)
|
|
elif _match == 'boolean':
|
|
if sx_truthy(expr):
|
|
return 'true'
|
|
else:
|
|
return 'false'
|
|
elif _match == 'list':
|
|
if sx_truthy(empty_p(expr)):
|
|
return ''
|
|
else:
|
|
return render_list_to_html(expr, env)
|
|
elif _match == 'symbol':
|
|
return render_value_to_html(trampoline(eval_expr(expr, env)), env)
|
|
elif _match == 'keyword':
|
|
return escape_html(keyword_name(expr))
|
|
elif _match == 'raw-html':
|
|
return raw_html_content(expr)
|
|
else:
|
|
return render_value_to_html(trampoline(eval_expr(expr, env)), env)
|
|
|
|
# render-value-to-html
|
|
def render_value_to_html(val, env):
|
|
_match = type_of(val)
|
|
if _match == 'nil':
|
|
return ''
|
|
elif _match == 'string':
|
|
return escape_html(val)
|
|
elif _match == 'number':
|
|
return sx_str(val)
|
|
elif _match == 'boolean':
|
|
if sx_truthy(val):
|
|
return 'true'
|
|
else:
|
|
return 'false'
|
|
elif _match == 'list':
|
|
return render_list_to_html(val, env)
|
|
elif _match == 'raw-html':
|
|
return raw_html_content(val)
|
|
else:
|
|
return escape_html(sx_str(val))
|
|
|
|
# RENDER_HTML_FORMS
|
|
RENDER_HTML_FORMS = ['if', 'when', 'cond', 'case', 'let', 'let*', 'begin', 'do', 'define', 'defcomp', 'defisland', 'defmacro', 'defstyle', 'defhandler', 'map', 'map-indexed', 'filter', 'for-each']
|
|
|
|
# render-html-form?
|
|
def is_render_html_form(name):
|
|
return contains_p(RENDER_HTML_FORMS, name)
|
|
|
|
# render-list-to-html
|
|
def render_list_to_html(expr, env):
|
|
if sx_truthy(empty_p(expr)):
|
|
return ''
|
|
else:
|
|
head = first(expr)
|
|
if sx_truthy((not sx_truthy((type_of(head) == 'symbol')))):
|
|
return join('', map(lambda x: render_value_to_html(x, env), expr))
|
|
else:
|
|
name = symbol_name(head)
|
|
args = rest(expr)
|
|
if sx_truthy((name == '<>')):
|
|
return join('', map(lambda x: render_to_html(x, env), args))
|
|
elif sx_truthy((name == 'raw!')):
|
|
return join('', map(lambda x: sx_str(trampoline(eval_expr(x, env))), args))
|
|
elif sx_truthy((name == 'lake')):
|
|
return render_html_lake(args, env)
|
|
elif sx_truthy((name == 'marsh')):
|
|
return render_html_marsh(args, env)
|
|
elif sx_truthy(contains_p(HTML_TAGS, name)):
|
|
return render_html_element(name, args, env)
|
|
elif sx_truthy((starts_with_p(name, '~') if not sx_truthy(starts_with_p(name, '~')) else (env_has(env, name) if not sx_truthy(env_has(env, name)) else is_island(env_get(env, name))))):
|
|
return render_html_island(env_get(env, name), args, env)
|
|
elif sx_truthy(starts_with_p(name, '~')):
|
|
val = env_get(env, name)
|
|
if sx_truthy(is_component(val)):
|
|
return render_html_component(val, args, env)
|
|
elif sx_truthy(is_macro(val)):
|
|
return render_to_html(expand_macro(val, args, env), env)
|
|
else:
|
|
return error(sx_str('Unknown component: ', name))
|
|
elif sx_truthy(is_render_html_form(name)):
|
|
return dispatch_html_form(name, expr, env)
|
|
elif sx_truthy((env_has(env, name) if not sx_truthy(env_has(env, name)) else is_macro(env_get(env, name)))):
|
|
return render_to_html(expand_macro(env_get(env, name), args, env), env)
|
|
else:
|
|
return render_value_to_html(trampoline(eval_expr(expr, env)), env)
|
|
|
|
# dispatch-html-form
|
|
def dispatch_html_form(name, expr, env):
|
|
if sx_truthy((name == 'if')):
|
|
cond_val = trampoline(eval_expr(nth(expr, 1), env))
|
|
if sx_truthy(cond_val):
|
|
return render_to_html(nth(expr, 2), env)
|
|
else:
|
|
if sx_truthy((len(expr) > 3)):
|
|
return render_to_html(nth(expr, 3), env)
|
|
else:
|
|
return ''
|
|
elif sx_truthy((name == 'when')):
|
|
if sx_truthy((not sx_truthy(trampoline(eval_expr(nth(expr, 1), env))))):
|
|
return ''
|
|
else:
|
|
return join('', map(lambda i: render_to_html(nth(expr, i), env), range(2, len(expr))))
|
|
elif sx_truthy((name == 'cond')):
|
|
branch = eval_cond(rest(expr), env)
|
|
if sx_truthy(branch):
|
|
return render_to_html(branch, env)
|
|
else:
|
|
return ''
|
|
elif sx_truthy((name == 'case')):
|
|
return render_to_html(trampoline(eval_expr(expr, env)), env)
|
|
elif sx_truthy(((name == 'let') if sx_truthy((name == 'let')) else (name == 'let*'))):
|
|
local = process_bindings(nth(expr, 1), env)
|
|
return join('', map(lambda i: render_to_html(nth(expr, i), local), range(2, len(expr))))
|
|
elif sx_truthy(((name == 'begin') if sx_truthy((name == 'begin')) else (name == 'do'))):
|
|
return join('', map(lambda i: render_to_html(nth(expr, i), env), range(1, len(expr))))
|
|
elif sx_truthy(is_definition_form(name)):
|
|
trampoline(eval_expr(expr, env))
|
|
return ''
|
|
elif sx_truthy((name == 'map')):
|
|
f = trampoline(eval_expr(nth(expr, 1), env))
|
|
coll = trampoline(eval_expr(nth(expr, 2), env))
|
|
return join('', map(lambda item: (render_lambda_html(f, [item], env) if sx_truthy(is_lambda(f)) else render_to_html(apply(f, [item]), env)), coll))
|
|
elif sx_truthy((name == 'map-indexed')):
|
|
f = trampoline(eval_expr(nth(expr, 1), env))
|
|
coll = trampoline(eval_expr(nth(expr, 2), env))
|
|
return join('', map_indexed(lambda i, item: (render_lambda_html(f, [i, item], env) if sx_truthy(is_lambda(f)) else render_to_html(apply(f, [i, item]), env)), coll))
|
|
elif sx_truthy((name == 'filter')):
|
|
return render_to_html(trampoline(eval_expr(expr, env)), env)
|
|
elif sx_truthy((name == 'for-each')):
|
|
f = trampoline(eval_expr(nth(expr, 1), env))
|
|
coll = trampoline(eval_expr(nth(expr, 2), env))
|
|
return join('', map(lambda item: (render_lambda_html(f, [item], env) if sx_truthy(is_lambda(f)) else render_to_html(apply(f, [item]), env)), coll))
|
|
else:
|
|
return render_value_to_html(trampoline(eval_expr(expr, env)), env)
|
|
|
|
# render-lambda-html
|
|
def render_lambda_html(f, args, env):
|
|
local = env_merge(lambda_closure(f), env)
|
|
for_each_indexed(lambda i, p: _sx_dict_set(local, p, nth(args, i)), lambda_params(f))
|
|
return render_to_html(lambda_body(f), local)
|
|
|
|
# render-html-component
|
|
def render_html_component(comp, args, env):
|
|
kwargs = {}
|
|
children = []
|
|
reduce(lambda state, arg: (lambda skip: (assoc(state, 'skip', False, 'i', (get(state, 'i') + 1)) if sx_truthy(skip) else ((lambda val: _sx_begin(_sx_dict_set(kwargs, keyword_name(arg), val), assoc(state, 'skip', True, 'i', (get(state, 'i') + 1))))(trampoline(eval_expr(nth(args, (get(state, 'i') + 1)), env))) if sx_truthy(((type_of(arg) == 'keyword') if not sx_truthy((type_of(arg) == 'keyword')) else ((get(state, 'i') + 1) < len(args)))) else _sx_begin(_sx_append(children, arg), assoc(state, 'i', (get(state, 'i') + 1))))))(get(state, 'skip')), {'i': 0, 'skip': False}, args)
|
|
local = env_merge(component_closure(comp), env)
|
|
for p in component_params(comp):
|
|
local[p] = (dict_get(kwargs, p) if sx_truthy(dict_has(kwargs, p)) else NIL)
|
|
if sx_truthy(component_has_children(comp)):
|
|
local['children'] = make_raw_html(join('', map(lambda c: render_to_html(c, env), children)))
|
|
return render_to_html(component_body(comp), local)
|
|
|
|
# render-html-element
|
|
def render_html_element(tag, args, env):
|
|
parsed = parse_element_args(args, env)
|
|
attrs = first(parsed)
|
|
children = nth(parsed, 1)
|
|
is_void = contains_p(VOID_ELEMENTS, tag)
|
|
return sx_str('<', tag, render_attrs(attrs), (' />' if sx_truthy(is_void) else sx_str('>', join('', map(lambda c: render_to_html(c, env), children)), '</', tag, '>')))
|
|
|
|
# render-html-lake
|
|
def render_html_lake(args, env):
|
|
_cells = {}
|
|
_cells['lake_id'] = NIL
|
|
_cells['lake_tag'] = 'div'
|
|
children = []
|
|
reduce(lambda state, arg: (lambda skip: (assoc(state, 'skip', False, 'i', (get(state, 'i') + 1)) if sx_truthy(skip) else ((lambda kname: (lambda kval: _sx_begin((_sx_cell_set(_cells, 'lake_id', kval) if sx_truthy((kname == 'id')) else (_sx_cell_set(_cells, 'lake_tag', kval) if sx_truthy((kname == 'tag')) else NIL)), assoc(state, 'skip', True, 'i', (get(state, 'i') + 1))))(trampoline(eval_expr(nth(args, (get(state, 'i') + 1)), env))))(keyword_name(arg)) if sx_truthy(((type_of(arg) == 'keyword') if not sx_truthy((type_of(arg) == 'keyword')) else ((get(state, 'i') + 1) < len(args)))) else _sx_begin(_sx_append(children, arg), assoc(state, 'i', (get(state, 'i') + 1))))))(get(state, 'skip')), {'i': 0, 'skip': False}, args)
|
|
return sx_str('<', _cells['lake_tag'], ' data-sx-lake="', escape_attr((_cells['lake_id'] if sx_truthy(_cells['lake_id']) else '')), '">', join('', map(lambda c: render_to_html(c, env), children)), '</', _cells['lake_tag'], '>')
|
|
|
|
# render-html-marsh
|
|
def render_html_marsh(args, env):
|
|
_cells = {}
|
|
_cells['marsh_id'] = NIL
|
|
_cells['marsh_tag'] = 'div'
|
|
children = []
|
|
reduce(lambda state, arg: (lambda skip: (assoc(state, 'skip', False, 'i', (get(state, 'i') + 1)) if sx_truthy(skip) else ((lambda kname: (lambda kval: _sx_begin((_sx_cell_set(_cells, 'marsh_id', kval) if sx_truthy((kname == 'id')) else (_sx_cell_set(_cells, 'marsh_tag', kval) if sx_truthy((kname == 'tag')) else (NIL if sx_truthy((kname == 'transform')) else NIL))), assoc(state, 'skip', True, 'i', (get(state, 'i') + 1))))(trampoline(eval_expr(nth(args, (get(state, 'i') + 1)), env))))(keyword_name(arg)) if sx_truthy(((type_of(arg) == 'keyword') if not sx_truthy((type_of(arg) == 'keyword')) else ((get(state, 'i') + 1) < len(args)))) else _sx_begin(_sx_append(children, arg), assoc(state, 'i', (get(state, 'i') + 1))))))(get(state, 'skip')), {'i': 0, 'skip': False}, args)
|
|
return sx_str('<', _cells['marsh_tag'], ' data-sx-marsh="', escape_attr((_cells['marsh_id'] if sx_truthy(_cells['marsh_id']) else '')), '">', join('', map(lambda c: render_to_html(c, env), children)), '</', _cells['marsh_tag'], '>')
|
|
|
|
# render-html-island
|
|
def render_html_island(island, args, env):
|
|
kwargs = {}
|
|
children = []
|
|
reduce(lambda state, arg: (lambda skip: (assoc(state, 'skip', False, 'i', (get(state, 'i') + 1)) if sx_truthy(skip) else ((lambda val: _sx_begin(_sx_dict_set(kwargs, keyword_name(arg), val), assoc(state, 'skip', True, 'i', (get(state, 'i') + 1))))(trampoline(eval_expr(nth(args, (get(state, 'i') + 1)), env))) if sx_truthy(((type_of(arg) == 'keyword') if not sx_truthy((type_of(arg) == 'keyword')) else ((get(state, 'i') + 1) < len(args)))) else _sx_begin(_sx_append(children, arg), assoc(state, 'i', (get(state, 'i') + 1))))))(get(state, 'skip')), {'i': 0, 'skip': False}, args)
|
|
local = env_merge(component_closure(island), env)
|
|
island_name = component_name(island)
|
|
for p in component_params(island):
|
|
local[p] = (dict_get(kwargs, p) if sx_truthy(dict_has(kwargs, p)) else NIL)
|
|
if sx_truthy(component_has_children(island)):
|
|
local['children'] = make_raw_html(join('', map(lambda c: render_to_html(c, env), children)))
|
|
body_html = render_to_html(component_body(island), local)
|
|
state_json = serialize_island_state(kwargs)
|
|
return sx_str('<span data-sx-island="', escape_attr(island_name), '"', (sx_str(' data-sx-state="', escape_attr(state_json), '"') if sx_truthy(state_json) else ''), '>', body_html, '</span>')
|
|
|
|
# serialize-island-state
|
|
def serialize_island_state(kwargs):
|
|
if sx_truthy(is_empty_dict(kwargs)):
|
|
return NIL
|
|
else:
|
|
return json_serialize(kwargs)
|
|
|
|
|
|
# === Transpiled from adapter-sx ===
|
|
|
|
# render-to-sx
|
|
def render_to_sx(expr, env):
|
|
result = aser(expr, env)
|
|
if sx_truthy((type_of(result) == 'string')):
|
|
return result
|
|
else:
|
|
return serialize(result)
|
|
|
|
# aser
|
|
def aser(expr, env):
|
|
set_render_active_b(True)
|
|
_match = type_of(expr)
|
|
if _match == 'number':
|
|
return expr
|
|
elif _match == 'string':
|
|
return expr
|
|
elif _match == 'boolean':
|
|
return expr
|
|
elif _match == 'nil':
|
|
return NIL
|
|
elif _match == 'symbol':
|
|
name = symbol_name(expr)
|
|
if sx_truthy(env_has(env, name)):
|
|
return env_get(env, name)
|
|
elif sx_truthy(is_primitive(name)):
|
|
return get_primitive(name)
|
|
elif sx_truthy((name == 'true')):
|
|
return True
|
|
elif sx_truthy((name == 'false')):
|
|
return False
|
|
elif sx_truthy((name == 'nil')):
|
|
return NIL
|
|
else:
|
|
return error(sx_str('Undefined symbol: ', name))
|
|
elif _match == 'keyword':
|
|
return keyword_name(expr)
|
|
elif _match == 'list':
|
|
if sx_truthy(empty_p(expr)):
|
|
return []
|
|
else:
|
|
return aser_list(expr, env)
|
|
else:
|
|
return expr
|
|
|
|
# aser-list
|
|
def aser_list(expr, env):
|
|
head = first(expr)
|
|
args = rest(expr)
|
|
if sx_truthy((not sx_truthy((type_of(head) == 'symbol')))):
|
|
return map(lambda x: aser(x, env), expr)
|
|
else:
|
|
name = symbol_name(head)
|
|
if sx_truthy((name == '<>')):
|
|
return aser_fragment(args, env)
|
|
elif sx_truthy(starts_with_p(name, '~')):
|
|
return aser_call(name, args, env)
|
|
elif sx_truthy((name == 'lake')):
|
|
return aser_call(name, args, env)
|
|
elif sx_truthy((name == 'marsh')):
|
|
return aser_call(name, args, env)
|
|
elif sx_truthy(contains_p(HTML_TAGS, name)):
|
|
return aser_call(name, args, env)
|
|
elif sx_truthy((is_special_form(name) if sx_truthy(is_special_form(name)) else is_ho_form(name))):
|
|
return aser_special(name, expr, env)
|
|
elif sx_truthy((env_has(env, name) if not sx_truthy(env_has(env, name)) else is_macro(env_get(env, name)))):
|
|
return aser(expand_macro(env_get(env, name), args, env), env)
|
|
else:
|
|
f = trampoline(eval_expr(head, env))
|
|
evaled_args = map(lambda a: trampoline(eval_expr(a, env)), args)
|
|
if sx_truthy((is_callable(f) if not sx_truthy(is_callable(f)) else ((not sx_truthy(is_lambda(f))) if not sx_truthy((not sx_truthy(is_lambda(f)))) else ((not sx_truthy(is_component(f))) if not sx_truthy((not sx_truthy(is_component(f)))) else (not sx_truthy(is_island(f))))))):
|
|
return apply(f, evaled_args)
|
|
elif sx_truthy(is_lambda(f)):
|
|
return trampoline(call_lambda(f, evaled_args, env))
|
|
elif sx_truthy(is_component(f)):
|
|
return aser_call(sx_str('~', component_name(f)), args, env)
|
|
elif sx_truthy(is_island(f)):
|
|
return aser_call(sx_str('~', component_name(f)), args, env)
|
|
else:
|
|
return error(sx_str('Not callable: ', inspect(f)))
|
|
|
|
# aser-fragment
|
|
def aser_fragment(children, env):
|
|
parts = filter(lambda x: (not sx_truthy(is_nil(x))), map(lambda c: aser(c, env), children))
|
|
if sx_truthy(empty_p(parts)):
|
|
return ''
|
|
else:
|
|
return sx_str('(<> ', join(' ', map(serialize, parts)), ')')
|
|
|
|
# aser-call
|
|
def aser_call(name, args, env):
|
|
parts = [name]
|
|
reduce(lambda state, arg: (lambda skip: (assoc(state, 'skip', False, 'i', (get(state, 'i') + 1)) if sx_truthy(skip) else ((lambda val: _sx_begin((_sx_begin(_sx_append(parts, sx_str(':', keyword_name(arg))), _sx_append(parts, serialize(val))) if sx_truthy((not sx_truthy(is_nil(val)))) else NIL), assoc(state, 'skip', True, 'i', (get(state, 'i') + 1))))(aser(nth(args, (get(state, 'i') + 1)), env)) if sx_truthy(((type_of(arg) == 'keyword') if not sx_truthy((type_of(arg) == 'keyword')) else ((get(state, 'i') + 1) < len(args)))) else (lambda val: _sx_begin((_sx_append(parts, serialize(val)) if sx_truthy((not sx_truthy(is_nil(val)))) else NIL), assoc(state, 'i', (get(state, 'i') + 1))))(aser(arg, env)))))(get(state, 'skip')), {'i': 0, 'skip': False}, args)
|
|
return sx_str('(', join(' ', parts), ')')
|
|
|
|
# SPECIAL_FORM_NAMES
|
|
SPECIAL_FORM_NAMES = ['if', 'when', 'cond', 'case', 'and', 'or', 'let', 'let*', 'lambda', 'fn', 'define', 'defcomp', 'defmacro', 'defstyle', 'defhandler', 'defpage', 'defquery', 'defaction', 'defrelation', 'begin', 'do', 'quote', 'quasiquote', '->', 'set!', 'letrec', 'dynamic-wind', 'defisland']
|
|
|
|
# HO_FORM_NAMES
|
|
HO_FORM_NAMES = ['map', 'map-indexed', 'filter', 'reduce', 'some', 'every?', 'for-each']
|
|
|
|
# special-form?
|
|
def is_special_form(name):
|
|
return contains_p(SPECIAL_FORM_NAMES, name)
|
|
|
|
# ho-form?
|
|
def is_ho_form(name):
|
|
return contains_p(HO_FORM_NAMES, name)
|
|
|
|
# aser-special
|
|
def aser_special(name, expr, env):
|
|
_cells = {}
|
|
args = rest(expr)
|
|
if sx_truthy((name == 'if')):
|
|
if sx_truthy(trampoline(eval_expr(first(args), env))):
|
|
return aser(nth(args, 1), env)
|
|
else:
|
|
if sx_truthy((len(args) > 2)):
|
|
return aser(nth(args, 2), env)
|
|
else:
|
|
return NIL
|
|
elif sx_truthy((name == 'when')):
|
|
if sx_truthy((not sx_truthy(trampoline(eval_expr(first(args), env))))):
|
|
return NIL
|
|
else:
|
|
_cells['result'] = NIL
|
|
for body in rest(args):
|
|
_cells['result'] = aser(body, env)
|
|
return _cells['result']
|
|
elif sx_truthy((name == 'cond')):
|
|
branch = eval_cond(args, env)
|
|
if sx_truthy(branch):
|
|
return aser(branch, env)
|
|
else:
|
|
return NIL
|
|
elif sx_truthy((name == 'case')):
|
|
match_val = trampoline(eval_expr(first(args), env))
|
|
clauses = rest(args)
|
|
return eval_case_aser(match_val, clauses, env)
|
|
elif sx_truthy(((name == 'let') if sx_truthy((name == 'let')) else (name == 'let*'))):
|
|
local = process_bindings(first(args), env)
|
|
_cells['result'] = NIL
|
|
for body in rest(args):
|
|
_cells['result'] = aser(body, local)
|
|
return _cells['result']
|
|
elif sx_truthy(((name == 'begin') if sx_truthy((name == 'begin')) else (name == 'do'))):
|
|
_cells['result'] = NIL
|
|
for body in args:
|
|
_cells['result'] = aser(body, env)
|
|
return _cells['result']
|
|
elif sx_truthy((name == 'and')):
|
|
_cells['result'] = True
|
|
some(_sx_fn(lambda arg: (
|
|
_sx_cell_set(_cells, 'result', trampoline(eval_expr(arg, env))),
|
|
(not sx_truthy(_cells['result']))
|
|
)[-1]), args)
|
|
return _cells['result']
|
|
elif sx_truthy((name == 'or')):
|
|
_cells['result'] = False
|
|
some(_sx_fn(lambda arg: (
|
|
_sx_cell_set(_cells, 'result', trampoline(eval_expr(arg, env))),
|
|
_cells['result']
|
|
)[-1]), args)
|
|
return _cells['result']
|
|
elif sx_truthy((name == 'map')):
|
|
f = trampoline(eval_expr(first(args), env))
|
|
coll = trampoline(eval_expr(nth(args, 1), env))
|
|
return map(lambda item: ((lambda local: _sx_begin(_sx_dict_set(local, first(lambda_params(f)), item), aser(lambda_body(f), local)))(env_merge(lambda_closure(f), env)) if sx_truthy(is_lambda(f)) else invoke(f, item)), coll)
|
|
elif sx_truthy((name == 'map-indexed')):
|
|
f = trampoline(eval_expr(first(args), env))
|
|
coll = trampoline(eval_expr(nth(args, 1), env))
|
|
return map_indexed(lambda i, item: ((lambda local: _sx_begin(_sx_dict_set(local, first(lambda_params(f)), i), _sx_dict_set(local, nth(lambda_params(f), 1), item), aser(lambda_body(f), local)))(env_merge(lambda_closure(f), env)) if sx_truthy(is_lambda(f)) else invoke(f, i, item)), coll)
|
|
elif sx_truthy((name == 'for-each')):
|
|
f = trampoline(eval_expr(first(args), env))
|
|
coll = trampoline(eval_expr(nth(args, 1), env))
|
|
results = []
|
|
for item in coll:
|
|
if sx_truthy(is_lambda(f)):
|
|
local = env_merge(lambda_closure(f), env)
|
|
local[first(lambda_params(f))] = item
|
|
results.append(aser(lambda_body(f), local))
|
|
else:
|
|
invoke(f, item)
|
|
if sx_truthy(empty_p(results)):
|
|
return NIL
|
|
else:
|
|
return results
|
|
elif sx_truthy((name == 'defisland')):
|
|
trampoline(eval_expr(expr, env))
|
|
return serialize(expr)
|
|
elif sx_truthy(((name == 'define') if sx_truthy((name == 'define')) else ((name == 'defcomp') if sx_truthy((name == 'defcomp')) else ((name == 'defmacro') if sx_truthy((name == 'defmacro')) else ((name == 'defstyle') if sx_truthy((name == 'defstyle')) else ((name == 'defhandler') if sx_truthy((name == 'defhandler')) else ((name == 'defpage') if sx_truthy((name == 'defpage')) else ((name == 'defquery') if sx_truthy((name == 'defquery')) else ((name == 'defaction') if sx_truthy((name == 'defaction')) else (name == 'defrelation')))))))))):
|
|
trampoline(eval_expr(expr, env))
|
|
return NIL
|
|
else:
|
|
return trampoline(eval_expr(expr, env))
|
|
|
|
# eval-case-aser
|
|
def eval_case_aser(match_val, clauses, env):
|
|
if sx_truthy((len(clauses) < 2)):
|
|
return NIL
|
|
else:
|
|
test = first(clauses)
|
|
body = nth(clauses, 1)
|
|
if sx_truthy((((type_of(test) == 'keyword') if not sx_truthy((type_of(test) == 'keyword')) else (keyword_name(test) == 'else')) if sx_truthy(((type_of(test) == 'keyword') if not sx_truthy((type_of(test) == 'keyword')) else (keyword_name(test) == 'else'))) else ((type_of(test) == 'symbol') if not sx_truthy((type_of(test) == 'symbol')) else ((symbol_name(test) == ':else') if sx_truthy((symbol_name(test) == ':else')) else (symbol_name(test) == 'else'))))):
|
|
return aser(body, env)
|
|
else:
|
|
if sx_truthy((match_val == trampoline(eval_expr(test, env)))):
|
|
return aser(body, env)
|
|
else:
|
|
return eval_case_aser(match_val, slice(clauses, 2), env)
|
|
|
|
|
|
# === Transpiled from deps (component dependency analysis) ===
|
|
|
|
# scan-refs
|
|
def scan_refs(node):
|
|
refs = []
|
|
scan_refs_walk(node, refs)
|
|
return refs
|
|
|
|
# scan-refs-walk
|
|
def scan_refs_walk(node, refs):
|
|
if sx_truthy((type_of(node) == 'symbol')):
|
|
name = symbol_name(node)
|
|
if sx_truthy(starts_with_p(name, '~')):
|
|
if sx_truthy((not sx_truthy(contains_p(refs, name)))):
|
|
return _sx_append(refs, name)
|
|
return NIL
|
|
return NIL
|
|
elif sx_truthy((type_of(node) == 'list')):
|
|
return for_each(lambda item: scan_refs_walk(item, refs), node)
|
|
elif sx_truthy((type_of(node) == 'dict')):
|
|
return for_each(lambda key: scan_refs_walk(dict_get(node, key), refs), keys(node))
|
|
else:
|
|
return NIL
|
|
|
|
# transitive-deps-walk
|
|
def transitive_deps_walk(n, seen, env):
|
|
if sx_truthy((not sx_truthy(contains_p(seen, n)))):
|
|
seen.append(n)
|
|
val = env_get(env, n)
|
|
if sx_truthy((type_of(val) == 'component')):
|
|
return for_each(lambda ref: transitive_deps_walk(ref, seen, env), scan_refs(component_body(val)))
|
|
elif sx_truthy((type_of(val) == 'macro')):
|
|
return for_each(lambda ref: transitive_deps_walk(ref, seen, env), scan_refs(macro_body(val)))
|
|
else:
|
|
return NIL
|
|
return NIL
|
|
|
|
# transitive-deps
|
|
def transitive_deps(name, env):
|
|
seen = []
|
|
key = (name if sx_truthy(starts_with_p(name, '~')) else sx_str('~', name))
|
|
transitive_deps_walk(key, seen, env)
|
|
return filter(lambda x: (not sx_truthy((x == key))), seen)
|
|
|
|
# compute-all-deps
|
|
def compute_all_deps(env):
|
|
return for_each(lambda name: (lambda val: (component_set_deps(val, transitive_deps(name, env)) if sx_truthy((type_of(val) == 'component')) else NIL))(env_get(env, name)), env_components(env))
|
|
|
|
# scan-components-from-source
|
|
def scan_components_from_source(source):
|
|
matches = regex_find_all('\\(~([a-zA-Z_][a-zA-Z0-9_\\-]*)', source)
|
|
return map(lambda m: sx_str('~', m), matches)
|
|
|
|
# components-needed
|
|
def components_needed(page_source, env):
|
|
direct = scan_components_from_source(page_source)
|
|
all_needed = []
|
|
for name in direct:
|
|
if sx_truthy((not sx_truthy(contains_p(all_needed, name)))):
|
|
all_needed.append(name)
|
|
val = env_get(env, name)
|
|
deps = (component_deps(val) if sx_truthy(((type_of(val) == 'component') if not sx_truthy((type_of(val) == 'component')) else (not sx_truthy(empty_p(component_deps(val)))))) else transitive_deps(name, env))
|
|
for dep in deps:
|
|
if sx_truthy((not sx_truthy(contains_p(all_needed, dep)))):
|
|
all_needed.append(dep)
|
|
return all_needed
|
|
|
|
# page-component-bundle
|
|
def page_component_bundle(page_source, env):
|
|
return components_needed(page_source, env)
|
|
|
|
# page-css-classes
|
|
def page_css_classes(page_source, env):
|
|
needed = components_needed(page_source, env)
|
|
classes = []
|
|
for name in needed:
|
|
val = env_get(env, name)
|
|
if sx_truthy((type_of(val) == 'component')):
|
|
for cls in component_css_classes(val):
|
|
if sx_truthy((not sx_truthy(contains_p(classes, cls)))):
|
|
classes.append(cls)
|
|
for cls in scan_css_classes(page_source):
|
|
if sx_truthy((not sx_truthy(contains_p(classes, cls)))):
|
|
classes.append(cls)
|
|
return classes
|
|
|
|
# scan-io-refs-walk
|
|
def scan_io_refs_walk(node, io_names, refs):
|
|
if sx_truthy((type_of(node) == 'symbol')):
|
|
name = symbol_name(node)
|
|
if sx_truthy(contains_p(io_names, name)):
|
|
if sx_truthy((not sx_truthy(contains_p(refs, name)))):
|
|
return _sx_append(refs, name)
|
|
return NIL
|
|
return NIL
|
|
elif sx_truthy((type_of(node) == 'list')):
|
|
return for_each(lambda item: scan_io_refs_walk(item, io_names, refs), node)
|
|
elif sx_truthy((type_of(node) == 'dict')):
|
|
return for_each(lambda key: scan_io_refs_walk(dict_get(node, key), io_names, refs), keys(node))
|
|
else:
|
|
return NIL
|
|
|
|
# scan-io-refs
|
|
def scan_io_refs(node, io_names):
|
|
refs = []
|
|
scan_io_refs_walk(node, io_names, refs)
|
|
return refs
|
|
|
|
# transitive-io-refs-walk
|
|
def transitive_io_refs_walk(n, seen, all_refs, env, io_names):
|
|
if sx_truthy((not sx_truthy(contains_p(seen, n)))):
|
|
seen.append(n)
|
|
val = env_get(env, n)
|
|
if sx_truthy((type_of(val) == 'component')):
|
|
for ref in scan_io_refs(component_body(val), io_names):
|
|
if sx_truthy((not sx_truthy(contains_p(all_refs, ref)))):
|
|
all_refs.append(ref)
|
|
return for_each(lambda dep: transitive_io_refs_walk(dep, seen, all_refs, env, io_names), scan_refs(component_body(val)))
|
|
elif sx_truthy((type_of(val) == 'macro')):
|
|
for ref in scan_io_refs(macro_body(val), io_names):
|
|
if sx_truthy((not sx_truthy(contains_p(all_refs, ref)))):
|
|
all_refs.append(ref)
|
|
return for_each(lambda dep: transitive_io_refs_walk(dep, seen, all_refs, env, io_names), scan_refs(macro_body(val)))
|
|
else:
|
|
return NIL
|
|
return NIL
|
|
|
|
# transitive-io-refs
|
|
def transitive_io_refs(name, env, io_names):
|
|
all_refs = []
|
|
seen = []
|
|
key = (name if sx_truthy(starts_with_p(name, '~')) else sx_str('~', name))
|
|
transitive_io_refs_walk(key, seen, all_refs, env, io_names)
|
|
return all_refs
|
|
|
|
# compute-all-io-refs
|
|
def compute_all_io_refs(env, io_names):
|
|
return for_each(lambda name: (lambda val: (component_set_io_refs(val, transitive_io_refs(name, env, io_names)) if sx_truthy((type_of(val) == 'component')) else NIL))(env_get(env, name)), env_components(env))
|
|
|
|
# component-io-refs-cached
|
|
def component_io_refs_cached(name, env, io_names):
|
|
key = (name if sx_truthy(starts_with_p(name, '~')) else sx_str('~', name))
|
|
val = env_get(env, key)
|
|
if sx_truthy(((type_of(val) == 'component') if not sx_truthy((type_of(val) == 'component')) else ((not sx_truthy(is_nil(component_io_refs(val)))) if not sx_truthy((not sx_truthy(is_nil(component_io_refs(val))))) else (not sx_truthy(empty_p(component_io_refs(val))))))):
|
|
return component_io_refs(val)
|
|
else:
|
|
return transitive_io_refs(name, env, io_names)
|
|
|
|
# component-pure?
|
|
def component_pure_p(name, env, io_names):
|
|
key = (name if sx_truthy(starts_with_p(name, '~')) else sx_str('~', name))
|
|
val = env_get(env, key)
|
|
if sx_truthy(((type_of(val) == 'component') if not sx_truthy((type_of(val) == 'component')) else (not sx_truthy(is_nil(component_io_refs(val)))))):
|
|
return empty_p(component_io_refs(val))
|
|
else:
|
|
return empty_p(transitive_io_refs(name, env, io_names))
|
|
|
|
# render-target
|
|
def render_target(name, env, io_names):
|
|
key = (name if sx_truthy(starts_with_p(name, '~')) else sx_str('~', name))
|
|
val = env_get(env, key)
|
|
if sx_truthy((not sx_truthy((type_of(val) == 'component')))):
|
|
return 'server'
|
|
else:
|
|
affinity = component_affinity(val)
|
|
if sx_truthy((affinity == 'server')):
|
|
return 'server'
|
|
elif sx_truthy((affinity == 'client')):
|
|
return 'client'
|
|
elif sx_truthy((not sx_truthy(component_pure_p(name, env, io_names)))):
|
|
return 'server'
|
|
else:
|
|
return 'client'
|
|
|
|
# page-render-plan
|
|
def page_render_plan(page_source, env, io_names):
|
|
needed = components_needed(page_source, env)
|
|
comp_targets = {}
|
|
server_list = []
|
|
client_list = []
|
|
io_deps = []
|
|
for name in needed:
|
|
target = render_target(name, env, io_names)
|
|
comp_targets[name] = target
|
|
if sx_truthy((target == 'server')):
|
|
server_list.append(name)
|
|
for io_ref in component_io_refs_cached(name, env, io_names):
|
|
if sx_truthy((not sx_truthy(contains_p(io_deps, io_ref)))):
|
|
io_deps.append(io_ref)
|
|
else:
|
|
client_list.append(name)
|
|
return {'components': comp_targets, 'server': server_list, 'client': client_list, 'io-deps': io_deps}
|
|
|
|
# env-components
|
|
def env_components(env):
|
|
return filter(lambda k: (lambda v: (is_component(v) if sx_truthy(is_component(v)) else is_macro(v)))(env_get(env, k)), keys(env))
|
|
|
|
|
|
# === Transpiled from engine (fetch/swap/trigger pure logic) ===
|
|
|
|
# ENGINE_VERBS
|
|
ENGINE_VERBS = ['get', 'post', 'put', 'delete', 'patch']
|
|
|
|
# DEFAULT_SWAP
|
|
DEFAULT_SWAP = 'outerHTML'
|
|
|
|
# parse-time
|
|
def parse_time(s):
|
|
if sx_truthy(is_nil(s)):
|
|
return 0
|
|
elif sx_truthy(ends_with_p(s, 'ms')):
|
|
return parse_int(s, 0)
|
|
elif sx_truthy(ends_with_p(s, 's')):
|
|
return (parse_int(replace(s, 's', ''), 0) * 1000)
|
|
else:
|
|
return parse_int(s, 0)
|
|
|
|
# parse-trigger-spec
|
|
def parse_trigger_spec(spec):
|
|
if sx_truthy(is_nil(spec)):
|
|
return NIL
|
|
else:
|
|
raw_parts = split(spec, ',')
|
|
return filter(lambda x: (not sx_truthy(is_nil(x))), map(lambda part: (lambda tokens: (NIL if sx_truthy(empty_p(tokens)) else ({'event': 'every', 'modifiers': {'interval': parse_time(nth(tokens, 1))}} if sx_truthy(((first(tokens) == 'every') if not sx_truthy((first(tokens) == 'every')) else (len(tokens) >= 2))) else (lambda mods: _sx_begin(for_each(lambda tok: (_sx_dict_set(mods, 'once', True) if sx_truthy((tok == 'once')) else (_sx_dict_set(mods, 'changed', True) if sx_truthy((tok == 'changed')) else (_sx_dict_set(mods, 'delay', parse_time(slice(tok, 6))) if sx_truthy(starts_with_p(tok, 'delay:')) else (_sx_dict_set(mods, 'from', slice(tok, 5)) if sx_truthy(starts_with_p(tok, 'from:')) else NIL)))), rest(tokens)), {'event': first(tokens), 'modifiers': mods}))({}))))(split(trim(part), ' ')), raw_parts))
|
|
|
|
# default-trigger
|
|
def default_trigger(tag_name):
|
|
if sx_truthy((tag_name == 'FORM')):
|
|
return [{'event': 'submit', 'modifiers': {}}]
|
|
elif sx_truthy(((tag_name == 'INPUT') if sx_truthy((tag_name == 'INPUT')) else ((tag_name == 'SELECT') if sx_truthy((tag_name == 'SELECT')) else (tag_name == 'TEXTAREA')))):
|
|
return [{'event': 'change', 'modifiers': {}}]
|
|
else:
|
|
return [{'event': 'click', 'modifiers': {}}]
|
|
|
|
# get-verb-info
|
|
def get_verb_info(el):
|
|
return some(lambda verb: (lambda url: ({'method': upper(verb), 'url': url} if sx_truthy(url) else NIL))(dom_get_attr(el, sx_str('sx-', verb))), ENGINE_VERBS)
|
|
|
|
# build-request-headers
|
|
def build_request_headers(el, loaded_components, css_hash):
|
|
headers = {'SX-Request': 'true', 'SX-Current-URL': browser_location_href()}
|
|
target_sel = dom_get_attr(el, 'sx-target')
|
|
if sx_truthy(target_sel):
|
|
headers['SX-Target'] = target_sel
|
|
if sx_truthy((not sx_truthy(empty_p(loaded_components)))):
|
|
headers['SX-Components'] = join(',', loaded_components)
|
|
if sx_truthy(css_hash):
|
|
headers['SX-Css'] = css_hash
|
|
extra_h = dom_get_attr(el, 'sx-headers')
|
|
if sx_truthy(extra_h):
|
|
parsed = parse_header_value(extra_h)
|
|
if sx_truthy(parsed):
|
|
for key in keys(parsed):
|
|
headers[key] = sx_str(get(parsed, key))
|
|
return headers
|
|
|
|
# process-response-headers
|
|
def process_response_headers(get_header):
|
|
return {'redirect': get_header('SX-Redirect'), 'refresh': get_header('SX-Refresh'), 'trigger': get_header('SX-Trigger'), 'retarget': get_header('SX-Retarget'), 'reswap': get_header('SX-Reswap'), 'location': get_header('SX-Location'), 'replace-url': get_header('SX-Replace-Url'), 'css-hash': get_header('SX-Css-Hash'), 'trigger-swap': get_header('SX-Trigger-After-Swap'), 'trigger-settle': get_header('SX-Trigger-After-Settle'), 'content-type': get_header('Content-Type'), 'cache-invalidate': get_header('SX-Cache-Invalidate'), 'cache-update': get_header('SX-Cache-Update')}
|
|
|
|
# parse-swap-spec
|
|
def parse_swap_spec(raw_swap, global_transitions_p):
|
|
_cells = {}
|
|
parts = split((raw_swap if sx_truthy(raw_swap) else DEFAULT_SWAP), ' ')
|
|
style = first(parts)
|
|
_cells['use_transition'] = global_transitions_p
|
|
for p in rest(parts):
|
|
if sx_truthy((p == 'transition:true')):
|
|
_cells['use_transition'] = True
|
|
elif sx_truthy((p == 'transition:false')):
|
|
_cells['use_transition'] = False
|
|
return {'style': style, 'transition': _cells['use_transition']}
|
|
|
|
# parse-retry-spec
|
|
def parse_retry_spec(retry_attr):
|
|
if sx_truthy(is_nil(retry_attr)):
|
|
return NIL
|
|
else:
|
|
parts = split(retry_attr, ':')
|
|
return {'strategy': first(parts), 'start-ms': parse_int(nth(parts, 1), 1000), 'cap-ms': parse_int(nth(parts, 2), 30000)}
|
|
|
|
# next-retry-ms
|
|
def next_retry_ms(current_ms, cap_ms):
|
|
return min((current_ms * 2), cap_ms)
|
|
|
|
# filter-params
|
|
def filter_params(params_spec, all_params):
|
|
if sx_truthy(is_nil(params_spec)):
|
|
return all_params
|
|
elif sx_truthy((params_spec == 'none')):
|
|
return []
|
|
elif sx_truthy((params_spec == '*')):
|
|
return all_params
|
|
elif sx_truthy(starts_with_p(params_spec, 'not ')):
|
|
excluded = map(trim, split(slice(params_spec, 4), ','))
|
|
return filter(lambda p: (not sx_truthy(contains_p(excluded, first(p)))), all_params)
|
|
else:
|
|
allowed = map(trim, split(params_spec, ','))
|
|
return filter(lambda p: contains_p(allowed, first(p)), all_params)
|
|
|
|
# resolve-target
|
|
def resolve_target(el):
|
|
sel = dom_get_attr(el, 'sx-target')
|
|
if sx_truthy((is_nil(sel) if sx_truthy(is_nil(sel)) else (sel == 'this'))):
|
|
return el
|
|
elif sx_truthy((sel == 'closest')):
|
|
return dom_parent(el)
|
|
else:
|
|
return dom_query(sel)
|
|
|
|
# apply-optimistic
|
|
def apply_optimistic(el):
|
|
directive = dom_get_attr(el, 'sx-optimistic')
|
|
if sx_truthy(is_nil(directive)):
|
|
return NIL
|
|
else:
|
|
target = (resolve_target(el) if sx_truthy(resolve_target(el)) else el)
|
|
state = {'target': target, 'directive': directive}
|
|
(_sx_begin(_sx_dict_set(state, 'opacity', dom_get_style(target, 'opacity')), dom_set_style(target, 'opacity', '0'), dom_set_style(target, 'pointer-events', 'none')) if sx_truthy((directive == 'remove')) else (_sx_begin(_sx_dict_set(state, 'disabled', dom_get_prop(target, 'disabled')), dom_set_prop(target, 'disabled', True)) if sx_truthy((directive == 'disable')) else ((lambda cls: _sx_begin(_sx_dict_set(state, 'add-class', cls), dom_add_class(target, cls)))(slice(directive, 10)) if sx_truthy(starts_with_p(directive, 'add-class:')) else NIL)))
|
|
return state
|
|
|
|
# revert-optimistic
|
|
def revert_optimistic(state):
|
|
if sx_truthy(state):
|
|
target = get(state, 'target')
|
|
directive = get(state, 'directive')
|
|
if sx_truthy((directive == 'remove')):
|
|
dom_set_style(target, 'opacity', (get(state, 'opacity') if sx_truthy(get(state, 'opacity')) else ''))
|
|
return dom_set_style(target, 'pointer-events', '')
|
|
elif sx_truthy((directive == 'disable')):
|
|
return dom_set_prop(target, 'disabled', (get(state, 'disabled') if sx_truthy(get(state, 'disabled')) else False))
|
|
elif sx_truthy(get(state, 'add-class')):
|
|
return dom_remove_class(target, get(state, 'add-class'))
|
|
return NIL
|
|
return NIL
|
|
|
|
# find-oob-swaps
|
|
def find_oob_swaps(container):
|
|
results = []
|
|
for attr in ['sx-swap-oob', 'hx-swap-oob']:
|
|
oob_els = dom_query_all(container, sx_str('[', attr, ']'))
|
|
for oob in oob_els:
|
|
swap_type = (dom_get_attr(oob, attr) if sx_truthy(dom_get_attr(oob, attr)) else 'outerHTML')
|
|
target_id = dom_id(oob)
|
|
dom_remove_attr(oob, attr)
|
|
if sx_truthy(target_id):
|
|
results.append({'element': oob, 'swap-type': swap_type, 'target-id': target_id})
|
|
return results
|
|
|
|
# morph-node
|
|
def morph_node(old_node, new_node):
|
|
if sx_truthy((dom_has_attr_p(old_node, 'sx-preserve') if sx_truthy(dom_has_attr_p(old_node, 'sx-preserve')) else dom_has_attr_p(old_node, 'sx-ignore'))):
|
|
return NIL
|
|
elif sx_truthy((dom_has_attr_p(old_node, 'data-sx-island') if not sx_truthy(dom_has_attr_p(old_node, 'data-sx-island')) else (is_processed_p(old_node, 'island-hydrated') if not sx_truthy(is_processed_p(old_node, 'island-hydrated')) else (dom_has_attr_p(new_node, 'data-sx-island') if not sx_truthy(dom_has_attr_p(new_node, 'data-sx-island')) else (dom_get_attr(old_node, 'data-sx-island') == dom_get_attr(new_node, 'data-sx-island')))))):
|
|
return morph_island_children(old_node, new_node)
|
|
elif sx_truthy(((not sx_truthy((dom_node_type(old_node) == dom_node_type(new_node)))) if sx_truthy((not sx_truthy((dom_node_type(old_node) == dom_node_type(new_node))))) else (not sx_truthy((dom_node_name(old_node) == dom_node_name(new_node)))))):
|
|
return dom_replace_child(dom_parent(old_node), dom_clone(new_node), old_node)
|
|
elif sx_truthy(((dom_node_type(old_node) == 3) if sx_truthy((dom_node_type(old_node) == 3)) else (dom_node_type(old_node) == 8))):
|
|
if sx_truthy((not sx_truthy((dom_text_content(old_node) == dom_text_content(new_node))))):
|
|
return dom_set_text_content(old_node, dom_text_content(new_node))
|
|
return NIL
|
|
elif sx_truthy((dom_node_type(old_node) == 1)):
|
|
sync_attrs(old_node, new_node)
|
|
if sx_truthy((not sx_truthy((dom_is_active_element_p(old_node) if not sx_truthy(dom_is_active_element_p(old_node)) else dom_is_input_element_p(old_node))))):
|
|
return morph_children(old_node, new_node)
|
|
return NIL
|
|
return NIL
|
|
|
|
# sync-attrs
|
|
def sync_attrs(old_el, new_el):
|
|
ra_str = (dom_get_attr(old_el, 'data-sx-reactive-attrs') if sx_truthy(dom_get_attr(old_el, 'data-sx-reactive-attrs')) else '')
|
|
reactive_attrs = ([] if sx_truthy(empty_p(ra_str)) else split(ra_str, ','))
|
|
for attr in dom_attr_list(new_el):
|
|
name = first(attr)
|
|
val = nth(attr, 1)
|
|
if sx_truthy(((not sx_truthy((dom_get_attr(old_el, name) == val))) if not sx_truthy((not sx_truthy((dom_get_attr(old_el, name) == val)))) else (not sx_truthy(contains_p(reactive_attrs, name))))):
|
|
dom_set_attr(old_el, name, val)
|
|
return for_each(lambda attr: (lambda aname: (dom_remove_attr(old_el, aname) if sx_truthy(((not sx_truthy(dom_has_attr_p(new_el, aname))) if not sx_truthy((not sx_truthy(dom_has_attr_p(new_el, aname)))) else ((not sx_truthy(contains_p(reactive_attrs, aname))) if not sx_truthy((not sx_truthy(contains_p(reactive_attrs, aname)))) else (not sx_truthy((aname == 'data-sx-reactive-attrs')))))) else NIL))(first(attr)), dom_attr_list(old_el))
|
|
|
|
# morph-children
|
|
def morph_children(old_parent, new_parent):
|
|
_cells = {}
|
|
old_kids = dom_child_list(old_parent)
|
|
new_kids = dom_child_list(new_parent)
|
|
old_by_id = reduce(lambda acc, kid: (lambda id_: (_sx_begin(_sx_dict_set(acc, id_, kid), acc) if sx_truthy(id_) else acc))(dom_id(kid)), {}, old_kids)
|
|
_cells['oi'] = 0
|
|
for new_child in new_kids:
|
|
match_id = dom_id(new_child)
|
|
match_by_id = (dict_get(old_by_id, match_id) if sx_truthy(match_id) else NIL)
|
|
if sx_truthy((match_by_id if not sx_truthy(match_by_id) else (not sx_truthy(is_nil(match_by_id))))):
|
|
if sx_truthy(((_cells['oi'] < len(old_kids)) if not sx_truthy((_cells['oi'] < len(old_kids))) else (not sx_truthy((match_by_id == nth(old_kids, _cells['oi'])))))):
|
|
dom_insert_before(old_parent, match_by_id, (nth(old_kids, _cells['oi']) if sx_truthy((_cells['oi'] < len(old_kids))) else NIL))
|
|
morph_node(match_by_id, new_child)
|
|
_cells['oi'] = (_cells['oi'] + 1)
|
|
elif sx_truthy((_cells['oi'] < len(old_kids))):
|
|
old_child = nth(old_kids, _cells['oi'])
|
|
if sx_truthy((dom_id(old_child) if not sx_truthy(dom_id(old_child)) else (not sx_truthy(match_id)))):
|
|
dom_insert_before(old_parent, dom_clone(new_child), old_child)
|
|
else:
|
|
morph_node(old_child, new_child)
|
|
_cells['oi'] = (_cells['oi'] + 1)
|
|
else:
|
|
dom_append(old_parent, dom_clone(new_child))
|
|
return for_each(lambda i: ((lambda leftover: (dom_remove_child(old_parent, leftover) if sx_truthy((dom_is_child_of_p(leftover, old_parent) if not sx_truthy(dom_is_child_of_p(leftover, old_parent)) else ((not sx_truthy(dom_has_attr_p(leftover, 'sx-preserve'))) if not sx_truthy((not sx_truthy(dom_has_attr_p(leftover, 'sx-preserve')))) else (not sx_truthy(dom_has_attr_p(leftover, 'sx-ignore')))))) else NIL))(nth(old_kids, i)) if sx_truthy((i >= _cells['oi'])) else NIL), range(_cells['oi'], len(old_kids)))
|
|
|
|
# morph-island-children
|
|
def morph_island_children(old_island, new_island):
|
|
old_lakes = dom_query_all(old_island, '[data-sx-lake]')
|
|
new_lakes = dom_query_all(new_island, '[data-sx-lake]')
|
|
old_marshes = dom_query_all(old_island, '[data-sx-marsh]')
|
|
new_marshes = dom_query_all(new_island, '[data-sx-marsh]')
|
|
new_lake_map = {}
|
|
new_marsh_map = {}
|
|
for lake in new_lakes:
|
|
id_ = dom_get_attr(lake, 'data-sx-lake')
|
|
if sx_truthy(id_):
|
|
new_lake_map[id_] = lake
|
|
for marsh in new_marshes:
|
|
id_ = dom_get_attr(marsh, 'data-sx-marsh')
|
|
if sx_truthy(id_):
|
|
new_marsh_map[id_] = marsh
|
|
for old_lake in old_lakes:
|
|
id_ = dom_get_attr(old_lake, 'data-sx-lake')
|
|
new_lake = dict_get(new_lake_map, id_)
|
|
if sx_truthy(new_lake):
|
|
sync_attrs(old_lake, new_lake)
|
|
morph_children(old_lake, new_lake)
|
|
for old_marsh in old_marshes:
|
|
id_ = dom_get_attr(old_marsh, 'data-sx-marsh')
|
|
new_marsh = dict_get(new_marsh_map, id_)
|
|
if sx_truthy(new_marsh):
|
|
morph_marsh(old_marsh, new_marsh, old_island)
|
|
return process_signal_updates(new_island)
|
|
|
|
# morph-marsh
|
|
def morph_marsh(old_marsh, new_marsh, island_el):
|
|
transform = dom_get_data(old_marsh, 'sx-marsh-transform')
|
|
env = dom_get_data(old_marsh, 'sx-marsh-env')
|
|
new_html = dom_inner_html(new_marsh)
|
|
if sx_truthy((env if not sx_truthy(env) else (new_html if not sx_truthy(new_html) else (not sx_truthy(empty_p(new_html)))))):
|
|
parsed = parse(new_html)
|
|
sx_content = (invoke(transform, parsed) if sx_truthy(transform) else parsed)
|
|
dispose_marsh_scope(old_marsh)
|
|
return with_marsh_scope(old_marsh, lambda : (lambda new_dom: _sx_begin(dom_remove_children_after(old_marsh, NIL), dom_append(old_marsh, new_dom)))(render_to_dom(sx_content, env, NIL)))
|
|
else:
|
|
sync_attrs(old_marsh, new_marsh)
|
|
return morph_children(old_marsh, new_marsh)
|
|
|
|
# process-signal-updates
|
|
def process_signal_updates(root):
|
|
signal_els = dom_query_all(root, '[data-sx-signal]')
|
|
return for_each(lambda el: (lambda spec: ((lambda colon_idx: ((lambda store_name: (lambda raw_value: _sx_begin((lambda parsed: reset_b(use_store(store_name), parsed))(json_parse(raw_value)), dom_remove_attr(el, 'data-sx-signal')))(slice(spec, (colon_idx + 1))))(slice(spec, 0, colon_idx)) if sx_truthy((colon_idx > 0)) else NIL))(index_of(spec, ':')) if sx_truthy(spec) else NIL))(dom_get_attr(el, 'data-sx-signal')), signal_els)
|
|
|
|
# swap-dom-nodes
|
|
def swap_dom_nodes(target, new_nodes, strategy):
|
|
_match = strategy
|
|
if _match == 'innerHTML':
|
|
if sx_truthy(dom_is_fragment_p(new_nodes)):
|
|
return morph_children(target, new_nodes)
|
|
else:
|
|
wrapper = dom_create_element('div', NIL)
|
|
dom_append(wrapper, new_nodes)
|
|
return morph_children(target, wrapper)
|
|
elif _match == 'outerHTML':
|
|
parent = dom_parent(target)
|
|
((lambda fc: (_sx_begin(morph_node(target, fc), (lambda sib: insert_remaining_siblings(parent, target, sib))(dom_next_sibling(fc))) if sx_truthy(fc) else dom_remove_child(parent, target)))(dom_first_child(new_nodes)) if sx_truthy(dom_is_fragment_p(new_nodes)) else morph_node(target, new_nodes))
|
|
return parent
|
|
elif _match == 'afterend':
|
|
return dom_insert_after(target, new_nodes)
|
|
elif _match == 'beforeend':
|
|
return dom_append(target, new_nodes)
|
|
elif _match == 'afterbegin':
|
|
return dom_prepend(target, new_nodes)
|
|
elif _match == 'beforebegin':
|
|
return dom_insert_before(dom_parent(target), new_nodes, target)
|
|
elif _match == 'delete':
|
|
return dom_remove_child(dom_parent(target), target)
|
|
elif _match == 'none':
|
|
return NIL
|
|
else:
|
|
if sx_truthy(dom_is_fragment_p(new_nodes)):
|
|
return morph_children(target, new_nodes)
|
|
else:
|
|
wrapper = dom_create_element('div', NIL)
|
|
dom_append(wrapper, new_nodes)
|
|
return morph_children(target, wrapper)
|
|
|
|
# insert-remaining-siblings
|
|
def insert_remaining_siblings(parent, ref_node, sib):
|
|
if sx_truthy(sib):
|
|
next = dom_next_sibling(sib)
|
|
dom_insert_after(ref_node, sib)
|
|
return insert_remaining_siblings(parent, sib, next)
|
|
return NIL
|
|
|
|
# swap-html-string
|
|
def swap_html_string(target, html, strategy):
|
|
_match = strategy
|
|
if _match == 'innerHTML':
|
|
return dom_set_inner_html(target, html)
|
|
elif _match == 'outerHTML':
|
|
parent = dom_parent(target)
|
|
dom_insert_adjacent_html(target, 'afterend', html)
|
|
dom_remove_child(parent, target)
|
|
return parent
|
|
elif _match == 'afterend':
|
|
return dom_insert_adjacent_html(target, 'afterend', html)
|
|
elif _match == 'beforeend':
|
|
return dom_insert_adjacent_html(target, 'beforeend', html)
|
|
elif _match == 'afterbegin':
|
|
return dom_insert_adjacent_html(target, 'afterbegin', html)
|
|
elif _match == 'beforebegin':
|
|
return dom_insert_adjacent_html(target, 'beforebegin', html)
|
|
elif _match == 'delete':
|
|
return dom_remove_child(dom_parent(target), target)
|
|
elif _match == 'none':
|
|
return NIL
|
|
else:
|
|
return dom_set_inner_html(target, html)
|
|
|
|
# handle-history
|
|
def handle_history(el, url, resp_headers):
|
|
push_url = dom_get_attr(el, 'sx-push-url')
|
|
replace_url = dom_get_attr(el, 'sx-replace-url')
|
|
hdr_replace = get(resp_headers, 'replace-url')
|
|
if sx_truthy(hdr_replace):
|
|
return browser_replace_state(hdr_replace)
|
|
elif sx_truthy((push_url if not sx_truthy(push_url) else (not sx_truthy((push_url == 'false'))))):
|
|
return browser_push_state((url if sx_truthy((push_url == 'true')) else push_url))
|
|
elif sx_truthy((replace_url if not sx_truthy(replace_url) else (not sx_truthy((replace_url == 'false'))))):
|
|
return browser_replace_state((url if sx_truthy((replace_url == 'true')) else replace_url))
|
|
return NIL
|
|
|
|
# PRELOAD_TTL
|
|
PRELOAD_TTL = 30000
|
|
|
|
# preload-cache-get
|
|
def preload_cache_get(cache, url):
|
|
entry = dict_get(cache, url)
|
|
if sx_truthy(is_nil(entry)):
|
|
return NIL
|
|
else:
|
|
if sx_truthy(((now_ms() - get(entry, 'timestamp')) > PRELOAD_TTL)):
|
|
dict_delete(cache, url)
|
|
return NIL
|
|
else:
|
|
dict_delete(cache, url)
|
|
return entry
|
|
|
|
# preload-cache-set
|
|
def preload_cache_set(cache, url, text, content_type):
|
|
return _sx_dict_set(cache, url, {'text': text, 'content-type': content_type, 'timestamp': now_ms()})
|
|
|
|
# classify-trigger
|
|
def classify_trigger(trigger):
|
|
event = get(trigger, 'event')
|
|
if sx_truthy((event == 'every')):
|
|
return 'poll'
|
|
elif sx_truthy((event == 'intersect')):
|
|
return 'intersect'
|
|
elif sx_truthy((event == 'load')):
|
|
return 'load'
|
|
elif sx_truthy((event == 'revealed')):
|
|
return 'revealed'
|
|
else:
|
|
return 'event'
|
|
|
|
# should-boost-link?
|
|
def should_boost_link_p(link):
|
|
href = dom_get_attr(link, 'href')
|
|
return (href if not sx_truthy(href) else ((not sx_truthy(starts_with_p(href, '#'))) if not sx_truthy((not sx_truthy(starts_with_p(href, '#')))) else ((not sx_truthy(starts_with_p(href, 'javascript:'))) if not sx_truthy((not sx_truthy(starts_with_p(href, 'javascript:')))) else ((not sx_truthy(starts_with_p(href, 'mailto:'))) if not sx_truthy((not sx_truthy(starts_with_p(href, 'mailto:')))) else (browser_same_origin_p(href) if not sx_truthy(browser_same_origin_p(href)) else ((not sx_truthy(dom_has_attr_p(link, 'sx-get'))) if not sx_truthy((not sx_truthy(dom_has_attr_p(link, 'sx-get')))) else ((not sx_truthy(dom_has_attr_p(link, 'sx-post'))) if not sx_truthy((not sx_truthy(dom_has_attr_p(link, 'sx-post')))) else (not sx_truthy(dom_has_attr_p(link, 'sx-disable'))))))))))
|
|
|
|
# should-boost-form?
|
|
def should_boost_form_p(form):
|
|
return ((not sx_truthy(dom_has_attr_p(form, 'sx-get'))) if not sx_truthy((not sx_truthy(dom_has_attr_p(form, 'sx-get')))) else ((not sx_truthy(dom_has_attr_p(form, 'sx-post'))) if not sx_truthy((not sx_truthy(dom_has_attr_p(form, 'sx-post')))) else (not sx_truthy(dom_has_attr_p(form, 'sx-disable')))))
|
|
|
|
# parse-sse-swap
|
|
def parse_sse_swap(el):
|
|
return (dom_get_attr(el, 'sx-sse-swap') if sx_truthy(dom_get_attr(el, 'sx-sse-swap')) else 'message')
|
|
|
|
|
|
# === Transpiled from router (client-side route matching) ===
|
|
|
|
# split-path-segments
|
|
def split_path_segments(path):
|
|
trimmed = (slice(path, 1) if sx_truthy(starts_with_p(path, '/')) else path)
|
|
trimmed2 = (slice(trimmed, 0, (len(trimmed) - 1)) if sx_truthy(((not sx_truthy(empty_p(trimmed))) if not sx_truthy((not sx_truthy(empty_p(trimmed)))) else ends_with_p(trimmed, '/'))) else trimmed)
|
|
if sx_truthy(empty_p(trimmed2)):
|
|
return []
|
|
else:
|
|
return split(trimmed2, '/')
|
|
|
|
# make-route-segment
|
|
def make_route_segment(seg):
|
|
if sx_truthy((starts_with_p(seg, '<') if not sx_truthy(starts_with_p(seg, '<')) else ends_with_p(seg, '>'))):
|
|
param_name = slice(seg, 1, (len(seg) - 1))
|
|
d = {}
|
|
d['type'] = 'param'
|
|
d['value'] = param_name
|
|
return d
|
|
else:
|
|
d = {}
|
|
d['type'] = 'literal'
|
|
d['value'] = seg
|
|
return d
|
|
|
|
# parse-route-pattern
|
|
def parse_route_pattern(pattern):
|
|
segments = split_path_segments(pattern)
|
|
return map(make_route_segment, segments)
|
|
|
|
# match-route-segments
|
|
def match_route_segments(path_segs, parsed_segs):
|
|
_cells = {}
|
|
if sx_truthy((not sx_truthy((len(path_segs) == len(parsed_segs))))):
|
|
return NIL
|
|
else:
|
|
params = {}
|
|
_cells['matched'] = True
|
|
for_each_indexed(lambda i, parsed_seg: ((lambda path_seg: (lambda seg_type: ((_sx_cell_set(_cells, 'matched', False) if sx_truthy((not sx_truthy((path_seg == get(parsed_seg, 'value'))))) else NIL) if sx_truthy((seg_type == 'literal')) else (_sx_dict_set(params, get(parsed_seg, 'value'), path_seg) if sx_truthy((seg_type == 'param')) else _sx_cell_set(_cells, 'matched', False))))(get(parsed_seg, 'type')))(nth(path_segs, i)) if sx_truthy(_cells['matched']) else NIL), parsed_segs)
|
|
if sx_truthy(_cells['matched']):
|
|
return params
|
|
else:
|
|
return NIL
|
|
|
|
# match-route
|
|
def match_route(path, pattern):
|
|
path_segs = split_path_segments(path)
|
|
parsed_segs = parse_route_pattern(pattern)
|
|
return match_route_segments(path_segs, parsed_segs)
|
|
|
|
# find-matching-route
|
|
def find_matching_route(path, routes):
|
|
_cells = {}
|
|
path_segs = split_path_segments(path)
|
|
_cells['result'] = NIL
|
|
for route in routes:
|
|
if sx_truthy(is_nil(_cells['result'])):
|
|
params = match_route_segments(path_segs, get(route, 'parsed'))
|
|
if sx_truthy((not sx_truthy(is_nil(params)))):
|
|
matched = merge(route, {})
|
|
matched['params'] = params
|
|
_cells['result'] = matched
|
|
return _cells['result']
|
|
|
|
|
|
# === Transpiled from signals (reactive signal runtime) ===
|
|
|
|
# signal
|
|
def signal(initial_value):
|
|
return make_signal(initial_value)
|
|
|
|
# deref
|
|
def deref(s):
|
|
if sx_truthy((not sx_truthy(is_signal(s)))):
|
|
return s
|
|
else:
|
|
ctx = get_tracking_context()
|
|
if sx_truthy(ctx):
|
|
tracking_context_add_dep(ctx, s)
|
|
signal_add_sub(s, tracking_context_notify_fn(ctx))
|
|
return signal_value(s)
|
|
|
|
# reset!
|
|
def reset_b(s, value):
|
|
if sx_truthy(is_signal(s)):
|
|
old = signal_value(s)
|
|
if sx_truthy((not sx_truthy(is_identical(old, value)))):
|
|
signal_set_value(s, value)
|
|
return notify_subscribers(s)
|
|
return NIL
|
|
return NIL
|
|
|
|
# swap!
|
|
def swap_b(s, f, *args):
|
|
if sx_truthy(is_signal(s)):
|
|
old = signal_value(s)
|
|
new_val = apply(f, cons(old, args))
|
|
if sx_truthy((not sx_truthy(is_identical(old, new_val)))):
|
|
signal_set_value(s, new_val)
|
|
return notify_subscribers(s)
|
|
return NIL
|
|
return NIL
|
|
|
|
# computed
|
|
def computed(compute_fn):
|
|
s = make_signal(NIL)
|
|
deps = []
|
|
compute_ctx = NIL
|
|
recompute = _sx_fn(lambda : (
|
|
for_each(lambda dep: signal_remove_sub(dep, recompute), signal_deps(s)),
|
|
signal_set_deps(s, []),
|
|
(lambda ctx: (lambda prev: _sx_begin(set_tracking_context(ctx), (lambda new_val: _sx_begin(set_tracking_context(prev), signal_set_deps(s, tracking_context_deps(ctx)), (lambda old: _sx_begin(signal_set_value(s, new_val), (notify_subscribers(s) if sx_truthy((not sx_truthy(is_identical(old, new_val)))) else NIL)))(signal_value(s))))(invoke(compute_fn))))(get_tracking_context()))(make_tracking_context(recompute))
|
|
)[-1])
|
|
recompute()
|
|
register_in_scope(lambda : dispose_computed(s))
|
|
return s
|
|
|
|
# effect
|
|
def effect(effect_fn):
|
|
_cells = {}
|
|
_cells['deps'] = []
|
|
_cells['disposed'] = False
|
|
_cells['cleanup_fn'] = NIL
|
|
run_effect = lambda : (_sx_begin((invoke(_cells['cleanup_fn']) if sx_truthy(_cells['cleanup_fn']) else NIL), for_each(lambda dep: signal_remove_sub(dep, run_effect), _cells['deps']), _sx_cell_set(_cells, 'deps', []), (lambda ctx: (lambda prev: _sx_begin(set_tracking_context(ctx), (lambda result: _sx_begin(set_tracking_context(prev), _sx_cell_set(_cells, 'deps', tracking_context_deps(ctx)), (_sx_cell_set(_cells, 'cleanup_fn', result) if sx_truthy(is_callable(result)) else NIL)))(invoke(effect_fn))))(get_tracking_context()))(make_tracking_context(run_effect))) if sx_truthy((not sx_truthy(_cells['disposed']))) else NIL)
|
|
run_effect()
|
|
dispose_fn = _sx_fn(lambda : (
|
|
_sx_cell_set(_cells, 'disposed', True),
|
|
(invoke(_cells['cleanup_fn']) if sx_truthy(_cells['cleanup_fn']) else NIL),
|
|
for_each(lambda dep: signal_remove_sub(dep, run_effect), _cells['deps']),
|
|
_sx_cell_set(_cells, 'deps', [])
|
|
)[-1])
|
|
register_in_scope(dispose_fn)
|
|
return dispose_fn
|
|
|
|
# *batch-depth*
|
|
_batch_depth = 0
|
|
|
|
# *batch-queue*
|
|
_batch_queue = []
|
|
|
|
# batch
|
|
def batch(thunk):
|
|
_batch_depth = (_batch_depth + 1)
|
|
invoke(thunk)
|
|
_batch_depth = (_batch_depth - 1)
|
|
if sx_truthy((_batch_depth == 0)):
|
|
queue = _batch_queue
|
|
_batch_queue = []
|
|
seen = []
|
|
pending = []
|
|
for s in queue:
|
|
for sub in signal_subscribers(s):
|
|
if sx_truthy((not sx_truthy(contains_p(seen, sub)))):
|
|
seen.append(sub)
|
|
pending.append(sub)
|
|
return for_each(lambda sub: sub(), pending)
|
|
return NIL
|
|
|
|
# notify-subscribers
|
|
def notify_subscribers(s):
|
|
if sx_truthy((_batch_depth > 0)):
|
|
if sx_truthy((not sx_truthy(contains_p(_batch_queue, s)))):
|
|
return _sx_append(_batch_queue, s)
|
|
return NIL
|
|
else:
|
|
return flush_subscribers(s)
|
|
|
|
# flush-subscribers
|
|
def flush_subscribers(s):
|
|
return for_each(lambda sub: sub(), signal_subscribers(s))
|
|
|
|
# dispose-computed
|
|
def dispose_computed(s):
|
|
if sx_truthy(is_signal(s)):
|
|
for dep in signal_deps(s):
|
|
signal_remove_sub(dep, NIL)
|
|
return signal_set_deps(s, [])
|
|
return NIL
|
|
|
|
# *island-scope*
|
|
_island_scope = NIL
|
|
|
|
# with-island-scope
|
|
def with_island_scope(scope_fn, body_fn):
|
|
prev = _island_scope
|
|
_island_scope = scope_fn
|
|
result = body_fn()
|
|
_island_scope = prev
|
|
return result
|
|
|
|
# register-in-scope
|
|
def register_in_scope(disposable):
|
|
if sx_truthy(_island_scope):
|
|
return _island_scope(disposable)
|
|
return NIL
|
|
|
|
# with-marsh-scope
|
|
def with_marsh_scope(marsh_el, body_fn):
|
|
disposers = []
|
|
with_island_scope(lambda d: _sx_append(disposers, d), body_fn)
|
|
return dom_set_data(marsh_el, 'sx-marsh-disposers', disposers)
|
|
|
|
# dispose-marsh-scope
|
|
def dispose_marsh_scope(marsh_el):
|
|
disposers = dom_get_data(marsh_el, 'sx-marsh-disposers')
|
|
if sx_truthy(disposers):
|
|
for d in disposers:
|
|
invoke(d)
|
|
return dom_set_data(marsh_el, 'sx-marsh-disposers', NIL)
|
|
return NIL
|
|
|
|
# *store-registry*
|
|
_store_registry = {}
|
|
|
|
# def-store
|
|
def def_store(name, init_fn):
|
|
registry = _store_registry
|
|
if sx_truthy((not sx_truthy(has_key_p(registry, name)))):
|
|
_store_registry = assoc(registry, name, invoke(init_fn))
|
|
return get(_store_registry, name)
|
|
|
|
# use-store
|
|
def use_store(name):
|
|
if sx_truthy(has_key_p(_store_registry, name)):
|
|
return get(_store_registry, name)
|
|
else:
|
|
return error(sx_str('Store not found: ', name, '. Call (def-store ...) before (use-store ...).'))
|
|
|
|
# clear-stores
|
|
def clear_stores():
|
|
return _sx_cell_set(_cells, '_store_registry', {})
|
|
|
|
# emit-event
|
|
def emit_event(el, event_name, detail):
|
|
return dom_dispatch(el, event_name, detail)
|
|
|
|
# on-event
|
|
def on_event(el, event_name, handler):
|
|
return dom_listen(el, event_name, handler)
|
|
|
|
# bridge-event
|
|
def bridge_event(el, event_name, target_signal, transform_fn):
|
|
return effect(lambda : (lambda remove: remove)(dom_listen(el, event_name, lambda e: (lambda detail: (lambda new_val: reset_b(target_signal, new_val))((invoke(transform_fn, detail) if sx_truthy(transform_fn) else detail)))(event_detail(e)))))
|
|
|
|
# resource
|
|
def resource(fetch_fn):
|
|
state = signal({'loading': True, 'data': NIL, 'error': NIL})
|
|
promise_then(invoke(fetch_fn), lambda data: reset_b(state, {'loading': False, 'data': data, 'error': NIL}), lambda err: reset_b(state, {'loading': False, 'data': NIL, 'error': err}))
|
|
return state
|
|
|
|
|
|
# =========================================================================
|
|
# Fixups -- wire up render adapter dispatch
|
|
# =========================================================================
|
|
|
|
def _setup_html_adapter():
|
|
global _render_expr_fn
|
|
_render_expr_fn = lambda expr, env: render_list_to_html(expr, env)
|
|
|
|
def _setup_sx_adapter():
|
|
global _render_expr_fn
|
|
_render_expr_fn = lambda expr, env: aser_list(expr, env)
|
|
|
|
|
|
# Wrap aser_call and aser_fragment to return SxExpr
|
|
# so serialize() won't double-quote them
|
|
_orig_aser_call = None
|
|
_orig_aser_fragment = None
|
|
|
|
def _wrap_aser_outputs():
|
|
global aser_call, aser_fragment, _orig_aser_call, _orig_aser_fragment
|
|
_orig_aser_call = aser_call
|
|
_orig_aser_fragment = aser_fragment
|
|
def _aser_call_wrapped(name, args, env):
|
|
result = _orig_aser_call(name, args, env)
|
|
return SxExpr(result) if isinstance(result, str) else result
|
|
def _aser_fragment_wrapped(children, env):
|
|
result = _orig_aser_fragment(children, env)
|
|
return SxExpr(result) if isinstance(result, str) else result
|
|
aser_call = _aser_call_wrapped
|
|
aser_fragment = _aser_fragment_wrapped
|
|
|
|
|
|
# Override sf_set_bang to walk the Env scope chain so that (set! var val)
|
|
# updates the variable in its defining scope, not just the local copy.
|
|
def sf_set_bang(args, env):
|
|
name = symbol_name(first(args))
|
|
value = trampoline(eval_expr(nth(args, 1), env))
|
|
env = _ensure_env(env)
|
|
try:
|
|
env.set(name, value)
|
|
except KeyError:
|
|
# Not found in chain — define locally (matches prior behavior)
|
|
env[name] = value
|
|
return value
|
|
|
|
|
|
# =========================================================================
|
|
# Public API
|
|
# =========================================================================
|
|
|
|
# Wrap aser outputs to return SxExpr
|
|
_wrap_aser_outputs()
|
|
|
|
# Set HTML as default adapter
|
|
_setup_html_adapter()
|
|
|
|
def evaluate(expr, env=None):
|
|
"""Evaluate expr in env and return the result."""
|
|
if env is None:
|
|
env = _Env()
|
|
elif isinstance(env, dict):
|
|
env = _Env(env)
|
|
result = eval_expr(expr, env)
|
|
while is_thunk(result):
|
|
result = eval_expr(thunk_expr(result), thunk_env(result))
|
|
return result
|
|
|
|
|
|
def render(expr, env=None):
|
|
"""Render expr to HTML string."""
|
|
global _render_mode
|
|
if env is None:
|
|
env = {}
|
|
try:
|
|
_render_mode = True
|
|
return render_to_html(expr, env)
|
|
finally:
|
|
_render_mode = False
|
|
|
|
|
|
def make_env(**kwargs):
|
|
"""Create an environment with initial bindings."""
|
|
return _Env(dict(kwargs))
|