ast.Node 가 이름이 무엇으로 해석됐는지 들고, Map 이 저장소를 유지한 채 키만 잊는다. 스코프가 끝날 때 표를 다음 스코프에 넘기는 것이 리졸버가 원하는 모양이다 -- 함수마다가 아니라 중첩 단계마다 표 하나. bind 는 Name 노드의 남는 a 필드를 재활용할 수도 있었지만 명시적인 쪽을 골랐다. 노드가 32 에서 36 바이트가 되는 값으로 그 자리가 무엇인지 이름이 말한다. clear 는 아무도 부르지 않는 채로 들어와 있었다. maps.fe 가 이제 부른다: 키가 사라지고, 방은 64 로 남고, 그 위에 다시 채워도 버퍼를 새로 잡지 않는다. cleared 0 room 64 gone 0 / refilled 3 / balanced GOAL.md 를 더했다. 외부 감사와, 그 항목들을 실제로 빌드해서 확인한 결과를 합친 P0~P4 다. 228/228, 32/32.
54 lines
1.7 KiB
Plaintext
54 lines
1.7 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
|
|
/// What a `Name` resolved to: the handle of the `Let` or `Fn` that
|
|
/// declared it. The resolver writes this back so the tree carries its own
|
|
/// answers and nothing has to look the name up a second time.
|
|
pub bind: usize,
|
|
}
|
|
|
|
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 "?";
|
|
}
|