Files
coollang/bin/main.ml
T
coolguyandClaude Opus 5 1c4f46e5e5 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
2026-08-30 14:58:05 +09:00

102 lines
3.2 KiB
OCaml

let usage =
{|coollang toolchain
사용법:
coolc check <file.cool>... 타입/effect/capability 검사 (import를 따라 모듈 그래프 전체)
coolc iface <file.cool> interface 표면과 해시 출력
coolc run <file.cool> typed IR 인터프리터로 실행
coolc tokens <file.cool> 토큰 덤프 (렉서 디버깅)
coolc ast <file.cool> 구문 트리 덤프 (파서 디버깅)
coolc deps <file.cool> 외부 참조 목록 (모듈의 의존 표면)
coolc version 버전 출력
|}
let report_errors errors =
List.iter (fun e -> prerr_endline (Coollang.Driver.string_of_error e)) errors;
1
let report = function Ok () -> 0 | Error errors -> report_errors errors
let dump_tokens file =
match Coollang.Driver.tokens file with
| Error errors -> report_errors errors
| Ok tokens ->
List.iter (fun t -> print_endline (Coollang.Token.show t)) tokens;
0
let dump_ast file =
match Coollang.Driver.ast file with
| Error errors -> report_errors errors
| Ok m ->
List.iter
(fun it -> print_endline (Coollang.Ast.show_item it))
m.Coollang.Ast.items;
0
(* import를 따라 모듈 그래프를 로드하고 전부 검사한다. root는 첫 파일의 디렉터리다. *)
let check_graph files =
match files with
| [] ->
prerr_endline "검사할 파일이 없습니다";
2
| first :: _ ->
let st = Coollang.Session.create ~root:(Filename.dirname first) () in
List.iter (fun f -> Coollang.Session.load st f) files;
let errors = Coollang.Session.errors st in
List.iter
(fun e -> prerr_endline (Coollang.Session.string_of_error e))
errors;
if errors = [] then 0 else 1
let dump_iface file =
let st = Coollang.Session.create ~root:(Filename.dirname file) () in
Coollang.Session.load st file;
match Coollang.Session.find st file with
| None -> 1
| Some e ->
Printf.printf "hash %s\n" e.iface.Coollang.Iface.hash;
List.iter
(fun it -> print_endline (Coollang.Ast.show_item it))
e.iface.Coollang.Iface.items;
0
let dump_deps file =
match Coollang.Driver.resolve file with
| Error errors -> report_errors errors
| Ok info ->
List.iter
(fun (name, (p : Coollang.Token.pos)) ->
Printf.printf "%d:%d %s\n" p.line p.col name)
info.Coollang.Resolve.externals;
0
let () =
let argv = Array.to_list Sys.argv in
let code =
match List.tl argv with
| "check" :: files -> check_graph files
| [ "iface"; file ] -> dump_iface file
| [ "run"; file ] -> (
let st = Coollang.Session.create ~root:(Filename.dirname file) () in
match Coollang.Session.run st file with
| Ok out ->
print_string out;
0
| Error e ->
prerr_endline (Coollang.Session.string_of_error e);
1)
| [ "tokens"; file ] -> dump_tokens file
| [ "ast"; file ] -> dump_ast file
| [ "deps"; file ] -> dump_deps file
| [ "version" ] ->
print_endline Coollang.Version.string;
0
| [] | [ "help" ] | [ "--help" ] | [ "-h" ] ->
print_string usage;
0
| cmd :: _ ->
Printf.eprintf "알 수 없는 명령: %s\n\n%s" cmd usage;
2
in
exit code