From 499d20c5dbbca4a036727e47c312187c3539d373 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 22 Dec 2020 20:52:18 -0600 Subject: [PATCH] some junk --- comptime_vars.zig | 21 +++++++++++++++++++++ float_foo.zig | 12 ++++++++++++ namespaced_global.zig | 6 ------ pwd.zig | 15 +++++++++++++++ 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 float_foo.zig create mode 100644 pwd.zig diff --git a/comptime_vars.zig b/comptime_vars.zig index e69de29..3f1ff39 100644 --- a/comptime_vars.zig +++ b/comptime_vars.zig @@ -0,0 +1,21 @@ +const std = @import("std"); +const expect = std.testing.expect; + +test "comptime vars" { + var x: i32 = 1; + comptime var y: i32 = 1; + + x += 1; + y += 1; + + expect(x == 2); + expect(y == 2); + + if (y != 2) { + // this compile error never triggers. y is a comptime variable, which + // makes `y != 2` knowable at comptime (would we call this a comptime + // expression?). the "if" statement is statically evaluated. I don't + // get it. + @compileError("wrong y value"); + } +} diff --git a/float_foo.zig b/float_foo.zig new file mode 100644 index 0000000..413031a --- /dev/null +++ b/float_foo.zig @@ -0,0 +1,12 @@ +const std = @import("std"); +const builtin = std.builtin; +const big = @as(f64, 1 << 40); + +export fn foo_strict(x: f64) f64 { + return x + big - big; +} + +export fn foo_optimized(x: f64) f64 { + @setFloatMode(.Optimized); + return x + big - big; +} diff --git a/namespaced_global.zig b/namespaced_global.zig index c725577..2d06ced 100644 --- a/namespaced_global.zig +++ b/namespaced_global.zig @@ -4,7 +4,6 @@ const expect = std.testing.expect; test "namespaced global variable" { expect(foo() == 1235); expect(foo() == 1236); - expect(boffin() == 1237); } fn foo() i32 { @@ -14,8 +13,3 @@ fn foo() i32 { S.x += 1; return S.x; } - -fn boffin() i32 { - S.x += 1; - return S.x; -} diff --git a/pwd.zig b/pwd.zig new file mode 100644 index 0000000..bb7e3b6 --- /dev/null +++ b/pwd.zig @@ -0,0 +1,15 @@ +const std = @import("std"); +const os = std.os; +const print = std.debug.print; + +pub fn main() !void { + var buf: [256]u8 = undefined; + var fba = std.heap.FixedBufferAllocator.init(&buf); + var allocator = &fba.allocator; + + const cwd = try allocator.alloc(u8, 256); + defer allocator.free(cwd); + + const z = try os.getcwd(cwd); + print("{}\n", .{z}); +}