Files
doslang-mirror/fec/tests/exec/lexer/ast.fe
T
coolguy e84f892147 Ferro 파서를 Ferro 로, 그리고 그것이 드러낸 네 가지
렉서 다음은 파서다. 노드는 한 배열에 살고 자식은 그 안의 인덱스다 -- 노드는
^Node 를 들 수 없고(여럿이며 한 번씩 소유하지 않는다) &Node 도 들 수 없다(R4).
인덱스는 둘 다 아니다. 소스도 필드가 아니라 매 단계에 같이 다닌다.

  unit demo / fn answer @2 / let n = (+ 1 (* 2 3)) / return n / balanced

전위 표기로 다시 찍는 것이 시험의 요점이다. 1 + 2 * 3 이 어떻게 묶였는지는
그렇게만 보인다.

쓰면서 나온 컴파일러 버그 넷:

1. 다른 유닛의 타입을 필드로 쓰면 그 필드 타입이 영영 UNKNOWN 이었다. 필드
   해석이 유닛마다 선언 직후에 돌아서, 아직 선언되지 않은 유닛의 타입을 찾다
   실패하고 그 답을 굳혔다. 이제 모든 유닛이 선언을 마친 뒤에 한 번 푼다.

2. 그리고 그 해석은 타입을 선언한 유닛에서 해야 한다. 필드 타입은 그 유닛의
   import 로 쓰였는데 아무 유닛에서나 풀고 있었다. 타입 계층에 enter/leave
   콜백을 두고 체커가 그 자리로 데려간다.

3. cycle_state 를 재귀 검사와 크기 계산이 같이 썼다. 첫 번째가 보는 중인 구조체
   가 두 번째에게는 다 끝난 것으로 보여서, 필드가 하나뿐인 것처럼 1 바이트로
   자리를 잡았다 -- Parser 가 그래서 자기 토큰을 밟았다. layout_state 로 나눴다.

4. 다른 유닛의 상수(ast.NONE)를 lowering 이 필드 접근으로 봤다. 체커가 이미
   링크 이름을 붙여두었으니 그것이 있으면 전역이다.

그리고 R1 을 실제로 지키게 했다: 소유자를 놓으면 그것이 가진 것도 놓는다.
전에는 자기 drop 이 있거나 자기가 owned 일 때만이어서, drop 을 가진 타입을
필드로 담은 구조체는 그것을 놓을 방법이 없었다(drop 은 손으로 못 부른다).
이제 release_at 이 drop 을 부르고 필드로 내려간다. 그 덕에 List/Arena/Map 의
drop 이 전부 필요 없어져서 지웠다 -- 버퍼가 owned 이니 R1 이 알아서 한다.

221/221, 29/29.
2026-08-17 12:49:53 +09:00

50 lines
1.5 KiB
Plaintext

unit ast;
// The tree the parser builds.
//
// Nodes live in one growing array and refer to each other by index, which is
// what SPEC R11 asks for: an owner that holds the values and handles that
// point at them. A node cannot hold a `^Node` for its children because a node
// has several and they are not each owned once; it cannot hold a `&Node`
// because R4 keeps borrows out of aggregate storage. An index is neither.
pub enum Shape {
Unit, // a: name
Fn, // a: name, b: first statement
Let, // a: name, b: value
Return, // a: value, or NONE
Binary, // a: left, b: right, from/len: the operator
Number,
Name,
Text,
Error,
}
/// No node. Zero is a real index, so the empty handle is the largest one.
pub const NONE: usize = 4294967295;
pub struct Node {
pub shape: Shape,
pub from: usize, // where in the source this came from
pub len: usize,
pub line: usize,
pub a: usize, // handles into the same tree
pub b: usize,
pub next: usize, // the following statement, when there is one
}
pub fn name_of(s: Shape) -> []u8 {
match s {
Unit => { return "unit"; }
Fn => { return "fn"; }
Let => { return "let"; }
Return => { return "return"; }
Binary => { return "binary"; }
Number => { return "number"; }
Name => { return "name"; }
Text => { return "text"; }
Error => { return "error"; }
}
return "?";
}