LoginSignup
6
1

More than 1 year has passed since last update.

All your codebase are belong to us. ーー ZigのHello, world!

Last updated at Posted at 2022-01-03

山中の賊を破るは易く心中の賊を破るは難し

Advent Calendar 2022 4日目1の記事です。
私のAdvent Calendar 2022 一覧


はじめに

Elixirを楽しんでいますか:bangbang::bangbang::bangbang:

昨日「Fortranをはじめる(Nervesにてはじめる前の準備運動を開始)」という記事を書きました。

@mnishiguchi さんからZigなるものを教えてもらいました。
とりあえずZig単体で、Hello, Worldしてみます。
近いうちにNervesと組み合わせて、アレした〜 コレした〜 を書いてみたいとおもっています。

この記事では、Elixirには触れません。

Zigの公式ページ

Zig

Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

2015年に誕生したプログラミング言語とのことで、Youngです。

Qiita内では2022/01/03 18:41現在、ZIGのTagが付いた記事は私の記事を入れて6本です。

公式のトップには以下のプログラムが書いてあります。

const std = @import("std");
const json = std.json;
const payload =
    \\{
    \\    "vals": {
    \\        "testing": 1,
    \\        "production": 42
    \\    },
    \\    "uptime": 9999
    \\}
;
const Config = struct {
    vals: struct { testing: u8, production: u8 },
    uptime: u64,
};
const config = x: {
    var stream = json.TokenStream.init(payload);
    const res = json.parse(Config, &stream, .{});
    // Assert no error can occur since we are
    // parsing this JSON at comptime!
    break :x res catch unreachable;
};
pub fn main() !void {
    if (config.vals.production > 50) {
        @compileError("only up to 50 supported");
    }
    std.log.info("up={d}", .{config.uptime});
}

おそらくは、up=9999が出力されるものとおもいます。
本当にそうなのか、インストールから取り組んでみます。

インストール

Getting Startedをご参照ください。

私は、macOSを使っています。
Homebrewにてインストールしました。

$ brew install zig

(任意)VS Codeのプラグイン

Toolsというドキュメントでススメられていました。

Run Hello World

$ mkdir hello-world
$ cd hello-world
$ zig init-exe

こんなふうにファイルが作られます。

.
├── build.zig
└── src
    └── main.zig

とりあえず、build.zigはそっと閉じます。
src/main.zigを見てみます。

src/main.zig
const std = @import("std");

pub fn main() anyerror!void {
    std.log.info("All your codebase are belong to us.", .{});
}

test "basic test" {
    try std.testing.expectEqual(10, 3 + 7);
}

テスト

ほうほう、テストコードもあります。
テストからやってみます。
ファイル名を指定する必要があります。

$ zig test src/main.zig

当然パスします。
これまた当然、期待結果の1011などに書き換えるとテストはコケます。

Run

$ zig build run

$\huge{All\ your\ codebase\ are\ belong\ to\ us.}$

:tada::tada::tada:

Hello, world!ではなく、All your codebase are belong to us.zig init-exeコマンドで作られたファイルに書いてあるので、その通りに出力されています。

一瞬、コンピュータが乗っ取られたのかと思いましたが、そんなことはありません2
安心してください。
そう書いてあるから、書いてある通りに出力されただけです。


All your codebase are belong to us.

ところでこのAll your codebase are belong to us.という英文、
$\huge{なんだか変}$
です3

Google翻訳やDeepLに聞いてみましたが、どうもピンときません。

結論から言うと、日本発のゼロウィングというゲームのヨーロッパ版の英訳を起源とするもので、一種のネタのようなものでした。

以下、リンクを示しておきます。

さらに、ゼロウィングのゲームの内容は、

宇宙海賊「CATS」を倒すために、自機「ZIG-01」が8つの防衛基地に向けて発進するという内容となっている。1

とのことです。

Zigというプログラミング言語の由来は、ゼロウィングというゲームにありそうですが、正確なことはわかりません。


公式ページトップに載っているプログラムの実行結果

話をプログラミング言語Zigに戻します。

src/main.zig公式ページトップに載っているプログラムに書き換えます。

src/main.zig
const std = @import("std");
const json = std.json;
const payload =
    \\{
    \\    "vals": {
    \\        "testing": 1,
    \\        "production": 42
    \\    },
    \\    "uptime": 9999
    \\}
;
const Config = struct {
    vals: struct { testing: u8, production: u8 },
    uptime: u64,
};
const config = x: {
    var stream = json.TokenStream.init(payload);
    const res = json.parse(Config, &stream, .{});
    // Assert no error can occur since we are
    // parsing this JSON at comptime!
    break :x res catch unreachable;
};
pub fn main() !void {
    if (config.vals.production > 50) {
        @compileError("only up to 50 supported");
    }
    std.log.info("up={d}", .{config.uptime});
}
$ zig build run

$\huge{info: up=9999}$

Wrapping up :lgtm::lgtm::lgtm::lgtm::lgtm:

Enjoy Zig:bangbang::bangbang::bangbang:

$\huge{All\ your\ codebase\ are\ belong\ to\ us.}$


Zigの公式ページの下のほうにも書いてあります。

スクリーンショット 2022-01-03 19.22.48.png


"Zig" in Japanese

When Japanese folks hear "zig", they think of the below songs.
We hope you enjoy them.

近藤真彦 スニーカーぶる~す :video_camera:
シブがき隊 ZIG ZAG セブンティーン :video_camera:


  1. @kaizen_nagoya さんの「「@e99h2121 アドベントカレンダーではありますまいか Advent Calendar 2020」の改訂版ではありますまいか Advent Calendar 2022 1日目 Most Breakthrough Generator」から着想を得て、模倣いたしました。  

  2. 「All your codebase are belong to us.」の話が好きで、私はZigの虜になりそうです。そういう意味では私のパソコンはすっかり乗っ取られてしまったのかもしれません。 

  3. ブームを起こしたことはすごいことです。私は「ゼロウィング」をプレイしたことはないし、何も関わってはいませんし、私だけの感じ方だとはおもいますが、これからももっともっと日本から英語で堂々と発信して行けばいいとおもっています。何か私も世界を驚かせるsomethingを残したいとおもっています。 

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1