| 1 | module scheme |
| 2 | |
| 3 | import encoding.utf8 |
| 4 | import strings |
| 5 | import sync.stdatomic |
| 6 | |
| 7 | const data_primitive_descriptors = [ |
| 8 | PrimitiveDescriptor{'set-car!', Primitive.set_car} |
| 9 | PrimitiveDescriptor{'set-cdr!', Primitive.set_cdr} |
| 10 | PrimitiveDescriptor{'list-tail', Primitive.list_tail} |
| 11 | PrimitiveDescriptor{'list-ref', Primitive.list_ref} |
| 12 | PrimitiveDescriptor{'reverse', Primitive.reverse} |
| 13 | PrimitiveDescriptor{'memq', Primitive.memq} |
| 14 | PrimitiveDescriptor{'memv', Primitive.memv} |
| 15 | PrimitiveDescriptor{'member', Primitive.member} |
| 16 | PrimitiveDescriptor{'assq', Primitive.assq} |
| 17 | PrimitiveDescriptor{'assv', Primitive.assv} |
| 18 | PrimitiveDescriptor{'assoc', Primitive.assoc} |
| 19 | PrimitiveDescriptor{'string', Primitive.string_chars} |
| 20 | PrimitiveDescriptor{'make-string', Primitive.make_string} |
| 21 | PrimitiveDescriptor{'string-ref', Primitive.string_ref} |
| 22 | PrimitiveDescriptor{'string-set!', Primitive.string_set} |
| 23 | PrimitiveDescriptor{'substring', Primitive.substring} |
| 24 | PrimitiveDescriptor{'string-copy', Primitive.string_copy} |
| 25 | PrimitiveDescriptor{'string-fill!', Primitive.string_fill} |
| 26 | PrimitiveDescriptor{'string->list', Primitive.string_to_list} |
| 27 | PrimitiveDescriptor{'list->string', Primitive.list_to_string} |
| 28 | PrimitiveDescriptor{'string->symbol', Primitive.string_to_symbol} |
| 29 | PrimitiveDescriptor{'vector-set!', Primitive.vector_set} |
| 30 | PrimitiveDescriptor{'vector-fill!', Primitive.vector_fill} |
| 31 | PrimitiveDescriptor{'vector->list', Primitive.vector_to_list} |
| 32 | PrimitiveDescriptor{'list->vector', Primitive.list_to_vector} |
| 33 | PrimitiveDescriptor{'char-alphabetic?', Primitive.char_alphabetic} |
| 34 | PrimitiveDescriptor{'char-numeric?', Primitive.char_numeric} |
| 35 | PrimitiveDescriptor{'char-whitespace?', Primitive.char_whitespace} |
| 36 | PrimitiveDescriptor{'char-upper-case?', Primitive.char_upper} |
| 37 | PrimitiveDescriptor{'char-lower-case?', Primitive.char_lower} |
| 38 | PrimitiveDescriptor{'char->integer', Primitive.char_to_integer} |
| 39 | PrimitiveDescriptor{'integer->char', Primitive.integer_to_char} |
| 40 | PrimitiveDescriptor{'char-upcase', Primitive.char_upcase} |
| 41 | PrimitiveDescriptor{'char-downcase', Primitive.char_downcase} |
| 42 | PrimitiveDescriptor{'string=?', Primitive.string_eq} |
| 43 | PrimitiveDescriptor{'string<?', Primitive.string_less} |
| 44 | PrimitiveDescriptor{'string>?', Primitive.string_greater} |
| 45 | PrimitiveDescriptor{'string<=?', Primitive.string_le} |
| 46 | PrimitiveDescriptor{'string>=?', Primitive.string_ge} |
| 47 | PrimitiveDescriptor{'string-ci=?', Primitive.string_ci_eq} |
| 48 | PrimitiveDescriptor{'string-ci<?', Primitive.string_ci_less} |
| 49 | PrimitiveDescriptor{'string-ci>?', Primitive.string_ci_greater} |
| 50 | PrimitiveDescriptor{'string-ci<=?', Primitive.string_ci_le} |
| 51 | PrimitiveDescriptor{'string-ci>=?', Primitive.string_ci_ge} |
| 52 | PrimitiveDescriptor{'char=?', Primitive.char_eq} |
| 53 | PrimitiveDescriptor{'char<?', Primitive.char_less} |
| 54 | PrimitiveDescriptor{'char>?', Primitive.char_greater} |
| 55 | PrimitiveDescriptor{'char<=?', Primitive.char_le} |
| 56 | PrimitiveDescriptor{'char>=?', Primitive.char_ge} |
| 57 | PrimitiveDescriptor{'char-ci=?', Primitive.char_ci_eq} |
| 58 | PrimitiveDescriptor{'char-ci<?', Primitive.char_ci_less} |
| 59 | PrimitiveDescriptor{'char-ci>?', Primitive.char_ci_greater} |
| 60 | PrimitiveDescriptor{'char-ci<=?', Primitive.char_ci_le} |
| 61 | PrimitiveDescriptor{'char-ci>=?', Primitive.char_ci_ge}, |
| 62 | ]! |
| 63 | |
| 64 | fn (mut vm VM) install_data_primitives() { |
| 65 | for index in 0 .. data_primitive_descriptors.len { |
| 66 | entry := &data_primitive_descriptors[index] |
| 67 | vm.bind_global(entry.name, procedure_value(&Procedure{ |
| 68 | mode: if entry.primitive in [ |
| 69 | .member, |
| 70 | .assoc, |
| 71 | ] { |
| 72 | .search |
| 73 | } else { |
| 74 | .primitive |
| 75 | } |
| 76 | name: entry.name |
| 77 | owner: vm.owner |
| 78 | primitive: entry.primitive |
| 79 | })) |
| 80 | } |
| 81 | // R5RS includes every composed car/cdr accessor of depths two through four. |
| 82 | for depth in 2 .. 5 { |
| 83 | for mask in 0 .. (1 << depth) { |
| 84 | mut name := 'c' |
| 85 | for bit in 0 .. depth { name += if mask & (1 << bit) == 0 { 'a' } else { 'd' } } |
| 86 | name += 'r' |
| 87 | vm.bind_global(name, procedure_value(&Procedure{ mode: .primitive, name: name, owner: vm.owner, primitive: .cxr })) |
| 88 | } |
| 89 | } |
| 90 | vm.install_sequence_primitives() |
| 91 | } |
| 92 | |
| 93 | struct ListBuilder { |
| 94 | mut: |
| 95 | head Value = nil_value() |
| 96 | tail &Pair = unsafe { nil } |
| 97 | } |
| 98 | |
| 99 | fn (mut builder ListBuilder) push(value Value) { |
| 100 | node := &Pair{ car: value, cdr: nil_value() } |
| 101 | link := payload_value(.pair, node) |
| 102 | if isnil(builder.tail) { builder.head = link } else { builder.tail.cdr = link } |
| 103 | builder.tail = node |
| 104 | } |
| 105 | |
| 106 | fn (mut builder ListBuilder) finish(tail Value) Value { |
| 107 | if isnil(builder.tail) { return tail } |
| 108 | builder.tail.cdr = tail |
| 109 | return builder.head |
| 110 | } |
| 111 | |
| 112 | fn append_lists(args []Value) !Value { |
| 113 | if args.len == 0 { return nil_value() } |
| 114 | mut out := ListBuilder{} |
| 115 | for i in 0 .. args.len - 1 { |
| 116 | count := args[i].list_length()! |
| 117 | mut current := args[i] |
| 118 | for _ in 0 .. count { |
| 119 | pair := current.pair_pointer() |
| 120 | out.push(pair.car) |
| 121 | current = pair.cdr |
| 122 | } |
| 123 | } |
| 124 | return out.finish(args.last()) |
| 125 | } |
| 126 | |
| 127 | fn data_call(p Primitive, args []Value, name string) !Value { |
| 128 | if int(p) >= int(Primitive.make_list) { |
| 129 | if int(p) >= int(Primitive.bytevectorp) { return bytevector_call(p, args, name) } |
| 130 | return sequence_call(p, args, name) |
| 131 | } |
| 132 | minimum, maximum := match p { |
| 133 | .string_chars { 0, -1 } |
| 134 | .make_string { 1, 2 } |
| 135 | .set_car, .set_cdr, .list_tail, .list_ref, .string_ref, .memq, .memv, .member, .assq, |
| 136 | .assv, .assoc { |
| 137 | 2, 2 |
| 138 | } |
| 139 | .string_set, .vector_set, .substring { 3, 3 } |
| 140 | .string_copy, .string_to_list, .vector_to_list { 1, 3 } |
| 141 | .string_fill, .vector_fill { 2, 4 } |
| 142 | .string_eq, .string_less, .string_greater, .string_le, .string_ge, |
| 143 | .string_ci_eq, .string_ci_less, .string_ci_greater, .string_ci_le, .string_ci_ge, |
| 144 | .char_eq, .char_less, .char_greater, .char_le, .char_ge, |
| 145 | .char_ci_eq, .char_ci_less, .char_ci_greater, .char_ci_le, .char_ci_ge { |
| 146 | 2, -1 |
| 147 | } |
| 148 | else { 1, 1 } |
| 149 | } |
| 150 | check_arity(name, args.len, minimum, maximum)! |
| 151 | // These enums are contiguous groups of five order predicates each. |
| 152 | if int(p) >= int(Primitive.string_eq) && int(p) <= int(Primitive.string_ci_ge) { |
| 153 | mode := int(p) - int(Primitive.string_eq) |
| 154 | mut previous := string_data(args[0])! |
| 155 | mut matches := true |
| 156 | for i in 1 .. args.len { |
| 157 | current := string_data(args[i])! |
| 158 | if !order_matches(compare_text(previous, current, mode >= 5), mode % 5) { |
| 159 | matches = false |
| 160 | } |
| 161 | previous = current |
| 162 | } |
| 163 | return boolean(matches) |
| 164 | } |
| 165 | if int(p) >= int(Primitive.char_eq) && int(p) <= int(Primitive.char_ci_ge) { |
| 166 | mode := int(p) - int(Primitive.char_eq) |
| 167 | mut previous := args[0].as_character()! |
| 168 | if mode >= 5 { previous = unicode_simple_fold(previous) } |
| 169 | mut matches := true |
| 170 | for i in 1 .. args.len { |
| 171 | mut current := args[i].as_character()! |
| 172 | if mode >= 5 { current = unicode_simple_fold(current) } |
| 173 | order := if previous < current { |
| 174 | -1 |
| 175 | } else if previous > current { |
| 176 | 1 |
| 177 | } else { |
| 178 | 0 |
| 179 | } |
| 180 | if !order_matches(order, mode % 5) { matches = false } |
| 181 | previous = current |
| 182 | } |
| 183 | return boolean(matches) |
| 184 | } |
| 185 | match p { |
| 186 | .set_car, .set_cdr { |
| 187 | args[0].require_mutable(.pair)! |
| 188 | mut pair := args[0].pair_pointer() |
| 189 | if p == .set_car { pair.car = args[1] } else { pair.cdr = args[1] } |
| 190 | return unspecified() |
| 191 | } |
| 192 | .cxr { |
| 193 | mut current := args[0] |
| 194 | for i := name.len - 2; i >= 1; i-- { |
| 195 | a, d := current.pair_parts()! |
| 196 | current = if name[i] == `a` { a } else { d } |
| 197 | } |
| 198 | return current |
| 199 | } |
| 200 | .list_tail, .list_ref { |
| 201 | index := args[1].as_integer()! |
| 202 | if index < 0 { return failure('list index must be nonnegative') } |
| 203 | mut current := args[0] |
| 204 | for _ in i64(0) .. index { |
| 205 | if current.tag != .pair { return failure('list index out of range') } |
| 206 | current = current.pair_pointer().cdr |
| 207 | } |
| 208 | if p == .list_tail { return current } |
| 209 | a, _ := current.pair_parts()! |
| 210 | return a |
| 211 | } |
| 212 | .reverse { |
| 213 | count := args[0].list_length()! |
| 214 | mut current := args[0] |
| 215 | mut result := nil_value() |
| 216 | for _ in 0 .. count { |
| 217 | pair := current.pair_pointer() |
| 218 | result = cons(pair.car, result) |
| 219 | current = pair.cdr |
| 220 | } |
| 221 | return result |
| 222 | } |
| 223 | .memq, .memv, .member, .assq, .assv, .assoc { |
| 224 | mut current := args[1] |
| 225 | mut slow := current |
| 226 | mut steps := 0 |
| 227 | association := p in [.assq, .assv, .assoc] |
| 228 | structural := p in [.member, .assoc] |
| 229 | for current.tag == .pair { |
| 230 | pair := current.pair_pointer() |
| 231 | candidate := if association { |
| 232 | head, _ := pair.car.pair_parts()! |
| 233 | head |
| 234 | } else { |
| 235 | pair.car |
| 236 | } |
| 237 | matches := if structural { |
| 238 | equal(args[0], candidate) |
| 239 | } else { |
| 240 | eqv(args[0], candidate) |
| 241 | } |
| 242 | if matches { return if association { pair.car } else { current } } |
| 243 | current = pair.cdr |
| 244 | if !args[1].read_only || args[1].datum_graph { |
| 245 | steps++ |
| 246 | if steps % 2 == 0 { |
| 247 | slow = slow.pair_pointer().cdr |
| 248 | if current.tag == .pair && current.pointer() == slow.pointer() { |
| 249 | return failure('expected a proper list, got a circular list') |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | if current.tag != .nil { return failure('expected a proper list') } |
| 255 | return boolean(false) |
| 256 | } |
| 257 | .string_chars { |
| 258 | mut out := strings.new_buffer(args.len) |
| 259 | for value in args { out.write_string(value.as_character()!.str()) } |
| 260 | return owned_string(out.take_string(), args.len) |
| 261 | } |
| 262 | .make_string { |
| 263 | ch := if args.len == 2 { args[1].as_character()! } else { rune(` `) } |
| 264 | encoded := ch.str() |
| 265 | count := allocation_count(args[0], encoded.len)! |
| 266 | return owned_string(encoded.repeat(count), count) |
| 267 | } |
| 268 | .string_to_symbol { return text_value(.symbol, string_data(args[0])!.clone()) } |
| 269 | .string_copy { |
| 270 | return copy_string_range(args) |
| 271 | } |
| 272 | .string_ref { |
| 273 | _ := string_data(args[0])! |
| 274 | text := args[0].string_pointer() |
| 275 | offset := text_offset(text, args[1].as_integer()!)! |
| 276 | if offset == text.value.len { return failure('string index out of range') } |
| 277 | ch, _ := utf8.decode_rune_at(text.value, offset) |
| 278 | return character(ch) |
| 279 | } |
| 280 | .string_set { |
| 281 | args[0].require_mutable(.string)! |
| 282 | ch := args[2].as_character()! |
| 283 | mut target := args[0].string_pointer() |
| 284 | replace_character(mut target, args[1].as_integer()!, ch)! |
| 285 | target.revision++ |
| 286 | return unspecified() |
| 287 | } |
| 288 | .string_fill { |
| 289 | return fill_string_range(args) |
| 290 | } |
| 291 | .substring { |
| 292 | _ := string_data(args[0])! |
| 293 | text := args[0].string_pointer() |
| 294 | start, end := args[1].as_integer()!, args[2].as_integer()! |
| 295 | if end < start { return failure('substring end precedes start') } |
| 296 | first, last := text_offset(text, start)!, text_offset(text, end)! |
| 297 | return owned_string(text_view(text.value, first, last).clone(), int(end - start)) |
| 298 | } |
| 299 | .string_to_list { |
| 300 | text, first, last, _, _ := string_range(args, 0, 1)! |
| 301 | mut out := ListBuilder{} |
| 302 | for ch in text_view(text.value, first, last).runes_iterator() { |
| 303 | out.push(character(ch)!) |
| 304 | } |
| 305 | return out.finish(nil_value()) |
| 306 | } |
| 307 | .list_to_string { |
| 308 | count := args[0].list_length()! |
| 309 | mut current := args[0] |
| 310 | mut out := strings.new_buffer(count) |
| 311 | for _ in 0 .. count { |
| 312 | pair := current.pair_pointer() |
| 313 | out.write_string(pair.car.as_character()!.str()) |
| 314 | current = pair.cdr |
| 315 | } |
| 316 | return owned_string(out.take_string(), count) |
| 317 | } |
| 318 | .vector_set { |
| 319 | args[0].require_mutable(.vector)! |
| 320 | index := args[1].as_integer()! |
| 321 | mut content := args[0].vector_pointer() |
| 322 | if index < 0 || index >= content.items.len { |
| 323 | return failure('vector index out of range') |
| 324 | } |
| 325 | container_store_validated(mut content.items, int(index), args[2]) |
| 326 | return unspecified() |
| 327 | } |
| 328 | .vector_fill { |
| 329 | args[0].require_mutable(.vector)! |
| 330 | mut content := args[0].vector_pointer() |
| 331 | start, end := sequence_range(args, 2, content.items.len)! |
| 332 | for i in start .. end { container_store_validated(mut content.items, i, args[1]) } |
| 333 | return unspecified() |
| 334 | } |
| 335 | .vector_to_list { |
| 336 | if args[0].tag != .vector { return failure('expected a vector') } |
| 337 | items := args[0].vector_pointer().items |
| 338 | start, end := sequence_range(args, 1, items.len)! |
| 339 | mut out := ListBuilder{} |
| 340 | for i in start .. end { out.push(items[i]) } |
| 341 | return out.finish(nil_value()) |
| 342 | } |
| 343 | .list_to_vector { return payload_value(.vector, &Vector{ items: args[0].list_items()! }) } |
| 344 | .integer_to_char { |
| 345 | value := args[0].as_integer()! |
| 346 | if value < 0 || value > 0x10ffff { return failure('invalid Unicode character') } |
| 347 | return character(rune(value)) |
| 348 | } |
| 349 | .char_to_integer { return integer(i64(args[0].as_character()!)) } |
| 350 | .char_upcase { return character(unicode_simple_case(args[0].as_character()!, 0)) } |
| 351 | .char_downcase { return character(unicode_simple_case(args[0].as_character()!, 1)) } |
| 352 | .char_alphabetic, .char_numeric, .char_whitespace, .char_upper, .char_lower { |
| 353 | ch := args[0].as_character()! |
| 354 | return boolean(match p { |
| 355 | .char_alphabetic { unicode_alphabetic(ch) } |
| 356 | .char_numeric { unicode_decimal(ch) >= 0 } |
| 357 | .char_whitespace { unicode_white_space(ch) } |
| 358 | .char_upper { unicode_case_property(ch, true) } |
| 359 | else { unicode_case_property(ch, false) } |
| 360 | }) |
| 361 | } |
| 362 | else { return failure('unimplemented data primitive ${name}') } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | fn string_data(value Value) !string { |
| 367 | if value.tag != .string { return failure('expected a string, got ${value.tag}') } |
| 368 | return value.text() |
| 369 | } |
| 370 | |
| 371 | fn allocation_count(value Value, width int) !int { |
| 372 | count := value.as_integer()! |
| 373 | if count < 0 || count > max_int / width { return failure('invalid allocation length') } |
| 374 | return int(count) |
| 375 | } |
| 376 | |
| 377 | fn text_offset(text &StringData, index i64) !int { |
| 378 | if index < 0 || index > text.length { return failure('string index out of range') } |
| 379 | if text.length == text.value.len { return int(index) } |
| 380 | if index == text.length { return text.value.len } |
| 381 | revision := text.revision |
| 382 | position_revision := stdatomic.load_u64(&text.cursor_revision) |
| 383 | position := stdatomic.load_u64(&text.cursor_position) |
| 384 | mut start_index := i64(0) |
| 385 | mut offset := 0 |
| 386 | if position_revision == revision && i64(position >> 32) <= index { |
| 387 | start_index = i64(position >> 32) |
| 388 | offset = int(u32(position)) |
| 389 | } |
| 390 | for _ in start_index .. index { |
| 391 | if offset >= text.value.len { return failure('string index out of range') } |
| 392 | _, width := utf8.decode_rune_at(text.value, offset) |
| 393 | offset += width |
| 394 | } |
| 395 | // Publish the position first: a reader seeing the new revision must also |
| 396 | // see a position for that revision. String mutations already bump revision. |
| 397 | stdatomic.store_u64(&text.cursor_position, (u64(index) << 32) | u64(offset)) |
| 398 | stdatomic.store_u64(&text.cursor_revision, revision) |
| 399 | return offset |
| 400 | } |
| 401 | |
| 402 | fn compare_text(a string, b string, fold bool) int { |
| 403 | if fold { return compare_folded_text(a, b) } |
| 404 | mut left := a.runes_iterator() |
| 405 | mut right := b.runes_iterator() |
| 406 | for { |
| 407 | x := left.next() or { |
| 408 | _ := right.next() or { return 0 } |
| 409 | return -1 |
| 410 | } |
| 411 | y := right.next() or { return 1 } |
| 412 | lx := x |
| 413 | ry := y |
| 414 | if lx < ry { return -1 } |
| 415 | if lx > ry { return 1 } |
| 416 | } |
| 417 | return 0 |
| 418 | } |
| 419 | |
| 420 | fn order_matches(order int, mode int) bool { |
| 421 | return match mode { |
| 422 | 0 { order == 0 } |
| 423 | 1 { order < 0 } |
| 424 | 2 { order > 0 } |
| 425 | 3 { order <= 0 } |
| 426 | else { order >= 0 } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | fn replace_character(mut target StringData, index i64, ch rune) ! { |
| 431 | old := target.value |
| 432 | start := text_offset(target, index)! |
| 433 | if start == old.len { return failure('string index out of range') } |
| 434 | _, width := utf8.decode_rune_at(old, start) |
| 435 | if ch < 0x80 && width == 1 { |
| 436 | overwrite_owned_byte(target, start, u8(ch)) |
| 437 | return |
| 438 | } |
| 439 | replacement := ch.str() |
| 440 | if width == replacement.len { |
| 441 | overwrite_owned_text(target, start, replacement) |
| 442 | return |
| 443 | } |
| 444 | end := start + width |
| 445 | // Keep the old buffer rooted until the builder copies both borrowed ranges. |
| 446 | prefix := text_view(old, 0, start) |
| 447 | suffix := text_view(old, end, old.len) |
| 448 | mut out := strings.new_buffer(old.len - width + replacement.len) |
| 449 | out.write_string(prefix) |
| 450 | out.write_string(replacement) |
| 451 | out.write_string(suffix) |
| 452 | target.value = out.take_string() |
| 453 | } |
| 454 | |
| 455 | fn overwrite_owned_text(target &StringData, start int, replacement string) { |
| 456 | // Callers checked mutability, character bounds, and equal encoded widths. |
| 457 | // Mutable Text buffers are owned allocations; exported strings are clones. |
| 458 | unsafe { |
| 459 | mut bytes := &u8(target.value.str) |
| 460 | for i, byte in replacement { bytes[start + i] = byte } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | fn overwrite_owned_byte(target &StringData, index int, value u8) { |
| 465 | // String mutability and character/byte bounds were checked by the caller. |
| 466 | unsafe { |
| 467 | mut bytes := &u8(target.value.str) |
| 468 | bytes[index] = value |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | fn text_view(source string, start int, end int) string { |
| 473 | // Private caller validated the byte range and keeps source alive. The view |
| 474 | // is consumed only by a length-aware buffer writer, then discarded. |
| 475 | return unsafe { tos(source.str + start, end - start) } |
| 476 | } |
| 477 | |