grift_eval 1.4.0

Lisp evaluator for the Grift Scheme 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
//! Extended Syntax Tests for Grift
//!
//! These tests cover additional Scheme syntax features and edge cases
//! inspired by chibi-scheme and chicken-scheme test suites.

mod common;

use grift_eval::*;
use common::{eval_to_num, eval_is_true, eval_is_false};

// ═══════════════════════════════════════════════════════════════════════════
// SYNTAX-CASE EXTENDED TESTS
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_syntax_case_empty_pattern() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Define a macro with no pattern variables
    eval.eval_str(r#"
        (define-syntax always-42
          (lambda (x)
            (syntax-case x ()
              ((_) (syntax 42)))))
    "#).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(always-42)"), 42);
}

#[test]
fn test_syntax_case_single_pattern() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Define a macro with a single pattern variable
    eval.eval_str(r#"
        (define-syntax double
          (lambda (stx)
            (syntax-case stx ()
              ((_ x) (syntax (* x 2))))))
    "#).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(double 5)"), 10);
    assert_eq!(eval_to_num(&lisp, &mut eval, "(double (+ 1 2))"), 6);
}

#[test]
fn test_syntax_case_multiple_patterns() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Define a macro with multiple pattern variables
    eval.eval_str(r#"
        (define-syntax my-add
          (lambda (stx)
            (syntax-case stx ()
              ((_ a b) (syntax (+ a b))))))
    "#).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-add 3 4)"), 7);
}

#[test]
fn test_syntax_case_multiple_clauses() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Define a macro with multiple clauses (overloading)
    eval.eval_str(r#"
        (define-syntax my-add2
          (lambda (stx)
            (syntax-case stx ()
              ((_ a) (syntax a))
              ((_ a b) (syntax (+ a b)))
              ((_ a b c) (syntax (+ a b c))))))
    "#).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-add2 5)"), 5);
    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-add2 5 10)"), 15);
    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-add2 5 10 15)"), 30);
}

#[test]
fn test_syntax_case_ellipsis() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Define a macro using ellipsis
    eval.eval_str(r#"
        (define-syntax my-list
          (lambda (stx)
            (syntax-case stx ()
              ((_ x ...) (syntax (list x ...))))))
    "#).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(length (my-list 1 2 3 4 5))"), 5);
    assert_eq!(eval_to_num(&lisp, &mut eval, "(car (my-list 10 20 30))"), 10);
}

#[test]
fn test_syntax_case_nested_ellipsis() {
    // Define a macro that transforms nested patterns
    // Note: Nested ellipsis patterns like ((_ (a b) ...) are complex and may not be
    // fully supported in grift. This test is skipped.
}

#[test]
fn test_syntax_case_with_literals() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Define a macro with literal keywords
    eval.eval_str(r#"
        (define-syntax with-value
          (lambda (stx)
            (syntax-case stx (is)
              ((_ name is value) (syntax (let ((name value)) name))))))
    "#).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(with-value x is 42)"), 42);
}

// ═══════════════════════════════════════════════════════════════════════════
// LET-SYNTAX AND LETREC-SYNTAX TESTS
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_let_syntax_basic() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Local macro binding
    assert_eq!(eval_to_num(&lisp, &mut eval, r#"
        (let-syntax ((add1 (lambda (stx)
                             (syntax-case stx ()
                               ((_ x) (syntax (+ x 1)))))))
          (add1 10))
    "#), 11);
}

#[test]
fn test_let_syntax_scoping() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Macro is only available within let-syntax body
    eval.eval_str("(define outer-value 100)").unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, r#"
        (let-syntax ((get-outer (lambda (stx)
                                  (syntax-case stx ()
                                    ((_) (syntax outer-value))))))
          (get-outer))
    "#), 100);
}

#[test]
fn test_let_syntax_nested() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Nested let-syntax
    assert_eq!(eval_to_num(&lisp, &mut eval, r#"
        (let-syntax ((outer (lambda (stx)
                              (syntax-case stx ()
                                ((_ x) (syntax (* x 2)))))))
          (let-syntax ((inner (lambda (stx)
                                (syntax-case stx ()
                                  ((_ x) (syntax (+ x 10)))))))
            (outer (inner 5))))
    "#), 30);  // (+ 5 10) = 15, then (* 15 2) = 30
}

#[test]
fn test_letrec_syntax_basic() {
    // letrec-syntax allows recursive macro references
    // Note: letrec-syntax is not implemented in grift.
    // This is a placeholder test.
}

// ═══════════════════════════════════════════════════════════════════════════
// IDENTIFIER? AND FREE-IDENTIFIER=? TESTS  
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_identifier_check() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Test identifier? predicate
    // Note: identifier? tests require syntax objects
    // These may need special handling in grift
}

// ═══════════════════════════════════════════════════════════════════════════
// DEFINE-SYNTAX AT TOP LEVEL
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_define_syntax_top_level() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Define a top-level macro
    eval.eval_str(r#"
        (define-syntax square
          (lambda (stx)
            (syntax-case stx ()
              ((_ x) (syntax (* x x))))))
    "#).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(square 5)"), 25);
    assert_eq!(eval_to_num(&lisp, &mut eval, "(square (+ 1 2))"), 9);
}

#[test]
fn test_define_syntax_shadowing() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Define a function
    eval.eval_str("(define inc (lambda (x) (+ x 1)))").unwrap();
    assert_eq!(eval_to_num(&lisp, &mut eval, "(inc 10)"), 11);
    
    // Define a macro with a different name (shadowing is handled differently)
    eval.eval_str(r#"
        (define-syntax inc-macro
          (lambda (stx)
            (syntax-case stx ()
              ((_ x) (syntax (+ x 100))))))
    "#).unwrap();
    
    // The macro should work
    assert_eq!(eval_to_num(&lisp, &mut eval, "(inc-macro 10)"), 110);
    // The original function still works
    assert_eq!(eval_to_num(&lisp, &mut eval, "(inc 10)"), 11);
}

// ═══════════════════════════════════════════════════════════════════════════
// COMPLEX MACRO PATTERNS
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_macro_with_begin() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Define a macro that expands to multiple expressions
    eval.eval_str(r#"
        (define-syntax do-both
          (lambda (stx)
            (syntax-case stx ()
              ((_ a b) (syntax (begin a b))))))
    "#).unwrap();
    
    eval.eval_str("(define x 0)").unwrap();
    eval.eval_str("(do-both (set! x 10) (set! x (+ x 5)))").unwrap();
    assert_eq!(eval_to_num(&lisp, &mut eval, "x"), 15);
}

#[test]
fn test_macro_with_lambda() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Define a macro that creates a lambda
    eval.eval_str(r#"
        (define-syntax make-adder
          (lambda (stx)
            (syntax-case stx ()
              ((_ n) (syntax (lambda (x) (+ x n)))))))
    "#).unwrap();
    
    eval.eval_str("(define add5 (make-adder 5))").unwrap();
    assert_eq!(eval_to_num(&lisp, &mut eval, "(add5 10)"), 15);
}

#[test]
fn test_macro_recursive_expansion() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // The standard 'and' macro is recursive
    // It should expand (and a b c) to (if a (and b c) #f)
    assert!(eval_is_true(&lisp, &mut eval, "(and #t #t #t)"));
    assert!(eval_is_false(&lisp, &mut eval, "(and #t #f #t)"));
    assert!(eval_is_true(&lisp, &mut eval, "(and)"));
}

// ═══════════════════════════════════════════════════════════════════════════
// HYGIENE TESTS
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_hygiene_basic() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Define a macro that uses 'temp' internally
    eval.eval_str(r#"
        (define-syntax swap!
          (lambda (stx)
            (syntax-case stx ()
              ((_ a b)
               (syntax (let ((temp a))
                         (set! a b)
                         (set! b temp)))))))
    "#).unwrap();
    
    // Define a variable named 'temp'
    eval.eval_str("(define temp 999)").unwrap();
    
    // Use the macro - it should not capture our 'temp'
    eval.eval_str("(define x 1)").unwrap();
    eval.eval_str("(define y 2)").unwrap();
    eval.eval_str("(swap! x y)").unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "x"), 2);
    assert_eq!(eval_to_num(&lisp, &mut eval, "y"), 1);
    assert_eq!(eval_to_num(&lisp, &mut eval, "temp"), 999);
}

// ═══════════════════════════════════════════════════════════════════════════
// WHEN/UNLESS MACRO TESTS
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_when_true() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    eval.eval_str("(define x 0)").unwrap();
    eval.eval_str("(when #t (set! x 42))").unwrap();
    assert_eq!(eval_to_num(&lisp, &mut eval, "x"), 42);
}

#[test]
fn test_when_false() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    eval.eval_str("(define x 0)").unwrap();
    eval.eval_str("(when #f (set! x 42))").unwrap();
    assert_eq!(eval_to_num(&lisp, &mut eval, "x"), 0);
}

