grammar: v0 EBNF 확정과 열린 문법 결정 8건

열려 있던 문법 항목을 언어 철학에 따라 전부 닫고 EBNF로 고정한다.

- 제네릭 인자를 대괄호로 통일(List[a], map[Int, String](...)). <>는 식
  위치에서 비교 연산자와 갈리지 않아 turbofish라는 제2 표기를 부르는데,
  effect 규칙이 명시적 인스턴스화 문법을 요구하므로 회피할 수 없다.
  대괄호는 전위=리터럴 / 후위=인스턴스화로 위치가 결정해 LL(1)이고 표기가
  하나다. 대가로 인덱싱 연산자를 두지 않는다.
- 파라미터 규칙을 callable 한정에서 전 파라미터로 일반화. 기본은 빌림,
  own만 소유 이전. capability를 예외로 두지 않는 이유는 capability의 존재가
  이미 타입과 effects 절에 드러나기 때문이다 — 표기가 실을 정보는 소유 이동뿐.
- 문 구분은 줄바꿈(Go식 자동 삽입), 블록은 식, return은 조기 탈출 전용.
  if/match를 식으로 두면 재대입이 줄어 move 검사가 단순해진다.
- match 가드 없음. 가드는 exhaustiveness를 흐리고 SMT 없이는 _ 분기를
  강요한다. 철학 1의 대표 항목을 문법 편의와 바꾸지 않는다.
- ?는 Result 전용 하드코딩. 조기 탈출도 블록 종료이므로 scope의 join/cancel이
  ? 경로에서도 실행된다. v1 linear 도입 시 이 경로가 암묵 drop 문제와 만난다.
- 수식어 순서는 바인딩(own, mut) 먼저, 타입(affine) 나중.

