seq-repl 5.5.1

TUI REPL for the Seq programming language with IR visualization
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! ASCII Art Stack Effect Diagrams
//!
//! Renders stack states and transitions using box-drawing characters.
//! These are pure functions that take stack data and return multi-line strings.
//!
//! # Example Output
//!
//! ```text
//!  swap ( ..a x y -- ..a y x )
//!  ┌───────┐      ┌───────┐
//!  │ Float │      │  Int  │
//!  ├───────┤  →   ├───────┤
//!  │  Int  │      │ Float │
//!  ├───────┤      ├───────┤
//!  │  ..a  │      │  ..a  │
//!  └───────┘      └───────┘
//! ```

use std::fmt;

/// A value on the stack - either a concrete type/value or a rest variable
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StackValue {
    /// A concrete type (e.g., "Int", "Float", "Bool")
    Type(String),
    /// A concrete value (e.g., "5", "3.14", "true")
    Value(String),
    /// A type variable (e.g., "x", "y", "a")
    TypeVar(String),
    /// A rest/row variable representing remaining stack (e.g., "..a", "..s")
    Rest(String),
}

impl StackValue {
    /// Create a type value
    pub fn ty(s: impl Into<String>) -> Self {
        Self::Type(s.into())
    }

    /// Create a concrete value
    pub fn val(s: impl Into<String>) -> Self {
        Self::Value(s.into())
    }

    /// Create a type variable
    pub fn var(s: impl Into<String>) -> Self {
        Self::TypeVar(s.into())
    }

    /// Create a rest variable
    pub fn rest(s: impl Into<String>) -> Self {
        Self::Rest(s.into())
    }

    /// Get the display string for this value
    fn display(&self) -> String {
        match self {
            Self::Type(s) | Self::Value(s) | Self::TypeVar(s) => s.clone(),
            Self::Rest(s) => format!("..{}", s),
        }
    }

    /// Check if this is a rest variable
    #[allow(dead_code)]
    fn is_rest(&self) -> bool {
        matches!(self, Self::Rest(_))
    }
}

impl fmt::Display for StackValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.display())
    }
}

/// A stack state - a sequence of values with bottom (rest) first
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Stack {
    /// Values from bottom to top (rest variable, if any, is first)
    values: Vec<StackValue>,
}

impl Stack {
    /// Create a new stack from values (bottom to top)
    pub fn new(values: Vec<StackValue>) -> Self {
        Self { values }
    }

    /// Create an empty stack
    #[allow(dead_code)]
    pub fn empty() -> Self {
        Self { values: vec![] }
    }

    /// Create a stack with just a rest variable
    pub fn with_rest(name: impl Into<String>) -> Self {
        Self {
            values: vec![StackValue::rest(name)],
        }
    }

    /// Push a value onto the stack (top)
    pub fn push(mut self, value: StackValue) -> Self {
        self.values.push(value);
        self
    }

    /// Get all values (bottom to top)
    #[allow(dead_code)]
    pub fn values(&self) -> &[StackValue] {
        &self.values
    }

    /// Check if the stack has values (excluding rest)
    #[allow(dead_code)]
    pub fn has_concrete_values(&self) -> bool {
        self.values.iter().any(|v| !v.is_rest())
    }

    /// Get the width needed to display this stack
    fn display_width(&self) -> usize {
        self.values
            .iter()
            .map(|v| v.display().len())
            .max()
            .unwrap_or(0)
            .max(3) // Minimum width of 3
    }
}

/// A stack effect signature (inputs → outputs)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StackEffect {
    /// Name of the word/operation
    pub name: String,
    /// Input stack (what the operation consumes)
    pub inputs: Stack,
    /// Output stack (what the operation produces)
    pub outputs: Stack,
}

impl StackEffect {
    /// Create a new stack effect
    pub fn new(name: impl Into<String>, inputs: Stack, outputs: Stack) -> Self {
        Self {
            name: name.into(),
            inputs,
            outputs,
        }
    }

