module scheme fn star_check(ok bool, message string) { if !ok { panic(message) } } fn star_eval(mut vm VM, source string) Value { return vm.eval(source) or { panic('${source}: ${err}') } } fn star_written(mut vm VM, source string) string { return write(star_eval(mut vm, source)) or { panic(err) } } fn test_lambda_star_optional_keyword_rest_and_duplicate_last_wins() { mut vm := new() star_check(star_written(mut vm, '(define f (lambda* (a #:optional (b 20) #:key (c 30) d #:rest r) (list a b c d r))) (list (f 1) (f 1 2) (f 1 #:c 3) (f 1 2 #:c 3 #:d 4) (f 1 #:d 4 #:c 3) (f 1 #:c 3 9) (f 1 #:c 3 #:c 4))') == '((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)))', 'Guile 3.0.11 extended formal matrix') } fn test_lambda_star_defaults_are_lazy_and_see_prior_bindings() { mut vm := new() actual := star_written(mut vm, '(define count 0) (define f (lambda* (a #:optional (b (begin (set! count (+ count 1)) (+ a 10))) #:key (c (begin (set! count (+ count 1)) (+ b 20))) #:rest r) (list a b c r count))) (list (f 1) (f 1 2 #:c 3) count)') star_check(actual == '((1 11 31 () 2) (1 2 3 (#:c 3) 2) 2)', actual) } fn test_lambda_star_rejects_bad_keyword_packets_without_poisoning_vm() { mut vm := new() for source in ['((lambda* (a #:key b) b) 1 #:x 3)', '((lambda* (a #:key b) b) 1 #:b)', '((lambda* (a #:key b) b) 1 9)', '((lambda* (a #:key b) b) 1 #:b 2 9)', '((lambda* (a #:optional b) b) 1 2 3)', '((lambda* (a #:optional b) b))'] { if _ := vm.eval(source) { panic('accepted invalid lambda* call ${source}') } star_check(star_eval(mut vm, '(+ 20 22)').whole() == 42, 'VM poisoned') } star_check(star_written(mut vm, '((lambda* (a #:key b #:allow-other-keys #:rest r) (list a b r)) 1 #:x 5)') == '(1 #f (#:x 5))', 'allow-other-keys') } fn test_lambda_star_rest_and_tail_calls() { mut vm := new() actual := star_written(mut vm, '(define walk (lambda* (n #:optional (acc 0) #:rest rest) (if (= n 0) (list acc rest) (walk (- n 1) (+ acc 1))))) (walk 12000)') star_check(actual == '(12000 ())', actual) } fn test_lambda_star_rejects_malformed_formal_lists() { mut vm := new() for source in ['(lambda* (#:rest) 1)', '(lambda* (a #:rest a) 1)', '(lambda* (a #:rest r b) 1)', '(lambda* (a #:key (b 1 2)) b)', '(lambda* (a #:optional b #:optional c) b)', '(lambda* (a #:allow-other-keys) a)', '(lambda* (a #:key b #:allow-other-keys c) b)'] { if _ := vm.eval(source) { panic('accepted malformed lambda* ${source}') } } } fn test_define_star_and_r7_companion_library() { mut vm := new() actual := star_written(mut vm, '(import (prefix (vile optargs) o:)) (o:define* (f a #:optional (b (+ a 1)) #:key (c (+ b 1))) (list a b c)) (list (f 2) (f 2 8 #:c 9) ((o:lambda* (#:key (x 5)) x)))') star_check(actual == '((2 3 4) (2 8 9) 5)', actual) } fn test_lambda_star_rest_is_visible_to_keyword_defaults() { mut vm := new() actual := star_written(mut vm, '((lambda* (#:key (x (length r)) #:allow-other-keys #:rest r) (list x r)) #:other 5)') star_check(actual == '(2 (#:other 5))', actual) } fn test_alt_function_literals_and_named_extended_definitions() { mut vm := new() value := vm.eval_alt('f(a, #:optional, (b, 5)) := a + b g := fn(a, #:key, (b, 7)) { a + b } [f(3), f(3, 4), g(2), g(2, #:b, 9)]') or { panic(err) } star_check(write(value) or { panic(err) } == '#(8 7 9 11)', 'Vile extended formals') } fn test_lambda_star_default_scope_and_early_return() { mut vm := new() actual := star_written(mut vm, '(let ((x 7)) (list ((lambda* (#:optional (x x)) x)) ((lambda* (#:optional (x (return 9))) x)) ((lambda* (#:optional (x (return 9))) x) 3)))') star_check(actual == '(7 9 3)', actual) } fn test_lambda_star_default_continuation_can_resume() { mut vm := new() star_eval(mut vm, '(define saved #f) (define f (lambda* (#:optional (x (call/cc (lambda (k) (set! saved k) 2)))) x))') star_check(star_eval(mut vm, '(f)').whole() == 2, 'initial default continuation') star_check(star_eval(mut vm, '(saved 3)').whole() == 3, 'resumed default continuation') star_check(star_eval(mut vm, '(saved 4)').whole() == 4, 'multishot default continuation') } fn test_lambda_star_rejects_unknown_keyword_with_rest() { mut vm := new() if _ := vm.eval('((lambda* (#:key x #:rest r) r) #:unknown 3)') { panic('rest accepted unknown keyword without allow-other-keys') } star_check(star_written(mut vm, '((lambda* (#:key x #:rest r) r) #:x 3)') == '(#:x 3)', 'known keyword in rest') }