vile / scheme / reader_syntax_graph_test.v
177 lines · 166 sloc · 7.48 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4import os
5import time
6
7fn graph_syntax_value(mut vm VM, source string) Value {
8 return vm.eval(source) or { panic('${source}\n${err}') }
9}
10
11fn test_staged_begin_body_and_feature_cycles_are_located_errors_and_leave_vm_usable() {
12 mut vm := new_with_options(Options{ max_steps: 2000, max_expansions: 1000, max_frames: 64 }) or { panic(err) }
13 for source in [
14 '#1=(begin #1#)',
15 '#1=(begin (begin #1#))',
16 '((lambda () #1=(begin #1#)))',
17 '((lambda () #1=(cond-expand (else #1#))))',
18 '#1=(cond-expand (else #1#))',
19 '(cond-expand (#1=(not #1#) 1) (else 2))',
20 '(define-library (reader cyclic) #1=(cond-expand (else #1#)))',
21 ] {
22 if _ := vm.eval('\n' + source) {
23 assert false, source
24 } else {
25 assert err is EvalError, '${source}: ${err}'
26 assert err.msg().contains('circular syntax'), '${source}: ${err}'
27 if err is EvalError {
28 assert err.line >= 2 && err.column >= 1, err.msg()
29 }
30 }
31 assert vm.active.len == 0
32 assert graph_syntax_value(mut vm, '(+ 20 22)').whole() == 42
33 }
34}
35
36fn test_generated_staged_graph_identity_survives_import_and_allows_shared_groups() {
37 mut vm := new_with_options(Options{ max_steps: 2000, max_expansions: 1000 }) or { panic(err) }
38 graph_syntax_value(mut vm, "(define body (list 'begin #f)) (set-car! (cdr body) body)")
39 if _ := vm.eval('(eval body (interaction-environment))') {
40 assert false
41 } else {
42 assert err.msg().contains('circular syntax'), err.msg()
43 }
44 // Every imported subtree starts its own compact generated arena. Detached
45 // identity must distinguish those local IDs from the enclosing begin's IDs.
46 assert graph_syntax_value(mut vm, "(define count 0)
47 (define chunk (list 'begin '(set! count (+ count 1))))
48 (eval (list 'begin chunk chunk 'count) (interaction-environment))").whole() == 2
49 assert graph_syntax_value(mut vm, '(begin #1=(begin (set! count (+ count 1))) #1# count)').whole() == 4
50 assert graph_syntax_value(mut vm, '(let ((n 0)) #1=(begin (set! n (+ n 1))) #1# n)').whole() == 2
51}
52
53fn test_actual_macro_bindings_can_discard_graphs_before_executable_cycle_checks() {
54 mut vm := new()
55 assert graph_syntax_value(mut vm, '(define-syntax discard (syntax-rules () ((_ . ignored) 42)))
56 (discard #1=(begin #1#))').whole() == 42
57 // Expansion is staged: this definition changes the next occurrence from a
58 // begin group into a macro invocation that ignores its captured graph.
59 assert graph_syntax_value(mut vm, '#1=(begin
60 (define-syntax begin (syntax-rules () ((_ . ignored) 43))) #1#)').whole() == 43
61}
62
63fn test_quasiquote_rejects_circular_templates_but_preserves_shared_data_and_unquotes() {
64 mut vm := new()
65 for source in ['`#1=(1 . #1#)', '`#1=#(#1#)', '`#1=(quasiquote #1#)',
66 '(let ((x 42)) `#1=(,x . #1#))'] {
67 if _ := vm.eval(source) {
68 assert false, source
69 } else {
70 assert err.msg().contains('circular quasiquote'), '${source}: ${err}'
71 }
72 assert graph_syntax_value(mut vm, '42').whole() == 42
73 }
74 assert graph_syntax_value(mut vm, '(let ((v `(#1=(1 2) #1#))) (eq? (car v) (cadr v)))').is_true()
75 assert graph_syntax_value(mut vm, '(let ((v `(,(quote #1=(1 . #1#)))))
76 (eq? (car v) (cdr (car v))))').is_true()
77 assert graph_syntax_value(mut vm, "(let ((quasiquote (lambda (v) v)))
78 (let ((v (quasiquote '#1=(a . #1#)))) (eq? v (cdr v))))").is_true()
79}
80
81fn test_library_staging_and_library_discovery_keep_reader_graph_safety() {
82 mut vm := new()
83 assert graph_syntax_value(mut vm, "(define-library (reader graph)
84 (export datum) (import (scheme base)) (begin (define datum '#1=(leaf . #1#))))
85 (import (reader graph)) (eq? datum (cdr datum))").is_true()
86 dir := os.join_path(os.temp_dir(), 'vile-graph-library-${os.getpid()}-${time.sys_mono_now()}')
87 os.mkdir_all(os.join_path(dir, 'reader')) or { panic(err) }
88 defer { os.rmdir_all(dir) or {} }
89 os.write_file(os.join_path(dir, 'reader', 'bad.sld'), '#1=(define-library (reader bad) . #1#)') or { panic(err) }
90 vm.add_library_path(dir) or { panic(err) }
91 assert graph_syntax_value(mut vm, '(cond-expand ((library (reader bad)) #f) (else #t))').is_true()
92 // Includes can switch graph tracking on in the middle of an unlabelled
93 // body/declaration walk; its existing cursor stack must remain aligned.
94 cycle := os.join_path(dir, 'body.scm')
95 declarations := os.join_path(dir, 'declarations.scm')
96 os.write_file(cycle, '#1=(begin #1#)') or { panic(err) }
97 os.write_file(declarations, '#1=(cond-expand (else #1#))') or { panic(err) }
98 for source in ['(let () (include "${cycle}"))',
99 '(define-library (reader included) (include-library-declarations "${declarations}"))'] {
100 if _ := vm.eval(source) {
101 assert false, source
102 } else {
103 assert err.msg().contains('circular syntax'), err.msg()
104 }
105 }
106}
107
108fn test_quasiquote_graph_scratch_is_released_before_published_values_escape() {
109 mut region := arena.new(chunk_size: 1024 * 1024)
110 region.push()
111 mut vm := new()
112 value := graph_syntax_value(mut vm, '(let ((x 42)) `(#1=(,x "kept") #1#))')
113 assert region.owns(value.pointer())
114 assert region.owns(value.pair_pointer().car.pointer())
115 assert value.pair_pointer().car.pair_pointer().car.whole() == 42
116 // Both appearances are active template occurrences; each owns valid storage.
117 gc_collect()
118 text := write(value) or { panic(err) }
119 region.pop()
120 owned := text.clone()
121 region.free()
122 assert owned == '((42 "kept") (42 "kept"))'
123 unsafe { owned.free() }
124}
125
126fn test_failed_graph_literals_do_not_publish_partial_cached_placeholders() {
127 mut vm := new()
128 // Both occurrences refer to one labelled graph. The inactive branch is
129 // compiled first; its failure must not turn the active one into an empty pair.
130 for source in ["(if #f '#1=(#u8(256)) '#1#)", "(if #f '#1=#(#u8(1.0)) '#1#)",
131 "(if #f '#1=(#2=(ok) #u8(-1)) '#1#)"] {
132 if _ := vm.eval(source) {
133 assert false, source
134 } else {
135 assert err.msg().contains(if source.contains('1.0') { 'integer' } else { 'byte' }), err.msg()
136 }
137 assert vm.active.len == 0
138 assert graph_syntax_value(mut vm, '42').whole() == 42
139 }
140}
141
142fn test_eval_generated_libraries_preserve_pair_and_vector_graphs_without_reader_labels() {
143 for vector_cycle in [false, true] {
144 mut vm := new_with_options(Options{ max_steps: 2000 }) or { panic(err.msg()) }
145 make_data := if vector_cycle {
146 '(define data (vector #f)) (vector-set! data 0 data)'
147 } else {
148 "(define data (cons 'leaf '())) (set-cdr! data data)"
149 }
150 vm.eval(make_data) or { panic(err.msg()) }
151 vm.eval("(eval (list 'define-library '(generated graph) '(export result) '(import (scheme base))
152 (list 'begin (list 'define 'result (list 'quote data)))) (interaction-environment))") or { panic(err.msg()) }
153 vm.eval('(import (generated graph))') or { panic(err.msg()) }
154 expression := if vector_cycle {
155 '(eq? result (vector-ref result 0))'
156 } else {
157 '(eq? result (cdr result))'
158 }
159 assert vm.eval(expression) or { panic(err.msg()) }.is_true()
160 // A library publishes a literal snapshot rather than the mutable input graph.
161 assert !eqv(vm.lookup('data') or { panic(err.msg()) }, vm.lookup('result') or { panic(err.msg()) })
162 }
163}
164
165fn test_eval_generated_library_rejects_executable_cycles_and_recovers() {
166 mut vm := new_with_options(Options{ max_steps: 2000, max_frames: 64 }) or { panic(err.msg()) }
167 vm.eval("(define form (list 'begin #f)) (set-car! (cdr form) form)
168 (eval (list 'define-library '(generated circular) '(import (scheme base))
169 (list 'begin form)) (interaction-environment))") or { panic(err.msg()) }
170 if _ := vm.eval('(import (generated circular))') {
171 assert false
172 } else {
173 assert err.msg().contains('circular syntax'), err.msg()
174 }
175 assert vm.active.len == 0
176 assert vm.eval('(+ 20 22)') or { panic(err.msg()) }.whole() == 42
177}
178