The Zig language

Moderator: BarryK

Post Reply
User avatar
BarryK
Posts: 2659
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 126 times
Been thanked: 734 times

The Zig language

Post by BarryK »

EasyOS Scarthgap-series and Daedalus-series now both have support for Zig in the devx SFS.
Today announced this for Daedalus 6.4.1 on my blog:

https://bkhome.org/news/202410/easyos-d ... n-641.html

What's so special about Zig?
We know that Rust is supposed to be the next great language, but Zig???

To get a feel for how Zig fits into the scheme of things, this chat between two programmers is very informative. Quite long but very watchable:

...you don't need to understand all the concepts discussed; after all these guys are professional programmers, university-trained. But easy to pick up the salient points.

1. A better C compiler than gcc, with all libc's builtin
2. Cross-compile builtin, not just for different CPUs but also OSs (Linux, Mac, Windows, Android).
3. Build system supposedly much better than autotools, cmake, etc.
4. Very simple language compared with Rust

Those two guys also recorded another video discussing point-3.

I posted a brief intro to Zig, with links, to my blog:

https://bkhome.org/news/202410/the-zig-language.html

...discovered after that, more text editors that support Zig syntax-highlighting, such as Vim and NeoVim.

dimkr
Posts: 2407
Joined: Wed Dec 30, 2020 6:14 pm
Has thanked: 53 times
Been thanked: 1191 times

Re: The Zig language

Post by dimkr »

https://github.com/techee/geany-lsp

I found about this some time ago and trying it is somewhere in my TODO list. A lightweight IDE with LSP support and not just basic syntax highlighting sounds perfect to me.

User avatar
BarryK
Posts: 2659
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 126 times
Been thanked: 734 times

Re: The Zig language

Post by BarryK »

dimkr wrote: Wed Oct 30, 2024 6:38 am

https://github.com/techee/geany-lsp

I found about this some time ago and trying it is somewhere in my TODO list. A lightweight IDE with LSP support and not just basic syntax highlighting sounds perfect to me.

Dude the Builder shows how to setup NeoVim with ZLS, Zig's language server:

Personally, I would prefer an editor with a GUI. NeoVim is a text-mode app.
I did try neovim-qt, but it hardly adds any GUI functionality.

ZLS is just an executable, /usr/bin/zls
I've included it in the devx in latest EasyOS; just need to find an editor to use it with.

User avatar
BarryK
Posts: 2659
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 126 times
Been thanked: 734 times

Re: The Zig language

Post by BarryK »

Sublime text editor works really well with Zig. Snapshot:

sublime-zig.png
sublime-zig.png (109.23 KiB) Viewed 382 times

Sublime is a commercial product, free for personal use.
They say even for personal use supposed to pay, but there is no cutout date for free usage.

Website:
https://www.sublimetext.com/

I'm using this Zig plugin:
https://github.com/aidenfoxivey/sublime-zig-unofficial

...there is another "official" plugin, but the unofficial project is more active.

Just had a quick look, it supports code-completion, and there is a setting to turn on Zig build ide.

User avatar
BarryK
Posts: 2659
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 126 times
Been thanked: 734 times

Re: The Zig language

Post by BarryK »

If anyone is interested, I made Sublime into a PET:

https://distro.ibiblio.org/easyos/amd64 ... -amd64.pet

Not compiled by me, it is their official x86_64 tarball. I put the Zig and Nim plugins into the PET.

In EasyOS, the script build-rox-sendto now recognises Sublime and sets up mime-handling, so right-click on a text file and Sublime will be in the menu.

For other distros, you might want to edit /usr/share/applications/sublime.desktop with a MimeType entry, like this:

Code: Select all

MimeType=text/plain;text/x-chdr;text/x-csrc;text/x-c++hdr;text/x-c++src;text/x-java;text/x-dsrc;text/x-pascal;text/x-perl;text/x-python;application/x-php;application/x-httpd-php3;application/x-httpd-php4;application/x-httpd-php5;application/xml;text/html;text/css;text/x-sql;text/x-diff;image/x-xpixmap;application/xml;application/javascript;application/x-shellscript;text/x-python3;text/x-nim;text/x-zig

