Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Single-line comment
/// Documentation comment.

// Imports
const std = @import("std");
const builtin = @import("builtin");

// Constants and variables
const answer: i32 = 42;
const pi = 3.14159;
var counter: usize = 0;
pub const name = "Zig";

// Integer literals
const decimal = 123;
const binary = 0b1010_0101;
const octal = 0o755;
const hex = 0xDEAD_BEEF;
const separated = 1_000_000;

// Floating-point literals
const float = 3.14;
const exponent = 1.5e-3;
const hex_float = 0x1.8p1;

// Character and string literals
const char = 'a';
const newline = '\n';
const unicode = '\u{1F600}';
const string = "escapes: \" \\ \n \t";

// Multiline string
const multiline =
\\first line
\\second line
;

// Identifiers
const @"quoted identifier" = 123;
const snake_case = true;

// Operators
const arithmetic = 1 + 2 * 3 - 4 / 2 % 2;
const comparison = a == b and c != d or e >= f;
const bitwise = x & y | z ^ w;
const shifts = value << 2 >> 1;
const wrapping = a +% b;
const saturating = a +| b;

// Struct
const Point = struct {
x: i32,
y: i32,

pub fn init(x: i32, y: i32) Point {
return .{ .x = x, .y = y };
}
};

const origin = Point{
.x = 0,
.y = 0,
};

// Enum
const Color = enum {
red,
green,
blue,
};

// Tagged union
const Value = union(enum) {
integer: i64,
float: f64,
boolean: bool,
};

// Arrays, slices, tuples
const array = [_]u8{ 1, 2, 3, 4 };
const slice = array[1..3];
const tuple = .{ 42, true, "hello" };

// Pointers
var value: i32 = 42;
const ptr: *i32 = &value;
const many_ptr: [*]i32 = undefined;
const sentinel_ptr: [*:0]const u8 = undefined;

// Functions
fn add(a: i32, b: i32) i32 {
return a + b;
}

fn generic(comptime T: type, value: T) T {
return value;
}

// If / else
fn classify(x: i32) i32 {
if (x > 10) {
return 1;
} else if (x == 10) {
return 0;
} else {
return -1;
}
}

// While / for / labels
fn loops() void {
var i: usize = 0;

while (i < 10) : (i += 1) {
if (i == 5) continue;
if (i == 8) break;
}

for (array, 0..) |item, index| {
_ = item;
_ = index;
}

outer: for (array) |item| {
if (item == 3) break :outer;
}
}

// Switch
fn describe(value: Value) void {
switch (value) {
.integer => |x| _ = x,
.float => |x| _ = x,
.boolean => |x| _ = x,
}
}

// Optionals
fn optional(value: ?i32) i32 {
return value orelse 0;
}

// Errors
const MyError = error{
NotFound,
InvalidValue,
};

fn mightFail() MyError!i32 {
return error.NotFound;
}

fn handleError() !void {
const result = try mightFail();
_ = result;

_ = mightFail() catch 42;
}

// Defer
fn cleanup() void {
defer std.debug.print("done\n", .{});
}

// Comptime / builtins
const type_of_value = @TypeOf(value);
const size_of_point = @sizeOf(Point);
const alignment = @alignOf(Point);
const field_name = @tagName(Color.red);

// Builtin-heavy expression
const converted = @intCast(@as(u64, 42));
const bits = @bitCast(@as(u32, 0x3F800000));

// Extern / export / calling convention
extern fn external_function(x: i32) i32;

export fn exported_function(x: i32) callconv(.c) i32 {
return x;
}

// Tests
test "basic arithmetic" {
try std.testing.expect(add(1, 2) == 3);
}

test "optional" {
try std.testing.expect(optional(null) == 0);
}
52 changes: 52 additions & 0 deletions zig.lsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#[display_name = "Zig"]
#[path = "**/*.zig"]
#[path = "**/*.zon"]
pub fn zig() {
until /$/ {
yield other;

if /\/\/.*/ {
yield comment;
} else if /\\\\.*/ {
yield string;
} else if /"/ {
loop {
if /\\./ {
} else if /"/ {
yield string;
break;
} else if /$/ {
yield string;
break;
} else if /./ {}
}
} else if /'(\\.|[^'\\])'/ {
yield string;
} else if /@([a-zA-Z_]\w*)\s*\(/ {
yield $1 as method;
} else if /@[a-zA-Z_]\w*/ {
yield storage.annotation;
} else if /(?:break|continue|return|try|catch|orelse|if|else|switch|while|for|suspend|resume|async|await|defer|errdefer|unreachable)\>/ {
yield keyword.control;
} else if /(?:const|var|fn|pub|usingnamespace|struct|enum|union|error|opaque|inline|noinline|comptime|callconv|volatile|allowzero|align|linksection|threadlocal|anytype)\>/ {
yield keyword.other;
} else if /(?:true|false|null|undefined|nil)\>/ {
yield constant.language;
} else if /(?:i\d+|u\d+|f16|f32|f64|f128|isize|usize|bool|void|noreturn|type|anyerror)\>/ {
yield storage.type;
} else if /-?(?:0x[0-9a-fA-F_]+|0b[01_]+|0o[0-7_]+|\d[\d_]*(?:\.\d[\d_]*)?)\>/ {
if /\w+/ {
} else {
yield constant.numeric;
}
} else if /\.[a-zA-Z_]\w*/ {
yield variable; // Catches ZON field keys like .name, .version
} else if /(\w+)\s*\(/ {
yield $1 as method;
} else if /\w+/ {
// Gobble word chars
}

yield other;
}
}