| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | fn qq_text(source string) string { |
| 6 | mut vm := new() |
| 7 | value := vm.eval(source) or { panic('${source}: ${err}') } |
| 8 | return write(value) or { panic(err) } |
| 9 | } |
| 10 | |
| 11 | fn test_quasiquote_standard_examples_and_nested_levels() { |
| 12 | for source, expected in { |
| 13 | '`(list ,(+ 1 2) 4)': '(list 3 4)' |
| 14 | "(let ((name 'a)) `(list ,name ',name))": '(list a (quote a))' |
| 15 | '`(a ,(+ 1 2) ,@(list 4 5 6) b)': '(a 3 4 5 6 b)' |
| 16 | "`((foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons)))": '((foo 7) . cons)' |
| 17 | '`#(10 5 ,(+ 1 1) ,@(list 4 3) 8)': '#(10 5 2 4 3 8)' |
| 18 | '`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)': '(a (quasiquote (b (unquote (+ 1 2)) (unquote (foo 4 d)) e)) f)' |
| 19 | "(let ((name1 'x) (name2 'y)) `(a `(b ,,name1 ,',name2 d) e))": '(a (quasiquote (b (unquote x) (unquote (quote y)) d)) e)' |
| 20 | '(let ((x 42)) ``(a ,,x))': '(quasiquote (a (unquote 42)))' |
| 21 | '(quasiquote (list (unquote (+ 1 2)) 4))': '(list 3 4)' |
| 22 | '`42': '42' |
| 23 | '`()': '()' |
| 24 | '`#()': '#()' |
| 25 | '`(a b . c)': '(a b . c)' |
| 26 | '`,(+ 40 2)': '42' |
| 27 | '`(,@(list) ,@(list 1 2) ,@(list) . 3)': '(1 2 . 3)' |
| 28 | '`#(,@(list) ,@(list 1 2) ,@(list))': '#(1 2)' |
| 29 | '(let ((cons #f) (append #f) (list->vector #f)) `#(1 ,@(list 2) ,(+ 1 2)))': '#(1 2 3)' |
| 30 | '(let ((x 0)) `(,(begin (set! x (+ x 1)) x) ,(begin (set! x (+ x 1)) x) ,x))': '(1 2 2)' |
| 31 | } { |
| 32 | assert qq_text(source) == expected, source |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | fn test_quasiquote_splices_copy_prefixes_and_share_unquoted_values() { |
| 37 | for source, expected in { |
| 38 | '(let ((x (list 1 2))) (let ((y `(0 ,@x 3))) (set-car! x 99) (list x y)))': '((99 2) (0 1 2 3))' |
| 39 | '(let ((x (list 1 2))) (eq? (cdr `(0 . ,x)) x))': '#t' |
| 40 | '(let ((x (list 1 2))) (eq? (car `(,x)) x))': '#t' |
| 41 | '(let ((x (list 1 2))) (let ((y `#(,@x))) (set-car! x 99) y))': '#(1 2)' |
| 42 | '(let ((x (list 1 2))) (let ((y `(,x))) (set-car! (car y) 42) x))': '(42 2)' |
| 43 | '(let ((x 1)) (let ((y `(,x 2 3))) (set-car! y 42) y))': '(42 2 3)' |
| 44 | } { |
| 45 | assert qq_text(source) == expected, source |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn test_quasiquote_rejects_invalid_templates_and_nonlist_splices() { |
| 50 | mut vm := new() |
| 51 | for source in ['(quasiquote)', '(quasiquote a b)', ',42', ',@(list 1)', '`,@(list 1)', |
| 52 | '`(1 . ,@(list 2))', '`(,@1)', '`#(,@1)', '`(unquote)', '`(unquote 1 2)', '`(quasiquote 1 2)', |
| 53 | '`(a (unquote-splicing))', '(let ((x (list 1))) (set-cdr! x x) `(,@x))'] { |
| 54 | if _ := vm.eval(source) { |
| 55 | assert false, source |
| 56 | } else { |
| 57 | assert err is EvalError, source |
| 58 | } |
| 59 | } |
| 60 | assert qq_text('(if #t 42 `(,@1))') == '42' |
| 61 | } |
| 62 | |
| 63 | fn test_quasiquote_constants_and_static_suffixes_need_no_runtime_reconstruction() { |
| 64 | code := &Code{ doc: reader.read_all('`(a b c)') or { panic(err) } } |
| 65 | function := compile_program(code, code.doc.forms) |
| 66 | assert function.instructions.len == 2 |
| 67 | assert function.instructions[0].op == .constant |
| 68 | assert function.instructions[1].op == .ret |
| 69 | code2 := &Code{ doc: reader.read_all('(lambda (x) `(,x a b c))') or { panic(err) } } |
| 70 | function2 := compile_program(code2, code2.doc.forms).children[0] |
| 71 | assert function2.instructions.len == 4 |
| 72 | assert function2.instructions[0].op == .local |
| 73 | assert function2.instructions[1].op == .constant |
| 74 | assert function2.instructions[2].op == .qq_cons |
| 75 | assert function2.instructions[3].op == .ret |
| 76 | code3 := &Code{ doc: reader.read_all('(lambda (x) `(' + '0 '.repeat(12000) + ',x))') or { panic(err) } } |
| 77 | function3 := compile_program(code3, code3.doc.forms).children[0] |
| 78 | assert function3.instructions.len == 5 |
| 79 | assert function3.instructions[3].op == .qq_prefix |
| 80 | } |
| 81 | |
| 82 | fn test_quasiquote_long_flat_templates_do_not_recurse_on_the_cdr_chain() { |
| 83 | mut vm := new() |
| 84 | value := vm.eval('`(' + '0 '.repeat(12000) + ',42)') or { panic(err) } |
| 85 | items := value.list_items() or { panic(err) } |
| 86 | assert items.len == 12001 |
| 87 | assert items.last().as_integer() or { panic(err) } == 42 |
| 88 | vector_value := vm.eval('`#(' + '0 '.repeat(12000) + ',42)') or { panic(err) } |
| 89 | vector_items := vector_value.vector_items() or { panic(err) } |
| 90 | assert vector_items.len == 12001 |
| 91 | assert vector_items.last().as_integer() or { panic(err) } == 42 |
| 92 | } |
| 93 | |