| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | fn returned(source string, alternate bool) string { |
| 6 | mut vm := new_with_options(Options{ max_steps: 0, max_frames: 32 }) or { panic(err.msg()) } |
| 7 | value := if alternate { |
| 8 | vm.eval_alt(source) or { panic('${source}: ${err.msg()}') } |
| 9 | } else { |
| 10 | vm.eval(source) or { panic('${source}: ${err.msg()}') } |
| 11 | } |
| 12 | return write(value) or { panic(err.msg()) } |
| 13 | } |
| 14 | |
| 15 | fn test_return_skips_following_forms_through_lexical_binding_helpers() { |
| 16 | for source in [ |
| 17 | 'f():={return(42),missing}\nf()', |
| 18 | 'f():={let((x(20),),{return(x+22),missing}),missing}\nf()', |
| 19 | 'f():={let*((x(20),y(22)),{return(x+y),missing}),missing}\nf()', |
| 20 | 'f():={letrec((x(20),),{return(x+22),missing}),missing}\nf()', |
| 21 | 'f():={letrec*((x(20),y(x+2)),{return(x+y),missing}),missing}\nf()', |
| 22 | 'f():={let-values((((a,b),values(20,22)),),{return(a+b),missing}),missing}\nf()', |
| 23 | 'f():={let*-values((((a,),20),((b,),22)),{return(a+b),missing}),missing}\nf()', |
| 24 | 'f():={for(i:=0,i<10,i=i+1){if i == 3 {return(42)}}\nmissing}\nf()', |
| 25 | 'f():={i:=0\nwhile #t {i=i+1\nif i == 3 {return(42)}}\nmissing}\nf()', |
| 26 | ] { |
| 27 | assert returned(source, true) == '42', source |
| 28 | } |
| 29 | assert returned('(define(f)(let-values (((x y)(values 20 22))) (return (+ x y)) missing) missing) (f)', false) == '42' |
| 30 | } |
| 31 | |
| 32 | fn test_return_targets_source_procedures_and_named_let_not_outer_functions() { |
| 33 | for source in [ |
| 34 | 'f():={g:=fn(){return(20),missing}\ng()+22}\nf()', |
| 35 | 'f():={g:=case-lambda(((),{return(20),missing}),((x,),x))\ng()+22}\nf()', |
| 36 | 'f():={x:=let(loop,(),{return(20),missing})\nx+22}\nf()', |
| 37 | 'f():={g:=lambda((),{return(20),missing})\ng()+22}\nf()', |
| 38 | ] { |
| 39 | assert returned(source, true) == '42', source |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fn test_return_transports_zero_one_or_multiple_values_and_unspecified() { |
| 44 | assert returned('f():={return(values(20,22)),missing}\ncall-with-values(f,+)', true) == '42' |
| 45 | assert returned('f():={return(values()),missing}\ncall-with-values(f,list)', true) == '()' |
| 46 | for source in ['f():={return(),missing}\nf()', 'f():={return\nmissing}\nf()'] { |
| 47 | assert returned(source, true) == '#<unspecified>', source |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fn test_return_tail_recursion_through_branch_and_binding_bodies_stays_bounded() { |
| 52 | for body in [ |
| 53 | 'if n == 0 {return(acc)} else {return(sum(n - 1,acc+n))}', |
| 54 | 'if n == 0 {return(acc)}\nreturn(sum(n - 1,acc+n))', |
| 55 | 'if n == 0 {return(acc)}\nreturn(sum(n - 1,acc+n))\nmissing', |
| 56 | 'return(if(n == 0,acc,sum(n - 1,acc+n)))\nmissing', |
| 57 | 'return(let((x(n),),if(x == 0,acc,sum(x - 1,acc+x))))\nmissing', |
| 58 | 'let((x(n),),{if x == 0 {return(acc)} else {return(sum(x - 1,acc+x))}})', |
| 59 | 'let*-values((((x,),n),),{if x == 0 {return(acc)} else {return(sum(x - 1,acc+x))}})', |
| 60 | ] { |
| 61 | mut vm := new_with_options(Options{ max_steps: 0, max_frames: 3 }) or { panic(err.msg()) } |
| 62 | source := 'sum(n,acc):={${body}}\nsum(100000,0)' |
| 63 | assert vm.eval_alt(source) or { panic('${source}: ${err.msg()}') }.as_integer() or { panic(err.msg()) } == 5000050000 |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | fn test_return_call_continuations_skip_unreachable_forms_on_every_replay() { |
| 68 | mut vm := new() |
| 69 | first := vm.eval_alt('saved:=#f\nf():={let((),{return(call/cc(fn(k){saved=k,42}))\nmissing})\nmissing}\nf()') or { panic(err.msg()) } |
| 70 | assert first.as_integer() or { panic(err.msg()) } == 42 |
| 71 | for n in 43 .. 46 { |
| 72 | gc_collect() |
| 73 | assert vm.call(vm.lookup('saved') or { panic(err.msg()) }, [integer(n)]) or { panic(err.msg()) }.as_integer() or { panic(err.msg()) } == n |
| 74 | assert vm.active.len == 0 |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | fn test_return_call_bridge_replays_zero_and_multiple_values_with_a_caller_prefix() { |
| 79 | mut vm := new() |
| 80 | first := vm.eval_alt('saved:=#f\nf():={let((),{return(call/cc(fn(k){saved=k,values(20,22)}))\nmissing})\nmissing}\nlist(10,call-with-values(f,list),30)') or { panic(err.msg()) } |
| 81 | assert write(first) or { panic(err.msg()) } == '(10 (20 22) 30)' |
| 82 | for i, args in [[]Value{}, [integer(42)], [integer(4), integer(2), integer(0)]] { |
| 83 | gc_collect() |
| 84 | result := vm.call(vm.lookup('saved') or { panic(err.msg()) }, args) or { panic(err.msg()) } |
| 85 | assert write(result) or { panic(err.msg()) } == ['(10 () 30)', '(10 (42) 30)', '(10 (4 2 0) 30)'][i] |
| 86 | assert vm.active.len == 0 |
| 87 | } |
| 88 | assert write(first) or { panic(err.msg()) } == '(10 (20 22) 30)' |
| 89 | } |
| 90 | |
| 91 | fn test_return_call_arguments_run_once_before_abandoning_enclosing_forms() { |
| 92 | assert returned('trace:=0\ntick(n,x):={trace=trace*10+n,values(x)}\nf():={let((x(20),),return(tick(1,+)(tick(2,x),tick(3,22))))\nmissing}\nlist(f(),trace)', true) == '(42 123)' |
| 93 | assert returned('p:=make-parameter(1)\ng():=p()\nf():={parameterize((p(42),),return(g()))\nmissing}\nlist(f(),p())', true) == '(42 1)' |
| 94 | } |
| 95 | |
| 96 | fn test_return_restores_parameters_and_keeps_enclosing_winders() { |
| 97 | assert returned('p:=make-parameter(1)\nf():={parameterize((p(42),),return(p()))\nmissing}\nr:=f()\nlist(r,p())', true) == '(42 1)' |
| 98 | assert returned('trace:=list()\nf():={dynamic-wind(fn(){trace=cons(1,trace)},fn(){return(20),missing},fn(){trace=cons(2,trace)})+22}\nlist(f(),reverse(trace))', true) == '(42 (1 2))' |
| 99 | } |
| 100 | |
| 101 | fn test_return_continuation_replay_restores_original_activation_markers() { |
| 102 | mut vm := new() |
| 103 | first := vm.eval_alt('saved:=#f\np:=make-parameter(1)\nf():={parameterize((p(42),),{x:=call/cc(fn(k){saved=k,20})\nreturn(values(x,p()))})\nmissing}\ncall-with-values(f,list)') or { panic(err.msg()) } |
| 104 | assert write(first) or { panic(err.msg()) } == '(20 42)' |
| 105 | for n in 21 .. 24 { |
| 106 | gc_collect() |
| 107 | result := vm.call(vm.lookup('saved') or { panic(err.msg()) }, [integer(n)]) or { panic(err.msg()) } |
| 108 | assert write(result) or { panic(err.msg()) } == '(${n} 42)' |
| 109 | assert vm.eval('(p)') or { panic(err.msg()) }.as_integer() or { panic(err.msg()) } == 1 |
| 110 | assert vm.active.len == 0 |
| 111 | } |
| 112 | assert write(first) or { panic(err.msg()) } == '(20 42)' |
| 113 | } |
| 114 | |
| 115 | fn test_return_keyword_shadowing_imports_and_hygienic_macros() { |
| 116 | assert returned('let((return(+),),return(20,22))', true) == '42' |
| 117 | assert returned('f(return):=return(20,22)\nf(+)', true) == '42' |
| 118 | assert returned('import(prefix(only((vile,base),return),r:))\nf():={r:return(42),missing}\nf()', true) == '42' |
| 119 | assert returned('define-syntax(stop,syntax-rules((),((_,x),return(x))))\nf():={let((return(+),),stop(42))\nmissing}\nf()', true) == '42' |
| 120 | assert returned('define-library((test,ret),export(f),import((vile,base)),{f():={return(42),missing}})\nimport((test,ret))\nf()', true) == '42' |
| 121 | } |
| 122 | |
| 123 | fn test_return_scope_arity_and_delayed_body_errors_are_located_and_recoverable() { |
| 124 | for source in ['return(42)', 'return', 'f():={return(1,2)}\nf()', |
| 125 | 'f():={force(delay(return(42)))}\nf()', 'f():={force(delay-force(delay(return(42))))}\nf()'] { |
| 126 | mut vm := new() |
| 127 | if _ := vm.eval_alt('\n ' + source) { |
| 128 | assert false, source |
| 129 | } else { |
| 130 | assert err is EvalError, '${source}: ${err.msg()}' |
| 131 | if err is EvalError { |
| 132 | assert err.line >= 2 |
| 133 | } |
| 134 | } |
| 135 | assert vm.active.len == 0 |
| 136 | assert vm.eval_alt('20+22') or { panic(err.msg()) }.as_integer() or { panic(err.msg()) } == 42 |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | fn test_return_only_tail_forms_need_no_marker_or_extra_slots() { |
| 141 | mut code := &Code{ doc: reader.read_alt('f(n):={if n == 0 {return(42)} else {return(f(n - 1))}}') or { panic(err.msg()) } } |
| 142 | program := compile_program(code, code.doc.forms) |
| 143 | mut pending := [program] |
| 144 | for pending.len > 0 { |
| 145 | function := pending.pop() |
| 146 | for instruction in function.instructions { |
| 147 | assert instruction.op !in [.exit_mark, .early_return] |
| 148 | } |
| 149 | for child in function.children { pending << child } |
| 150 | } |
| 151 | assert program.children[0].slot_count == 1 |
| 152 | } |
| 153 | |