This commit is contained in:
nothke
2024-08-08 20:39:18 +02:00
commit 64be90de80
11 changed files with 726 additions and 0 deletions

43
src/main.zig Normal file
View File

@@ -0,0 +1,43 @@
const std = @import("std");
fn calc(a: i32) i32 {
return a;
}
const SOmethin = enum {
One,
Two,
Three,
};
fn Optional(comptime T: type) type {
return struct {
valid: bool,
value: T,
};
}
fn add(a: anytype, b: anytype) @TypeOf(a + b) {
return a + b;
}
const DivisionError = error{DivisionByZero};
fn divide(a: f32, b: f32) !f32 {
if (b == 0)
return DivisionError.DivisionByZero;
return a / b;
}
pub fn main() !void {
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
const vec = @Vector(4, i32){ 1, 0, 1, 0 };
const vec2 = @Vector(4, i32){ 0, 1, 0, 1 };
std.debug.print("vec: {}", .{vec + vec2});
}

126
src/main2.zig Normal file
View File

@@ -0,0 +1,126 @@
const std = @import("std");
const Other = @import("other.zig");
const WRB = @import("rbuff.zig").WrappingRingBuffer;
const Vector2D = packed struct {
x: i5 = 2,
y: i23,
fn name(self: Vector2D) void {
_ = self; // autofix
}
fn init() void {}
fn deinit() void {}
};
fn add(a: *i32, b: i32) i32 {
a.* = 3;
return a.* + b;
}
const DivideError = error{
DivivisionError,
};
fn divide(a: f32, b: f32) !f32 {
if (b == 0) {
return DivideError.DivivisionError;
} else {
return a / b;
}
}
fn Optional(comptime T: type) type {
return struct {
value: T,
exists: bool,
};
}
const Person = struct {
name: []const u8,
};
const Horse = struct {
canRide: bool = false,
rider: ?*const Person = null,
};
const Duck = struct {
friend: ?*Animal = null,
gone: bool = false,
};
const AnimalType = enum {
Horse,
Duck,
Person,
};
const Animal = union(AnimalType) {
Horse: Horse,
Duck: Duck,
Person: Person,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
defer _ = gpa.deinit();
var list = std.ArrayList(i32).init(alloc);
defer list.deinit();
try list.append(324);
try list.append(456);
for (list.items) |item| {
std.log.debug("item: {}", .{item});
}
var animal = Animal{ .Horse = .{ .canRide = true } };
const person = Person{ .name = "Dragutin" };
if (animal == .Horse) {
animal.Horse.rider = &person;
}
switch (animal) {
.Horse => |*horse| horse.rider = &person,
else => {},
}
var personAnimal = Animal{ .Person = .{ .name = "Aleksa" } };
var duck = Duck{ .friend = &personAnimal };
duck.gone = true;
std.log.debug("Rider name: {s}", .{if (animal.Horse.rider) |rider| rider.name else "none"});
{
std.log.info("Ring buffer!", .{});
var rbuff = try WRB(i32).initCapacity(alloc, 32);
defer rbuff.deinit();
if (rbuff.isEmpty())
try rbuff.pushBack(14);
try rbuff.pushBack(16);
try rbuff.pushBack(2);
const front = rbuff.frontPtr();
front.* = 333;
var iter = rbuff.iter();
while (iter.next()) |*value| {
std.log.info("rbuff value: {}", .{value});
}
std.log.info("the end!", .{});
}
}

9
src/other.zig Normal file
View File

@@ -0,0 +1,9 @@
const main = @import("main.zig");
pub const pi = 3.14;
pub fn add() i32 {
try main.main();
return 3;
}