shape-vm 0.3.2

Stack-based bytecode virtual machine for the Shape programming language
Documentation
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//! Compile-time string interpolation compilation.
//!
//! Interpolation syntax parsing itself lives in `shape-ast` so compiler,
//! type inference, and LSP all use the same parser.

use crate::bytecode::{BuiltinFunction, Constant, Instruction, OpCode, Operand};
use crate::compiler::BytecodeCompiler;
use shape_ast::ast::InterpolationMode;
use shape_ast::content_style::{ColorSpec, ContentFormatSpec, NamedContentColor};
use shape_ast::error::{Result, ShapeError};
use shape_ast::interpolation::{
    FormatAlignment, FormatColor, InterpolationFormatSpec, InterpolationPart,
    parse_interpolation_with_mode,
};
pub use shape_ast::interpolation::{has_interpolation, has_interpolation_with_mode};

const FORMAT_SPEC_FIXED: i64 = 1;
const FORMAT_SPEC_TABLE: i64 = 2;

// R8 W4 W18.4: encoding constants mirroring `executor/vm_impl/builtins.rs`
// `decode_fstring_*` helpers. Keep these in lockstep.
const FSTRING_COLOR_NONE: i64 = -1;
const FSTRING_COLOR_NAMED: i64 = 0;
const FSTRING_COLOR_RGB: i64 = 1;

const FSTRING_FLAG_BOLD: i64 = 1;
const FSTRING_FLAG_ITALIC: i64 = 2;
const FSTRING_FLAG_UNDERLINE: i64 = 4;
const FSTRING_FLAG_DIM: i64 = 8;

fn encode_color_args(color: Option<&ColorSpec>) -> (i64, i64) {
    match color {
        None => (FSTRING_COLOR_NONE, 0),
        Some(ColorSpec::Named(named)) => {
            let id: i64 = match named {
                NamedContentColor::Red => 0,
                NamedContentColor::Green => 1,
                NamedContentColor::Blue => 2,
                NamedContentColor::Yellow => 3,
                NamedContentColor::Magenta => 4,
                NamedContentColor::Cyan => 5,
                NamedContentColor::White => 6,
                NamedContentColor::Default => 7,
            };
            (FSTRING_COLOR_NAMED, id)
        }
        Some(ColorSpec::Rgb(r, g, b)) => {
            let payload = ((*r as i64) << 16) | ((*g as i64) << 8) | (*b as i64);
            (FSTRING_COLOR_RGB, payload)
        }
    }
}

fn encode_flag_bits(spec: &ContentFormatSpec) -> i64 {
    let mut bits: i64 = 0;
    if spec.bold {
        bits |= FSTRING_FLAG_BOLD;
    }
    if spec.italic {
        bits |= FSTRING_FLAG_ITALIC;
    }
    if spec.underline {
        bits |= FSTRING_FLAG_UNDERLINE;
    }
    if spec.dim {
        bits |= FSTRING_FLAG_DIM;
    }
    bits
}

/// R8 W4 W18.4: presence-test for any `ContentStyle` arm in a parsed
/// f-string. Drives the D1 syntax-determined `string` → `content` flip:
/// if any interpolation part carries a content-styling spec, the whole
/// f-string lowers through the `ContentNode::Fragment` path and the
/// inference engine reports `content` as the return type.
pub fn has_content_style_spec(parts: &[InterpolationPart]) -> bool {
    parts.iter().any(|p| {
        matches!(
            p,
            InterpolationPart::Expression {
                format_spec: Some(InterpolationFormatSpec::ContentStyle(_)),
                ..
            }
        )
    })
}

