| 1 | module scheme |
| 2 | |
| 3 | import io |
| 4 | |
| 5 | @[heap] |
| 6 | struct SyntaxChunkReader { |
| 7 | data []u8 |
| 8 | chunk int |
| 9 | mut: |
| 10 | position int |
| 11 | } |
| 12 | |
| 13 | fn (mut r SyntaxChunkReader) read(mut destination []u8) !int { |
| 14 | if r.position == r.data.len { return io.Eof{} } |
| 15 | count := int_min(destination.len, int_min(r.chunk, r.data.len - r.position)) |
| 16 | copy(mut destination[..count], r.data[r.position..r.position + count]) |
| 17 | r.position += count |
| 18 | return count |
| 19 | } |
| 20 | |
| 21 | fn test_syntax_quote_abbreviation_works_with_transformers_with_syntax_and_vile() { |
| 22 | mut vm := new() |
| 23 | value := vm.eval("(define-syntax double (lambda (stx) (syntax-case stx () ((_ x) #'(+ x x))))) (double 21)") or { panic(err) } |
| 24 | assert value.as_integer() or { panic(err) } == 42 |
| 25 | assert (vm.eval("(syntax->datum (with-syntax ((x #'42)) #'x))") or { panic(err) }).as_integer() or { panic(err) } == 42 |
| 26 | assert (vm.eval_alt("syntax->datum(#'42)") or { panic(err) }).as_integer() or { panic(err) } == 42 |
| 27 | } |
| 28 | |
| 29 | fn test_syntax_abbreviation_port_framing_handles_every_prefix_split_and_leaves_next_datum() { |
| 30 | source := "#'42 99 #`(a #,b #,@c) #'last" |
| 31 | for chunk in [1, 2, 3, 7] { |
| 32 | mut vm := new() |
| 33 | mut bytes := &SyntaxChunkReader{ data: source.bytes(), chunk: chunk } |
| 34 | input := vm.input_port(bytes, read_size: 1) or { panic(err) } |
| 35 | for expected in ['(syntax 42)', '99', '(quasisyntax (a (unsyntax b) (unsyntax-splicing c)))', |
| 36 | '(syntax last)'] { |
| 37 | assert write(vm.read(input) or { panic(err) }) or { panic(err) } == expected |
| 38 | } |
| 39 | assert (vm.read(input) or { panic(err) }).tag == .eof |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fn test_syntax_abbreviation_port_prefix_eof_cleans_reader_state() { |
| 44 | for source in ["#'", '#`', '#,', '#,@'] { |
| 45 | mut vm := new() |
| 46 | mut bytes := &SyntaxChunkReader{ data: source.bytes(), chunk: 1 } |
| 47 | input := vm.input_port(bytes, read_size: 1) or { panic(err) } |
| 48 | if _ := vm.read(input) { |
| 49 | assert false, source |
| 50 | } else { |
| 51 | assert err.msg().len > 0 |
| 52 | } |
| 53 | assert !input.input_pointer().busy && input.input_pointer().mark == -1 |
| 54 | } |
| 55 | } |
| 56 | |