vile / scheme / static_expand.v
140 lines · 133 sloc · 4.39 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import os
4import reader
5
6pub fn (mut vm VM) add_include_path(path string) ! {
7 vm.ready()!
8 resolved := source_path(path, '')!
9 if resolved !in vm.include_paths { vm.include_paths << resolved }
10}
11
12fn (mut b Builder) static_forms(name string, nodes []reader.NodeId, origin reader.NodeId) ![]reader.NodeId {
13 if b.code.expansion_limit > 0 && b.code.expansions >= b.code.expansion_limit {
14 return failure('source expansion limit exceeded')
15 }
16 b.code.expansions++
17 b.code.has_static = true
18 if name == 'cond-expand' { return b.conditional_forms(nodes) }
19 check_arity(name, nodes.len - 1, 1, -1)!
20 mut forms := []reader.NodeId{}
21 for file in nodes[1..] {
22 if b.code.node(file)!.kind != .string_literal {
23 return failure('${name} requires literal file names')
24 }
25 filename := string_data(b.code.atom_datum(file)!)!
26 path := b.include_path(filename, b.code.source_file(origin))!
27 doc := read_source_input(unsafe { &VM(b.code.compiler_vm) }, path: path) or {
28 return source_error(path, err)
29 }
30 forms << b.code.add_source(doc, path, origin)!
31 }
32 return forms
33}
34
35fn (b &Builder) include_path(filename string, caller string) !string {
36 path := source_path(filename, caller)!
37 if os.is_file(path) { return path }
38 if !os.is_abs_path(filename) {
39 if !isnil(b.code.compiler_vm) {
40 vm := unsafe { &VM(b.code.compiler_vm) }
41 for directory in vm.include_paths {
42 candidate := os.join_path(directory, filename)
43 if os.is_file(candidate) { return candidate }
44 }
45 }
46 candidate := source_path(filename, '')!
47 if os.is_file(candidate) { return candidate }
48 }
49 return failure('include file not found: ${path}')
50}
51
52fn (mut b Builder) conditional_forms(nodes []reader.NodeId) ![]reader.NodeId {
53 check_arity('cond-expand', nodes.len - 1, 1, -1)!
54 for i in 1 .. nodes.len {
55 parts := b.code.nodes(nodes[i])!
56 if parts.len == 0 { return failure('cond-expand clause must be nonempty') }
57 if b.code.node(parts[0])!.kind == .symbol && b.code.datum_name(parts[0])! == 'else' {
58 if i != nodes.len - 1 { return failure('cond-expand else clause must be last') }
59 return parts[1..]
60 }
61 if b.feature_requirement(parts[0])! { return parts[1..] }
62 }
63 return []reader.NodeId{}
64}
65
66struct FeatureCursor {
67 kind string
68 items []reader.NodeId
69mut:
70 next int
71}
72
73// Evaluate static requirements iteratively and short-circuit. Library tests
74// discover declarations but never run initializers or publish user libraries.
75fn (mut b Builder) feature_requirement(root reader.NodeId) !bool {
76 group_base := if isnil(b.code.graphs) { 0 } else { b.code.graphs.groups.len }
77 defer {
78 if !isnil(b.code.graphs) { b.code.graphs.groups.trim(group_base) }
79 }
80 mut current := root
81 mut pending := []FeatureCursor{}
82 mut value := false
83 mut complete := false
84 for {
85 if !complete {
86 node := b.code.node(current)!
87 if node.kind == .symbol {
88 value = b.code.feature_present(b.code.datum_name(current)!)
89 complete = true
90 } else {
91 parts := b.code.nodes(current)!
92 if parts.len == 0 { return failure('feature requirement must be nonempty') }
93 kind := b.library_identifier(parts[0])!
94 match kind {
95 'library' {
96 check_arity('library feature requirement', parts.len - 1, 1, 1)!
97 name := b.library_name(parts[1])!
98 value = if isnil(b.code.compiler_vm) {
99 false
100 } else {
101 unsafe { (&VM(b.code.compiler_vm)).library_available(name, b.code.source_file(current), b.code.source_library_root())! }
102 }
103 complete = true
104 }
105 'and', 'or', 'not' {
106 if kind == 'not' {
107 check_arity('not feature requirement', parts.len - 1, 1, 1)!
108 }
109 if parts.len == 1 {
110 value = kind == 'and'
111 complete = true
112 } else {
113 b.code.enter_syntax_group(current)!
114 pending << FeatureCursor{ kind: kind, items: unsafe { parts[1..] }, next: 1 }
115 current = parts[1]
116 continue
117 }
118 }
119 else { return failure('unknown feature requirement ${kind}') }
120 }
121 }
122 }
123 if pending.len == 0 { return value }
124 last := pending.len - 1
125 kind := pending[last].kind
126 if kind == 'not' {
127 value = !value
128 pending.pop()
129 if !isnil(b.code.graphs) { b.code.graphs.groups.pop() }
130 } else if (kind == 'and' && !value) || (kind == 'or' && value) || pending[last].next == pending[last].items.len {
131 pending.pop()
132 if !isnil(b.code.graphs) { b.code.graphs.groups.pop() }
133 } else {
134 current = pending[last].items[pending[last].next]
135 pending[last].next++
136 complete = false
137 }
138 }
139 return false
140}
141