| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | fn parsed_library_declaration(source string) &LibraryDeclaration { |
| 6 | code := &Code{ doc: reader.read_all(source) or { panic(err) } } |
| 7 | mut b := Builder{ code: code, function: &Function{ code: code } } |
| 8 | return b.library_declaration(code.nodes(code.doc.forms[0]) or { panic(err) }) or { panic('${source}: ${err}') } |
| 9 | } |
| 10 | |
| 11 | fn test_library_declarations_collect_imports_and_exports_separately_from_ordered_bodies() { |
| 12 | declaration := parsed_library_declaration('(define-library (EXAMPLE state 1) |
| 13 | (export value (rename next! advance)) |
| 14 | (begin (define value 40)) |
| 15 | (import (only (scheme base) define set! +)) |
| 16 | (begin (define (next!) (set! value (+ value 1)) value)) |
| 17 | (import (prefix (example helpers) h-)) |
| 18 | (export next!))') |
| 19 | assert declaration.name.display == '(example state 1)' |
| 20 | assert declaration.imports.len == 2 |
| 21 | assert declaration.imports[0].library.display == '(scheme base)' |
| 22 | assert declaration.imports[1].transforms[0].prefix == 'h-' |
| 23 | assert declaration.exports.map(it.internal) == ['value', 'next!', 'next!'] |
| 24 | assert declaration.exports.map(it.external) == ['value', 'advance', 'next!'] |
| 25 | assert declaration.forms.len == 2 |
| 26 | mut code := declaration.code |
| 27 | assert write(code.datum(declaration.forms[0]) or { panic(err) }) or { panic(err) } == '(define value 40)' |
| 28 | assert write(code.datum(declaration.forms[1]) or { panic(err) }) or { panic(err) } == '(define (next!) (set! value (+ value 1)) value)' |
| 29 | assert isnil(code.compiler_vm) && isnil(code.known_globals) |
| 30 | } |
| 31 | |
| 32 | fn test_empty_library_bodies_imports_and_exports_are_well_defined() { |
| 33 | for source in ['(define-library (example))', '(define-library (example) (export) (import) (begin))'] { |
| 34 | declaration := parsed_library_declaration(source) |
| 35 | assert declaration.forms.len == 0 && declaration.imports.len == 0 && declaration.exports.len == 0 |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | fn test_library_declaration_parser_rejects_invalid_names_clauses_and_export_specs() { |
| 40 | for source in ['(define-library)', '(define-library ())', '(define-library (example) ())', |
| 41 | '(define-library (example) (nonsense))', '(define-library (example) (42 x))', |
| 42 | '(define-library (example) (export 42))', '(define-library (example) (export (foo x y)))', |
| 43 | '(define-library (example) (export (rename x)))', |
| 44 | '(define-library (example) (export (rename x y z)))', |
| 45 | '(define-library (example) (export (rename 1 x)))', |
| 46 | '(define-library (example) (export (rename x 1)))', '(define-library (example) (begin . rest))', |
| 47 | '(define-library (example) (import (only (example) 42)))'] { |
| 48 | code := &Code{ doc: reader.read_all(source) or { panic(err) } } |
| 49 | mut b := Builder{ code: code, function: &Function{ code: code } } |
| 50 | if _ := b.library_declaration(code.nodes(code.doc.forms[0]) or { panic(err) }) { |
| 51 | assert false, source |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn test_generated_library_bodies_own_their_expansion_subtrees_after_parent_compilation_finishes() { |
| 57 | mut vm := new() |
| 58 | vm.eval('(define-syntax library-maker (syntax-rules () ((_ public) |
| 59 | (define-library (example generated) (export public) |
| 60 | (begin (define private 42) (define (public) private))))))') or { panic(err) } |
| 61 | mut code := vm.new_code(reader.read_all('(library-maker answer)') or { panic(err) }, vm.next_context(), unsafe { nil }, 'library.scm') |
| 62 | mut b := Builder{ code: code, function: &Function{ code: code } } |
| 63 | root := b.expand_syntax_head(code.doc.forms[0]) or { panic(err) } |
| 64 | declaration := b.library_declaration(code.nodes(root) or { panic(err) }) or { panic(err) } |
| 65 | assert declaration.exports[0].internal == 'answer' |
| 66 | assert declaration.exports[0].external == 'answer' |
| 67 | assert declaration.code.source_path == 'library.scm' |
| 68 | assert !isnil(declaration.code.generated) |
| 69 | b.finish_program() |
| 70 | assert code.generated.nodes.len == 0 |
| 71 | assert declaration.code.generated.nodes.len > 0 |
| 72 | gc_collect() |
| 73 | mut body_code := declaration.code |
| 74 | assert write(body_code.datum(declaration.forms[0]) or { panic(err) }) or { panic(err) } == '(define private 42)' |
| 75 | assert write(body_code.datum(declaration.forms[1]) or { panic(err) }) or { panic(err) } == '(define (answer) private)' |
| 76 | } |
| 77 | |