docs/grammar.ebnf는 LL(1)을 설계 제약으로 명시하고, effect 합집합을
결과 위치 전용 프로덕션으로 새겨 파라미터 위치의 역산을 파서가 거부하게 한다.
samples/는 확정 표기로 전면 갱신.

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 02:09:58 +09:00
co-authored by Claude Opus 5
parent e3b19b1300
commit 630d12104a
9 changed files with 329 additions and 108 deletions
+181
View File
@@ -0,0 +1,181 @@
(* coollang v0 EBNF
*
* :
* =
* |
* [ ] (0 1)
* { } (0 )
* ( )
* " "
* (* *)
*
* : LL(1). backtracking 없음, .
* ( 2).
*)
(* ------------------------------------------------------------------ *)
(* *)
(* ------------------------------------------------------------------ *)
(* // . ( ) *)
(* NEWLINE :
* ident, , ")", "]", "}", "?", "return"
* NEWLINE . .
* .
* NEWLINE .
*)
ident = letter , { letter | digit | "_" } ;
int_lit = digit , { digit | "_" } ;
string_lit = '"' , { char - '"' } , '"' ;
bool_lit = "true" | "false" ;
literal = int_lit | string_lit | bool_lit ;
(* ------------------------------------------------------------------ *)
(* *)
(* ------------------------------------------------------------------ *)
module = { NEWLINE } , { item } ;
item = ( import | reexport | decl ) , NEWLINE ;
import = "import" , string_lit , "as" , ident ;
reexport = "reexport" , ident ;
decl = [ "pub" ] , ( fn_decl | struct_decl | enum_decl
| capability_decl | const_decl ) ;
(* ------------------------------------------------------------------ *)
(* *)
(* ------------------------------------------------------------------ *)
fn_decl = "fn" , ident , [ gen_params ] , "(" , [ params ] , ")" ,
[ eff_result ] , [ "->" , type ] , [ block ] ;
(* block . interface capability *)
struct_decl = [ "copyable" ] , "struct" , ident , [ gen_params ] ,
"{" , { field } , "}" ;
field = ident , ":" , type , "," , { NEWLINE } ;
enum_decl = "enum" , ident , [ gen_params ] , "{" , { variant } , "}" ;
variant = ident , [ "(" , type_list , ")" ] , "," , { NEWLINE } ;
capability_decl = "capability" , ident , "{" , { cap_method } , "}" ;
cap_method = "fn" , ident , "(" , [ params ] , ")" ,
[ eff_result ] , [ "->" , type ] , NEWLINE ;
const_decl = "const" , ident , ":" , type , "=" , expr ;
gen_params = "[" , gen_param , { "," , gen_param } , [ "," ] , "]" ;
gen_param = ident , [ ":" , "effects" ] ;
(* ident = , ": effects" = effect *)
params = param , { "," , param } , [ "," ] ;
param = [ "own" ] , [ "mut" ] , ident , ":" , type ;
(* = use(). own .
* : (own, mut) , (affine) type *)
(* ------------------------------------------------------------------ *)
(* effect *)
(* ------------------------------------------------------------------ *)
(* effect. *)
eff_result = "effects" , eff_union ;
eff_union = eff_atom , { "|" , eff_atom } ;
(* effect. .
* " " " " *)
eff_param = "effects" , eff_atom ;
eff_atom = ident | eff_set ;
eff_set = "{" , [ eff_name , { "," , eff_name } , [ "," ] ] , "}" ;
eff_name = ident , "." , ident ;
(* . capability identity *)
(* ------------------------------------------------------------------ *)
(* *)
(* ------------------------------------------------------------------ *)
type = fn_type | named_type ;
fn_type = [ "affine" ] , "fn" , "(" , [ type_list ] , ")" ,
[ eff_param ] , [ "->" , type ] ;
named_type = ident , [ type_args ] ;
type_args = "[" , type , { "," , type } , [ "," ] , "]" ;
type_list = type , { "," , type } , [ "," ] ;
(* ------------------------------------------------------------------ *)
(* *)
(* ------------------------------------------------------------------ *)
block = "{" , { NEWLINE } , { stmt } , "}" ;
stmt = ( let_stmt | return_stmt | assign_stmt | expr ) , NEWLINE ;
let_stmt = "let" , [ "mut" ] , pattern , [ ":" , type ] , "=" , expr ;
return_stmt = "return" , [ expr ] ;
assign_stmt = place , "=" , expr ;
place = ident , { "." , ident } ;
(* = stmt expr , Unit.
* return , return formatter *)
(* ------------------------------------------------------------------ *)
(* *)
(* ------------------------------------------------------------------ *)
expr = or_expr ;
or_expr = and_expr , { "||" , and_expr } ;
and_expr = cmp_expr , { "&&" , cmp_expr } ;
cmp_expr = add_expr , [ cmp_op , add_expr ] ;
cmp_op = "==" | "!=" | "<" | "<=" | ">" | ">=" ;
add_expr = mul_expr , { ( "+" | "-" ) , mul_expr } ;
mul_expr = unary , { ( "*" | "/" | "%" ) , unary } ;
unary = [ "!" | "-" ] , postfix ;
postfix = primary , { call_sfx | field_sfx | inst_sfx | "?" } ;
call_sfx = "(" , [ args ] , ")" ;
field_sfx = "." , ident ;
inst_sfx = type_args ;
(* "[" = , "[" = . *)
args = expr , { "," , expr } , [ "," ] ;
primary = literal
| ident
| list_lit
| struct_lit
| closure
| if_expr
| match_expr
| scope_expr
| "(" , expr , ")" ;
list_lit = "[" , [ expr , { "," , expr } , [ "," ] ] , "]" ;
(* : let xs: List[Int] = [] *)
struct_lit = ident , "{" , { field_init } , "}" ;
field_init = ident , ":" , expr , "," , { NEWLINE } ;
closure = "fn" , "(" , [ cl_params ] , ")" ,
[ eff_param ] , [ "->" , type ] , block ;
cl_params = cl_param , { "," , cl_param } , [ "," ] ;
cl_param = ident , [ ":" , type ] ;
(* . .
* error *)
if_expr = "if" , expr_ns , block , [ "else" , ( block | if_expr ) ] ;
match_expr = "match" , expr_ns , "{" , { arm } , "}" ;
arm = pattern , "=>" , ( expr | block ) , "," , { NEWLINE } ;
scope_expr = "scope" , ident , block ;
(* expr_ns = struct_lit expr.
* if/match/scope "{" struct
* , struct *)
(* ------------------------------------------------------------------ *)
(* *)
(* ------------------------------------------------------------------ *)
pattern = "_" | literal | ctor_pattern | ident ;
ctor_pattern = ident , "(" , pattern , { "," , pattern } , [ "," ] , ")" ;
(* . (exhaustiveness ) *)