Skip to content

Instantly share code, notes, and snippets.

@travisstaloch
travisstaloch / ergo-vec.zig
Last active July 30, 2024 06:09
A more ergonomic vector type without usingnamespace. If you wanted to create a new Vec3(u32) type for instance, youd to have copy some decls. Don't forget to call check(Vec3(u32)) to make sure you haven't missed any shared methods.
const std = @import("std");
pub fn SharedVecMethods(comptime Self: type) type {
return struct {
const fields = @typeInfo(Self).Struct.fields;
const T = fields[0].type;
comptime {
// check that all fields have the same type
for (fields) |f| std.debug.assert(T == f.type);
}
@travisstaloch
travisstaloch / tests.zig
Last active August 15, 2024 11:05
zonny - a toy zon parser
//!
//! tests written for zonny.zig
//!
//! there is an example of parsing a build.zig.zon file at the bottom of this file.
//!
const std = @import("std");
const testing = std.testing;
const zonny = @import("zonny");
const talloc = testing.allocator;
@travisstaloch
travisstaloch / build.zig
Last active August 2, 2024 19:28
minimal build.zig for wasm, August 2024, zig version 0.14.0-dev.591+1c35e73b6
const std = @import("std");
pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "lib",
.root_source_file = .{ .path = "src/lib.zig" },
.target = b.resolveTargetQuery(std.Target.Query.parse(
.{ .arch_os_abi = "wasm32-freestanding" },
) catch unreachable),
@travisstaloch
travisstaloch / gen_test_case.py
Last active January 4, 2024 07:54
nice n-dimensional and generic matrix code
# generates valid zig multiply test case code which may used in matrix.zig
import numpy as np
np.random.seed(42)
# A = np.empty((2, 3, 2))
# A.fill(1)
# A = np.array([
# [
# [1,2,3],
# [4,5,6],
@travisstaloch
travisstaloch / vec.zig
Last active July 30, 2024 03:25
A nice way to represent vectors in zig. Mixin methods allow all types to share code. extern structs mean they can bitcast to/from arrays and simd vectors.
//!
//! updated. now without usingnamespace. see previous revisions for a version with usingnamespace.
//!
pub fn VecMethods(comptime Self: type) type {
return struct {
const info = @typeInfo(Self);
pub const len = info.Struct.fields.len - 1;
pub const Arr = [len]info.Struct.fields[0].type;
@travisstaloch
travisstaloch / view-disassembly.zig
Created November 18, 2023 21:27
produce a small object file with zig and disassemble
// zig build-obj -fstrip -OReleaseSmall /tmp/tmp.zig -femit-bin=/tmp/tmp.o && objdump -d /tmp/tmp.o
const std = @import("std");
pub fn panic(msg: []const u8, st: ?*std.builtin.StackTrace, start: ?usize) noreturn {
_ = .{ msg, st, start };
@trap();
}
pub export fn _start() callconv(.Naked) noreturn {
@travisstaloch
travisstaloch / levenshtein-distance.zig
Created August 16, 2023 07:25
levenshtein distance implementation in zig
//! https://en.wikipedia.org/wiki/Levenshtein_distance
const std = @import("std");
pub fn levRec(a: []const u8, b: []const u8) u16 {
if (a.len == 0) return @truncate(b.len);
if (b.len == 0) return @truncate(a.len);
if (a[0] == b[0])
return levRec(a[1..], b[1..]);
return @min(
//! adapted from https://ravendb.net/articles/atomic-reference-counting-with-zig-code-samples
//! compiled with zig version 0.11.0-dev.3771+128fd7dd0
const std = @import("std");
pub fn RefCounted(comptime T: type) type {
return struct {
const Self = @This();
const InternalRef = packed union { item: packed struct { value: u44, is_error: bool, references: u19 }, raw: u64 };
# create this file at <roc-repo-root>/examples/parser/parse-json.roc
# run
# $ cd <roc-repo-root> && roc examples/parser/parse-json.roc
#
# one-liner: clone roc repo to /tmp + wget gist + run gist
# $ cd /tmp && git clone [email protected]:roc-lang/roc.git && cd roc && wget https://gist.github.com/travisstaloch/acae64ec4d8346597f1aaecfe8c401e4/raw/parse-json.roc -O examples/parser/parse-json.roc && roc examples/parser/parse-json.roc
#
# resources:
# https://github.com/tsoding/haskell-json/blob/master/Main.hs
# create this file at <roc-repo-root>/examples/parser/repro.roc
# run
# $ cd <roc-repo-root> && roc examples/parser/repro.roc
#
app "lazy-parser-repro"
packages { pf: "platform/main.roc" }
imports [
Parser.Core.{ Parser, map, lazy},
Parser.Str.{ parseStr, RawStr }