shape-vm 0.3.1

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
//! Extend Block UFCS Execution Tests
//!
//! These tests verify the complete extend block execution including:
//! - Basic method extension on built-in types
//! - Generic type extension (Vec<T>)
//! - Multiple methods in one extend block
//! - `self` binding correctness
//! - UFCS (Uniform Function Call Syntax) dispatch

use crate::compiler::BytecodeCompiler;
use crate::executor::VirtualMachine;
use crate::{VMConfig, VMError};
use shape_ast::parser::parse_program;
use shape_value::{ValueWord, ValueWordExt};

/// Extract a numeric value from ValueWord, accepting both Number and Int variants
fn as_f64(v: &ValueWord) -> Option<f64> {
    v.as_number_coerce()
}

/// Helper to compile and execute a Shape program
fn compile_and_execute(source: &str) -> Result<ValueWord, VMError> {
    // Parse the program
    let program = parse_program(source).map_err(|e| VMError::RuntimeError(format!("{:?}", e)))?;

    // Compile to bytecode
    let mut compiler = BytecodeCompiler::new();
    compiler.set_source(source);
    let bytecode = compiler
        .compile(&program)
        .map_err(|e| VMError::RuntimeError(format!("{:?}", e)))?;

    // Execute
    let mut vm = VirtualMachine::new(VMConfig::default());
    vm.load_program(bytecode);
    vm.execute(None).map(|nb| nb.clone())
}

#[test]
fn test_extend_number_basic() {
    // Test: extend Number { method double() { self * 2 } } → (5).double() = 10
    let source = r#"
        extend Number {
            method double() {
                return self * 2
            }
        }

        (5).double()
    "#;

    let result = compile_and_execute(source);
    assert!(
        result.is_ok(),
        "Basic Number extension should work: {:?}",
        result.err()
    );

    let val = result.unwrap();
    assert_eq!(
        as_f64(&val),
        Some(10.0),
        "5.double() should return 10, got {:?}",
        val
    );
}

#[test]
fn test_extend_number_with_param() {
    // Test that extend methods can take parameters
    let source = r#"
        extend Number {
            method add(n) {
                return self + n
            }
        }

        (5).add(3)
    "#;

    let result = compile_and_execute(source);
    assert!(
        result.is_ok(),
        "Number extension with param should work: {:?}",
        result.err()
    );

    let val = result.unwrap();
    assert_eq!(
        as_f64(&val),
        Some(8.0),
        "5.add(3) should return 8, got {:?}",
        val
    );
}

#[test]
fn test_extend_number_multiple_methods() {
    // Test: Extend with multiple methods, verify all callable via UFCS
    let source = r#"
        extend Number {
            method double() {
                return self * 2
            }

            method triple() {
                return self * 3
            }

            method square() {
                return self * self
            }
        }

        let x = 5;
        let doubled = x.double();
        let tripled = x.triple();
        let squared = x.square();

        [doubled, tripled, squared]
    "#;

    let result = compile_and_execute(source);
    assert!(
        result.is_ok(),
        "Multiple methods should work: {:?}",
        result.err()
    );

    let binding = result.unwrap();
    let arr = binding.to_array_arc().expect("Expected Vec");
    assert_eq!(arr.len(), 3, "Should have 3 results");

    assert_eq!(
        as_f64(&arr[0]),
        Some(10.0),
        "double() should return 10, got {:?}",
        arr[0]
    );
    assert_eq!(
        as_f64(&arr[1]),
        Some(15.0),
        "triple() should return 15, got {:?}",
        arr[1]
    );
    assert_eq!(
        as_f64(&arr[2]),
        Some(25.0),
        "square() should return 25, got {:?}",
        arr[2]
    );
}

#[test]
fn test_extend_string_basic() {
    // Test extending String type
    let source = r#"
        extend String {
            method repeat(times) {
                var result = "";
                var i = 0;
                while (i < times) {
                    result = result + self;
                    i = i + 1;
                }
                return result
            }
        }

        "hi".repeat(3)
    "#;

    let result = compile_and_execute(source);
    assert!(
        result.is_ok(),
        "String extension should work: {:?}",
        result.err()
    );

    let val = result.unwrap();
    let s = val.as_arc_string().expect("Expected String");
    assert_eq!(s.as_ref(), "hihihi", "Should repeat 3 times");
}

#[test]
fn test_extend_array_basic() {
    // Test extending Vec type
    let source = r#"
        extend Vec {
            method sum() {
                var total = 0;
                var i = 0;
                while (i < self.length()) {
                    total = total + self[i];
                    i = i + 1;
                }
                return total
            }
        }

        [1, 2, 3, 4, 5].sum()
    "#;

    let result = compile_and_execute(source);
    assert!(
        result.is_ok(),
        "Vec extension should work: {:?}",
        result.err()
    );

    let val = result.unwrap();
    assert_eq!(
        as_f64(&val),
        Some(15.0),
        "Sum of [1,2,3,4,5] should be 15, got {:?}",
        val
    );
}