    /// Create a stack effect for a literal value (pushes onto stack)
    pub fn literal(value: impl Into<String>) -> Self {
        let val = value.into();
        Self {
            name: val.clone(),
            inputs: Stack::with_rest("a"),
            outputs: Stack::with_rest("a").push(StackValue::val(val)),
        }
    }

    /// Render the effect signature line: `swap ( ..a x y -- ..a y x )`
    pub fn render_signature(&self) -> String {
        let inputs: Vec<_> = self.inputs.values.iter().map(|v| v.display()).collect();
        let outputs: Vec<_> = self.outputs.values.iter().map(|v| v.display()).collect();
        format!(
            "{} ( {} -- {} )",
            self.name,
            inputs.join(" "),
            outputs.join(" ")
        )
    }
}

/// Render a single stack as ASCII art box
///
/// Returns a vector of lines (top to bottom in visual display)
pub fn render_stack(stack: &Stack) -> Vec<String> {
    if stack.values.is_empty() {
        return vec!["(empty)".to_string()];
    }

    let width = stack.display_width();
    let inner_width = width + 2; // Padding on each side

    let top_border = format!("{}", "".repeat(inner_width));
    let mid_border = format!("{}", "".repeat(inner_width));
    let bot_border = format!("{}", "".repeat(inner_width));

    let mut lines = Vec::new();

    // Render from top to bottom (reverse of storage order)
    let values: Vec<_> = stack.values.iter().rev().collect();

    for (i, value) in values.iter().enumerate() {
        if i == 0 {
            lines.push(top_border.clone());
        } else {
            lines.push(mid_border.clone());
        }

        let display = value.display();
        let padding = inner_width - display.len();
        let left_pad = padding / 2;
        let right_pad = padding - left_pad;
        lines.push(format!(
            "{}{}{}",
            " ".repeat(left_pad),
            display,
            " ".repeat(right_pad)
        ));
    }

    lines.push(bot_border);
    lines
}

/// Render a stack transition (before → after) for a single operation
pub fn render_transition(effect: &StackEffect, before: &Stack, after: &Stack) -> Vec<String> {
    let sig = effect.render_signature();
    let before_lines = render_stack(before);
    let after_lines = render_stack(after);

    // Calculate widths
    let before_width = before_lines.iter().map(|l| l.len()).max().unwrap_or(0);
    let arrow_col = "";

    // Pad to same height
    let max_height = before_lines.len().max(after_lines.len());

    let mut result = vec![format!(" {}", sig)];

    for i in 0..max_height {
        let before_line = before_lines
            .get(i)
            .map(|s| s.as_str())
            .unwrap_or("")
            .to_string();
        let after_line = after_lines
            .get(i)
            .map(|s| s.as_str())
            .unwrap_or("")
            .to_string();

        // Pad before_line to consistent width
        let padded_before = format!("{:width$}", before_line, width = before_width);

        // Arrow only on the middle-ish row
        let arrow = if i == max_height / 2 {
            arrow_col
        } else {
            "      "
        };

        result.push(format!(" {}{}{}", padded_before, arrow, after_line));
    }

    result
}

