| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | import arena |
| 5 | |
| 6 | // Items are already on the stack in source order. Repeated cons operations |
| 7 | // build the list in place without an intermediate array or a shadowable `list` |
| 8 | // binding. These allocations happen only when the class definition executes. |
| 9 | fn (mut b Builder) goops_stack_list(count int, origin reader.NodeId) { |
| 10 | b.constant(nil_value(), origin) |
| 11 | for _ in 0 .. count { b.emit(.qq_cons, 0, 0, origin) } |
| 12 | } |
| 13 | |
| 14 | fn (mut b Builder) goops_slot_syntax(id reader.NodeId, definition bool) ! { |
| 15 | if b.code.node(id)!.kind == .symbol { |
| 16 | b.constant(b.code.datum(id)!, id) |
| 17 | b.goops_stack_list(1, id) |
| 18 | return |
| 19 | } |
| 20 | parts := b.code.nodes(id)! |
| 21 | if parts.len == 0 || b.code.node(parts[0])!.kind != .symbol { |
| 22 | return failure('class slot name must be a symbol') |
| 23 | } |
| 24 | if (parts.len - 1) % 2 != 0 { return failure('class slot options need keyword/value pairs') } |
| 25 | mut items := parts.len |
| 26 | b.constant(b.code.datum(parts[0])!, id) |
| 27 | for index := 1; index < parts.len; index += 2 { |
| 28 | option := b.code.datum(parts[index])! |
| 29 | if option.tag != .keyword { return failure('class slot option must be a keyword') } |
| 30 | b.constant(option, parts[index]) |
| 31 | if option.text() == 'init-form' { |
| 32 | b.constant(b.code.datum(parts[index + 1])!, parts[index + 1]) |
| 33 | items += 2 |
| 34 | b.constant(keyword('init-thunk')!, parts[index]) |
| 35 | b.make_closure(b.expression_child(parts[index + 1]), parts[index + 1]) |
| 36 | } else if definition && option.text() in ['init-keyword', 'allocation'] { |
| 37 | value := b.code.datum(parts[index + 1])! |
| 38 | if value.tag != .keyword { return failure('slot ${option.text()} must be a keyword') } |
| 39 | b.constant(value, parts[index + 1]) |
| 40 | } else if definition && option.text() in ['getter', 'setter', 'accessor'] { |
| 41 | name := b.code.variable(parts[index + 1])! |
| 42 | b.declare_global_keyword(name) |
| 43 | depth, slot := b.goops_definition_slot(name)! |
| 44 | b.constant(b.code.datum(parts[index + 1])!, parts[index + 1]) |
| 45 | b.emit(if option.text() == 'accessor' { |
| 46 | .goops_define_accessor |
| 47 | } else { |
| 48 | .goops_define_generic |
| 49 | }, |
| 50 | depth, -slot - 1, parts[index + 1]) |
| 51 | } else { |
| 52 | b.expression(parts[index + 1], false) |
| 53 | } |
| 54 | } |
| 55 | b.goops_stack_list(items, id) |
| 56 | } |
| 57 | |
| 58 | fn (mut b Builder) goops_class_expression(supers_id reader.NodeId, items []reader.NodeId, name reader.NodeId, origin reader.NodeId) ! { |
| 59 | supers := b.code.nodes(supers_id)! |
| 60 | mut options := items.len |
| 61 | for index, item in items { |
| 62 | if b.code.node(item)!.kind == .keyword { |
| 63 | options = index |
| 64 | break |
| 65 | } |
| 66 | } |
| 67 | if (items.len - options) % 2 != 0 { return failure('class options need keyword/value pairs') } |
| 68 | // Definition syntax publishes all accessor bindings before class operands. |
| 69 | // A slot initializer can refer to an accessor declared by a later slot. |
| 70 | if name != reader.no_node { |
| 71 | for slot_id in items[..options] { |
| 72 | if b.code.node(slot_id)!.kind == .symbol { continue } |
| 73 | parts := b.code.nodes(slot_id)! |
| 74 | for index := 1; index + 1 < parts.len; index += 2 { |
| 75 | option := b.code.datum(parts[index])! |
| 76 | if option.tag != .keyword || option.text() !in ['getter', 'setter', 'accessor'] { |
| 77 | continue |
| 78 | } |
| 79 | accessor := b.code.variable(parts[index + 1])! |
| 80 | b.declare_global_keyword(accessor) |
| 81 | depth, slot := b.goops_definition_slot(accessor)! |
| 82 | b.constant(b.code.datum(parts[index + 1])!, parts[index + 1]) |
| 83 | b.emit(if option.text() == 'accessor' { |
| 84 | .goops_define_accessor |
| 85 | } else { |
| 86 | .goops_define_generic |
| 87 | }, depth, -slot - 1, parts[index + 1]) |
| 88 | b.emit(.drop, 0, 0, parts[index + 1]) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | for super in supers { b.expression(super, false) } |
| 93 | b.goops_stack_list(supers.len, supers_id) |
| 94 | for slot in items[..options] { b.goops_slot_syntax(slot, name != reader.no_node)! } |
| 95 | b.goops_stack_list(options, origin) |
| 96 | for index := options; index < items.len; index += 2 { |
| 97 | option := b.code.datum(items[index])! |
| 98 | if option.tag != .keyword { return failure('class option must be a keyword') } |
| 99 | b.constant(option, items[index]) |
| 100 | b.expression(items[index + 1], false) |
| 101 | } |
| 102 | if name != reader.no_node { |
| 103 | b.constant(keyword('name')!, origin) |
| 104 | b.constant(b.code.datum(name)!, name) |
| 105 | } |
| 106 | count := 2 + items.len - options + if name == reader.no_node { 0 } else { 2 } |
| 107 | b.emit(.goops_make_class, count, 0, origin) |
| 108 | } |
| 109 | |
| 110 | fn (mut b Builder) goops_class_definition(nodes []reader.NodeId, origin reader.NodeId) ! { |
| 111 | check_arity('define-class', nodes.len - 1, 2, -1)! |
| 112 | name := b.code.variable(nodes[1])! |
| 113 | b.declare_global_keyword(name) |
| 114 | b.goops_class_expression(nodes[2], nodes[3..], nodes[1], origin)! |
| 115 | depth, slot := b.goops_definition_slot(name)! |
| 116 | b.emit(.goops_class_redefinition, depth, slot, origin) |
| 117 | b.store(name, true, origin)! |
| 118 | } |
| 119 | |
| 120 | fn (mut b Builder) goops_accessor_definition(nodes []reader.NodeId, origin reader.NodeId) ! { |
| 121 | check_arity('define-accessor', nodes.len - 1, 1, 1)! |
| 122 | name := b.code.variable(nodes[1])! |
| 123 | b.declare_global_keyword(name) |
| 124 | depth, slot := b.goops_definition_slot(name)! |
| 125 | b.constant(b.code.datum(nodes[1])!, nodes[1]) |
| 126 | b.emit(.goops_define_accessor, depth, slot, origin) |
| 127 | } |
| 128 | |
| 129 | fn (mut b Builder) goops_generic_definition(nodes []reader.NodeId, origin reader.NodeId) ! { |
| 130 | check_arity('define-generic', nodes.len - 1, 1, 1)! |
| 131 | name := b.code.variable(nodes[1])! |
| 132 | b.declare_global_keyword(name) |
| 133 | depth, slot := b.goops_definition_slot(name)! |
| 134 | b.constant(b.code.datum(nodes[1])!, nodes[1]) |
| 135 | b.emit(.goops_define_generic, depth, slot, origin) |
| 136 | } |
| 137 | |
| 138 | fn (mut b Builder) goops_definition_slot(name string) !(int, int) { |
| 139 | binding := b.resolve_binding(name) |
| 140 | if binding.kind == .lexical { return b.syntax_binding_depth(binding)!, binding.slot } |
| 141 | return -1, b.name_index(b.global_definition_name(name)) |
| 142 | } |
| 143 | |
| 144 | fn (mut b Builder) goops_method_definition(nodes []reader.NodeId, origin reader.NodeId) ! { |
| 145 | check_arity('define-method', nodes.len - 1, 2, -1)! |
| 146 | signature := b.code.node(nodes[1])! |
| 147 | if signature.kind != .pair { return failure('define-method needs a generic name') } |
| 148 | name := b.code.variable(signature.car)! |
| 149 | b.declare_global_keyword(name) |
| 150 | depth, slot := b.goops_definition_slot(name)! |
| 151 | b.constant(b.code.datum(signature.car)!, signature.car) |
| 152 | rest := b.goops_method_body(signature.cdr, nodes[2..], origin)! |
| 153 | b.goops_method_metadata(signature.cdr, nodes[2..], origin)! |
| 154 | b.emit(.goops_define_method, depth, if rest { -slot - 1 } else { slot }, origin) |
| 155 | } |
| 156 | |
| 157 | fn (mut b Builder) goops_method_expression(nodes []reader.NodeId, origin reader.NodeId) ! { |
| 158 | check_arity('method', nodes.len - 1, 1, -1)! |
| 159 | rest := b.goops_method_body(nodes[1], nodes[2..], origin)! |
| 160 | b.goops_method_metadata(nodes[1], nodes[2..], origin)! |
| 161 | b.emit(.goops_make_method, if rest { 1 } else { 0 }, 0, origin) |
| 162 | } |
| 163 | |
| 164 | fn (mut b Builder) goops_method_body(formals reader.NodeId, body []reader.NodeId, origin reader.NodeId) !bool { |
| 165 | if !isnil(b.code.graphs) { b.code.require_finite_spine(formals)! } |
| 166 | mut signature := []reader.NodeId{} |
| 167 | mut cursor := formals |
| 168 | mut rest := '' |
| 169 | for { |
| 170 | item := b.code.node(cursor)! |
| 171 | if item.kind == .empty_list { break } |
| 172 | if item.kind == .symbol { |
| 173 | rest = b.code.variable(cursor)! |
| 174 | break |
| 175 | } |
| 176 | if item.kind != .pair { return failure('method formals must be a list') } |
| 177 | signature << item.car |
| 178 | cursor = item.cdr |
| 179 | } |
| 180 | mut params := []string{cap: signature.len} |
| 181 | mut seen := map[string]bool{} |
| 182 | for spec in signature { |
| 183 | item := b.code.node(spec)! |
| 184 | if item.kind == .symbol { |
| 185 | param := b.code.variable(spec)! |
| 186 | if b.code.binding_key(param) in seen { |
| 187 | return failure('duplicate method parameter ${param}') |
| 188 | } |
| 189 | seen[b.code.binding_key(param)] = true |
| 190 | params << param |
| 191 | b.emit(.goops_top, 0, 0, spec) |
| 192 | } else { |
| 193 | parts := b.code.nodes(spec)! |
| 194 | if parts.len != 2 { return failure('method argument needs a name and class') } |
| 195 | param := b.code.variable(parts[0])! |
| 196 | if b.code.binding_key(param) in seen { |
| 197 | return failure('duplicate method parameter ${param}') |
| 198 | } |
| 199 | seen[b.code.binding_key(param)] = true |
| 200 | params << param |
| 201 | b.expression(parts[1], false) |
| 202 | } |
| 203 | } |
| 204 | if rest != '' { |
| 205 | if b.code.binding_key(rest) in seen { return failure('duplicate method parameter ${rest}') } |
| 206 | } |
| 207 | b.goops_stack_list(params.len, formals) |
| 208 | b.make_closure(b.goops_method_child(params, rest, body), origin) |
| 209 | return rest != '' |
| 210 | } |
| 211 | |
| 212 | fn (mut b Builder) goops_method_star_definition(nodes []reader.NodeId, origin reader.NodeId) ! { |
| 213 | check_arity('define-method*', nodes.len - 1, 2, -1)! |
| 214 | if !isnil(b.code.graphs) { b.code.require_finite_spine(nodes[1])! } |
| 215 | signature := b.code.node(nodes[1])! |
| 216 | if signature.kind != .pair { return failure('define-method* needs a generic name') } |
| 217 | name := b.code.variable(signature.car)! |
| 218 | formals := b.code.star_formals(signature.cdr, true)! |
| 219 | b.declare_global_keyword(name) |
| 220 | depth, slot := b.goops_definition_slot(name)! |
| 221 | b.constant(b.code.datum(signature.car)!, signature.car) |
| 222 | b.goops_method_star_body(formals, nodes[2..], origin)! |
| 223 | b.goops_method_metadata(signature.cdr, nodes[2..], origin)! |
| 224 | rest := formals.optional.len > 0 || formals.has_keys || formals.rest != '' |
| 225 | b.emit(.goops_define_method, depth, if rest { -slot - 1 } else { slot }, origin) |
| 226 | } |
| 227 | |
| 228 | fn (mut b Builder) goops_method_star_expression(nodes []reader.NodeId, origin reader.NodeId) ! { |
| 229 | check_arity('method*', nodes.len - 1, 1, -1)! |
| 230 | formals := b.code.star_formals(nodes[1], true)! |
| 231 | b.goops_method_star_body(formals, nodes[2..], origin)! |
| 232 | b.goops_method_metadata(nodes[1], nodes[2..], origin)! |
| 233 | rest := formals.optional.len > 0 || formals.has_keys || formals.rest != '' |
| 234 | b.emit(.goops_make_method, if rest { 1 } else { 0 }, 0, origin) |
| 235 | } |
| 236 | |
| 237 | fn (mut b Builder) goops_method_star_body(formals StarDeclaration, body []reader.NodeId, origin reader.NodeId) ! { |
| 238 | for specializer in formals.required_specializers { |
| 239 | if specializer == reader.no_node { |
| 240 | b.emit(.goops_top, 0, 0, origin) |
| 241 | } else { |
| 242 | b.expression(specializer, false) |
| 243 | } |
| 244 | } |
| 245 | b.goops_stack_list(formals.required.len, origin) |
| 246 | b.make_closure(b.star_child(formals, body, true), origin) |
| 247 | } |
| 248 | |
| 249 | // Preserve public source metadata while removing only required specializers. |
| 250 | // Optional and keyword declarations retain their original syntax. |
| 251 | fn (mut b Builder) goops_method_metadata(formals reader.NodeId, body []reader.NodeId, origin reader.NodeId) ! { |
| 252 | mut names := []Value{} |
| 253 | mut cursor := formals |
| 254 | mut required := true |
| 255 | for { |
| 256 | node := b.code.node(cursor)! |
| 257 | if node.kind != .pair { break } |
| 258 | item := b.code.node(node.car)! |
| 259 | if item.kind == .keyword { required = false } |
| 260 | name := if required && item.kind == .pair { |
| 261 | b.code.datum(item.car)! |
| 262 | } else { |
| 263 | b.code.datum(node.car)! |
| 264 | } |
| 265 | names << name |
| 266 | cursor = node.cdr |
| 267 | } |
| 268 | mut declaration := b.code.datum(cursor)! |
| 269 | for index := names.len - 1; index >= 0; index-- { |
| 270 | declaration = literal_value(cons(names[index], declaration)) |
| 271 | } |
| 272 | mut expressions := nil_value() |
| 273 | for index := body.len - 1; index >= 0; index-- { |
| 274 | expressions = literal_value(cons(b.code.datum(body[index])!, expressions)) |
| 275 | } |
| 276 | if body.len == 0 { |
| 277 | expressions = literal_value(cons(literal_value(list([ |
| 278 | symbol('if', reader.ReadOptions{})!, |
| 279 | boolean(false), |
| 280 | boolean(false), |
| 281 | ])), nil_value())) |
| 282 | } |
| 283 | b.constant(declaration, origin) |
| 284 | b.constant(expressions, origin) |
| 285 | } |
| 286 | |
| 287 | fn (mut b Builder) goops_extended_constructor(origin reader.NodeId) ! { |
| 288 | if isnil(b.code.compiler_vm) { |
| 289 | return failure('extended generic definition needs a compilation VM') |
| 290 | } |
| 291 | vm := unsafe { &VM(b.code.compiler_vm) } |
| 292 | b.constant(vm.goops.extended_constructor, origin) |
| 293 | } |
| 294 | |
| 295 | fn (mut b Builder) goops_extended_definition(nodes []reader.NodeId, origin reader.NodeId) ! { |
| 296 | check_arity('define-extended-generic', nodes.len - 1, 2, 2)! |
| 297 | name := b.code.variable(nodes[1])! |
| 298 | b.declare_global_keyword(name) |
| 299 | b.goops_extended_constructor(origin)! |
| 300 | b.expression(nodes[2], false) |
| 301 | b.constant(b.code.datum(nodes[1])!, nodes[1]) |
| 302 | b.emit(.call, 2, 0, origin) |
| 303 | b.store(name, true, origin)! |
| 304 | } |
| 305 | |
| 306 | fn (mut code Code) goops_extended_prefixes(nodes []reader.NodeId) !([]reader.NodeId, []reader.NodeId) { |
| 307 | check_arity('define-extended-generics', nodes.len - 1, 3, 3)! |
| 308 | marker := code.datum(nodes[2])! |
| 309 | if marker.tag != .keyword || marker.text() != 'prefix' { |
| 310 | return failure('define-extended-generics needs #:prefix') |
| 311 | } |
| 312 | names := code.nodes(nodes[1])! |
| 313 | prefixes := code.nodes(nodes[3])! |
| 314 | for name in names { _ = code.variable(name)! } |
| 315 | for prefix in prefixes { _ = code.variable(prefix)! } |
| 316 | return names, prefixes |
| 317 | } |
| 318 | |
| 319 | // Guile's datum->syntax anchor is the unprefixed name, not the prefix token. |
| 320 | // Reuse the ordinary context capture and hydration path so manufactured names |
| 321 | // retain lexical and macro definition scopes even when absent from the input. |
| 322 | fn (mut b Builder) goops_prefixed_reference(name reader.NodeId, prefix reader.NodeId) !reader.NodeId { |
| 323 | mut storage := arena.new(chunk_size: 1024) |
| 324 | defer { storage.free() } |
| 325 | mut origins := new_syntax_input_origins(storage) |
| 326 | input := b.procedural_input(name, mut origins)! |
| 327 | raw := b.code.datum_name(prefix)! + b.code.datum_name(name)! |
| 328 | output := datum_to_syntax(input, text_value(.symbol, raw))! |
| 329 | return b.hydrate_procedural_output(output, (syntax_object_ref(input)!).data, origins, name) |
| 330 | } |
| 331 | |
| 332 | fn (mut b Builder) goops_extended_definitions(nodes []reader.NodeId, origin reader.NodeId) ! { |
| 333 | names, prefixes := b.code.goops_extended_prefixes(nodes)! |
| 334 | if names.len == 0 { |
| 335 | b.constant(unspecified(), origin) |
| 336 | return |
| 337 | } |
| 338 | for index, id in names { |
| 339 | name := b.code.variable(id)! |
| 340 | b.declare_global_keyword(name) |
| 341 | b.goops_extended_constructor(origin)! |
| 342 | for prefix in prefixes { b.expression(b.goops_prefixed_reference(id, prefix)!, false) } |
| 343 | b.goops_stack_list(prefixes.len, origin) |
| 344 | b.constant(b.code.datum(id)!, id) |
| 345 | b.emit(.call, 2, 0, origin) |
| 346 | b.store(name, true, origin)! |
| 347 | if index + 1 < names.len { b.emit(.drop, 0, 0, origin) } |
| 348 | } |
| 349 | } |
| 350 | |