module scheme // This first pass reports malformed keyword packets before an activation can // reuse the caller's tail environment. It borrows the machine's argument // stack and does not allocate or retain an argument array. @[direct_array_access] fn (m &Machine) validate_star_arguments(formals &StarFormals, base int, argc int) !int { mut cursor := formals.required for _ in 0 .. formals.optional { if cursor >= argc || (formals.has_keys && m.stack[base + 1 + cursor].tag == .keyword) { break } cursor++ } if !formals.has_keys { return cursor } mut position := cursor for position < argc { argument := m.stack[base + 1 + position] if argument.tag != .keyword { if formals.rest_slot < 0 { return failure('non-keyword argument after keyword formals') } position++ continue } if position + 1 >= argc { return failure('keyword argument has no value') } mut known := false for key in formals.keys { if argument.text() == key.name { known = true break } } if !known && !formals.allow_other { return failure('unknown keyword argument') } position += 2 } return cursor } @[direct_array_access] fn (mut m Machine) bind_star_arguments(vm &VM, mut scope Environment, formals &StarFormals, base int, argc int, optional_end int) ! { for index in 0 .. formals.required { scope.slots[index] = m.stack[base + 1 + index] } for index in formals.required .. optional_end { scope.slots[index] = m.stack[base + 1 + index] } if formals.has_keys { mut position := optional_end for position < argc { argument := m.stack[base + 1 + position] if argument.tag != .keyword { position++ continue } for key in formals.keys { if argument.text() == key.name { scope.slots[key.slot] = m.stack[base + 2 + position] break } } position += 2 } } if formals.rest_slot >= 0 { scope.slots[formals.rest_slot] = m.rest_arguments(vm, base, optional_end, argc)! } } @[inline] fn (m &Machine) rest_arguments(vm &VM, base int, first int, argc int) !Value { if first == argc { return nil_value() } items := unsafe { m.stack[base + 1 + first .. base + 1 + argc] } if vm.uses_bounded_operations() { return vm.owner.bounded_files.argument_list(items) } return list(items) }