vile / scheme / syntax_shared_match_test.v
78 lines · 71 sloc · 3.78 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// Compact diamonds exercise matching, not just grammar validation. Expanding
4// either input into a tree would require billions of recursive visits.
5fn shared_match_diamond(leaf string, depth int, base int) string {
6 mut result := leaf
7 for level in 0 .. depth {
8 label := base + level
9 result = '#(#${label}=${result} #${label}#)'
10 }
11 return result
12}
13
14fn test_shared_rules_match_deep_constant_graph_and_preserve_adjacent_captures() {
15 mut vm := new()
16 pattern := shared_match_diamond('_', 36, 1)
17 input := shared_match_diamond('1', 36, 101)
18 vm.eval('(define-syntax recognize (syntax-rules () ((_ ${pattern} x ...) (list x ...)) ((_ . rest) #f)))')!
19 assert write(vm.eval('(recognize ${input} 7 8 9)')!)! == '(7 8 9)'
20 assert write(vm.eval('(recognize ${input})')!)! == '()'
21 assert !vm.eval('(recognize #())')!.is_true()
22 assert vm.active.len == 0
23}
24
25fn test_shared_case_match_handles_wrapped_and_raw_graphs_with_captures() {
26 mut vm := new()
27 pattern := shared_match_diamond('_', 36, 1)
28 input := shared_match_diamond('1', 36, 101)
29 vm.eval("(define-syntax recognize (lambda (stx) (syntax-case stx () ((_ ${pattern} x) #'x) (_ #'#f))))")!
30 assert vm.eval('(recognize ${input} 42)')!.as_integer()! == 42
31 assert vm.eval("(syntax-case '#(${input} 43) () (#(${pattern} x) (syntax->datum (syntax x))) (_ #f))")!.as_integer()! == 43
32 assert !vm.eval('(recognize #() 9)')!.is_true()
33}
34
35fn test_shared_matches_distinguish_input_nodes_and_restart_on_each_invocation() {
36 mut vm := new()
37 vm.eval('(define-syntax same-ones (syntax-rules () ((_ #(#1=#(1) #1#)) #t) ((_ x) #f)))')!
38 assert vm.eval('(same-ones #(#(1) #(1)))')!.is_true()
39 assert !vm.eval('(same-ones #(#(1) #(2)))')!.is_true()
40 assert vm.eval('(same-ones #(#(1) #(1)))')!.is_true()
41 vm.eval("(define (same-ones-case x) (syntax-case x () (#(#1=#(1) #1#) #t) (_ #f)))")!
42 assert vm.eval("(same-ones-case '#(#(1) #(1)))")!.is_true()
43 assert !vm.eval("(same-ones-case '#(#(1) #(2)))")!.is_true()
44 assert vm.eval("(same-ones-case '#(#(1) #(1)))")!.is_true()
45}
46
47fn test_shared_constant_literals_keep_binding_context_and_shadowing() {
48 mut vm := new()
49 vm.eval('(define lit 1) (define-syntax recognizes-lit (syntax-rules (lit) ((_ #(#1=#(lit) #1#)) #t) ((_ x) #f)))')!
50 assert vm.eval('(recognizes-lit #(#(lit) #(lit)))')!.is_true()
51 assert !vm.eval('(let ((lit 2)) (recognizes-lit #(#(lit) #(lit))))')!.is_true()
52 assert vm.eval('(begin (set! lit 3) (recognizes-lit #(#(lit) #(lit))))')!.is_true()
53 vm.eval("(define-syntax recognizes-case-lit (lambda (stx) (syntax-case stx (lit) ((_ #(#1=#(lit) #1#)) #'#t) (_ #'#f))))")!
54 assert vm.eval('(recognizes-case-lit #(#(lit) #(lit)))')!.is_true()
55 assert !vm.eval('(let ((lit 2)) (recognizes-case-lit #(#(lit) #(lit))))')!.is_true()
56}
57
58fn test_shared_constant_matches_keep_ragged_repetition_coordinates() {
59 mut vm := new()
60 pattern := shared_match_diamond('1', 12, 1)
61 input := shared_match_diamond('1', 12, 101)
62 vm.eval('(define-syntax collect (syntax-rules () ((_ (${pattern} x ...) ...) (list (list x ...) ...))))')!
63 assert write(vm.eval('(collect (#200=${input} 7 8) (#200#) (#200# 9))')!)! == '((7 8) () (9))'
64 vm.eval("(define-syntax collect-case (lambda (stx) (syntax-case stx () ((_ (${pattern} x ...) ...) #'(list (list x ...) ...)))))")!
65 assert write(vm.eval('(collect-case (#200=${input} 7 8) (#200#) (#200# 9))')!)! == '((7 8) () (9))'
66}
67
68fn test_plain_patterns_need_no_shared_constant_table_and_duplicate_variables_still_fail() {
69 mut vm := new()
70 assert vm.eval('(define-syntax plain (syntax-rules () ((_ (x ...)) (list x ...)))) (plain (1 2 3))')!.kind() == .pair
71 for source in [
72 '(define-syntax invalid (syntax-rules () ((_ #(#1=#(x) #1#)) 1)))',
73 "(syntax-case '#(#(1) #(2)) () (#(#1=#(x) #1#) x))",
74 ] {
75 if _ := vm.eval(source) { panic('accepted duplicate shared pattern variable') }
76 }
77 assert vm.eval('42')!.as_integer()! == 42
78}
79