| 1 | module scheme |
| 2 | |
| 3 | fn star_check(ok bool, message string) { |
| 4 | if !ok { panic(message) } |
| 5 | } |
| 6 | |
| 7 | fn star_eval(mut vm VM, source string) Value { |
| 8 | return vm.eval(source) or { panic('${source}: ${err}') } |
| 9 | } |
| 10 | |
| 11 | fn star_written(mut vm VM, source string) string { |
| 12 | return write(star_eval(mut vm, source)) or { panic(err) } |
| 13 | } |
| 14 | |
| 15 | fn test_lambda_star_optional_keyword_rest_and_duplicate_last_wins() { |
| 16 | mut vm := new() |
| 17 | star_check(star_written(mut vm, '(define f (lambda* (a #:optional (b 20) #:key (c 30) d #:rest r) |
| 18 | (list a b c d r))) (list (f 1) (f 1 2) (f 1 #:c 3) |
| 19 | (f 1 2 #:c 3 #:d 4) (f 1 #:d 4 #:c 3) |
| 20 | (f 1 #:c 3 9) (f 1 #:c 3 #:c 4))') == |
| 21 | '((1 20 30 #f ()) (1 2 30 #f ()) (1 20 3 #f (#:c 3)) (1 2 3 4 (#:c 3 #:d 4)) (1 20 3 4 (#:d 4 #:c 3)) (1 20 3 #f (#:c 3 9)) (1 20 4 #f (#:c 3 #:c 4)))', |
| 22 | 'Guile 3.0.11 extended formal matrix') |
| 23 | } |
| 24 | |
| 25 | fn test_lambda_star_defaults_are_lazy_and_see_prior_bindings() { |
| 26 | mut vm := new() |
| 27 | actual := star_written(mut vm, '(define count 0) |
| 28 | (define f (lambda* (a #:optional (b (begin (set! count (+ count 1)) (+ a 10))) |
| 29 | #:key (c (begin (set! count (+ count 1)) (+ b 20))) #:rest r) |
| 30 | (list a b c r count))) |
| 31 | (list (f 1) (f 1 2 #:c 3) count)') |
| 32 | star_check(actual == '((1 11 31 () 2) (1 2 3 (#:c 3) 2) 2)', actual) |
| 33 | } |
| 34 | |
| 35 | fn test_lambda_star_rejects_bad_keyword_packets_without_poisoning_vm() { |
| 36 | mut vm := new() |
| 37 | for source in ['((lambda* (a #:key b) b) 1 #:x 3)', '((lambda* (a #:key b) b) 1 #:b)', |
| 38 | '((lambda* (a #:key b) b) 1 9)', '((lambda* (a #:key b) b) 1 #:b 2 9)', |
| 39 | '((lambda* (a #:optional b) b) 1 2 3)', '((lambda* (a #:optional b) b))'] { |
| 40 | if _ := vm.eval(source) { panic('accepted invalid lambda* call ${source}') } |
| 41 | star_check(star_eval(mut vm, '(+ 20 22)').whole() == 42, 'VM poisoned') |
| 42 | } |
| 43 | star_check(star_written(mut vm, |
| 44 | '((lambda* (a #:key b #:allow-other-keys #:rest r) (list a b r)) 1 #:x 5)') == |
| 45 | '(1 #f (#:x 5))', 'allow-other-keys') |
| 46 | } |
| 47 | |
| 48 | fn test_lambda_star_rest_and_tail_calls() { |
| 49 | mut vm := new() |
| 50 | actual := star_written(mut vm, '(define walk |
| 51 | (lambda* (n #:optional (acc 0) #:rest rest) |
| 52 | (if (= n 0) (list acc rest) (walk (- n 1) (+ acc 1))))) |
| 53 | (walk 12000)') |
| 54 | star_check(actual == '(12000 ())', actual) |
| 55 | } |
| 56 | |
| 57 | fn test_lambda_star_rejects_malformed_formal_lists() { |
| 58 | mut vm := new() |
| 59 | for source in ['(lambda* (#:rest) 1)', '(lambda* (a #:rest a) 1)', '(lambda* (a #:rest r b) 1)', |
| 60 | '(lambda* (a #:key (b 1 2)) b)', '(lambda* (a #:optional b #:optional c) b)', |
| 61 | '(lambda* (a #:allow-other-keys) a)', '(lambda* (a #:key b #:allow-other-keys c) b)'] { |
| 62 | if _ := vm.eval(source) { panic('accepted malformed lambda* ${source}') } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn test_define_star_and_r7_companion_library() { |
| 67 | mut vm := new() |
| 68 | actual := star_written(mut vm, '(import (prefix (vile optargs) o:)) |
| 69 | (o:define* (f a #:optional (b (+ a 1)) #:key (c (+ b 1))) |
| 70 | (list a b c)) |
| 71 | (list (f 2) (f 2 8 #:c 9) ((o:lambda* (#:key (x 5)) x)))') |
| 72 | star_check(actual == '((2 3 4) (2 8 9) 5)', actual) |
| 73 | } |
| 74 | |
| 75 | fn test_lambda_star_rest_is_visible_to_keyword_defaults() { |
| 76 | mut vm := new() |
| 77 | actual := star_written(mut vm, |
| 78 | '((lambda* (#:key (x (length r)) #:allow-other-keys #:rest r) (list x r)) #:other 5)') |
| 79 | star_check(actual == '(2 (#:other 5))', actual) |
| 80 | } |
| 81 | |
| 82 | fn test_alt_function_literals_and_named_extended_definitions() { |
| 83 | mut vm := new() |
| 84 | value := vm.eval_alt('f(a, #:optional, (b, 5)) := a + b |
| 85 | g := fn(a, #:key, (b, 7)) { a + b } |
| 86 | [f(3), f(3, 4), g(2), g(2, #:b, 9)]') or { panic(err) } |
| 87 | star_check(write(value) or { panic(err) } == '#(8 7 9 11)', 'Vile extended formals') |
| 88 | } |
| 89 | |
| 90 | fn test_lambda_star_default_scope_and_early_return() { |
| 91 | mut vm := new() |
| 92 | actual := star_written(mut vm, '(let ((x 7)) |
| 93 | (list ((lambda* (#:optional (x x)) x)) |
| 94 | ((lambda* (#:optional (x (return 9))) x)) |
| 95 | ((lambda* (#:optional (x (return 9))) x) 3)))') |
| 96 | star_check(actual == '(7 9 3)', actual) |
| 97 | } |
| 98 | |
| 99 | fn test_lambda_star_default_continuation_can_resume() { |
| 100 | mut vm := new() |
| 101 | star_eval(mut vm, '(define saved #f) |
| 102 | (define f (lambda* (#:optional (x (call/cc (lambda (k) (set! saved k) 2)))) x))') |
| 103 | star_check(star_eval(mut vm, '(f)').whole() == 2, 'initial default continuation') |
| 104 | star_check(star_eval(mut vm, '(saved 3)').whole() == 3, 'resumed default continuation') |
| 105 | star_check(star_eval(mut vm, '(saved 4)').whole() == 4, 'multishot default continuation') |
| 106 | } |
| 107 | |
| 108 | fn test_lambda_star_rejects_unknown_keyword_with_rest() { |
| 109 | mut vm := new() |
| 110 | if _ := vm.eval('((lambda* (#:key x #:rest r) r) #:unknown 3)') { |
| 111 | panic('rest accepted unknown keyword without allow-other-keys') |
| 112 | } |
| 113 | star_check(star_written(mut vm, '((lambda* (#:key x #:rest r) r) #:x 3)') == |
| 114 | '(#:x 3)', 'known keyword in rest') |
| 115 | } |
| 116 | |