panic: panic/Never와 내장 테스트 — 문법을 먼저 고치고 대조 장치가 파서를 지적했다

순서가 요점이다. 문법에 test_decl과 panic_expr을 넣고 파서는 안 고친 채로
대조 장치를 돌렸더니 즉시 잡혔다:

  문장 300개 중 파서가 거부한 것 140개
  [1] 선언 (fn, struct, enum, capability, const)이(가) 필요합니다 — test 발견

파서를 따라가게 하니 다시 0건. 문법과 구현이 어긋나는 상태가 관측 가능한
것이 되었다는 뜻이다.

panic:
- 키워드다. prelude가 없어 함수로 두면 쓸 때마다 import해야 한다
- effect가 아니다. 경계 검사 하나에 {Panic}이 호출자 전부로 전염되면
  effect 절은 신호가 아니라 잡음이 된다
- Never는 어떤 타입 자리에도 놓인다. 없으면 panic을 match 팔에서 못 쓴다
- 언어 수준 recover 없음. 되감기 없음. 자원 해제 여부는 열어둔다
- 0으로 나누기, assert 실패가 이 하나로 모인다

test:
- 파라미터가 없어 capability를 받을 수 없고, 만들 문법도 없다. 그래서
  effect-free임이 증명된다 — 관례가 아니라 검사다. 시험해 보니 실제로
  "테스트는 effect를 수행할 수 없습니다"로 거부한다
- 일반 코드와 같은 타입/effect/move 검사를 받는다
- interface hash에서 제외 — 테스트를 고쳤다고 downstream이 재검사되면 안 된다
- 격리는 런타임의 일이다. 하나가 죽어도 나머지는 돈다

assert는 std/test.cool에 coollang으로 쓰였다 — panic 위의 설탕임이 코드로
보이고, std에서 본문이 있는 첫 함수가 됐다. 그 바람에 std/런타임 양방향
테스트가 걸렸고(본문 있는 함수에 런타임 구현을 요구했다), 그 구분을 넣었다.

