From c87ae5ce70f226cc15c5e9996e2538fecc699079 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 20 Dec 2020 10:05:49 -0600 Subject: [PATCH] strings weird --- .gitignore | 2 ++ global_variables.zig | 14 ++++++++++++++ hello.zig | 6 +++--- iterator.zig | 9 +++++++++ optional.zig | 12 ++++++++++++ point.zig | 8 ++++++++ string_literals.zig | 14 ++++++++++++++ values.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 global_variables.zig create mode 100644 iterator.zig create mode 100644 optional.zig create mode 100644 point.zig create mode 100644 string_literals.zig create mode 100644 values.zig diff --git a/.gitignore b/.gitignore index 2040c29..dcbf096 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ zig-cache +*.exe +*.pdb diff --git a/global_variables.zig b/global_variables.zig new file mode 100644 index 0000000..bfb3cc1 --- /dev/null +++ b/global_variables.zig @@ -0,0 +1,14 @@ +var y: i32 = add(10, x); +const x: i32 = add(12, 34); + +test "global variables" { + assert(x == 46); + assert(y == 56); +} + +fn add(a: i32, b: i32) i32 { + return a + b; +} + +const std = @import("std"); +const assert = std.debug.assert; diff --git a/hello.zig b/hello.zig index 98ed35a..6f68d6f 100644 --- a/hello.zig +++ b/hello.zig @@ -1,6 +1,6 @@ const std = @import("std"); -pub fn main() void { - std.debug.print("Hello, {}!\n", .{"World"}); +pub fn main() !void { + const stdout = std.io.getStdOut().writer(); + try stdout.print("Hello, {}!\n", .{"World"}); } - diff --git a/iterator.zig b/iterator.zig new file mode 100644 index 0000000..ea3d370 --- /dev/null +++ b/iterator.zig @@ -0,0 +1,9 @@ +const std = @import("std"); + +pub fn main() void { + const msg = "hello this is dog"; + var words = std.mem.tokenize(msg, " "); + while (words.next()) |word| { + std.debug.print("{}\n", .{word}); + } +} diff --git a/optional.zig b/optional.zig new file mode 100644 index 0000000..bd407bd --- /dev/null +++ b/optional.zig @@ -0,0 +1,12 @@ +const std = @import("std"); +const assert = std.debug.assert; + +test "null @intToPtr" { + // the ? means that the pointer can be null. If you remove it, the test + // fails because you're trying to set a null pointer. + // + // the 0 can also be written 0x0 but I don't know if there's any practical + // difference. + const ptr = @intToPtr(?*i32, 0); + assert(ptr == null); +} diff --git a/point.zig b/point.zig new file mode 100644 index 0000000..cac1cb4 --- /dev/null +++ b/point.zig @@ -0,0 +1,8 @@ +const Point = struct { + x: i32, + y: i32, + + pub fn mag() f32 { + return 0; + } +}; diff --git a/string_literals.zig b/string_literals.zig new file mode 100644 index 0000000..af6bdc1 --- /dev/null +++ b/string_literals.zig @@ -0,0 +1,14 @@ +const expect = @import("std").testing.expect; +const mem = @import("std").mem; + +test "string literals" { + const bytes = "hello"; + expect(@TypeOf(bytes) == *const [5:0]u8); + expect(bytes.len == 5); + expect(bytes[1] == 'e'); + expect(bytes[5] == 0); + expect('e' == '\x65'); + expect('\u{1f4a9}' == 128169); + expect('💯' == 128175); + expect(mem.eql(u8, "hello", "h\x65llo")); +} diff --git a/values.zig b/values.zig new file mode 100644 index 0000000..d77d5f8 --- /dev/null +++ b/values.zig @@ -0,0 +1,43 @@ +const std = @import("std"); +const print = std.debug.print; +const os = std.os; +const assert = std.debug.assert; + +pub fn main() void { + // integers + const one_plus_one: i32 = 1 + 1; + print("1 + 1 = {}\n", .{one_plus_one}); + + // floats + const seven_div_three: f32 = 7.0 / 3.0; + print("7.0 / 3.0 = {}\n", .{seven_div_three}); + + // booleans + print("{}\n{}\n{}\n", .{ + true and false, + true or false, + !true, + }); + + // optional hmm + var optional_value: ?[]const u8 = null; + assert(optional_value == null); + + print("\noptional 1\ntype: {}\nvalue: {}\n", .{ + @typeName(@TypeOf(optional_value)), + optional_value, + }); + + // error union + var number_or_error: anyerror!i32 = error.ArgNotFound; + print("\nerror union 1\ntype: {}\nvalue: {}\n", .{ + @typeName(@TypeOf(number_or_error)), + number_or_error, + }); + + number_or_error = 1234; + print("\nerror union2\ntype: {}\nvalue: {}\n", .{ + @typeName(@TypeOf(number_or_error)), + number_or_error, + }); +}