A note for Puppy-and-derivative and Woof* developers, in EasyOS I have this added to rootfs-skeleton/usr/share/mime/packages/puppy.xml:

Code: Select all

  <mime-type type="text/x-nim">
    <comment>Nim source file</comment>
    <glob pattern="*.nim"/>
  </mime-type>

  <mime-type type="application/x-xbps">
    <comment>Void Linux package</comment>
    <glob pattern="*.xbps"/>
  </mime-type>

  <mime-type type="text/x-zig">
    <comment>Zig source file</comment>
    <glob pattern="*.zig"/>
  </mime-type>
User avatar
BarryK
Posts: 2659
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 126 times
Been thanked: 734 times

Re: The Zig language

Post by BarryK »

Continuing the topic of text editors that support Zig, I have created a separate thread for Lite XL:

"Lite XL text editor"
viewtopic.php?t=13002

don570
Posts: 678
Joined: Sat Nov 21, 2020 4:43 pm
Has thanked: 5 times
Been thanked: 114 times

Video - Zig language using EasyOS

Post by don570 »

Zig language programming using EasyOS Daedelus 6.4.1

I made a video to show how easy it is to program using DEVX file of EasyOS Daedelus 6.4.1
Just download 3 files from
https://distro.ibiblio.org/easyos/amd64 ... /daedalus/

I used Sublime text editor

Video available
https://youtu.be/IrxsNI9oWIU

__________________________________________
Based on info in
https://gencmurat.com/en/posts/introduction-to-zig/
Program code:

Code: Select all

const std = @import("std");

pub fn main() void {
    std.debug.print("Hello, Zig! Ready to revolutionize systems programming?\n", .{});
}

Save this as hello.zig, then run:

Code: Select all

zig run hello.zig

__________________________________________________

User avatar
BarryK
Posts: 2659
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 126 times
Been thanked: 734 times

Re: Video - Zig language using EasyOS

Post by BarryK »

don570 wrote: Sat Nov 02, 2024 6:46 pm

Video available
https://youtu.be/IrxsNI9oWIU

I watched it!

don570
Posts: 678
Joined: Sat Nov 21, 2020 4:43 pm
Has thanked: 5 times
Been thanked: 114 times

Zig Examples

Post by don570 »

Zig wiki page has more examples...To print to screen

Code: Select all

const std = @import("std");

pub fn main() void {
    std.debug.print("Hello, Zig! Ready to revolutionize systems programming?\n", .{});
}

or

Code: Select all

const std = @import("std");

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

___________________________________________________

Last edited by don570 on Sun Nov 03, 2024 6:25 pm, edited 1 time in total.
don570
Posts: 678
Joined: Sat Nov 21, 2020 4:43 pm
Has thanked: 5 times
Been thanked: 114 times

Zig language examples

Post by don570 »

This is called a generic linked list

Code: Select all

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});
}

The output is
( 3 2 1 )
( 11 10 1 )
______________________________________________

don570
Posts: 678
Joined: Sat Nov 21, 2020 4:43 pm
Has thanked: 5 times
Been thanked: 114 times

Zig language examples

Post by don570 »

This example is called string repetition with allocator

Code: Select all

const std = @import("std");

fn repeat(
    allocator: *std.mem.Allocator,
    original: []const u8,
    times: usize,
) std.mem.Allocator.Error![]const u8 {
    var buffer = try allocator.alloc(
        u8,
        original.len * times,
    );

    for (0..times) |i| {
        std.mem.copyForwards(
            u8,
            buffer[(original.len * i)..],
            original,
        );
    }

    return buffer;
}

pub fn main() !void {
    const stdout = std.io.getStdOut().writer();

    var arena = std.heap.ArenaAllocator.init(
        std.heap.page_allocator,
    );
    defer arena.deinit();

    var allocator = arena.allocator();

    const original = "Hello ";
    const repeated = try repeat(
        &allocator,
        original,
        3,
    );

    // Prints "Hello Hello Hello "
    try stdout.print("{s}\n", .{repeated});
}

The output is

Hello Hello Hello

__________________________________________________

Post Reply

Return to “EasyOS”