module scheme import reader struct StarParameter { name string default_id reader.NodeId = reader.no_node } struct StarDeclaration { mut: required []string required_specializers []reader.NodeId optional []StarParameter keywords []StarParameter rest string has_keys bool allow_other bool } struct StarKey { name string slot int } // This metadata is compiled once per lambda*. A call scans the borrowed stack // packet and binds directly into its activation. It does not copy arguments. struct StarFormals { required int optional int keys []StarKey has_keys bool allow_other bool rest_slot int = -1 } fn (mut code Code) star_parameter(id reader.NodeId) !StarParameter { n := code.node(id)! if n.kind == .symbol { return StarParameter{ name: code.variable(id)! } } if n.kind != .pair { return failure('extended formal needs a name or default') } parts := code.nodes(id)! if parts.len != 2 { return failure('extended formal needs one default expression') } return StarParameter{ name: code.variable(parts[0])!, default_id: parts[1] } } fn (mut code Code) star_formals(id reader.NodeId, method bool) !StarDeclaration { if !isnil(code.graphs) { code.require_finite_spine(id)! } mut formals := StarDeclaration{} mut seen := map[string]bool{} mut current := id mut phase := 0 // required, optional, keyword, after allow-other-keys mut optional_seen := false mut key_seen := false for { n := code.node(current)! if n.kind == .empty_list { break } if n.kind == .symbol { formals.rest = code.variable(current)! key := code.binding_key(formals.rest) if key in seen { return failure('duplicate parameter ${formals.rest}') } break } if n.kind != .pair { return failure('invalid extended formal list') } item := code.node(n.car)! if item.kind == .keyword { marker_value := code.datum(n.car)! marker := marker_value.text() match marker { 'optional' { if optional_seen || key_seen { return failure('misplaced #:optional') } optional_seen = true phase = 1 } 'key' { if key_seen { return failure('duplicate #:key') } key_seen = true formals.has_keys = true phase = 2 } 'allow-other-keys' { if phase != 2 { return failure('misplaced #:allow-other-keys') } formals.allow_other = true phase = 3 } 'rest' { tail := code.node(n.cdr)! if tail.kind != .pair { return failure('#:rest needs one name') } formals.rest = code.variable(tail.car)! last := code.node(tail.cdr)! if last.kind != .empty_list { return failure('#:rest must be last') } key := code.binding_key(formals.rest) if key in seen { return failure('duplicate parameter ${formals.rest}') } break } else { return failure('unexpected formal keyword #:${marker}') } } current = n.cdr continue } if phase == 3 { return failure('keyword formal after #:allow-other-keys') } mut specializer := reader.no_node param := if phase == 0 { if method && item.kind == .pair { parts := code.nodes(n.car)! if parts.len != 2 { return failure('method argument needs a name and class') } specializer = parts[1] StarParameter{ name: code.variable(parts[0])! } } else { StarParameter{ name: code.variable(n.car)! } } } else { code.star_parameter(n.car)! } key := code.binding_key(param.name) if key in seen { return failure('duplicate parameter ${param.name}') } seen[key] = true match phase { 0 { formals.required << param.name if method { formals.required_specializers << specializer } } 1 { formals.optional << param } 2 { formals.keywords << param } else { return failure('invalid extended formal phase') } } current = n.cdr } return formals } fn (mut b Builder) star_child(declaration StarDeclaration, body []reader.NodeId, method bool) &Function { rest_slot := if declaration.rest == '' { -1 } else { declaration.required.len + declaration.optional.len } mut keys := []StarKey{cap: declaration.keywords.len} for index, param in declaration.keywords { keys << StarKey{ name: param.name slot: declaration.required.len + declaration.optional.len + if rest_slot < 0 { 0 } else { 1 } + index } } formals := &StarFormals{ required: declaration.required.len optional: declaration.optional.len keys: keys has_keys: declaration.has_keys allow_other: declaration.allow_other rest_slot: rest_slot } mut function := &Function{ code: b.code, params: declaration.required.len, star: formals } mut scope := new_compile_scope(Scope{ parent: b.scope, function: function, return_boundary: .function }) for name in declaration.required { scope.declare(name) } mut inner := Builder{ code: b.code, function: function, scope: scope, return_tail: true } for param in declaration.optional { inner.star_default(param) } if declaration.rest != '' { scope.declare(declaration.rest) } for param in declaration.keywords { inner.star_default(param) } if method && 'next-method' !in scope.names { function.goops_next_slot = scope.declare('next-method') } inner.body(body) inner.emit(.ret, 0, 0, if body.len > 0 { body.last() } else { reader.no_node }) inner.prepend_exit_mark(scope) return function } fn (mut b Builder) star_default(param StarParameter) { slot := b.function.slot_count origin := if param.default_id == reader.no_node { reader.no_node } else { param.default_id } skip := b.emit(.star_bound_skip, 0, slot, origin) if param.default_id == reader.no_node { b.constant(boolean(false), origin) } else { b.expression(param.default_id, false) } b.scope.declare(param.name) b.emit(.assign_local, 0, slot, origin) b.emit(.drop, 0, 0, origin) b.patch(skip, b.function.instructions.len) }