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:
@@ -986,3 +986,118 @@ let () =
|
||||
(fun (e : Session.error) ->
|
||||
e.file = b && String.length e.message > 0 && has_sub e.message "빠진 경우")
|
||||
(Session.errors st))
|
||||
|
||||
(* ------------------------------------------------------------------ *)
|
||||
(* 인터프리터 *)
|
||||
(* *)
|
||||
(* 샘플은 이제 "검사를 통과한다"가 아니라 "이 값을 낸다"까지 말한다. *)
|
||||
(* 실행 가능한 명세가 되는 지점이고, v1이 백엔드를 바꿔도 남는다. *)
|
||||
(* ------------------------------------------------------------------ *)
|
||||
|
||||
let run_src src =
|
||||
let dir = Filename.concat (Filename.get_temp_dir_name ()) "cool_runtest" in
|
||||
ignore (Sys.command (Printf.sprintf "mkdir -p %s" (Filename.quote dir)));
|
||||
let f = Filename.concat dir "m.cool" in
|
||||
write f src;
|
||||
let st = Session.create ~root:dir () in
|
||||
Session.run st f
|
||||
|
||||
let console =
|
||||
"pub capability Console {\n\
|
||||
\ fn print(s: String) effects {Console.print}\n\
|
||||
}\n\n"
|
||||
|
||||
let outputs src expected =
|
||||
match run_src (console ^ src) with
|
||||
| Ok out -> out = expected
|
||||
| Error e ->
|
||||
Printf.printf " (실행 오류: %s)\n" (Session.string_of_error e);
|
||||
false
|
||||
|
||||
let () =
|
||||
check "산술과 출력"
|
||||
(outputs
|
||||
"pub fn main(c: Console) effects {Console.print} {\n\
|
||||
\ c.print(Int.show(2 + 3 * 4))\n\
|
||||
}"
|
||||
"14\n");
|
||||
check "match와 생성자"
|
||||
(outputs
|
||||
"pub enum S {\n\
|
||||
\ A(Int),\n\
|
||||
\ B,\n\
|
||||
}\n\n\
|
||||
pub fn f(s: S) -> Int {\n\
|
||||
\ match s {\n\
|
||||
\ A(n) => n + 1,\n\
|
||||
\ B => 0,\n\
|
||||
\ }\n\
|
||||
}\n\n\
|
||||
pub fn main(c: Console) effects {Console.print} {\n\
|
||||
\ c.print(Int.show(f(A(41))))\n\
|
||||
\ c.print(Int.show(f(B)))\n\
|
||||
}"
|
||||
"42\n0\n");
|
||||
check "mut 바인딩과 대입"
|
||||
(outputs
|
||||
"pub fn main(c: Console) effects {Console.print} {\n\
|
||||
\ let mut n = 1\n\
|
||||
\ n = n + 10\n\
|
||||
\ c.print(Int.show(n))\n\
|
||||
}"
|
||||
"11\n");
|
||||
check "?는 Err에서 즉시 반환한다"
|
||||
(outputs
|
||||
"pub enum E {\n\
|
||||
\ Bad,\n\
|
||||
}\n\n\
|
||||
pub fn half(n: Int) -> Result[Int, E] {\n\
|
||||
\ if n % 2 == 0 { Ok(n / 2) } else { Err(Bad) }\n\
|
||||
}\n\n\
|
||||
pub fn twice(n: Int) -> Result[Int, E] {\n\
|
||||
\ let a = half(n)?\n\
|
||||
\ let b = half(a)?\n\
|
||||
\ Ok(a + b)\n\
|
||||
}\n\n\
|
||||
pub fn show_res(r: Result[Int, E]) -> String {\n\
|
||||
\ match r {\n\
|
||||
\ Ok(v) => Int.show(v),\n\
|
||||
\ Err(_) => \"err\",\n\
|
||||
\ }\n\
|
||||
}\n\n\
|
||||
pub fn main(c: Console) effects {Console.print} {\n\
|
||||
\ c.print(show_res(twice(8)))\n\
|
||||
\ c.print(show_res(twice(7)))\n\
|
||||
}"
|
||||
"6\nerr\n");
|
||||
check "클로저가 바깥 capability를 잡는다"
|
||||
(outputs
|
||||
"pub fn main(c: Console) effects {Console.print} {\n\
|
||||
\ List.each([1, 2], fn(n) {\n\
|
||||
\ c.print(Int.show(n))\n\
|
||||
\ })\n\
|
||||
}"
|
||||
"1\n2\n");
|
||||
check "scope 블록은 순차로 돌고 나갈 때 join한다"
|
||||
(outputs
|
||||
"pub fn main(c: Console, root: TaskScope) effects {Console.print} {\n\
|
||||
\ scope sc = root {\n\
|
||||
\ sc.spawn(fn() { c.print(\"a\") })\n\
|
||||
\ sc.spawn(fn() { c.print(\"b\") })\n\
|
||||
\ }\n\
|
||||
\ c.print(\"after\")\n\
|
||||
}"
|
||||
"a\nb\nafter\n");
|
||||
(* 권한은 런타임에서만 온다. main이 선언하지 않으면 존재하지 않는다. *)
|
||||
check "선언하지 않은 capability는 실행 시점에도 없다"
|
||||
(match run_src "pub fn main() { }" with Ok "" -> true | _ -> false);
|
||||
check "런타임이 모르는 capability는 거절한다"
|
||||
(match
|
||||
run_src
|
||||
"pub capability Db {\n\
|
||||
\ fn read() effects {Db.read} -> Int\n\
|
||||
}\n\n\
|
||||
pub fn main(d: Db) effects {Db.read} { }"
|
||||
with
|
||||
| Error e -> has_sub e.message "제공하지 않습니다"
|
||||
| Ok _ -> false)
|
||||
|
||||
Reference in New Issue
Block a user