#[test]
fn test_extend_array_generic() {
    // Test: extend Vec<T> { method ... } → verify generic types
    // Note: This tests that Vec methods work regardless of element type
    let source = r#"
        extend Vec {
            method first() {
                if (self.length() > 0) {
                    return self[0]
                }
                return None
            }

            method last() {
                let len = self.length();
                if (len > 0) {
                    return self[len - 1]
                }
                return None
            }
        }

        // Test with number array
        let nums = [10, 20, 30];
        let num_first = nums.first();
        let num_last = nums.last();

        // Test with string array
        let strings = ["a", "b", "c"];
        let str_first = strings.first();
        let str_last = strings.last();

        [num_first, num_last, str_first, str_last]
    "#;

    let result = compile_and_execute(source);
    assert!(
        result.is_ok(),
        "Generic Vec extension should work: {:?}",
        result.err()
    );

    let binding = result.unwrap();
    let arr = binding.to_array_arc().expect("Expected Vec");
    assert_eq!(arr.len(), 4, "Should have 4 results");

    assert_eq!(
        as_f64(&arr[0]),
        Some(10.0),
        "First number should be 10, got {:?}",
        arr[0]
    );
    assert_eq!(
        as_f64(&arr[1]),
        Some(30.0),
        "Last number should be 30, got {:?}",
        arr[1]
    );

    let s2 = arr[2].as_arc_string().expect("Expected String");
    assert_eq!(s2.as_ref(), "a", "First string should be 'a'");

    let s3 = arr[3].as_arc_string().expect("Expected String");
    assert_eq!(s3.as_ref(), "c", "Last string should be 'c'");
}

#[test]
fn test_extend_this_binding_in_nested_context() {
    // Test: `self` binding correctness in various contexts
    let source = r#"
        extend Number {
            method add_and_multiply(a, b) {
                // `self` should refer to the number, not get confused
                // even when used in nested expressions
                let sum = self + a;
                let product = sum * b;
                return product
            }
        }

        (10).add_and_multiply(5, 2)
    "#;

    let result = compile_and_execute(source);
    assert!(
        result.is_ok(),
        "Complex self binding should work: {:?}",
        result.err()
    );

    let val = result.unwrap();
    assert_eq!(
        as_f64(&val),
        Some(30.0),
        "Should get (10 + 5) * 2 = 30, got {:?}",
        val
    );
}

#[test]
fn test_extend_this_in_closure() {
    // Test that `self` is correctly bound when methods use loops referencing self
    let source = r#"
        extend Vec {
            method double_all() {
                var result = [];
                var i = 0;
                while (i < self.length()) {
                    result = result.push(self[i] * 2);
                    i = i + 1;
                }
                return result
            }
        }

        [1, 2, 3].double_all()
    "#;

    let result = compile_and_execute(source);
    assert!(
        result.is_ok(),
        "This in closure context should work: {:?}",
        result.err()
    );

    let binding = result.unwrap();
    let arr = binding.to_array_arc().expect("Expected Vec");
    assert_eq!(arr.len(), 3, "Should have 3 elements");

    assert_eq!(as_f64(&arr[0]), Some(2.0), "Expected 2, got {:?}", arr[0]);
    assert_eq!(as_f64(&arr[1]), Some(4.0), "Expected 4, got {:?}", arr[1]);
    assert_eq!(as_f64(&arr[2]), Some(6.0), "Expected 6, got {:?}", arr[2]);
}

#[test]
fn test_extend_chained_method_calls() {
    // Test that extended methods can be chained
    let source = r#"
        extend Number {
            method add(n) {
                return self + n
            }

            method multiply(n) {
                return self * n
            }
        }

        (5).add(3).multiply(2)
    "#;

    let result = compile_and_execute(source);
    assert!(
        result.is_ok(),
        "Chained method calls should work: {:?}",
        result.err()
    );

    let val = result.unwrap();
    assert_eq!(
        as_f64(&val),
        Some(16.0),
        "Should get (5 + 3) * 2 = 16, got {:?}",
        val
    );
}

#[test]
fn test_extend_method_with_default_param() {
    // Test that extended methods can use default parameters
    let source = r#"
        extend Number {
            method power(exponent = 2) {
                var result = 1;
                var i = 0;
                while (i < exponent) {
                    result = result * self;
                    i = i + 1;
                }
                return result
            }
        }

        // Call with explicit param
        let with_3 = (5).power(3);

        // Call with default param (should be 2)
        let with_default = (5).power();

        [with_3, with_default]
    "#;

    let result = compile_and_execute(source);
    assert!(
        result.is_ok(),
        "Methods with default params should work: {:?}",
        result.err()
    );

    let binding = result.unwrap();
    let arr = binding.to_array_arc().expect("Expected Vec");
    assert_eq!(arr.len(), 2, "Should have 2 results");

    assert_eq!(
        as_f64(&arr[0]),
        Some(125.0),
        "5^3 should be 125, got {:?}",
        arr[0]
    );
    assert_eq!(
        as_f64(&arr[1]),
        Some(25.0),
        "5^2 should be 25, got {:?}",
        arr[1]
    );
}

#[test]
fn test_extend_multiple_types() {
    // Test that we can extend multiple types in the same program
    let source = r#"
        extend Number {
            method negate() {
                return -self
            }
        }

        extend String {
            method upper() {
                // Note: Actual toUpperCase() might not be implemented yet
                // This is a simple test placeholder
                return self
            }
        }

        extend Vec {
            method count() {
                return self.length()
            }
        }

        let num = (42).negate();
        let str = "hello".upper();
        let cnt = [1, 2, 3].count();

        [num, str, cnt]
    "#;

    let result = compile_and_execute(source);
    assert!(
        result.is_ok(),
        "Multiple type extensions should work: {:?}",
        result.err()
    );

    let binding = result.unwrap();
    let arr = binding.to_array_arc().expect("Expected Vec");
    assert_eq!(arr.len(), 3, "Should have 3 results");

    assert_eq!(
        as_f64(&arr[0]),
        Some(-42.0),
        "Should negate to -42, got {:?}",
        arr[0]
    );

    let s = arr[1].as_arc_string().expect("Expected String");
    assert_eq!(s.as_ref(), "hello");

    assert_eq!(
        as_f64(&arr[2]),
        Some(3.0),
        "Should count to 3, got {:?}",
        arr[2]
    );
}