Zig (プログラミング言語)

Zigは、アンドリュー・ケリーによって設計された命令型汎用静的型付けコンパイル型システムプログラミング言語である[2][3]。 この言語は「堅牢性、最適性及び保守性」向けに設計されており[4][5]コンパイル時ジェネリクスリフレクションクロスコンパイル及び手動メモリ管理をサポートしている[6]。 この言語の主な目標は、C言語に依存せずにこれを改善し[7][8]Rustなどから着想を得ることである[9]

Zig
Zig
Zigのロゴ
パラダイム
登場時期
  • 2015年 ウィキデータを編集
開発者 アンドリュー・ケリー ウィキデータを編集
最新リリース 0.11.0 / 2023年8月4日[1]
型付け
影響を受けた言語 C言語C++GoRustJavaScript ウィキデータを編集
プラットフォーム クロスプラットフォーム
ライセンス MIT License ウィキデータを編集
ウェブサイト
拡張子 zig ウィキデータを編集

Zigにはパックされた構造体[注釈 1]、多倍長整数[10]、複数のポインタ型などの低レベルプログラミングのための多くの機能がある[11]

リファレンス実装コンパイラはZig及びC++で記述されており、LLVM[12]をバックエンドとして使用し[13][14]、LLVMがサポートするターゲットの多くをサポートしている[15]。 コンパイラはフリーかつオープンソースで、MITライセンスの条件に基づいて配布されている[14]。 Zigのコンパイラはzig cc及びzig c++をそれぞれ使用することによって、Clangと同様にC言語及びC++をコンパイルすることができる[16]Nimプログラミング言語はC言語のコンパイラとしてzig ccを使用することをサポートしている[17]

コード例

Hello world

// zig version 0.9.1
const std = @import("std");

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();
    try stdout.print("Hello, {s}!\n", .{"world"});
}

ジェネリック連結リスト

// ジェネリックなLinkedList型

const std = @import("std");
const stdout = std.io.getStdOut().writer();

fn LinkedList(comptime T: type) type {
    return struct {
        const Self = @This();
        pub const Node = struct {
            next: ?*Node = null,
            data: T,
        };

        first: ?*Node = null,

        pub fn prepend(list: *Self, new_node: *Node) void {
            new_node.next = list.first;
            list.first = new_node;
        }
        pub fn format(
            list: Self,
            comptime fmt: []const u8,
            options: std.fmt.FormatOptions,
            out_stream: anytype,
        ) !void {
            try out_stream.writeAll("( ");
            var it = list.first;
            while (it) |node| : (it = node.next) {
                try std.fmt.formatType(node.data, fmt, options, out_stream, 1);
                try out_stream.writeAll(" ");
            }
            try out_stream.writeAll(" )");
        }
    };
}

pub fn main() !void {
    const ListU32 = LinkedList(u32);
    var list = ListU32{};
    var node1 = ListU32.Node{ .data = 1 };
    var node2 = ListU32.Node{ .data = 2 };
    var node3 = ListU32.Node{ .data = 3 };
    list.prepend(&node1);
    list.prepend(&node2);
    list.prepend(&node3);
    try stdout.print("{}\n", .{list});
    try stdout.print("{b}\n", .{list});
}
実行結果
( 3 2 1 ) 
( 11 10 1 )

このコードは、Zig言語を使用して単方向連結リスト(LinkedList)を定義し、それを使用して整数の単方向連結リストを作成し、標準出力に出力するプログラムである。

最初の2行では、stdパッケージからioモジュールをインポートし、標準出力ストリームを取得する。

次に、LinkedList関数が定義されている。この関数は、ジェネリックなLinkedList型を作成する。型Tを取り、LinkedList型を返す。このLinkedList型は、Nodeという構造体を含み、それぞれがデータを保持する。また、LinkedList型自体もprependとformatというメソッドを持つ。prependメソッドは、リストの先頭にノードを追加する。formatメソッドは、リストの内容を指定された書式でフォーマットして出力する。

main関数では、LinkedList関数を使用してListU32という型を作成し、整数のLinkedListを作成する。その後、3つのノードを作成し、prependメソッドを使用してリストに追加する。最後に、標準出力にリストを出力する。

脚注

注釈

  1. フィールド間のパディングがゼロの構造体のこと。

出典

  1. 出典URL: https://ziglang.org/download/#release-0.11.0, 題名: Release 0.11.0
  2. Motroc, Gabriela (2017年10月31日). “Zig has all the elegant simplicity of C, minus all the ways to shoot yourself in the foot””. JAXenter. 2021年1月1日閲覧。
  3. Elizabeth, Jane (2017年10月19日). Tired of C? New programming language Zig aims to be more pragmatic and readable”. JAXenter. 2021年1月1日閲覧。
  4. Yegulalp, Serdar (2016年8月29日). New challenger joins Rust to topple C language”. InfoWorld. 2021年1月1日閲覧。
  5. IT之家 (2020年7月12日). 想替代 C 的 Zig 语言成立了基金会 (中国語). 新浪数码. 2021年1月1日閲覧。
  6. The Zig Programming Language”. ziglang.org. 2021年1月1日閲覧。
  7. Mozilla’s Observatory, the Zig programming language, and uSens’ VR/AR SDK—SD Times news digest: Aug. 29, 2016”. SD Times (2016年8月29日). 2021年1月1日閲覧。
  8. Zig competes with C instead of depending on it”. ziglang.org. 2021年1月1日閲覧。
  9. Kelley, Andrew (2018年1月24日). Unsafe Zig is Safer than Unsafe Rust”. andrewkelley.me. 2021年1月1日閲覧。
  10. Anderson, Tim (2020年4月24日). Keen to go _ExtInt? LLVM Clang compiler adds support for custom width integers”. The Register. 2021年1月1日閲覧。
  11. Documentation - The Zig Programming Language”. ziglang.org. 2021年1月1日閲覧。
  12. Lewkowicz, Jakub (2020年4月14日). SD Times news digest: C++20 concepts in Visual Studio 2010 version 16.3, Bootstrap to drop IE support, and Zig 0.60 released”. SD Times. 2021年1月1日閲覧。
  13. Bill, Ginger (2019年5月13日). A Reply to The Road to Zig 1.0”. gingerBill. 2021年1月1日閲覧。
  14. ziglang/zig”. GitHub. 2021年1月1日閲覧。
  15. Tier System”. ziglang.org. 2021年1月1日閲覧。
  16. zig cc”. ziglang.org (2020年4月13日). 2021年1月1日閲覧。
  17. Add support for `zig cc` as C compiler. #13757”. GitHub. 2021年1月1日閲覧。

関連項目

外部リンク

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.