impl BytecodeCompiler {
    fn emit_interpolation_format_call(
        &mut self,
        format_spec: Option<&InterpolationFormatSpec>,
    ) -> Result<()> {
        match format_spec {
            None => {
                // Args: [value]
                let count = self.program.add_constant(Constant::Int(1));
                self.emit(Instruction::new(
                    OpCode::PushConst,
                    Some(Operand::Const(count)),
                ));
                self.emit(Instruction::new(
                    OpCode::BuiltinCall,
                    Some(Operand::Builtin(BuiltinFunction::FormatValueWithMeta)),
                ));
            }
            Some(InterpolationFormatSpec::Fixed { precision }) => {
                // Args: [value, spec_tag, precision]
                let tag = self.program.add_constant(Constant::Int(FORMAT_SPEC_FIXED));
                self.emit(Instruction::new(
                    OpCode::PushConst,
                    Some(Operand::Const(tag)),
                ));
                let precision = self.program.add_constant(Constant::Int(*precision as i64));
                self.emit(Instruction::new(
                    OpCode::PushConst,
                    Some(Operand::Const(precision)),
                ));
                let count = self.program.add_constant(Constant::Int(3));
                self.emit(Instruction::new(
                    OpCode::PushConst,
                    Some(Operand::Const(count)),
                ));
                self.emit(Instruction::new(
                    OpCode::BuiltinCall,
                    Some(Operand::Builtin(BuiltinFunction::FormatValueWithSpec)),
                ));
            }
            Some(InterpolationFormatSpec::ContentStyle(_)) => {
                // Unreachable on the string-concat lowering path:
                // `compile_interpolated_string_expression` dispatches to
                // the content-fragment path BEFORE this is called when any
                // part has a `ContentStyle` spec. Reaching here means the
                // dispatch decision is out of sync with this match — a
                // compiler bug, not user error.
                return Err(ShapeError::RuntimeError {
                    message: "internal: ContentStyle reached \
                              string-concat emitter — \
                              compile_interpolated_string_expression \
                              dispatch is out of sync"
                        .to_string(),
                    location: None,
                });
            }
            Some(InterpolationFormatSpec::Table(spec)) => {
                // Args: [value, spec_tag, max_rows, align, precision, color, border]
                let tag = self.program.add_constant(Constant::Int(FORMAT_SPEC_TABLE));
                self.emit(Instruction::new(
                    OpCode::PushConst,
                    Some(Operand::Const(tag)),
                ));

                let max_rows = self
                    .program
                    .add_constant(Constant::Int(spec.max_rows.map(|v| v as i64).unwrap_or(-1)));
                self.emit(Instruction::new(
                    OpCode::PushConst,
                    Some(Operand::Const(max_rows)),
                ));

                let align = self.program.add_constant(Constant::Int(
                    spec.align
                        .map(|v| match v {
                            FormatAlignment::Left => 0,
                            FormatAlignment::Center => 1,
                            FormatAlignment::Right => 2,
                        })
                        .unwrap_or(-1),
                ));
                self.emit(Instruction::new(
                    OpCode::PushConst,
                    Some(Operand::Const(align)),
                ));

                let precision = self.program.add_constant(Constant::Int(
                    spec.precision.map(|v| v as i64).unwrap_or(-1),
                ));
                self.emit(Instruction::new(
                    OpCode::PushConst,
                    Some(Operand::Const(precision)),
                ));

                let color = self.program.add_constant(Constant::Int(
                    spec.color
                        .map(|v| match v {
                            FormatColor::Default => 0,
                            FormatColor::Red => 1,
                            FormatColor::Green => 2,
                            FormatColor::Yellow => 3,
                            FormatColor::Blue => 4,
                            FormatColor::Magenta => 5,
                            FormatColor::Cyan => 6,
                            FormatColor::White => 7,
                        })
                        .unwrap_or(-1),
                ));
                self.emit(Instruction::new(
                    OpCode::PushConst,
                    Some(Operand::Const(color)),
                ));

                let border = self.program.add_constant(Constant::Bool(spec.border));
                self.emit(Instruction::new(
                    OpCode::PushConst,
                    Some(Operand::Const(border)),
                ));

                let count = self.program.add_constant(Constant::Int(7));
                self.emit(Instruction::new(
                    OpCode::PushConst,
                    Some(Operand::Const(count)),
                ));
                self.emit(Instruction::new(
                    OpCode::BuiltinCall,
                    Some(Operand::Builtin(BuiltinFunction::FormatValueWithSpec)),
                ));
            }
        }

        Ok(())
    }

