Zig (プログラミング言語)
修正、加筆に協力し、現在の表現をより自然な表現にして下さる方を求めています。ノートページや履歴も参照してください。(2021年1月)
Zig Zigのロゴ | |
パラダイム | |
---|---|
登場時期 |
|
開発者 | アンドリュー・ケリー ウィキデータを編集 |
最新リリース | 0.13.0 / 2024年6月6日[1] |
型付け | |
影響を受けた言語 | C言語、C++、Go、Rust、JavaScript ウィキデータを編集 |
プラットフォーム | クロスプラットフォーム |
ライセンス | MIT License ウィキデータを編集 |
ウェブサイト | |
拡張子 | zig ウィキデータを編集 |
テンプレートを表示 |
Zigは、アンドリュー・ケリーによって設計された命令型の汎用の静的型付けのコンパイル型 システムプログラミング言語である[2] [3] 。 この言語は「堅牢性、最適性及び保守性」向けに設計されており[4] [5] 、コンパイル時のジェネリクス、リフレクション、クロスコンパイル及び手動メモリ管理 (英語版)をサポートしている[6] 。 この言語の主な目標は、C言語に依存せずにこれを改善し[7] [8] 、Rustなどから着想を得ることである[9] 。
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 conststd=@import("std"); pubfnmain()!void{ conststdout=std.io.getStdOut().writer(); trystdout.print("Hello, {s}!\n",.{"world"}); }
ジェネリック連結リスト
[編集 ]// ジェネリックなLinkedList型 conststd=@import("std"); conststdout=std.io.getStdOut().writer(); fnLinkedList(comptimeT:type)type{ returnstruct{ constSelf=@This(); pubconstNode=struct{ next:?*Node=null, data:T, }; first:?*Node=null, pubfnprepend(list:*Self,new_node:*Node)void{ new_node.next=list.first; list.first=new_node; } pubfnformat( list:Self, comptimefmt:[]constu8, options:std.fmt.FormatOptions, out_stream:anytype, )!void{ tryout_stream.writeAll("( "); varit=list.first; while(it)|node|:(it=node.next){ trystd.fmt.formatType(node.data,fmt,options,out_stream,1); tryout_stream.writeAll(" "); } tryout_stream.writeAll(" )"); } }; } pubfnmain()!void{ constListU32=LinkedList(u32); varlist=ListU32{}; varnode1=ListU32.Node{.data=1}; varnode2=ListU32.Node{.data=2}; varnode3=ListU32.Node{.data=3}; list.prepend(&node1); list.prepend(&node2); list.prepend(&node3); trystdout.print("{}\n",.{list}); trystdout.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メソッドを使用してリストに追加する。最後に、標準出力にリストを出力する。
脚注
[編集 ]注釈
[編集 ]- ^ フィールド間のパディングがゼロの構造体のこと。
出典
[編集 ]- ^ 出典URL: https://ziglang.org/download/#release-0.13.0, 題名: Release 0.13.0
- ^ 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日閲覧。
- ^ Elizabeth, Jane (2017年10月19日). "Tired of C? New programming language Zig aims to be more pragmatic and readable". JAXenter. 2021年1月1日閲覧。
- ^ Yegulalp, Serdar (2016年8月29日). "New challenger joins Rust to topple C language". InfoWorld. 2021年1月1日閲覧。
- ^ IT之家 (2020年7月12日). "想替代 C 的 Zig 语言成立了基金会" (中国語). 新浪数码. 2021年1月1日閲覧。
- ^ "The Zig Programming Language". ziglang.org. 2021年1月1日閲覧。
- ^ "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日閲覧。
- ^ "Zig competes with C instead of depending on it". ziglang.org. 2021年1月1日閲覧。
- ^ Kelley, Andrew (2018年1月24日). "Unsafe Zig is Safer than Unsafe Rust". andrewkelley.me. 2021年1月1日閲覧。
- ^ Anderson, Tim (2020年4月24日). "Keen to go _ExtInt? LLVM Clang compiler adds support for custom width integers". The Register. 2021年1月1日閲覧。
- ^ "Documentation - The Zig Programming Language". ziglang.org. 2021年1月1日閲覧。
- ^ 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日閲覧。
- ^ Bill, Ginger (2019年5月13日). "A Reply to The Road to Zig 1.0". gingerBill. 2021年1月1日閲覧。
- ^ a b "ziglang/zig". GitHub. 2021年1月1日閲覧。
- ^ "Tier System". ziglang.org. 2021年1月1日閲覧。
- ^ "zig cc". ziglang.org (2020年4月13日). 2021年1月1日閲覧。
- ^ "Add support for `zig cc` as C compiler. #13757". GitHub. 2021年1月1日閲覧。