
2026/01/12 21:57
**Zen‑C** 「高水準言語のように書き、C のように実行する。」
RSS: https://news.ycombinator.com/rss
要約▶
Japanese Translation:
Zen Cは、既存のCライブラリと完全なABI互換性を維持しつつ、人間が読めるGNU C/C11へ直接コンパイルされるモダンなシステムプログラミング言語です。型推論、パターンマッチング、ジェネリクス、トレイト、async/await、RAII(
deferとautofreeを併用)による手動メモリ管理、およびゼロランタイムオーバーヘッドを提供します。コンパイラは純粋なCコードを出力し、GCC、Clang、またはZigでビルドできます;言語の型はネイティブC型(int, I8..I128 → <int>_t, string → char* など)に直接マッピングされ、配列・タプル・構造体(オプションでビットフィールド付き)、合計型としての列挙子、ユニオンをサポートします。関数は fn で宣言し、クロージャは矢印記法またはブロック構文で書けます。制御フローには if/else、三項演算子、範囲ベースおよびイテレータ付きの for ループ、while、ラベル付き無限ループ、repeat、ガード/unless 構文が含まれます。オブジェクト指向機能としては impl ブロック内でメソッドを定義し、静的コンストラクタ、インスタンスメソッド、トレイト、および use による合成を利用できます。並行処理は pthreads をベースにした async fn/await で実装されています。メタプログラミング機能にはコンパイル時実行(comptime { … })、ファイル埋め込み(embed "file")、コンパイラプラグイン、ジェネリックCマクロ、およびインラインアセンブリブロック(asm { … })が含まれます。 @must_use, @deprecated, @inline などの属性はコンパイラ動作を制御します。インストールは簡単で、リポジトリをクローンし、make を実行した後に sudo make install;zc run <file>.zc、zc build <file>.zc -o <exe>、または zc repl で使用できます。テストスイート(make test)が新しい言語拡張を検証します。今後のリリースではメタプログラミング機能を拡充し、並行処理サポートを深化させつつABIを維持し続けます。これによりシステム開発者はレガシーCと相互運用可能でありながら、モダンで型安全なコードへの低コストパスを得ることができます。
Text to translate
(combining all major points for clarity):**
Zen C is a modern systems‑programming language that compiles directly to human‑readable GNU C/C11, preserving full ABI compatibility with existing C libraries. It offers type inference, pattern matching, generics, traits, async/await, manual memory management via RAII (alongside
defer and autofree), and zero runtime overhead. The compiler emits plain C that can be built with GCC, Clang, or Zig; the language maps its types straight to native C types (int, I8..I128 → <int>_t, string → char*, etc.) and supports aggregates (arrays, tuples, structs with optional bitfields, enums as sum types, unions). Functions are declared with fn; closures can be written as arrows or block syntax. Control flow includes if/else, ternary operators, range‑based and iterator for loops, while, infinite loops with labels, repeat, and guard/unless constructs. Object‑oriented features let you define methods in impl blocks, use static constructors, instance methods, traits, and composition via use. Concurrency is built on pthreads with async fn/await. Metaprogramming facilities include compile‑time execution (comptime { … }), file embedding (embed "file"), compiler plugins, generic C macros, and inline assembly blocks (asm { … }). Attributes such as @must_use, @deprecated, @inline, etc., control compiler behavior. Installation is simple: clone the repo, run make, then sudo make install; use zc run <file>.zc, zc build <file>.zc -o <exe>, or zc repl. A test suite (make test) validates new language extensions. Upcoming releases will expand metaprogramming and deepen concurrency support while keeping the ABI intact, giving systems developers a low‑cost path to modern, type‑safe code that still interoperates with legacy C.本文
Zen C
モダン・エルゴノミクス – ゼロオーバーヘッド – 純粋な C.
高レベル言語のように書き、C のように動かす。
概要
Zen C は、GNU C/C11 に人間が読める形でコンパイルされるモダンなシステムプログラミング言語です。以下の機能を備えています:
- 型推論
- パターンマッチング
- ジェネリクス & トレイト(Traits)
- async/await
- 手動メモリ管理と RAII(100 % C ABI 互換)
クイックスタート
インストール
git clone https://github.com/z-libs/Zen-C.git cd Zen-C make sudo make install
実行例
# コンパイルして実行 zc run hello.zc # 実行ファイルをビルド zc build hello.zc -o hello # インタラクティブシェル zc repl
言語リファレンス
-
変数と定数
var x = 42; // int が推論される const PI = 3.14159; var explicit: float = 1.0; // 明示的な型指定可変性 – デフォルトは可変。
又はvar mut
を使う。//> immutable‑by‑default -
プリミティブ型
| Zen | C 等価 | 説明 | |-----|--------|------| |
|int, uint
,int
| プラットフォーム整数 | |unsigned int
|I8 … I128
| 署名付き固定幅 | |int8_t … __int128_t
|U8 … U128
| 署名無し固定幅 | |uint8_t … __uint128_t
|isize, usize
,ptrdiff_t
| ポインタサイズ | |size_t
|byte
|uint8_t
の別名 | |U8
|F32, F64
,float
| 浮動小数点 | |double
|bool
| 真偽値 | |bool
|char
| 単一文字 | |char
|string
| C 文字列(ヌル終端) | |char*
|U0, u0, void
| 空型 |void -
集合型
配列
var ints: int[5] = {1, 2, 3, 4, 5}; var zeros: [int; 5]; // ゼロ初期化タプル
var pair = (1, "Hello"); var x = pair.0; var s = pair.1;構造体
struct Point { x: int; y: int; } var p = Point { x: 10, y: 20 };ビットフィールド
struct Flags { valid: U8 : 1; mode: U8 : 3; }列挙型(sum type)
enum Shape { Circle(float), Rect(float, float), Point }共用体 – 標準 C の共用体(不安全アクセス)。
-
関数とラムダ
関数
fn add(a: int, b: int) -> int { return a + b; } add(a: 10, b: 20); // 名前付き引数ラムダ / クロージャ
var factor = 2; var double = x -> x * factor; // アロー構文 var full = fn(x: int) -> int { return x * factor; }; // ブロック構文 -
制御フロー
条件分岐
if x > 10 { print("Large"); } else if x > 5 { print("Medium"); } else { print("Small"); } var y = if x > 10 ? 1 : 0; // 三項演算子パターンマッチ
match val { 1 => print("One"), 2 | 3 => print("Two or Three"), 4..10 => print("Range"), _ => print("Other") }列挙型の分解:
match shape { Circle(r) => print(f"Radius: {r}"), Rect(w, h) => print(f"Area: {w*h}"), Point => print("Point") }ループ
// 範囲 for i in 0..10 { ... } for i in 0..10 step 2 { ... } // イテレータ/コレクション for item in vec { ... } while x < 10 { ... } outer: loop { if done { break outer; } } repeat 5 { ... }高度な構文
guard ptr != NULL else { return; } unless is_valid { return; } -
演算子 – C の関数(
,add
…)にマッピング。注目点:null 合体 (sub
) や安全ナビゲーション (??
) は純粋な C ではノープです。?. -
メモリ管理
Defer – スコープ終了時に実行。
var f = fopen("file.txt", "r"); defer fclose(f);Autofree – 自動で解放。
autofree var types = malloc(1024);RAII / Drop トレイト
impl Drop for MyStruct { fn drop(mut self) { free(self.data); } } -
オブジェクト指向
メソッド –
で定義。implimpl Point { fn new(x: int, y: int) -> Point { return Point{x: x, y: y}; } fn dist(self) -> float { return sqrt(self.x * self.x + self.y * self.y); } }トレイト – 共有挙動。
trait Drawable { fn draw(self); } impl Drawable for Circle { fn draw(self) { … } }コンポジション – フィールドのミックスイン。
struct Entity { id: int; } struct Player { use Entity; name: string; } -
ジェネリクス – 型安全なテンプレート。
struct Box<T> { item: T; } fn identity<T>(val: T) -> T { return val; } -
並行(Async/Await) – pthreads に基づく。
async fn fetch_data() -> string { return "Data"; } var future = fetch_data(); var result = await future; -
メタプログラミング
Comptime – コンパイル時に実行。
comptime { print("Compiling..."); }Embed – ファイルをバイト配列として埋め込む。
var png = embed "assets/logo.png";Plugins – コンパイラプラグインをインポート。
import plugin "regex" var re = regex! { ^[a-z]+$ }; -
属性 – 関数/構造体に付与。
| 属性 | スコープ | 説明 | |------|----------|------| |
| Fn | 戻り値が無視された場合警告 | |@must_use
| Fn/Struct | メッセージ付き警告 | |@deprecated("msg")
,@inline
| Fn | コンパイラへのヒント | |@noinline
,@packed
| Struct | パディング/アラインメント | |@align(N)
,@constructor
| Fn |@destructor
前後に実行 | |main
| Fn/Var | 未使用警告を抑制 | |@unused
| Fn | 弱リンク | |@weak
| Fn | 特定セクションへ配置 | |@section("name")
| Fn | 戻らない関数 | |@noreturn
| Struct | トレイトを自動実装 |@derived(...) -
インラインアセンブリ – GCC スタイル拡張 asm、名前付き制約。
fn add(a: int, b: int) -> int { var result: int; asm { "add {result}, {a}, {b}" : out(result) : in(a), in(b) : clobber("cc") } return result; }
コンパイラサポート & 互換性
| コンパイラ | パス率 | 対応機能 | 知られた制限 |
|---|---|---|---|
| GCC | 100 % | 全て | – |
| Clang | 100 % | 全て | – |
| Zig | 100 % | 全て | – |
| TCC | ~70 % | 基本構文、ジェネリクス、トレイト | 未実装、Intel ASM 非対応、入れ子関数非対応 |
推奨: 本番環境では GCC, Clang, または Zig。
TCC は高速プロトタイピングに最適。
Zig でビルド
# Zen C プログラムを Zig で実行 zc run app.zc --cc zig # Zen C コンパイラ自体を Zig でビルド make zig
コントリビューション
- リポジトリをフォーク。
- フィーチャーブランチを作成 (
)。git checkout -b feature/NewThing - 既存の C スタイルに従い、
を実行して全テストが通ることを確認。make test
に新しいテストを追加。tests/- 明確な説明付きでプルリクエストを送信。
テストの実行
# 全テスト(GCC) make test # 特定テスト ./zc run tests/test_match.zc # 別コンパイラで実行 ./tests/run_tests.sh --cc clang ./tests/run_tests.sh --cc zig ./tests/run_tests.sh --cc tcc
コンパイラ拡張の仕方
- パーサ:
(再帰下降)src/parser/ - コード生成:
– Zen C → GNU C/C11 へのトランスパイルsrc/codegen/ - 標準ライブラリ:
は Zen C で実装std/