    /// Compile an interpolated string. Per R8 W4 W18.4 (supervisor
    /// 2026-05-24 D1 + (a-modified) REVIVE-WITH-SHARED-MODULE), the
    /// lowering shape is syntax-determined:
    ///
    /// - If NO part carries a `ContentStyle` spec: traditional
    ///   string-concat path (preserves the `string` return type used by
    ///   500+ existing call sites).
    /// - If ANY part carries a `ContentStyle` spec: lower to a
    ///   `ContentNode::Fragment` of styled/plain text nodes; return type
    ///   is `content` (`Ptr(HeapKind::Content)`).
    pub(in crate::compiler) fn compile_interpolated_string_expression(
        &mut self,
        s: &str,
        mode: InterpolationMode,
    ) -> Result<()> {
        let parts = parse_interpolation_with_mode(s, mode)?;

        if has_content_style_spec(&parts) {
            self.compile_interpolated_string_as_content(&parts)
        } else {
            self.compile_interpolated_string_as_string(parts)
        }
    }

    /// Original f-string lowering path: every part lowers to a string,
    /// concatenated via `StringConcat`. Preserved verbatim from pre-W18.4
    /// behaviour — every existing call site without styling syntax stays
    /// on this path.
    fn compile_interpolated_string_as_string(
        &mut self,
        parts: Vec<InterpolationPart>,
    ) -> Result<()> {
        if parts.is_empty() {
            // Empty string
            let const_idx = self.program.add_constant(Constant::String(String::new()));
            self.emit(Instruction::new(
                OpCode::PushConst,
                Some(Operand::Const(const_idx)),
            ));
            return Ok(());
        }

        let mut first = true;

        for part in parts {
            match part {
                InterpolationPart::Literal(text) => {
                    let const_idx = self.program.add_constant(Constant::String(text));
                    self.emit(Instruction::new(
                        OpCode::PushConst,
                        Some(Operand::Const(const_idx)),
                    ));
                }
                InterpolationPart::Expression { expr, format_spec } => {
                    // Parse the expression string
                    let expr = shape_ast::parser::parse_expression_str(&expr).map_err(|e| {
                        ShapeError::RuntimeError {
                            message: format!(
                                "Failed to parse expression '{}' in interpolation: {}",
                                expr, e
                            ),
                            location: None,
                        }
                    })?;

                    // Compile the expression
                    self.compile_expr(&expr)?;

                    // Format value using typed interpolation spec.
                    self.emit_interpolation_format_call(format_spec.as_ref())?;
                }
            }

            // Concatenate with previous result (except for first part).
            // Every interpolation part is statically a String — literal parts
            // come from `Constant::String` and expression parts go through
            // `emit_interpolation_format_call` which always produces a string.
            if !first {
                self.emit(Instruction::simple(OpCode::StringConcat));
            }
            first = false;
        }

        Ok(())
    }

    /// R8 W4 W18.4 content-fragment lowering:
    /// 1. For each plain literal part: push the string, call
    ///    `FStringContentText` → `ContentNode::Text` plain span on stack.
    /// 2. For each expression with NO styling: compile expr →
    ///    `FormatValueWithMeta` to string → `FStringContentText`.
    /// 3. For each expression WITH `ContentStyle` spec: compile expr →
    ///    `FormatValueWithMeta` to string → push encoded style args →
    ///    `FStringContentStyledText` → styled `ContentNode::Text` on stack.
    /// 4. After all parts emitted: push count → `FStringContentFragment`
    ///    → `ContentNode::Fragment` on stack.
    fn compile_interpolated_string_as_content(
        &mut self,
        parts: &[InterpolationPart],
    ) -> Result<()> {
        if parts.is_empty() {
            // Empty f-string with no parts shouldn't reach here (we only
            // dispatch when content-style is present), but be defensive:
            // emit a plain empty content text.
            self.emit_empty_content_text()?;
            return Ok(());
        }

        let part_count = parts.len();

        for part in parts {
            match part {
                InterpolationPart::Literal(text) => {
                    // Push literal string, then FStringContentText.
                    let const_idx =
                        self.program.add_constant(Constant::String(text.clone()));
                    self.emit(Instruction::new(
                        OpCode::PushConst,
                        Some(Operand::Const(const_idx)),
                    ));
                    self.emit_fstring_content_text_call()?;
                }
                InterpolationPart::Expression { expr, format_spec } => {
                    let parsed_expr =
                        shape_ast::parser::parse_expression_str(expr).map_err(|e| {
                            ShapeError::RuntimeError {
                                message: format!(
                                    "Failed to parse expression '{}' in \
                                     interpolation: {}",
                                    expr, e
                                ),
                                location: None,
                            }
                        })?;
                    self.compile_expr(&parsed_expr)?;

                    match format_spec {
                        Some(InterpolationFormatSpec::ContentStyle(spec)) => {
                            // Convert expression value to string first.
                            self.emit_format_value_with_meta()?;
                            // Then emit FStringContentStyledText with
                            // encoded style payload.
                            self.emit_fstring_content_styled_text(spec)?;
                        }
                        _ => {
                            // Plain or fixed/table-spec'd expression: route
                            // through the existing format path to a string,
                            // then wrap as plain content.
                            self.emit_interpolation_format_call(
                                format_spec.as_ref(),
                            )?;
                            self.emit_fstring_content_text_call()?;
                        }
                    }
                }
            }
        }

        // Combine all part-results into a Fragment.
        let count_idx = self
            .program
            .add_constant(Constant::Int(part_count as i64));
        self.emit(Instruction::new(
            OpCode::PushConst,
            Some(Operand::Const(count_idx)),
        ));
        self.emit(Instruction::new(
            OpCode::BuiltinCall,
            Some(Operand::Builtin(BuiltinFunction::FStringContentFragment)),
        ));

        Ok(())
    }

