パース可能な文法 Syntax
パース可能な文法をBNF1で示す。
なお、パース可能なだけでは意味論上の文法の規約を必ずしも満たすわけではない。
正しいコードの例
import bar::Bar;
fn add(x: Int, y: Int) -> Int {
x + y
}
struct Foo[T] {
a: Int,
b: Bar,
c: T,
}
impl[T] Foo[T] {
fn new(b: Bar, c: T) -> Self {
Self {
a = 0,
b = b,
c = c,
}
}
fn set_c(self, c: T) {
self.c = c;
}
}
<module>
::= <global-symbol>*
<global-symbol>
::= <import-declaration>
| <global-variable-definition>
| <function-definition>
| <type-definition>
| <implementation-block>
| <scene-definition>
モジュール(拡張子.biwaのファイルがモジュールそのものとなる)のトップレベルに置けるシンボルは、
- importの宣言 (<import-declaration>)
- グローバル変数の定義 ()
- 関数の定義 (<function-definition>)
- 型の定義 (<type-definition>)
implブロック ()- ノベルシーン定義 (<scene-definition>)
のみである。
<scene-definition>
::= scene <identifier> <argument-declaration> -> <type-representation> {{ <novel-mode> }}
ノベルゲームのシーンを記述する。
詳しくはノベルモード Novel Modeを参照。
<import-declaration>
::= import <qualified-identifier> (as <identifier>)? ;
他のパッケージやモジュールのシンボルを短い名前で使用できるようにする。
foo::bar::baz() という関数があるとき、
- そもそも
foo::bar::baz()のままで使用することが可能である。 import宣言で短縮した名前で使用することが可能である。import foo::bar;としたとき、bar::baz()で使用できる。import foo::bar::baz;としたとき、baz()で使用できる。
<function-definition>
::= fn <identifier> <type-generics-argument-declaration>? <argument-declaration> -> <type-representation> (<block-statement> | <block-expression>)
関数の定義。引数及び戻り値の型は明示する必要がある。
<argument-declaration>
::= ( (<identifier> : <type-representation> ,)* (<identifier> : <type-representation>)? )
<type-definition>
::= <struct-definition>
| <enum-definition>
| <type-alias-definition>
<struct-definition>
::= struct <identifier> <type-generics-argument-declaration> {
(<identifier> : <type-representation> ,)*
(<identifier> : <type-representation>)? }
構造体の定義。構造体はメンバを定義した型のメンバを保持できる。
struct Point {
x: Int,
y: Int,
}
のように構造体Pointを定義したとき、
以下のように初期化でき、メンバにアクセスして使用できる。
let p = Point { x = 1, y = 2, }; // 初期化
p.x // メンバアクセス。 1 が得られる
<global-variable-definition> ::= ("let" | "const") <identifier> (":" <type-representator>)? "=" <expression> ";"
// <literal> が限界かもしれない
<enum-definition> ::= "enum" <identifier> "{" (<identifier> ",")* <identifier>? }"
<interface-definition> ::= "interface" <identifier> "[" <type-generics-argument-list> "]" "{"
("fn" <identifier> "(" <argument-declaration-list> ")" "->" <type-representator> ",")*
("fn" <identifier> "(" <argument-declaration-list> ")" "->" <type-representator>)?
"}"
-
Backus-Naur Form / Backus Normal Form. バッカス・ナウア記法 / バッカス標準記法 とも。
<foo> ::= <bar>の形式で左辺のシンボルが右辺の表現に展開されることを示す。ここでは正規表現記法的な?,*などの使用を許すように拡張している。 ↩