std: str 과 list, 그리고 자동 drop 이 사용자 타입까지 닿는다

std.str 은 eq/starts_with/find/trim/parse_int 을 바이트 슬라이스 위에서 한다.
std.list 는 자라는 제네릭 시퀀스다 -- 버퍼를 소유하므로 리스트를 놓으면
원소도 놓인다. 성장은 두 배씩이라 push 당 복사량이 상수로 눌린다.

찾은 버그 넷:

- 메서드가 자기 타입의 유닛이 아니라 호출한 유닛에 속한 것으로 계산됐다.
  다른 유닛의 제네릭 타입을 쓰면 필드가 전부 private 으로 보였다.
- 참조로 도달한 메서드를 찾지 못했다. self.grow() 가 안 됐다.
- 이미 참조인 수신자의 주소를 한 번 더 떠서 넘겼다. 포인터의 포인터를 받은
  메서드가 그것을 구조체로 읽었다.
- 유닛으로 한정된 제네릭 타입(list.List(i32))이 타입 자리에서도 식 자리에서도
  해석되지 않았다.

drop 을 가진 타입은 인스턴스마다 그 메서드가 존재해야 한다 -- 이름으로 부르는
사람이 없어도 스코프 정리가 부른다. 그리고 자기 drop 안에서는 필드를 꺼낼 수
있다. 객체가 사라지는 중이라 뒤에 남아 읽힐 것이 없다.

run.py 207/207, exec.py 19/19.
This commit is contained in:
2026-08-17 06:42:16 +09:00
parent b0c9338cf3
commit 206799d1cb
6 changed files with 317 additions and 18 deletions
+50 -3
View File
@@ -1,6 +1,53 @@
unit list;
unit std.list;
// A growable sequence. The buffer is owned, so a List owns its elements and
// releasing it releases them (SPEC 5 R1). Growth doubles, which keeps the
// total copying proportional to the number of pushes.
pub struct List(T) {
items: ^[]T,
items: ^[]mut T,
len: usize,
pub fn at(self: &Self, i: usize) -> &T;
pub fn with_capacity(n: usize) -> !Self {
let room: ^[]mut T = try mem.alloc_slice(T, n);
return Self{ items: room, len: 0 };
}
pub fn count(self: &Self) -> usize { return self.len; }
pub fn at(self: &Self, i: usize) -> T {
return self.items.^[i];
}
pub fn set(self: &mut Self, i: usize, v: T) -> void {
self.items.^[i] = v;
}
pub fn push(self: &mut Self, v: T) -> !void {
if self.len == self.items.^.n { try self.grow(); }
self.items.^[self.len] = v;
self.len = self.len + 1;
return;
}
/// Move to a buffer twice the size. Kept apart from `push` because the
/// borrow that hands over the old buffer must not be live while the old
/// buffer is still being read (SPEC 5 R6).
fn grow(self: &mut Self) -> !void {
var room: usize = self.items.^.n * 2;
if room == 0 { room = 4; }
let bigger: ^[]mut T = try mem.alloc_slice(T, room);
var i: usize = 0;
while i < self.len {
bigger.^[i] = self.items.^[i];
i = i + 1;
}
let old: ^[]mut T = mem.replace(&mut self.items, bigger);
mem.destroy(old);
return;
}
pub fn drop(self: &mut Self) -> void {
mem.destroy(self.items);
}
}
+66 -3
View File
@@ -1,3 +1,66 @@
unit str;
pub fn eq(a: str, b: str) -> bool;
pub fn trim(s: str) -> str;
unit std.str;
// `str` is `[]u8` (SPEC 4.2), so these take and give plain byte slices.
pub fn eq(a: []u8, b: []u8) -> bool {
if a.n != b.n { return false; }
var i: usize = 0;
while i < a.n {
if a[i] != b[i] { return false; }
i = i + 1;
}
return true;
}
pub fn starts_with(s: []u8, prefix: []u8) -> bool {
if prefix.n > s.n { return false; }
return eq(s[0..prefix.n], prefix);
}
/// Where `needle` first appears in `s`, or the length of `s` when it does not.
/// An index past the end is how "not found" is said without an optional.
pub fn find(s: []u8, needle: []u8) -> usize {
if needle.n == 0 { return 0; }
if needle.n > s.n { return s.n; }
var at: usize = 0;
let last: usize = s.n - needle.n;
while at <= last {
if eq(s[at..at + needle.n], needle) { return at; }
at = at + 1;
}
return s.n;
}
pub fn trim(s: []u8) -> []u8 {
var from: usize = 0;
var to: usize = s.n;
while from < to {
if s[from] != 32 and s[from] != 9 and s[from] != 10 and s[from] != 13 {
break;
}
from = from + 1;
}
while to > from {
let c: u8 = s[to - 1];
if c != 32 and c != 9 and c != 10 and c != 13 { break; }
to = to - 1;
}
return s[from..to];
}
pub fn parse_int(s: []u8) -> ?i32 {
if s.n == 0 { return null; }
var value: i32 = 0;
var i: usize = 0;
var negative: bool = false;
if s[0] == 45 { negative = true; i = 1; }
if i >= s.n { return null; }
while i < s.n {
let c: u8 = s[i];
if c < 48 or c > 57 { return null; }
value = value * 10 + ((c - 48) as i32);
i = i + 1;
}
if negative { return 0 - value; }
return value;
}