From 4ec36e3cf01677f5dedb55010342a2382d095578 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 19 Dec 2020 12:08:46 -0600 Subject: [PATCH] hey --- .gitignore | 1 + hello.zig | 6 ++++ if.zig | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 .gitignore create mode 100644 hello.zig create mode 100644 if.zig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2040c29 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +zig-cache diff --git a/hello.zig b/hello.zig new file mode 100644 index 0000000..98ed35a --- /dev/null +++ b/hello.zig @@ -0,0 +1,6 @@ +const std = @import("std"); + +pub fn main() void { + std.debug.print("Hello, {}!\n", .{"World"}); +} + diff --git a/if.zig b/if.zig new file mode 100644 index 0000000..691bea3 --- /dev/null +++ b/if.zig @@ -0,0 +1,94 @@ +const expect = @import("std").testing.expect; + +test "if statement" { + const a = true; + var x: u16 = 0; + if (a) { + x += 1; + } else { + x += 2; + } + expect(x == 1); +} + +test "if statement expression" { + const a = true; + var x: u16 = 0; + x += if (a) 1 else 2; + expect(x == 1); +} + +test "while" { + var i: u8 = 2; + while (i < 100) { + i *= 2; + } + expect(i == 128); +} + +test "while with continue expression" { + var sum: u8 = 0; + var i: u8 = 1; + while (i <= 10) : (i += 1) { + sum += i; + } + expect(sum == 55); +} + +test "while with continue" { + var sum: u8 = 0; + var i: u8 = 0; + while (i <= 3) : (i += 1) { + if (i == 2) continue; + sum += i; + } + expect(sum == 4); +} + +test "while with break" { + var sum: u8 = 0; + var i: u8 = 0; + while (i <= 3) : (i += 1) { + if (i == 2) break; + sum += i; + } + expect(sum == 1); +} + +test "for" { + const string = [_]u8{ 'a', 'b', 'c' }; + + for (string) |character, index| {} + for (string) |character| {} + for (string) |_, index| {} + for (string) |_| {} +} + +fn addFive(x: u32) u32 { + return x + 5; +} + +test "function" { + const y = addFive(0); + expect(@TypeOf(y) == u32); + expect(y == 5); +} + +fn fibonacci(n: u16) u16 { + if (n == 0 or n == 1) return n; + return fibonacci(n - 1) + fibonacci(n - 2); +} + +test "function recursion" { + const x = fibonacci(10); + expect(x == 55); +} + +test "defer" { + var x: i16 = 5; + { + defer x += 2; + expect(x == 5); + } + expect(x == 7); +}