module scheme import reader fn returned(source string, alternate bool) string { mut vm := new_with_options(Options{ max_steps: 0, max_frames: 32 }) or { panic(err.msg()) } value := if alternate { vm.eval_alt(source) or { panic('${source}: ${err.msg()}') } } else { vm.eval(source) or { panic('${source}: ${err.msg()}') } } return write(value) or { panic(err.msg()) } } fn test_return_skips_following_forms_through_lexical_binding_helpers() { for source in [ 'f():={return(42),missing}\nf()', 'f():={let((x(20),),{return(x+22),missing}),missing}\nf()', 'f():={let*((x(20),y(22)),{return(x+y),missing}),missing}\nf()', 'f():={letrec((x(20),),{return(x+22),missing}),missing}\nf()', 'f():={letrec*((x(20),y(x+2)),{return(x+y),missing}),missing}\nf()', 'f():={let-values((((a,b),values(20,22)),),{return(a+b),missing}),missing}\nf()', 'f():={let*-values((((a,),20),((b,),22)),{return(a+b),missing}),missing}\nf()', 'f():={for(i:=0,i<10,i=i+1){if i == 3 {return(42)}}\nmissing}\nf()', 'f():={i:=0\nwhile #t {i=i+1\nif i == 3 {return(42)}}\nmissing}\nf()', ] { assert returned(source, true) == '42', source } assert returned('(define(f)(let-values (((x y)(values 20 22))) (return (+ x y)) missing) missing) (f)', false) == '42' } fn test_return_targets_source_procedures_and_named_let_not_outer_functions() { for source in [ 'f():={g:=fn(){return(20),missing}\ng()+22}\nf()', 'f():={g:=case-lambda(((),{return(20),missing}),((x,),x))\ng()+22}\nf()', 'f():={x:=let(loop,(),{return(20),missing})\nx+22}\nf()', 'f():={g:=lambda((),{return(20),missing})\ng()+22}\nf()', ] { assert returned(source, true) == '42', source } } fn test_return_transports_zero_one_or_multiple_values_and_unspecified() { assert returned('f():={return(values(20,22)),missing}\ncall-with-values(f,+)', true) == '42' assert returned('f():={return(values()),missing}\ncall-with-values(f,list)', true) == '()' for source in ['f():={return(),missing}\nf()', 'f():={return\nmissing}\nf()'] { assert returned(source, true) == '#', source } } fn test_return_tail_recursion_through_branch_and_binding_bodies_stays_bounded() { for body in [ 'if n == 0 {return(acc)} else {return(sum(n - 1,acc+n))}', 'if n == 0 {return(acc)}\nreturn(sum(n - 1,acc+n))', 'if n == 0 {return(acc)}\nreturn(sum(n - 1,acc+n))\nmissing', 'return(if(n == 0,acc,sum(n - 1,acc+n)))\nmissing', 'return(let((x(n),),if(x == 0,acc,sum(x - 1,acc+x))))\nmissing', 'let((x(n),),{if x == 0 {return(acc)} else {return(sum(x - 1,acc+x))}})', 'let*-values((((x,),n),),{if x == 0 {return(acc)} else {return(sum(x - 1,acc+x))}})', ] { mut vm := new_with_options(Options{ max_steps: 0, max_frames: 3 }) or { panic(err.msg()) } source := 'sum(n,acc):={${body}}\nsum(100000,0)' assert vm.eval_alt(source) or { panic('${source}: ${err.msg()}') }.as_integer() or { panic(err.msg()) } == 5000050000 } } fn test_return_call_continuations_skip_unreachable_forms_on_every_replay() { mut vm := new() first := vm.eval_alt('saved:=#f\nf():={let((),{return(call/cc(fn(k){saved=k,42}))\nmissing})\nmissing}\nf()') or { panic(err.msg()) } assert first.as_integer() or { panic(err.msg()) } == 42 for n in 43 .. 46 { gc_collect() assert vm.call(vm.lookup('saved') or { panic(err.msg()) }, [integer(n)]) or { panic(err.msg()) }.as_integer() or { panic(err.msg()) } == n assert vm.active.len == 0 } } fn test_return_call_bridge_replays_zero_and_multiple_values_with_a_caller_prefix() { mut vm := new() 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()) } assert write(first) or { panic(err.msg()) } == '(10 (20 22) 30)' for i, args in [[]Value{}, [integer(42)], [integer(4), integer(2), integer(0)]] { gc_collect() result := vm.call(vm.lookup('saved') or { panic(err.msg()) }, args) or { panic(err.msg()) } assert write(result) or { panic(err.msg()) } == ['(10 () 30)', '(10 (42) 30)', '(10 (4 2 0) 30)'][i] assert vm.active.len == 0 } assert write(first) or { panic(err.msg()) } == '(10 (20 22) 30)' } fn test_return_call_arguments_run_once_before_abandoning_enclosing_forms() { 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)' assert returned('p:=make-parameter(1)\ng():=p()\nf():={parameterize((p(42),),return(g()))\nmissing}\nlist(f(),p())', true) == '(42 1)' } fn test_return_restores_parameters_and_keeps_enclosing_winders() { assert returned('p:=make-parameter(1)\nf():={parameterize((p(42),),return(p()))\nmissing}\nr:=f()\nlist(r,p())', true) == '(42 1)' 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))' } fn test_return_continuation_replay_restores_original_activation_markers() { mut vm := new() 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()) } assert write(first) or { panic(err.msg()) } == '(20 42)' for n in 21 .. 24 { gc_collect() result := vm.call(vm.lookup('saved') or { panic(err.msg()) }, [integer(n)]) or { panic(err.msg()) } assert write(result) or { panic(err.msg()) } == '(${n} 42)' assert vm.eval('(p)') or { panic(err.msg()) }.as_integer() or { panic(err.msg()) } == 1 assert vm.active.len == 0 } assert write(first) or { panic(err.msg()) } == '(20 42)' } fn test_return_keyword_shadowing_imports_and_hygienic_macros() { assert returned('let((return(+),),return(20,22))', true) == '42' assert returned('f(return):=return(20,22)\nf(+)', true) == '42' assert returned('import(prefix(only((vile,base),return),r:))\nf():={r:return(42),missing}\nf()', true) == '42' assert returned('define-syntax(stop,syntax-rules((),((_,x),return(x))))\nf():={let((return(+),),stop(42))\nmissing}\nf()', true) == '42' assert returned('define-library((test,ret),export(f),import((vile,base)),{f():={return(42),missing}})\nimport((test,ret))\nf()', true) == '42' } fn test_return_scope_arity_and_delayed_body_errors_are_located_and_recoverable() { for source in ['return(42)', 'return', 'f():={return(1,2)}\nf()', 'f():={force(delay(return(42)))}\nf()', 'f():={force(delay-force(delay(return(42))))}\nf()'] { mut vm := new() if _ := vm.eval_alt('\n ' + source) { assert false, source } else { assert err is EvalError, '${source}: ${err.msg()}' if err is EvalError { assert err.line >= 2 } } assert vm.active.len == 0 assert vm.eval_alt('20+22') or { panic(err.msg()) }.as_integer() or { panic(err.msg()) } == 42 } } fn test_return_only_tail_forms_need_no_marker_or_extra_slots() { mut code := &Code{ doc: reader.read_alt('f(n):={if n == 0 {return(42)} else {return(f(n - 1))}}') or { panic(err.msg()) } } program := compile_program(code, code.doc.forms) mut pending := [program] for pending.len > 0 { function := pending.pop() for instruction in function.instructions { assert instruction.op !in [.exit_mark, .early_return] } for child in function.children { pending << child } } assert program.children[0].slot_count == 1 }