| 1 | module scheme |
| 2 | |
| 3 | fn contextual_value(source string) string { |
| 4 | mut vm := new() |
| 5 | return write(vm.eval_alt(source) or { panic('${source}: ${err}') }) or { panic(err) } |
| 6 | } |
| 7 | |
| 8 | fn test_vile_contextual_keyword_callbacks_and_literals_share_the_same_scope() { |
| 9 | for source, expected in { |
| 10 | 'apply-fn(fn,x):={fn(x)}\napply-fn(fn(x){x+1},41)': '42' |
| 11 | 'fn(x):=x+1\nfn(41)': '42' |
| 12 | 'fn:=fn(x){x+1}\nfn(41)': '42' |
| 13 | 'let((if(fn(x){x+1}),),if(41))': '42' |
| 14 | 'let((while(fn(x){x+1}),),while(41))': '42' |
| 15 | 'let((if(fn(x){x+1}),),2*if(20)+0)': '42' |
| 16 | 'let((while(fn(x){x+1}),),while(20)*2)': '42' |
| 17 | 'if (20) > 0 {42} else {missing}': '42' |
| 18 | 'x:=0\nwhile(x)<3 {x=x+1}\nx': '3' |
| 19 | 'if #f {missing} else if (20)>0 {42} else {missing}': '42' |
| 20 | } { |
| 21 | assert contextual_value(source) == expected, source |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn test_contextual_keyword_values_higher_order_calls_and_error_locations() { |
| 26 | assert contextual_value('if:=41\nif+1') == '42' |
| 27 | assert contextual_value('while:=43\nwhile - 1') == '42' |
| 28 | assert contextual_value('if !#f {42} else {missing}') == '42' |
| 29 | assert contextual_value('if:=[1]\nif[0]=42\nif[0]') == '42' |
| 30 | assert contextual_value('while:=[42]\nwhile[0]') == '42' |
| 31 | for source, expected in { |
| 32 | 'fn:=42\nfn': '42' |
| 33 | 'if:=42\nif': '42' |
| 34 | 'while:=42\nwhile': '42' |
| 35 | 'use(fn,if,while):={fn(if)+while}\nuse(fn(x){x+1},20,21)': '42' |
| 36 | 'if(x):=x+1\nif(41)': '42' |
| 37 | 'while(x):=x+1\nwhile(41)': '42' |
| 38 | 'fn:=fn(f,x){f(x)}\nfn(fn(x){x+1},41)': '42' |
| 39 | 'let((if(fn(x){x+1}),),if(20)+21)': '42' |
| 40 | 'let((while(fn(x){x+1}),),while(20)!=22)': '#t' |
| 41 | } { |
| 42 | assert contextual_value(source) == expected, source |
| 43 | } |
| 44 | mut vm := new() |
| 45 | for source in ['\n fn(1)', '\n if(1)', '\n while(1)'] { |
| 46 | if _ := vm.eval_alt(source) { |
| 47 | assert false |
| 48 | } else { |
| 49 | assert err is EvalError |
| 50 | if err is EvalError { |
| 51 | assert err.line == 2 && err.column == 2 |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |