module scheme fn contextual_value(source string) string { mut vm := new() return write(vm.eval_alt(source) or { panic('${source}: ${err}') }) or { panic(err) } } fn test_vile_contextual_keyword_callbacks_and_literals_share_the_same_scope() { for source, expected in { 'apply-fn(fn,x):={fn(x)}\napply-fn(fn(x){x+1},41)': '42' 'fn(x):=x+1\nfn(41)': '42' 'fn:=fn(x){x+1}\nfn(41)': '42' 'let((if(fn(x){x+1}),),if(41))': '42' 'let((while(fn(x){x+1}),),while(41))': '42' 'let((if(fn(x){x+1}),),2*if(20)+0)': '42' 'let((while(fn(x){x+1}),),while(20)*2)': '42' 'if (20) > 0 {42} else {missing}': '42' 'x:=0\nwhile(x)<3 {x=x+1}\nx': '3' 'if #f {missing} else if (20)>0 {42} else {missing}': '42' } { assert contextual_value(source) == expected, source } } fn test_contextual_keyword_values_higher_order_calls_and_error_locations() { assert contextual_value('if:=41\nif+1') == '42' assert contextual_value('while:=43\nwhile - 1') == '42' assert contextual_value('if !#f {42} else {missing}') == '42' assert contextual_value('if:=[1]\nif[0]=42\nif[0]') == '42' assert contextual_value('while:=[42]\nwhile[0]') == '42' for source, expected in { 'fn:=42\nfn': '42' 'if:=42\nif': '42' 'while:=42\nwhile': '42' 'use(fn,if,while):={fn(if)+while}\nuse(fn(x){x+1},20,21)': '42' 'if(x):=x+1\nif(41)': '42' 'while(x):=x+1\nwhile(41)': '42' 'fn:=fn(f,x){f(x)}\nfn(fn(x){x+1},41)': '42' 'let((if(fn(x){x+1}),),if(20)+21)': '42' 'let((while(fn(x){x+1}),),while(20)!=22)': '#t' } { assert contextual_value(source) == expected, source } mut vm := new() for source in ['\n fn(1)', '\n if(1)', '\n while(1)'] { if _ := vm.eval_alt(source) { assert false } else { assert err is EvalError if err is EvalError { assert err.line == 2 && err.column == 2 } } } }