/// Render a sequence of operations showing the stack evolving
///
/// Each step shows: word name, input stack → output stack
#[allow(dead_code)]
pub fn render_sequence(steps: &[(StackEffect, Stack, Stack)]) -> Vec<String> {
    if steps.is_empty() {
        return vec![];
    }

    let mut result = Vec::new();

    for (i, (effect, before, after)) in steps.iter().enumerate() {
        if i > 0 {
            result.push(String::new()); // Blank line between steps
        }
        result.extend(render_transition(effect, before, after));
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_stack_value_display() {
        assert_eq!(StackValue::ty("Int").display(), "Int");
        assert_eq!(StackValue::val("42").display(), "42");
        assert_eq!(StackValue::var("x").display(), "x");
        assert_eq!(StackValue::rest("a").display(), "..a");
    }

    #[test]
    fn test_empty_stack_render() {
        let stack = Stack::empty();
        let lines = render_stack(&stack);
        assert_eq!(lines, vec!["(empty)"]);
    }

    #[test]
    fn test_single_value_stack() {
        let stack = Stack::new(vec![StackValue::val("42")]);
        let lines = render_stack(&stack);
        assert_eq!(lines.len(), 3); // top, content, bottom
        assert!(lines[0].contains(""));
        assert!(lines[1].contains("42"));
        assert!(lines[2].contains(""));
    }

    #[test]
    fn test_multi_value_stack() {
        let stack = Stack::new(vec![
            StackValue::rest("a"),
            StackValue::ty("Int"),
            StackValue::ty("Float"),
        ]);
        let lines = render_stack(&stack);

        // Should have: top, Float, mid, Int, mid, ..a, bottom = 7 lines
        assert_eq!(lines.len(), 7);
        // Float is on top visually (rendered first)
        assert!(lines[1].contains("Float"));
        // Int in middle
        assert!(lines[3].contains("Int"));
        // Rest at bottom
        assert!(lines[5].contains("..a"));
    }

    #[test]
    fn test_stack_effect_signature() {
        let effect = StackEffect::new(
            "swap",
            Stack::new(vec![
                StackValue::rest("a"),
                StackValue::var("x"),
                StackValue::var("y"),
            ]),
            Stack::new(vec![
                StackValue::rest("a"),
                StackValue::var("y"),
                StackValue::var("x"),
            ]),
        );

        assert_eq!(effect.render_signature(), "swap ( ..a x y -- ..a y x )");
    }

    #[test]
    fn test_swap_transition() {
        let effect = StackEffect::new(
            "swap",
            Stack::new(vec![
                StackValue::rest("a"),
                StackValue::var("x"),
                StackValue::var("y"),
            ]),
            Stack::new(vec![
                StackValue::rest("a"),
                StackValue::var("y"),
                StackValue::var("x"),
            ]),
        );

        let before = Stack::new(vec![
            StackValue::rest("a"),
            StackValue::ty("Int"),
            StackValue::ty("Float"),
        ]);

        let after = Stack::new(vec![
            StackValue::rest("a"),
            StackValue::ty("Float"),
            StackValue::ty("Int"),
        ]);

        let lines = render_transition(&effect, &before, &after);

        // First line should be the signature
        assert!(lines[0].contains("swap ( ..a x y -- ..a y x )"));

        // Should contain the arrow
        let arrow_line = lines.iter().find(|l| l.contains(""));
        assert!(arrow_line.is_some());
    }

    #[test]
    fn test_dup_multiply_example() {
        // dup ( ..a x -- ..a x x )
        let dup_effect = StackEffect::new(
            "dup",
            Stack::new(vec![StackValue::rest("a"), StackValue::var("x")]),
            Stack::new(vec![
                StackValue::rest("a"),
                StackValue::var("x"),
                StackValue::var("x"),
            ]),
        );

        let before_dup = Stack::new(vec![StackValue::val("5")]);
        let after_dup = Stack::new(vec![StackValue::val("5"), StackValue::val("5")]);

        // i.multiply ( ..a Int Int -- ..a Int )
        let mult_effect = StackEffect::new(
            "i.multiply",
            Stack::new(vec![
                StackValue::rest("a"),
                StackValue::ty("Int"),
                StackValue::ty("Int"),
            ]),
            Stack::new(vec![StackValue::rest("a"), StackValue::ty("Int")]),
        );

        let after_mult = Stack::new(vec![StackValue::val("25")]);

        let sequence = vec![
            (dup_effect, before_dup, after_dup.clone()),
            (mult_effect, after_dup, after_mult),
        ];

        let lines = render_sequence(&sequence);

        // Should contain both word signatures
        let text = lines.join("\n");
        assert!(text.contains("dup"));
        assert!(text.contains("i.multiply"));
        assert!(text.contains("5"));
        assert!(text.contains("25"));
    }

    #[test]
    fn test_width_calculation() {
        let stack = Stack::new(vec![
            StackValue::ty("VeryLongTypeName"),
            StackValue::ty("Int"),
        ]);

        let lines = render_stack(&stack);
        // All lines should have the same visual width (character count, not bytes)
        let widths: Vec<_> = lines.iter().map(|l| l.chars().count()).collect();
        assert!(widths.windows(2).all(|w| w[0] == w[1]));
    }
}