module scheme import os import reader pub fn (mut vm VM) add_include_path(path string) ! { vm.ready()! resolved := source_path(path, '')! if resolved !in vm.include_paths { vm.include_paths << resolved } } fn (mut b Builder) static_forms(name string, nodes []reader.NodeId, origin reader.NodeId) ![]reader.NodeId { if b.code.expansion_limit > 0 && b.code.expansions >= b.code.expansion_limit { return failure('source expansion limit exceeded') } b.code.expansions++ b.code.has_static = true if name == 'cond-expand' { return b.conditional_forms(nodes) } check_arity(name, nodes.len - 1, 1, -1)! mut forms := []reader.NodeId{} for file in nodes[1..] { if b.code.node(file)!.kind != .string_literal { return failure('${name} requires literal file names') } filename := string_data(b.code.atom_datum(file)!)! path := b.include_path(filename, b.code.source_file(origin))! doc := read_source_input(unsafe { &VM(b.code.compiler_vm) }, path: path) or { return source_error(path, err) } forms << b.code.add_source(doc, path, origin)! } return forms } fn (b &Builder) include_path(filename string, caller string) !string { path := source_path(filename, caller)! if os.is_file(path) { return path } if !os.is_abs_path(filename) { if !isnil(b.code.compiler_vm) { vm := unsafe { &VM(b.code.compiler_vm) } for directory in vm.include_paths { candidate := os.join_path(directory, filename) if os.is_file(candidate) { return candidate } } } candidate := source_path(filename, '')! if os.is_file(candidate) { return candidate } } return failure('include file not found: ${path}') } fn (mut b Builder) conditional_forms(nodes []reader.NodeId) ![]reader.NodeId { check_arity('cond-expand', nodes.len - 1, 1, -1)! for i in 1 .. nodes.len { parts := b.code.nodes(nodes[i])! if parts.len == 0 { return failure('cond-expand clause must be nonempty') } if b.code.node(parts[0])!.kind == .symbol && b.code.datum_name(parts[0])! == 'else' { if i != nodes.len - 1 { return failure('cond-expand else clause must be last') } return parts[1..] } if b.feature_requirement(parts[0])! { return parts[1..] } } return []reader.NodeId{} } struct FeatureCursor { kind string items []reader.NodeId mut: next int } // Evaluate static requirements iteratively and short-circuit. Library tests // discover declarations but never run initializers or publish user libraries. fn (mut b Builder) feature_requirement(root reader.NodeId) !bool { group_base := if isnil(b.code.graphs) { 0 } else { b.code.graphs.groups.len } defer { if !isnil(b.code.graphs) { b.code.graphs.groups.trim(group_base) } } mut current := root mut pending := []FeatureCursor{} mut value := false mut complete := false for { if !complete { node := b.code.node(current)! if node.kind == .symbol { value = b.code.feature_present(b.code.datum_name(current)!) complete = true } else { parts := b.code.nodes(current)! if parts.len == 0 { return failure('feature requirement must be nonempty') } kind := b.library_identifier(parts[0])! match kind { 'library' { check_arity('library feature requirement', parts.len - 1, 1, 1)! name := b.library_name(parts[1])! value = if isnil(b.code.compiler_vm) { false } else { unsafe { (&VM(b.code.compiler_vm)).library_available(name, b.code.source_file(current), b.code.source_library_root())! } } complete = true } 'and', 'or', 'not' { if kind == 'not' { check_arity('not feature requirement', parts.len - 1, 1, 1)! } if parts.len == 1 { value = kind == 'and' complete = true } else { b.code.enter_syntax_group(current)! pending << FeatureCursor{ kind: kind, items: unsafe { parts[1..] }, next: 1 } current = parts[1] continue } } else { return failure('unknown feature requirement ${kind}') } } } } if pending.len == 0 { return value } last := pending.len - 1 kind := pending[last].kind if kind == 'not' { value = !value pending.pop() if !isnil(b.code.graphs) { b.code.graphs.groups.pop() } } else if (kind == 'and' && !value) || (kind == 'or' && value) || pending[last].next == pending[last].items.len { pending.pop() if !isnil(b.code.graphs) { b.code.graphs.groups.pop() } } else { current = pending[last].items[pending[last].next] pending[last].next++ complete = false } } return false }