module scheme import os import time fn conditional_eval(source string) Value { mut vm := new_with_options(Options{ max_steps: 0 }) or { panic(err) } return vm.eval(source) or { panic('${source}: ${err}') } } fn test_features_are_truthful_immutable_cached_and_available_in_standard_libraries() { mut vm := new() features := vm.eval('(features)') or { panic(err) } mut current := features mut seen := map[string]bool{} for current.tag == .pair { pair := current.pair_pointer() assert pair.car.tag == .symbol assert pair.car.text() !in seen seen[pair.car.text()] = true current = pair.cdr } assert current.tag == .nil && 'vile' in seen && 'ieee-float' in seen && 'exact-complex' in seen for name in ['r7rs', 'r6rs', 'exact-closed', 'ratios'] { assert name !in seen } assert vm.eval('(eq? (features) (features))') or { panic(err) }.is_true() for source in ["(set-car! (features) 'forged)", "(set-cdr! (features) '())", '(features 1)'] { if _ := vm.eval(source) { assert false, source } else { assert err is EvalError } } assert vm.eval('(import (only (scheme base) features)) (pair? (features))') or { panic(err) }.is_true() vm.eval('(define features (lambda () (list (quote fake))))') or { // The source import protects its local name; use an independent VM below. assert err.msg().contains('imported binding') } mut other := new() assert other.eval('(define features (lambda () #f)) (let ((vile #f)) (cond-expand (vile 42) (else 0)))') or { panic(err) }.whole() == 42 } fn test_cond_expand_boolean_requirements_are_static_and_short_circuit() { for source in [ '(cond-expand (vile 42) (else missing))', '(cond-expand ((and vile ieee-float (not absent)) 42) (else 0))', '(cond-expand ((or absent (and)) 42) (else 0))', '(cond-expand ((or) missing) ((not (and)) missing) (else 42))', '(let ((and #f) (not #f) (vile #f)) (cond-expand ((and vile (not absent)) 42)))', '(cond-expand ((or vile (unknown bad)) 42) (else 0))', '(cond-expand ((and absent (unknown bad)) missing) (else 42))', '(call-with-values (lambda () (cond-expand (vile (values 20 22)))) +)', '(cond-expand (absent (include "must-not-open.scm")) (vile 42))', '(let () (cond-expand (vile (define answer 42))) answer)', '(let ((cond-expand +)) (cond-expand 20 22))', '(define-syntax select (syntax-rules () ((_) (cond-expand (vile 42))))) (select)', ] { assert conditional_eval(source).whole() == 42, source } assert conditional_eval('(cond-expand (absent missing))').tag == .unspecified assert conditional_eval('(cond-expand (vile))').tag == .unspecified assert conditional_eval('(cond-expand (vile (define answer 42))) answer').whole() == 42 assert conditional_eval('(cond-expand ((library (scheme base)) 42) (else 0))').whole() == 42 assert conditional_eval('(cond-expand ((library (scheme nonexistent)) 0) (else 42))').whole() == 42 } fn test_cond_expand_rejects_malformed_requirements_and_recovers_the_vm() { mut vm := new() for source in ['(cond-expand)', '(cond-expand ())', '(cond-expand (else 1) (vile 2))', '(cond-expand ((not) 1))', '(cond-expand ((not vile absent) 1))', '(cond-expand ((library) 1))', '(cond-expand ((library ()) 1))', '(cond-expand ((unknown vile) 1))', '(cond-expand (#t 1))'] { if _ := vm.eval(source) { assert false, source } else { assert err is EvalError, '${source}: ${err}' } assert vm.eval('42') or { panic(err) }.whole() == 42 } assert vm.eval_alt('cond-expand((vile,42),(else,0))') or { panic(err) }.whole() == 42 } fn test_library_availability_does_not_register_or_initialize_discovered_user_libraries() { root := os.join_path(os.temp_dir(), 'vile-feature-${os.getpid()}-${time.sys_mono_now()}') os.mkdir_all(root) or { panic(err) } defer { os.rmdir_all(root) or {} } os.write_file(os.join_path(root, 'present.sld'), '(define-library (present) (export value) (import (scheme base)) (begin (define value (/ 1 0))))') or { panic(err) } path := os.join_path(root, 'main.scm') os.write_file(path, '(cond-expand ((library (present)) 42) (else 0))') or { panic(err) } mut vm := new() assert vm.eval_file(path) or { panic(err) }.whole() == 42 if !isnil(vm.libraries) { for _, library in vm.libraries.entries { assert library.name.display != '(present)' } } os.write_file(os.join_path(root, 'present.sld'), '(define-library (other))') or { panic(err) } assert vm.eval_file(path) or { panic(err) }.whole() == 0 os.write_file(os.join_path(root, 'present.sld'), '(define-library (present)) (display "bad")') or { panic(err) } assert vm.eval_file(path) or { panic(err) }.whole() == 0 assert vm.eval('(define-library (inline)) (cond-expand ((library (inline)) 42) (else 0))') or { panic(err) }.whole() == 42 for _, library in vm.libraries.entries { if library.name.display == '(inline)' { assert library.status == .registered } } } fn test_static_selection_handles_deep_requirements_and_preserves_tail_space() { mut predicate := 'vile' for _ in 0 .. 200 { predicate = '(not ${predicate})' } assert conditional_eval('(cond-expand (${predicate} 42) (else 0))').whole() == 42 mut vm := new_with_options(Options{ max_steps: 0, max_frames: 10 }) or { panic(err) } assert vm.eval('(define (loop n) (cond-expand (vile (if (= n 0) 42 (loop (- n 1)))))) (loop 100000)') or { panic(err) }.whole() == 42 }