| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | fn compiled_value(source string) string { |
| 6 | mut vm := new() |
| 7 | value := vm.eval(source) or { panic('${source}: ${err}') } |
| 8 | return write(value) or { panic(err) } |
| 9 | } |
| 10 | |
| 11 | fn test_compiled_slots_capture_mutation_shadowing_and_internal_recursion() { |
| 12 | for source, expected in { |
| 13 | '(let ((x 1)) (let ((f (lambda () (set! x (+ x 1)) x))) (list (f) (f) x)))': '(2 3 3)' |
| 14 | '(let ((x 1)) (let ((f (lambda () x))) (let ((x 99)) (f))))': '1' |
| 15 | '(let* ((x 1) (f (lambda () x)) (x 2)) (list x (f)))': '(2 1)' |
| 16 | '((lambda (x) (define (f) x) (set! x 42) (f)) 1)': '42' |
| 17 | '((lambda () (define (f) (g)) (define (g) 42) (f)))': '42' |
| 18 | '((lambda () (define (f) x) (define x 42) (f)))': '42' |
| 19 | '((lambda (a . rest) (lambda () (list a rest))) 1 2 3)': '#<procedure>' |
| 20 | '(((lambda (a . rest) (lambda () (list a rest))) 1 2 3))': '(1 (2 3))' |
| 21 | '(let loop ((loop 42)) loop)': '42' |
| 22 | '(let ((loop 42)) (let loop ((n loop)) n))': '42' |
| 23 | '(let ((x 1)) (letrec ((f (lambda () (set! x 42)))) (f) x))': '42' |
| 24 | } { |
| 25 | assert compiled_value(source) == expected, source |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn test_compiled_calls_preserve_order_global_rebinding_and_pending_operands() { |
| 30 | for source, expected in { |
| 31 | '(define x 0) (list (begin (set! x (+ x 1)) x) (begin (set! x (+ x 1)) x) x)': '(1 2 2)' |
| 32 | '(define (f x) (+ x 1)) (define (g x) (f x)) (define (f x) (* x 2)) (g 21)': '42' |
| 33 | '(define (f x) (+ x 1)) (define + *) (f 42)': '42' |
| 34 | '(define (f x) (+ x 1)) (+ 1 (* 2 (f 3)) (f (f 4)))': '15' |
| 35 | '(define (f . xs) xs) (apply f 1 2 (list 3 4))': '(1 2 3 4)' |
| 36 | "(apply apply (list apply (list + '(1 2 3))))": '6' |
| 37 | '(list (and 1 2) (or #f 3) (cond (#f 0) ((+ 1 3))) 5)': '(2 3 4 5)' |
| 38 | '(+ 1 (cond ((+ 1 2) => (lambda (x) (+ x 38)))) )': '42' |
| 39 | } { |
| 40 | assert compiled_value(source) == expected, source |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | fn test_compiler_failures_remain_lazy_and_do_not_rollback_earlier_forms() { |
| 45 | for source in ['(if #t 42 (if))', '(if #f (lambda (x x) x) 42)', '(or 42 (quote))', |
| 46 | '(and #f (let ((x)) x))', '(cond (#t 42) ())', '(if #t 42 1/0)'] { |
| 47 | expected := if source.starts_with('(and') { '#f' } else { '42' } |
| 48 | assert compiled_value(source) == expected, source |
| 49 | } |
| 50 | mut vm := new() |
| 51 | if _ := vm.eval('(define kept 42) (if)') { |
| 52 | assert false |
| 53 | } |
| 54 | assert vm.lookup('kept') or { panic(err) }.as_integer() or { panic(err) } == 42 |
| 55 | if _ := vm.eval('((lambda () (define x x) x))') { |
| 56 | assert false |
| 57 | } else { |
| 58 | assert err is EvalError |
| 59 | } |
| 60 | // The outer call location must not replace a failure in an older compilation. |
| 61 | vm.eval('(define (saved)\n (+ 1 "bad"))') or { panic(err) } |
| 62 | if _ := vm.eval('\n\n\n(saved)') { |
| 63 | assert false |
| 64 | } else { |
| 65 | assert err is EvalError |
| 66 | if err is EvalError { |
| 67 | assert err.line == 2 && err.column == 3 |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn test_flat_binding_and_cond_compilation_does_not_recurse_on_form_length() { |
| 73 | source := '(let* (' + '(x 42) '.repeat(3000) + ') x)' |
| 74 | assert compiled_value(source) == '42' |
| 75 | source2 := '(cond ' + '(#f 0) '.repeat(6000) + '(else 42))' |
| 76 | assert compiled_value(source2) == '42' |
| 77 | } |
| 78 | |
| 79 | fn test_instruction_operands_resolve_local_slots_and_return_frames_are_compact() { |
| 80 | source := '(lambda (x) (+ x ((lambda (y) (+ x y)) 2)))' |
| 81 | code := &Code{ doc: reader.read_all(source) or { panic(err) } } |
| 82 | function := compile_program(code, code.doc.forms) |
| 83 | outer := function.children[0] |
| 84 | inner := outer.children[0] |
| 85 | mut outer_local := false |
| 86 | mut captured_local := false |
| 87 | for instruction in outer.instructions { |
| 88 | if instruction.op == .local && instruction.a == 0 && instruction.b == 0 { |
| 89 | outer_local = true |
| 90 | } |
| 91 | } |
| 92 | for instruction in inner.instructions { |
| 93 | if instruction.op == .local && instruction.a == 1 && instruction.b == 0 { |
| 94 | captured_local = true |
| 95 | } |
| 96 | } |
| 97 | assert outer_local && captured_local |
| 98 | assert sizeof(Instruction) == 16 |
| 99 | assert sizeof(CallFrame) <= 32 |
| 100 | } |
| 101 | |
| 102 | fn test_primitive_arguments_borrow_and_reuse_the_operand_stack_buffer() { |
| 103 | mut vm := new() |
| 104 | plus := vm.lookup('+') or { panic(err) } |
| 105 | mut machine := Machine{ stack: [plus, integer(20), integer(22)] } |
| 106 | pointer := machine.stack.data |
| 107 | machine.apply(mut vm, 2, false) or { panic(err) } |
| 108 | assert machine.stack.data == pointer |
| 109 | assert machine.stack.len == 1 |
| 110 | assert machine.stack[0].as_integer() or { panic(err) } == 42 |
| 111 | // Trimmed capacity must not keep the previous argument handles alive. |
| 112 | unsafe { |
| 113 | raw := &Value(machine.stack.data) |
| 114 | assert raw[1].tag == .unspecified |
| 115 | assert raw[2].tag == .unspecified |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | fn test_allocation_free_counts_match_public_container_views() { |
| 120 | for value in [nil_value(), list([integer(1)]), list([integer(1), integer(2)]), |
| 121 | cons(integer(1), integer(2))] { |
| 122 | if items := value.list_items() { |
| 123 | assert value.list_length() or { panic(err) } == items.len |
| 124 | } else { |
| 125 | if _ := value.list_length() { |
| 126 | assert false |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | for text in ['', 'ascii', 'λ😀', 'a\x00b', '\xff\x80a', '\xf0\x9f'] { |
| 131 | assert unicode_length(text) == text.runes().len |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | fn test_integer_fast_paths_keep_exact_boundaries_and_rational_results() { |
| 136 | for source, expected in { |
| 137 | '(+ 9223372036854775806 1)': '9223372036854775807' |
| 138 | '(- -9223372036854775807 1)': '-9223372036854775808' |
| 139 | '(* -9223372036854775808 1)': '-9223372036854775808' |
| 140 | '(* -9223372036854775808 0)': '0' |
| 141 | '(/ -9223372036854775808 2)': '-4611686018427387904' |
| 142 | '(/ 5 2)': '5/2' |
| 143 | '(> 9223372036854775807 -9223372036854775808)': '#t' |
| 144 | '(= 9007199254740992 9007199254740993)': '#f' |
| 145 | } { |
| 146 | assert compiled_value(source) == expected, source |
| 147 | } |
| 148 | for source, expected in { |
| 149 | '(+ 9223372036854775807 1)': '9223372036854775808' |
| 150 | '(- -9223372036854775808 1)': '-9223372036854775809' |
| 151 | '(* -9223372036854775808 -1)': '9223372036854775808' |
| 152 | '(* 9223372036854775807 2)': '18446744073709551614' |
| 153 | '(- -9223372036854775808)': '9223372036854775808' |
| 154 | } { |
| 155 | assert compiled_value(source) == expected |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | fn test_named_let_closures_do_not_retain_hidden_initial_argument_slots() { |
| 160 | mut vm := new() |
| 161 | value := vm.eval('(let loop ((initial (list 1 2 3))) loop)') or { panic(err) } |
| 162 | procedure := value.procedure_pointer() |
| 163 | assert procedure.env.slots.len == 2 |
| 164 | assert procedure.env.slots[0].tag == .unspecified |
| 165 | assert procedure.env.slots[1].tag == .procedure |
| 166 | } |
| 167 | |