Skip to content

Language Specification

james-yusuke edited this page Jul 16, 2026 · 1 revision

言語仕様

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 fnintrinsic fn、struct、enum、 type alias、constです。pubを付けた宣言はimport先から参照できます。 intrinsic fnはbundled std module専用です。

分類 構文
整数 i32i64bytecharsize_t
浮動小数 floatdouble
その他primitive boolstringvoid
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")
    }
}

breakcontinuereturn、short-circuit &&/||、index、 member access、cast、compound assignmentをサポートします。

defer、scope、UFCS

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を参照してください。

Clone this wiki locally