vile / scheme / star_formals.v
186 lines · 177 sloc · 5.73 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import reader
4
5struct StarParameter {
6 name string
7 default_id reader.NodeId = reader.no_node
8}
9
10struct StarDeclaration {
11mut:
12 required []string
13 required_specializers []reader.NodeId
14 optional []StarParameter
15 keywords []StarParameter
16 rest string
17 has_keys bool
18 allow_other bool
19}
20
21struct StarKey {
22 name string
23 slot int
24}
25
26// This metadata is compiled once per lambda*. A call scans the borrowed stack
27// packet and binds directly into its activation. It does not copy arguments.
28struct StarFormals {
29 required int
30 optional int
31 keys []StarKey
32 has_keys bool
33 allow_other bool
34 rest_slot int = -1
35}
36
37fn (mut code Code) star_parameter(id reader.NodeId) !StarParameter {
38 n := code.node(id)!
39 if n.kind == .symbol { return StarParameter{ name: code.variable(id)! } }
40 if n.kind != .pair { return failure('extended formal needs a name or default') }
41 parts := code.nodes(id)!
42 if parts.len != 2 { return failure('extended formal needs one default expression') }
43 return StarParameter{ name: code.variable(parts[0])!, default_id: parts[1] }
44}
45
46fn (mut code Code) star_formals(id reader.NodeId, method bool) !StarDeclaration {
47 if !isnil(code.graphs) { code.require_finite_spine(id)! }
48 mut formals := StarDeclaration{}
49 mut seen := map[string]bool{}
50 mut current := id
51 mut phase := 0 // required, optional, keyword, after allow-other-keys
52 mut optional_seen := false
53 mut key_seen := false
54 for {
55 n := code.node(current)!
56 if n.kind == .empty_list { break }
57 if n.kind == .symbol {
58 formals.rest = code.variable(current)!
59 key := code.binding_key(formals.rest)
60 if key in seen { return failure('duplicate parameter ${formals.rest}') }
61 break
62 }
63 if n.kind != .pair { return failure('invalid extended formal list') }
64 item := code.node(n.car)!
65 if item.kind == .keyword {
66 marker_value := code.datum(n.car)!
67 marker := marker_value.text()
68 match marker {
69 'optional' {
70 if optional_seen || key_seen { return failure('misplaced #:optional') }
71 optional_seen = true
72 phase = 1
73 }
74 'key' {
75 if key_seen { return failure('duplicate #:key') }
76 key_seen = true
77 formals.has_keys = true
78 phase = 2
79 }
80 'allow-other-keys' {
81 if phase != 2 { return failure('misplaced #:allow-other-keys') }
82 formals.allow_other = true
83 phase = 3
84 }
85 'rest' {
86 tail := code.node(n.cdr)!
87 if tail.kind != .pair { return failure('#:rest needs one name') }
88 formals.rest = code.variable(tail.car)!
89 last := code.node(tail.cdr)!
90 if last.kind != .empty_list { return failure('#:rest must be last') }
91 key := code.binding_key(formals.rest)
92 if key in seen { return failure('duplicate parameter ${formals.rest}') }
93 break
94 }
95 else { return failure('unexpected formal keyword #:${marker}') }
96 }
97 current = n.cdr
98 continue
99 }
100 if phase == 3 { return failure('keyword formal after #:allow-other-keys') }
101 mut specializer := reader.no_node
102 param := if phase == 0 {
103 if method && item.kind == .pair {
104 parts := code.nodes(n.car)!
105 if parts.len != 2 { return failure('method argument needs a name and class') }
106 specializer = parts[1]
107 StarParameter{ name: code.variable(parts[0])! }
108 } else {
109 StarParameter{ name: code.variable(n.car)! }
110 }
111 } else {
112 code.star_parameter(n.car)!
113 }
114 key := code.binding_key(param.name)
115 if key in seen { return failure('duplicate parameter ${param.name}') }
116 seen[key] = true
117 match phase {
118 0 {
119 formals.required << param.name
120 if method { formals.required_specializers << specializer }
121 }
122 1 { formals.optional << param }
123 2 { formals.keywords << param }
124 else { return failure('invalid extended formal phase') }
125 }
126 current = n.cdr
127 }
128 return formals
129}
130
131fn (mut b Builder) star_child(declaration StarDeclaration, body []reader.NodeId, method bool) &Function {
132 rest_slot := if declaration.rest == '' {
133 -1
134 } else {
135 declaration.required.len + declaration.optional.len
136 }
137 mut keys := []StarKey{cap: declaration.keywords.len}
138 for index, param in declaration.keywords {
139 keys << StarKey{
140 name: param.name
141 slot: declaration.required.len + declaration.optional.len +
142 if rest_slot < 0 { 0 } else { 1 } + index
143 }
144 }
145 formals := &StarFormals{
146 required: declaration.required.len
147 optional: declaration.optional.len
148 keys: keys
149 has_keys: declaration.has_keys
150 allow_other: declaration.allow_other
151 rest_slot: rest_slot
152 }
153 mut function := &Function{ code: b.code, params: declaration.required.len, star: formals }
154 mut scope := new_compile_scope(Scope{ parent: b.scope, function: function, return_boundary: .function })
155 for name in declaration.required { scope.declare(name) }
156 mut inner := Builder{ code: b.code, function: function, scope: scope, return_tail: true }
157 for param in declaration.optional {
158 inner.star_default(param)
159 }
160 if declaration.rest != '' { scope.declare(declaration.rest) }
161 for param in declaration.keywords {
162 inner.star_default(param)
163 }
164 if method && 'next-method' !in scope.names {
165 function.goops_next_slot = scope.declare('next-method')
166 }
167 inner.body(body)
168 inner.emit(.ret, 0, 0, if body.len > 0 { body.last() } else { reader.no_node })
169 inner.prepend_exit_mark(scope)
170 return function
171}
172
173fn (mut b Builder) star_default(param StarParameter) {
174 slot := b.function.slot_count
175 origin := if param.default_id == reader.no_node { reader.no_node } else { param.default_id }
176 skip := b.emit(.star_bound_skip, 0, slot, origin)
177 if param.default_id == reader.no_node {
178 b.constant(boolean(false), origin)
179 } else {
180 b.expression(param.default_id, false)
181 }
182 b.scope.declare(param.name)
183 b.emit(.assign_local, 0, slot, origin)
184 b.emit(.drop, 0, 0, origin)
185 b.patch(skip, b.function.instructions.len)
186}
187