Skip to content

Instantly share code, notes, and snippets.

View winksaville's full-sized avatar

Wink Saville winksaville

  • Soquel, CA. USA
View GitHub Profile
@winksaville
winksaville / gist:a094ac6fe99f7bccdf81301b2e942c0a
Created September 1, 2018 03:47
How to make a struct in zig
//$ zig test make_struct.zig
// make_struct.zig:20:7: error: no member named 'int64' in struct 'makeStruct((struct []const u8 constant),i64)'
// v1.int64 = -123;
// Seems the "name" parameter needs to be a "symbol"
// and the return would be something like:
// return struct { @symbol(name): T, };
fn makeStruct(comptime name: []const u8, comptime T: type) type {
return struct { name: T, };
}
//$ zig test opaquetype.zig
//opaquetype.zig:8:30: error: parameter of type '(anonymous opaque at opaquetype.zig:12:30)' not allowed
// fn getValue(pOpaqueSelf: @OpaqueType()) u32 {
// ^
const assert = @import("std").debug.assert;
const S = struct {
const Self = @This();
value: u32,
@winksaville
winksaville / pZeroConditional.zig
Last active September 17, 2018 23:24
Test ?*u0
const std = @import("std");
const assert = std.debug.assert;
const warn = std.debug.warn;
const builtin = @import("builtin");
const TypeId = builtin.TypeId;
test "u0" {
var zero: u0 = 0;
var pZeroOptional: ?*u0 = &zero;
assert(pZeroOptional != null); // this is true
const std = @import("std");
const assert = std.debug.assert;
test "u0-memory-address" {
var one: u1 = 1;
var zero: u0 = 0;
var pZero = &zero;
assert(pZero == &zero);
// $ zig test u0.zig
@winksaville
winksaville / gist:8758b59384c7ce0c3604d8b4f708929c
Last active September 18, 2018 17:53
compiler aborts when there is a u0 field in a packed struct
const assert = @import("std").debug.assert;
// zig: ../src/analyze.cpp:499: ZigType* get_pointer_to_type_extra(CodeGen*, ZigType*, bool, bool, PtrLen, uint32_t, uint32_t, uint32_t): Assertion `byte_alignment == 0' failed.
// Aborted (core dumped)
test "u0-packed-struct-fails" {
const S = packed struct { f0: u0, };
var s = S { .f0 = 0, };
assert(s.f0 == 0);
}
@winksaville
winksaville / gist:ac77c92619b23ad1763f267bfc078a17
Created September 23, 2018 18:07
Extending zig structs at compile time
const S1 = struct {
n: u32,
}
fn S2() type {
return S1 {
m: u64,
};
}
@winksaville
winksaville / gist:24189b220dd884c1aae53e41b2d88272
Created November 10, 2018 05:40
Test approxEql.sum.large.f64
$ zig test --test-filter approxEql.sum.large.f64 modules/zig-approxEql/approxeql.zig
Test 1/1 approxEql.sum.large.f64...
x=8.988465674311579e+307 end=8.988465674311085e+307
approxEql: x=8.988465674311579e+307 y=8.988465674311085e+307 digits=0 digits == 0 result=true
approxEql: x=8.988465674311579e+307 y=8.988465674311085e+307 digits=1 abs_diff=4.9397047660984315e+294 max_diff=1.0e+00 scaled_max_diff=8.988465674311579e+306 scaled_epsilon=1.9958403095347196e+292 result=true
approxEql: x=8.988465674311579e+307 y=8.988465674311085e+307 digits=2 abs_diff=4.9397047660984315e+294 max_diff=1.0e-01 scaled_max_diff=8.988465674311578e+305 scaled_epsilon=1.9958403095347196e+292 result=true
approxEql: x=8.988465674311579e+307 y=8.988465674311085e+307 digits=3 abs_diff=4.9397047660984315e+294 max_diff=1.0e-02 scaled_max_diff=8.988465674311578e+304 scaled_epsilon=1.9958403095347196e+292 result=true
approxEql: x=8.988465674311579e+307 y=8.988465674311085e+307 digits=4 abs_diff=4.9397047660984315e+294 max_diff=1.0e-03 scaled
@winksaville
winksaville / winksaville.asc
Created April 9, 2019 20:45
PGP armored signature file
-----BEGIN PGP PUBLIC KEY BLOCK-----
mQINBFrXsMwBEACTOyxM37EOX5IUjQoS1ApAV8jjK6cEPtPwaHMg4YnpnbdrHXXK
2E+CKr4JFJp0qJ8vm0VyUKnv/VE+dARHtVS1mIdglP0+l1pJWodLHKmxa5R7tP2x
u5/BQf3w2vhjQSgUQfd/hFOaHQpFfqRF87/9Yitz6pIuOlxZii0PDPBGDkzaTYFj
z5VOpWSFGVHIo6uGUd8DMC8lw4N0WBC08jivkclSKpJtazbmMuBMgcWwbwbAMEg3
BE6ouBG48F82UWKgVNxypTfULfV1l0aEhb/I5v33u38I/IQdM5x9k+C++teSY9FJ
y9F6kYLdzwLU0pDSJXsac9EAX5uKDe7Zn/jxWt97v4VZPPhTyc/pbuwkvF+q0Ef4
Ixa5YWBmcEFCIQl3BLc3QUdljzODJfU5KwcJjBUcv1AbWXxmFy0msdpBP5wQJYTL
M0D/q+FHQ1BAV45M0nnO4olgyDfSvbt+0ce1rEpw9hoyakIkM18ivV+2Bs+NvVZH
$ /usr/bin/c++ -v -O3 -DNDEBUG -rdynamic CMakeFiles/zig.dir/src/analyze.cpp.o CMakeFiles/zig.dir/src/ast_render.cpp.o CMakeFiles/zig.dir/src/bigfloat.cpp.o CMakeFiles/zig.dir/src/bigint.cpp.o CMakeFiles/zig.dir/src/buffer.cpp.o CMakeFiles/zig.dir/src/c_tokenizer.cpp.o CMakeFiles/zig.dir/src/cache_hash.cpp.o CMakeFiles/zig.dir/src/codegen.cpp.o CMakeFiles/zig.dir/src/compiler.cpp.o CMakeFiles/zig.dir/src/errmsg.cpp.o CMakeFiles/zig.dir/src/error.cpp.o CMakeFiles/zig.dir/src/ir.cpp.o CMakeFiles/zig.dir/src/ir_print.cpp.o CMakeFiles/zig.dir/src/libc_installation.cpp.o CMakeFiles/zig.dir/src/link.cpp.o CMakeFiles/zig.dir/src/main.cpp.o CMakeFiles/zig.dir/src/os.cpp.o CMakeFiles/zig.dir/src/parser.cpp.o CMakeFiles/zig.dir/src/range_set.cpp.o CMakeFiles/zig.dir/src/target.cpp.o CMakeFiles/zig.dir/src/tokenizer.cpp.o CMakeFiles/zig.dir/src/translate_c.cpp.o CMakeFiles/zig.dir/src/util.cpp.o -o zig -L/home/wink/local/lib -Wl,-v,--verbose,-rpath,/home/wink/local/lib::::: libzig_cpp.a libopt_c_util.a libembedded_s
@winksaville
winksaville / cmake-llvm-project-master.0b098754b73
Created April 24, 2019 06:24
cmake llvm-project master 0b098754b73f3b96d00ecb1c7605760b11c90298
$ rm -rf * .ninja_* && cmake ../llvm -G Ninja -DCMAKE_INSTALL_PREFIX=~/local.master -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;libcxx;libcxxabi;libunwind;compiler-rt;lld;lldb;polly;debuginfo-tests" -DLLVM_BUILD_LLVM_DYLIB=ON
-- The C compiler identification is GNU 8.3.0
-- The CXX compiler identification is GNU 8.3.0
-- The ASM compiler identification is GNU
-- Found assembler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features