-
Notifications
You must be signed in to change notification settings - Fork 0
Language Specification
YCPL sourceは.ycファイルです。文は主に改行で区切り、// line commentと
ネスト可能な/* ... */ block commentを使えます。
module math.basic
import "std/fmt" as fmt
pub struct Point {
x i32
y i32
}
pub enum Color {
Red = 1,
Green,
Blue,
}
pub type Score = i32
pub fn add(a i32, b i32) i32 {
return a + b
}
fn main() {
fmt.println(add(1, 2))
}
対応するtop-level宣言はfunction、extern fn、intrinsic fn、struct、enum、
type alias、constです。pubを付けた宣言はimport先から参照できます。
intrinsic fnはbundled std module専用です。
| 分類 | 構文 |
|---|---|
| 整数 |
i32、i64、byte、char、size_t
|
| 浮動小数 |
float、double
|
| その他primitive |
bool、string、void
|
| pointer | *T |
| slice view | []T |
| dynamic array | Vec<T> |
| map handle | Map<K,V> |
| ownership intent | owned T |
| user type | struct、enum、type alias |
noneはoptional型ではなくnull literalです。owned Tはownership intentを
表しますが、現行ABIではTと同じ表現です。Vec<T>とMap<K,V>は
コンパイラ組み込みparameterized typeであり、一般のユーザー定義genericsでは
ありません。
count := 10
name: string := "YCPL"
const label: string := "alpha"
count = count + 1
count += 1
-
name := value: 型推論されたmutable binding -
name: Type := value: 明示型binding -
const name := value: immutable binding -
=,+=,-=,*=,/=,%=: 代入
if score >= 80 {
println("pass")
} else {
println("retry")
}
for i := 0; i < 10; i++ {
println(i)
}
for value in values {
println(value)
}
switch color {
case Color.Red {
println("red")
}
default {
println("other")
}
}
break、continue、return、short-circuit &&/||、index、
member access、cast、compound assignmentをサポートします。
defer fmt.println("leaving")
scope temporary {
value := make_value()
}
defer expressionまたはdefer { ... }はscope/functionを抜ける時にLIFOで
実行されます。scope name { ... }は名前付きlexical scopeです。
import済みmoduleに一致するpublic functionが1つだけある場合、
value.method(arg)をmodule.method(value, arg)として解決できます。
Vec<T>の詳細はVec-and-Memory、project/import規則は
Projects-and-Modulesを参照してください。