interp: 얇은 typed IR과 트리 워킹 인터프리터 — coolc run
문서에만 있던 실행 의미가 코드가 된다. - ir.ml: AST를 얇은 IR로 낮춘다. `?`는 Result에 대한 match로 펼쳐지고, 타입 인자는 사라지며(단형화 없음), 한정 이름은 하나의 이름으로 접힌다. 이름은 낮추기 시점에 분류된다 — 실행 중에 "지역인가 전역인가"를 다시 묻지 않는다. - interp.ml: 검사하지 않는 인터프리터. 여기 도달한 프로그램은 이미 타입, effect, capability, ownership 검사를 통과했고, 같은 질문을 두 번 묻는 것은 두 번째 진실을 만드는 일이다. 권한의 유일한 출처는 런타임이다. 소스에는 capability를 만드는 문법이 없고, main은 자기가 선언한 것만 받는다. 파라미터에서 Console을 지우면 출력할 방법이 프로그램 안에 없다 — 보안 정리 (i)의 실행 시점 대응물. TaskScope의 뿌리도 같은 이유로 런타임이 준다. scope의 v0 실행 의미는 순차다. 구조가 먼저고 병렬성은 그 위의 최적화다. samples/run/은 이제 "검사를 통과한다"가 아니라 "이 값을 낸다"까지 말하고, test/가 같은 것을 검사한다. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019ZVDeU6KLuUVL3gs18Hm3E
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
// 실행 의미를 한 파일에 모은 것: ?, mut, struct, match, scope.
|
||||
//
|
||||
// 기대 출력:
|
||||
// 7
|
||||
// 6
|
||||
// err
|
||||
// in scope
|
||||
|
||||
pub capability Console {
|
||||
fn print(s: String) effects {Console.print}
|
||||
}
|
||||
|
||||
pub enum E {
|
||||
Bad,
|
||||
}
|
||||
|
||||
pub copyable struct P {
|
||||
x: Int,
|
||||
y: Int,
|
||||
}
|
||||
|
||||
pub fn half(n: Int) -> Result[Int, E] {
|
||||
if n % 2 == 0 {
|
||||
Ok(n / 2)
|
||||
} else {
|
||||
Err(Bad)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn twice(n: Int) -> Result[Int, E] {
|
||||
let a = half(n)?
|
||||
let b = half(a)?
|
||||
Ok(a + b)
|
||||
}
|
||||
|
||||
pub fn show_res(r: Result[Int, E]) -> String {
|
||||
match r {
|
||||
Ok(v) => Int.show(v),
|
||||
Err(_) => "err",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main(c: Console, root: TaskScope) effects {Console.print} {
|
||||
let mut total = 0
|
||||
let p = P { x: 3, y: 4 }
|
||||
total = total + p.x + p.y
|
||||
c.print(Int.show(total))
|
||||
c.print(show_res(twice(8)))
|
||||
c.print(show_res(twice(7)))
|
||||
scope sc = root {
|
||||
sc.spawn(fn() {
|
||||
c.print("in scope")
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// 실행되는 첫 프로그램.
|
||||
//
|
||||
// main은 자기가 선언한 capability만 받는다. Console을 파라미터에서 지우면
|
||||
// print할 방법이 프로그램 안에 없다 — ambient authority가 없다는 것의
|
||||
// 실행 시점 의미다.
|
||||
|
||||
pub capability Console {
|
||||
fn print(s: String) effects {Console.print}
|
||||
}
|
||||
|
||||
pub enum Shape {
|
||||
Circle(Int),
|
||||
Square(Int),
|
||||
}
|
||||
|
||||
pub fn area(s: Shape) -> Int {
|
||||
match s {
|
||||
Circle(r) => r * r * 3,
|
||||
Square(w) => w * w,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main(c: Console) effects {Console.print} {
|
||||
let shapes = [Circle(2), Square(3), Circle(1)]
|
||||
List.each(shapes, fn(s) {
|
||||
c.print(String.concat("area = ", Int.show(area(s))))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user