samples/app/config.cool에 첫 테스트 넷.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ZVDeU6KLuUVL3gs18Hm3E
This commit is contained in:
2026-08-30 17:51:56 +09:00
co-authored by Claude Opus 5
parent 8ff35c5d9b
commit 78ef07d2ee
22 changed files with 551 additions and 80 deletions
+50 -19
View File
@@ -20,8 +20,9 @@ let compare_files files =
List.iter
(fun f ->
match Coollang.Lexer.lex_result (read f) with
| Error e -> Printf.printf " 렉서 오류 %s:%d:%d %s\n" f e.pos.line e.pos.col e.msg
| Ok toks ->
| Error e ->
Printf.printf " 렉서 오류 %s:%d:%d %s\n" f e.pos.line e.pos.col e.msg
| Ok toks -> (
let hand =
match Coollang.Parser.parse_result toks with
| Ok _ -> None
@@ -33,7 +34,7 @@ let compare_files files =
| Error e ->
Some (e.pos, Printf.sprintf "%s이(가) 필요한데 %s" e.expected e.got)
in
(match (hand, spec) with
match (hand, spec) with
| None, None -> ()
| Some _, Some _ -> ()
| None, Some (p, m) ->
@@ -81,7 +82,17 @@ let fuzz n =
done;
Printf.printf "\n문장 %d개 중 파서가 거부한 것 %d개\n" n !bad;
(* 어휘 층은 파서 문법에서 도달할 수 없다 — 분모에서 뺀다 *)
let lexical = [ "ident"; "int_lit"; "string_lit"; "str_char"; "escape"; "bool_lit"; "literal" ] in
let lexical =
[
"ident";
"int_lit";
"string_lit";
"str_char";
"escape";
"bool_lit";
"literal";
]
in
let all =
Coollang.Ebnf.expand g
|> List.map (fun (r : Coollang.Ebnf.rule) -> r.name)
@@ -97,18 +108,25 @@ let fuzz n =
let () =
if Array.length Sys.argv > 1 && Sys.argv.(1) = "--dump" then (
let g = match Coollang.Ebnf.parse_result (read "docs/grammar.ebnf") with
| Ok g -> g | Error e -> failwith (string_of_int e.line) in
let tokens = [ "ident"; "int_lit"; "string_lit"; "NEWLINE"; "char"; "digit"; "letter" ] in
let g =
match Coollang.Ebnf.parse_result (read "docs/grammar.ebnf") with
| Ok g -> g
| Error e -> failwith (string_of_int e.line)
in
let tokens =
[ "ident"; "int_lit"; "string_lit"; "NEWLINE"; "char"; "digit"; "letter" ]
in
for i = 1 to 8 do
Random.init i;
let toks = Coollang.Ebnf_gen.sentence ~tokens g in
Printf.printf "[%d] (%d토큰) " i (List.length toks);
List.iter (fun (t : Coollang.Token.t) ->
match t.kind with
| Coollang.Token.Eof -> ()
| Coollang.Token.Newline -> print_string "\\n "
| k -> Printf.printf "%s " (Coollang.Token.show_kind k)) toks;
List.iter
(fun (t : Coollang.Token.t) ->
match t.kind with
| Coollang.Token.Eof -> ()
| Coollang.Token.Newline -> print_string "\\n "
| k -> Printf.printf "%s " (Coollang.Token.show_kind k))
toks;
print_newline ()
done;
exit 0);
@@ -116,13 +134,16 @@ let () =
fuzz (if Array.length Sys.argv > 2 then int_of_string Sys.argv.(2) else 200);
exit 0);
if Array.length Sys.argv > 2 && Sys.argv.(1) = "--compare" then (
compare_files (Array.to_list (Array.sub Sys.argv 2 (Array.length Sys.argv - 2)));
compare_files
(Array.to_list (Array.sub Sys.argv 2 (Array.length Sys.argv - 2)));
exit 0);
if Array.length Sys.argv > 1 && Sys.argv.(1) = "--lexical" then (
print_string (Coollang.Lexical_doc.render ());
print_newline ();
exit 0);
let file = if Array.length Sys.argv > 1 then Sys.argv.(1) else "docs/grammar.ebnf" in
let file =
if Array.length Sys.argv > 1 then Sys.argv.(1) else "docs/grammar.ebnf"
in
match Coollang.Ebnf.parse_result (read file) with
| Error e -> Printf.printf "%s:%d: %s\n" file e.line e.msg
| Ok g ->
@@ -140,15 +161,21 @@ let () =
| Ref _ | RefArg _ -> ()
| Seq xs | Alt xs -> List.iter walk xs
| Opt e | Rep e -> walk e
| Except (a, b) -> walk a; walk b
| Except (a, b) ->
walk a;
walk b
in
List.iter (fun (r : Coollang.Ebnf.rule) -> walk r.body) g;
let ts = Hashtbl.fold (fun k () acc -> k :: acc) terms [] |> List.sort compare in
let ts =
Hashtbl.fold (fun k () acc -> k :: acc) terms [] |> List.sort compare
in
Printf.printf "\n문법이 쓰는 단말 %d개:\n %s\n" (List.length ts)
(String.concat " " ts);
(* 어휘 절의 이름은 파서 층에서 단말이다 *)
let tokens =
[ "ident"; "int_lit"; "string_lit"; "NEWLINE"; "letter"; "digit"; "char" ]
[
"ident"; "int_lit"; "string_lit"; "NEWLINE"; "letter"; "digit"; "char";
]
in
let g = Coollang.Ebnf.expand g in
let a = Coollang.Ebnf.analyze ~tokens g in
@@ -166,8 +193,12 @@ let () =
(Coollang.Ebnf.SS.elements (Coollang.Ebnf.first a n))))
[ "decl"; "stmt"; "primary"; "type"; "pattern"; "item" ];
let cs = Coollang.Ebnf.conflicts ~tokens ~greedy:[ "NEWLINE" ] g in
let real = List.filter (fun (c : Coollang.Ebnf.conflict) -> not c.c_greedy) cs in
let soft = List.filter (fun (c : Coollang.Ebnf.conflict) -> c.c_greedy) cs in
let real =
List.filter (fun (c : Coollang.Ebnf.conflict) -> not c.c_greedy) cs
in
let soft =
List.filter (fun (c : Coollang.Ebnf.conflict) -> c.c_greedy) cs
in
Printf.printf "\n== LL(1) 충돌: 진짜 %d건, greedy로 해소 %d건 ==\n"
(List.length real) (List.length soft);
List.iter