1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! Register variables (`\count`, `\countdef`)

use texcraft_stdext::collections::groupingmap;
use texlang::parse::OptionalEquals;
use texlang::traits::*;
use texlang::*;

pub const COUNT_DOC: &str = "Get or set an integer register";
pub const COUNTDEF_DOC: &str = "Bind an integer register to a control sequence";

pub struct Component<T, const N: usize>(
    // We currently box the values because putting them directly on the stack causes the
    // message pack decoder to stack overflow. It's a pity that we have to pay a runtime
    // cost due to this, and it would be nice to fix the issue another way.
    Box<[T; N]>,
);

#[cfg(feature = "serde")]
impl<T: serde::Serialize, const N: usize> serde::Serialize for Component<T, N> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let v: Vec<&T> = self.0.iter().collect();
        v.serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de, T: std::fmt::Debug + serde::Deserialize<'de>, const N: usize> serde::Deserialize<'de>
    for Component<T, N>
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let v = Vec::<T>::deserialize(deserializer)?;
        let a: Box<[T; N]> = v.try_into().unwrap();
        Ok(Component(a))
    }
}

impl<const N: usize> Default for Component<i32, N> {
    fn default() -> Self {
        Self(Box::new([Default::default(); N]))
    }
}

impl<const N: usize> Default for Component<Vec<token::Token>, N> {
    fn default() -> Self {
        let mut v = vec![];
        for _ in 0..N {
            v.push(vec![])
        }
        Self(Box::new(v.try_into().unwrap()))
    }
}

/// Get the `\count` command.
pub fn get_count<S: HasComponent<Component<i32, N>>, const N: usize>() -> command::BuiltIn<S> {
    variable::Command::new_array(ref_fn, mut_fn, variable::IndexResolver::Dynamic(count_fn)).into()
}

/// Get the `\toks` command.
pub fn get_toks<S: HasComponent<Component<Vec<token::Token>, N>>, const N: usize>(
) -> command::BuiltIn<S> {
    variable::Command::new_array(ref_fn, mut_fn, variable::IndexResolver::Dynamic(count_fn)).into()
}

fn count_fn<T, S: HasComponent<Component<T, N>>, const N: usize>(
    _: token::Token,
    input: &mut vm::ExpandedStream<S>,
) -> command::Result<variable::Index> {
    let index = parse::Uint::<N>::parse(input)?;
    Ok(index.0.into())
}

/// Get the `\countdef` command.
pub fn get_countdef<S: HasComponent<Component<i32, N>>, const N: usize>() -> command::BuiltIn<S> {
    command::BuiltIn::new_execution(countdef_fn)
}

/// Get the `\toksdef` command.
pub fn get_toksdef<S: HasComponent<Component<Vec<token::Token>, N>>, const N: usize>(
) -> command::BuiltIn<S> {
    command::BuiltIn::new_execution(countdef_fn)
}

fn countdef_fn<T: variable::SupportedType, S: HasComponent<Component<T, N>>, const N: usize>(
    _: token::Token,
    input: &mut vm::ExecutionInput<S>,
) -> command::Result<()> {
    let (target, _, index) = <(token::CommandRef, OptionalEquals, parse::Uint<N>)>::parse(input)?;
    // TODO: I suspect \countdef should honor \global, but haven't checked pdfTeX.
    input.commands_map_mut().insert_variable_command(
        target,
        variable::Command::new_array(
            ref_fn,
            mut_fn,
            variable::IndexResolver::Static(index.0.into()),
        ),
        groupingmap::Scope::Local,
    );
    Ok(())
}

fn ref_fn<T, S: HasComponent<Component<T, N>>, const N: usize>(
    state: &S,
    index: variable::Index,
) -> &T {
    state.component().0.get(index.0).unwrap()
}

fn mut_fn<T, S: HasComponent<Component<T, N>>, const N: usize>(
    state: &mut S,
    index: variable::Index,
) -> &mut T {
    state.component_mut().0.get_mut(index.0).unwrap()
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;
    use crate::the;
    use texlang::vm::implement_has_component;
    use texlang_testing::*;

    #[derive(Default, serde::Serialize, serde::Deserialize)]
    struct State {
        registers_i32: Component<i32, 256>,
        registers_token_list: Component<Vec<token::Token>, 256>,
        testing: TestingComponent,
    }

    impl TexlangState for State {
        fn recoverable_error_hook(
            vm: &vm::VM<Self>,
            recoverable_error: Box<error::Error>,
        ) -> Result<(), Box<error::Error>> {
            TestingComponent::recoverable_error_hook(vm, recoverable_error)
        }
    }

    implement_has_component![State{
        registers_i32: Component<i32, 256>,
        registers_token_list: Component<Vec<token::Token>, 256>,
        testing: TestingComponent,
    }];

    fn built_in_commands() -> HashMap<&'static str, command::BuiltIn<State>> {
        HashMap::from([
            ("the", the::get_the()),
            ("count", get_count()),
            ("countdef", get_countdef()),
            ("toks", get_toks()),
            ("toksdef", get_toksdef()),
        ])
    }

    test_suite![
        expansion_equality_tests(
            (write_and_read_register, r"\count 23 4 \the\count 23", r"4"),
            (
                write_and_read_register_eq,
                r"\count 23 = 4 \the\count 23",
                r"4"
            ),
            (
                negative_negative,
                r"\count 1=5000 \count 0=-1 \the \count -\count 0",
                r"5000"
            ),
            (countdef_base_case, r"\countdef\A 23\A 4 \the\A", r"4"),
            (countdef_base_case_eq, r"\countdef\A = 23\A 4 \the\A", r"4"),
            (
                countdef_with_count,
                r"\countdef\A 23\A 4\count 1 0 \the\A",
                r"4"
            ),
            (
                countdef_with_same_count,
                r"\countdef\A 23\A 4\count 23 5 \the\A",
                r"5"
            ),
            (
                toks_basic,
                r"\toks 1 = {Hola, Mundo}\the \toks 1",
                r"Hola, Mundo"
            ),
            (
                toksdef_basic,
                r"\toksdef\content 1 \toks 1 = {Hola, Mundo}\the \content",
                r"Hola, Mundo"
            ),
            (
                toks_copy,
                r"\toks 1 = {Hola, Mundo}\toks 2 = \toks 1 \the \toks 2",
                r"Hola, Mundo"
            ),
        ),
        serde_tests(
            (serde_basic, r"\count 100 200 ", r"\the \count 100"),
            (serde_countdef, r"\countdef \A 100 \A = 200 ", r"\the \A"),
            (
                serde_group,
                r"\count 1 2 {\count 1 3 ",
                r"\the \count 1 } \the \count 1"
            ),
        ),
        recoverable_failure_tests(
            (
                write_register_index_too_big,
                r"\count 260 = 4 \the\count 0",
                "4"
            ),
            (
                write_register_negative_index,
                r"\count -1 = 4 \the\count 0",
                "4"
            ),
            (
                countdef_register_index_too_big,
                r"\countdef\A 260 \A= 4 \the\count 0",
                "4"
            ),
        ),
    ];
}