    fn emit_format_value_with_meta(&mut self) -> Result<()> {
        let count = self.program.add_constant(Constant::Int(1));
        self.emit(Instruction::new(
            OpCode::PushConst,
            Some(Operand::Const(count)),
        ));
        self.emit(Instruction::new(
            OpCode::BuiltinCall,
            Some(Operand::Builtin(BuiltinFunction::FormatValueWithMeta)),
        ));
        Ok(())
    }

    fn emit_fstring_content_text_call(&mut self) -> Result<()> {
        let count = self.program.add_constant(Constant::Int(1));
        self.emit(Instruction::new(
            OpCode::PushConst,
            Some(Operand::Const(count)),
        ));
        self.emit(Instruction::new(
            OpCode::BuiltinCall,
            Some(Operand::Builtin(BuiltinFunction::FStringContentText)),
        ));
        Ok(())
    }

    fn emit_fstring_content_styled_text(
        &mut self,
        spec: &ContentFormatSpec,
    ) -> Result<()> {
        // Stack on entry: [value_str]. Push 5 i64 style args, then 6 as
        // arg-count, then BuiltinCall.
        let (fg_kind, fg_payload) = encode_color_args(spec.fg.as_ref());
        let (bg_kind, bg_payload) = encode_color_args(spec.bg.as_ref());
        let flags = encode_flag_bits(spec);

        for v in [fg_kind, fg_payload, bg_kind, bg_payload, flags] {
            let idx = self.program.add_constant(Constant::Int(v));
            self.emit(Instruction::new(
                OpCode::PushConst,
                Some(Operand::Const(idx)),
            ));
        }
        let count_idx = self.program.add_constant(Constant::Int(6));
        self.emit(Instruction::new(
            OpCode::PushConst,
            Some(Operand::Const(count_idx)),
        ));
        self.emit(Instruction::new(
            OpCode::BuiltinCall,
            Some(Operand::Builtin(BuiltinFunction::FStringContentStyledText)),
        ));
        Ok(())
    }

