| 1 | module scheme |
| 2 | |
| 3 | fn syntax_text(source string) string { |
| 4 | mut vm := new() |
| 5 | return write(vm.eval(source) or { panic('${source}: ${err}') }) or { panic(err) } |
| 6 | } |
| 7 | |
| 8 | fn test_lexical_variables_can_shadow_every_expression_keyword() { |
| 9 | for keyword in ['quote', 'if', 'begin', 'lambda', 'define', 'set!', 'let', 'let*', 'letrec', |
| 10 | 'and', 'or', 'cond', 'case', 'do', 'delay', 'quasiquote', 'unquote', 'unquote-splicing'] { |
| 11 | assert syntax_text('((lambda (${keyword}) (${keyword} 20 22)) +)') == '42', keyword |
| 12 | assert syntax_text('(let ((${keyword} +)) (${keyword} 20 22))') == '42', keyword |
| 13 | } |
| 14 | assert syntax_text('((lambda (if) (set! if +) (if 20 22)) #f)') == '42' |
| 15 | assert syntax_text('(let () (define if +) (if 20 22))') == '42' |
| 16 | assert syntax_text('(let ((quote (lambda (x) (+ 1 x)))) (quote 41))') == '42' |
| 17 | assert syntax_text('(let ((if +)) (if))') == '0' |
| 18 | assert syntax_text('(list (let ((if +)) (if 20 22)) (if #t 42 0))') == '(42 42)' |
| 19 | } |
| 20 | |
| 21 | fn test_cond_auxiliary_identifiers_respect_lexical_binding_identity() { |
| 22 | assert syntax_text('(let ((else #f)) (cond (else 1) (#t 42)))') == '42' |
| 23 | assert syntax_text('(let ((else 41)) (cond (else (+ else 1)) (#t 0)))') == '42' |
| 24 | assert syntax_text('(let ((=> #f)) (cond (#t => (quote ok))))') == 'ok' |
| 25 | assert syntax_text('(let ((=> 1)) (cond (41 => (+ => 41))))') == '42' |
| 26 | assert syntax_text('(cond ((+ 20 21) => (lambda (x) (+ x 1))))') == '42' |
| 27 | assert syntax_text('((lambda (begin) (begin 20 22)) +)') == '42' |
| 28 | } |
| 29 | |
| 30 | fn test_quasiquote_markers_respect_lexical_bindings_and_preserve_literal_names() { |
| 31 | assert syntax_text('(let ((unquote 1)) (quasiquote (unquote (+ 1 2))))') == '(unquote (+ 1 2))' |
| 32 | assert syntax_text('(let ((unquote 1)) (quasiquote (a (unquote (+ 1 2)) b)))') == '(a (unquote (+ 1 2)) b)' |
| 33 | assert syntax_text('(let ((unquote-splicing 1)) (quasiquote (a (unquote-splicing (list 1 2)) b)))') == '(a (unquote-splicing (list 1 2)) b)' |
| 34 | assert syntax_text('(let ((quasiquote +)) (quasiquote 20 22))') == '42' |
| 35 | } |
| 36 | |
| 37 | fn test_top_level_and_host_bindings_can_shadow_syntax_across_evaluations() { |
| 38 | mut vm := new() |
| 39 | assert vm.eval('(define if +) (if 20 22)') or { panic(err) }.as_integer() or { panic(err) } == 42 |
| 40 | assert vm.eval('(if 1 2 3)') or { panic(err) }.as_integer() or { panic(err) } == 6 |
| 41 | assert vm.eval('(define else #f) (cond (else 1) (#t 42))') or { panic(err) }.as_integer() or { panic(err) } == 42 |
| 42 | mut other := new() |
| 43 | plus := other.lookup('+') or { panic(err) } |
| 44 | other.define('if', plus) or { panic(err) } |
| 45 | assert other.eval('(if 20 22)') or { panic(err) }.as_integer() or { panic(err) } == 42 |
| 46 | other.register('quote', fn (mut _vm VM, args []Value) !Value { |
| 47 | return integer(args[0].as_integer()! + 1) |
| 48 | }) or { panic(err) } |
| 49 | assert other.eval('(quote 41)') or { panic(err) }.as_integer() or { panic(err) } == 42 |
| 50 | } |
| 51 | |
| 52 | fn test_body_definition_regions_are_distinct_from_parameters_and_initializer_regions() { |
| 53 | assert syntax_text('(define x 1) |
| 54 | (letrec ((f (lambda () x))) (define x 42) (list (f) x))') == '(1 42)' |
| 55 | assert syntax_text('(define x 1) |
| 56 | (let* ((f (lambda () x))) (define x 42) (list (f) x))') == '(1 42)' |
| 57 | assert syntax_text('((lambda (x) (define x 42) x) 1)') == '42' |
| 58 | assert syntax_text('((lambda (x) (define x 42) (define (get) x) (get)) 1)') == '42' |
| 59 | mut vm := new() |
| 60 | if _ := vm.eval('((lambda (x) (define x x) x) 42)') { |
| 61 | assert false |
| 62 | } else { |
| 63 | assert err is EvalError |
| 64 | assert err.msg().contains('initialization') |
| 65 | } |
| 66 | } |
| 67 | |