Skip to main content

meta_language/translation/
source_loops.rs

1//! Owned closed scalar loop header, state and ordered continuation proof.
2//! This isolated draft retains value and type checks in the existing core.
3use serde::{Deserialize, Serialize};
4
5mod validation;
6pub(crate) use validation::{validate_source_loop, validate_source_loop_at};
7
8/// One actual function parameter participating in the owned loop state.
9#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(deny_unknown_fields)]
11pub struct ScalarLoopState {
12    /// Actual parameter binding identity.
13    pub name: String,
14    /// Whether the parsed source permits writes to this slot.
15    pub mutable: bool,
16}
17
18/// One scalar snapshot written to a parameter after every snapshot is read.
19#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
20#[serde(deny_unknown_fields)]
21pub struct ScalarLoopWrite {
22    /// The actual mutable parameter slot.
23    pub target: String,
24    /// The immutable binding evaluated in the previous state.
25    pub binding: String,
26}
27
28/// Actual ordered writes and the resulting complete continuation argument list.
29#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
30#[serde(deny_unknown_fields)]
31pub struct ScalarLoopTransition {
32    /// Snapshot evaluation order, independent of parameter position.
33    pub writes: Vec<ScalarLoopWrite>,
34    /// One actual argument binding for every state slot.
35    pub arguments: Vec<String>,
36}
37
38/// A selected JavaScript scalar interface position.
39#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
40#[serde(deny_unknown_fields)]
41pub struct ScalarLoopInterfaceType {
42    /// Number or Boolean value kind checked against the actual declaration.
43    pub kind: String,
44}
45
46/// Actual inferred Number/Boolean header facts, distinct from Rust owners.
47#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
48#[serde(deny_unknown_fields)]
49pub struct ScalarLoopInterface {
50    /// The selected JavaScript scalar interpretation.
51    pub model: String,
52    /// Inferred actual parameter kinds in header order.
53    pub parameters: Vec<ScalarLoopInterfaceType>,
54    /// Inferred actual result kind.
55    pub result: ScalarLoopInterfaceType,
56}
57
58/// Exact absolute namespace of the declaration owning a scalar continuation.
59#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
60#[serde(deny_unknown_fields)]
61pub struct ScalarLoopContinuation {
62    /// The shared absolute namespace root; independent of any local value name.
63    pub root: String,
64    /// Every actual declaration scope segment followed by the original name.
65    pub path: Vec<String>,
66}
67
68/// An owned proof of one closed scalar loop and its immutable-state lowering.
69#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
70#[serde(rename_all = "camelCase", deny_unknown_fields)]
71pub struct SourceLoop {
72    /// Explicit versioned source interpretation.
73    pub model: String,
74    /// Actual parsed source language.
75    pub language: String,
76    /// Actual original function name, including any checked reserved prefix.
77    pub source_name: String,
78    /// The explicitly selected iterative tail-continuation interpretation.
79    pub lowering: String,
80    /// Every actual parameter in its source order.
81    pub state: Vec<ScalarLoopState>,
82    /// Every actual continuation in expression traversal order.
83    pub transitions: Vec<ScalarLoopTransition>,
84    /// The JavaScript interface; Rust instead owns a source signature.
85    #[serde(rename = "interface", default, skip_serializing_if = "Option::is_none")]
86    pub source_interface: Option<ScalarLoopInterface>,
87    /// Owned declaration target, present only in the declaration-directed profile.
88    #[serde(default, skip_serializing_if = "Option::is_none")]
89    pub continuation: Option<ScalarLoopContinuation>,
90}
91
92pub(crate) fn scalar_loop_policy() -> &'static serde_json::Value {
93    static POLICY: std::sync::OnceLock<serde_json::Value> = std::sync::OnceLock::new();
94    POLICY.get_or_init(|| {
95        crate::read_structured_data_lino(include_str!("../data/rust-scalar-loops.lino"))
96            .expect("shared scalar loop policy is structured Links data")
97    })
98}
99
100pub(crate) fn scalar_loop_snapshot_binding(name: &str) -> bool {
101    static PATTERN: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
102    PATTERN
103        .get_or_init(|| {
104            regex::Regex::new(
105                scalar_loop_policy()["snapshotBindingPattern"]
106                    .as_str()
107                    .expect("shared snapshot grammar is text"),
108            )
109            .expect("shared scalar loop snapshot grammar is valid")
110        })
111        .is_match(name)
112}