    fn emit_empty_content_text(&mut self) -> Result<()> {
        let const_idx = self.program.add_constant(Constant::String(String::new()));
        self.emit(Instruction::new(
            OpCode::PushConst,
            Some(Operand::Const(const_idx)),
        ));
        self.emit_fstring_content_text_call()
    }

}

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

    fn parse_braces(s: &str) -> shape_ast::error::Result<Vec<InterpolationPart>> {
        parse_interpolation_with_mode(s, InterpolationMode::Braces)
    }

    #[test]
    fn test_no_interpolation() {
        let parts = parse_braces("Hello World").unwrap();
        assert_eq!(parts.len(), 1);
        assert!(matches!(&parts[0], InterpolationPart::Literal(s) if s == "Hello World"));
    }

    #[test]
    fn test_simple_interpolation() {
        let parts = parse_braces("value: {x}").unwrap();
        assert_eq!(parts.len(), 2);
        assert!(matches!(&parts[0], InterpolationPart::Literal(s) if s == "value: "));
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "x"
        ));
    }

    #[test]
    fn test_expression_interpolation() {
        let parts = parse_braces("sum: {x + y}").unwrap();
        assert_eq!(parts.len(), 2);
        assert!(matches!(&parts[0], InterpolationPart::Literal(s) if s == "sum: "));
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "x + y"
        ));
    }

    #[test]
    fn test_multiple_interpolations() {
        let parts = parse_braces("a={a}, b={b}").unwrap();
        assert_eq!(parts.len(), 4);
        assert!(matches!(&parts[0], InterpolationPart::Literal(s) if s == "a="));
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "a"
        ));
        assert!(matches!(&parts[2], InterpolationPart::Literal(s) if s == ", b="));
        assert!(matches!(
            &parts[3],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "b"
        ));
    }

    #[test]
    fn test_escaped_braces() {
        let parts = parse_braces("Use {{x}} for literal").unwrap();
        assert_eq!(parts.len(), 1);
        assert!(matches!(&parts[0], InterpolationPart::Literal(s) if s == "Use {x} for literal"));
    }

    #[test]
    fn test_as_type_in_interpolation() {
        let parts = parse_braces("{x as Percent}").unwrap();
        assert_eq!(parts.len(), 1);
        assert!(matches!(
            &parts[0],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "x as Percent"
        ));
    }

    #[test]
    fn test_nested_braces_in_object() {
        let parts = parse_braces("obj: {x.method({a: 1})}").unwrap();
        assert_eq!(parts.len(), 2);
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "x.method({a: 1})"
        ));
    }

    #[test]
    fn test_interpolation_with_format_spec() {
        let parts = parse_braces("px={price:fixed(2)}").unwrap();
        assert_eq!(parts.len(), 2);
        assert!(matches!(&parts[0], InterpolationPart::Literal(s) if s == "px="));
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: Some(spec)
            } if expr == "price"
                && *spec == InterpolationFormatSpec::Fixed { precision: 2 }
        ));
    }

    #[test]
    fn test_interpolation_does_not_split_double_colon() {
        let parts = parse_braces("{Type::Variant}").unwrap();
        assert_eq!(parts.len(), 1);
        assert!(matches!(
            &parts[0],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "Type::Variant"
        ));
    }

    #[test]
    fn test_missing_format_spec_error() {
        let result = parse_braces("value: {x:}");
        assert!(result.is_err());
    }

    #[test]
    fn test_unmatched_close_brace_error() {
        let result = parse_braces("value: }");
        assert!(result.is_err());
    }

    #[test]
    fn test_has_interpolation() {
        assert!(has_interpolation_with_mode(
            "value: {x}",
            InterpolationMode::Braces
        ));
        assert!(has_interpolation_with_mode(
            "{x + y}",
            InterpolationMode::Braces
        ));
        assert!(!has_interpolation_with_mode(
            "Hello World",
            InterpolationMode::Braces
        ));
        assert!(!has_interpolation_with_mode(
            "Use {{x}} for literal",
            InterpolationMode::Braces
        )); // Escaped, no real interpolation
    }

    #[test]
    fn test_empty_interpolation_error() {
        let result = parse_braces("value: {}");
        assert!(result.is_err());
    }

    #[test]
    fn test_dollar_mode_interpolation() {
        let parts =
            parse_interpolation_with_mode("{\"name\": ${user.name}}", InterpolationMode::Dollar)
                .unwrap();
        assert_eq!(parts.len(), 3);
        assert!(matches!(
            &parts[0],
            InterpolationPart::Literal(s) if s == "{\"name\": "
        ));
        assert!(matches!(
            &parts[1],
            InterpolationPart::Expression {
                expr,
                format_spec: None
            } if expr == "user.name"
        ));
        assert!(matches!(&parts[2], InterpolationPart::Literal(s) if s == "}"));
    }
}