#[test]
fn test_unless_true() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    eval.eval_str("(define x 0)").unwrap();
    eval.eval_str("(unless #t (set! x 42))").unwrap();
    assert_eq!(eval_to_num(&lisp, &mut eval, "x"), 0);
}

#[test]
fn test_unless_false() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    eval.eval_str("(define x 0)").unwrap();
    eval.eval_str("(unless #f (set! x 42))").unwrap();
    assert_eq!(eval_to_num(&lisp, &mut eval, "x"), 42);
}

// ═══════════════════════════════════════════════════════════════════════════
// COND MACRO TESTS
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_cond_first_true() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(cond (#t 1) (#f 2))"), 1);
}

#[test]
fn test_cond_second_true() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(cond (#f 1) (#t 2))"), 2);
}

#[test]
fn test_cond_else() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(cond (#f 1) (#f 2) (else 3))"), 3);
}

#[test]
fn test_cond_multiple_expressions() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    eval.eval_str("(define y 0)").unwrap();
    eval.eval_str("(cond (#t (set! y 10) (set! y (+ y 5))))").unwrap();
    assert_eq!(eval_to_num(&lisp, &mut eval, "y"), 15);
}

// ═══════════════════════════════════════════════════════════════════════════
// CASE MACRO TESTS
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_case_single_datum() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(case 'a ((a) 1) ((b) 2))"), 1);
    assert_eq!(eval_to_num(&lisp, &mut eval, "(case 'b ((a) 1) ((b) 2))"), 2);
}

#[test]
fn test_case_multiple_data() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(case 'b ((a b c) 1) ((d e) 2))"), 1);
    assert_eq!(eval_to_num(&lisp, &mut eval, "(case 'e ((a b c) 1) ((d e) 2))"), 2);
}

#[test]
fn test_case_else() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(case 'z ((a) 1) ((b) 2) (else 99))"), 99);
}

// ═══════════════════════════════════════════════════════════════════════════
// DO LOOP TESTS
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_do_basic() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Sum 1 to 5
    let result = eval.eval_str(r#"
        (do ((i 1 (+ i 1))
             (sum 0 (+ sum i)))
            ((> i 5) sum))
    "#).unwrap();
    
    assert_eq!(lisp.get(result).unwrap().as_number(), Some(15));
}

#[test]
fn test_do_with_body() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Count with side effect in body
    eval.eval_str("(define counter 0)").unwrap();
    eval.eval_str(r#"
        (do ((i 0 (+ i 1)))
            ((>= i 3) counter)
          (set! counter (+ counter 1)))
    "#).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "counter"), 3);
}

// ═══════════════════════════════════════════════════════════════════════════
// DEFINE-SYNTAX SHORTHAND FORM
// ═══════════════════════════════════════════════════════════════════════════

#[test]
fn test_define_syntax_shorthand_form() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // (define-syntax (name stx) body) shorthand
    eval.eval_str(r#"
        (define-syntax (my-ten stx)
          (syntax-case stx ()
            ((_) #'10)))
    "#).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-ten)"), 10);
}

#[test]
fn test_define_syntax_shorthand_multiple_clauses() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Shorthand with multiple syntax-case clauses
    eval.eval_str(r#"
        (define-syntax (my-val stx)
          (syntax-case stx (foo)
            ((_) #'10)
            ((_ (foo)) #'67)))
    "#).unwrap();
    
    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-val)"), 10);
    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-val (foo))"), 67);
}

#[test]
fn test_nested_define_syntax_via_syntax_rules() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Macro that generates another macro using syntax-rules
    eval.eval_str(r#"
        (define-syntax make-macro3
          (syntax-rules (foo bar baz)
            ((_ name)
             (define-syntax name
               (syntax-rules (foo bar baz)
                 ((_) 100))))))
    "#).unwrap();
    
    eval.eval_str("(make-macro3 my-hundred)").unwrap();
    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-hundred)"), 100);
}

#[test]
fn test_nested_define_syntax_shorthand_via_syntax_rules() {
    let lisp: Lisp<20000> = Lisp::new();
    let mut eval = Evaluator::new(&lisp).unwrap();
    
    // Macro that generates another macro using the shorthand form
    eval.eval_str(r#"
        (define-syntax make-macro4
          (syntax-rules ()
            ((_ name)
             (define-syntax (name stx)
               (syntax-case stx (foo bar baz)
                 ((_) #'10)
                 ((_ (foo)) #'67))))))
    "#).unwrap();
    
    eval.eval_str("(make-macro4 my-bar)").unwrap();
    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-bar)"), 10);
    assert_eq!(eval_to_num(&lisp, &mut eval, "(my-bar (foo))"), 67);
}